Assembly Protected Mode

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
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Assembly Protected Mode

Post 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
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Assembly Protected Mode

Post 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.
Last edited by Nable on Sun Dec 01, 2013 2:07 pm, edited 2 times in total.
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Re: Assembly Protected Mode

Post by roban100 »

Okay I will try that!
User avatar
nerdguy
Member
Member
Posts: 70
Joined: Wed Oct 30, 2013 8:11 am

Re: Assembly Protected Mode

Post 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. :(
Last edited by nerdguy on Mon Dec 02, 2013 5:48 pm, edited 1 time in total.
When you say, "I wrote a program that crashed Windows," people just stare at you blankly and say, "Hey, I got those with the system, for free." - Linus Torvalds
64 bit Kernel in early development
http://github.com/nerdguy12/core64
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Re: Assembly Protected Mode

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Assembly Protected Mode

Post by jnc100 »

Your stack is unaligned.

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

Regards,
John.
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Re: Assembly Protected Mode

Post 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
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: Assembly Protected Mode

Post 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.
"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 ]
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Assembly Protected Mode

Post 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.
User avatar
nerdguy
Member
Member
Posts: 70
Joined: Wed Oct 30, 2013 8:11 am

Re: Assembly Protected Mode

Post by nerdguy »

@Nable Thanks for pointing out.
When you say, "I wrote a program that crashed Windows," people just stare at you blankly and say, "Hey, I got those with the system, for free." - Linus Torvalds
64 bit Kernel in early development
http://github.com/nerdguy12/core64
Post Reply