Embedded Systems

Prof. Myung-Eui Lee (A-405) [email protected]

Embedded Systems 1-1 KUT Linux Structure

l Linux Structure

proc1 proc2 proc3 proc4 proc5 procn User Space

System Call Interface Filesystem Manager Manager Ext2fs proc devfs Process Management Memory Manager Scheduler minix nfs msdos Memory Management Signaling

Buffer Cache Kernel Space

Device Manager Network Manager char block Ipv6 ethernet Console KBD SCSI CD-ROM PCI network …..

Device Interface

dev1 dev2 dev3 dev4 devn

Embedded Systems 1-2 KUT Linux Structure

l Linux Structure

Applications: Graphics UI, Compilers, Media player UI Text UI = Shell: sh, csh, ksh, bash, tcsh, zsh Compiler libraries (libc.a) API System Shared Libraries (System Call Interface)

Memory File Process management management management Kernel Device Drives BIOS Computer Hardware

Embedded Systems 1-3 KUT System Call Interface

Standard C Lib. System Call C program invoking printf() library call, API – System call – OS Relationship which calls write() system call ar –t /usr/lib/libc.a | grep printf /usr/src/linux-2.4/arch/i386/kernel/entry.S

Embedded Systems 1-4 KUT System Call

l System calls define the programmer interface to Linux system » Interface between user-level processes and hardware devices u CPU, memory, disks etc. » Make programming easier u Let kernel take care of hardware-specific issues » Increase system security u Let kernel check requested service via system call » Provide portability u Maintain interface but change functional implementation l Roughly five categories of system calls in Linux » Process control » File management * 288 system calls » Device management refer to slide #6 » Information maintenance » Communication

Embedded Systems 1-5 KUT System Call

l Invoked by executing int or swi instruction » CPU switches to kernel mode & executes a kernel function x86 : movl %eax, 2 ; system call number int $0x80 ARM : swi 2 ; system call number l Linux files relating to system call » /usr/src/linux-2.4/arch/i386/kernel/entry.S x86 host #408 /pxa270/kernel/linux-2.6…/arch/arm/kernel/calls.S ARM target #17

u System call and low-level fault handling routines

u ENTRY(sys_call_table) » /usr/src/linux-2.4/include/asm-i386/unistd.h x86 host /pxa270/kernel/linux-2.6…/include/asm-arm/unistd.h ARM target

u System call numbers and macros

Embedded Systems 1-6 KUT ARM System Call

l ARM System Call Example An example to write a short file on disk

SWI_Exit EQU 0x01 SWI_Write EQU 0x04 SWI_Open EQU 0x05 SWI_Close EQU 0x06 write_only EQU 4 ; mode 4 = open to write

ENTRY ; mark first instruction to execute start ADR r0, filename ; r0 points to string MOV r1, #write_only ; file attribute for write only SWI SWI_Open ; open a file for writing MOV r5, r0 ; save file-handle in r5 ADR r1, string ; point to a string MOV r2, #14 ; 14 characters long SWI SWI_Write ; write to file MOV r0, r5 ; restore file-handle SWI SWI_Close ; close the file SWI SWI_Exit

filename = "test.txt",0 string = "Hello World!",&0a,&0d END

Embedded Systems 1-7 KUT System Call

l System Call Handling

kernel user task ENTRY(system_call) /* arch/i386/kernel/entry.S */ main() SAVE_ALL { . …. … fork() idt_table call *SYMBOL_NAME(sys_call_table)(,%eax,2) } 0x0 divide_error() …. debug() ret_from_sys_call (schedule, signal, bh_active, nmi() nested interrupt handling) libc.a

. sys_call_table … …. fork() { …. 1 sys_exit() movl %eax, 2 0x80 int $0x80 system_call() 2 sys_fork() sys_fork() …. …. sys_read () } 3 /* arch/i386/kernel/process.c */ . … 4 sys_write () /* kernel/fork.c */ ….

