Presentation is loading. Please wait.

Presentation is loading. Please wait.

OS Homework 1 February 22, 2017.

Similar presentations


Presentation on theme: "OS Homework 1 February 22, 2017."— Presentation transcript:

1 OS Homework 1 February 22, 2017

2 Outline Linux Installation Linux Kernel Compilation
System Call Development Kernel Modules / 45 2

3 Part I Linux Installation

4 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 (desktop version). Download Link: (64-bit) (32-bit) / 45 4

5 Preparation (1/2) A computer An Ubuntu 12.04 DVD or image
A clear head and relaxed mind Some drinks, food, or comics  / 45 5

6 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: Please refer to this website for creating a VM in VirtualBox. / 45 6

7 / 45 7

8 / 45 8

9 / 45 9

10 / 45 10

11 / 45 11

12 / 45 12

13 / 45 13

14 / 45 14

15 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 / 45 15

16 Some Useful Tools SSH server SSH client for Windows PieTTY
$ sudo apt-get install ssh SSH client for Windows PieTTY / 45 16

17 References 鳥哥的私房菜 Ubuntu 正體中文站 Ubuntu Homepage http://linux.vbird.org
Ubuntu Homepage / 45 17

18 Part II Linux Kernel Compilation

19 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 / 45 19

20 Download Source Archives
Kernel Website: Mirror: / 45 20

21 Preparation Setting up build environment Getting the source archive
$ sudo apt-get update $ sudo apt-get install gcc vim ssh libncurses5-dev libssl-dev Getting the source archive $ cd ${HOME} $ wget Extracting the archive $ tar Jxvf linux-3.X.X.tar.xz $ cd linux-3.X.X / 45 21

22 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 This step is optional. / 45 22

23 Kernel Configuration (2/2)
/ 45 23

24 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): Then reboot! / 45 24

25 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 ... / 45 25

26 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’ / 45 26

27 Rebooting Do not forget to reboot every time you install a new kernel image! You can use ‘uname -a’ to check current system information. / 45 27

28 Part III System Call Development

29 Preparation Find out whether the kernel is 32-bit or 64-bit version.
$ uname -p 32-bit: i386 i686 64-bit: x86_64 / 45 29

30 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 01 02 03 04 05 06 07 08 #include <linux/linkage.h> #include <linux/kernel.h> asmlinkage int sys_myservice(int arg1) { printk(“My service is invoked!\n"); return arg1 * 10; } / 45 30

31 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); / 45 31

32 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. / 45 32

33 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. / 45 33

34 System Call Invocation
Create a test file ap.c to try your system call. Compile & execute. $ gcc -o ap ap.c $ ./ap 10 01 02 03 04 05 06 07 08 09 10 11 12 13 #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; } / 45 34

35 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. / 45 35

36 Some Useful Tools (1/2) LXR Website: http://lxr.linux.no/linux/ / 45
36

37 Some Useful Tools (2/2) Manpages $ sudo apt-get install manpages-dev
$ man XXXX / 45 37

38 References Kernel Website LXR http://www.kernel.org
/ 45 38

39 Part IV Kernel Modules

40 (1/5) Definition of the module structure / 45 40

41 (2/5) Initialization and cleanup functions / 45 41

42 (3/5) File operations / 45 42

43 (4/5) User AP / 45 43

44 (5/5) Makefile Testing $ sudo insmod hello.ko $ dmesg $ sudo ./ap
$ sudo rmmod hello / 45 44

45 Contact us Lab 408 TA 張文 - R05922160@ntu.edu.tw
/ 45 45 45


Download ppt "OS Homework 1 February 22, 2017."

Similar presentations


Ads by Google