[Solved]User Mode (Ring 3)
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
[Solved]User Mode (Ring 3)
Hi everyone ,
I want to know how can i switch to user mode.
I have the task state segment (TSS) but i don't know how to switch to user mode.
I have followed the tutorial that provided in the wiki about setting up the user mode but it didn't run because i am using MSVC 2005 inline assembly not gcc or other compilers like it.
Now i have a question:
How can i switch to user mode?
----------------------------------------------
Thanks in advance
I want to know how can i switch to user mode.
I have the task state segment (TSS) but i don't know how to switch to user mode.
I have followed the tutorial that provided in the wiki about setting up the user mode but it didn't run because i am using MSVC 2005 inline assembly not gcc or other compilers like it.
Now i have a question:
How can i switch to user mode?
----------------------------------------------
Thanks in advance
Last edited by melgmry0101b on Sun Jul 24, 2011 12:49 pm, edited 1 time in total.
Re: User Mode (Ring 3)
1. Try this:
Or this:
3. Use far call/int to call/int gate.
Code: Select all
push USERDATA_SELECTOR ; (RPL=3)
push stackpointer
push flags
push USERCODE_SELECTOR ; (RPL=3)
push startaddress
iret
Code: Select all
push USERDATA_SELECTOR ; (RPL=3)
push stackpointer
push USERCODE_SELECTOR ; (RPL=3)
push startaddress
retf
If you have seen bad English in my words, tell me what's wrong, please.
- Combuster
- 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:
Re: User Mode (Ring 3)
How did reading the manuals, tutorials, and existing forum posts not answer your question?
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: User Mode (Ring 3)
Thank you very muchegos wrote:1. Try this:Or this:Code: Select all
push USERDATA_SELECTOR ; (RPL=3) push stackpointer push flags push USERCODE_SELECTOR ; (RPL=3) push startaddress iret
3. Use far call/int to call/int gate.Code: Select all
push USERDATA_SELECTOR ; (RPL=3) push stackpointer push USERCODE_SELECTOR ; (RPL=3) push startaddress retf
I have read as much as i can , but i didn't find a solution.Combuster wrote:How did reading the manuals, tutorials, and existing forum posts not answer your question?
Thank you i used it before and i think i will give it another try but in another way.Nessphoro wrote:There is a page for that!™
Re: User Mode (Ring 3)
By the way reading from floppy via BIOS interrupt is a bad idea
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: User Mode (Ring 3)
My FDC is worse than BIOS ints because it case a bad sectors in the floppy on some machines.Nessphoro wrote:By the way reading from floppy via BIOS interrupt is a bad idea
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: User Mode (Ring 3)
Hi,
I am using this code to enter user mode but it always case General Protection Fault (#GPF):
And Bochs Debugger gave me this error:
[CPU0] check_cs <0x0023> : not a valid code segment !
Can anyone help me?
----------------------------------
Thanks in advance.
I am using this code to enter user mode but it always case General Protection Fault (#GPF):
Code: Select all
_asm {
cli
mov ax, 0x23 ; user mode data selector is 0x20 (GDT entry 3). Also sets RPL to 3
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
push 0x23 ; SS, notice it uses same selector as above
push esp ; ESP
pushfd ; EFLAGS
pop eax
or eax, 0x200 ; enable IF in EFLAGS
push eax
push 0x1b ; CS, user mode code selector is 0x18. With RPL 3 this is 0x1b
lea eax, [a] ; EIP first
push eax
iretd
a:
add esp, 4 ;fix stack
}
[CPU0] check_cs <0x0023> : not a valid code segment !
Can anyone help me?
----------------------------------
Thanks in advance.
Last edited by quok on Sun Jul 24, 2011 2:00 pm, edited 1 time in total.
Reason: Stripped colors from post. Read the forum rules! NO COLORS!
Reason: Stripped colors from post. Read the forum rules! NO COLORS!
Re: User Mode (Ring 3)
Yes - that's a data segment - you need a valid code segment from the GDT
0x1B - If properly set up I believe
Just follow the tutorial man - it even avoids that ugly stack fix
0x1B - If properly set up I believe
Just follow the tutorial man - it even avoids that ugly stack fix
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: User Mode (Ring 3)
Hi everyone,
This code is the code that provided with the tutorial of user mode but it is in AT&T syntax and Microsoft Visual C++ is using Intel syntax and i didn't use AT&T before so that can anyone help me by converting it to Intel syntax?
------------------------------------------------
Thanks in advance.
Code: Select all
asm volatile(" \
cli; \
mov $0x23, %ax; \
mov %ax, %ds; \
mov %ax, %es; \
mov %ax, %fs; \
mov %ax, %gs; \
\
mov %esp, %eax; \
pushl $0x23; \
pushl %eax; \
pushf; \
mov $0x200, %eax; \
push %eax; \
pushl $0x1B; \
push $1f; \
iret; \
1: \
");
------------------------------------------------
Thanks in advance.
Re: User Mode (Ring 3)
Taken directly from my os. I use this C header to call the code:
The address parameter is the address of where you want the code to start executing after you have entered usermode. I just find it more robust than using the jump forward.
Code: Select all
extern void cpu_usermode(unsigned int address);
Code: Select all
global cpu_usermode
cpu_usermode:
cli
mov ax, 0x23
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov eax, esp
push 0x23
push eax
pushf
pop eax
or eax, 0x200
push eax
push 0x1B
mov eax, [esp + 20]
push eax
iret
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
-
- Member
- Posts: 109
- Joined: Wed Nov 10, 2010 10:49 am
Re: User Mode (Ring 3)
Thank you very much Jezze.