Kernel Address Mapping Making a System Call

register values are typically stored on the stack, only to be restored when the func- tion returns. The trick in using this for context switching is to swap stacks in the middle Thus the current process calls the context switching function, causing all the hardware context to be stored on the kernel stack. This function loads the memory mapping registers with values of the new process, including those used to access the stack. At this instant, the CPU switches from executing the old process to the new one — which is also in the middle of the same function... The function completes already in the context of the new process, and returns, restoring the state of the new process as it was when it called the context switching function in order to stop running.

9.7 Making a System Call

To understand the details of implementing a system call, it is first necessary to un- derstand how the kernel accesses different parts of the system’s memory.

9.7.1 Kernel Address Mapping

Recall that system calls and, for that matter, interrupt handlers are individual entry points to the kernel. But in what context do these routines run? The kernel can access everything To answer this, we must first consider what data structures are required. These can be divided into three groups: • Global kernel data structures, such as the process table, tables needed for mem- ory allocation, tables describing the file system, and so forth. • Process-specific data structures, in case the event being handled is specific to the current process. In Unix, the main one is the u-area. Access to the process’s address space may also be needed. • A stack for use when kernel routines call each other. Note that the kernel as a whole is re-entrant: a process may issue a system call and block in the middle of it e.g. waiting for the completion of a disk operations, and then another process may also issue a system call, even the same one. Therefore a separate stack is needed for kernel activity on behalf of each process. 181 only by kernel accessible accessible by user program global per process u-area kernel stack kernel text kernel data user text user data user heap user stack But different mappings are used To enable the above distinction, separate address mapping data can be used. In par- ticular, the kernel address space is mapped using distinct page tables. In total, the following tables are needed. • Tables for the kernel text and global data. These are never changed, as there is a single operating system kernel. They are marked as being usable only in kernel mode, to prevent user code from accessing this data. • Tables for per-process kernel data. While these are also flagged as being acces- sible only in kernel mode, they are switched on every context switch. Thus the currently installed tables at each instant reflect the data of the current process. Data for other processes can be found by following pointers in kernel data struc- tures, but it cannot be accessed directly using the data structures that identify the “current process”. • Tables for the user address space. These are the segment and page tables dis- cussed in Chapter 5. Access to data in user space requires special handling. Consider a system call like read, that specifies a user-space buffer in which data should be deposited. This is a pointer that contains a virtual address. The problem is that the same memory may be mapped to a different virtual address when accessed from the kernel. Thus accessing user memory cannot be done directly, but must first undergo an address adjustment. Interrupt handlers are special A special case is the handling of interrupts, because interrupts happen asynchronously, and may not be related to any process e.g. the clock interrupt. Some systems there- fore have a special system context that is used for the handling of intrrupts. In Unix, 182 interrupt handling is done within the context of the current process, despite that fact that this process is probably unrelated to the interrupt. Exercise 140 Does the Unix scheme impose any restrictions on the interrupt handlers?

9.7.2 To Kernel Mode and Back