Search code examples
linuxlinux-kernelkernelcontext-switchpage-caching

Page Cache and Mode Switch


I read that when an OS call is executed, the process only undergoes a mode switch as opposed to a context switch. From my understanding, this will elevate the privilege of the process and allow it to access the kernel code which is mapped into it's virtual address space. (This will require a security ring change using a Trap gate I believe). However, if this OS call is going to perform IO, it might need to use the page cache. How does this happen without a context switch? Or is the page cache also mapped to every process's virtual address space?

I could be wrong in some of my descriptions above. Correct me if I am. I am trying to piece this together. Also, I am more interested in the Linux kernel.


Solution

  • A simplified explanation:

    The page mapping does not change when you change to kernel mode. However, the kernel's own memory space becomes accessible (due to the ring change). When in kernel mode, one can still access the userspace memory of the process. Therefore for standard I/O calls there is nothing to do - userspace can be accessed directly. However, in many circumstances this is not a good idea as the pointers passed may point to unmapped memory, or paged out memory, or disappear half-way through the call. Hence normally copy_to_user and copy_from_user are used.

    There may be some system calls that do change the memory map. For instance, fork() creates CoW copies of the page map. exec and friends remap pages to an executable on disk. These are however the exception.

    Also, a system call may context switch. For instance sleep() is almost guaranteed to. However, that is not for the purposes of accessing the calling program's memory.