Page 1 of 1

Unreal mode causing kernel panic

Posted: Wed Apr 04, 2012 10:01 am
by BetaWar
Based off the amount of trouble that I was having with my previous implementation of my kernel I have decided to attempt starting over (rewriting everything from scratch) and seeing if I can get further with it this time.

At the moment I am just working on my bootloader, and am attempting to set up unreal mode (so I can load my kernel after the 1 MB mark without trouble (theoretically)). I am running into an error when attempting to reload real mode (swap back to real mode after starting protected mode) that states "load_seg_reg(DS, 0x0008): invalid segment".

Based on what I have read about unreal mode it is best to have a20 mode set up first (so you can get the odd 1 MB blocks), so I have set that up and it seems to work (or at least not cause a panic). I have also gone through an loaded a simple GDT for the bootloader, and found the amount of RAM the VM has available (storing it in 0x1000, amount between 1 and 16 MB, and 0x1004, number of 64 KB pages after the 16 MB mark), and it does in fact find the entire amount of RAM I allow it to use (256 MB).

Anyways, here is my current bootloader code:

Code: Select all

[BITS 16]
[ORG 0x700]

checkENine:
	in al, 0xe9
	cmp al, 0xe9
	jne .noENine
	jmp .ENine
.noENine:
	mov al, 'N'
	jmp .print
.ENine:
	mov al, 'Y'
.print:
	mov ah, 0x0e
	mov bh, 0x0f
	mov bl, 0
	int 0x10

reset_drive:
	mov ah, 0
	int 0x13
	or ah, ah
	jnz reset_drive

	xor ax, ax ; set up the stack
	mov ss, ax
	mov sp, 0x600

	mov al, '1'
	call eNinePrint

loadGDT:
	xor ax, ax
	mov ds, ax
	lgdt [gdt_desc]

findRAM:
	xor cx, cx
	xor dx, dx
	mov ax, 0xe801
	int 0x15
	jc .err
	cmp ah, 0x86
	je .err
	cmp ah, 0x80
	je .err
	jcxz .useax
	mov ax, cx
	mov bx, dx
.useax:
	mov [0x1000], ax
	mov [0x1004], bx
	jmp .end
.err:
	mov al, 'e'
	call eNinePrint
	mov al, 'r'
	call eNinePrint
	mov al, 'r'
	call eNinePrint
	mov al, ' '
	call eNinePrint
.end:

check_for_a20:
	cli
	xor ax, ax
	mov es, ax
	not ax
	mov dx, ax
	mov di, 0x500
	mov si, 0x510
	mov al, byte [es:di]
	push ax
	mov al, byte [ds:si]
	push ax
	mov byte [es:di], 0x00
	mov byte [ds:si], 0xff
	cmp byte [es:di], 0xff
	pop ax
	mov byte [ds:si], al
	pop ax
	mov byte [es:di], al
	sti
	mov ax, 0
	je .exit
.enableATwenty:
	mov ax, 0x2401
	int 0x15
	mov al, 'Y'
	call eNinePrint
.exit:
	mov al, 'E'
	call eNinePrint

unrealMode:
	push ds
	mov eax, cr0 ; goto protected mode
	or al, 1
	mov cr0, eax ; end pmode switch
	mov bx, 0x08
	mov ds, bx ; kernel panic is caused either here
	and al, 0xfe ; go back to real mode ; or here as far as I can tell
	mov cr0, eax
	pop ds
	sti
;
;loadKernel: ; does nothing other than theoretically output a smiley to the screen
;	mov bx, 0xf01
;	mov eax, 0xb8000
;	mov word [ds:eax], bx

halt:
	hlt
	jmp halt

eNinePrint:
	out 0xe9, al
	ret

gdt:
gdt_null:
	dd 0
	dd 0
gdt_code:
	dw 0x0ffff
	dw 0
	db 0
	db 10011010b
	db 11001111b
	db 0
gdt_data:
	dw 0x0ffff
	dw 0
	db 0
	db 10010010b
	db 11001111b
	db 0
gdt_end:
gdt_desc:
	dw gdt_end - gdt - 1 ; the size of the gdt
	dd gdt ; address of the gdt

times 510-($-$$) db 0
	dw 0x0aa55
NOTE - This is the only code my kernel currently has. The compile line is simply:

Code: Select all

nasm -f bin bootloader.s -o bootloader.bin
cat bootloader.bin > floppy.img
Does anyone know a reason for this to be happening? Is there a way to tell if unreal mode is supported/ not supported?

Thanks for your time

Re: Unreal mode causing kernel panic

Posted: Wed Apr 04, 2012 10:21 am
by Nable
> [ORG 0x700]
afaik, bootsector is loaded at 0x7C00

Re: Unreal mode causing kernel panic

Posted: Wed Apr 04, 2012 10:30 am
by BetaWar
Bah! Woops :) Thanks for that. Now it just outputs a LOT of "write_virtual_checks(): no write access to seg" errors. I'll have to look into it... Would it be caused by the GDT not allowing that far (that much) memory to be used?

... and update:

I have run through the debugger a few time and am unable to find any error (in fact, when I step through the bootloader script manually it seems to work fine -- gets to the infinite loop "halt"). However, when I allow it to continue or just run it without stepping it quickly gets to the write_virtual_check error messages.

It is interesting to note that getting into unreal mode at least appears to work. If I comment that code out and continue to the "loadKernel" function it outputs a message "write_virtual_checks(): write beyond limit, r/w" instead of the no access error.

If I also comment out the "loadKernel" function it runs without error.