Page 2 of 3
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 6:49 pm
by brodeur235
You're so right, I just got way ahead of myself, thanks,
Brodeur235
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 8:03 pm
by brodeur235
The following code freezes my emulator (VBox):
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
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 8:17 pm
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.
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 9:37 pm
by yemista
brodeur235 wrote:The following code freezes my emulator (VBox):
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.
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 9:56 pm
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.
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 11:13 pm
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
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 11:19 pm
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.
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 11:22 pm
by Troy Martin
Yeah, and I'm not sure if LGDT uses DS or ES... Try setting ES to zero as well.
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 11:33 pm
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
Re: Practical Application For the GDT
Posted: Fri Jun 12, 2009 11:48 pm
by Troy Martin
Take the sti out. It should be cli.
Re: Practical Application For the GDT
Posted: Sat Jun 13, 2009 2:52 am
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
Re: Practical Application For the GDT
Posted: Sat Jun 13, 2009 7:59 am
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.

Re: Practical Application For the GDT
Posted: Sat Jun 13, 2009 11:35 am
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
Re: Practical Application For the GDT
Posted: Sat Jun 13, 2009 12:36 pm
by Troy Martin
syntropy: It's one byte for the ascii and one for the colour, not two each.
Re: Practical Application For the GDT
Posted: Sat Jun 13, 2009 12:41 pm
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