UserSpace? KernelSpace? HyperSpace? Which one?!.....

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

UserSpace? KernelSpace? HyperSpace? Which one?!.....

Post by piranha »

A more clever title for this post would be Alice in Userland....but oh well....

I was browsing the Linux Kernel 0.01 sources the other day (ok ok, it was about 5 min ago) when I came across something in the init code: move_to_user_mode();
It's a #defined thingy, here is the code:

Code: Select all

#define move_to_user_mode() \
__asm__ ("movl %%esp,%%eax\n\t" \
	"pushl $0x17\n\t" \
	"pushl %%eax\n\t" \
	"pushfl\n\t" \
	"pushl $0x0f\n\t" \
	"pushl $1f\n\t" \
	"iret\n" \
	"1:\tmovl $0x17,%%eax\n\t" \
	"movw %%ax,%%ds\n\t" \
	"movw %%ax,%%es\n\t" \
	"movw %%ax,%%fs\n\t" \
	"movw %%ax,%%gs" \
	:::"ax")
Uh....Um...what?! asm is not one of my strong points (or not even one of my points)...

Apparently.......it moves in to userspace. What is that? Advantages? Is there any way to exit it?
I already looked on Wikipedia, but all it says is the differences between the two modes(usermode and kernel mode).

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

It jumps to ring 3 by first pushing SS, ESP, EFLAGS, CS and EIP (for ring 3) then iret'ing to it. Then it sets up ring3 segment selectors for ring3. Its basically a 'jump to ring3 in-place' where esp isn't changed (its stored at the beginning to eax then pushed at the right point for iret) and eip just continues on from the end of the function (the '1:' label).
pirahna wrote:Is there any way to exit it?
Being able to leave ring 3 at will would kind of be against the point of hardware protection, wouldn't it? That's the purpose of call gates, interrupts and sysenter.

Regards,
John.
Post Reply