Problem with protected mode jump

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.
Ferrarius
Member
Member
Posts: 69
Joined: Sun Oct 28, 2007 4:10 pm

Problem with protected mode jump

Post by Ferrarius »

hi,

I'm still quite early in the development of my kernel. Which I wanted to build from the ground up and run in protected mode. After writing a bootloader succesfully enabling the A20 line and loading the kernel (both has been succesfully tested in bochs). I decided to move to the kernel as quickly as possible and make the kernel responsible for strting PM. After creating a GDT I decided to firt test whether this would work before moving on. As follows the GDT and kernel test code:

Code: Select all

GDT:

;####Null-Descriptor####
 dw 0x0000
 dw 0x0000
 db 0x00
 db 0x00
 db 0x00
 db 0x00

;####Code Ring 0####
 dw 0xFFFF   ;Limit
 dw 0x0000   ;Base
 db 0x00     ;Base
 db 0x9A     ;Present, Code|Execute/Read
 db 0xCF     ;Granu,32bits|Limit    
 db 0x00     ;Base

;####Data Ring 0####
 dw 0xFFFF   ;Limit
 dw 0x0000   ;Base
 db 0x00     ;Base
 db 0x92     ;Present, Data|R/W E-up
 db 0xCF     ;Granu,32bits|Limit
 db 0x00     ;Base

;####Code Ring 3####
 dw 0xFFFF   ;Limit
 dw 0x0000   ;Base
 db 0x00     ;Base
 db 0xFA     ;Present, Code|Execute/Read
 db 0xCF     ;Granu,32bits|Limit
 db 0x00     ;Base

;####Data Ring 3####
 dw 0xFFFF   ;Limit
 dw 0x0000   ;Base
 db 0x00     ;Base
 db 0xF2     ;Present, Data|R/W E-up
 db 0xCF     ;Granu,32bits|Limit
 db 0x00     ;Base

;####Task State Segment####
 dw 103      ;Limit
 dw 0        ;Base
 db 0        ;Base
 db 0x89     ;Ring 0, Present, Non-Busy
 db 0x00     ;BS
 db 0xC0     ;Base
GDTEND:

Code: Select all

[ORG 0x00020000]
[BITS 16]
main:
mov   ax,0xB800
mov   es,ax
mov   byte[es:0],'b'
cli
xor   ax,ax
mov   ds,ax

lgdt  [GDTDESC]
mov   eax,cr0
or    eax,1
mov   cr0,eax
jmp   0x08:enter_pmode

[BITS 32]
enter_pmode:
mov   ax, 0x08
mov   cs, ax
mov   ax, 0x10
mov   ds, ax
mov   ss, ax
mov   esp,0x90000

.print_char:
mov   byte[ds:0x0B8002],'a'
mov   byte[ds:0x0B8003],0x7F

.hang:
jmp .hang

%include "d:\vinitech\mik\gdt006.asm"

GDTDESC:
dw (GDTEND - GDT - 1)
dd GDT
I've tried several ways to get this working but I've reached the end of my Latin. According to bochs the kernel triple faults when jumping to enter_pmode
Modular Interface Kernel With a lot of bugs ;)
Martijn
Posts: 22
Joined: Tue Feb 26, 2008 3:43 am
Location: The Netherlands

Re: Problem with protected mode jump

Post by Martijn »

Remove 'mov cs, ax'. Mov can't place a value in the cs register.
Only 'far' control-transfer instructions (retf, jmp 0x?:0x?, ...) can change cs.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Problem with protected mode jump

Post by System123 »

Ferrarius wrote:[BITS 32]
enter_pmode:
mov ax, 0x08
mov cs, ax
mov ax, 0x10
mov ds, ax
mov ss, ax
mov esp,0x90000
Your problem like Martijn said is that you can not change cs like you have. You have alreaady changed the cs register when you jumped like:
Ferrarius wrote:jmp 0x08:enter_pmode
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Ferrarius
Member
Member
Posts: 69
Joined: Sun Oct 28, 2007 4:10 pm

Re: Problem with protected mode jump

Post by Ferrarius »

I've changed the oversight, it however changes nothing about the triple faults.
Modular Interface Kernel With a lot of bugs ;)
Martijn
Posts: 22
Joined: Tue Feb 26, 2008 3:43 am
Location: The Netherlands

Re: Problem with protected mode jump

Post by Martijn »

I think your compiler already does this for you automatically, but you can give it a try.

Replace: jmp 0x08:enter_pmode
With: jmp pword 0x08:enter_pmode
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Problem with protected mode jump

Post by egos »

And what this means?

Code: Select all

[ORG 0x00020000]
...
xor   ax,ax
mov   ds,ax

