• ### What is your GameDev Story?

This topic is 4968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, since I've left this for a while, and still have no idea, I'm going to do something that I don't do very often. I'm going to ask outright for an answer. Some of you may have seen some of my previous thread about this: I'm using a tutorial to write a small OS and I've gotten stuck fairly early on, where the global descriptor table is to be loaded. I'm using bochs to run the OS, and when it gets to loading the gdt, something wrong happens and the whole thing resets. It doens't seem to happen during the gdt load, but I don't think the gdt is loaded properly, because printing to the screen is next, and the reset happens right after the text is printed to the screen. I was advised to look at the Intel Architecture System Programming Guide, and, it has helped, but not enough that I'm going to be able to solve this on my own. Here is a zip file of all the files that I'm using: os.zip To compile, link and run this, you'll need gcc, nasm, ld, syslinux and bochs, and I've included a makefile, config file for syslinux and the bochsrc file for bochs. To compile everthing once it's unzipped, simple run 'make'. To create the floppy image run 'make createimage' and then to copy everything to the floppy image, ready to run, 'make kernelimage', and the run it using bochs. And, if you decide to look at all this, and can give me a hand, just know that I planned to use the tutorial to get all the assembly up and going, and then concentrate on the C code, so I don't know a lot of intel assembly (I did some MIPS in one of my uni classes, so I can figure out most of, it, but writing is another matter). I would appreciate any help that you guys can give me.

##### Share on other sites
Have you tried using the halt instruction for debugging purposes? Just put a hlt somewhere in the code and move it down until the OS crashes. Now you know which line the crashes are occuring.

When I tried writing my own OS I also had major troubles getting the GDT and the code around it right. It's easy to forget about something.

[Edited by - doho on June 12, 2005 11:09:09 PM]

##### Share on other sites
What if the 'hlt' instruction isn't working?

I've tried putting the halt instruction at every single line inside the gdt_flush function (the one in the start.asm assembly file), and I got nothing. I assume that the instruction is just supposed to stop everthing, right? I is it supposed to reset the system as well? Because if it's not, then it didn't work.

##### Share on other sites
Yea it's supposed to stop/halt the system. If a hlt in the beginning of gdt_flush doesn't halt the system it just means that the crash occurs before the gdt_flush. If this is the case, put it in the beginning of the startup code and move it down until it doesn't halt anymore.

##### Share on other sites
But, here's the strange thing. It looks like the crash happens after the gdt_flush function has been exited.

void main(){        init_video();   // init screen for output        gdt_install();  // init the gdt for memory bounds        //idt_install();        // install the interrupt descriptor table        puts("Hello World!\n");        //i = 10 / 0;        //putch(i);        for (;;);}

I've put init_video() before the gdt_install(), so I can output some error messages.

void gdt_install(){        // setup the GDT pointer and limit        gp.limit = (sizeof(struct gdt_entry) * 3) - 1;        gp.base = (unsigned int)&gdt;        // our NULL descriptor        gdt_set_gate(0,0,0,0,0);        puts("Set NULL gate");        /* 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);        puts("Set gate 1");        /* 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);        puts("Set gate 2");        // flush out the old GDT and install the new changes         gdt_flush();}

All the debug messages show up, and so does the "Hello World" from the main function, which happens after the gdt_install() function is returned from.

##### Share on other sites
Well it sounds like you have a wierd problem then. The for(;;) loop should not reset the computer.

Just use hlt as I said to find out which line is causing the crash and then try to think of a possible reason why the crash is occuring.

##### Share on other sites
The hlt instruction is resetting the os!!

void main(){        init_video();   // init screen for output        gdt_install();  // init the gdt for memory bounds        //idt_install();        // install the interrupt descriptor table        //__asm__ ("hlt");        puts("Hello World!\n");        for (;;);}

As is, when it is run, everything shows up: the three debug messages in the gdt_install() function and the "Hello World". But, if I uncomment the assembly line in the main function above, only the gdt_install() messages show up.

It looks like the hlt command is resetting the os.

Anyone know why?

##### Share on other sites
Is it beeing reseted or halted? There is huge difference.

##### Share on other sites
All I know is that when the os hits the 'hlt' line, the whole os is booted up again.

Here's what happens:

1. I compile, link, and copy the kernel to the floppy image.
2. I run bochs and run the simulation.
3. The os goes through all the general start up system info.
4. It prints out the 3 debug messages from the gdt_install() function.
5. It prints out the "Hello World" message from the main() function.
6. The screen goes blank.
7. Goes back to 3.

And all of this happens fairly fast.

##### Share on other sites
Try setting a for(;;); in the beginning of the main and then move it down. Then you know if the setup_gdt is making the machine restart when more instructions are beeing executed after its execution. Maybe some interrupt is occuring while your GDT isn't correctly initialized and that this is causing a crash as soon as you return from setup_gdt or maybe it crashed inside setup_gdt. Have you tried putting hlt inside setup_gdt too? if you have weird problems with halt you could just do for(;;); as you are doing now in your main.

[Edited by - doho on June 13, 2005 6:52:21 AM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 10
• 11
• 13
• 9
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015467
×