OSDev.org
https://forum.osdev.org/

Triple fault when GDT size < 23
https://forum.osdev.org/viewtopic.php?f=1&t=33811
Page 1 of 1

Author:  babel92 [ Mon Aug 05, 2019 10:52 pm ]
Post subject:  Triple fault when GDT size < 23

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;

Author:  Octocontrabass [ Tue Aug 06, 2019 12:12 am ]
Post subject:  Re: Triple fault when GDT size < 23

That sounds like the correct behavior to me.

Why do you think it's wrong?

Author:  LtG [ Tue Aug 06, 2019 1:06 am ]
Post subject:  Re: Triple fault when GDT size < 23

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.

Author:  babel92 [ Tue Aug 06, 2019 10:17 am ]
Post subject:  Re: Triple fault when GDT size < 23

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

Author:  Solar [ Wed Aug 07, 2019 4:30 am ]
Post subject:  Re: Triple fault when GDT size < 23

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

-- Uwe Überfuhr

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/