Triple fault when GDT size < 23

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
babel92
Posts: 8
Joined: Thu Oct 25, 2012 9:21 am

Triple fault when GDT size < 23

Post by babel92 »

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: Select all

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;
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: Triple fault when GDT size < 23

Post by Octocontrabass »

That sounds like the correct behavior to me.

Why do you think it's wrong?
LtG
Member
Member
Posts: 384
Joined: Thu Aug 13, 2015 4:57 pm

Re: Triple fault when GDT size < 23

Post by LtG »

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.
babel92
Posts: 8
Joined: Thu Oct 25, 2012 9:21 am

Re: Triple fault when GDT size < 23

Post by babel92 »

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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Triple fault when GDT size < 23

Post by Solar »

Every bug is trivial... once you found it.

-- Uwe Überfuhr
Every good solution is obvious once you've found it.
Post Reply