0

I read that the bootloader (Grub, or U-boot etc. ) locates the kernel (from maybe the HDD, or even flash memory if it's an embedded system) and loads it into memory on startup.

Now, does this mean that the bootloader loads the kernel fully into available RAM and then passes control to it? My guess is that the kernel will not fully fit into the RAM and so there will be swapping of the kernel instructions from RAM to the storage device and vice-versa.

What I don't understand is, the bootloader has stopped executing at this stage and the kernel has taken over, so how can the bootloader take care about loading the rest of the kernel in and out of memory if it doesn't fully fit at first?

1

The kernel is fully loaded into RAM on startup, and it is not swapped. The kernel does not know and does not care where it is loaded from. It could be loaded from the network, and it is hard to swap over tftp.

  • You could sell t-shirts with “it is hard to swap over tftp”! 🤣 – Stephen Kitt Dec 19 '18 at 7:07
  • Where does virtual memory come into all this? – Engineer999 Dec 19 '18 at 21:57
0

It's more accurate to say that the bootloader loads the entirety of what it's told to load as the kernel into memory. it doesn't really have to be the kernel itself, it could be a second-stage bootloader (and in most cases, this is actually what's technically happening, because GRUB jumps to a decompression routine that extracts the real kernel), or it could be a type-1 hypervisor which then loads and executes the Linux kernel.

Put simply, it all has to fit in memory and have enough space left over for things like userspace, or the system can't boot (this is part of why it's so hard to build a Linux system that runs with less than about 64M of RAM these days). The same can be said about every other UNIX system I know of (SVR4, BSD, Solaris, IRIX, Ultrix, Xenix, etc). It's just too much of a pain to handle swapping of memory in kernel-space for it to be worth implementing.

  • could you explain where virtual memory and the MMU comes into all this then? – Engineer999 Dec 19 '18 at 21:58
  • The kernel itself provides all the abstractions for virtual memory, both those used internally and those for userspace. GRUB intentionally doesn't use virtual memory (because it would have to stick around in the background to keep providing it, which is not what a bootloader is for), and a lot of the early startup code in the kernel doesn't either because there are somethings that really do just need to be done before setting up virtual memory. – Austin Hemmelgarn Dec 20 '18 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.