I understand this:

User code executes in a process/thread context in a process address space, in user mode, and can address only user code/data defined for the process, with no access to privileged instructions.

How does operating system know the process/thread created by user is in the user space, and in user mode, instead of kernel space and kernel mode? Is it controlled by register/cpu/hardware settings? How exactly does it work?


1 Answer 1


The processor (CPU) has a mode bit (bits). From privileged mode ring-0, the computer can run all instructions, and access all resources. The kernel runs in this mode. There are instructions to change mode, e.g. change to usermode ring-3 (ring-1 and ring-2 are not used).

The instruction to move to privileged mode, are more complex. These are interrupts and traps: If hardware interrupts, or a user mode program executes a software interrupt, then the processor looks up a pre-registered address, changes mode puts return address on stack and jumps to this address. Only privileged mode can can register these addresses (it is analogous to set-uid).

As for memory: in both modes, user and kernel memory is mapped. However in user mode kernel memory is disabled. When a jump is made to kernel space it can be quickly re-enabled.

This is not specific to a particular CPU, except the mention of rings (used on x86). If you wish to learn how the CPU does things, then the ARM is one of the simplest, contemporary processors. x86 is more of the most complex (but not in a good way).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .