+ All Categories
Home > Documents > OS Project 0 & 1

OS Project 0 & 1

Date post: 09-Jan-2016
Category:
Upload: kreeli
View: 33 times
Download: 3 times
Share this document with a friend
Description:
OS Project 0 & 1. Advisor: Dr. Chih-Wen Hsueh Student: Tang-Hsun Tu 台灣大學 網媒所 / 資工所 Wireless Networking and Embedded Systems Laboratory Real-Time System Software Group October 3, 2014. Linux Installation Linux Kernel Compilation System Call. Outline. Linux Installation. - PowerPoint PPT Presentation
56
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 June 23, 2022
Transcript
Page 1: OS Project 0 & 1

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 21, 2023

Page 2: OS Project 0 & 1

/552National Taiwan University

Tang-Hsun Tu

Linux Installation Linux Kernel Compilation System Call

Outline

Page 3: OS Project 0 & 1

National Taiwan University

Linux Installation

Page 4: OS Project 0 & 1

/554National Taiwan University

Tang-Hsun Tu

Including shells, libraries, tools, compiler, servers, applications.

Ubuntu, Redhat, Fedora, Mandrake, SuSE, Debian, Ubuntu, Gentoo…

Live CDs You can make your own Linux distribution This slide is based on Ubuntu 10.04 LTS

Linux Distribution

Page 5: OS Project 0 & 1

/555National Taiwan University

Tang-Hsun Tu

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

Page 6: OS Project 0 & 1

/556National Taiwan University

Tang-Hsun Tu

Page 7: OS Project 0 & 1

/557National Taiwan University

Tang-Hsun Tu

Page 8: OS Project 0 & 1

/558National 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

Page 9: OS Project 0 & 1

/559National Taiwan University

Tang-Hsun Tu

Swap partition is usually twice as RAM when it is less than 1GB

No more than four primary partition including root partition and swap partition

If four isn’t enough, use extend partition Make sure all your mount points are correct

Partition Division

Page 10: OS Project 0 & 1

/5510National Taiwan University

Tang-Hsun Tu

Page 11: OS Project 0 & 1

/5511National Taiwan University

Tang-Hsun Tu

Page 12: OS Project 0 & 1

/5512National Taiwan University

Tang-Hsun Tu

Page 13: OS Project 0 & 1

/5513National 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 the same as graphic mode

End of Installation

Page 14: OS Project 0 & 1

/5514National Taiwan University

Tang-Hsun Tu

鳥哥的私房菜 http://linux.vbird.org/

Ubuntu 正體中文站 http://www.ubuntu-tw.org/

Ubuntu Homepage http://www.ubuntu.com/

Reference

Page 15: OS Project 0 & 1

National Taiwan University

Linux Kernel Compilation

Page 16: OS Project 0 & 1

/5516National Taiwan University

Tang-Hsun Tu

Kernel is the core of an operating system. Scheduler, task management, memory

management, … You need to compile kernel source code to

binary in order to run.

What is Linux Kernel?

Page 17: OS Project 0 & 1

/5517National Taiwan University

Tang-Hsun Tu

You can configure your Linux by compiling a new kernel. Add new features, ex. patch kernel. Support new hardware. Disable functions you don’t need. Develop your own kernel. ...

When Should We Compile Kernel?

Page 18: OS Project 0 & 1

/5518National Taiwan University

Tang-Hsun Tu

Kernel Website, http://www.kernel.org

You also can download the source from Ubuntu.

Where to D/L Linux Kernel?

Page 19: OS Project 0 & 1

/5519National Taiwan University

Tang-Hsun Tu

You need to be root to compile kernel 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 code e.g. 2.6.35.5

Prepare Your Kernel Source Code

Page 20: OS Project 0 & 1

/5520National Taiwan University

Tang-Hsun Tu

Unzip kernel source code. cd /usr/src tar jxvf linux-source-2.6.X.tar.bz2

You may have many versions of Linux source codes.

Prepare Your Kernel Source Code (Cont)

Page 21: OS Project 0 & 1

/5521National 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 menuconfig

Configure your Kernel

Page 22: OS Project 0 & 1

/5522National Taiwan University

Tang-Hsun Tu

Configure your Kernel (Cont)

Page 23: OS Project 0 & 1

/5523National 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)

Page 24: OS Project 0 & 1

/5524National Taiwan University

Tang-Hsun Tu

#make clean #make bzImage #make modules #make modules_install #make install #mkinitramfs –o /boot/initrd.img-2.6.x 2.6.x

Kernel Compilation

Page 25: OS Project 0 & 1

/5525National Taiwan University

Tang-Hsun Tu

See how many cores/hyperthreading on your machine cat /proc/cpuinfo | grep processor | wc –l e.g. 8

Compile with the number of jobs make –j8 bzImage make –j8 modules ...

Speed up Kernel Compilation

Page 26: OS Project 0 & 1

/5526National 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 2.6.35.5 uuid xxxx kernel /boot/vmlinuz-2.6.35.5 root=UUID=xxxx

ro splash initrd /boot/initrd.img-2.6.35.5

Configure your Boot Menu

Page 27: OS Project 0 & 1

/5527National Taiwan University

Tang-Hsun Tu

Setup your boot manager. vim /boot/grub/grub.cfg

Add the following section (Grub2):

Configure your Boot Menu (Cont)

Page 28: OS Project 0 & 1

/5528National 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

Page 29: OS Project 0 & 1

/5529National Taiwan University

Tang-Hsun Tu

