national taiwan university os project 0 & 1 advisor: dr. chih-wen hsueh student: tang-hsun tu...
Post on 26-Dec-2015
216 Views
Preview:
TRANSCRIPT
National Taiwan University
OS Project 0 & 1
Advisor: Dr. Chih-Wen Hsueh
Student: Tang-Hsun Tu
台灣大學網媒所 / 資工所
Wireless Networking and Embedded Systems Laboratory Real-Time System Software Group
April 19, 2023
/602National Taiwan University
Tang-Hsun Tu
Linux Installation Linux Kernel Compilation System Call Project Hints
Outline
National Taiwan University
Linux Installation
/604National Taiwan University
Tang-Hsun Tu
Including shells, libraries, tools, compiler, servers, applications.
Ubuntu, Redhat, Fedora, Mandrake, SuSE, Debian, Ubuntu, Gentoo …
This slides are based on Ubuntu 10.04 LTS. 11.04 11.10 Beta Linux 3.0
http://www.ubuntu-tw.org/modules/tinyd0/
Linux Distribution
/605National Taiwan University
Tang-Hsun Tu
Windows can be also installed together. Modify the boot sequence to boot your
computer from CD-ROM. Make sure your hardware and device. A clear head and relaxed mind. Some drinks and food.
Before Installation
/606National Taiwan University
Tang-Hsun Tu
/607National Taiwan University
Tang-Hsun Tu
/608National Taiwan University
Tang-Hsun Tu
/dev/hda, /dev/hdb, /dev/hdc, … /dev/hda1, /dev/hda2, …
/dev/sda, /dev/scd0, … Mount Points
/ /swap …
Disks and Partitions
/609National Taiwan University
Tang-Hsun Tu
/6010National Taiwan University
Tang-Hsun Tu
Swap partition is usually twice as RAM when it is less than 1GB.
No more than 4 primary partitions including / partition and swap partition.
If the primary partitions are not enough, you can use logical partitions.
Make sure all your mount points are correct.
Partition Division
/6011National Taiwan University
Tang-Hsun Tu
/6012National Taiwan University
Tang-Hsun Tu
/6013National Taiwan University
Tang-Hsun Tu
After dividing partitions, you only need to click your mouse.
After installation, reboot and enjoy your Linux!
Installation by text mode is similar to graphic mode.
End of Installation
/6014National Taiwan University
Tang-Hsun Tu
Install packages in Ubuntu. apt-cache search xxxx apt-get install xxxx
GNU Compiler Collection. gcc –o test test.c ./test
Makefile. make
Command-line editor. vim
SSH server and client. ssh –oPort=port accout@ip
Some Useful Commands
/6015National Taiwan University
Tang-Hsun Tu
Install ssh server. apt-get install ssh
A ssh client on Windows. http://ntu.csie.org/~piaip/pietty/
Some Useful Tools
/6016National Taiwan University
Tang-Hsun Tu
鳥哥的私房菜 http://linux.vbird.org/
Ubuntu 正體中文站 http://www.ubuntu-tw.org/
Ubuntu Homepage http://www.ubuntu.com/
Reference
National Taiwan University
Linux Kernel Compilation
/6018National Taiwan University
Tang-Hsun Tu
Kernel is the core of an operating system. Process management Memory management Scheduler Filesystem Virtualization …
What is Linux Kernel?
/6019National Taiwan University
Tang-Hsun Tu
You can configure your Linux by compiling a new kernel. Add new features, eg. patch kernel. Support new hardware. Disable functions you don’t need. Develop your own kernel. ...
When Should We Compile Kernel?
/6020National Taiwan University
Tang-Hsun Tu
Kernel Website, http://www.kernel.org
You also can download the source codes patched by Ubuntu.
Where to D/L Linux Kernel?
/6021National Taiwan University
Tang-Hsun Tu
You need to be root to compile kernel. /usr/src/… sudo -i
Download the necessary tools. apt-get update apt-get install kernel-package gcc libncurses5-dev
automake gcc libc6-dev build-essential Go to System/Administration/synaptic
package Manager or http://www.kernel.org/. Get the kernel source codes e.g. 3.0.1
Prepare Your Kernel Source Code
/6022National Taiwan University
Tang-Hsun Tu
Unzip kernel source code. cd /usr/src tar jxvf linux-source-3.X.tar.bz2
You may have many versions of Linux source codes.
Prepare Your Kernel Source Code (Cont)
/6023National Taiwan University
Tang-Hsun Tu
There are many ways to configure. make config make menuconfig make xconfig ...
If you do not know how to configure, you can copy the old config file from /boot. make mrproper cp /boot/config-`uname -r` .config make oldconfig make menuconfig
Configure your Kernel
/6024National Taiwan University
Tang-Hsun Tu
Configure your Kernel (Cont)
/6025National Taiwan University
Tang-Hsun Tu
If you meet some problems (SATA). Device Drivers --->
SCSI device support ---> <*> SCSI device support <*> SCSI disk support
Device Drivers ---> SCSI device support --->
SCSI low-level drivers ---> [*] Serial ATA (SATA) support
Configure your Kernel (Cont)
/6026National Taiwan University
Tang-Hsun Tu
The first compilation (about 30min ~ 1.5hr): #make clean #make bzImage #make modules (long time) #make install #make modules_install #mkinitramfs –o /boot/initrd.img-3.x 3.x
do this only if you reinstall modules then reboot!
The second compilation (about 5~10min in our project): #make bzImage #make install then reboot!
Kernel Compilation
/6027National Taiwan University
Tang-Hsun Tu
See how many cores or hyper-threading 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 ...
Speed up Kernel Compilation
/6028National Taiwan University
Tang-Hsun Tu
Setup your boot manager. vim /boot/grub/menu.lst
Add the following section (Grub1): title Ubuntu 10.04 LTS, kernel 3.0.1 uuid xxxx kernel /boot/vmlinuz-3.0.1 root=UUID=xxxx ro
splash initrd /boot/initrd.img-3.0.1
Configure your Boot Menu
/6029National Taiwan University
Tang-Hsun Tu
Setup your boot manager. vim /boot/grub/grub.cfg
Add the following section (Grub2):
Configure your Boot Menu (Cont)
/6030National Taiwan University
Tang-Hsun Tu
鳥哥的私房菜 http://linux.vbird.org/
Google http://www.google.com/
Reference
National Taiwan University
System Call
/6032National Taiwan University
Tang-Hsun Tu
System call is the mechanism used by an application program to request service from the OS.
Users can use it to communicate with kernel. Here are two approaches developing our own
system calls. Using kernel module Modify the source codes of Linux directly
Introduction
/6033National Taiwan University
Tang-Hsun Tu
Building system calls in kernel module is more flexible than modifying kernel.
When we want to use our system call, just install our kernel modules. Also, if we don’t need it right away, just remove modules. Modifying kernel is not necessary. (But you still
need to modify your kernel for OS project 1.)
Using Kernel Module
/6034National Taiwan University
Tang-Hsun Tu
For sys_call_table, your should extern it in a file such as <top directory to the kernel sources>/arch/x86/kernel/i386_ksyms_32.c.
Export sys_call_table
extern void* sys_call_table[];/*variable should be exported. */ EXPORT_SYMBOL(sys_call_table);
010203
/6035National Taiwan University
Tang-Hsun Tu
sys_call_table is read-only after kernel version 2.6.23.
If you want to try this method using kernel version which is higher than 2.6.23, you have to modify your current kernel source codes and recompile it.
Export sys_call_table (Cont)
/6036National Taiwan University
Tang-Hsun Tu
Firstly, check your compiled kernel version. uname –a
In x86 32bit vim /usr/src/linux-3.x/arch/x86/kernel/entry_32.S .section .rodata, “a” .section .data, “aw”
Export sys_call_table (Cont)
/6037National Taiwan University
Tang-Hsun Tu
In x86 64bit vim /usr/src/linux-3.x/arch/x86/kernel/syscall_64.c line 22: delete the “const”
Export sys_call_table (Cont)
/6038National Taiwan University
Tang-Hsun Tu
Add to export symbol. vim /usr/src/linux-3.x/kernel/kallsyms.c extern void *sys_call_table; EXPORT_SYMBOL(sys_call_table);
Export sys_call_table (Cont)
/6039National Taiwan University
Tang-Hsun Tu
vim makefile vim myservice.c
Write Your Makefile
/6040National Taiwan University
Tang-Hsun Tu
Include and Define.
Extern the “sys_call_table”.
Write your own system call.
Write Kernel Module
#include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> #define __NR_mysyscall 200 /* define the number of our system call */
010203
typedef void (*sys_call_ptr_t)(void);extern sys_call_ptr_t sys_call_table[];sys_call_ptr_t orig_sys_call;
010203
/* Our system call */ asmlinkage int mysyscall(int arg1) { printk("enter mysyscall()\n"); return arg1 * 10; }
0102030405
/6041National Taiwan University
Tang-Hsun Tu
Initialize the kernel module.
Extern the “sys_call_table”.
Write Kernel Module (Cont)
/* Initialize the module - replace the system call */ int init_module() { printk("Insert mysyscall module\n"); orig_sys_call = sys_call_table[__NR_mysyscall]; sys_call_table[__NR_mysyscall] = mysyscall; return 0; }
01020304050607
/* Cleanup - unregister the appropriate file from /proc */void cleanup_module() { printk("Remove mysyscall module\n"); sys_call_table[__NR_mysyscall] = orig_sys_call; }
0102030405
/6042National Taiwan University
Tang-Hsun Tu
Compile. make
Insert the module to kernel. insmod ./myservice.ko
Remove the module from kernel. rmmod myservice
List the modules in kernel. lsmod
Use Kernel Module
/6043National Taiwan University
Tang-Hsun Tu
Write an application to use your system call. vim ap.c
Compile and execute. gcc ap.c –o ap ./ap 10
User Application
#include <stdlib.h>#include <stdio.h>#define _GNU_SOURCE #include <unistd.h>#include <sys/syscall.h>
#define __NR_mysyscall 200
int main(int argc,char *argv[]){ printf("%d\n", syscall(__NR_mysyscall, atoi(argv[1]))); return 0;}
01020304050607080910111213
/6044National Taiwan University
Tang-Hsun Tu
Here are two approaches developing our own system calls. Using kernel module Modify the source codes of Linux directly
Build Your Own System Calls
/6045National Taiwan University
Tang-Hsun Tu
Create a new file in /usr/src/linux-3.x/kernel/. vim myservice.c
Add your system call.
Write Your System Call
#include <linux/linkage.h>#include <linux/kernel.h>
asmlinkage int sys_myservice(int arg1){ printk("my service is invoked!\n"); return arg1 * 10;}
0102030405060708
/6046National Taiwan University
Tang-Hsun Tu
In x86 32bit /usr/src/linux-3.x/arch/x86/include/asm/unistd_32.h
The index must be the last in the list. e.g. #define __NR_myservice 347
Write Your System Call (Cont) - x86_32
/6047National Taiwan University
Tang-Hsun Tu
Create an entry (function name) in system call table. /usr/src/linux-3.x/arch/x86/kernel/syscall_table_32.S .long sys_myservice
Write Your System Call (Cont) - x86_32
/6048National Taiwan University
Tang-Hsun Tu
In x86 64bit /usr/src/linux-3.x/arch/x86/include/asm/unistd_64.h
The index must be the last in the list. #define __NR_myservice 309 __SYSCALL(__NR_myservice, sys_myservice)
Write Your System Call (Cont) - x86_64
/6049National Taiwan University
Tang-Hsun Tu
Define the prototype. /usr/src/linux-3.x/include/linux/syscalls.h asmlinkage int sys_myservice(int arg1);
Write Your System Call (Cont)
/6050National Taiwan University
Tang-Hsun Tu
Add to makefile to compile. vim /usr/linux-3.x/kernel/Makefile obj-y += myservice.o
Now, you can recompile your kernel.
Write Your System Call (Cont)
/6051National Taiwan University
Tang-Hsun Tu
Write an application to use your system call. vim ap.c
Compile and execute. gcc ap.c –o ap ./ap 10
User Application
#include <stdlib.h>#include <stdio.h>#define _GNU_SOURCE#include <unistd.h>#include <sys/syscall.h>
#define __NR_myservice 309
int main(int argc,char *argv[]){ printf("%d\n", syscall(__NR_myservice, atoi(argv[1]))); return 0;}
01020304050607080910111213
/6052National Taiwan University
Tang-Hsun Tu
Socket programming server: socket/bind/listen/accept client: socket/connect
New system calls sendmsg() recvmsg() sendmmsg(), 3.0↑ recvmmsg(), 2.6.33↑
Hints (1/4)
/6053National Taiwan University
Tang-Hsun Tu
Since we want measure the number of invoking time of sendmsg()/recvmsg() in sendmmsg()/recvmmsg(), we might need to add counters in /usr/src/Linux-3.x/net/socket.c. e.g. int nr_called_sendmsg;
If you want to use this variable in your system call or kernel module, you have to export it. EXPORT_SYMBOL(nr_called_sendmsg);
printk can print messages in kernel, use dmesg to check.
Hints (2/4)
/6054National Taiwan University
Tang-Hsun Tu
Hints (3/4) A generic interface: syscall(int no, ...)
__NR_sendmmsg and __NR_recvmmsg
syscall()
sendmmsg()/recvmmsg()
/6055National Taiwan University
Tang-Hsun Tu
Results
Hints (4/4)
server
client
1. syscall number of “recvmmsg()”2. received messages
3. the invoking times of “recvmsg()” in the kernel4. time of invoking recvmmsg()
1. typed message2. syscall number of “sendmmsg()”3. the invoking times of “sendmsg()” in the kernel4. time of invoking sendmmsg()
/6056National Taiwan University
Tang-Hsun Tu
LXR, http://rswiki.csie.org/lxr/http/source
Some Useful Tool
/6057National Taiwan University
Tang-Hsun Tu
Manpages apt-get install manpages-dev
Some Useful Tool (Cont)
/6058National Taiwan University
Tang-Hsun Tu
Kernel Website http://www.kernel.org
Socket Programming http://www.tenouk.com/Module41b.html
LXR http://rswiki.csie.org/lxr/http/source http://lxr.linux.no/linux/
Google http://www.google.com/
Recvmmsg() http://lwn.net/Articles/334532/
Sendmmsg() http://lwn.net/Articles/441169/
Reference
/6059National Taiwan University
Tang-Hsun Tu
We set up a desktop PC with virtualization in Lab202. cd /home/lab202/os_cfg Start Xen: ./runxen.sh Start Ubuntu VM: ./runubu.sh or ./runubu2.sh Start WinXP VM: ./runxp.sh Start VGA pass-through WinXP VM:
sudo ./runvgaxp.sh &
You can compile your kernels on the VMs! Create your own VM disk.
dd or qemu-img-xen create vmdisk.img 8G Backups are in /home/lab202/backup. Account and password.
Announced in the courses or send a mail to TA.
Resource in Lab202
/6060National Taiwan University
Tang-Hsun Tu
Q & A
top related