Embedded Systems 1-8 KUT Process

l Program » Structured set of commands stored in an executable file on a file system » Executed to create a process l Process » Program running in memory and on the CPU (active program) » Modern systems allow ‘multiprogramming’ (i.e., several processes exist concurrently) » Each process requires an allocation of cpu time, in order to make forward progress » The OS must do ‘scheduling’ of cpu time » Each process also needs the use of other system facilities (memory, files, devices) » The terms job (batch system) and process is almost used interchangeably

Embedded Systems 1-9 KUT Process

l A process includes Process in memory » text section : program » stack » data section » heap l User process » Process begun by a user that runs on a terminal (tty) l process » System process » Not associated with a terminal l Process ID (PID) top, ps -el » Unique identifier assigned to every process as it begins » Processes are identified by their process identifier, an integer

Embedded Systems 1-10 KUT Process

l Child processes » Refers to a process that was started by another process (parent process) l Parent processes » Process that has started other processes (child processes) l Parent Process ID (PPID) ps -el » The PID of the parent process that created the current process l Processes communicate via pipes (IPC) » Queues of bytes between two processes that are accessed by a file descriptor

Embedded Systems 1-11 KUT Process

l Process System Call » Creation : fork system call creates new process

u the process that calls fork() is parent, the new process is child

u fork() is calling sys_fork() → do_fork() in /kernel/fork.c

u “cloning flags” : man clone /usr/src/linux-2.4/include/linux/sched.h #39 » Execution : exec system call is used after a fork to execute the process » : exit terminates process and free all resources

u Calling exit() → sys_exit() → do_exit () explicit kernel/exit.c

u The child is turned into a until the parent makes the call to Refer to → When a process finishes execution, it will have an exit status to Slide #26 report to its parent process. Because of this last little bit of information, the process will remain in the operating system’s process table as a zombie process. indicating that it is not to be scheduled for further execution, but that it cannot be completely removed (and its process ID cannot be reused) » Wait : A parent may wait for a to terminate, and return its termination status

Embedded Systems 1-12 KUT Process

l Process termination » Voluntary termination 1. Returning to the main() function (implicit) 2. Calling exit() (explicit) » Involuntary termination 1. An exception might be raised during its kernel mode execution 2. The process might have received the SIGABRT or other termination signal » Termination process state 1. the parent is alive : zombie process 2. the parent is dead : orphan process An orphan process is a process that is still executing, they do not become zombie processes; instead, they are adopted(inherited) by init process (PID 1) l Task » Another name for a process is a task, and Linux kernel internally refers to processes as tasks » Generally refer to a process from the kernel’s point of view

Embedded Systems 1-13 KUT Task

» As seen by a process running under Linux Process and Tasks u The kernel is a provider of services

u Individual processes exist independently alongside each other and cannot affect each other directly

u Each process’s own area of memory is protected against modification by other processes » The internal viewpoint of a running Linux

u Only one program (OS) is running on the computer, can access all the resource

u The various tasks are carried out by co-routines

u An error in the kernel programming can block the entire system

Embedded Systems 1-14 KUT Process Control Block (PCB)

l Information associated with each process l type sturcture task_struct ; line 382 (target:528) » /usr/src/linux-2.4/include/linux/sched.h Process state Unique process identifier Memory-management information File system information Signal information Program counter CPU registers CPU scheduling information (e.g., priority) Accounting information I/O status information Pointers to other control blocks

Embedded Systems 1-15 KUT Process Control Block (PCB)

l task_struct

prev_task next_task pid gid task-name

process task-state task-priority execution aspects processor register-values (EIP, ESP, EFLAGS, etc)

memory-map

open files resource ownership terminal aspects timers

signal-handlers

Embedded Systems 1-16 KUT Process Control Block

l task_struct

