Thursday, December 30, 2004

Kernel : Basics

Kernel Basics
BASICS Of the Linux Kernel : Chapter 01.
WRITTEN BY : Karthikeyan Raghuraman
REFERENCE : Understanding the Linux Kernel, O'Reilly Publications
DISCLAIMER : The content below is my understanding of the Chapter 1 in the above mentioned book. I myself being a newbie do not guarantee the authenticity of the following information.
CONTACT : karthikeyan dot raghuraman at gmail dot com

An overview of the Unix/Linux Kernels
Kernels provide an environment for applications to run
The kernel implements a lot of services and exposes the corresponding interfaces.
Applications use these interfaces and do not interact directly with the Hardware

**Process/Kernel Model
A process can run in either of the two modes
- User Mode
- Kernel Mode

A program running in User mode can not access the Kernel programs/data structures, these restrictions do not apply when executing in Kernel Mode.
A program executes most of the time in User Mode and switches to the Kernel Mode only when requesting a service provided by the kernel.
The Linux systems apart from the user process a few privileged processes called Kernel Threads run...

*Activating Kernel Routines
1. Invoke a system Call
2. CPU signals an Exception
3. Peripheral issues an Interrupt.

** Process Implementation
- Each process has a process descriptor.
- When the kernel stops executing a process it saves the status of registers into this process descriptor
. PC : Program Counter
. SP : Stack Pointer
. GPR: General Purpose Register
. FPR: Floating Point Registers
. PSW: Processor Status Word
. Memory Registers used to keep track of RAM access

- When the kernel wants to invoke a previously running process, it uses the process's descriptor to load the registers
- When a process waits for an event to occur, it is put in a queue.
- There exists different queues each for a specific event.

**Reentrant Kernels
-A reentrant process consists of logically separate code and data segments and a private stack.
-Many reentrant process can share the same code segment but each instance has its own data segment and stack.

-Reentrant kernel means several processes could be executing at the same time in the kernel mode

-KERNEL CONTROL PATH : the sequence of instructions executed by the kernel to handle a system call
-the execution of the Kernel Control path is interleaved.

**Process Address Space
-Every process has its own address space.
-User Mode Execution
. Private Stack,Data and Code areas.
-Kernel Mode Execution
.Kernel Code and Data. Private Stack.
-Address space sharing for Communication
-mmap: memory map command. Allows a part of the file/memory to be mapped to the process address space.

**Synchronization and Critical Regions
- Reentrant kernels require synchronization.
- the Race and DeadLock conditions.
- Atomic operations disallow such possibilities of Race/Deadlock.
- Could be solved using
. Non preemptive kernels
. Interrupt Disabling before entering Critical region.
. Semaphores
. SpinLocks

**Signals and IPC
-System Events
.Asynchronous Notification
.Synchronous Errors/Exceptions
- POSIX defines 20 signals : 2 User definable.
- On reception of a Signal a process may
. handle the signal
. ignore it
- If the process is not performing any of the above mentioned actions the kernel wud take one of the default action listed below.
.Terminate the process.
.Core Dump and terminate the process.
.Ignore the signal.
.Suspend the process.
.Resume the process execution.
- Shared Memory provides the fastest way for exchanging data between processes

** I have skipped Process and Memory Management.