5

In practice, many operating systems are designed to have one kernel stack for each thread, or at least one for each CPU. But for an operating system that the kernel is locked every time a process traps, it seems unnecessary to have separated kernel stacks for each CPU. Since the kernel (with its own stack) only allows single CPU access, the CPUs should never be in kernel mode simultaneously. A CPU is always blocked until the previous CPU leaves and cleans up the kernel stack, even in the nested trap case. So in which case will multiple kernel stack be necessary in such an OS? Thanks.

Phil Guo
  • 67
  • 2

2 Answers2

8

You're right; in such a case multiple kernel stacks wouldn't be useful as long as you have a solid multi-core locking feature.

We usually have multiple kernel stacks (i.e. at least one kernel stack for each thread) for the following purposes:

  • When you want your kernel to be threaded and interruptible, like in a micro-kernel architecture.
  • When you want to migrate threads from one core to another, when load-balancing for instance. It would be cumbersome to copy the kernel stack of CPU1 to ther kernel stack of CPU2. Needless to say it would also be a performance killer since you are freezing two cores for that purpose.
  • Depending on the underlying architecture, having multiple kernel stacks makes MMU things easier (AS management, ...).
Benny
  • 4,095
  • 1
  • 26
  • 27
2

In practice, many operating systems do not restrict the number of threads running kernel-level code. In fact, placing this restriction would seriously limit the scalability of the kernel. Without this restriction, multiple kernel stacks are required.

Furthermore, in practice, operating systems may permit the preemption and migration of threads that are currently executing in kernel-mode, which again ties the kernel stack to the thread and not the processor.

If you are designing an operating system that does not have these features, and instead restricts kernel-level execution to a single thread, then a single stack would be all that is required.

Brian
  • 2,693
  • 5
  • 26
  • 27