GDT code - boom

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.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Have you tried it?
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

There's no point in trying things that will just cause your linker to refuse to link stuff..
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I still say, try it.

Either way, your Bochs log would definitely help in finding out exactly why you're having this problem and how to fix it.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

ehird wrote:

Code: Select all

00017504444e[CPU0 ] interrupt(): gate descriptor is not valid sys seg
00017504444e[CPU0 ] interrupt(): gate descriptor is not valid sys seg
00017504444i[CPU0 ] protected mode
00017504444i[CPU0 ] CS.d_b = 32 bit
00017504444i[CPU0 ] SS.d_b = 32 bit
00017504444i[CPU0 ] | EAX=00106814  EBX=0002bdc0  ECX=00000000  EDX=00000000
00017504444i[CPU0 ] | ESP=00106810  EBP=00106870  ESI=0002bf1a  EDI=0002bf1b
00017504444i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf ZF af PF cf
00017504444i[CPU0 ] | SEG selector     base    limit G D
00017504444i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00017504444i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] |  DS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] |  ES:0000( 0000| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00017504444i[CPU0 ] | EIP=001006fd (001006fd)
00017504444i[CPU0 ] | CR0=0x00000011 CR1=0 CR2=0x00000000
00017504444i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00017504444i[CPU0 ] >> mov eax, dword ptr ds:0x100223 : A123021000
00017504444e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00017504444i[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
from what I can uderstand from this is that DS isn't set (or is set to NULL), which would cause the general protection fault. In which case the code isn't even getting up to part of loading the GDT.

If this has change in the new version can someone post the new one. I'm also wondering, if you operating system is multi-tasking. It could be the TSS (or your process block) that may be causing this.

Anyway I got to get back to work :( :(
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Try this before any GDT init:

Code: Select all

mov ax,0x10
mov cs,ax
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
Note that SS should be a high value as well, as the stack grows downwards.
ehird
Member
Member
Posts: 214
Joined: Thu Mar 15, 2007 8:48 am

Post by ehird »

Code: Select all

gdt_flush:
	mov ax, 0x10
	mov cs, ax
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	lgdt [gp]
	jmp 0x08:flush2
still boom
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post by ~ »

If on (Un)Real Mode, try:

Code: Select all

	lgdt [cs:gp]
ehird
Member
Member
Posts: 214
Joined: Thu Mar 15, 2007 8:48 am

Post by ehird »

Nope. GRUB launches me into protected mode.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post by ~ »

ehird wrote:Nope. GRUB launches me into protected mode.
Why don't you upload a disk image, and if possible the sources? Last time it was possible to help out and resolve for somebody through that sort of interaction in about 1 day.
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 »

mov cs, ax
that can't even SAY boom. It just wont compile :shock:
"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 ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Also, are you trying to switch segments before loading your own GDT? GRUB legacy documentation states that you can't rely on segment numbers or even the value of GDTR before you load your own GDT.

Adam
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

pcmattman wrote:I still say, try it.
Trying things at random will at best give you code that works in some cases. The only way to build reliable code is to figure out why something does not work, and fix it based on the gathered information... not just randomly change stuff until something seems to run.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

pcmattman wrote:Try this before any GDT init:

Code: Select all

mov ax,0x10
mov cs,ax
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
This won't work. CS can't be the same as the rest, plus as said, you can't load CS that way, as you have to use FAR-jump instead.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

From the Bochs log above, it's obvious that since you are loading DS as the first new segment from the new GDT, and Bochs panics with invalid gate and DS=NULL in the register dump, your gate descriptor isn't valid, or your LGDT inststruction doesn't load the right stuff..

Interesting questions would be, do you have paging enabled, and if so, is your GDT mapped at the same address in physical and virtual memory?

If you scroll backwards in the Bochs log, what does it say about the LGDT? Enable CPU debug=log (in per-device log options) if it doesn't say anything. It could be simply that it fails to load the GDT if it's not aligned properly.

Code: Select all

struct gdt_ptr {
   unsigned short limit;
   unsigned int base;
} __attribute__((packed)); 

struct gdt_ptr gp; 
That won't get aligned right. You need to align it so that the 'base' (which is 4 bytes) get's properly aligned to 4 bytes. Easiest way to do it would be something like this:

Code: Select all


// You don't really need the attributes, since it'll get aligned to 4 anyway
// and since you have two shorts = 4bytes, and one int = 4bytes, it'll
// stay packed even without the attributes.
// They are good to have for documentation though, or in case future
// compiler versions start aligned differently.
struct gdt_ptr {
   unsigned short _dummy; // <- 2 bytes, to offset the alignment
   unsigned short limit;
   unsigned int base;
} __attribute__((aligned(4), packed));

struct gdt_ptr gp; 
If you still get tripple-fault after you've fixed that one, then there might also be something else wrong, but start by fixing that one. :)

edit: Also add 2 bytes to the address you load with LGDT, like this:

Code: Select all

   lgdt [gp+2]  // <- if I'm not mistaken, you want [] around it in Intel syntax? not sure to be honest
[/i]
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
Post Reply