OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Apr 15, 2024 9:29 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GDT after enabling paging
PostPosted: Sat Jun 06, 2020 8:18 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
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.


Top
 Profile  
 
 Post subject: Re: GDT after enabling paging
PostPosted: Sat Jun 06, 2020 8:58 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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


Top
 Profile  
 
 Post subject: Re: GDT after enabling paging
PostPosted: Sat Jun 06, 2020 9:27 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5134
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.


Top
 Profile  
 
 Post subject: Re: GDT after enabling paging
PostPosted: Sat Jun 06, 2020 9:47 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
Awesome, thanks everyone for the responses!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], ComputerFido, Google [Bot], Majestic-12 [Bot] and 354 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group