Week 3: Make Your Own System Call CS444/544 System Call
Total Page:16
File Type:pdf, Size:1020Kb
Week 3: Make Your Own System Call CS444/544 System Call • A system call is used by application (user) programs to request service from the operating system. • Switch to kernel mode.(higher privilege) • Required to ensure security. How to add a system call • Register the system call. • Declare the system call in the system header file. • Create a source file and a make file for implementing your system call. • Compile the kernel. • Create a client to test it. Steps • VM present in e:\systemcall\Lab1\ubuntu-server.vmx • For ubuntu go to /storage/systemcall/Lab1 • Log in the Virtual Machine • Username: root • Password: cspassword • Check the system kernel info (kernel version, 32/64bit) and note it down • uname -sr • Go to Linux kernel source code directory • cd /usr/src/linux Before the Lab ● Be careful to modify the kernel file ● Be aware of the naming consistency ● Don't miss any steps WHY? ○ Because you are hacking the kernel ○ Because no debug environment is set up yet Assume: Print Working Directory(pwd) is /usr/src/linux Edit arch/x86/syscalls/syscall_64.tbl to register your system call: Above x32-specific system call table around Line 323, register as 31x number, follow the same pattern 31? common yourname sys_yourname (should be 313) Edit include/linux/syscalls.h to declare your system call function: Right at the very end before #endif, add the system call function asmlinkage int sys_yourname(int num1, int num2); Create your directory(mkdir) kernel/yourname/ and create two files sys_yourname.c and Makefile inside to define your system call function sys_yourname.c in kernel/yourname/ should look like #include<linux/kernel.h> #include<linux/syscalls.h> asmlinkage int sys_yourname(int num1, int num2){ return num1+num2; } Makefile in kernel/yourname/ should look like obj-y := sys_yourname.o Edit kernel/Makefile to include kernel/yourname/ directory Right below obj-y +=power/, to add the line obj-y += yourname/ File Checklist Assume: Your Print Working Directory(pwd) is /usr/src/linux ● arch/x86/syscalls/syscall_64.tbl ● include/linux/syscalls.h ● kernel/yourname/sys_yourname.c ● kernel/yourname/Makefile ● kernel/Makefile Compile the Kernel Assume: Print Working Directory(pwd) is /usr/src/linux Generate kernel configuration file .config using: make clean make mrproper make menuconfig make localmodconfig Compile the kernel using: make -j2 Copy all kernel modules into /lib/modules/3.7.2/kernel/folder using: make modules_install Copy vmlinuz, system.map, and initramfs into /boot using: make install Update bootloader using: update-grub Reboot the System command: reboot • Assume: Present Working Directory(pwd) is /root • Check your system kernel version again • If it is still the previous kernel version, you need to go back to check your kernel compilation steps. Test your System Call Assume: Present Working Directory(pwd) is /root/yourname Create a directory yourname/ and create three files testyourname.h, testyourname.c, and Makefile in yourname/ Header file testanyname.h #include <linux/unistd.h> int yourname(int num1, int num2) { return syscall(syscallNO, num1, num2); } User code file testanyname.c #include <stdio.h> #include "testanyname.h" int main() { printf("the kernel returns %d\n", yourname(1, 25)); return 0; } Makefile will instruct the compiler and the linker how to compile and link all the separated files to be one program. Makefile all: testanyname testanyname: testanyname.c testanyname.h gcc testanyname.c -o testanyname //should have one tab in front of gcc, not //whitespace clean: rm testanyname Generate the executable file with command: make, testyourname should appear in yourname/ directory Does it run? Yes-> ask TA for credit. No-> ask for help. .