0
votes

Im studying for the first time "Operating System". In my book i found this sentence about "User Mode" and "Kernel Mode":

"Switch from user to kernel mode" instruction is executed only in kernel mode

I think that is a incorrect sentence as in practice there is no "switch of kernel". In fact, when a user process need to do a privileged instruction it simply ask the kernel to do something for itself. Is it correct ?

1
What is your book?Basile Starynkevitch
You need to supply more context..user3344003

1 Answers

4
votes

In fact, when a user process need to do a privileged instruction it simply ask the kernel to do something for itself.

But how does that happen? Details are processor (i.e. instruction set architecture) and OS specific (explained in ABI specifications relevant to your system, e.g. here), but that usually involves some machine code instruction like SYSENTER or SYSCALL (or SVC on mainframes) capable of atomically changing the CPU mode (that is switching it in a controlled manner to kernel mode). The actual parameters of the system call (including even the syscall number) are often passed in registers (but details are ABI specific).

So I feel the concept of switching from user-mode to kernel-mode is relevant, and meaningful (so "correct").

BTW, user-mode code is forbidden (by the hardware) to execute privileged machine instructions, such as those interacting with IO hardware devices (read about protection rings). If you try, you get some hardware exception (a bit similar to interrupts). Hence your code (even if it is malicious) has to make system calls, which the kernel controls (it has lots of code related to permission checking), for e.g. all IO.

Read also Operating Systems: Three Easy Pieces - freely downloadable. See also http://osdev.org/. Read system call wikipage & syscalls(2), and the Assembler HowTo.

In real life, things are much more complex. Read about System Management Mode and about the (scary) Intel Management Engine.