This is perhaps a very silly question, but it's been bugging me for a while. My doubt is that since the only threads that are scheduled onto the CPU are kernel threads(typically) and since every user thread is mapped to a kernel thread, does that mean that all applications essentially run in kernel mode?
1 Answers
There's multiple scenarios where some are less common. One possible example (of a less common scenario) is having a single kernel stack per CPU, with "save user-space thread state" as part of privilege level change from user-space to kernel and "load possibly different user-space thread's state" as part of privilege level change from kernel back to user-space (where it's hard to say if kernel's code use 1 thread/task per CPU or if kernel's code doesn't use the concept of threads/tasks at all and only exists between the 2 halves of a potential user-space thread switch).
For the common scenario, every thread/task has a kernel part (and its own kernel stack, etc) and a user-space part (and its own user-space stack, etc). In this case it can be described as task switches only ever occuring when the thread is using its kernel part; but after the task switch the task's kernel part can switch to the same task's user-space part and back again whenever it wants (e.g. for kernel system calls and IRQs).
Note that this isn't the only way to describe the common scenario; but it is my preferred way (because it avoids "beginner pitfalls" - specifically, the false assumption that IRQs are a necessary part of task switching).
I suspect that this way of describing the common scenario (i.e. that task switches only happen between tasks running kernel code) has caused your doubts.

- 35,656
- 2
- 39
- 66
-
If i understand correctly, you're saying that every thread is comprised of kernel segment and user segment and so kernel threads don't exist separately? – Jamāl Dec 03 '20 at 16:11
-
1@Jamāl: Yes (although I'd avoid the word "segment" - has too many other meanings). You can flip from kernel to use-space all you like without changing tasks (then when the task is in kernel you can do a task switch to another task in kernel, and then flip the next task from kernel to user-space as much as you like) – Brendan Dec 03 '20 at 17:14
-
Oh got it! Thanks. – Jamāl Dec 03 '20 at 17:16