Reputation: 45
I know the stack size is controllable through the limits facility, but how does the kernel enforce some of these limits, such as RLIMIT_STACK? Since linux is not involved in stack operations (it's just a mov or push instruction), how does the kernel issue SIGSEGV when you exceed the limit? I understand that for virtual addressing, the CPU provides a facility the linux kernel can use. Is this similar to how the stack size limit is enforced? Or does linux perform a routine check on stack sizes and issue segfaults 'after the crime has occurred'? Or is there some third option?
Upvotes: 3
Views: 1475
Reputation: 2335
The kernel can control this due to the virtual memory. The virtual memory (also known as memory mapping), is basically a list of virtual memory areas (base + size) and a target physically memory area that the kernel can manipulate that is unique to each program. When a program tries to access an address that is not on this list, an exception happens. This exception will cause a context switch into kernel mode. The kernel can look up the fault. If the memory is to become valid, it will be put into place before the program can continue (swap and mmap not read from disk yet for instance) or a SEGFAULT can be generated.
In order to decide the stack size limit, the kernel simply manipulates the virtual memory map.
Upvotes: 3