Introduction to Kernel (Part 1)

In this article we are going to learn information about Linux kernel source code and then further explore the Linux kernel and cross compile Linux kernel source code for ARM devices. Here we are using Arndale 5250 development board.

The linux kernel is the heart of Desktop system or embedded systems. The kernel is responsible for memory allocation, process and thread creation, memory management and scheduling, communication between hardware and peripheral devices. The Linux kernel is one of the components of a system, which also requires libraries and applications to provide features to end users. The initial development of Linux kernel was done by Linus Torvalds in 1990’s and he has been able to create a large and dynamic developer and user community around linux. Linux can be referred as in two ways one is “Linux” and other is “GNU/Linux”, the reason behind this is Linux is the kernel of an operating system and the wide range of applications that make operating system useful are the GNU software. The GNU software tools are like compiler, editors, variety of shells, utilities and other development tools are exist outside of the kernel. The figure1 shows the fundamental architecture of Linux operating system.

Figure1: Architecture of Linux operating system

The top of the architecture is the user space. This is where the user applications are executed. Bottom of the architecture is the kernel space where the Linux kernel exists. There is also the GNU C Library (glibc). This provides the system call interface that connects user space to the kernel space and provides the mechanism to transition between the user space applications to the kernel space. This is important because the kernel and user application occupy different protected address spaces. And while each user space process occupies its own virtual address space, the kernel occupies a single address space. The Linux kernel can be further divided into three levels. At the top is the system call interface, which implements the basic functions such as read and writes. Below the system call interface is the kernel code, which can be more accurately defined as the architecture-independent kernel code. This code is common to all of the processor architectures supported by Linux. At bottom is the architecture dependent code, mostly called a BSP (Board Support Package). This code serves as the processor and platform specific code for the given architecture. Linux is also a dynamic kernel, supporting the addition and removal of software components on the fly. These are called dynamically loadable kernel modules, and they can be inserted at the boot time when they are needed or at any time by the user.

Linux kernel key features:
  • Linux can be easy to program. It is open source and many useful resources available on the internet and you can learn from existing code.
  • It is easily portable and more hardware support. It runs on the most of the architecture.
  • Scalability. It can run on supercomputer as well as small tiny devices.
  • Stability and reliability.
  • Security.
  • Modularity.

The linux kernel main roles are manage all the hardware resources like CPU, Memory and I/O. Provide a set of portable, architecture and hardware independent APIs to allow user applications and libraries to use the hardware resources and handle concurrent accesses and usage of hardware resources form different applications. The kernel information available in user space through pseudo file systems also called as virtual file system. Pseudo file systems allow applications to see directories and files that do not exist on any real storage; they are created and updated on the fly by the kernel. The two most important file systems are
  • Proc: usually mounted on /proc – it contain operating system related information like processes and memory management parameters.
  • Sysfs: usually mounted on /sys


Figuere2: Linux kernel in the system.

Major subsystems of the Linux kernel
The Figure3 shows some major components of the Linux kernel.

Figure3: Major components of Linux kernel

System call interface
  • It is the main interface between the user space and kernel space.
  • It is architecture dependent, even within the same processor family.
  • There are more than 300 system calls that provide the main kernel services, like File operations, networking operations, inter process communication, memory management, process management, threads, etc.
  • It is stable over time, only new system calls to be added by the kernel developers.
  • This system call interface is wrapped by the C library, and user space applications never make a system call directly but rather use the corresponding C library function.
  • The system call interface implementation in ./linux/kernel and architecture dependent portions in ./linux/arch.

Process management:
  • Process management is focused on the execution of processes.
  • In the user space these are called process.
  • In the kernel space these are called threads and represent an individual virtualization of the process like CPU registers, data segment, stack segment and thread code.
  • The kernel provides an application program interface (API) through system call interface to create new process using fork, exec, POSIX (portable operating system interface) functions, to stop a process kill and exit functions and to communicate and synchronizing between them is signal and posix mechanisms.
  • The process management is also responsible for sharing CPU between the active threads. The kernel implements a novel scheduling algorithm that operates in constant time, regardless of the number of threads waiting for the CPU.

Memory management:
  • Linux provides abstractions over 4KB buffers, such as the slab allocator. This memory management scheme uses 4KB buffers as its base, but then allocates structures from within, keeping track of which pages are full, partially used, and empty.
  • Supporting multiple users of memory, there are times when the available memory can be exhausted. For this reason, pages can be moved out of memory and onto the disk. This process is called swapping because the pages are swapped from memory onto the hard disk.
  • The memory management sources in ./linux/mm.

