Documente Academic
Documente Profesional
Documente Cultură
Anand Sivasubramaniam
Dept. of Computer Science & Eng.
The Pennsylvania State University
Why Linux?
• It’s free!
• Open Source (modifiability, extensibility, …)
• Works on several platforms
• Robustness (after several revisions, and several people
working on it)
• Widespread Usage
• Compatibility with several other platforms.
Coverage from …
Init() {
kernel_thread(bdflush,NULL,0); // buffer cache sync daemon
kernel_thread(kswapd,NULL,0); // swap daemon
setup(); // init file sys, and mount root
… open console, file descriptors 0, 1 and 2 …
execve(“…../init”,argv_init,envp_init); // getty runs on each tty
}
Adding a System Call
• show_mult(x, y, *z)
• Each sys call has a name and a number
• Go to /asm/unistd.h:
#define __NR_sched_r_get_interval 161
#define __NR_nanosleep 162
#define __NR_mremap 163
#define __NR_show_mult 164
• Go to arch/i386/kernel/entry.S
.long SYMBOL_NAME(sys_nanosleep)
.long SYMBOL_NAME(sys_mremap)
.long SYMBOL_NAME(sys_show+mult)
.space (NR_syscalls-164)*4 // padding
• Now to add the code for the syscall, say in /sys.c (which has other syscalls)
#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>
_syscall3 (int, show_mult, int, x, int, y, int *, resul);
main() {
int ret=0;
show_mult(2, 5, &ret);
}
What happens on a syscall?
• On the software interrupt (0x80), the control is transferred to system_call() in
arch/i386/kernel/entry.S
• Here is what goes on inside this routine …
SAVE_ALL; // saves registers
…
*sys_call_table[sys_call_num](sys_call_args);
…
if (intr_count) goto exit_now // nested interrupts
if (bh_mask & bh_active) {
++intr_count;
Sti();
Do_bottom_half();
--intr_count;
}
sti();
if (need_resched) {
schedule(); // return much later!
goto ret_from_sys_call;
}
…
if (current->signal & ~current->blocked) do_signal();
…
exit_now: RESTORE ALL; // return using iret
Basics of Interrupt Handling
• In arch/i386/kernel/irq.c and include/asm/irq.h
• Three types of interrupts: Fast, Slow, System Calls (software)
• Slow Interrupts (typical), turn off interrupts only for a little while. E.g. timer