2

i'm trying to go back to real mode, after protected, and processor just got stuck right after changing cr0 register. I'm using nasm, and compile my program as binary .img to run under virtualbox as bootloader. Probably I'm missing something with long jump back to realMain proc.

[org 0x7C00] ; BIOS boot origin
[bits 16] ; 16-bit Real Mode

jmp start ;Jump to start() entry-point
%include "routines16.asm"
[bits 16]

start:
  mov si, welcomeMsg
  call print
  call getKey
  call clear

realMain:
  cli
  mov ax, cs
  mov ds, ax
  mov es, ax
  ; mov ax, STACK16
  ; mov ss, ax
  sti

  mov si, inrealmsg
  call print

  mov si, anykeyMsg
  call print
  call getKey

  call toProtected

toProtected:
  mov si, toprotectedmsg
  call print
  call clear;
  ;move
  cli
  lgdt [gdt32_descriptor] ; Load GDT
  mov eax, cr0
  or eax, 0x1
  mov cr0, eax
  jmp dword CODE_SEG_32:protectedMain

;;;;;;;;;;;;;;
welcomeMsg db "IPR1-degtyarev. Press any key to start", 0x0
inrealmsg db "in real", 0x0
toprotectedmsg db "switching to protected", 0x0
anykeyMsg db "Press any key to switch cpu mode...", 0x0
;;;;;;;;;;;;;;

%include "gdt32.asm"
%include "routines32.asm"

[bits 32]

protectedMain:
  mov eax, DATA_SEG_32
  mov ds, eax
  mov es, eax

  mov eax, 0x0000; zero line
  mov ebx, inprotectedmsg
  call print32
  add eax, 0x00A0; moving next line
  call toReal

toReal:
  mov edx, eax
  mov ebx, torealmsg
  call print32
  ;move
  cli

  mov eax, cr0
  dec al
  mov cr0, eax

  jmp 0x7C00:realMain

end:
  jmp end

;;;;;;;;;;;;;;
inprotectedmsg db "in protected", 0x0
torealmsg db "switching to real", 0x0
;;;;;;;;;;;;;;

times 510 - ($-$$) db 0 ;Fill the rest of the bootloader with zeros
dw 0xAA55 ;Boot signature

Attached GDT32

; Descriptor CONFIG
gdt32_start:

gdt32_null:         ; Initialization null
    dq 0x0

gdt32_cs:
    dw 0xFFFF    ; Limit
    dw 0x0000    ; Base
    db 0x0000    ; Base 23:16
    db 10011011b ; [p][dpl][][s][type][][][a]
    db 11011111b ; [g][x][0][avl][lim][][][]
    db 0x0000

gdt32_ds:
    dw 0xFFFF    ; Limit
    dw 0x0000    ; Base
    db 0x0000    ; Base 23:16
    db 10010011b
    db 11011111b
    db 0x0000

gdt32_end:         ; Pour avoir la taille du GDT

gdt32_descriptor:
    dw gdt32_end - gdt32_start - 1 ; GDT size
    dd gdt32_start

; Constants to get address of gdt32
CODE_SEG_32 equ gdt32_cs - gdt32_start
DATA_SEG_32 equ gdt32_ds - gdt32_start
xdegtyarev
  • 135
  • 1
  • 8
  • 2
    See [this OSDev wiki entry](http://wiki.osdev.org/Real_Mode#Switching_from_Protected_Mode_to_Real_Mode). – zx485 Nov 17 '16 at 19:49
  • 1
    Not a minimal complete verifiable example but this looks very wrong `jmp 0x7C00:realMain` that would be address (0x7C00<<4)+realMain = 0x7c000+realMain` which is not what you want. Since you used `org 0x7c00` you should use something like `jmp 0x0000:realMain`. There may be other problems but this is one stands out. – Michael Petch Nov 17 '16 at 21:18
  • I've just launched a vbox dbg, and here's what I get when jumping back to realMain: `cs={f000 base=000f0000 limit=ffffffff flags=d09b}` `ds={f000 base=000f0000 limit=ffffffff flags=d093}` `u: error: DBGCCmdHlpVarToDbgfAddr failed on 'f000:072aba87 L 0': VERR_OUT_OF_SELECTOR_BOUNDS` – xdegtyarev Nov 17 '16 at 21:32
  • It is hard to tell because you don't provide all your code, we don't see your descriptor table etc. (you don't have enough info to really help).. If you are trying to debug this rather than using Virtual Box, use Bochs and step through it and look at the register and descriptor state. The real mode debugging is better in that environment. – Michael Petch Nov 17 '16 at 21:41
  • Among other things we don't see you set the 16 bit selectors to a GDT entry that is set up for 16-bit data. We also don't see you set the 16-bit segment registers after you jump back into real mode (which is probably yet another bug) – Michael Petch Nov 17 '16 at 21:47
  • As zx385 points out there is a link to OSDev with an article on how to do it properly. – Michael Petch Nov 17 '16 at 21:50
  • Thanks Michael and zx485 for pointing to OSDev. I've also attached GDT entry – xdegtyarev Nov 17 '16 at 22:21

0 Answers0