howto transfer control from ring 0 to ring 3

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.
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

howto transfer control from ring 0 to ring 3

Post by orange »

Hi all....


Im playing in 64bit longmode

Can i directly transfer control from ring 0 to ring 3

i use far jmp to ring 3 GDT but the code raise exception.

Code: Select all

   jmp   gdt_code_ring3:ring3_code

ring3_code:
can u help to resolve my problem???

thanks...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

http://www.catb.org/~esr/faqs/smart-questions.html

in other words:
- you should've searched for an answer - specifically the intel manuals
- you provide far too few details to make a sensible diagnosis
- you use sms shortcuts.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: howto transfer control from ring 0 to ring 3

Post by AJ »

orange wrote: Can i directly transfer control from ring 0 to ring 3
Yes and No. If you mean 'can I do it with a far jump', the answer is no. You do it in the same way as you currently do your task switches (I assume if you have got to playing with ring 3, you do have a scheduler by now?).

Cheers,
Adam
Mikae
Member
Member
Posts: 94
Joined: Sun Jul 30, 2006 1:08 pm

Post by Mikae »

Also, you can use 'far ret' instruction to jump from r0 to r3. You have to push desired address (CS:RIP) on stack and execute 'far ret'.
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

for now i not use task to switching and i have change my code from "far jump" to "far ret". because i thing ring 0 can do anything, including jump to ring 3 directly.


i have do this for my code now

push code_selector_ring3
push offset_ring3
and then do retf

i just want to test syscall/sysret from ring3 to ring 0

thanks for help... next time i will attach my code.

or can u give me sample longmode with syscall/sysret... :lol:

Cheers.
Orange
User avatar
crazygray1
Member
Member
Posts: 168
Joined: Thu Nov 22, 2007 7:18 pm
Location: USA,Hawaii,Honolulu(Seriously)

Post by crazygray1 »

Caps your I's!!!

Sorry OCD
Codname: Cipher
Working On: Design Doc(CFFS file system)
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Caps your I's!!!

Sorry OCD
I have OCD too!

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

Sorry what that mean OCD???? :( :( :( :( :( :( :shock: :shock: :shock: :shock:
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

orange wrote:Sorry what that mean OCD???? :( :( :( :( :( :( :shock: :shock: :shock: :shock:
Are you serious? :roll: http://www.google.com/search?hl=en&q=OCD

Still, crazygray1.. you're considerably irritating as well, so please stay on topic. ;)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

:roll: :roll: :roll:

That OCD more make me confuse.. :lol: :lol: :lol:

and i still can't resolve my problem. but never mind i still work to find the answer..

:) :) :)
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

Hi guys...

this my code, whats wrong with this code.

i just want to jump to ring 3, but i get an error. :( :( :(

Code: Select all

ORG 0x7C00

	USE16

	cli

	lgdt	[cs:GDTR]

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

	jmp    8:PM_Start


GDTR:       dw 5*8-1
                 dd GDT	


GDT:	dw 0x0000,0x0000,0x0000,0x0000		; (0)  NUll Selector
	    dw 0xFFFF,0x0000,0x9A00,0x00CF		; (8)  Code Segment 32bit ring 0
	    dw 0xFFFF,0x0000,0x9200,0x00CF		; (16) Data Segment 32bit ring 0
	    dw 0xFFFF,0x0000,0xFA00,0x00CF		; (24) Code Segment 32bit ring 3
	   dw 0xFFFF,0x0000,0xF200,0x00CF		; (32) Data Segment 32bit ring 3

	USE32

PM_Start:
	mov	ax,16
	mov	ds,ax
	mov	es,ax
	mov	ss,ax
	mov 	fs,ax
	mov 	gs,ax

	mov	eax,'P M '
	mov	[0xB8000],eax

	push	dword 32			; SS
	push	dword Stack_Top		   ; ESP
	push	dword 0				; EFLAGS
	push	dword 24			; CS
	push	dword PM_Ring3		   ; EIP
	iret					       ; Jmp to ring 3

PM_Ring3:
	mov	ax,32
	mov	ds,ax
	mov	es,ax
	mov	fs,ax
	mov	gs,ax

	mov	eax,'R 3 '
	mov	[0B8000h],eax
	
	jmp	PM_Ring3


	db 512 dup (0)
Stack_Top:




Thanks...
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

You need the RPL of the segment to be 3. That means, or the value 24 with 3. I must say, I prefer these things to be shown in hex, especially when setting / clearing bits. This means it becomes 0x18 | 0x03.

If you test it in Bochs first, it will tell you what the error is which makes debugging a lot easier!

Cheers,
Adam
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

AJ wrote:You need the RPL of the segment to be 3. That means, or the value 24 with 3. I must say, I prefer these things to be shown in hex, especially when setting / clearing bits. This means it becomes 0x18 | 0x03.
Thank's Adam :D

but how to make RPL of the segment tobe 3????
If you test it in Bochs first, it will tell you what the error is which makes debugging a lot easier!
I test in vmware, because i can't use bochs. How to load img file without floppy in bochs??.

In vmware i just setting floppy use floppy image..


Cheers....
Orange
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

orange wrote: but how to make RPL of the segment tobe 3????
You OR 24 (0x18) with three which gives you 27 (0x1B) - set this as your CS instead of 24.

Cheers,
Adam
orange
Posts: 16
Joined: Sun Dec 16, 2007 11:13 pm

Post by orange »

I change my code like this...

first i change CS 24 tobe 27, but i still get an error :( . Then i try to change SS 32 tobe 35,,,, and thanks GOD, that ist works.

Code: Select all

push	dword 35             ; SS
push	dword Stack_Top ; ESP
push	dword 0              ; RFLAGS
push	dword 27            ; CS
push	dword PM_Ring3	; EIP
iret                               ; Jmp to ring 3


Thank's Adam....


Cheers...
Orange
Post Reply