Reputation: 85
Whenever a segmentation fault occurs, do we always have a page fault?
Anyone with linux kernel code experience can you pleas point the code here too?
I have already seen: segmentation fault vs page fault
Upvotes: 1
Views: 2679
Reputation: 4321
Pagefault (aka #PF) is the common cause for segfault (addressing some data at a non-present page, trying to read or write data from a protected page without the according level of privilege, fetching code from a no-execute page, ...).
But segfault can occurs for some other rare reasons. For instance if you try to execute a privileged instruction without the requested current privilege level (CPL). Check this sample:
% cat segfault.c && gcc segfault.c -o segfault
int main(void)
{
__asm__("invd");
return 0;
}
% ./segfault
Segmentation fault
In this sample, the segfault has nothing to do with memory. The invd
instruction is a privileged one (only executable when the CPL is ring0). Thus you can't execute it from the userspace. When executing this instruction, a general-protection fault (aka #GP) exception happens. The kernel catch the exception and send the kill signal to the faulty program.
Upvotes: 8
Reputation: 5264
Segmentation fault can also occur under following circumstances:
a) A buggy program / command, which can be only fixed by applying patch.
b) It can also appear when you try to access an array beyond the end of an array under C programming.
c) Inside a chrooted jail this can occur when critical shared libs, config file or /dev/ entry missing.
d) Sometime hardware or faulty memory or driver can also create problem.
e) Maintain suggested environment for all computer equipment (overheating can also generate this problem).
Why page fault occur:
a)Trying to access a virtual memory-address
b)Instruction-operand / instruction-address
c)Read-data/write-data, or fetch-instruction
d)Maybe page is ‘not present’
e)Maybe page is ‘not readable’
f)Maybe page is ‘not writable’
g)Maybe page is ‘not visible’
Upvotes: 0