struct task_struct { volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ unsigned long flags; /* per process flags */ mm_segment_t addr_limit; /* thread address space: 0-0xBFFFFFFF for user-thead 0-0xFFFFFFFF for kernel-thread */ mm_struct struct exec_domain *exec_domain; task_struct long need_resched; pid = 5 long counter; long priority; mm /* SMP and runqueue state */ slg signal_struct struct task_struct *next_task, *prev_task; struct task_struct *next_run, *prev_run; files ... /* task state */ /* limits */ files_struct /* file system info */ /* ipc stuff */ /* tss for this task */ … /* open file information */ /* memory management info */ /* signal handlers */ ... };

Embedded Systems 1-17 KUT Thread

l Thread of execution = Thread l The object of activity within the process l Each thread includes a unique program counter, process stack, and set of processor registers l The kernel schedules individual threads, not process l Linux does not differentiate between threads and processes : a thread is just a special kind of process l A process has two distinct aspects » Its ‘execution’ (forward progression of states) » Its ‘ownership’ (share of system’s resources) » The word ‘thread’ refers to the execution aspect of a process (i.e., the entity which gets ‘scheduled’ by an Operating System for a share of the available cpu time)

Embedded Systems 1-18 KUT Thread

l Multi-threading » It is possible for an operating system to support a concept of ‘process’ in which more than one execution- thread exists (with process-resources being shared)

one process with one process with one thread multiple threads l Advantages of ‘threads’ » All the ‘threads’ that belong to a process can access the same memory, the same files, the same terminal, the same timers and the same signal-handlers » Thus the system ‘overhead’ involved in managing a multithreaded task is more efficient (less time- consuming to set up and keep track of) than if each of those threads had individualized ownerships

Embedded Systems 1-19 KUT Thread

l Thread communication » Since the threads in a process share the same memory, no special mechanism is needed for them to communicate data

process

All these threads can read or write the same memory-locations

Embedded Systems 1-20 KUT User/Kernel Mode Transition

» 1. P1 in User Mode issues a Transitions between User system call and Kernel Mode » 2. The process switches to

Kernel Mode, and the system P1.a P1.b P2.a P2.b call is serviced » 3. P1 then resumes execution in User Mode until a timer interrupt occurs » 4. The scheduler is activated in Kernel Mode » 5. P2 starts its execution in User Mode until a hardware device raises an interrupt » 6. As a consequence of the interrupt, P2 switches to Kernel Mode and services the P1.a P1.b P2.a P2.b Request system Wait for timer Wait for device Device H/W interrupt call service interrupt H/W interrupt interrupt service System call handler Scheduler Interrupt handler

Embedded Systems 1-21 KUT Memory Space

l Virtual Memory = User + Kernel

kernel stack 0xFFFFFFFF kernel 0xC0000000 kernel bss 0xBFFFFFFF stack kernel data

kernel text kernel space 1-GB User bss stack stack data text mappings text data user space 3-GB 0x00000000 data text

physical memory virtual memory

Embedded Systems 1-22 KUT Process State

l TASK_RUNNING : 1. running / 2. ready » The process is runnable

u 1. currently running - running

u 2. on a run queue waiting to run - ready l TASK_INTERRUPTIBLE : waiting/sleeping/blocked » Waiting on a condition: interrupts, or signals » Becomes runnable if it receives a signal l TASK_UNINTERRUPTIBLE » Waiting process cannot be woken by a signal » Cannot become runnable if it receives a signal » This is used in situations where the process must wait without interruption l TASK_STOPPED : temporarily stopped » Stopped process - e.g., by a debugger » This occurs if the process receives SIGSTOP signal

Embedded Systems 1-23 KUT Process State

l TASK_ZOMBIE : zombie, defunct, immortal process » Process finished, but parent has not released PID » Process has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process that started the (now zombie) process to read its exit status » When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. The parent can read the child's exit status by executing the wait system call, at which stage the zombie is removed » The process has terminated, but its parent has not yet issued a wait system call » wait provides the process id of a terminated child so that the parent can tell which child terminated

