Page 1 of 1

Put me in ring3!

Posted: Sat Mar 03, 2007 10:52 am
by salil_bhagurkar
I need to move into ring3 in pmode.
My code:

Code: Select all

cli
	mov [tss_esp0],esp
	mov eax,0x900000                                 ; example stack
	mov [tss_esp],eax
	mov eax,esp
	push dword USER_DATA_SEL
	push eax
	push dword 0x00                                     ;eflags
	push dword USER_CODE_SEL
	lea eax,[ring3]
	push eax
	iret

ring3:
mov ax,USER_DATA_SEL
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
int 0x80
jmp $

syscall:
	push gs
	push fs
	push es
	push ds
	pusha
		mov ax,ss	
		mov ds,ax
		mov es,ax
		mov fs,ax
		call print 	;Function that prints a test message
	popa
	pop ds
	pop es
	pop fs
	pop gs
iret

; The idt part of int 0x80:

	dw syscall
	dw SYS_CODE_SEL
	db 0
	db 0xEE	; DPL=3
	dw 0

;;Following tss is also setup in the gdt correctly

tss:	dw 0, 0			; back link
tss_esp0:
	dd 0			; ESP0
	dw SYS_DATA_SEL, 0	; SS0, reserved
	dd 0			; ESP1
	dw 0, 0			; SS1, reserved
	dd 0			; ESP2
	dw 0, 0			; SS2, reserved
	dd 0			; CR3
	dd 0, 0			; EIP, EFLAGS (EFLAGS=0x200 for ints)
	dd 0, 0, 0, 0		; EAX, ECX, EDX, EBX
tss_esp:
	dd 0, 0, 0, 0		; ESP, EBP, ESI, EDI
	dw 0, 0			; ES, reserved
	dw 0, 0			; CS, reserved
	dw USER_DATA_SEL, 0	; SS, reserved
	dw 0, 0			; DS, reserved
	dw 0, 0			; FS, reserved
	dw 0, 0			; GS, reserved
	dw 0, 0			; LDT, reserved
	dw 0, 0			; debug, IO perm. bitmap
end:


int 0x80 is the syscall and its there in the idt as ring3. I successfully moved to the usermode. But when i make a syscall Bochs gives me an error about the io permission bitmap. Any code that either makes a syscall or calls any other function causes this problem. But none of the code uses io ports. Is this a stack problem? But then Bochs gives an iopb error... Help please...

Posted: Sat Mar 03, 2007 11:01 am
by Dex
This may help:

OoooooopS!

Posted: Sat Mar 03, 2007 11:07 am
by salil_bhagurkar
Dex, thats the code im using in my os... But its not working... Wat all do u need to setup and wat precautions should u take to pack ur bags and travel to ring3 ?

Posted: Sat Mar 03, 2007 11:25 am
by Dex
Sorry just posted the code i had, but if i remember right, when i tested it on real PC it worked fine ?.

Posted: Sat Mar 03, 2007 11:38 am
by salil_bhagurkar
Hmm... Last time i tried it dint work inspite of a lot of debugging by moving 'jmp $' around... Anyone has suggestions?