Page 2 of 4

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:24 pm
by egos
AJ gave good advice. Load your kernel somewhere in first 64 kb. In this case you can use linear addressing in RM as well as in PM. For example:

Code: Select all

  org 8000h

  mov ax,3
  int 10h ; I hope your stack is enough

  xor ax,ax
  mov ds,ax
  lgdt [GDTR]

  cli

  mov eax,cr0
  or al,1
  mov cr0,eax

  jmp CODESEL:start32 ; reset cs first

  use32
start32:
  mov eax,DATASEL
  mov ds,ax
  mov es,ax
  mov fs,ax
  mov gs,ax
  mov ss,ax
  mov esp,$$

  mov word [0B8000h],"x" + 7 shl 8
@@:
  hlt
  jmp @b
  ...

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:28 pm
by czlowieczek
I created new kernel with max size 512 ( i load it as bootloader), it works, but when i'm trying too put character 'x' it doesn't :(

There is fat12 header, beacuse I'm doing this on fat 12 formatted floppy

Code: Select all

[BITS 16]
[ORG 7C00h]


jmp     short   start
nop

bsOemName               DB      "DONTOST "      ; 0x03
bpbBytesPerSector       DW      512               ; 0x0B
bpbSectorsPerCluster    DB      1               ; 0x0D
bpbReservedSectors      DW      1               ; 0x0E
bpbNumberOfFATs         DB      2               ; 0x10
bpbRootEntries          DW      224               ; 0x11
bpbTotalSectors         DW      2880               ; 0x13
bpbMedia                DB      240               ; 0x15
bpbSectorsPerFAT        DW      9               ; 0x16
bpbSectorsPerTrack      DW      18               ; 0x18
bpbHeadsPerCylinder     DW      2               ; 0x1A
bpbHiddenSectors        DD      0               ; 0x1C
bpbTotalSectorsBig      DD      0               ; 0x20

bsDriveNumber           DB      0               ; 0x24
bsUnused                DB      0               ; 0x25
bsExtBootSignature      DB      41               ; 0x26
bsSerialNumber          DD      0x11            ; 0x27
bsVolumeLabel           DB      "DONTOSTBOOT"   ; 0x2B
bsFileSystem            DB      "FAT12   "      ; 0x36

start:

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

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

mov esp, 8000h

push word 0x08
push word start32

[bits 32]
retf

start32: 

mov al, 'x'
mov edi, 0B8000h
stosb
 
petla:
nop
hlt
jmp petla

times 365 db 0

gdt:
  ; NULL Descriptor
  dd 0
  dd 0
 
  ; kod, baza: 0, limit: 4GB, DPL: 0
  dw 0xFFFF    ; mlodsze slowo limitu
  dw 0         ; mlodsze slowo bazy
  db 0         ; wlodszy bajt starszego slowa bazy
  db 10011010b ; kod / exec-read
  db 11001111b ; flagi i 4 bity limitu
  db 0         ; najstarszy bajt bazy
 
  ; dane (odczyt/zapis), baza: 0, limit: 4GB, DPL: 0
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 
; naglowek
gdt_descr:
  dw gdt_end - gdt - 1    ; rozmiar gdt
  dd gdt 
  
 dw      0AA55h]

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:34 pm
by egos

Code: Select all

push word 0x08
push word start32

[bits 32]
retf
Wow, you're crazy programmer :D

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:39 pm
by czlowieczek
Why you think so, When I was trying

Code: Select all

jmp 0x08:start32
I had error in my bosch console and hardware restart :D

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:47 pm
by egos
Your trick has no effect. In my code jump instruction is not so good too. Try this or something like this:

Code: Select all

jmp fword CODESEL:start32

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:03 pm
by czlowieczek
But my "trick" works good, it goes into infinite loop according to my bosch console :)

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:18 pm
by egos
I meant that the following code gives the same result:

Code: Select all

push word 0x08
push word start32
retf
But using 32-bit offset in this case is more preferable.
czlowieczek wrote:but when i'm trying too put character 'x' it doesn't :(
My code works fine.

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:40 pm
by Casm
If you are using flat protected mode, with all the segments based at zero, then before switching to protected mode the cs:ip at the entry point to your code should (in theory) be 0:0x80000 and the org (in theory) should be 0x80000 - so that offset addresses before and after switching to protected mode were the same. Except that ip can't be loaded with 0x80000, because it is a sixteen bit register.

The obvious solution to your problem is to switch to protected mode before leaving the boot loader. Then the eip register will be available for any jumps you want to make. You can set up a temporary GDT for the purpose.

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 1:30 am
by czlowieczek
I wrote new bootloader with protected mode enabling, but i have error in nasm 'mov bp, ??' :FCFD My error is at eip 0x7D6A, I think that the last error in my code :)

Code: Select all

[BITS 16]
[ORG 7C00h]


jmp     short   start
nop

bsOemName               DB      "DONTOST "      ; 0x03
bpbBytesPerSector       DW      512               ; 0x0B
bpbSectorsPerCluster    DB      1               ; 0x0D
bpbReservedSectors      DW      1               ; 0x0E
bpbNumberOfFATs         DB      2               ; 0x10
bpbRootEntries          DW      224               ; 0x11
bpbTotalSectors         DW      2880               ; 0x13
bpbMedia                DB      240               ; 0x15
bpbSectorsPerFAT        DW      9               ; 0x16
bpbSectorsPerTrack      DW      18               ; 0x18
bpbHeadsPerCylinder     DW      2               ; 0x1A
bpbHiddenSectors        DD      0               ; 0x1C
bpbTotalSectorsBig      DD      0               ; 0x20

bsDriveNumber           DB      0               ; 0x24
bsUnused                DB      0               ; 0x25
bsExtBootSignature      DB      41               ; 0x26
bsSerialNumber          DD      0x11            ; 0x27
bsVolumeLabel           DB      "DONTOSTBOOT"   ; 0x2B
bsFileSystem            DB      "FAT12   "      ; 0x36

start:
  xor dl, dl
  mov ah, 02h
  mov al, 3
  mov ch, 1
  mov cl, 16
  mov dh, 00h
  mov bx, 0x1000
  mov es, bx
  mov bx, 00h
  int 0x13
  
  xor dl, dl
  mov ah, 02h
  mov al, 16
  mov ch, 2
  mov cl, 1
  mov dh, 00h
  mov bx, 0x1096
  mov es, bx
  mov bx, 00h
  int 0x13  

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

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax      ;it works  fine
mov gs, ax
mov fs, ax

mov esp, 8000h ;that too

push word 0x08
push word start32 ;It is working good (cs is reloading)

[bits 32]
retf

start32: 

jmp 0x10000 ;jmp to code

times 328 db 0

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:    
 

gdt_descr:
  dw gdt_end - gdt - 1    
  dd gdt 
  
 dw      0AA55h
and my little "kernel"

Code: Select all

[Bits 32]
[org 10000h]

petla:
nop
hlt
jmp petla

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:16 am
by egos
czlowieczek wrote:I wrote new bootloader with protected mode enabling
Very bad design.

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:36 am
by Combuster
The whole point of this whole thing is that you learn how to debug. Not throw away code and rewrite something until it just happens to work.

In other words, I'm getting the idea you haven't quite learned how to program yet and you're trying something way above your league.

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 :)