Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances does control pass from userspace to the Linux kernel space?

I'm trying to understand which events can cause a transition from userspace to the linux kernel. If it's relevant, the scope of this question can be limited to the x86/x86_64 architecture.

Here are some sources of transitions that I'm aware of:

  • System calls (which includes accessing devices) causes a context switch from userspace to kernel space.
  • Interrupts will cause a context switch. As far as I know, this also includes scheduler preemptions, since a scheduler usually relies on a timer interrupt to do its work.
  • Signals. It seems like at least some signals are implemented using interrupts but I don't know if some are implemented differently so I'm listing them separately.

I'm asking two things here:

  1. Am I missing any userspace->kernel path?
  2. What are the various code paths that are involved in these context switches?
like image 641
nitzanms Avatar asked Jul 23 '15 14:07

nitzanms


People also ask

Is the only way to transit from userspace to kernel space?

System Call Interfaces (SCI) are the only way to transit from User space to kernel space.

What is difference between userspace and kernel space?

Kernel space is strictly reserved for running a privileged operating system kernel, kernel extensions, and most device drivers. In contrast, user space is the memory area where application software and some drivers execute.

Which function allows the transfer of data from kernel space to user space?

You can use the copy_from_user() and copy_to_user() functions to move data between kernel space and user space.


1 Answers

One you are missing: Exceptions

(which can be further broken down in faults, traps and aborts)

For example a page fault, breakpoint, division by zero or floating-point exception. Technically, one can view exceptions as interrupts but not really the way you have defined an interrupt in your question.

You can find a list of x86 exceptions at this osdev webpage.

With regard to your second question:

What are the various code paths that are involved in these context switches?

That really depends on the architecture and OS, you will need to be more specific. For x86, when an interrupt occurs you go to the IDT entry and for SYSENTER you get to to address specified in the MSR. What happens after that is completely up to the OS.

like image 134
missimer Avatar answered Oct 13 '22 20:10

missimer