Protected Mode problem

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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Protected Mode problem

Post by bluemoon »

The first exception(06) is undefined opcode, it suggest that you might have jumped to bogus address.

Instead of trial and error, patching here and there to fix that issue,
bluemoon wrote:You should read more of different addressing methods and their transformation formula.
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: Protected Mode problem

Post by brain »

Yes you should set up an idt anyway, or keep interrupts disabled or the first one to occur has no place to go except to fail city :-) keeping them disabled forever though would be really impractical for obvious reasons :-)
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: Protected Mode problem

Post by Combuster »

Disabling interrupts doesn't help for a crashing kernel, only for avoiding crashes by external peripherals.
"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 ]
sumanx
Posts: 9
Joined: Wed Feb 01, 2012 9:17 am

Re: Protected Mode problem

Post by sumanx »

Invalid opcode? How?
:o
sumanx
Posts: 9
Joined: Wed Feb 01, 2012 9:17 am

Re: Protected Mode problem

Post by sumanx »

But my address is right, right?

FirstBootloader.asm

Code: Select all


[BITS 16]
[ORG 0x7C00]

jmp start


start:

mov ax,0x1000
mov es,ax
mov bx,0x0000
xor ax,ax
mov ah,2
mov al,1
mov ch,0
mov cl,2
mov dh,0
mov dl,0
int 0x13
jmp 0x1000:0x0000



TIMES 510-($-$$) db 0
db 0x55
db 0xAA
Second Stage:

Code: Select all


[ORG 0]
jmp main

gdt_t:
;----------------

dd 0
dd 0
;----------------

dw 0xFFFF
dw 0
db 0
db 10011010b
db 11001111b
db 0
;----------------
dw 0xFFFF
dw 0
db 0
dw 10010010b
db 11001111b
db 0
end:

gdt:
cli 
mov ax,cs
shl eax,4
add eax,gdt_t
mov [base],eax
mov ax,0
mov ds,ax
lgdt [addr]
ret

addr:
dw end - gdt_t -1
base dd 0

main:

call gdt
mov eax,cr0
or eax,1
mov cr0,eax

jmp 0x08:begin

bits 32

begin:

cli
hlt


Code descriptor is at 0x08, it should work??
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Protected Mode problem

Post by VolTeK »

sumanx wrote:Code descriptor is at 0x08, it should work??

Your emulator will answer that question ;)
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Protected Mode problem

Post by neon »

it should work??
Welcome to kernel land: What you see isn't what the machine sees. If it crashes, your code has a bug. Stop looking for reasons why it "should" work and look for why it doesn't. This is through debugging. If you don't have a debugger, get one.

There are a few problems:

1. No stack;
2. Relying on CS on start instruction of VBR (your "jmp start"). It is preferred to use a far jump;
3. Segments used before initialized;
4. No check for disk read error;
5. Improper use of ORG in "Second stage" when CS=0x1000, DS=?

The general rule of thumb I personally go by is to either set segments=0 with a valid ORG offset, or set ORG to 0 with segments properly set up. ie; in your case your "Second stage" can use ORG 0 with CS=DS both set to 0x1000 (meaning the program is loaded at 0x1000:0, or 64k)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply