You are on page 1of 18

Linux

Kernel Organization
Linux is a monolithic kernel
Processes and resource management,
memory management, file management all
implemented in a single executable
software module
Data structures for each aspect of kernel
accessible by all.

Kernel Extensions
Two types of extensions to the kernel
possible:
device drivers and interrupt handlers. These
execute in kernel mode.
modules, a independent software unit. Can be
installed dynamically as the system runs.

Modules
Interface between module and kernel more
general that interface between device driver
and kernel
Modules can be used to implement device
drivers.
Project 4 has the object of writing a module.

Modules
Modules
ModuleInterface
Interface
Module

DeviceDriver
DriverInterface
Interface
Device

Device
DeviceDrivers
Drivers

Linux
LinuxKernel
Kernel

Interrupts
Kernel reacts to requests from any active,
external entity.
Interrupt. An electronic signal produced by
an external component.
fielded by the CPU hardware
CPU begins executing different process to deal
with the interrupt
often result of I/O completion or clock.

Interrupts
Fetch-decode-execute cycle
InterruptRequest = FALSE;

while (haltFlag not set during execution){


IR = memory[PC];
PC++;
execute(IR);
if (InterruptRequest) {
save_PC( );
restore_PC(IRQ); /* branch to the ISR for this IRQ*/
}
}

Interrupts
Problem: interrupt can be interrupted.
if ISR is saving the state of a process and is
interrupted, may not complete
when second ISR is finished, first ISR will not
restore state correctly.
called a race condition
if A occurs before B, system changes to one state
if B occurs before A, system changes to different state.

Interrupts
Solution: disable interrupts
kernel function cli( ) sets interrupt-enable flag to
FALSE.
kernel function sti( ) sets interrupt-enable flag to
TRUE.

Kernel Services
User programs view kernel as an ADT.
interface to the ADT is the system call
interface
Linux conforms to POSIX.1 specification
(same as UNIX).
Implementation of different kernel versions
of Linux can vary, but all implement the
same system call interface (usually)

User Space Program Calls System

public POSIX.1 System Call Interface

Kernel

Private Implementation

Kernel Services
POSIX.1 functions are implemented in the kernel,
device drivers, and modules.
Some system call functions might be user-space
programs (eg, threads)
Internal software is passive
does not have any internal thread of execution
is simply a collection of functions and data structures

Kernel Services
user code is active;
makes a procedure call on POSIX.1
a process outside the kernel begins to execute kernel code
when it makes a system call.

In Linux, the process executing the user program


also executes the kernel programs when a system
call is made.
Before system call a process is executing user code.
After call same process is executing kernel code.
Process remains the same.

Kernel Services
Problem:
changing hardware mode bit is a privileged instruction.
user code cant change mode bit when makes sys call
normal function call is a link to the function code; but
this would link the user program into the kernel.
thus kernel code is readable by the user code
so user code could copy the mode change instruction to
its space and execute!

Kernel Services
Solution: CPU contains a hardware trap instruction
causes branch to a prespecified address (could be a
function of the instruction operand)
also switches mode to supervisor
trap is not privileged
destination is determined by set of addresses in kernel
space.
addresses point to kernel code.

1. switch CPU to supervisor mode


Mode
2. Look up a branch address
s
Branch Table in a kernel space table
2

trap
3

3. Branch to a trusted
OS function

Trusted
Code

User

Supervisor

Linux System Call


1.
2.
3.
4.
5.
6.

For system call F, a stub procedure is used to invoke F


(stub is named F)
stub is linked into the user-space calling program.
When process executes the call to F at runtime, control
is transferred to the stub procedure, not directly to
kernel.
Stub procedure validates the parameter values being
passed to the kernel procedure.
The stub procedure executes a trap instruction that
switches the CPU to supervisory mode.
Stub then branches (via kernel table) to entry point for
the target kernel function.

Linux System Call

kernel functions are executed as if they are in a


critical section.
system call keeps CPU until it finishes
can only be interrupted by IRQ.
1.
2.
3.

Thus a kernel function can update kernel data


structures without being interrupted.
When you write a kernel function, cannot write code
that will block!
Could develop a race condition between a system call
and a ISR.

Daemons
process that is executed without a terminal
execute in background
perform critical services, e.g., respond to
incoming network packets.
name usually ends in d
examples: syslogd, crond, lpd

You might also like