GDT after enabling paging

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

GDT after enabling paging

Post by 8infy »

Hi, I recently moved my kernel into higher half.
However, since my GDT is stored in the first megabyte (generated by my bootloader) after enabling paging that address refers to a page (directory) that is not present.
So I'm now creating a new GDT in the entrypoint of my kernel.

Here are a few questions that I have:

1. Why does having an invalid GDT pointer only triple fault whenever an IRQ happens and not when executing random code, even with jmps in it?
(if I don't ever do sti it seems to work fine on all virtual machines i've tested on: VMWare, bochs, qemu)

2. When is GDT actually accessed by the CPU and how far can you go with it being invalid?

3. When I create a new GDT and load it, do I need to reload the registers (by jumping etc) if the offsets are still the same? (e.g 0x08 for code and 0x10 for data)

Thanks.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: GDT after enabling paging

Post by nexos »

1. The reason that it triple faults when an interrupt occurs is because the processor reloads the segment registers during an IRQ
2. The processor caches descriptors, and how far you can go depends
3. Yes, so the processor will refresh its descriptor cache

I would recommend what you do is load the GDT first thing before anything, except maybe a serial port driver for debugging
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Octocontrabass
Member
Member
Posts: 5575
Joined: Mon Mar 25, 2013 7:01 pm

Re: GDT after enabling paging

Post by Octocontrabass »

8infy wrote:1. Why does having an invalid GDT pointer only triple fault whenever an IRQ happens and not when executing random code, even with jmps in it?
Your random code doesn't reload any segment registers, so the CPU can keep running with cached segments. IRQs involve loading the CS register with the selector in your IDT, and loading any segment register typically requires the GDT to be present. (The only exceptions to this are the "fast system call" instructions.)
8infy wrote:2. When is GDT actually accessed by the CPU and how far can you go with it being invalid?
Other than the fast system call instructions, anything that causes the CPU to load a segment register will involve accessing the GDT. That includes obvious things like "MOV DS, AX" as well as implicit things like exceptions and IRQs.

As long as you never do anything that requires accessing the GDT, you can go forever with the cached values. (It would be very inconvenient.)
8infy wrote:3. When I create a new GDT and load it, do I need to reload the registers (by jumping etc) if the offsets are still the same? (e.g 0x08 for code and 0x10 for data)
No, but it's probably a good idea to do it anyway.
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Re: GDT after enabling paging

Post by 8infy »

Awesome, thanks everyone for the responses!
Post Reply