Embedded Systems 1-24 KUT Process State

l 2.4 /usr/src/linux-2.4/include/linux/sched.h Line # 106 » TASK_RUNNING » TASK_INTERRUPTIBLE » TASK_UNINTERRUPTIBLE » TASK_STOPPED » TASK_ZOMBIE » TASK_DEAD : currently not used l 2.6 /pxa270/kernel/ linux-2.6.11-h270-tku_v1.1 /include/linux/sched.h » TASK_RUNNING » TASK_INTERRUPTIBLE » TASK_UNINTERRUPTIBLE » TASK_STOPPED » TASK_ZOMBIE » TASK_DEAD : currently not used » TASK_TRACED : being traced by a debugger

Embedded Systems 1-25 KUT Diagram of Process State

Embedded Systems 1-26 KUT Process

l Context Switch » When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process » Context-switch time is overhead; the system does no useful work while switching » Time dependent on hardware support

l Remote Procedure Call » Remote procedure call (RPC) abstracts procedure calls between processes on networked systems

Embedded Systems 1-27 KUT Process

l Viewing Processes » ps –f command : user process

u User identifier (UID), PID, PPID, start time, CPU utilization » ps –l command : user process

u More complete information

u Process state can be seen

u R = running, S = sleeping, D = uninterruptible sleep, T = stopped or being traced, Z = zombie » top or ps –efl command : system/user process

u Process priority (PRI) : priority → Higher value means lower priority → 0 (high priority) to 139 (low priority)

u Nice value (NI) : time slice → Indirectly represents priority → The priority value may be in the range -20 (800ms) to 19(5ms). → Higher value means lower priority

u NICE_TO_PRIO(nice), PRIO_TO_NICE(prio) : kernel/sched.c

Embedded Systems 1-28 KUT InterProcess Communication(IPC)

l Mechanism for processes to communicate and to synchronize their actions l Message system – processes communicate with each other without resorting to shared variables l IPC facility provides two operations: » send(message) – message size fixed or variable » receive(message) l If P and Q wish to communicate, they need to: » Establish a communication link between them » Exchange messages via send/receive l Implementation of communication link » Physical : shared memory, hardware bus » Logical : logical properties(protocol, layer)

Embedded Systems 1-29 KUT Communications Model

l Direct Communication » Processes must name each other explicitly: » send (P, message) – send a message to process P » receive(Q, message) – receive a message from process Q l Indirect Communication » Messages are directed and received from mailboxes (also referred to as ports) » Each mailbox has a unique id » Processes can communicate only if they share a mailbox » Operations

u create a new mailbox

u send and receive messages through mailbox

u destroy a mailbox » send(A, message) – send a message to mailbox A » receive(A, message) – receive a message from mailbox A

Embedded Systems 1-30 KUT Signal

l Introduced in UNIX systems to simplify IPC l Used by the kernel to notify processes of system events l A signal is a short message sent to a process, or group of processes, containing the number identifying the signal l Linux supports 31 non-RT signals » /include/asm-i386/signal.h l POSIX standard defines a range of values for RT signals

Embedded Systems 1-31 KUT Signal

l A signal is sent due to occurrence of corresponding event (kernel/signal.c – line 1110) » send_sig_info(int sig, struct siginfo *info, struct task_struct *t); » sig is signal number » info is either:

u 0, if user mode process is signal sender

u 1, if kernel is signal sender

# Signal Name Default Action Comment 1 SIGHUP Abort Hangup terminal or process 2 SIGINT Abort Keyboard interrupt (usually Ctrl-C) … 9 SIGKILL Abort Forced process termination 10 SIGUSR1 Abort Process specific 11 SIGSEGV Dump Invalid memory reference …

Embedded Systems 1-32 KUT Signal

l Sending a signal » Kernel sends (delivers) a signal to a destination process to update some state in the context of the destination process l Kernel sends a signal for one of the following reasons: » Generated internally:

