Understanding Real Memory: Creating Kernel Modules for Alcatel OneTouch Pixi Avion (A571VL)
In computing, memory management is a critical aspect of operating systems. One of the key concepts in memory management is the distinction between real memory and virtual memory.
Real Memory vs Virtual Memory
Real memory, also known as physical memory, refers to the actual RAM (Random Access Memory) installed on a device. Real memory is used to store data and instructions that are currently being used by the CPU (Central Processing Unit).
Virtual memory, on the other hand, is a memory management technique that allows a computer to be able to compensate for shortages of physical memory by temporarily transferring pages of data from RAM to a hard drive.
Creating Kernel Modules for Alcatel OneTouch Pixi Avion (A571VL)
A kernel module is a piece of code that can be loaded and unloaded from the kernel on demand. They extend the functionality of the kernel without the need to reboot the system.
In this case study, we will be creating a kernel module for the Alcatel OneTouch Pixi Avion (A571VL) to understand the real memory usage of the device.
Prerequisites
To follow along with this case study, you will need the following:
- A Linux system with kernel headers for the kernel version running on the Alcatel OneTouch Pixi Avion (A571VL) installed.
- The
gcccompiler installed. - The
makeutility installed.
Creating the Kernel Module
Let's start by creating a new directory for our kernel module and navigating into it:
$ mkdir mykernelmodule
$ cd mykernelmodule
Next, we'll create a new file called mykernelmodule.c and open it in our favorite text editor.
$ touch mykernelmodule.c
$ nano mykernelmodule.c
In this file, we'll add the following code to create a simple kernel module that prints out the amount of free real memory on the system:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int __init mykernelmodule_init(void)
{
printkm("My Kernel Module: Free real memory: %lu kB
", (unsigned long)get_free_page_count(get_zone(NULL)));
return 0;
}
static void __exit mykernelmodule_exit(void)
{
printkm("My Kernel Module: Goodbye
");
}
module_init(mykernelmodule_init);
module_exit(mykernelmodule_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple kernel module that prints out the amount of free real memory");
This code includes the necessary headers for creating a kernel module, defines the initialization and exit functions for the module, and uses the get_free_page_count() function to get the number of free pages in the system's zero page zone, which corresponds to the amount of free real memory.
Next, we'll create a makefile to build the kernel module:
$ touch Makefile
$ nano Makefile
In this file, we'll add the following code to build the kernel module:
obj-m += mykernelmodule.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
This makefile uses the make utility to build the kernel module by calling the appropriate commands in the kernel's build system.
Finally, we can build the kernel module by running the following command:
$ make
If the build is successful, you should see a .ko file called mykernelmodule.ko in the current directory.
Loading and Unloading the Kernel Module
To load the kernel module, run the following command:
$ sudo insmod mykernelmodule.ko
You should see the message "My Kernel Module: Free real memory: [amount] kB" printed out in the terminal.
To unload the kernel module, run the following command:
$ sudo rmmod mykernelmodule
You should see the message "My Kernel Module: Goodbye" printed out in the terminal.
In this case study, we have learned about real memory and virtual memory, and created a kernel module for the Alcatel OneTouch Pixi Avion (A571VL) to understand the real memory usage of the device. We have also learned how to build, load, and unload kernel modules on a Linux system.