Does GRUB switch to protected mode?
Asked Answered
D

3

21

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.

Dibromide answered 27/1, 2011 at 21:10 Comment(3)
Are you asking about grub legacy or grub2?Chronic
I am interested in both versions of grub.Dibromide
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).Maryammaryann
L
19

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.

Lethargic answered 18/6, 2011 at 14:21 Comment(10)
+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).Steersman
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.Lethargic
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.Steersman
i 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 (?)Lethargic
Well, I am pretty sure the entry point to Linux is header.S.Steersman
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?Lethargic
OK, I think I found it. GRUB mostly runs in protected mode, but it drops back to real mode 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. That's the hook.Steersman
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.Lethargic
Modern 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.Purposeful
a simple test of cr0 in a kernel launched via multiboot suggests that bit 0 is not set when the kernel is invoked...?Searle
S
0

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.

https://en.wikipedia.org/wiki/Unreal_mode

Searle answered 17/9, 2019 at 20:22 Comment(1)
Well look, I RTFMd properly. The answers to all your questions are here; gnu.org/software/grub/manual/multiboot/html_node/…Searle
M
-1

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

Maryammaryann answered 27/1, 2011 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.