Page 1 of 1

Privilege level 3

Posted: Thu Feb 26, 2004 8:29 am
by DennisCGc
Hello.
I'm writing an OS (a small one), but I have a problem.
Many say it's easy to do that, but I think not.
My problem is I can't run a task in ring 3.
For example, when I set the SS to 43 (a privilege 3 descriptor) the OS gives a #GP fault :'(
The TSS is loaded before this, and with the good values, so I think that ain't the problem.
What could be the problem ?
My descriptors(maybe that will help):

GDT_ME dd 0,0

dd 0000ffffh ;ring 0 code segment 8
dd 00cf9a00h

dd 0000ffffh ;ring 0 data segment 16
dd 00cf9200h

   dw 103
   dw tss_begin
   dw 1000100100000000b
   dw 0


;define a ring 3 code segment
   dd 0x0000ffff
   dd 0x00cffa00
;data
   dd 0x0000ffff
   dd 0x00cff200

Re:Privilege level 3

Posted: Thu Feb 26, 2004 10:03 am
by Pype.Clicker
Only a DPL-0 segment will be a valid stack segment for DPL0 code. Which mean you cannot simply

Code: Select all

    mov ss,UDATA_SELECTOR
    jmp UCODE_SELECTOR:entrypoint
to enter a user-mode task... Even just 'jmp UCODE:point' is invalid because you can only jmp to code segment that have the same DPL as your CPL (DPL of the current code segment).

You should push things on the stack so that an IRET instruction would believe that the current execution comes from a call to INT nn that made a stack switch (see Holy Intel Manuals for the exact structure)

Code: Select all

    push USTACK_SELECTOR
    push user_stack_pointer
    push SOME_FLAGS
    push UCODE_SELECTOR
    push entry_point
    iret
or something alike ...

Re:Privilege level 3

Posted: Fri Feb 27, 2004 4:32 am
by DennisCGc
Pype.Clicker wrote: Only a DPL-0 segment will be a valid stack segment for DPL0 code. Which mean you cannot simply

Code: Select all

    mov ss,UDATA_SELECTOR
    jmp UCODE_SELECTOR:entrypoint
to enter a user-mode task... Even just 'jmp UCODE:point' is invalid because you can only jmp to code segment that have the same DPL as your CPL (DPL of the current code segment).

You should push things on the stack so that an IRET instruction would believe that the current execution comes from a call to INT nn that made a stack switch (see Holy Intel Manuals for the exact structure)

Code: Select all

    push USTACK_SELECTOR
    push user_stack_pointer
    push SOME_FLAGS
    push UCODE_SELECTOR
    push entry_point
    iret
or something alike ...
Thanks! ;D
I shall try it ;)