I would like to ask if it is GRUB that switch the CPU to protected mode during boot up or is it the Linux kernel that does it. And also I would like to ask - is the kernel itself (vmlinuz) an ELF or is it plain binary format? Thanks.
-
Are you asking about grub legacy or grub2? – Ben Voigt Jan 27 '11 at 21:24
-
1I am interested in both versions of grub. – mnc Jan 28 '11 at 15:30
-
I think it is safe to assume because both versions of Grub and LILO would work exactly the same in terms of your questions (because it is the same kernel they are booting up). – MK. Jan 28 '11 at 19:56
3 Answers
GRUB does drop you in protected mode.
The GRUB Multiboot Specification (version 0.6.96) Section 3.2 tells you this
‘CR0’
Bit 31 (PG) must be cleared. Bit 0 (PE) must be set. Other bits are all undefined.
And CR0 Register mapping tells you that the system should be in protected mode.
Linux is not a multiboot kernel and does not rely on some bootloader for switching in the PM, it follows the Linux Boot Protocol. But linux does the protected mode switch itself, and does not rely in the bootloader
check : http://lxr.linux.no/#linux+v2.6.39/arch/x86/boot/main.c
Here it calls go_to_protected_mode();
when then calls protected_mode_jump ()
which then does the CR0
stuff (sets the bit 0)
(The other bit says that paging is disabled)
EDIT
What i can figure out is that GRUB can detect linux boot protocol (GRUB2, and legacy should also) and loads linux on memory, but does not switch to protected mode. Have a look at this link: http://www.gnu.org/software/grub/manual/grub.html#GNU_002fLinux and section 16 of the page in this link.
-
+1 for a correct answer. His question is tagged "linux", though, so you might consider rewriting your first line as "On other operating systems, GRUB _does_ drop you in protected mode, but not on Linux" (or similar). – Nemo Jun 18 '11 at 16:18
-
actually i am researching on it to defend the above line, what exactly happens, and there are some confusion. but there is no confusion that linux makes the switch when linux is booted. And for other multiboot compliant kernels , grub makes the switch. – phoxis Jun 18 '11 at 16:45
-
Fair enough. I am certain Linux starts off in real mode because I once had to patch that code. Regardless, when you get a complete answer, I think you should open with the answer for Linux. – Nemo Jun 18 '11 at 16:54
-
1i am finding the answer for appx say 3 hrs now (reading specs, linux soruces, freenode channels etc, no definite answer), last resort is to check code. i think you can help me (?) – phoxis Jun 18 '11 at 16:56
-
Well, I am pretty sure the entry point to Linux is [header.S](http://lxr.linux.no/linux+v2.6.39/arch/x86/boot/header.S). – Nemo Jun 18 '11 at 17:00
-
the question is, that when linux kernel is not multiboot compliant, then how GRUB loads it. Does it detect the linux boot protocol ? and if GRUB loads linux in the memory, then the system is in protected mode already. Then linux tries to enable protected mode again? and if yes then how the real mode codes work in this case? – phoxis Jun 18 '11 at 17:14
-
2OK, I think I found it. GRUB mostly runs in protected mode, but it [drops back to real mode](http://bzr.savannah.gnu.org/lh/grub/trunk/grub/annotate/head:/grub-core/lib/i386/relocator16.S) when it invokes the kernel. The implementation for grub_linux_cmd (which implements the "linux" grub.conf command) calls grub_loader_set() with [grub_linux16_boot](http://bzr.savannah.gnu.org/lh/grub/trunk/grub/annotate/head:/grub-core/loader/i386/pc/linux.c#L82). That's the hook. – Nemo Jun 18 '11 at 18:11
-
Great point out. I think this is the story for GRUB2. But what about GRUB legacy ? It does not have "linux" command. Probably it makes some kind of auto discovery. – phoxis Jun 19 '11 at 06:01
-
2Modern Linux kernel actually works with disregard whether CPU in real or protected mode. Because, although, with BIOS systems kernel would have to switch the mode themselves, but (U)EFI systems switch to protected mode long before execution comes till kernel or grub. – Hi-Angel Jul 06 '16 at 05:20
-
a simple test of cr0 in a kernel launched via multiboot suggests that bit 0 is *not* set when the kernel is invoked...? – SonarJetLens Sep 17 '19 at 06:11
According to http://www.moses.uklinux.net/patches/lki-1.html Linux kernel is an ELF binary. I'm pretty sure that it is Linux that switches to protected mode, not the boot loader. This page agrees: http://oss.sgi.com/LDP/HOWTO/Kernel-HOWTO/linux_boot_process.html

- 33,605
- 18
- 74
- 111
I would suggest that the answer here is that GRUB loader switches to "unreal mode" before loading the kernel (which explains why CR0 bit 0 isn't set at that point.) Full 32 bit addressing is enabled, a flat GDT is set up for [0,4Gigs>, the A20 line is enabled, but paging isn't enabled so the Linux kernel (or any other kernel) will still have to do that and switch back to protected mode.

- 386
- 1
- 9
-
Well look, I RTFMd properly. The answers to all your questions are here; https://www.gnu.org/software/grub/manual/multiboot/html_node/Machine-state.html#Machine-state – SonarJetLens Oct 30 '19 at 06:48