40
1 Linux Kernel Internals - By Ganesh Naik Director – Levana Technologies Embedded Android & Linux Consultant and Trainer www.levanatech.com

Ganesh naik linux_kernel_internals

Embed Size (px)

Citation preview

Page 1: Ganesh naik linux_kernel_internals

1

Linux Kernel Internals

- By Ganesh NaikDirector – Levana Technologies Embedded Android & Linux Consultant and Trainerwww.levanatech.com

Page 2: Ganesh naik linux_kernel_internals

2

About

Author of book : “Learning Linux Shell Scripting”, plublished by Packt Publication

Website : https://www.packtpub.com/networking-and-servers/learning-linux-shell-scripting

Page 3: Ganesh naik linux_kernel_internals

3

• Compiling & testing hello World

– Preprocessing

– Compilation

– Assembling

– Linking

Understanding Hello World Program

Page 4: Ganesh naik linux_kernel_internals

4

Discussion

Motor

Motor Application

Start_motor()

Stop_motor()

Hardware

Kernel

User

space

Page 5: Ganesh naik linux_kernel_internals

55

• Why do we need device drivers ?

• Conceptual understanding of real world character device driver

• User Space, Kernel Space & Hardware interaction

Conceptual Understanding of Device Driver

Page 6: Ganesh naik linux_kernel_internals

66

Application and device driver

Page 7: Ganesh naik linux_kernel_internals

7

Memory-mapped device registers and I/O space ports

Memory

Register

Device

Register

I/O Space

Register

CPULOAD/STORE IN/OUT

Page 8: Ganesh naik linux_kernel_internals

8

• Device Registers: command, status and data

buffer

• Accessing device Registers: (i) address of the

device’s first register (ii) address space where

these registers live

• I/O Space Registers:

(i) inb() Read a single value from I/O Port

(ii) outb() Write a single value to I/O Port

• Memory-mapped registers:

(i) readb() Read a single value from I/O register

(ii) writeb() Write a single value to I/O register

Memory-mapped device registers and I/O space ports

Page 9: Ganesh naik linux_kernel_internals

99

There are three types of device driver

Char (c)

Block (b)

Network

Linux Treats every device as a file and to interface the driver

uses device nodes found in /dev historically

Network device driver unlike char, and block uses a different

approach for the same

CHAR DRIVERS

Page 10: Ganesh naik linux_kernel_internals

1010

Kernel architecture

System call interface

Processmanagement

Memorymanagement

Filesystemsupport

Devicecontrol Networking

CPU supportcode

Filesystemtypes

Storagedrivers

Characterdevice drivers

Networkdevice drivers

CPU / MMUsupport code

C library

App1 App2 ... Userspace

Kernelspace

Hardware

CPU RAM Storage

Page 11: Ganesh naik linux_kernel_internals

1111

Linux Kernel Generic Structure

System Call Interface

ProcessManagement

Memory Management

Device

ControlFile

Management

NetworkManagement

CPUMMU

File Systems

Block

Devices

(Disks,CDs)

Character

Devices

(Keyboard,

Monitor)

Network

Subsystem

Network

Card

Concurrency VM, paging Handling Files tty access N/W

Connectivity

Featu

res

Page 12: Ganesh naik linux_kernel_internals

12

Memory Management - Introduction

Memory management: one of the most important kernel subsystems

Virtual Memory: Programmer need not to worry about the size of RAM (Large address space)

static allocation: internal Fragmentation

Dynamic allocation: External Fragmentation

Avoid Fragmentation: Thrashing -overhead

Page 13: Ganesh naik linux_kernel_internals

13

Memory mapping

executable image spilt into equal sized small parts(normally a page size)

virtual memory assigns virtual address to a each part linking of an executable image into a process virtual

memory

Swapping swap space is in hard disk partition if a page is waiting for certain event to occur, swap it. use physical memory space efficiently if there is no space in Physical memory, swap LRU pages

into swap space

Page 14: Ganesh naik linux_kernel_internals

14

Demand Paging

Don't load all the pages of a process into memory

Load only necessary pages initially

if a required page is not found, generate page fault

page fault handler brings the corresponding page into

memory.

Page 15: Ganesh naik linux_kernel_internals

15

A tour to a program execution

Execute

./a.out

Memory

Mapping

Demand

paging

LoadPage

