Basics of Kernel

General
Kernel is divided into layers:
  1. Electronics like electronics, I/O, CPU, memory, disk controllers, network controllers, user peripherals
  2. Hardware interfaces: Devices access and bus drivers, CPU specific, physical memory operations, disk controllers drivers, network device drivers, HI peripherals device drivers
  3. Device control: Generic HW access, Interrupt context, Page allocator, Block devices, Virtual network device, abstract devices and HID class drivers
  4. Funtional: system run, Schedular, logical memory, Logical file systems, protocols, HI subsytems
  5. Bridges: Synchronization, Memory mapping, Page Cache and Swap, Networking storage,
  6. Virtual: Device model, threads, virtual memory, virtual file system, protocol families,
  7. user space interfaces: system interfaces, processes, memory access, file and directories access, sockets access, HI char devices
  8. functions layers: system, processing, memory, storage, networking, human interface.


Architecture:
  1. Device drivers and kernel extensions run in kernel space - ring 0 with full access to the H/W.
  2. Some exceptions run in user space.
  3. Kernel is also called as core, internal, and supervisior.
  4. Kernel Space includes System state and memory space.
  5. Applications running on the system communicate with the kernel via the system call.
  6. An application typically calls functins in a library for example, the C library that in turn rely on the system call interface to instruct the kernel to carry out tasks on their behalf.
  7. Some library calls provide many features not found in the system call, thus calling into the kernel is just one step in an otherwise large function.
  8. The applications call into the kernel via the system call interface is the fundamental manner in which the application get work done.
  9. The processor is done one of the three things at any given moment:
  10. In kernel space, in process context, executing on behalf of a specific process
  11. In kernel space, in interrupt context, not associated with a process, handling an interrupt
  12. In user-space, executing user code in a process


Monolithic kernels Versus Microkernel Designs:
  1. Monolithic kernels: Implemented entirely as a singel large processes running entirely in a singel address space. They typically exist on disk assingle static binaries. Communication within kernel is trivial.  
  2. Linux is monolithic. It has some features of the microkernel like: modular design with kernel preemption, kernel threads, dynamically load separate binaries into the kernel.
  3. Microkernel: The functionality of the kernel is broken down into separate processes, usually called servers. Direct funtion invocation is not possible. An IPC is built into the system and the varios servers communicate and invoke 'Services'. Their is an overhead in this mechanism.


How are Linux kernel Versions defined?
  1. 2.6.0
  2. 2 is the major release
  3. 6 is the minor release. Even means it is stable and odd means it is in the development stage.
  4. 0 is the patch level


Installation
  1. tar xvjf linux-x.y.z.tar.bz2 //for bzip2
  2. tar xvzf linux-x.y.z.tar.gz //for GNU zip


Using patches:
patch -p1 < ../patch-x.y.z


Kernel Features:
  1. coded in GNU C
    1. Inline functions: It makes the code efficient because the function invocation and return are saved making optimizations because the compiler can optimize the caller and the called    function together.
    2. For large sized functions it becomes an overhead because memory consumption and instruction cache footprint increases.
    3. It is declared as:
      1. static inline void abc(unsigned long var1)
    4. They are commonly included in the header files.
    5. They are type safe as compared to macros
    6. gcc c compiler enables the embedding of assembly instructions in normal C functions. It is used in those parts of the kernel that are unique to system architecture.
    7. asm() compiler directive is used to inline assembly code.
    8. gcc c compiler directive divides the conditional branches as very likely or unlikely taken. 2 macros like: likely() and unlikely()
if(unlikely(f00)
{
/*------*/
}


if(likely(foo))
{
/*--------*/
}


Performance boost when the branch is predicted correctly or loss when it is predicated uncorrectly.


  1. Lacks memory protection like user-space
    1. For a user process error the kernel can trap the error, send SIGSEGV, and kill the process.
    2. If kernel does any error then there is no one to look out.


  1. Cannot easily use floating point


  1. Has a small fixed size stack
    1. The kernel stack is fixed in size and is small. Stack size varies by architecture.
    2. Stack is configure at compile time and is either 4 or 8kb
    3. It is of 2 pages, 8KB on 32 bit architecture and 16KB on 64 bit architecture


  1. Synchronization and concurrency are major concerns.
    1. Kernel is vulnerable to race conditions.
    2. Linux kernel supports multiprocessing. Without proper protection, kernel code executing on two or more processes can access the same resource
    3. Solution: Semaphores and spinlocks


  1. Portability - C code is architecture independent.


  1. Does not have access to the C library
    1. Reason is space and speed.
    2. Many of the usual libc functions have been implemented inside the kernel. EX: lib/string.c <linux/string.h>
    3. Kernel source files cannot use outside source files because it cannot include them.
    4. printk() is used in place of printf(). It copies the formatted string into the kernel log buffer, which is normally read by the syslog program. Allows to use the priority flag, which is used         by syslogd(8) to decide where to display kernel messages.






No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...