Page 1 of 1

Can't call BIOS Interrupts after switch to real mode

Posted: Tue Apr 08, 2014 7:36 pm
by themodder
In my kernel, I am testing a 16 bit 'payload' to run after exit from 32-bit mode. (What I do is compile file w/ nasm, encode with base64, copy paste to C file and decode in kernel) After the jump to 16-bit mode, my code is something like this (Most of the switch to real mode is ripped off from the wiki):

Code: Select all

[ORG 0x7E00]
[bits 16]


Entry16:
        ; We are already in 16-bit mode here!
 
	cli			; Disable interrupts.
 
	; Need 16-bit Protected Mode GDT entries!
	mov eax, 0x30	; 16-bit Protected Mode data selector.
	mov ds, eax
	mov es, eax
	mov fs, eax
	mov gs, eax
 
	; Disable paging (we need everything to be 1:1 mapped).
	mov eax, cr0
	mov [savcr0], eax	; save pmode CR0
	and eax, 0x7FFFFFFe	; Disable paging bit & enable 16-bit pmode.
	mov cr0, eax
	lidt [idt_real]
	mov sp, 0x8000
	jmp 0:GoRMode

GoRMode:
	
	mov ax, 0		
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	sti
printHAI:
	mov ax,0xB800
	mov es,ax
	xor ax,ax
	mov al,'H'
	mov ah,'!'
	mov word [es:0],ax
	mov al,'A'
	mov ah,'!'
	mov word [es:2],ax
	mov al,'I'
	mov ah,'!'
	mov word [es:4],ax
	mov al,'!'
	mov ah,'!'
	mov word [es:6],ax
clrScreen:
        xor ax,ax
        int 0x10
idt_real:
	dw 0x3FF		; 256 entries, 4b each = 1K
	dd 0			; Real Mode IVT @ 0x0000
 
savcr0:
	dd 0	


This code successfully puts "HAI!" on the screen, but doesnt seem to clear the screen. As a result Im very confused :? . Could someone pls help? :mrgreen:

Re: Can't call BIOS Interrupts after switch to real mode

Posted: Tue Apr 08, 2014 8:40 pm
by Brendan
HI,

Code: Select all

GoRMode:
	
	mov ax, 0		
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
You also need to load a "real-mode compatible" value into CS here. If you don't (e.g. CS is left set to a 16-bit GDT entry) then for any interrupt the value of CS pushed on the stack won't make sense with the interrupt handler returns and you'll end up executing bytes at a dodgy address.


Cheers,

Brendan

Re: Can't call BIOS Interrupts after switch to real mode

Posted: Tue Apr 08, 2014 10:49 pm
by themodder
This doesn't fix anything, same condition, except for that QEMU now crashes!YAY :mrgreen: ! None of the other interrupts work either.

Re: Can't call BIOS Interrupts after switch to real mode

Posted: Wed Apr 09, 2014 11:29 am
by DavidCooper
I don't see you setting SS for real mode - it'll still be using protected mode content.

Re: Can't call BIOS Interrupts after switch to real mode

Posted: Thu Apr 10, 2014 12:50 am
by themodder
Thanks, worked like a charm! If no one minds ill modify the wiki code to factor this in.