TableVPFN

VPFN

P

R

O

C

C

E

S

S

O

R

execute

Virtual

Memory Page

Table

Page Fault

Swap

Space

Hard

DiscExit

Page 16: Ganesh naik linux_kernel_internals

16

• Regular

• Directory

• Symbolic Link – Hard Link, Soft Link

• Pseudo - /proc

• Special files – Device drivers

• Pipe - FIFO

• Socket

Everything in Linux is a file

Page 17: Ganesh naik linux_kernel_internals

17

VFS

Page 18: Ganesh naik linux_kernel_internals

18

When you create a file system, Linux creates a number of blocks on that

device.

– Boot Block

– Super-block

– I-node table

– Data Blocks

• Linux also creates an entry for the “/” (root) directory in the I-node table, and allocates

data block to store the contents of the “/” directory.

File Systems - Creating

B S inode table Data blocks

Page 19: Ganesh naik linux_kernel_internals

19

• The super-block contains info. such as:

– a bitmap of blocks on the device, each bit specifies whether a block is

free or in use.

– the size of a data block

– the count of entries in the I-node table

– the date and time when the file system was last checked

– the date and time when the file system was last backed up

– Each device also contains more than one copy of the super-block.

– Linux maintains multiple copies of super-block, as the super-block

contains information that must be available to use the device.

– If the original super-block is corrupted, an alternate super-block can be

used to mount the file system.

File Systems - Superblock

Page 20: Ganesh naik linux_kernel_internals

20

• The I-node table contains an entry for each file stored in the file system. The

total number of I-nodes in a file system determine the number of files that a

file system can contain.

• When a file system is created, the I-node for the root directory of the file

system is automatically created.

• Each I-node entry describes one file.

File Systems – I-node table

Page 21: Ganesh naik linux_kernel_internals

21

• Each I-node contains following info:

– file owner UID and GID

– file type and access permissions

– date/time the file was created, last modified, last accessed

– the size of the file

– the number of hard links to the file

– Each I-node entry can track a very large file

File Systems – I-node table

Page 22: Ganesh naik linux_kernel_internals

22

ext File System I-node

I-node StructureI-node No

……….

Block -1 Address

Block -N Address

……….

Single Indirect

Block Address

Double Indirect

Block Address

Triple Indirect

Block Address

N direct

Block Address

N single indirect

Block Address

N double indirect

Block Address

Page 23: Ganesh naik linux_kernel_internals

23

• A device special file describes following characteristics of a device

– Device name

– Device type (block device or character device)

– Major device number (for example ‘2” for floppy, “3” for hard-disk )

– Minor device number (such as “1” for “hda1”)

• Switch Table - Linux kernel maintains a set of tables using the major

device numbers.

• The switch table is used to find out which device driver should be

invoked

• For example : fd –> file table –> inode table –> switch table –>

device drivers

Device Special Files

Page 24: Ganesh naik linux_kernel_internals

Gnu compiler

• A C source code should have a .c extension.

• The command used to compile a C source code isgcc [options] <filename>.c

• When a C source code is compiled, the compiler generates

the executable.

• By default, a.out ( the name of the executable) is generated,

if no options are given to the compiler.

Page 25: Ganesh naik linux_kernel_internals

• size command

$ size sample

text data bss dec hex filename

968 260 12 1240 4d8 sample

text executable code in bytes (in decimal format)

data initialized data in bytes (in decimal format)

bss uninitialized data in bytes (in decimal format)

Total size of text, data, bss in

decimal & hex notation

Page 26: Ganesh naik linux_kernel_internals

• readelf

• readelf is useful to inspect an executable in a detailed way.

• Use –d option to get the list of dynamic libraries an executable depends

on

$ readelf -d sample

Dynamic segment at offset 0x4f4 contains 20 entries:

Tag Type Name/Value

0x00000001 (NEEDED) Shared library: [libc.so.6]

$ readelf -d /bin/ls

Dynamic segment at offset 0x104d4 contains 22 entries:

Tag Type Name/Value

0x00000001 (NEEDED) Shared library: [libacl.so.1]

0x00000001 (NEEDED) Shared library: [libtermcap.so.2]

0x00000001 (NEEDED) Shared library: [libc.so.6]

Page 27: Ganesh naik linux_kernel_internals

Memory layout of a C program

