11
$\begingroup$

I am reading Operating Systems: Three Easy Pieces, but I can't fully understand the following paragraph. It's from the 6th chapter of the book. I think it's self-contained, so I quote here.

Note that there are two types of register saves/restores that happen during this protocol (context switch). The first is when the timer interrupt occurs; in this case, the user registers of the running process are implicitly saved by the hardware, using the kernel stack of that process. The second is when the OS decides to switch from A to B; in this case, the kernel registers are explicitly saved by the software (i.e., the OS), but this time into memory in the process structure of the process. The latter action moves the system from running as if it just trapped into the kernel from A to as if it just trapped into the kernel from B.

I understand that OS need to save some registers to do context switch, but what does it mean by saying user registers and kernel registers? What are the differences between them?

Thanks!

$\endgroup$
1
  • 1
    $\begingroup$ I hope the answer given has helped you, but I would also remark that the behaviour described sounds like a design decision for a particular OS, which would not necessarily be implemented in another OS, even assuming the hardware has distinct user and kernel registers. $\endgroup$
    – PJTraill
    Aug 25, 2018 at 18:04

4 Answers 4

7
$\begingroup$

It's simple - when each application program runs, it has access to its own set of registers. When you switch to other application, these register contents is saved to memory, and registers, saved from other application, loaded and this application continues its execution.

Similarly, OS has its own register contents. Depending on CPU, it may even has its own, different register set. Moreover, some CPUs has two physical register sets - for apps and for OS, thus allowing fast switching from app to OS and back again (f.e. when app requests some service from OS).

Now let's look at your citation. I'm not an OS specialist, but this text means that for each application, we have specific contents of OS registers. F.e. these registers may hold app-specific data which isn't available to app itself (such as security context of the app). So, when you switch from app to OS (usually by requesting some OS service), OS registers are immediately ready to use, making the call faster. OTOH, when you switch to other app, the entire set of kernel registers need to be saved and kernel registers for other app should be loaded instead.

Since task switching is much less frequent operation, this makes the entire execution faster.

$\endgroup$
0
$\begingroup$

I would add that kernel registers are the ones extraneous to a user process, but help the OS and CPU enforce privilege levels. The registers that contain the upper and lower bounds of memory accessible by a user process in x86 are examples.

$\endgroup$
0
$\begingroup$

There have been processors doing it differently, but it is most most likely that your processor has one set of registers per virtual core. Having a second set of registers that are just needed when switching between user mode and kernel mode would be extremely wasteful. Registers are expensive.

What happens: Your user mode program is running and using all the registers. Then it is interrupted. The interrupt stashes a copy of all the registers away in a location for your process, then it grabs the register contents for the kernel mode, puts it into the same registers, and switches to kernel mode - so the kernel continues exactly where it left over. Eventually the kernel decides to switch back to your process. At that point all the registers are stored in a location that only the kernel can access, your user mode register values are restored from where they were stored, and the processor switches back to your user mode program, with all the registers exactly the same as they were before.

The same registers inside the processor are used both for user mode and kernel mode.

Sometimes you have interrupts that can be handle very easily. In that situation, maybe only 16 of your 32 integer registers, and none of your floating point or vector registers are stored and restored. If the kernel can run the interrupt handler code with these 16 registers only, then this can save substantial time.

$\endgroup$
0
$\begingroup$

Kernel registers can only be accessed in kernel mode, but are particular to a process. In x86 they include, for example, a pair of registers which demlimit the address space of the process.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.