CSCI-3753: Operating Systems Fall 2019
Anh Nguyen Department of Computer Science University of Colorado Boulder Week 2: System Calls vs Loadable Kernel Modules (LKMs)
CSCI 3753 Fall 2019 2 What is a System Call?
• A programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on
• Each system call corresponds to a number defined in a syscalls table.
syscall(number, …)
CSCI 3753 Fall 2019 3 Do we use system calls at all?
• Example 1: • If you open a file using fopen() in the library stdio.h, it gets translated into the open() system call. • In the standard library, the user-space implementation of the open() system call executes and passes the system call number.
• Example 2: • In Unix-like systems, fork() and exec() are C- library functions that in turn execute instructions that invoke the fork() and exec() system calls.
CSCI 3753 Fall 2019 4 EXECUTing a System Call
1. A user space program invokes the syscall 2. A (typically) software interrupt called a trap is triggered (INT) 3. Mode bit is flipped from user to kernel (1 to 0) 4. The interrupt tells the kernel which syscall was called 1. Requisite data may be passed in 2. The kernel verifies if all parameters are legal before executing the system call 5. After execution, mode bit flips and user program resumes
CSCI 3753 Fall 2019 5 ADDing a System Call
1. Write the system call source code arch/x86/kernel/newSysCall.c 2. Add the syscall prototype to the syscalls header file include/linux/syscalls.h 3. Add the new syscall to the Makefile arch/x86/kernel/Makefile 4. Add the syscall to the syscalls table arch/x86/entry/syscalls/syscall_64.tbl 5. Re-compile the kernel
CSCI 3753 Fall 2019 6 Protection of Passing Values Between Spaces
• copy_from_user() • get_user()
• copy_to_user() • put_user()
• printk()
• cat /var/log/syslog • dmesg
CSCI 3753 Fall 2019 7 How to Extend a Kernel?
• Method 1: System calls
Recompile the kernel !!!
è It takes a long time to compile the kernel source code (2-3 hours)
• Method 2: Loadable Kernel Module (LKM)
CSCI 3753 Fall 2019 10 Loadable Kernel Module (LKM)
• LKM is an object file that contains a chunk of code to add to the base kernel of an operating system while it is RUNNING.
• Typically, it is used to add support for • New hardware as device drivers • Filesystem drivers
CSCI 3753 Fall 2019 11 LKM Pros & Cons
• Pros: • DON’T have to REBUILD the kernel • Save memory • Unloaded in order to free memory and other resources when it is no longer required • Be MUCH faster to maintain and debug
• Cons • FRAGMENTATION penalty • Security
CSCI 3753 Fall 2019 12 LKM Utilities
• insmod: Insert an LKM into the kernel. • rmmod: Remove an LKM from the kernel. • lsmod: List currently loaded LKMs. • modprobe: Insert/remove an LKM or set of LKMs intelligently. • e.g., if you must load A before loading B, modprobe will automatically load A when you tell it to load B. • kerneld: Kernel daemon program • allows kernel modules to be loaded automatically rather than manually with insmod/modprobe
CSCI 3753 Fall 2019 13 LKM Utilities
• depmod: Determine interdependencies between LKMs. • ksyms: Display symbols that are exported by the kernel for use by new LKMs. • modinfo: Display contents of .modinfo section in an LKM object file.
CSCI 3753 Fall 2019 14 Week 2 – Checklist q Submit PS1 by 6PM today !!! q Start PA1 q Read more about LKMs q Quiz o Every week o Open book BUT NO use of device to interact with others o Only ONE attempt
CSCI 3753 Fall 2019 16