Page 1 of 1

Failing at loading the GDT

Posted: Mon Jun 13, 2005 6:42 pm
by Endar
Hi, nice to meet all you guys

I'm working with Bran's Kernel Development Tutorial from osdever.net, and I've run into a fairly major snag. I've followed everything there, up til loading the gdt, and haven't gone any further, because loading the gdt just won't work.

I'm using linux - fedora core 3, gcc, nasm, ld, syslinux as the bootloader, and I'm using a floppy image file with bochs.

Here's the gdt loading part of the assembly file for those of you who don't know the tutorial:

Code: Select all

; gdt loading part of start.asm
global gdt_flush     ; Allows the C code to link to this
extern gp            ; Says that '_gp' is in another file
gdt_flush:
;looop:
;   jmp looop
   lgdt [gp]        ; Load the GDT with our '_gp' which is a special pointer
   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax
   jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
   ret               ; Returns back to the C code!

Here is all my files, in case you wanted a look. http://www.geocities.com/naim_kingston/os.zip (run 'make', then 'make createimage', then 'make kernelimage' to compile, create a floppy image, copy the files to the floppy image)

In my main() function, I init_video() for writing text to the screen, then I call gdt_install(), and then I print out a message from the main, "Hello World".

The fairly confusing part is that whenever I run it, it continually resets. I understand that this probably means I'm not giving the gdt proper values. So, I'll start up bochs and run it, and I'll see the basic system info come up on the screen and then I'll get the 3 debug messages from the gdt_install() function and I'll also see the "Hello World" from main() pop up with the debug messages, and then it will reset and I'll see all of that again.

The very confusing part is that in the portion of the start.asm file I gave, there is a commented out infinite looop, and if I uncomment that, it will pause and not go any futher. So, I'll see the 3 debug messages and no resetting. But, if I move the loop to the line under "lgdt [gp]", it will start resetting again, but, this time, without the "Hello World" message from main().

So, I pretty much have no idea what's going on.

Re:Failing at loading the GDT

Posted: Mon Jun 13, 2005 8:13 pm
by GLneo
i used that tutorial it's very good starter will get you far ;D
ok, a big problem is you forgot that gcc puts "_" in front of names like "gp" would be in nasm _gp, same for "_gdt_flush"
hope helps you ;)

Code: Select all

; gdt loading part of start.asm
global _gdt_flush    ; Allows the C code to link to this
extern _gp            ; Says that '_gp' is in another file
_gdt_flush:
;looop:
;  jmp looop
  lgdt [_gp]        ; Load the GDT with our '_gp' which is a special pointer
  mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
  mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax
  jmp 0x08:flush2  ; 0x08 is the offset to our code segment: Far jump!
flush2:
  ret              ; Returns back to the C code!

Re:Failing at loading the GDT

Posted: Mon Jun 13, 2005 9:54 pm
by mystran
GLneo wrote: i used that tutorial it's very good starter will get you far ;D
ok, a big problem is you forgot that gcc puts "_" in front of names like "gp" would be in nasm _gp, same for "_gdt_flush"
hope helps you ;)
1. Not all targets (namely at least ELF) require any underscores. On those targets (say, GCC on Linux) gcc doesn't put any underscores. Easiest way to find out which is the case is to simply try...
2. If it links fine, then it probably isn't an underscore problem.

Re:Failing at loading the GDT

Posted: Wed Jun 15, 2005 3:39 am
by Endar
Well, that was the original reason that I took out the underscores.

Does anyone have any other ideas?

Or, barring that, are there many other OS dev forums that I can log on to? Because I've noticed over the past 2-3 days that this forum (or at least, the OS Dev, Design part of the forum) hasn't gotten much traffic.

Re:Failing at loading the GDT

Posted: Wed Jun 15, 2005 3:51 am
by Solar
Endar wrote:Or, barring that, are there many other OS dev forums that I can log on to?
http://www.osdever.net/
http://www.osdcom.info/
Because I've noticed over the past 2-3 days that this forum (or at least, the OS Dev, Design part of the forum) hasn't gotten much traffic.
We prefer to think it's quality over quantity. 8) No, honestly, I would be surprised if your problem couldn't be sorted out by one of the regulars here. (My own knowledge of gory ASM register hacking is severly limited due to my OS project dying rather too early for me to have hands-on experience. ;) )

Re:Failing at loading the GDT

