Reputation: 21560
I asked a similar question a little while ago but I may have not worded it correctly. So if process A is running and it causes a page fault at location 4000 in memory, and that causes a page fault. Execution will be transferred to the kernel. Then which stack will the page fault handler run on? Is this in the virtual address space of the kernel? Or is stack space reserved for all interrupts of this sort?
Upvotes: 1
Views: 991
Reputation: 21677
At the risk of over simpliciation and ignoring the specifics of any particular operating system—
Then which stack will the page fault handler run on?
Most operating systems allocate a (relatively) small kernel mode stack for each process.
When a process accesses a memory location that is valid but not mapped to a physical page frame, the processor raises a page FAULT (exception). This causes the CPU to switch to kernel mode. This causes the processor to switch to the processes's kernel mode stack and invoke the page fault handler set up by the operating system.
There has to be a separate kernel mode stack for each process (or even thread) because the multiple processors can be in kernel mode at the same time (even in a single processor system).
Interrupt and exception handlers must use a kernel stack (protected from user mode access) as a security measure. If the stack were accessible in user mode, it would be possible for someone to muck with the stack, then invoke a kernel mode handler.
Is this in the virtual address space of the kernel?
Exceptions (Faults and Traps) and Interrupts are handled by the process currently executing but done in kernel mode. This is done in the virtual address space of the PROCESS.
There will be some range of kernel mode address that all processes share in common. Because any process has to be able to handle interrupts, all addresses referenced by interrupt handlers have to be the same. Some systems (hardware) have a dedicated range of kernel addresses. Other system configure page tables to have a shared range of system addresses.
Upvotes: 0
Reputation: 180295
Linux doesn't care whether you're running a C or C++ program, really.
When the CPU detects a faulty address, it raises an interrupt. There's no reasonable way to use the user stack, as it may be in a totally corrupt state. The kernel has its own private stack for this kind of serious faults.
This isn't universally the case. If you normally call the kernel to do things for you, the kernel may assume that you have a reasonable stack available.
Upvotes: 2