Page 3 of 4

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:40 am
by czlowieczek
You mean I should do pmode enabling in kernel ??

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:55 am
by egos
czlowieczek wrote:You mean I should do pmode enabling in kernel ??
Yes, in kernel or in stage 2 boot loader, not in stage 1.

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 3:10 am
by Casm
czlowieczek wrote:You mean I should do pmode enabling in kernel ??
It means that you should switch into protected mode whilst you are still in the first megabyte of memory, because that is all a sixteen bit instruction pointer can manage, and being in "flat" real mode doesn't change that - it only allows data accesses above 1mb.

When, in olden days, real mode MS-DOS programs had their code sections restricted to the first 1mb, it wasn't because they had taken a vow of poverty, so far as memory was concerned.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 5:28 am
by czlowieczek
Ok, I found better bootloader and my kernel is working...... partly. My kernel turn protected mode on and load gdt but crash after trying to reload any segmen ds,es ... and when i'm trying to reload cs by jumping (jmp 08h:start32) it crashes too. :( Bootloader load my kernel at adress 0000h:500h

Code: Select all

[bits 16]
[org 500h]
jmp start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

gdt:

  dd 0
  dd 0
 
 
  dw 0xFFFF    
  dw 0        
  db 0         
  db 10011010b 
  db 11001111b 
  db 0         
 
 
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 
; naglowek
gdt_descr:
  dw gdt_end - gdt - 1    
  dd gdt 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

start:
cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax
[bits 32]

xor eax, eax
mov esp, 0x8000

jmp 08h:start32

start32: 

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov gs, ax
mov fs, ax

petla:


jmp petla
I think that is the last issue in my code :)

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 7:46 am
by egos
What's this epidemic!

The following code is very bad.

Code: Select all

mov eax, cr0
or eax, 1
mov cr0, eax
[bits 32]

xor eax, eax ; really it's "xor ax,ax"
mov esp, 0x8000 ; "mov sp,8000h" and "dw 0"!!!

jmp 08h:start32 ; "jmp 0:start32" and "dw 8"!!!

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 9:40 am
by czlowieczek
Egos, I don't understand your
xor eax, eax ; really it's "xor ax,ax"
mov esp, 0x8000 ; "mov sp,8000h" and "dw 0"!!!
Because, I want xor EAX not AX, and I want to move 8000h to ESP not to SP !!

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:06 am
by Gigasoft
You have an incorrect bits 32 directive, causing the generation of wrong instructions. Move it to the start32 label.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:15 am
by czlowieczek
Ok, I moved it after start32 section, but jump to that code (jmp 0x08:start32) generates an error too.

My code:

Code: Select all

[bits 16]
[org 500h]
jmp start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

gdt:

  dd 0
  dd 0
 
 
  dw 0xFFFF    
  dw 0        
  db 0         
  db 10011010b 
  db 11001111b 
  db 0         
 
 
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 
; naglowek
gdt_descr:
  dw gdt_end - gdt - 1    
  dd gdt 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

start:
cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax
[bits 32]


jmp 0x08:start32

start32: 

xor eax, eax
mov esp, 0x8000

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov gs, ax
mov fs, ax

petla:


jmp petla

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:41 am
by egos
It's not summer school here. Learn Intel manuals.

To run 32-bit code you should jump to 32-bit code.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:48 am
by czlowieczek
I am trying but bosch report an error at jumping to code (jmp 0x08:start32)

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:57 am
by egos
As I said, my code works fine. I tested it in Bochs.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 10:59 am
by Mikemk
czlowieczek wrote:Ok, I moved it after start32 section, but jump to that code (jmp 0x08:start32) generates an error too.

My code:

Code: Select all

  dw 0xFFFF    
  dw 0        
  db 0         
  db 10011010b 
  db 11001111b 
  db 0         
This results in:
0xFFFF0000009ACF00
base: 0xFF00009A
limit: 0xFCF00
flags:
GR=true SZ=true L=true false=true
access:
pr=false privilege=ring0 true=false
code/data=data direction=up read/write=false accessed=false
Try:

Code: Select all

dq 0
dq 0xCF9A000000FFFF
dq 0xCF92000000FFFF
(If you want to modify, most programming calculators let you set or unset individual bits. I suggest you get one.)
mov eax, cr0
or eax, 1
mov cr0, eax
[bits 32]


jmp 0x08:start32

start32:
The [bits 32] is still in front of the jmp instruction.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 11:09 am
by egos
m12, descriptors were correct.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 11:26 am
by Casm
dw gdt_end - gdt - 1
It seems to me that should be: dw gdt_end - gdt + 1.

If your gdt was only 8 bytes long, and it began at (say) 16h the second byte would be, 17h, the third byte 18h, and so on up to 1dh

1dh - 16h = 7, which is one short of what it should be.

Re: 32bit protected mode init problem

Posted: Fri Jul 12, 2013 11:26 am
by czlowieczek
I used them, but I had the same error
Code:

Code: Select all

[bits 16]
[org 500h]
jmp start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

gdt:

  dq 0
dq 0xCF9A000000FFFF        ;m12 descriptors
dq 0xCF92000000FFFF       
  

gdt_end:    
 
gdt_descr:
  dw gdt_end - gdt + 1 ; +1    
  dd gdt 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

start:
cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax

[bits 32]         ;before jump

jmp 0x08:start32

start32: 

xor eax, eax
mov esp, 0x8000

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov gs, ax
mov fs, ax

petla:


jmp petla