Posted: Wed Jun 15, 2005 3:55 am
by Candy
mystran wrote:
GLneo wrote: i used that tutorial it's very good starter will get you far ;D
ok, a big problem is you forgot that gcc puts "_" in front of names like "gp" would be in nasm _gp, same for "_gdt_flush"
hope helps you ;)
1. Not all targets (namely at least ELF) require any underscores. On those targets (say, GCC on Linux) gcc doesn't put any underscores. Easiest way to find out which is the case is to simply try...
2. If it links fine, then it probably isn't an underscore problem.
If it is a leading underscore problem with gcc, try -fno-leading-underscore. Google for it (between quotes) to find more.

Re:Failing at loading the GDT

Posted: Wed Jun 15, 2005 3:14 pm
by mystran
Ehm, let's forget the underscores, ok? :)

Also notice that you might get better code with using EAX for the segment loads instead of AX. Both will work, the first MIGHT (depending on assembler) cause redundant operand size prefixes.

Now, as for the actual problem here, I find it likely that the problem is with your GDT entries, but without seeing them, I can't really tell what could be the specific problem.

Also remember that LGDT has special alignment requirements.
If you hardcode the GDT entries, then you can try this (which is in GNU AS syntax, because that's what I use, and I don't know NASM well enough to give you the equivalent):

Code: Select all

        .data

        .align 4  /* alignment to 4 bytes... */
        .word 0  /* then 2 bytes of dummy stuff */
.L_gdt_desc:  /* so that we get correct alignment here */
        .word (.L_end_gdt-gdt-1) /* automagically calculate, hopefully right */
        .long gdt

        .align 4  /* alignment to 4 bytes... */
        .global gdt
gdt:    /* first entry is not used */
        .quad 0x0000000000000000
        
        .quad 0x00cf9a000000ffff /* kernel cs */
        .quad 0x00cf92000000ffff /* kernel ds */
        
        .quad 0x00cffa000000ffff /* user cs */
        .quad 0x00cff2000000ffff /* user ds */

        /* there is space for one TSS per CPU, these are filled by tss_init */
        .fill MAX_CPU_NUM, 8, 0         /* MAX_CPU_NUM is defined in config.h */

.L_end_gdt:/* END HERE, symbol needed for automagical calculation */
There .L_gdt_desc is what is given to LGDT:

Code: Select all

        lgdt    .L_gdt_desc
