1. introto linux kernel

Upload: alamxaib-khan

Post on 07-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 1. IntroTo Linux Kernel

    1/41

  • 8/6/2019 1. IntroTo Linux Kernel

    2/41

    What is Linux?

    Is it different from Windows?

    Before moving along, let's make sure thatwe understand the difference between aGUI and an OS.

    Windows is an operating system that includesa GUI.

    Mac OS X is an operating system thatincludes a GUI.

    Linux is an operating system that may or may

    not include a GUI.

  • 8/6/2019 1. IntroTo Linux Kernel

    3/41

    what is an OS?

    Computers are really dumb and don't think. They blindly followwhatever instructions they are given.

    That said, the operating system is a program that exists to

    control access to hardware and software resources like yourmemory (RAM), hard-drives, network, display, keyboard, mouseand so on.

    Thus, a program like OpenOffice needs to ask the OS which

    keys were pressed on the keyboard. OpenOffice also needs to ask the operation system to draw

    letters on the screen.

    OpenOffice also needs to ask the operating system to allocatememory, load and save information to the hard disk, and send

    printing instructions to your printer.

  • 8/6/2019 1. IntroTo Linux Kernel

    4/41

  • 8/6/2019 1. IntroTo Linux Kernel

    5/41

    Putting all this into a picture..

    http://farstarnet.com/jfbilodeau/uploaded_images/os-layers-705763.png
  • 8/6/2019 1. IntroTo Linux Kernel

    6/41

    Back to the original question:

    What is Linux?The answer is: Linux is a kernel. Nomore. No less.

    It is the fundamental piece of code thatcontrols the resources on your machine.

    Windows has a kernel.

    Mac OS X has a kernel.

    Pretty much every operating system has akernel.

  • 8/6/2019 1. IntroTo Linux Kernel

    7/41

    So, whats inside a kernel?

  • 8/6/2019 1. IntroTo Linux Kernel

    8/41

    But, I have never seen akernel!!

    End users do not interact directly with the kernel. To allow users to interact with their computer, a set of

    tools are provided with the operating system. In the world of Windows, you are familiar with the Start

    menu, the Control Panel and the Windows Explorer. These are some of the operating system's tools that one

    uses everyday to control the machine. These programs are part of the operating system, but run

    on top of the kernel. Such tools are sometimes called shell

    programs. A kernel provides API that computer programmers use to write

    these shell programs.

  • 8/6/2019 1. IntroTo Linux Kernel

    9/41

    So, is Linux not an OS?

    Linux is only a kernel.

    It does not include a shell or programs.

    However, Linux is most commonly packaged

    with a combination of free programs fromanother--as of yet incomplete--operatingsystem called GNU.

    Because it borrows from the GNU project,you may hear people refer to the operatingsystem as GNU/Linux.

    http://en.wikipedia.org/wiki/GNUhttp://en.wikipedia.org/wiki/GNU
  • 8/6/2019 1. IntroTo Linux Kernel

    10/41

    Another view..

    With the combinationof the GNU tools andthe Linux kernel, we

    now have a completeoperating system.Lets visualize it

  • 8/6/2019 1. IntroTo Linux Kernel

    11/41

    A little bit more on GNU

    GNU: Open Source OS before Linux

    The concept of free software

    Through 70s: Richard Stallman advocates free

    software free as in free speech not free beer (not zero

    cost) - free to use, distribute (for a profit), and modify.

    1984: Richard Stallman founded GNU

    Goal: to produce free software

    GPL: ensure software freedom by copyrightterms

    GNU software: Unix-like programs (no kernel)

  • 8/6/2019 1. IntroTo Linux Kernel

    12/41

    Linux History

    1991: Finnish student Linus Torvalds started working onan update of Minix (released in 1987 by Andrew STanenbaum).

    From the start, Linus asked volunteers on the Internet tohelp him develop Linux People started using and publicizing Linux A number of programmers joined the project Linux Community: created by a loose worldwide community of

    programmers collaborating mainly through the internet

    From the start, the source code has been freely available

    on the Internet Linux provides a kernel for GNU software

    GNU has lots of user-space programs (like libraries, compilers,text editors and a Windowing System) but no kernel

    Linux kernel + GNU software = Linux distribution

  • 8/6/2019 1. IntroTo Linux Kernel

    13/41

    Linux Kernel Evolution Linux kernel has undergone massive development in the last one and

    a half decade : 0 (Apr 1991): First e-mail from Linus about his new OS. 0.01 (Sept 1991) 1.0 (March 94), user: 100,000 1.2 (March 95), user: 500,000 2.0 (June 96), user: 1,500,000

    2.2 (January 99) 2.4 (January 2001) 2.6 (December 2003)

    Odd/Even versioning system 2.4.x: stable kernels 2.5.x: development kernels

    Starting with the Linux 2.6.x series, there is no significance to even or odd numbers, with new feature

    development going on in the same kernel series. Linus Torvalds has statedthat this will be the model for the foreseeable future.

    The version number of the Linux kernel has changed from a three-numberversioning scheme to a four-number one. A.B.C[.D] (e.g. 2.2.1, 2.4.13 or2.6.12.3).

  • 8/6/2019 1. IntroTo Linux Kernel

    14/41

    Linux Kernel versions

  • 8/6/2019 1. IntroTo Linux Kernel

    15/41

    The Kernel of Our Study

    Using Linux on Windows By using partitioning and a dual boot system

    By running Linux in a virtual machine (usingVMWare, sun virtiual box or Virtual PC) on a

    Windows system.

  • 8/6/2019 1. IntroTo Linux Kernel

    16/41

    Recall..

  • 8/6/2019 1. IntroTo Linux Kernel

    17/41

    Kernel basic facilities The kernel's primary purpose is to manage the

    computer's resources and allow other programs to runand use these resources

    The Central Processing Unit (CPU, the processor). Kernels role - deciding at any time which of the many

    running programs should be allocated to the processor orprocessors .

    The computer's memory. Kernels role - deciding which memory each process can

    use, and determining what to do when not enough isavailable.

    Input/Output (I/O) devices. Kernels role - allocates requests from applications to

    perform I/O to an appropriate device and providesconvenient methods for using the device (typicallyabstracted to the point where the application does not needto know implementation details of the device).

  • 8/6/2019 1. IntroTo Linux Kernel

    18/41

    The Linux Kernel

    Linux kernel is written in C programminglanguage

    It implements POSIX standards in terms ofinterface, commands etc, but the sourcecode implementation is not UNIX.

  • 8/6/2019 1. IntroTo Linux Kernel

    19/41

    A. Frank - P. Weisberg

    UNIX System Structure

  • 8/6/2019 1. IntroTo Linux Kernel

    20/41

  • 8/6/2019 1. IntroTo Linux Kernel

    21/41

    Kernel design decisions

    1. Monolithic

    Examples: Linux, Unix

    2. Microkernel

    Examples: Minix

    3. Hybrid

    Examples: Windows NT

  • 8/6/2019 1. IntroTo Linux Kernel

    22/41

    CPU modes At a minimum, any CPU architecture offers two

    distinct operating modes; Kernel mode or supervisor mode or privilegedmode In kernel mode, the CPU may perform any operation

    allowed by its architecture; any instruction may be

    executed, any I/O operation initiated, any area ofmemory accessed, and so on

    User Mode or Slave Mode certain restrictions on CPU operations are enforced

    by the hardware. Typically, certain instructions are not

    permitted (especially thoseincluding I/Ooperationsthat could alter the global state of themachine), some memory areas cannot be accessed,etc.

  • 8/6/2019 1. IntroTo Linux Kernel

    23/41

    Monolithic Kernel

    As the name suggests kernel is oneunstructured program.

    It runs every basic system service likeprocess and memory management,interrupt handling and I/O communication,file system, etc. as a single process in the

    same address space (in kernel space).

  • 8/6/2019 1. IntroTo Linux Kernel

    24/41

    Monolithic Operating System

  • 8/6/2019 1. IntroTo Linux Kernel

    25/41

    Microkernel System Structure

    Move as much functionality as possible fromthe kernel into user space.

    Only a few essential functions in the kernel primitive memory management (address space) I/O and interrupt management Inter-Process Communication (IPC) basic scheduling

    Other OS services are provided by processesrunning in user mode (referred to as servers) device drivers, file system, virtual memory,

    networking

  • 8/6/2019 1. IntroTo Linux Kernel

    26/41

    A. Frank - P. Weisberg

    Layered vs. MicrokernelArchitecture

  • 8/6/2019 1. IntroTo Linux Kernel

    27/41

    Pros and Cons of Monolithickernels

    Pros increase the

    performance of thesystem.

    Cons : the kernel size,

    lack of extensibility

    bad maintainability. a bug in a device

    driver might crash theentire system.

  • 8/6/2019 1. IntroTo Linux Kernel

    28/41

    Pros and Cons of Microkernel

    Pros easier to maintain

    Cons

    the large number ofsystem calls andcontext switchesslow down thesystem becausethey generate moreoverhead than plainfunction calls.

  • 8/6/2019 1. IntroTo Linux Kernel

    29/41

    The Linux Kernel

    Linux kernel is monolithic Implemented in a single executable module

    Data structure for any aspect of the kernel isavailable to any other part of the kernel.

    Design Philosophy: Main part of kernel should never change

    To account for advanced devices (esp. bitmapdisplays and networks) Linux uses modules as containers to implement

    extensions to the main part of the kernel.

    Module an independent software unit that can bedesigned and implemented after the kernel and

    dynamically loaded into kernel space.

  • 8/6/2019 1. IntroTo Linux Kernel

    30/41

  • 8/6/2019 1. IntroTo Linux Kernel

    31/41

  • 8/6/2019 1. IntroTo Linux Kernel

    32/41

    Introduction to System Calls

  • 8/6/2019 1. IntroTo Linux Kernel

    33/41

    The System Call Path

  • 8/6/2019 1. IntroTo Linux Kernel

    34/41

    System Call Stub Functions The system call stub functions provide a high-level

    language interface to a function whose main job isto generate the software interrupt (trap) needed toget the kernel's attention.

    These functions are often called wrappers.

    The stub functions do the following:

    set up the parameters,

    trap to the kernel,

    check the return value when the kernel returns, and

    if no error: return immediately, else

    if there is an error: set a global error number variable (called"errno") and return a value of -1.

  • 8/6/2019 1. IntroTo Linux Kernel

    35/41

    x86 Linux read (glibc 2.1.3)

    read: push %ebx

    mov 0x10(%esp,1),%edx ; put the 3 parms in registers

    mov 0xc(%esp,1),%ecx

    mov 0x8(%esp,1),%ebx

    mov $0x3,%eax ; 3 is the syscall # for readint $0x80 ; trap to kernel

    pop %ebx

    cmp $0xfffff001,%eax ; check return value

    jae read_err

    read_ret: ret ; return if OK.read_err:

  • 8/6/2019 1. IntroTo Linux Kernel

    36/41

    The dilemma?

    How can a user mode program switch theCPU to supervisor mode with theassurance that once the switch is done the

    CPU will be executing trusted kernel codeand not untrusted user code?

  • 8/6/2019 1. IntroTo Linux Kernel

    37/41

    The trap instruction

    A trap instruction is not a privilegedinstruction, so any program can execute atrap.

    However, the destination of the branchinstruction is predetermined by a set ofaddresses that are kept in supervisory

    space and that are configured to point tokernel code

  • 8/6/2019 1. IntroTo Linux Kernel

    38/41

    The Trap Instruction Operation

    S

    trap

    Mode

    Branch Table

    TrustedCode

    1

    2

    3

    User Space Kernel Space

  • 8/6/2019 1. IntroTo Linux Kernel

    39/41

    Sequence of steps.

    The trap instruction does three things:

    1. Switch the CPU to supervisor mode.

    2. Look up a branch address in a kernel space table.

    3. Branch to a trusted OS function.

    Because the trap instruction is compiled into the stubprocedure, a user-space program cannot easilydetermine the traps destination address.

    Also, it cannot branch directly to the kernel functiononly through the system-provided stub.

  • 8/6/2019 1. IntroTo Linux Kernel

    40/41

  • 8/6/2019 1. IntroTo Linux Kernel

    41/41

    Daemons There is no special kernel process that executes the

    kernel code. Kernel code is actually induced by normal user processes.

    However, there are several user-transparentprocesses called daemons, that are started whena Linux machine is started, and which must exist

    for correct operation of the operating system. They do not belong to a user.

    Used for functioning in areas such as for Processing network packets

    Logging of system and error messages etc. Normally, filename ends with d, such as inetd, syslogd, crond, lpd etc

    ps aux | more