Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 1, 2013, 06:09 AM   #1
blueshogun96
macrumors newbie
 
Join Date: Nov 2012
Using mmap() in OSX

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;
mmap( memptr, 0xFFFFFFFF, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED, -1, 0 );

Would this work?
blueshogun96 is offline   0 Reply With Quote
Old Jan 1, 2013, 08:07 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by blueshogun96 View Post
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;
mmap( memptr, 0xFFFFFFFF, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED, -1, 0 );

Would this work?
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.
gnasher729 is offline   0 Reply With Quote
Old Jan 1, 2013, 12:20 PM   #3
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Have you read through Apple's docs?

https://developer.apple.com/library/...ing/vm/vm.html
robvas is offline   0 Reply With Quote
Old Jan 1, 2013, 08:13 PM   #4
blueshogun96
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by gnasher729 View Post
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.
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.

Quote:
Originally Posted by robvas View Post
Have you read through Apple's docs?

https://developer.apple.com/library/...ing/vm/vm.html
I don't think I've seen this yet. Thanks.
blueshogun96 is offline   0 Reply With Quote
Old Jan 3, 2013, 03:00 AM   #5
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by blueshogun96 View Post
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.
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.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Jan 3, 2013, 08:08 PM   #6
blueshogun96
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by Cromulent View Post
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.
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.
blueshogun96 is offline   0 Reply With Quote
Old Jan 4, 2013, 03:39 AM   #7
mfram
macrumors 6502a
 
Join Date: Jan 2010
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).
mfram is offline   0 Reply With Quote
Old Jan 4, 2013, 01:07 PM   #8
firewood
macrumors 603
 
Join Date: Jul 2003
Location: Silicon Valley
How do virtual machines such as virtualbox, parallels and vmware work under Mac OS X?
firewood is offline   0 Reply With Quote
Old Jan 4, 2013, 01:56 PM   #9
mfram
macrumors 6502a
 
Join Date: Jan 2010
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.
mfram is offline   0 Reply With Quote
Old Jan 4, 2013, 07:09 PM   #10
blueshogun96
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by mfram View Post
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).
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.

Quote:
Originally Posted by firewood View Post
How do virtual machines such as virtualbox, parallels and vmware work under Mac OS X?
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.
blueshogun96 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 03:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC