Can't call BIOS Interrupts after switch to real mode

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.
Post Reply
themodder
Posts: 3
Joined: Thu Oct 31, 2013 10:32 pm

Can't call BIOS Interrupts after switch to real mode

Post 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:
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
themodder
Posts: 3
Joined: Thu Oct 31, 2013 10:32 pm

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

Post by themodder »

This doesn't fix anything, same condition, except for that QEMU now crashes!YAY :mrgreen: ! None of the other interrupts work either.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

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

Post by DavidCooper »

I don't see you setting SS for real mode - it'll still be using protected mode content.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
themodder
Posts: 3
Joined: Thu Oct 31, 2013 10:32 pm

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

Post by themodder »

Thanks, worked like a charm! If no one minds ill modify the wiki code to factor this in.
Post Reply