Protected Mode Question

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
ComputerPsi
Member
Member
Posts: 83
Joined: Fri Oct 22, 2004 11:00 pm

Protected Mode Question

Post by ComputerPsi »

I have a kernel at ring 0 and a program at ring 3. Both are non-conforming, readable, code. I want to execute the program. Normally in real mode, I would just jump to it. Here, it's giving me an error saying that it's non-conforming. Anybody know how to execute the program? I am executing it from my kernel - I don't know why this error is appearing.
Anything is possible if you put your mind to it.
ComputerPsi
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

you cannot jump to ring3 from ring0

normal way to do this(both by common use and intel recomendation -- see ), is to set up the stack, as if the process had been interupted by an interupt/exception, and then iret into it (see the stack setup in intel volume 3a, figure 5-4 (section 5.12.1) for PMode, figure 5-8 (5.14.2-3) for LMode)

feel free to ask if you have any further questions, or this doesnt answer the question you had
ComputerPsi
Member
Member
Posts: 83
Joined: Fri Oct 22, 2004 11:00 pm

Post by ComputerPsi »

Interesting.. must iret or iretd be used, since it is in protected mode? Also, do you know how to formulate this? (How is the stack read by iret and iretd?) ..Sorry if I am asking questions that are in the manual.. my computer can't run acrobat.
Anything is possible if you put your mind to it.
ComputerPsi
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

There are a lot of html versions of the original 386 manuals which should work for you still. Here'e one with the section I think you need - http://pdos.csail.mit.edu/6.828/2005/re ... s09_06.htm
ComputerPsi
Member
Member
Posts: 83
Joined: Fri Oct 22, 2004 11:00 pm

Post by ComputerPsi »

It didn't work.. I pushed eflags, the new cs register and the new eip register, and did an Iretd instruction. When the program was ring 0, it worked. When I changed the program back to ring 3, I got the same error as before.

To be exact, this is what Bochs gave me:
check_cs: non-conforming code seg descriptor dpl != cpl
Anything is possible if you put your mind to it.
ComputerPsi
ComputerPsi
Member
Member
Posts: 83
Joined: Fri Oct 22, 2004 11:00 pm

Post by ComputerPsi »

okay.. I think I figured out what was wrong.. Never using rings other than zero before, I made my selector 32 (100000b), but I think a correction must be made to 100011b. When I did that, I got a different error:
"iret: SS selector null"
I'm not sure what this means.. Here is the code I jump with:

pushfd
push dword 100011b
push dword 0
iretd

Anybody know how to fix this? Is it something with the flags that I am not changing?
Anything is possible if you put your mind to it.
ComputerPsi
Toaster
Member
Member
Posts: 25
Joined: Wed Jul 19, 2006 7:11 am
Location: Austria

Post by Toaster »

yeah its quite easy:

when the processor switches from an interrupt back to CPL3, the stack have to be switched
and the intel manual says then the stack segment and stack pointer are pushed on the stack

so if your iret (the d isn't required if bits is 32) returns back from CPL0 to CPL3 you have to push ss and esp

some part of code of my operating system (entering the interrupt):

; the stack after entering:

; high
; [esp+16] stack segment
; [esp+12] stack pointer
; [esp+8] eflags
; [esp+4] cs
; [esp+0] eip
; esp system stack (in Kernel memory)
; low

greetings,

Toaster
E-Mail Adresse (= MSN): [email protected]
ICQ: 249-457-459
http://www.viennacomputerproducts.com/
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

ComputerPsi wrote:Sorry if I am asking questions that are in the manual.. my computer can't run acrobat.

heres an idea:
order the intel manuals from intel -- just send an email to the address listed in the 'order hard copies' link at the bottom of the page (link is in my sig), they will send you all 5 books -- completely free of charge!

as an additional advantage -- the hard copies are easier to read (though the PDFs have a search function)
Post Reply