When reading the "entering protected mode" section I started to question my own understanding of some the details. My first question is:
When a CPU starts up in real mode it's running in a kind of privileged state since there are none of the 4 protection rings that protected mode give us. We then build a GDT and switch the CPU to protected mode. In practical terms where does this code live GDT building and long mode jump code live? Is it in GRUB or whichever stage 2 bootloader is in use?
My second question was about the "chicken and egg" situation about how the kernel code inherits that privileged address space defined marked with ring 0 bits in the GDT. Is magic that once the GDT has been defined, when we subsequently decompresses the kernel into those memory addresses the kernel memory will have ring 0 bits in its descriptors?
This is how I've understood it but I've often found the texts about this a little murky and this part still feels a bit like magic to me I guess.