3

The OS X Lion (10.7) OS runs on mostly 64-bit binaries as reported by Activity Monitor. Given this, and the fact that my laptop runs a 32-bit version of the EFI and thus also a 32-bit kernel, how does the arch mixing work in general?

Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386

Normally one would run 32b binaries on x86_64, but the other way around would require pushing the cpu into 64b mode, which AFAIK cannot be undone.

Hope this question is clear enough..

overscore
  • 513
  • 4
  • 15

2 Answers2

4

The x86-64 architecture is pretty complex; it doesn't just have a 64-bit mode and a 32-bit mode, it has two main modes (long and legacy), each with a number of sub-modes (see the Wikipedia article).

In Legacy mode, the CPU essentially emulates a 32-bit CPU. It has various sub-modes (real, protected, etc), but no ability to switch use 64-bit instructions or addressing. Generally, a non-64-bit-capable OS will run in this mode.

In Long mode, the CPU has 64-bit capability, but can also run in 32- and 16-bit "compatibility" modes. The mode switching is controlled by the L and D flags in the code segment descriptor (see "Extending x86 for the 64-bit World" in this PDF) -- essentially, different sections of memory can be marked as containing 64-, 32-, or 16-bit code, and the CPU switches into the appropriate mode for the code segment it's currently running. The kernel's code segments can be flagged as 64- or 32-bit independently of the code segments for running applications.

So in principle, it's simple. In practice, I'm sure there are loads of complications I'm not aware of (I don't really know that much about the context switching process), but as long as the OS "knows" it's running on a 64-bit CPU and configures the code segment descriptors appropriately, there's no fundamental problem with running 64-bit processes under a 32-bit kernel.

BTW, OS X could also run 64-bit processes under a 32-bit kernel on PowerPC G5 CPUs, as far back as version 10.3. PPC CPUs have a completely different architecture, and I have no idea how the mode-switching worked there.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
1

The EFI and the kernel are two separate things...
You can have a 32bits EFI with a 64bits kernel. Not the point here...

Lions' kernel usually runs is 64bits mode by default, unless you force it to run in 32bits mode.

What you call «arch mixing» is guaranteed by the x86_64 standard.
64bits mode is fully backwards compatible with 32bits code.

Running 64bits code with 32bits mode is not possible.
But Apple's Mach-O format solves this, as you can build binaries which provides 32bits AND 64bits code.

EDIT

Apparently, Mac OS X does not have an overall 32/64bits CPU mode.
I really don't know how this is possible, and how it manages the stuff internally, with the CPU.

But it seems every program is run under its «best possible mode».
So I was just wrong, 64bits native code can run with a 32bits kernel...

Will try to look further... : )

Macmade
  • 52,708
  • 13
  • 106
  • 123
  • Thanks for the answer. Clearly my system always boots the 32-bit kernel, as reported by uname. My question was more about how it is possible for 64b code to run on a 32b kernel. I know the converse situation is common place ie on Linux, Win, etc – overscore Mar 30 '12 at 22:55