GDT=0 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
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

GDT=0 problem

Post by Dulci »

Hey, I'm trying to load in a GDT for my second stage boot-loader, but for some reason the gdt always equals 0 after loading it. Here's the code

Code: Select all

[BITS 16]
global ssblstart
ssblstart:
	; Set our data segment
	cli ; Clear instructions
	xor ax, ax
	mov ds, ax
	mov	es, ax
	mov	ax, 0x9000	; set stack at 0x9000-0xffff
	mov	ss, ax
	mov	sp, 0xFFFF
	pusha
	lgdt [TGDT]
	popa
	mov		eax, cr0
	or		eax, 1
	mov		cr0, eax
	jmp 0x8:PmodeE ; <-- this will cause qemu to crash, pretty sure its because the GDT is essentially null.

[BITS 32]

PmodeE:
	cli
	mov		ax, 0x10		; set data segments to data selector (0x10)
	mov		ds, ax
	mov		ss, ax
	mov		es, ax
	mov		esp, 90000h	
	cli
	hlt


data:

greeting2   db 'A'
error   db '2'
load   db '3'

;========================================
;===================GDT==================
;========================================

BGDT:
	; Null
	dd 0
	dd 0 
	; Code
	dw 0xFFFF 			; limit low
	dw 0 				; base low
	db 0 				; base middle
	db 10011010b 			; access
	db 11001111b 			; granularity
	db 0 				; base high
	; Data
	dw 0xFFFF 			; limit low 
	dw 0 				; base low
	db 0 				; base middle
	db 10010010b 			; access
	db 11001111b 			; granularity
	db 0				; base high
EGDT:

TGDT: 
	dw EGDT - BGDT - 1 	; limit (Size of GDT)
	dd BGDT
Any ideas?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: GDT=0 problem

Post by gerryg400 »

...but for some reason the gdt always equals 0 after loading it.
The gdt is a table. A table can't be equal to 0. Is the entire table full of zeroes. Or do you mean something else ?
If a trainstation is where trains stop, what is a workstation ?
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: GDT=0 problem

Post by Dulci »

Yes, sorry for my inaccuracy in wording, the entire table is full of zeros.
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:

Re: GDT=0 problem

Post by Combuster »

I don't see an org directive, I don't see documented what value CS will be having. As far as this code tells me, your second stage looks like it must be loaded at 0x00000 physical, which is wrong because it means overwriting the IVT and ruining your access to the BIOS.

And that is also the location you tell the processor to use when it has to look for the GDT.
"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 ]
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: GDT=0 problem

Post by Dulci »

Sorry for not mentioning that, I have a linker script link this file to 0x500, it gets loaded by my first boot loader which far jumps into 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:

Re: GDT=0 problem

Post by Combuster »

since you are apparently doing things the difficult way, would you post the file offset and disassembly of the following lines so we can check if it is your understanding of the linker that's wrong or not:

Code: Select all

lgdt [TGDT]
(..)
jmp 0x8:PmodeE 
(..)
dw EGDT - BGDT - 1
dd BGDT
"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 ]
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: GDT=0 problem

Post by Dulci »

(sorry it took me so long to respond, I was out of the state for a short amount of time).

The file is correctly loaded to 0x500, the linker looks like

Code: Select all

OUTPUT_ARCH(i386)
ENTRY(ssblstart)
SECTIONS
{
  .text 0x500 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}
Going to be removing 0000 instructions, but here is the disassembly.

Code: Select all

0000000F  60                pusha
00000010  0F01161B10        lgdt [0x101b]
00000015  61                popa
00000016  0F20C0            mov eax,cr0
00000019  660D01000000      or eax,0x1
0000001F  0F22C0            mov cr0,eax
00000022  FA                cli
00000023  F4                hlt
00000024  EA29050800        jmp 0x8:0x529

------the gdt stuff------

00000AFF  004132            add [bx+di+0x32],al
00000B02  3300              xor ax,[bx+si]
00000B04  0000              add [bx+si],al
00000B06  0000              add [bx+si],al
00000B08  0000              add [bx+si],al
00000B0A  00FF              add bh,bh
00000B0C  FF00              inc word [bx+si]
00000B0E  0000              add [bx+si],al
00000B10  9ACF00FFFF        call 0xffff:0xcf
00000B15  0000              add [bx+si],al
00000B17  0092CF00          add [bp+si+0xcf],dl
00000B1B  17                pop ss
00000B1C  0003              add [bp+di],al
00000B1E  1000              adc [bx+si],al

Post Reply