Practical Application For the GDT

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.
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

You're so right, I just got way ahead of myself, thanks,

Brodeur235
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

The following code freezes my emulator (VBox):

Code: Select all

mov cr0,eax
I don't know if it's the emulator or me, but it freezes my bootloader, as well as the short one Xiphia wrote (which i padded, and made meet bootloader standards)...

Brodeur235

P.S. If I comment the line, the bootloader ocntinues to run, but in real mode
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:

Re: Practical Application For the GDT

Post by pcmattman »

I would highly suggest trying it out in Bochs. That way you can get the post-mortem Bochs log, and you can also use the debugger. It helps you find the problem quickly, without all the guesswork.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Practical Application For the GDT

Post by yemista »

brodeur235 wrote:The following code freezes my emulator (VBox):

Code: Select all

mov cr0,eax
I don't know if it's the emulator or me, but it freezes my bootloader, as well as the short one Xiphia wrote (which i padded, and made meet bootloader standards)...

Brodeur235

P.S. If I comment the line, the bootloader ocntinues to run, but in real mode
Whats the code for your gdt? There is probably something wrong with your gdt.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Practical Application For the GDT

Post by Troy Martin »

Do you set up DS and ES in real mode with real mode segments for your bootloader? That could be a problem, as well as ORG.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

I think my GDT is fine now, but I know I did set up ds to equal 0 for my bootloader. If this is the problem, how do I fix it?

UPDATED bootloader code (all i changed is the gdt entries):

Code: Select all

[ORG 0x7C00]

;procedures

main:
	xor ax,ax
	mov ds,ax
	
	mov si,bl_loaded_msg
	call print_si
	
	call load_kernel
	mov si,kl_loaded_msg
	call print_si
	
	lgdt [gdt_head]
	mov si,gdt_loaded_msg
	call print_si
	
	mov eax, cr0
	or eax, 1
	mov cr0, eax                           ; <-- this line freezes my emulator's CPU. If I comment it out, the processor continues, but in real mode.
	jmp 0x8:kernel                         ; I tried disabling (cli) and reenabling interrupts before and after the "mov", but nothing changed

print_si:
	mov bh,0x00
	mov bl,0x07
	mov ah,0x0E
	.next_char
		lodsb
		cmp al,0x00
		jz .done
		int 0x10
		jmp .next_char
	.done
		ret

load_kernel:
	mov dh,0x00
	mov dl,0x00
	mov ch,0x00
	mov cl,0x02
	xor bx,bx
	mov es,bx
	mov bx,kernel
	mov ah,0x02
	mov al,0x01
	int 0x13
	ret
	
;gdt

gdt_entries:
	
	;null entry
	dd 0x0,0x0
	
	;code segment descriptor: OFFESET = 0x8
	dw 0xFFFF,0x0000
	db 0x00,10011010b
	db 11001111b,0x0
	
	;data segment descriptor: OFFSET = 0x10
	dw 0xFFFF,0x0000
	db 0x00,10010010b
	db 11001111b,0x0
	
gdt_head:
	size dw ($-gdt_entries)
	offset dd gdt_entries

;data

bl_loaded_msg db "Bootloader loaded.",0xA,0xD,0
kl_loaded_msg db "Kernel loaded.",0xA,0xD,0
gdt_loaded_msg db "GDT setup.",0xA,0xD,0
pm_loaded_msg db "Protected mode entered.",0xA,0xD,0

;padding

times 512-2-($-$$) db 0x0000
bootable db 0xAA, 0x55
Brodeur235
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:

Re: Practical Application For the GDT

Post by pcmattman »

Are you certain that the emulator is freezing on the "mov cr0, eax" line? I personally think you will need to flush your segment registers when entering protected mode, a bit like this:

Code: Select all

mov cr0, eax
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs,ax
mov ss, ax