(yes, it's a memory reference, it'd have $ in front of it if it wasn't)

Re:Failing at loading the GDT

Posted: Thu Jun 16, 2005 4:13 am
by Endar
;D

Yeah, its really not an underscore problem, otherwise it wouldn't even link.

Here's the code from for my gdt.

Code: Select all

/**
 * Defines a GDT entry. We say 'packed' because it prevents the
 * compiler from doing things it thinks is best: Prevent
 * compiler 'optimization' by packing.
 */
struct gdt_entry
{
   unsigned short limit_low;
   unsigned short base_low;
   unsigned char base_middle;
   unsigned char access;
   unsigned char granularity;
   unsigned char base_high;
} __attribute__((packed));

/**
 * Special pointer that includes the limit: The max bytes
 * taken up by the GDT, minus 1. Again, this NEEDS to be packed.
 */
struct gdt_ptr
{
   unsigned short limit;
   unsigned int base;
} __attribute((packed));


struct gdt_entry gdt[3];   ///< Our GDT, with 3 entries.
struct gdt_ptr gp;      ///< Our special GDT pointer.

/**
 * This will be a new function in start.asm. We use this to properly
 * reload the new segment registers.
 */
extern void gdt_flush();
 
/**
 * Setup a descriptor in the Global Descriptor Table.
 */
void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
{
   // Setup the descriptor base address
   gdt[num].base_low = (base & 0xffff);
   gdt[num].base_middle = (base >> 16) & 0xff;
   gdt[num].base_high = (base >> 24) & 0xff;

   // setup the descriptor limits
   gdt[num].limit_low = (limit & 0xffff);
   gdt[num].granularity = ((limit >> 16) & 0x0f);

   // finally setup the granularity and flags
   gdt[num].granularity |= (gran & 0xf0);
   gdt[num].access = access;
}

/**
 * Should be called by main. This will setup the special GDT
 * pointer, setup the first 3 entries in out GDT, and then finally
 * call gdt_flush() in out assembler file in order
 * to tell the processor where the new GDT is and update the
 * new segment registers.
 */
void gdt_install()
{
   // setup the GDT pointer and limit
   gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
   gp.base = (unsigned int)&gdt;
   
   // out NULL descriptor
   gdt_set_gate(0,0,0,0,0);

   /* The second entry is our code segment. The base address
    * is 0, the limit is 4Gbytes, it uses 4Kbyte ganularity,
    * uses 32-bit opcodes, and is a Code Segment descriptor.
    * Please check the table above in the tutorial in order
    * to see exactly what each value means.
    */
   gdt_set_gate(1, 0, 0xffffffff, 0x9a, 0xcf);

   /* The third entry is our Data Segment. It's EXACTLY the
    * same as our code segment, but the descriptor type in
    * this entry's access byte says its a Data Segment
    */
   gdt_set_gate(2, 0, 0xffffffff, 0x92, 0xcf);

   // flush out the old GDT and install the new changes
   gdt_flush();
}
This is taken directly from Bran's tutorial (linked above), so I'm not understanding just why it doens't work. Unless it has something to do with the fact that I'm using syslinux instead of grub as a bootloader......but, I really don't think so.

Re:Failing at loading the GDT

Posted: Thu Jun 16, 2005 2:27 pm
by GLneo
my bootloader is custom and the tutorial worked and all it does is pmode & a20, does syslinux do that ???

Re:Failing at loading the GDT

Posted: Thu Jun 16, 2005 10:36 pm
by Endar
I honestly have no idea.

I've been looking for syslinux info but can't find anything referring to a20 or protected mode.

Feel like giving me yours and intructions for testing? ;D

Re:Failing at loading the GDT

Posted: Thu Jun 16, 2005 10:56 pm
by Endar
What about using grub? Because that's what Bran suggests. I only went with syslinux because it looked easier to use and install onto a disk image.

Can anyone help with this? Tell me how to get grub, install it onto a disk image and use it?

Re:Failing at loading the GDT

Posted: Thu Jun 16, 2005 11:10 pm
by mystran
Grub is a good choice indeed, especially if you are willing to make your kernel multiboot compliant. Grub can be found at GNU site, but Google should give you the direct link.

Grub comes (surprise) in source form. The package includes a very basic "Hello World"-type example kernel, and decent documentation in info format. Since you probably want to read it in some other format, use Google again.

To install Grub you need a platform that can run it. Linux can, and many other x86 Unix-variants can too. I don't know about MS-land, but if you don't have a Unix system, you can also get a floppy image which contains Grub, and drop that to a floppy (or use directly).

Grub reads it's configuration from a file, and understands many filesystems directly, so you can customize everything else on the floppy. Final option is using a bootable Linux CD-ROM with Grub, if you want to install it on your harddrive. I have Grub for booting Linux, and found it useful to add a menuoption to boot the latest working binary of my kernel, directly from my home directory. YMMV.

One could say Grub is actually a bit more than just a bootloader. It's more or less a minimal operating system, capable of networking and what not. But it's one of the easiest ways to get started in OS development these days. :)

Re:Failing at loading the GDT

Posted: Sat Jun 18, 2005 8:18 am
by Endar
Okay, well, things are getting better and better. (I think)

Since I'm using FC3, I have grub already, and I needed only to find out how to install and setup grub on the floppy image and then to make the small changes in the link.ld file for the entry point, or something like that. (its just past midnight, and everything is kind of blurring together :D)

So, I think I've done fairly well. In regards to floppy images and grub, I've gone from 0 to finished in 2 hours, which isn't so bad :)

But, now, here's the problem. It is not constantly restarting, so I think that I'm all good in regards to loading the gdt. Actually, the 'init_video()' function comes right after the gdt loading, and that clears the screen, as its supposed to, but whenever there is a 'puts()' call to print something to the screen, nothing happens. It doens't get printed.

The clear screen happens, which basically prints a space in every single space on the screen, but when there is a 'puts("Hello World");' call, nothing.

So, just before I collapse into bed, does anyone have any ideas that could help? Could it be something interacting with the way grub sets up the computer for the OS? Because although the gdt wasn't working, text printing was. (gee, if it's not one, it's the other :D)

Re:Failing at loading the GDT

Posted: Sat Jun 18, 2005 3:19 pm
by mystran
You should look at the "multiboot specification" for details about the machine state after Grub starts a multiboot kernel.

Re:Failing at loading the GDT

Posted: Sat Jun 18, 2005 6:00 pm
by GLneo
Feel like giving me yours and intructions for testing?
sorry it took so long to get back to you, it looks like you got things worked out with grub but here it is:

First it loads sector 2-50(LBA) to 0x1000, then a20 & pmode, finaly jumps to 0x1000. so you should make a flat bianary(kernel.bin) and use makeboot.exe(source included) to put them together like this "makeboot a.img boot.bin kernel.bin" should give you "a.img" ready for floppy or bochs/qemu

p.s. grub makes a kernel look bad with a lot of grub menus. with custom it goes right to your kernel, no mess ;).