u Kernel has detected a system event such as divide-by-zero (SIGFPE) or the termination of a child process (SIGCHLD) » Generated externally:

u Another process has invoked the kill system call to explicitly request the kernel to send a signal to the destination process

Embedded Systems 1-33 KUT Signal

l Receiving a signal » A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal l Three possible ways to react: » Ignore the signal (do nothing) » Terminate the process » Catch the signal by executing a user-level function call(a signal handler)

u Similar to a hardware exception handler being called in response to an asynchronous interrupt

Embedded Systems 1-34 KUT Scheduler

l Scheduling » Non-preemptive scheduling: the process keeps the CPU until the process terminates or it switches to waiting state (simple to implement) » Preemptive scheduling: the process can be interrupted and must release the CPU l Scheduling Policies : /include/linux/sched.h Line # 136 » SCHED_NORMAL : non-RT / classic Unix

u Interactive : make the system appear fast to user

u RT have higher priorities than any non-RT tasks » SCHED_FIFO : soft-RT

u If no other higher-priority RT process is runnable, the process will continue to use the CPU as long as it wishes » SCHED_RR : soft-RT

u Assign CPU time to all SCHED_RR processes that have the same priority (fixed time slice)

u Interrupted when its time slice has expired * Scheduler schedule() function : /kernel/sched.c - line 986

Embedded Systems 1-35 KUT Scheduler

l Long-term scheduler (or job scheduler) » Selects which processes should be brought into the ready queue » Invoked very infrequently (seconds, minutes) Þ (may be slow) l Short-term scheduler (or CPU scheduler) » Selects which process should be executed next and allocates CPU » Invoked very frequently (milliseconds) Þ (must be fast) l Processes can be described as either: » I/O-bound process – spends more time doing I/O than computations, many short CPU bursts » CPU-bound process – spends more time doing computations, few very long CPU bursts Linux implicitly favors I/O-bound processes over CPU-bound ones

Embedded Systems 1-36 KUT Module

l Linux is a monolithic kernel » Trivial modifications require kernel to be recompiled » Kernel is increasing in size by adding new features » Many modules occupy permanent space in memory though they are used rarely l Module: steps toward micro-kernelized Linux » Small and compact kernel » Clean kernel » Rapid kernel » Components-based Linux l Easier way to extend Linux kernel functions l Modules allow to plug a new functionality into the kernel at runtime » A module is loaded only when it is needed » It is unloaded when it is not needed anymore

Embedded Systems 1-37 KUT Module

l LKM (Linux Kernel Module) = Kernel Module = Module l Linux Kernel = Base Kernel (Kernel Image) + LKM l Kernel does not need to be rebuilt so often » Reduce the likeliness of errors l Bugs in code do not affect the kernel boot » Easier debugging: you know where to look » Faster debugging: no need to reboot l Modules can be compiled and dynamically linked into kernel address space » Useful for device drivers that need not always be resident until needed.

u Keeps core kernel “footprint” small

Embedded Systems 1-38 KUT Module versus Application

l Modules l Applications » 1. runs in kernel space » 1. runs in user space (no main function) (main function) » 2. just registers itself in » 2. performs a single task order to serve future from beginning to end requests, and its (not all applications are event- initialization function driven) terminates immediately » 3. can call functions it (event-driven program) doesn’t define: the linking » 3. no libraries to link to stage resolves external (linked only to the kernel): references using the the only functions it can call appropriate library of are the ones exported by the functions (libc)

kernel u printf() u printk() : kernel function

Embedded Systems 1-39 KUT Module

l Every module consist of two basic functions (minimum) int init_module(void) /*used for all initialization*/ { ... } void cleanup_module(void) /*used for exit */ { ... } l Loading a module - by issuing the following command: » insmod module.o l Removing a module - by issuing the following command: » rmmod module

Embedded Systems 1-40 KUT Module

» Basic two Interfaces kernel u module_init()

u module_exit() module register init_module() insmod system call module_init()

