Page 1 of 1

LGDT

Posted: Sun Dec 29, 2002 11:46 am
by nullify
I am trying to move my LGDT routine into a C file, because I don't feel like having an assembly file for just one function.

So, consider the following (not working code):

Code: Select all

static void i386_lgdt(uint32 base, uint32 limit)
{
   uint32 desc[2] = { limit << 16, base };
   char *ptr = ( (char *) desc ) + 2;
   
   __asm__ __volatile__
   (
      "lgdt (%0)\n"
      "ljmp $0x08, $.i386_lgdt_jump\n"
      ".i386_lgdt_jump:\n"
      "movw $0x10, %%ax\n"
      "movw %%ax, %%ds\n"
      "movw %%ax, %%es\n"
      "movw %%ax, %%fs\n"
      "movw %%ax, %%gs\n"
      "movw %%ax, %%ss\n"
      :
      : "r" (ptr)
   );
}
The above function is supposed to replace the assembly routine below (which I stole from EduOS :-) ):

Code: Select all

load_gdtr:
   push ebp
   mov ebp, esp
   sub esp, 6      ; need 6 bytes for data structure of LGDT
   mov eax, param1   ; the address parameter
   mov dword [ebp - 4], eax
   mov eax, param2   ; the size parameter
   dec eax         ; limit = size - 1
   mov word [ebp - 6], ax
   lgdt [ebp - 6]

   jmp dword CodeSel:.jump
.jump
   mov ax, DataSel   ; Kernel Data Selector
               ; load flat address data
   mov ds, ax      ; selector in every data
   mov es, ax      ; segment register
   mov fs, ax      ; GCC compiled code works
   mov gs, ax      ; with flat addressing mode

   mov ax, StackSel
   mov ss, ax

   mov esp, ebp
   pop ebp
   ret
Note that 0x08 = code selector, 0x10 = data and stack selectors.

It pretty much sets up the table descriptor, gives it to LGDT, far jumps, and reloads the segment selectors. Bochs picks up an exception when it executes the LJMP instruction in the C function. I can't figure out what is going wrong.

Re:LGDT

Posted: Sun Dec 29, 2002 8:02 pm
by nullify
Maybe I should be asking: is this possible? has anyone initialized the GDT without requiring a separate assembly file (i.e., only C + inline assembly)? ???

Re:LGDT

Posted: Sun Dec 29, 2002 8:32 pm
by jrfritz
Hmmm...I don't know....

Wait...it IS possible!

Download GazOS from Sourceforge.net...it has some inline asm GCC code!

GazOS

Re:LGDT

Posted: Sun Dec 29, 2002 8:33 pm
by Perica
..

Re:LGDT

Posted: Sun Dec 29, 2002 8:35 pm
by jrfritz
GazOS ( look above ) used some inline asm code in GCC.

I guess it's easier to compile that way...using C++.