Linux Device Drivers Part - 2 : System Memory, Static Modules and Dynamic Modules

Before proceeding further, I strongly recommend you to watch Part-1 of Linux Device Drivers from MrTechpathi tutorials.
mrtechpathi_tutorials_linux_device_drivers_part_1

In this part we will learn about following topics.
  1. System Memory
  2. Kernel Space and User Space
  3. Kernel Modules : Static Modules and Dynamic Modules
So lets start with our first topic

1. System Memory
This topic is choosen to completely understand user space and kernel space.

Every system has certain amount of memory.

This memory consists of RAM (Random Access Memory) cells, whose contents can be accessed (i.e., read and written to) at extremely high speeds but are retained only temporarily (i.e., while in use or, at most, while the power supply remains on).

Coming to the purpose of memory, it holds programs and data that are currently in use and thereby serve as a high speed mediumbetween the CPU (central processing unit) and the much slower storages like hard disk drives (HDDs).

This System memory in Linux can be divided into two distinct regions: Kernel space and User space.

Hope you understood how system memory, user space and kernel space are correlated.

Now, lets move to our next topic.

2. Kernel Space and User space
So, what is a kernel space ?

Kernel space is where the  core of the operating system runs and provides its services.

The left paritition is the kernel space where the operating system runs and provides services.

The right partition is the user space where user processes run.

What is a user space ?

User space is that set of memory locations in which user processes run. A process is an executing instance of a program. One of the roles of the kernel is to manage individual user processes within this space and to prevent them from interfering with each other.

Note that Device Driver runs in Kernel space and not in user space. All applications run in User space.These are very important points to be remembred by every device programmer.

3. Kernel Space and User space  Interaction
How does this interaction happens ?

To answer this quesion in short, it happens only through system calls.

In detail, Kernel space can be accessed by user processes only through the use of system calls.System calls are requests in a Unix-like operating system by an active process for a service provided by the kernel, such as input/output (I/O) or process creation.

In this what exactly we mean by Active process ?

 An active process is a process that is currently progressing in the CPU. Whereas, process is the one which is waiting for its next turn in the CPU. I/O is any program, operation or device that transfers data to or from a CPU and to or from a peripheral device (such as disk drives, keyboards, mice and printers).

Please note that user space processes can't access the hardware directly. They need to use system calls for accessing certain functionality of hardware. These system calls are implemented by a driver and runs in kernel space to provide functionaliy of hardware to user.

All these interactions are managed by the core kernel.

If we go one step further and relate the previous "User space and Kernel Space" diagram with our program, kernel and hardware, it will resemble as this figure.

Our application program will reside in user space and driver will reside in Kernel space. System calls are the only way for a user space program to access kernel space Device Drivers.

Hope all are clear with Kernel space , User space and their interaction using system calls.

Kernel Modules

Kernel Modules can be classified into two types

Static modules
Dynamic modules

What is a static module ?

Static modules are compiled as a part of the kernel and are available at anytime. These make the kernel larger and has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.

Is there any better way than static modules ? Yes they are dynamic modules.

What is a dynamic module ?

Dynamic modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.  After loading the kernel, we can load this modules using insmod and unload using rmmod. We will be discussing about insmod and rmmod in upcoming tutorials.

Dynamic Modules have the advantage that it uses the Memory more efficiently than the Statically linked drivers.

Hope you are clear about Static modules which will be part of the kernel and dynamic modules which can be loaded into kernel based on our requirement.

This is end of part-2 of Linux Device Drivers.

If you are not clear on any topic discussed till now, please watch part-1 and part-2 couple of times without missing a single point. This will help in upcoming tutorials.

Comments

Popular posts from this blog

Linux Device Drivers - Part 4 : Linux Kernel Moduels (LKM) and types of LKM's

Linux Device Drivers Part - 5 : Building and Compiling Kernel Moduels

Linux Device Drivers - Part 13 : More on Device Numbers