rmmod cleanup_module() module_exit() unregister system call

» Function units which can be implemented as modules u File system : register_filesystem(), unregister_filesystem(), read_super function (mount), put_super function (unmount)

u Block device drivers : register_blkdev(), unregister_blkdev(), open function, release function

u Character device drivers : register_chrdev(), unregister_chrdev(), open function, release function

u Network device drivers : register_netdev(), unregister_netdev(), open function, close function

Embedded Systems 1-41 KUT Module

l Instruction » insmod : /proc/modules

u Insert an module into the kernel » rmmod

u Remove an module from the kernel » depmod : /lib/modules/2.4.20-8/modules.dep

u Determine interdependencies between modules » ksyms : /proc/ksyms

u Display symbols that are exported by the kernel » lsmod = /proc/modules

u List currently loaded modules » modinfo

u Display module information (.modinfo section in module object file) » modprobe : /etc/modules.conf (alias & options)

u Insert module and resolve module dependency (as described by modules.dep). For example, if you must load A before loading B, modprobe will automatically load A when you tell it to load B

Embedded Systems 1-42 KUT Module

l modinfo hello.o » MODULE_AUTHOR(“MELEE”); » MODULE_DESCRIPTION(“Hello Module”); » MODULE_LICENCE(“GPL”); l /proc/ksyms : Kernel Symbol Table » lists all the symbols the kernel exports » /usr/src/linux/System.map (core symbols) » /proc/ksyms (all symbols) l /proc/modules » holds information about the loaded modules » column : module name / size / use count / dependency l /etc/modules.conf » modprobe command » A module name : eth0, usb-controller » A more generic identifier : e100, ehci-hcd

Embedded Systems 1-43 KUT Module

l kernel 2.4 : host

/* HELLO MODULE example -Tested on Linux Kernel 2.4 */ ## Makefile for Linux Kernel Module for 2.4 #include #include CC = gcc #include KERNEL_PATH = /usr/src/linux-2.4.20-8 MODULE_LICENSE("GPL"); CFLAGS = -DMODULE -D__KERNEL__ -I$(KERNEL_PATH)/include MODULE_AUTHOR(“MELEE”); MODULE_DESCRIPTION(“HELLO MODULE”); MOD_OBJ = hello

int init_module(void) all: $(MOD_OBJ).o { printk("HELLO MODULE is loaded. \n"); $(MOD_OBJ): $(MOD_OBJ).c $(CC) $(CFLAGS) -c $(MOD_OBJ).c return 0; } clean: rm -f *.o void cleanup_module(void) { printk("HELLO MODULE is unloaded. \n"); } -D__KERNEL__ = #define __KERNEL__ -DMODULE = #define MODULE

Embedded Systems 1-44 KUT Module

l kernel 2.6 : target

/* HELLO MODULE example - Tested on Linux Kernel 2.6.x */

#include ## Makefile for Linux Kernel Module for 2.6 #include #include obj-m := hello.o MODULE_LICENSE("GPL"); CC := /opt/iwmmxt-1.0.0/bin/arm-linux-gcc

KDIR := /pxa270/kernel/linux-2.6.11-h270-tku_v1.1 int hello_init(void) { PWD := $(shell pwd) printk(" HELLO MODULE is loaded. \n"); default: return 0; $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules } clean: void hello_exit(void) rm -rf *.ko { rm -rf *.mod.* printk(" HELLO MODULE is unloaded. \n"); rm -rf .*.cmd } rm -rf .tmp* rm -rf *.o module_init(hello_init); module_exit(hello_exit);

Embedded Systems 1-45 KUT Module

l Make module : sample modules (microcom homepage) l Load the module » insmod hello.o or hello.ko » dmesg : at host (2.4) or /var/log/messages l Check that the module is loaded » lsmod = cat /proc/modules l Remove the module » rmmod hello » dmesg : at host (2.4)

Embedded Systems 1-46 KUT