我正在尝试使用 Apple Silicon MacOS 上的物理地址读取数据。 我觉得最好的办法就是把物理地址转成虚拟地址再访问,或者直接用物理地址访问。
我想我可以使用 IOKit(或 kext)来实现这一点。 但是,我找不到任何与此相关的 API 或系统调用的文档。我可以就此得到一些建议吗?
提前致谢。
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();