gtd setup bochs error

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
anti86
Posts: 2
Joined: Thu Nov 30, 2006 9:48 am
Location: Turkey

gtd setup bochs error

Post by anti86 »

Hi,

I am so new in os development area. I am trying to figure out a project (it's due date has passed =). I have a problem about GDT. I am trying to add to new segment descriptors to my gdt table but bochs gives panic (i'am using bochs as emulator). I have taken the code from a web site and try to modify it.

#include <gdt.h>
// Setup a descriptor in the Global Descriptor Table
void gdt_set_descriptor(int index, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
{
// Setup the descriptor base address
gdt[index].base_low = (base & 0xFFFF);
gdt[index].base_middle = (base >> 16) & 0xFF;
gdt[index].base_high = (base >> 24) & 0xFF;

// Setup the descriptor limits
gdt[index].limit_low = (limit & 0xFFFF);
gdt[index].granularity = ((limit >> 16) & 0x0F);

//Finally, set up the granularity and access flags
gdt[index].granularity |= (gran & 0xF0);
gdt[index].access = access;
}

// This function will be called by kernel.cpp
void gdt_install(unsigned long csbase,unsigned long limit)
{
/* Setup the GDT pointer and limit */
gp.limit = (sizeof(struct gdt_entry) * 5) - 1;
gp.base = &gdt;

/* Our NULL descriptor */
gdt_set_descriptor(0, 0, 0, 0, 0);

gdt_set_descriptor(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);


gdt_set_descriptor(2, 0, 0xFFFFFFFF, 0x92, 0xCF);


gdt_set_descriptor(3, csbase, limit,0xFA, 0xCF);// GIVES PANIC


gdt_set_descriptor(4, csbase, limit, 0xFA, 0xCF);

/* Flush out the old GDT and install the new changes! */
UpdateGdt(gp.base,gp.limit);
}

UpdateGdt
mov eax, [esp+4]
mov [GDTR+2], eax
mov ax, [esp+8]
mov [GDTR], ax
lgdt [GDTR]
ret

I thought that problem can be about access value or granularity.

Thank you.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Standard replies:
- What panic does bochs give exactly, what are the exact circumstances
- What have you tried to solve it
- What have you not tried to solve it
- Which part of the code causes the panic
- Have you tried bochs' debugger
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
anti86
Posts: 2
Joined: Thu Nov 30, 2006 9:48 am
Location: Turkey

Post by anti86 »

Standard replies:
- What panic does bochs give exactly, what are the exact circumstances
==> ========================================================================
Bochs is exiting with the following message:
[CPU0 ] exception(): 3rd (13) exception with no resolution
========================================================================

- What have you tried to solve it
I have try to change the access values of the new descriptors

- What have you not tried to solve it

I did not search about the granularity values. What should they be ?

- Which part of the code causes the panic

There is a comment at the code you can see it

gdt_set_descriptor(3, csbase, limit,0xFA, 0xCF);// GIVES PANIC
gdt_set_descriptor(4, csbase, limit, 0xFA, 0xCF);// Gives panic too

- Have you tried bochs' debugger

I do not know how to use it exactly sorry for that if you tell how to then I can try it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Well for what i can see the panic does not occur in the gdt write functions, but rather the code that follows it.

Anyway what happens right now is that you set the GDT as follows:
0x00 (0): null selector
0x08 (1): flat ring 0 code selector
0x10 (2): flat ring 0 data selector
0x18 (3): ring 3 code segment with base = csbase and limit = limit * 4096
0x20 (4): ring 3 code segment with the same properties
(I've underlined the things you should check against your specification)

For the rest, the likely culprit here is that you attempted to load or use a wrong selector somewhere, causing a fault (and since your os has no error handling, a triple fault, panic, and consequently a reset). Which of the two, i cant tell. Bochs should've given you a cpu dump when you quit which will probably contain the answer.

As for debugging with bochs: the first hit on google
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply