os project 0 february 25, 2015. outline linux installation linux kernel compilation system call...
TRANSCRIPT
![Page 1: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/1.jpg)
OS Project 0
February 25, 2015
![Page 2: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/2.jpg)
/ 452
Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules
![Page 3: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/3.jpg)
Part I
Linux Installation
![Page 4: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/4.jpg)
/ 454
Kernel versus Distribution Kernel is the core of the operating system including
scheduler, memory management, file system, etc. Distribution is a full functional environment
including the kernel and a large collection of software applications.
The following slides are based on Ubuntu 12.04.4 (desktop version). Download Link:
http://ftp.twaren.net/Linux/Ubuntu/ubuntu-cd/precise/ubuntu-12.04.4-desktop-amd64.iso (64-bit)
http://ftp.twaren.net/Linux/Ubuntu/ubuntu-cd/precise/ubuntu-12.04.4-desktop-i386.iso (32-bit)
![Page 5: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/5.jpg)
/ 455
Preparation (1/2) A computer An Ubuntu 12.04 DVD or image A clear head and relaxed mind Some drinks, food, or comics
![Page 6: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/6.jpg)
/ 456
Preparation (2/2) If you want to install Linux on your hard disk,
please set at least 16GB unpartitioned space. You may want to shrink your existing disk by disk
management if there is no more unpartitioned space.
Ubuntu can coexist with Windows. Change the boot sequence to boot from CD-ROM
first.
If you want to install Linux on a virtual machine, you can use VirtualBox or VMware. VirtualBox Website: https://www.virtualbox.org/
Please refer to this website for creating a VM in VirtualBox.
![Page 7: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/7.jpg)
/ 457
![Page 8: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/8.jpg)
/ 458
![Page 9: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/9.jpg)
/ 459
![Page 10: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/10.jpg)
/ 4510
![Page 11: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/11.jpg)
/ 4511
![Page 12: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/12.jpg)
/ 4512
![Page 13: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/13.jpg)
/ 4513
![Page 14: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/14.jpg)
/ 4514
![Page 15: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/15.jpg)
/ 4515
Some Useful Commands Installing packages in Ubuntu
$ apt-cache search xxxx $ sudo apt-get install xxxx
GNU Compiler Collection $ gcc -o test test.c $ ./test
Makefile $ make
Text-mode editor $ vim
SSH server and client $ ssh -p port user@hostname
![Page 16: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/16.jpg)
/ 4516
Some Useful Tools SSH server
$ sudo apt-get install ssh
SSH client for Windows PieTTY http://ntu.csie.org/~piaip/pietty/
![Page 17: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/17.jpg)
/ 4517
References 鳥哥的私房菜
http://linux.vbird.org Ubuntu 正體中文站
http://www.ubuntu-tw.org Ubuntu Homepage
http://www.ubuntu.com
![Page 18: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/18.jpg)
Part II
Linux Kernel Compilation
![Page 19: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/19.jpg)
/ 4519
When Should We Compile Kernel? There are various situations we have to
compile the kernel. Try new kernel patches for the latest laptops. Enable/disable features built in the kernel. Develop new features. …
This slides are based on Linux 3.13.2. http://
ftp.ntu.edu.tw/linux/kernel/v3.x/linux-3.13.2.tar.xz
![Page 20: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/20.jpg)
/ 4520
Download Source Archives Kernel Website: http://www.kernel.org
Mirror: http://ftp.ntu.edu.tw/linux/kernel/v3.x/
![Page 21: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/21.jpg)
/ 4521
Preparation Setting up build environment
$ sudo apt-get update $ sudo apt-get install gcc vim ssh libncurses5-dev
Getting the source archive $ cd ${HOME} $ wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.X.X.tar.xz
Extracting the archive $ tar Jxvf linux-3.X.X.tar.xz $ cd linux-3.X.X
![Page 22: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/22.jpg)
/ 4522
Kernel Configuration (1/2) There are several ways to configure the
kernel. $ make config $ make menuconfig $ make xconfig
(It will need qt4-qmake and qt4-dev-tools packages.) ...
If you do not know how to configure, you can copy the old configuration file from /boot. $ cp /boot/config-`uname -r` .config $ make olddefconfig $ make menuconfig
This step is optional.
![Page 23: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/23.jpg)
/ 4523
Kernel Configuration (2/2)
![Page 24: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/24.jpg)
/ 4524
Kernel Compilation The first time (about 30min ~ 1.5hr):
$ make bzImage $ make modules (It takes quite a long time.) $ sudo make modules_install $ sudo make install $ sudo mkinitramfs -o /boot/initrd.img-3.X.X 3.X.X Do this whenever you reinstall the modules.
Configure the boot loader and then reboot! Others (about 5~10min):
$ make bzImage $ sudo make install Then reboot!
![Page 25: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/25.jpg)
/ 4525
Kernel Compilation with Multiple Cores See how many cores on your machine.
$ cat /proc/cpuinfo | grep processor | wc -l E.g., 4.
Compile with the number of jobs (threads). $ make -j4 bzImage $ make -j4 modules ...
![Page 26: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/26.jpg)
/ 4526
Boot Loader Configuration (Optional) Ubuntu will do this for you when installing the
kernel. Adding a new entry to /boot/grub/grub.cfg
If you got a black screen after booting the new kernel Change ‘gfxmode $linux_gfx_mode’ ‘gfxmode text’
![Page 27: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/27.jpg)
/ 4527
Rebooting Do not forget to reboot every time you install
a new kernel image!
You can use ‘uname -a’ to check current system information.
![Page 28: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/28.jpg)
Part III
System Call Development
![Page 29: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/29.jpg)
/ 4529
Preparation Find out whether the kernel is 32-bit or 64-bit
version. $ uname -p
32-bit: i386 i686 64-bit: x86_64
![Page 30: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/30.jpg)
/ 4530
Adding a System Call (1/4) Define your system calls.
Create a new file kernel/myservice.c.
Add this new attribute to kernel/Makefile. obj-y += myservice.o
#include <linux/linkage.h>#include <linux/kernel.h>
asmlinkage int sys_myservice(int arg1){ printk(“My service is invoked!\n"); return arg1 * 10;}
0102030405060708
![Page 31: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/31.jpg)
/ 4531
Adding a System Call (2/4) Declare the prototype of your system calls.
Add the declaration to include/linux/syscalls.h. asmlinkage int sys_myservice(int arg1);
![Page 32: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/32.jpg)
/ 4532
Adding a System Call (3/4) Register a system call number (32-bit).
For Linux 3.X, it has a automatic way to generate system calls compared to the previous versions.
Add a new entry to arch/x86/syscalls/syscall_32.tbl. 350 i386 myservice sys_myservice
The generated file is in arch/x86/include/generated/uapi/asm, after compilation.
![Page 33: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/33.jpg)
/ 4533
Adding a System Call (4/4) Register a system call number (64-bit).
Add a new entry to arch/x86/syscalls/syscall_64.tbl. 313 common myservice sys_myservice
The generated file is in arch/x86/include/generated/uapi/asm, after compilation.
![Page 34: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/34.jpg)
/ 4534
System Call Invocation Create a test file ap.c to try your system call.
Compile & execute. $ gcc -o ap ap.c $ ./ap 10
#include <stdlib.h>#include <stdio.h>#define _GNU_SOURCE#include <unistd.h>#include <sys/syscall.h>
#define __NR_myservice 350
int main(int argc, char** argv){ printf("%d\n", syscall(__NR_myservice, atoi(argv[1]))); return 0;}
01020304050607080910111213
![Page 35: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/35.jpg)
/ 4535
Related APIs Access a block of data from/to user space in
the kernel. #include <linux/uaccess.h> unsigned long copy_from_user (void* to, const void __user* from, unsigned long n);
unsigned long copy_to_user (void __user* to, const void* from, unsigned long n);
Use the generic function syscall to invoke a specific system call in user space. #include <sys/syscall.h> int syscall(int number, ...);
The first argument, number, is the system call number.
![Page 36: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/36.jpg)
/ 4536
Some Useful Tools (1/2) LXR Website: http://lxr.linux.no/linux/
![Page 37: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/37.jpg)
/ 4537
Some Useful Tools (2/2) Manpages
$ sudo apt-get install manpages-dev $ man XXXX
![Page 38: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/38.jpg)
/ 4538
References Kernel Website
http://www.kernel.org LXR
http://lxr.linux.no/linux/
![Page 39: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/39.jpg)
Part IV
Kernel Modules
![Page 40: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/40.jpg)
/ 4540
An Example of Writing a Kernel Module(1/5) Definition of the module structure
![Page 41: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/41.jpg)
/ 4541
An Example of Writing a Kernel Module(2/5) Initialization and cleanup functions
![Page 42: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/42.jpg)
/ 4542
An Example of Writing a Kernel Module(3/5) File operations
![Page 43: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/43.jpg)
/ 4543
An Example of Writing a Kernel Module(4/5) User AP
![Page 44: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/44.jpg)
/ 4544
An Example of Writing a Kernel Module(5/5) Makefile
Testing $ sudo insmod hello.ko $ dmesg $ sudo ./ap $ dmesg $ sudo rmmod hello
![Page 45: OS Project 0 February 25, 2015. Outline Linux Installation Linux Kernel Compilation System Call Development Kernel Modules / 452](https://reader035.vdocument.in/reader035/viewer/2022062409/5697c0021a28abf838cc2c22/html5/thumbnails/45.jpg)
/ 4545
Q & A