Virtual File System (VFS)
  • The virtual file system is an important aspect in the Linux kernel; it provides a common interface abstraction for file systems. The VFS provides a switching layer between the system call interface and the file system supported by the kernel.

    Figure4: Virtual File System
  • At the top of the virtual file system is a common API abstraction of functions such as open, close, read and write.
  • At the bottom of the virtual file system are the file system abstractions that define how the upper layer functions are implements.
  • Below the file system layer is buffer cache which provides a common set of functions to the file system layer independent of any file system. This caching layer optimizes access to the physical devices by keeping data around for a short time.
  • Below the buffer cache are the device drivers which implement the interface for the particular physical device.
  • The system call interface implementation in ./linux/fs

Network Stack
  • The network stack follows a layered architecture modeled after protocols themselves. The internet protocol is the core network layer protocol that sits below the transport protocol, above transport control protocol is the socket layer which is invoked through system call interface.
  • The sockets layer is the standard API to the networking subsystem and provides a user interface to a variety of networking protocols.
  • The system call interface implementation in ./linux/net

Device Drivers:
  • The vast majority of the source code in the Linux kernel exists in device drivers that make a particular hardware device usable.
  • The Linux source tree provides a drivers subdirectory that is further divided by the various devices that are supported, such as Bluetooth, I2C, serial, and so on.
  • The device driver sources implementation in ./linux/drivers.

Supported Hardware Architectures:
The ./linux/arch subdirectory defines the architecture dependent portion of the kernel source contained in a number of subdirectories that are specific to the architecture.
  • 32 bit architectures with MMU and without MMU, gcc support.
    Examples: arm, avr32, mips, microblaze – (arch/subdirectories)
  • 64 bit architectures
    Examples: arm64, alpha – (32/64 bit architectures)

Linux versioning scheme and development process
The versioning system of Linux kernel tells about two types of kernel one is stable kernel which is fully developed and ready to use and it is almost bugs free, and second one is unstable source which is under development.
  • Stable branch will be every 2 or 3 years. It identified by an even middle number. For example 1.0.x, 2.0.x, 3.0.x.
  • Development branch is to integrate new functionalities and major changes. Identified by an odd middle number. For example 1.1.x, 2.1.x, 3.1.x.
  • These development versions can be stable version after some time.

It is easy to identify what kernel version we are using. The first few lines of the top level makefile in the kernel source tree give details regarding the kernel version as shown in below.

VERSION = 3
PATCHLEVEL = 4
SUBLEVEL = 5
EXTRAVERSION =
NAME = Saber-toothed Squirrel

$ cat /proc/version – it will give the following output with kernel version.
Linux version 3.11.0-26-generic (buildd@komainu) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #45~precise1-Ubuntu SMP Tue Jul 15 04:02:35 UTC 2014

Linux kernel source
The official versions of the Linux Kernel sources released by Linua Torvalds are available at https://www.kernel.org/. Many chip vendors supply their own kernel sources focusing on hardware support. The kernel sources are available from https://kernel.org/pub/linux/kernel as complete kernel sources and patches. Using git tool we can download the kernel source from internet.

$ git clone git://android.git.linaro.org/device/samsung/manta.git
$ cd manta
$ git checkout -b linaro_android_4.4.2 origin/linaro_android_4.4.2

Linux kernel size
  • Linux 3.10 source
    • Raw size – 573 MB
    • gzip compressed tar archive – 105MB
    • bzip2 compressed tar archive – 83MB
    • xz compressed tar archive – 69MB
  • These sources are includes so many device drivers, many protocols and support for many architectures. The linux core which memory management and scheduler are very small.
    • drivers/ -- 49.4%
    • arch/ -- 21.9%
    • fs/ -- 6.%
    • include/ -- 4.7%
    • sound/ -- 4.4%
    • documentation/ -- 4%
    • net/ -- 3.9%
    • firmware/ -- 1.0%
    • tools/ -- 0.9%
    • scripts/ -- 0.5%
    • mm/ -- 0.5%
    • lib/ -- 0.4%
    • block/ -- 0.2%
    • kernel/ -- 1.0%

Download linux kernel source and uncompress it.
https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.9.tar.xz
tar -xvf linux-3.10.9.tar.xz




























#Tags