Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come a 32 bit kernel can run a 64 bit binary?

On my OS X box, the kernel is a 32 bit binary and yet it can run a 64 bit binary. How does this work?

 cristi:~ diciu$ file ./a.out ./a.out: Mach-O 64-bit executable x86_64 cristi:~ diciu$ file /mach_kernel /mach_kernel: Mach-O universal binary with 2 architectures /mach_kernel (for architecture i386):   Mach-O executable i386 /mach_kernel (for architecture ppc):    Mach-O executable ppc cristi:~ diciu$ ./a.out cristi:~ diciu$ echo $? 1 
like image 708
diciu Avatar asked Sep 08 '08 15:09

diciu


People also ask

Why can't 32-bit run 64-bit?

Usually, you can't directly run a 64-bit program on a 32-bit system for a 32-bit system can't provide the necessary resources and RAM a 64-bit app requires.

Can 32-bit run 64-bit OS?

Both a 32 and 64 bit OS can run on a 64 bit processor, but the 64 bit OS can use full-power of the 64bit processor (larger registers, more instructions) - in short it can do more work in same time. A 32 bit processor supports only 32 bit Windows OS. This is incorrect generally, though correct for x64 specifically.

How can I tell if binary is 32 or 64-bit?

If you are on Windows 7, on a Windows Explorer, right click on the executable and select Properties. At the properties window select the Compatibility tab. If under the Compatibility Mode section you see Windows XP, this is a 32 bit executable. If you see Windows Vista, it is 64 bit.


2 Answers

The CPU can be switched from 64 bit execution mode to 32 bit when it traps into kernel context, and a 32 bit kernel can still be constructed to understand the structures passed in from 64 bit user-space apps.

The MacOS X kernel does not directly dereference pointers from the user app anyway, as it resides its own separate address space. A user-space pointer in an ioctl call, for example, must first be resolved to its physical address and then a new virtual address created in the kernel address space. It doesn't really matter whether that pointer in the ioctl was 64 bits or 32 bits, the kernel does not dereference it directly in either case.

So mixing a 32 bit kernel and 64 bit binaries can work, and vice-versa. The thing you cannot do is mix 32 bit libraries with a 64 bit application, as pointers passed between them would be truncated. MacOS X supplies more of its frameworks in both 32 and 64 bit versions in each release.

like image 200
DGentry Avatar answered Oct 09 '22 11:10

DGentry


It's not the kernel that runs the binary. It's the processor.

The binary does call library functions and those need to be 64bit. And if they need to make a system call, it's their responsibility to cope with the fact that they themselves are 64bit, but the kernel is only 32.

But that's not something you would have to worry about.

like image 32
pilif Avatar answered Oct 09 '22 11:10

pilif