LGDT

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
nullify

LGDT

Post 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.
nullify

Re:LGDT

Post 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)? ???
jrfritz

Re:LGDT

Post by jrfritz »

Hmmm...I don't know....

Wait...it IS possible!

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

GazOS
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:LGDT

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:41 pm, edited 1 time in total.
jrfritz

Re:LGDT

Post by jrfritz »

GazOS ( look above ) used some inline asm code in GCC.

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