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.
When an IRQ fires it loads CS from the IDT, this sets the new PL, which is usually zero. Hence, once you drop to user-mode (or to rings 1 or 2), an interrupt will set the ring level to what the IDT says, allowing you to return to kernel mode to handle task switches and syscalls.
If I add one more iret after nop I have GP with "invalid TSS". I find some info [url]here|http://geezer.osdevbrasil.net/osd/gotchas/index.htm[/url]
f an exception switches the processor from Ring 3 (user privilege) to Ring 0 (kernel privilege), the Ring 0 stack pointer will automatically be loaded from the TSS. However, the reverse is not true: before using IRET to return from Ring 0 to Ring 3, you must save the Ring 0 stack pointer in the TSS:
If I understand right I must save ring 0 stack and after iret i return to ring0
But what about Linux kernel do? If I understand rigth it use software interupt. But I'am not understand how it switch context and how it return after switch.
I don't really know how linux does it, only how I do it.
In my kernel, when a user mode program wants to access a kernel service (VFS, Process control) it calls a software interrupt. My IDT has CS for that interrupt set to a ring 0 descriptor (0x08). The TSS contains the kernel's SS (0x10) and the starting kernel ESP (for single threaded apps, this stays at 0xF0008000, but is usually changed on task switch to allow kernel threads)
This setup allows the processor to return to ring 0 to do privileged operations.
It returns by calling IRET in the interrupt handler (with the stack pointer being the same as when the handler first received control), hence returning to user mode.