lgdt  [GDTDESC]
Segment size in the real mode is 0x10000. Whether you indicated not too large displacement.
If you have seen bad English in my words, tell me what's wrong, please.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Problem with protected mode jump

Post by System123 »

Why are you doing [ORG 0x20000] is that where your code is loaded?

try this code in place of
Ferrarius wrote: cli
xor ax,ax
mov ds,ax

lgdt [GDTDESC]
mov eax,cr0
or eax,1
mov cr0,eax
jmp 0x08:enter_pmode

Code: Select all

	cli				; clear interrupts
	xor	ax, ax			; null segments
	mov	ds, ax
	mov	es, ax
	mov	ax, 0x9000		; stack begins at 0x9000-0xffff
	mov	ss, ax
	mov	sp, 0xFFFF
	sti	

	lgdt  [GDTDESC]

        cli
        mov   eax,cr0
        or    eax,1
        mov   cr0,eax
        jmp   0x08:enter_pmode
Gizmic OS
Currently - Busy with FAT12 driver and VFS
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Problem with protected mode jump

Post by AJ »

Hi,

lgdt[GDTDESC] actually loads from ds:[GDTDESC]. This means that clearing DS by loading it with a null AX stops you from accessing the GDT descriptor if you are above the 0xFFFF mark. That ORG 0x20000 also definitely dodgy to me in RMode.

Do you actually mean ORG 0x0000? In this case, you could place JMP 0x2000:entry at the start of your code, meaning that you are actually running at your desired offset.

Cheers,
Adam
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Problem with protected mode jump

Post by sebihepp »

Hi,

hmmm... does the assembler not add 0x2000 to the ds:[GDTDESC], because of the org directive?
So that should be right.
But when you jump to the PMode, your assembler adds 0x2000 to the address of enter_pmode too.
But you can only use 16 bit for this address and that results, that the assembler adds 0x0002 to the
GDT-Value of the jump. You can change the base of the GDT-Entry, you jump to, to 0x2000, but this
will cause you to change many things in your code.

Greetings Sebihepp
Ferrarius
Member
Member
Posts: 69
Joined: Sun Oct 28, 2007 4:10 pm

Re: Problem with protected mode jump

Post by Ferrarius »

[ORG 0x00020000] is indeed the place I've loaded the program into memory.

The jump is made now, the program arrives at enter_pmode. It however still triple faults at

Code: Select all

mov ds,ax
Modular Interface Kernel With a lot of bugs ;)
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Problem with protected mode jump

Post by sebihepp »

hmmm, just try

Code: Select all

mov ax, (2<<3)
I had the same problem earlier and this solved it.
But I don't know why, because (2<<3) is equal to 0x10. It should be equal
at least...
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Problem with protected mode jump

Post by System123 »

I had the same problem however this was due to a fault in the pmode jump. Just double check the code. Post your new source here so we can have a look.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Ferrarius
Member
Member
Posts: 69
Joined: Sun Oct 28, 2007 4:10 pm

Re: Problem with protected mode jump

Post by Ferrarius »

The ocde as it stands now:

Code: Select all


[ORG 0x00020000]
[BITS 16]
main:
mov   ax,0xB800
mov   es,ax
mov   byte[es:0],'b'
cli
xor   ax,ax
mov   ds,ax
mov   es,ax
mov   ax,0x9000
mov   ss,ax
mov   sp,0xffff
sti

lgdt  [GDTDESC]
cli
mov   eax,cr0
or    eax,1
mov   cr0,eax
jmp   dword enter_pmode

%include "d:\vinitech\mik\gdt006.asm"

GDTDESC:
dw (GDTEND - GDT - 1)
dd GDT

[BITS 32]
enter_pmode:
mov   eax, 0x10
mov   ds,ax
mov   es,ax
mov   esp,0x00090000
sti

.print_char:
mov   byte[ds:0x000B8002],'a'
mov   byte[ds:0x000B8003],0x7F

.hang:
jmp .hang


times 512 - ($ - $$) db 0
Modular Interface Kernel With a lot of bugs ;)
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Problem with protected mode jump

Post by egos »

You did not remove the error mentioned by me.

Code: Select all

mov   ax,0x9000
mov   ss,ax
mov   sp,0xffff
I'm crying seeing this: mov sp,0xffff :cry:
Furthermore, you try to use Extended BDA as a stack.
If you have seen bad English in my words, tell me what's wrong, please.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Problem with protected mode jump

Post by System123 »

Ferrarius wrote:jmp dword enter_pmode
You need to set the cs! rather jump like so:

Code: Select all

jmp 08h:enter_pmode
The other thing is you can't enable interrupts "sti" until you have an IDT set up. the CPU is getting confused.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Post Reply