Page 1 of 1

Assembly Protected Mode

Posted: Sun Dec 01, 2013 1:04 pm
by roban100
So again I got some problems...

This time I'm trying to go into 32bit protected mode.
But it seems like the code below crashes in virtualbox.
Can anyone spot the problem?

Code to enter protected mode:

Code: Select all

cli
	lgdt [GDT]
	mov eax, cr0
	or al, 1
	mov cr0, eax
	
	jmp 0x08:PMode ; Enter Protected Mode!
GDT:

Code: Select all

gdt_data: 
	dd 0 				; null descriptor
	dd 0 

	dw 0FFFFh 			; limit low
	dw 0 				; base low
	db 0 				; base middle
	db 10011010b 			; access
	db 11001111b 			; granularity
	db 0 				; base high

	dw 0FFFFh 			; limit low (Same as code)
	dw 0 				; base low
	db 0 				; base middle
	db 10010010b 			; access
	db 11001111b 			; granularity
	db 0				; base high

end_of_gdt:
GDT: 
	dw end_of_gdt - gdt_data - 1 	; limit (Size of GDT)
	dd gdt_data 			; base of GDT
PMode:

Code: Select all

[bits 32]
PMode:
	mov ax, 0x10
	mov ds, ax
	mov ss, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	hlt
	jmp $
This code crashes with ( in virtualbox ):

VirtualBox - Guru Meditation
A critical error has occured while running the virtual machine and the machine execution has been stopped.

Btw I'm using nasm to assemble! :o

Re: Assembly Protected Mode

Posted: Sun Dec 01, 2013 2:01 pm
by Nable
It may sound a bit rude but it's time to learn how to educate yourself instead of asking for spoon-feeding.
I can suggest you using Bochs or QEmu (or learn how to attach debugger to VBox), with proper tools you can see all the gory details about failed code.

Upd: Btw, I don't see org statement in your code, far jump may have a wrong destination because of it.

Re: Assembly Protected Mode

Posted: Sun Dec 01, 2013 2:04 pm
by roban100
Okay I will try that!

Re: Assembly Protected Mode

Posted: Sun Dec 01, 2013 7:44 pm
by nerdguy
Try the code in Bochs, It gives you the complete info where you are going wrong, and post the output here. Also where is the loader that places this code? Where is this code loaded? The code is similar to broken thorn tutorials, As a recommendation you should enable A20 before entering Protected Mode. Also, I see no ORG's. :(

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 7:52 am
by roban100
I didn't post the hole code as that would be too much I feel but this is code the 2:nd stage bootloader, my first bootloader loads that at: 0x0500:0x0000 <- I don't know if this is good or not but. And also I'm enabling A20 before this happens ( The crash )!

The jmp $ at the end was for safety reasons as I didn't know if the hlt alone would work but I will try without later I guess! :)

Also this is the beginning of the 2nd stage bootloader:

Code: Select all

[bits 16]
[org 0x0000]
main:
cli
	mov     ax, 0x500
	mov     ds, ax
	mov     es, ax
	mov     fs, ax
	mov     gs, ax
	; create stack
	mov     ax, 0x9000
	mov     ss, ax
	mov     sp, 0xFFFF
sti	
	mov Byte[DriveNumber], dl
	mov Word[DataCluster], bx
The dl registry is loaded with the drivenumber before my first bootloader jumps into this one. And bx with the cluster ( sector in this case. ) of the start of the root directory entries. :D

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 12:03 pm
by jnc100
Your stack is unaligned.

But besides that, have you tried debugging in bochs yet?

Regards,
John.

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 2:07 pm
by roban100
Thanks to everybody, I solved it!

My solution:
I tried it in bochs and got some information. I didn't really understand but it crashed on the jmp 0x08:PMode.
So I suspected that it jumped to the wrong location so I changed my 1 bootloader to load this bootloader at: 0x0000:0x7E00 instead of 0x0500:0x0000 as it was before. And now it all works perfectly!

Thanks again! :D

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 2:56 pm
by Combuster
That probably means you had your segmentation math wrong :wink:


Also,

Code: Select all

   mov     ax, 0x9000
   mov     ss, ax
   mov     sp, 0xFFFF
Please don't trash reserved memory for your own sanity.

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 3:37 pm
by Nable
nerdguy wrote: Or maybe just
HLT
...and have tons of random error logs after sudden interrupt. Very nice idea, especially if you're debugging smth. No, thanks.
nerdguy wrote:Why not simply
CLI
HLT
If there is a specific reason please correct me.
It's know that emulators often stop screen updates when they detect such hard-lock. When you are testing your first kernel steps this can lead to a situation when you output some string to screen, lock CPU and then you see that there's no your string on the screen. So, you can think that you code wasn't executed due to some strange reason. Here begins panic and wasting of time to find non-existent bug.

Plain 'jmp $' gives you 100% CPU load and that's rather annoying. So, I always prefer this variant (FASM syntax) :

Code: Select all

@@:
	hlt
	jmp	@b
No 100%-load, ~no random errors. I think that topicstarter saw this variant some time ago but didn't understood what's the meaning of 'hlt' in such case.

Re: Assembly Protected Mode

Posted: Mon Dec 02, 2013 5:49 pm
by nerdguy
@Nable Thanks for pointing out.