JJang
JJang

Reputation: 9

How to directly access physical addresses or convert to virtual address on macOS for Apple silicon?

I am trying to read data using a physical address on Apple Silicon MacOS. I think the best way is to convert the physical address to a virtual address and access it, or access it directly with the physical address.

I guess I could achieve this with IOKit (or kext). However, I couldn't find any documentation about the API or Syscall related to this. Could I please get some advice on this?

Thanks in advance.

Upvotes: 0

Views: 242

Answers (1)

Siguza
Siguza

Reputation: 23820

From a kext, you can use IOMemoryDescriptor::withPhysicalAddress() to create an IOMemoryDescriptor, which you can then map into any address space you want, either via map() for the kernel's address space, or createMappingInTask() for userland processes.

Example:

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();

I wrote a kext not long ago that does exactly this, and exposes this functionality to userland via IOKit external methods, along with some convenience wrappers.

Upvotes: 1

Related Questions