Ring 3 query

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
worldsapart
Member
Member
Posts: 36
Joined: Sat Jan 03, 2009 4:12 am

Ring 3 query

Post by worldsapart »

Hi,

I am currently in ring 0. the code I use to jump to ring 3 is shown below...

Code: Select all

void switch_to_user_mode()
{
   print("\nJumping to ring 3");

   // Set up a stack structure for switching to user mode.
   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; \
     pop %eax;\
     orl $0x200, %eax;\
     push %eax;\
     pushl $0x1B;\
     push $1f;\
     iret; \
   1: \
     ");
}


void user_task()
{
	switch_to_user_mode();
	print("hello world");
	asm volatile("sysenter");
}


The problem here is that everything works fine without any triple faults! Actually, the "print()" function is a function in the kernel to print data on the screen. So typically if I jump from Ring 0 to Ring 3 this function should not be available unless accessed using some kind of system call, rite? But "hello world" is printed out on the screen in this case? What could be wrong?

Just to add, the system call i implemented works. But I guess that does not tell me anything, since "sysenter" can be called from ring 0 and ring 3. The switch_to_user_mode() function is the same from JamesM's kernel development tutorials. I could use some insight from you guys here. I'm pretty sure, I'm doing something wrong. Please also tell me if I need to give more details about my implementation. Thanks.
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Ring 3 query

Post by gzaloprgm »

The problem here is that everything works fine without any triple faults! Actually, the "print()" function is a function in the kernel to print data on the screen. So typically if I jump from Ring 0 to Ring 3 this function should not be available unless accessed using some kind of system call, rite? But "hello world" is printed out on the screen in this case? What could be wrong?
Why shouldn't it be printed? if you mapped the kernel and video memory to be user accessible and writable, unless you are using a "privileged" instruction (cli, sti, hlt, lgdt, lidt, etc...) in your print function it should work perfectly.

To avoid that, you could map the kernel as read only - supervisor and avoid mapping the video memory in the process PD.

Cheers,
Gzaloprgm
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
Post Reply