Far jump problem.

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
huxuelei
Member
Member
Posts: 35
Joined: Tue May 27, 2008 8:32 am

Far jump problem.

Post by huxuelei »

Hi, I have some question about far jump.here is the code:

Code: Select all

static void init_gdt()
{
   gdt_ptr.limit = (sizeof(gdt_entry_t) * 5) - 1;
   gdt_ptr.base  = (u32int)&gdt_entries;

   gdt_set_gate(0, 0, 0, 0, 0);                // Null segment
   gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
   gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
   gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // User mode code segment
   gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // User mode data segment

   gdt_flush((u32int)&gdt_ptr);
}

[GLOBAL gdt_flush]    ; Allows the C code to call gdt_flush().

gdt_flush:
   mov eax, [esp+4]  ; Get the pointer to the GDT, passed as a parameter.
   lgdt [eax]        ; Load the new GDT pointer

   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax        ; Load all data segment selectors
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:.flush   ; 0x08 is the offset to our code segment: Far jump!
.flush:
   ret 
As the article(http://www.jamesmolloy.co.uk/tutorial_h ... 20IDT.html) say, after gdt_flush, the code will jump to second of the GDT.In the function init_gdt(), the second GDT entry is set like this:

Code: Select all

gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
But I don't understand, the second GDT entry do not point to any code segment. It's base address is 0.What will happen at this time?
milouz
Posts: 8
Joined: Wed Apr 16, 2008 3:55 am
Location: France
Contact:

Post by milouz »

It simply points to the whole memory. :wink:
midir
Member
Member
Posts: 46
Joined: Fri Jun 13, 2008 4:09 pm

Re: Far jump problem.

Post by midir »

huxuelei wrote:But I don't understand, the second GDT entry do not point to any code segment. It's base address is 0.What will happen at this time?
This is simply what's known as the flat memory model. The base is 0x00000000, the lowest point of addressable memory. The limit is 0xFFFFFFFF, which is 4 gigabytes, giving you the full 32-bit address space to play with.

Modern operating systems seem to find it easier to do things this way, where the segment base and limit don't really get used, so your code is at some offset within this segment (rather than the beginning of it).

A far jump switches code segment and jumps to an offset within it. This is what the line:

Code: Select all

jmp 0x08:.flush
is doing (it's has both a segment and offset). One advantage of the flat model is that pointers (offsets) point to the same place in any segment, since they all have the same base.
Post Reply