[Solved]User Mode (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.
Post Reply
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

[Solved]User Mode (Ring 3)

Post by melgmry0101b »

Hi everyone :D ,
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.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: User Mode (Ring 3)

Post by egos »

1. Try this:

Code: Select all

  push USERDATA_SELECTOR ; (RPL=3)
  push stackpointer
  push flags
  push USERCODE_SELECTOR ; (RPL=3)
  push startaddress
  iret
Or this:

Code: Select all

  push USERDATA_SELECTOR ; (RPL=3)
  push stackpointer
  push USERCODE_SELECTOR ; (RPL=3)
  push startaddress
  retf
3. Use far call/int to call/int gate.
If you have seen bad English in my words, tell me what's wrong, please.
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:

Re: User Mode (Ring 3)

Post by Combuster »

How did reading the manuals, tutorials, and existing forum posts not answer your question?
"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
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: User Mode (Ring 3)

Post by Nessphoro »

melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: User Mode (Ring 3)

Post by melgmry0101b »

egos wrote:1. Try this:

Code: Select all

  push USERDATA_SELECTOR ; (RPL=3)
  push stackpointer
  push flags
  push USERCODE_SELECTOR ; (RPL=3)
  push startaddress
  iret
Or this:

Code: Select all

  push USERDATA_SELECTOR ; (RPL=3)
  push stackpointer
  push USERCODE_SELECTOR ; (RPL=3)
  push startaddress
  retf
3. Use far call/int to call/int gate.
Thank you very much
Combuster wrote:How did reading the manuals, tutorials, and existing forum posts not answer your question?
I have read as much as i can , but i didn't find a solution.
Thank you i used it before and i think i will give it another try but in another way.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: User Mode (Ring 3)

Post by Nessphoro »

By the way reading from floppy via BIOS interrupt is a bad idea
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: User Mode (Ring 3)

Post by melgmry0101b »

Nessphoro wrote:By the way reading from floppy via BIOS interrupt is a bad idea
My FDC is worse than BIOS ints because it case a bad sectors in the floppy on some machines.
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: User Mode (Ring 3)

Post by melgmry0101b »

Hi, :D
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
	}
And Bochs Debugger gave me this error:
[CPU0] check_cs <0x0023> : not a valid code segment !
Can anyone help me?
----------------------------------
Thanks in advance. :D
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!
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: User Mode (Ring 3)

Post by Nessphoro »

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
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: User Mode (Ring 3)

Post by melgmry0101b »

Hi everyone,

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: \ 
				 ");
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.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: User Mode (Ring 3)

Post by Jezze »

Taken directly from my os. I use this C header to call the code:

Code: Select all

extern void cpu_usermode(unsigned int address);
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

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/
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: User Mode (Ring 3)

Post by melgmry0101b »

Thank you very much Jezze. :D
Post Reply