1

I guess no because user processes should be able save and restore its own registers (e.g user level thread) and it doesn't harm any other processes and the OS. However, when doing context switching, why do we need to change to kernel mode first then save and restore the registers? I am confusing. Could anyone help? Thank you!

Kevin Liang
  • 427
  • 6
  • 13

1 Answers1

1

It depends on the processor used and on the list of registers that need to be saved and restored.

No → typical save/restore scenario known as setjmp/longjmp does not need anything special. You can find more about it's internals by studying it's implementation in the source codes of the runtime library delivered with your favorite C(++) compiler.

Yes → restoring control registers like CR0 on the x86 architecture is definitely privileged operation as tampering the register may break all security guarantees and let a malicious code do a big harm to the system.

Chapter "5.9 Privileged Instructions" in Intel® 64 and IA-32 Architectures Software Developer’s Manual lists following registers (and their save/restore instructions) as privileged:

  • LGDT - Load GDT register.
  • LLDT - Load LDT register.
  • LTR - Load task register.
  • LIDT - Load IDT register.
  • MOV (control registers) - Load and store control registers.
  • LMSW - Load machine status word.
  • CLTS - Clear task-switched flag in register CR0.
  • MOV (debug registers) - Load and store debug registers.
  • RDMSR - Read Model-Specific Registers.
  • WRMSR - Write Model-Specific Registers.
  • ...

See also:

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54