1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Using mmap() in OSX

Discussion in 'Mac Programming' started by blueshogun96, Jan 1, 2013.

  1. macrumors member

    I haven't found much information on Mac OSX's memory layout (who am I kidding, I haven't found a darn thing), so I was hoping that someone would be able to shed some light on this.

    This is going to be a rather "off the wall" request, but I want to use mmap() to claim the first 4GB of the 64-bit address space in Mac OSX (more specifically, the memory range of 0x10000 - 0xFFFFFFFF). I'm assuming that it may be impossible to get access to that memory range. In Windows, I can at least get that specific base address with a few hacky methods, but I'm still rather new to MacOSX and UNIX altogether.

    I was thinking of trying something like this:

    void* memptr = (void*) 0x10000;

    Would this work?
  2. macrumors G5


    In MacOS X 64 bit, the first four Gigabyte of address space are always unmapped. The idea is that casting a 32 bit int to a pointer should be guaranteed to crash. I wouldn't think you can get around that, and you'd better tell us what you are actually trying to achieve.
  3. macrumors 68000

  4. macrumors member

    I'm writing an emulator/VM in an attempt to emulate the P6 architecture (Pentium III to be specific). To be more specific, I'm writing an hardware level Xbox emulator. I've managed to get this technique to work on Windows before.

    I've already taken multiple precautions into account here (non-encodable instructions, mmio, privileged registers, exception handling, etc.), so now I needed to make sure that the first 4GB of address space are available for usage in 64-bit.

    I don't think I've seen this yet. Thanks.
  5. macrumors 603


    Forgive me if I am misunderstanding something here but why don't you just write your own memory access functions that take an address in the first 4GBs of memory space and converts it to the actual address that Mac OS X has allocated you? Some sort of memory address translation. That would solve the problem at hand, would work on any operating system and means you don't need to do any sort of hacky stuff.
  6. macrumors member

    I have thought about this, and quite frankly, it's not that simple to do in this particular case. Example, Xbox games most commonly reference absolute addresses and make absolute calls/jumps. Relative addresses are rarely used. Another example is that Xbox games are fixed at a very specific base address, and so far, using this particular method, no one has ever gotten this to work unless we have gotten access to the specific memory range.

    It sounds like a good idea, but it really creates more hurdles to deal with.
  7. macrumors 6502a

    You aren't quite understanding. Treat the VM as virtual memory. To the VM side, it will see the 32bit address space. But when you store the data in your program, you store it at a base address pus the 32bit address. Then you ask the OS to give you a 4GB block of address space. You don't care where it is. Just store the base address and do translations on every VM access. The is essentially how virtual memory works (with some more complications).
  8. macrumors 603

    How do virtual machines such as virtualbox, parallels and vmware work under Mac OS X?
  9. macrumors 6502a

    Virtualbox is open source, so you can download the source and take a look. But in that case, they are emulating the x86 processor so there would be a whole lot more complexity in the memory mapping code. The memory mapping the O.P. is describing sounds a lot simpler.
  10. macrumors member

    I do understand what you're saying. Although this is a bit more clear, this is what I'd rather avoid. It can be done, but it's much more work than it sounds. If that's the only way to do it, then I guess I have no choice.

    They use virtualization instructions (processor specific), such as Intel VMX. VMX is a special instruction set used to create virtual machines, but it's a rather complex instruction set and using it doesn't appear to be very straightforward.

Share This Page