1 12

전공핵심실습1:운영체제론 Lecture 5. System Call

Sungkyunkwan University Embedded Lab. Dongkun Shin

Embedded Software Lab. 2 Contents 12

• Interrupt • System Call • Practice 1. Tizen App Calling a System Call • Practice 2. Making a New System Call • Practice 3. Tizen App Calling the New System Call

Embedded Software Lab. 3 Interrupts 12

• Interrupt (signal) – an event that alters the sequence of instructions executed by a processor

• Exception – by CPU control unit while executing instructions only after terminating the executing – By programming error – By anomalous condition ex. Divided by zero, Page Fault

Embedded Software Lab. 4 The Role of Interrupt Signal 12

• When the Interrupt signal arrives at Kernel CPU must stop what it’s currently doing, and switch to new activity. – 1) Save the current value of the program counter(eip, cs) in Kernel mode stack. – 2) Place an address related to the interrupt type into the program counter.

Interrupt User signal arrives User PC

Save old PC Kernel PC Kernel Memory representation when interrupt signal arrives

Embedded Software Lab. 5 System Call 12

• System Call – interface between user mode process and H/W device ex. brk, open, close, read, write • Advantages of System Call – Freeing users from studying low-level programming of H/W – Increasing system security – Making programs more portable

Embedded Software Lab. 6 System Call vs. POSIX API 12

User Application System Call • malloc(), free() return value`

– An explicit request to the kernel POSIX Library (ex. stdlib.h) made via a S/W interrupt syscall(45, brk_num) return value – Belong to kernel System Call Interface

ex. brk, open, close, read, write sys_brk(brk_num) return value

Kernel • cf. POSIX API – Common functions defined for compatibility between OSes. – It can be used on every UNIX-compatible OSes. • Linux, Darwin(OS X), (Windows), Contiki, Nuttx, Minix, … • Reference: https://en.wikipedia.org/wiki/C_POSIX_library – glibc: a C POSIX Library in Linux ex. malloc(), calloc(), free(), fopen(), fread(), fwrite()

Embedded Software Lab. 7 Parameter Passing 12

• Parameters are passed by using CPU registers 1. Write in the CPU registers before issuing the system call 2. Kernel copy the parameters in CPU registers onto the Kernel Mode stack before invoking the system call service routine • Reason – Working with two stacks at the same time is complex – Make the structure of the system call handler similar to that of exception handler • Constraints – The length of each parameter cannot exceed the length of a register – The number of parameters must not exceed 6

Embedded Software Lab. 8 Practice 1. Tizen App Calling a System Call 12

1. Download SystemCallApp on your Ubuntu shell 1. $ git clone https://github.com/SKKU-ESLAB-Tizen/SystemCallApp

Embedded Software Lab. 9 Practice 1. Tizen App Calling a System Call 12

2. Call “gettimeofday” system call – Location (App): src/gettimeofday_syscall.c

Embedded Software Lab. 10 Practice 2. Making a New System Call 12

1. System call function declaration – Location (Kernel): include/linux/syscalls.h (Line 850)

asmlinkage long sys_seccomp(unsigned int op, unsigned int flags, const char__user *uargs); asmlinkage long sys_print_hello(int value); #endif

Embedded Software Lab. 11 Practice 2. Making a New System Call 12

2. Make a system call function – Location (Kernel): kernel/printhello.c (New File)

#include #include

asmlinkage long sys_print_hello(int value) { const int answer = 10; printk(KERN_EMERG "Hello world: %\n", value); if(value == answer) { return 1; // answer } else { return -1; // error } }

Embedded Software Lab. 12 Practice 2. Making a New System Call 12

3. Add the source code to Makefile – Location (Kernel): kernel/Makefile (Line 13)

obj-y = fork.o exec_domain.o panic.o \ cpu.o exit.o itimer.o time.o softirq.o resource.o \ sysctl.o sysctl_binary.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o workqueue.o pid.o task_work.o \ extable.o params.o -timers.o \ kthread.o sys_ni.o posix-cpu-timers.o \ hrtimer.o nsproxy.o \ notifier.o ksysfs.o cred.o \ async.o range.o groups.o smpboot.o printhello.o

Embedded Software Lab. 13 Practice 2. Making a New System Call 12

4. Allocate a number to the system call – Location (Kernel): arch/arm/kernel/calls.S (Line 394) …/* 380 */ CALL(sys_ni_syscall) CALL(sys_ni_syscall) CALL(sys_ni_syscall) CALL(sys_seccomp) CALL(sys_print_hello) #ifndef syscalls_counted .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls

– Location (Kernel): arch/arm/include/asm/unistd.h (Line 18) #include

#define __NR_syscalls (388) #define __ARM_NR_cmpxchg (__ARM_NR_BASE+0x00fff0) Embedded Software Lab. 14 Practice 3. Calling the New System Call 12

2. Call “printhello” system call – Location (App): src/printhello_syscall.c

Embedded Software Lab.