23 Oct Ce este kernelul?
The kernel is the „core” of any computer system: it is the „software” which allows users to share computer resources.
The kernel can be thought as the main software of the OS (Operating System), which may also include graphics management.
For example, under Linux (like other Unix-like OSs), the XWindow environment doesn’t belong to the Linux Kernel, because it manages only graphical operations (it uses user mode I/O to access video card devices).
By contrast, Windows environments (Win9x, WinME, WinNT, Win2K, WinXP, and so on) are a mix between a graphical environment and kernel.
3.2 What is the difference between User Mode and Kernel Mode?
Many years ago, when computers were as big as a room, users ran their applications with much difficulty and, sometimes, their applications crashed the computer.
To avoid having applications that constantly crashed, newer OSs were designed with 2 different operative modes:
Kernel Mode: the machine operates with critical data structure, direct hardware (IN/OUT or memory mapped), direct memory, IRQ, DMA, and so on.
User Mode: users can run applications.
| Applications /|
| ______________ |
| | User Mode | |
| ______________ |
| | |
Implementation | _______ _______ | Abstraction
Detail | | Kernel Mode | |
| _______________ |
| | |
| | |
| | |
|/ Hardware |
Kernel Mode „prevents” User Mode applications from damaging the system or its features.
Modern microprocessors implement in hardware at least 2 different states. For example under Intel, 4 states determine the PL (Privilege Level). It is possible to use 0,1,2,3 states, with 0 used in Kernel Mode.
Unix OS requires only 2 privilege levels, and we will use such a paradigm as point of reference.
3.3 Switching from User Mode to Kernel Mode
When do we switch?
Once we understand that there are 2 different modes, we have to know when we switch from one to the other.
Typically, there are 2 points of switching:
When calling a System Call: after calling a System Call, the task voluntary calls pieces of code living in Kernel Mode
When an IRQ (or exception) comes: after the IRQ an IRQ handler (or exception handler) is called, then control returns back to the task that was interrupted like nothing was happened.
System calls are like special functions that manage OS routines which live in Kernel Mode.
A system call can be called when we:
access an I/O device or a file (like read or write)
need to access privileged information (like pid, changing scheduling policy or other information)
need to change execution context (like forking or executing some other application)
need to execute a particular command (like ”chdir”, ”kill”, ”brk”, or ”signal”)
––->| System Call i | (Accessing Devices)
| | | | [sys_read()] |
| … | | | |
| system_call(i) |––– | |
| [read()] | | |
| … | | |
| system_call(j) |––– | |
| [get_pid()] | | | |
| … | ––->| System Call j | (Accessing kernel data structures)
| | | [sys_getpid()]|
USER MODE KERNEL MODE
Unix System Calls Working
System calls are almost the only interface used by User Mode to talk with low level resources (hardware). The only exception to this statement is when a process uses ”ioperm” system call. In this case a device can be accessed directly by User Mode process (IRQs cannot be used).
NOTE: Not every ”C” function is a system call, only some of them.
When an IRQ comes, the task that is running is interrupted in order to service the IRQ Handler.
After the IRQ is handled, control returns backs exactly to point of interrupt, like nothing happened.
NORMAL | | | [break execution] IRQ Handler
EXECUTION (1)| | | ––––->|–––|
| \|/ | | | does |
IRQ (2)–->| .. |––> | some |
| | |<----- | work | BACK TO | | | | | ..(4). | NORMAL (6)| \|/ | <-------------|_________| EXECUTION |___________| [return to code] (5) USER MODE KERNEL MODE User->Kernel Mode Transition caused by IRQ event
The numbered steps below refer to the sequence of events in the diagram above:
Process is executing
IRQ comes while the task is running.
Task is interrupted to call an „Interrupt handler”.
The „Interrupt handler” code is executed.
Control returns back to task user mode (as if nothing happened)
Process returns back to normal execution
Special interest has the Timer IRQ, coming every TIMER ms to manage:
System and task counters (used by schedule to decide when stop a process or for accounting)
Multitasking based on wake up mechanism after TIMESLICE time.