jmp 0x8:kernel
You probably also want to use "cli" when entering "main" (ie, before setting DS) so the CPU doesn't attempt to use the IDT until you're ready for it.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Practical Application For the GDT

Post by Troy Martin »

Yeah, and I'm not sure if LGDT uses DS or ES... Try setting ES to zero as well.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

Yeah, I'm pretty sure it's the "mov" line because when I comment it out, execution continues in real mode. If I don't comment it out, execution stops there. I tried what you said, refreshing the segment registers. Although it's a helpful tip that I will include in my bootloader, it didn't solve the problem. Here's an update of what I have changed (only the "main" proc):

Code: Select all

[ORG 0x7C00]

;procedures

main:
	cli
	xor ax,ax
	mov ds,ax
	mov es,ax
	sti
	
	mov si,bl_loaded_msg
	call print_si
	
	call load_kernel
	mov si,kl_loaded_msg
	call print_si
	
	lgdt [gdt_head]
	mov si,gdt_loaded_msg
	call print_si
	
	cli
	mov eax, cr0
	or eax, 1
	mov cr0, eax
	
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs,ax
	mov ss, ax
	sti
	
	jmp 0x8:kernel
Brodeur235
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Practical Application For the GDT

Post by Troy Martin »

Take the sti out. It should be cli.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

My fault guys... My fault. I was trying to verify that I was in protected mode by moving a string offset into si and then calling a procedure that outputs the string 1 char at a time using int 10h only... in pm you can't use interrupts. Brilliant me, i saw no output and cried wolf: "my emulator froze!!!" Sorry. When I uncommented the thing ("mov cr0,eax"), that left me in real mode, and I got the output I expected. Every time I WAS actually making it into Pmode. Now that I'm COMPLETELY on my own, without even BIOS functions to call, I'm looking to write a procedure that will output to the monitor, picking up at where the last string written to the monitor stopped. Anyways, the purpose of this thread has been served and more. I understand the gdt and my bootloader has made it into protected mode. Your help never goes unappreciated ;),

Brodeur235
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Practical Application For the GDT

Post by quok »

It's great that you got this solved and all, but I noticed you're not setting up a stack for your real mode code. Yet you're using call and ret, two instructions that do indeed use the stack. Your bootloader may work on in VirtualBox now, but fail mysteriously on real hardware later.

I'm surprised nobody else has pointed this out. Anyway, you should definitely fix this to prevent those types of mysterious failure later. :)
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: Practical Application For the GDT

Post by whowhatwhere »

brodeur235 wrote:My fault guys... My fault. I was trying to verify that I was in protected mode by moving a string offset into si and then calling a procedure that outputs the string 1 char at a time using int 10h only... in pm you can't use interrupts. Brilliant me, i saw no output and cried wolf: "my emulator froze!!!" Sorry. When I uncommented the thing ("mov cr0,eax"), that left me in real mode, and I got the output I expected. Every time I WAS actually making it into Pmode. Now that I'm COMPLETELY on my own, without even BIOS functions to call, I'm looking to write a procedure that will output to the monitor, picking up at where the last string written to the monitor stopped. Anyways, the purpose of this thread has been served and more. I understand the gdt and my bootloader has made it into protected mode. Your help never goes unappreciated ;),

Brodeur235
There is an 80x25 character array starting at 0xB800, which coincides with (0,0) on the top left hand side of the screen. I forget the format, but each 'cell' is a short integer width (4 bytes). Two of them are for an ascii character code, and two of them dictate forgeround/background color.

http://wiki.osdev.org/Drawing_In_Protected_Mode
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Practical Application For the GDT

Post by Troy Martin »

syntropy: It's one byte for the ascii and one for the colour, not two each.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Practical Application For the GDT

Post by brodeur235 »

lol, as soon as he posted that I started looking up specs and wondering if they were wrong or he was. Yeah, it's one byte /ea. Anyways, thanks syntropy,

Brodeur235
Post Reply