130 likes | 340 Views
제 40 강 : System Call. Ch 5 System Call. Kernel a.out. User a.out. my code. library. inode table. (system) file table. user. main() { add( ); sub( ); printf( );. /. u-ofile. FILE ( local buffer file descriptor }. fd. /. a. a. 0 1 2 3 4.
E N D
제40강 : System Call Ch 5 System Call
Kernel a.out User a.out my code library inode table (system) file table user main() { add( ); sub( ); printf( ); / u-ofile FILE ( local buffer file descriptor } fd / a a 0 1 2 3 4 offset b printf(3) data block data block sys call system call write(2) trap( ) sys_write() You have 2 write( ) functions . One in my a.out as a library function. another in kernel a.out. library write( ) is the caller kernel sys_write() is callee this issues trap i.e. chmodk (with para) this implements system call
Invoking a system call In Linux, system call interfaceis provided by the C library. Kernel a.out User a.out my code library trap() { ….. } printf(3) { write(2) } write() { load arg chmodk } main() { add( ); sub( ); printf( ); sys_write() { ….. } system call wrapper routine (system call interface)
Inside Wrapper Routine • Only purpose is to issue a system call • C library function example • libc.a : write() { ….. : push arguments movl 5, %eax : system call number int $0x80 : cause trap }
CompilingSystem Call Wrapper Routine • Before compile After compile • interrupt causes system to switch to kernel mode • kernel executes system call handler system_call() • which is coded in assembly (entry.S) <wrapper routine> arguments into registers (up to five) system call # into eax register software interruptinstruction ($0x80) printf() { write(2) }
System Call Handling in Kernel asmlinkage long sys_write(void) { return current->tgid; } • Naming convention: • “sys_ ” followed by system call name write • asmlinkage modifier on declaration • required for all system calls
Kernel system call function • Should verify the parameters • Every argument must be checked • If pointer is passed, check whether • points to the process’s user address space? • proper access rights? • Accessing user space • copy_to_user() write to user space -- subyte() • copy_from_user() read from user space -- fubyte() • capable() check permission copy_to_user() user a.out kernel a.out copy_trom_user()
System Call Number • Unique number that reference system call • include/asm-i386/unistd.h • sys_call_table (in entry.S) • NR_syscalls • Max number of implementable system calls • Architecture dependent • Itcannot be changed (since all a.out’s use these numbers) sys_call_table system call number *sys_write() NR_syscalls
Write a New System Call? • Pros • Simple to implement • Good Performance --- binding is fast on Linux • Cons • Need new syscall number • new program is platform dependent. • This program may not run on other platform • Cannot change existing system call (only can add) • Do you really need a new system call?
Alternative to New System Call • Implement a new file fdNEW • Let this file correspond to a new system call • read(fdNEW), write(fdNEW), ioctl(fdNEW) to it. • which pass data & control between user & kernel • Nobody adds new system call in Linux • Linux keeps very clean system call layer
System Call Implementation sys_call_table • Add an entry at the end of system call table • Define syscall # in include/asm/unistd.h • Add syscall function into kernel image • User-Space wrapper • glibcdoes not support new system call wrapper! • Linux provides a set of macro • _syscalln() (where n is # of parameters 0 to 6) • Example of using macro: • For new system call open() • long open(const char *filename, int flags, int mode) • Without library support, use this macro • _syscall3(long, open, const char*, filename, int, flags, int, mode) system call number *sys_write()