鳥哥的私房菜 http://linux.vbird.org/

Google http://www.google.com/

Reference

Page 30: OS Project 0 & 1

National Taiwan University

System Call

Page 31: OS Project 0 & 1

/5531National 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 code of linux directly

Introduction

Page 32: OS Project 0 & 1

/5532National 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; and 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 O.S. project one.)

Using Kernel Module

Page 33: OS Project 0 & 1

/5533National 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

Page 34: OS Project 0 & 1

/5534National Taiwan University

Tang-Hsun Tu

sys_call_table is read-only after kernel version 2.6.23.

If you really want to try this method using kernel version which is higher than 2.6.23, you will have to modify your current kernel source and recompile it.

Export sys_call_table (Cont)

Page 35: OS Project 0 & 1

/5535National Taiwan University

Tang-Hsun Tu

Firstly, check your compiled kernel version uname –a

In x86 32bit vim /usr/src/linux-2.6.x/arch/x86/kernel/ entry_32.S .section .rodata, “a” .section .data, “aw”

Export sys_call_table (Cont)

Page 36: OS Project 0 & 1

/5536National Taiwan University

Tang-Hsun Tu

In x86 64bit vim /usr/src/linux-2.6.x/arch/x86/kernel/syscall_64.c line 22: delete the “const”

Export sys_call_table (Cont)

Page 37: OS Project 0 & 1

/5537National Taiwan University

Tang-Hsun Tu

Add to export symbol vim /usr/src/linux-2.6.x/kernel/kallsyms.c extern void *sys_call_table; EXPORT_SYMBOL(sys_call_table);

Export sys_call_table (Cont)

Page 38: OS Project 0 & 1

/5538National Taiwan University

Tang-Hsun Tu

vim makefile vim myservice.c

Write Your Makefile

Page 39: OS Project 0 & 1

/5539National 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 n) { printk("enter mysyscall()\n"); return 2*n; }

0102030405

Page 40: OS Project 0 & 1

/5540National 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

Page 41: OS Project 0 & 1

/5541National 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

Page 42: OS Project 0 & 1

/5542National 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

Page 43: OS Project 0 & 1

/5543National Taiwan University

Tang-Hsun Tu

Here are two approaches developing our own system calls Using kernel module Modify the source code of Linux directly

Build Your Own System Calls

Page 44: OS Project 0 & 1

/5544National Taiwan University

Tang-Hsun Tu

Create a new file in /usr/src/linux-2.6.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

Page 45: OS Project 0 & 1

/5545National Taiwan University

Tang-Hsun Tu

In x86 32bit /usr/src/linux-2.6.x/arch/x86/include/asm/unistd_32.h

The index must be the last in the list e.g. #define __NR_myservice 338

Write Your System Call (Cont) - x86_32

Page 46: OS Project 0 & 1

/5546National Taiwan University

Tang-Hsun Tu

Create an entry (function name) in system call table /usr/src/linux-2.6.x/arch/x86/kernel/syscall_table_32.S .long sys_myservice

Write Your System Call (Cont) - x86_32

Page 47: OS Project 0 & 1

/5547National Taiwan University

Tang-Hsun Tu

In x86 64bit /usr/src/linux-2.6.x/arch/x86/include/asm/unistd_64.h

The index must be the last in the list #define __NR_myservice 300 __SYSCALL(__NR_myservice, sys_myservice)

Write Your System Call (Cont) - x86_64

Page 48: OS Project 0 & 1

/5548National Taiwan University

Tang-Hsun Tu

Define the prototype /usr/src/linux-2.6.x/include/linux/syscalls.h #define asmlinkage int sys_myservice(int arg1);

Write Your System Call (Cont)

Page 49: OS Project 0 & 1

/5549National Taiwan University

Tang-Hsun Tu

Add to makefile to compile vim /usr/linux-2.6.x/kernel/Makefile obj-y += myservice.o

Now, you can recompile your kernel

Write Your System Call (Cont)

Page 50: OS Project 0 & 1

/5550National 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 300

int main(int argc,char *argv[]){ printf("%d\n", syscall(__NR_myservice, atoi(argv[1]))); return 0;}

01020304050607080910111213

Page 51: OS Project 0 & 1

/5551National Taiwan University

Tang-Hsun Tu

Since we want measure the number of sent signals and called sys_kill(), we might need to add a counter to system call “kill()” in /usr/src/Linux-2.6.x/kernel/signal.c. e.g. int nr_kills;

If you want to use this variable in your system call or kernel module, you have to export it. EXPORT_SYMBOL(nr_kills);

printk can print messages in kernel, use dmesg to check.

Hints

Page 52: OS Project 0 & 1

/5552National Taiwan University

Tang-Hsun Tu

Results

Hints (Cont)

1.

2.

3.

Page 53: OS Project 0 & 1

/5553National Taiwan University

Tang-Hsun Tu

LXR, http://rswiki.csie.org/lxr/http/source

Some Useful Tool

Page 54: OS Project 0 & 1

/5554National Taiwan University

Tang-Hsun Tu

Manpages apt-get install manpages-dev

Some Useful Tool (Cont)

Page 55: OS Project 0 & 1

/5555National Taiwan University

Tang-Hsun Tu

Kernel Website http://www.kernel.org

LXR http://rswiki.csie.org/lxr/http/source http://lxr.linux.no/linux/

Google http://www.google.com/

Reference

Page 56: OS Project 0 & 1

/5556National Taiwan University

Tang-Hsun Tu

Q & A


Recommended