Stack

Heap

Uninitialized data( bss)

Initialized data

Text

High address

Low address

Command line args & environment variables

Gets initialized to 0 at runtime

Initialized global data

Program code

Page 28: Ganesh naik linux_kernel_internals

28

Parent process creates children processes, which, in turn create other processes,

forming a tree of processes.

Resource sharing

– Parent and children share all resources.

– Children share subset of parent’s resources.

– Parent and child share no resources.

Execution

– Parent and children execute concurrently.

– Parent waits until children terminate.

Address space

– Child duplicate of parent.

– Child has a program loaded into it.

Process Creation

Page 29: Ganesh naik linux_kernel_internals

29

pid_t fork (void); creates a new process

All statements after the fork() system call in a program are executed by two

processes - the original process that used fork(), plus the new process that

is created by fork( )

main ( ) {

printf (“ Hello fork %d\n, fork ( ) ”);

}

– Hello fork: 0

– Hello fork: x ( > 0);

– Hello fork: -1

fork ( )

Page 30: Ganesh naik linux_kernel_internals

30

if (!fork) {

/* Child Code */

}

else {

/* parent code */

wait (0); /* or */

waitpid(pid, ….);

}

Parent and child

Page 31: Ganesh naik linux_kernel_internals

31

To run a new program in a process, you use one of the “exec” family of calls

(such as “execl”) and specify following:

– the pathname of the program to run

– the name of the program

– each parameter to the program

– (char *)0 or NULL as the last parameter to specify end of parameter list

execl

Page 32: Ganesh naik linux_kernel_internals

32

int execl (const char *path, const char *arg, …..);

int execlp (const char *file, const char *arg);

int execle (const char *path, const char *arg, ……., char *const envp[ ]);

int execv (const char *path, char *const argv[ ]);

int execvp (const char *file, char *const argv[ ]);

All the above library functions call internally execve system call.

int execve (const char *filename, char *const argv [ ] , char *const evnp [ ]);

exec family

Page 33: Ganesh naik linux_kernel_internals

33

An executable image

Stack

Heap

Uninitialised data

Initialized read-write data

Initialized Read-only data

Text

$ size a.out (man size )

text data bss dec hex filename920 268 24 1212 4bc a.out

Page 34: Ganesh naik linux_kernel_internals

34

Very flexible and ease of use.

Fastest IPC mechanisms

shared memory is used to provide access to

– Global variable

– Shared libraries

– Word processors

– Multi-player gaming environment

– Http daemons

– Other programs written in languages like Perl, C etc.,

Shared Memory - Introduction

Page 35: Ganesh naik linux_kernel_internals

35

Shared memory is a much faster method of communication than either semaphores or message queues.

does not require an intermediate kernel buffer

Using shared memory is quite easy. After a shared memory segment is set up, it is manipulated exactly like any other memory area.

Why go for Shared Memory

Page 36: Ganesh naik linux_kernel_internals

36

The steps involved are

– Creating shared memory

– Connecting to the memory & obtaining a pointer to the memory

– Reading/Writing & changing access mode to the memory

– Detaching from memory

– Deleting the shared segment

Steps to access Shared Memory

Page 37: Ganesh naik linux_kernel_internals

37

used to attach the created shared memory segment onto a process

address space.

void *shmat(int shmid,void *shmaddr,int shmflg)

Example: data=shmat(shmid,(void *)0,0);

A pointer is returned on the successful execution of the system call

and the process can read or write to the segment using the pointer.

shmat

Page 38: Ganesh naik linux_kernel_internals

38

Reading or writing to a shared memory is the easiest part.

The data is written on to the shared memory as we do it with normal

memory using the pointers

Eg. Read:

printf(“SHM contents : %s \n”, data);

Write:

prinf(“”Enter a String : ”);

scanf(“ %[^\n]”,data);

Reading/ writing to SM

Page 39: Ganesh naik linux_kernel_internals

39

The detachment of an attached shared memory segment is done by shmdt

to pass the address of the pointer as an argument.

Syntax: int shmdt(void *shmaddr);

To remove shared memory call:

int shmctl(shmid,IPC_RMID,NULL);

These functions return –1 on error and 0 on successful execution.

Shmdt & shmctl

Page 40: Ganesh naik linux_kernel_internals

Thank You

Ganesh [email protected]