Page 1 of 1

GCC LGDT

Posted: Mon Nov 21, 2005 10:29 am
by Exabyte256
I was using a simple method to test the GDT functions I've made. First I compiled the application and ran it on an emulator, it didn't crash. So it must be working? Maybe, maybe not.

After a lot of messing around with the code, I finally figured out that using the lgdt command with _any_ value allowed the program to run fine. My best bet is I have to reload the CS register - that's what happened before in that protected mode jump. But unfortunately GCC doesn't seem to support the most simple command of "jmp Segment:Label".

Does anyone know how I can solve this problem?

The LGDT command is __asm volatile("lgdt (%0)" :: "r" (value));

(I'm using the GCC compiler, the code is in the kernel which is loaded in protected mode with A20 enabled, a basic GDT set up earlier and yes, it works, yes it can print output, yes there are no other problems anywhere and yes I have looked on google, manuals and tried a few days fixing the problem or else I wouldn't have started this topic.)

Re:GCC LGDT

Posted: Mon Nov 21, 2005 12:16 pm
by durand
Yes, you're right. The new GDT will only be referenced when the segment descriptors are reloaded and segment info needs to be queried. So you do a far jump, like you want to do:

Code: Select all

static void gdt_updateSegments(unsigned int new_code, unsigned int new_data)
{
  asm (
         "\n"
         "pushl %0\n"
         "pushl $1f\n"
         "lret \n"
        "1:\n"
         "mov %1, %%eax\n"
         "mov %%ax, %%ds\n"
         "mov %%ax, %%es\n"
         "mov %%ax, %%fs\n"
         "mov %%ax, %%gs\n"
         "mov %%ax, %%ss\n"

       :
       : "g" (new_code), "g" (new_data)
       : "eax");
}

Re:GCC LGDT

Posted: Mon Nov 21, 2005 12:38 pm
by Exabyte256
Thankyou very much! Time to test it properly...

*The program promptly tripplefaults* ...Mm. I have some debugging to do.

Re:GCC LGDT

Posted: Sat Nov 26, 2005 10:13 am
by shig
As a side note, I had the problem that I couldn't get GCC[0] inline ASM to load the GDT properly no matter what I did. Well, the GDT would load, but as soon as I tried to do a far call, everything would fall to pieces. The GDT itself was packed into memory correctly, and properly aligned (spent hours going over it a byte at a time, and rewrote it several times in case I was missing something), but no luck while I was trying to inline it in GCC.

I eventually figured it was ignorance, rewrote it in a .S file and linked it in that way[1]

A friend working independently ran into the same problem, so i'm wondering if anyone else has run into the same thing?

David

[0] 3.3 and 3.4. Still waiting for 4.0 to get stable enough.
[1] On the bright side, I now know waaay more ASM than I did to start with.

Re:GCC LGDT

Posted: Sat Nov 26, 2005 11:00 am
by 0Scoder
Not sure if this is any part of the problem, but is the lgdt inline asm correct? I use:

Code: Select all

asm volatile("lgdt (%0)" :: "m" (gdt_pointer)); /*You used 'r' instead of 'm', btw*/
Which has worked fine (and I've tested it with the use of a call gate, so it certainly loads properly)