CPU Switches from User mode to Kernel Mode : What exactly does it do? How does it makes this transition?
EDIT:
Even if it is architecture dependent please provide me with an answer. The architecture is up to you. Tell me for开发者_开发知识库 the architecture you know about.
I want to get an idea about what all things will be involved in it.
Note: this is mostly relevant to x86 architecture. Here's a somewhat simplified explanation.
The transition is usually caused by one of the following:
- Fault (e.g. a page fault or some other exception caused by executing an instruction)
- Interrupt (e.g. a keyboard interrupt or I/O finishing)
- Trap (e.g. a system call)
What normally happens is that system checks the Interrupt Descriptor Table (IDT). Each exception (interrupt, fault, etc.) has a number associated with it which is used to index into this table.
From this table the CPU can determine the interrupt handler to run.
As part of the transition the following changes (generally) take effect:
- Switch to Kernel stack
- EFLAGS are saved
- Code segment selector and EIP are saved.
- stack segment selector and stack pointer are saved
- Start executing the interrupt handler
- The general purpose registers are saved (handler's job)
- Segment selectors are changed to Kernel ones (handler's job)
You're now in kernel mode.
Hope that helps :)
That's system dependent, but the usual mechanism is some userland operation causes a software interrupt. That interrupt makes the processor switch modes and jump into kernel code, which then checks what the program was trying to do (system call?) and then does the requested action and jumps back to the user mode code. Other mechanisms besides a software interrupt might cause the transition as well; for instance in a preemptive multitasking system, a timer interrupt might trigger the scheduler to run.
My understanding is that any program whose segment registers have the two LSBs zero will be running in Kernel mode while any program whose segment registers have the two LSBs = 1 will be running in User Mode. In fact, the two LSBs of the segment rgeisters define the Priviledge Level (0 highest to 3 lowest)
So, to make a prgram run in Kernel mode you have to set up the segment registers to be 0010 hex (I believe). I'm not sure how you can place a program in that memory space without overwriting something else - in other words, how does the linker ensure that? Also, if you want to call the Kernel mode code from User mode code, you have to figure out how to pass parameters - they are not using the same memory soace, so can't pass data by memory reference. I guess you have to pass it in registers.
If anynody can fill in the gaps in the above, I would be very grateful.
In Windows, when you make a systemcall, the library routines call a kernel entrypoint residing in the operating system address space. It in turn takes the CPU into supervisor mode by executing an instruction specific for this purpose, such as sysenter. What it does is essentially setting a bit in the flags register. This enables the OS to use privileged instructions.
精彩评论