OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Triple fault when GDT size < 23
PostPosted: Mon Aug 05, 2019 10:52 pm 
Offline

Joined: Thu Oct 25, 2012 9:21 am
Posts: 8
I'm working on my toy kernel as an UEFI appication with GCC, gnu-efi and test with QEMU+OVMF and just ran into a weird issue with GDT.
After calling SystemTable->BootServices->ExitBootServices() and SystemTable->RuntimeServices->SetVirtualAddressMap(), I copied my kernel image to a fixed address and tried to load my own GDT instead of using the UEFI one (which has 70-ish entries).
However, whenever I attempt to load a data segment register (like mov %ax, %ds) or run lretq to load CS, a triple fault is thrown. After a few days' trial-and-error I found this was directly related to the limit value in GDTR. If its set to any value < 23, a triple fault occurs on segment register load. I'm fine with keeping a large GDT but still really curious about the reason, because I don't recall such a restriction on GDT size anywhere. Can anyone explain this to me? Thanks.

By the way, it's a 64 bit kernel and UEFI starts it in protected long mode.

Code:
struct gdtr {
  uint16_t size;
  uint64_t addr;
} __attribute__((packed));


  jos_gdt[1] = create_descriptor(0, 0xfffff, GDT_CODE_PL0);
  jos_gdt[2] = create_descriptor(0, 0xfffff, GDT_DATA_PL0);
  struct gdtr gdt;
  gdt.addr = (uint64_t)jos_gdt;
  // Triple fault if size < 23
  gdt.size = 24;


Top
 Profile  
 
 Post subject: Re: Triple fault when GDT size < 23
PostPosted: Tue Aug 06, 2019 12:12 am 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
That sounds like the correct behavior to me.

Why do you think it's wrong?


Top
 Profile  
 
 Post subject: Re: Triple fault when GDT size < 23
PostPosted: Tue Aug 06, 2019 1:06 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
Remember, size != count.

Also, why are you setting it to 24 and not 23?

I guess a better name would have been LIMIT instead of SIZE (due to the -1), though COUNT might have been even better, given fixed size per element and it would have allowed more elements, though that's a moot point these days due to x86_64.


Top
 Profile  
 
 Post subject: Re: Triple fault when GDT size < 23
PostPosted: Tue Aug 06, 2019 10:17 am 
Offline

Joined: Thu Oct 25, 2012 9:21 am
Posts: 8
Doh... It's been too long since I dived into the low level world last time. I thought the field would be number of GDT entries but it should actually be bytes (-1). :oops: Sorry for the stupid question


Top
 Profile  
 
 Post subject: Re: Triple fault when GDT size < 23
PostPosted: Wed Aug 07, 2019 4:30 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
Every bug is trivial... once you found it.

-- Uwe Überfuhr

_________________
Every good solution is obvious once you've found it.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Kamal123, Octocontrabass and 100 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