如何在Apple Silicon的macOS上直接访问物理地址或转换为虚拟地址?

问题描述 投票:0回答:1

我正在尝试使用 Apple Silicon MacOS 上的物理地址读取数据。 我觉得最好的办法就是把物理地址转成虚拟地址再访问,或者直接用物理地址访问。

我想我可以使用 IOKit(或 kext)来实现这一点。 但是,我找不到任何与此相关的 API 或系统调用的文档。我可以就此得到一些建议吗?

提前致谢。

macos apple-silicon iokit kernel-extension
1个回答
0
投票

在 kext 中,您可以使用

IOMemoryDescriptor::withPhysicalAddress()
创建
IOMemoryDescriptor
,然后可以将其映射到您想要的任何地址空间,无论是通过
map()
用于内核地址空间,还是通过
createMappingInTask()
用于用户态进程.

示例:

IOMemoryDescriptor *desc = IOMemoryDescriptor::withPhysicalAddress((IOPhysicalAddress)0x12345678, 0x4000, kIODirectionInOut);
if(!desc)
{
    // [error handling]
}
IOMemoryMap *map = desc->map(kIOMapInhibitCache); // kIOMapInhibitCache is only needed for MMIO, for DRAM you can pass 0x0
if(!map)
{
    // [error handling]
}
void *ptr = (void*)map->getVirtualAddress();

// [access memory through ptr]

map->release();
desc->release();

我不久前编写了一个 kext 正是这样做的,并通过 IOKit 外部方法以及一些方便的包装器向用户公开此功能。

© www.soinside.com 2019 - 2024. All rights reserved.