System call and context switch

You need to understand that a thread/process context has multiple parts, one, directly associated with execution and is held in the CPU and certain system tables in memory that the CPU uses (e.g. page tables), and the other, which is needed for the OS, for bookkeeping (think of the various IDs, handles, special OS-specific permissions, … Read more

What is better “int 0x80” or “syscall” in 32-bit code on Linux?

syscall is the default way of entering kernel mode on x86-64. This instruction is not available in 32 bit modes of operation on Intel processors. sysenter is an instruction most frequently used to invoke system calls in 32 bit modes of operation. It is similar to syscall, a bit more difficult to use though, but … Read more

How to write a signal handler to catch SIGSEGV?

When your signal handler returns (assuming it doesn’t call exit or longjmp or something that prevents it from actually returning), the code will continue at the point the signal occurred, reexecuting the same instruction. Since at this point, the memory protection has not been changed, it will just throw the signal again, and you’ll be … Read more

Magic numbers of the Linux reboot() system call

Just a guess, but those numbers look more interesting in hex: 672274793 = 0x28121969 85072278 = 0x05121996 369367448 = 0x16041998 537993216 = 0x20112000 Developers’ or developers’ children’s birthdays? Regarding finding the syscall implementation, I did a git grep -n LINUX_REBOOT_MAGIC2 and found the definition in kernel/sys.c. The symbol sys_reboot is generated by the SYSCALL_DEFINE4(reboot, … … Read more

dup2 / dup – why would I need to duplicate a file descriptor?

The dup system call duplicates an existing file descriptor, returning a new one that refers to the same underlying I/O object. Dup allows shells to implement commands like this: ls existing-file non-existing-file > tmp1 2>&1 The 2>&1 tells the shell to give the command a file descriptor 2 that is a duplicate of descriptor 1. … Read more