This repository contains a set of Linux Kernel Modules (LKMs) that provide insights into process and memory management in the Linux kernel. These modules demonstrate how to list processes, inspect process information, and map virtual to physical memory addresses.
-
Install necessary packages:
sudo apt install linux-headers-$(uname -r) build-essential
-
Follow instructions in each directory's
Makefile
to build and load the kernel modules and IOCTL drivers.
This kernel module lists all processes that are in a running or runnable state and prints their process IDs (PIDs).
- Load the module:
sudo insmod lkm1.ko
- Check the kernel log for the output:
dmesg | tail
- Unload the module:
sudo rmmod lkm1
This kernel module takes a process ID as input and prints the PIDs and states of its child processes.
- Load the module with a PID:
sudo insmod lkm2.ko pid=<PID>
- Check the kernel log for the output:
dmesg | tail
- Unload the module:
sudo rmmod lkm2
This kernel module takes a process ID and a virtual address as input. It determines if the virtual address is mapped and, if so, prints the corresponding physical address along with the PID and virtual address.
- Load the module with a PID and virtual address:
sudo insmod lkm3.ko pid=<PID> vaddr=<VIRTUAL_ADDRESS>
- Check the kernel log for the output:
dmesg | tail
- Unload the module:
sudo rmmod lkm3
This kernel module, given a process ID, determines the size of the allocated virtual address space and the mapped physical address space. A test program is also provided to allocate memory in stages and observe the memory stats.
- Load the module with a PID:
sudo insmod lkm4.ko pid=<PID>
- Run the test program:
./test_program
- Check the kernel log for the output:
dmesg | tail
- Unload the module:
sudo rmmod lkm4
To build all the modules, run:
make
To load a module:
sudo insmod <module_name>.ko
To unload a module:
sudo rmmod <module_name>
To view the kernel log:
dmesg | tail
This project includes multiple tasks involving kernel module programming and IOCTL (Input/Output Control) interface to interact with user-space programs. The project consists of three main components:
- Implementing an IOCTL device driver to translate virtual to physical addresses and modify physical memory.
- Implementing an IOCTL device driver to manage process signaling between a central station and soldier processes.
- Creating
/proc
filesystem entries to display custom information.
Create an IOCTL device driver to:
- Provide the physical address for a given virtual address.
- Write a byte value to a specified physical address.
- Kernel Module:
mem_ioctl.c
- User Space Program:
mem_user.c
- Script:
spock.sh
-
Kernel Module (
mem_ioctl.c
):- Provide IOCTL commands to get physical address and write to physical memory.
-
User Space Program (
mem_user.c
):- Allocate a byte of memory, assign a value, and print the virtual address and value.
- Use IOCTL calls to get the physical address and modify the value.
- Verify the changes by printing the modified value.
-
Script (
spock.sh
):- Compile the kernel module and user space application.
- Initiate the IOCTL device.
- Run the user space application.
- Cleanly remove the IOCTL device.
./spock.sh
Create an IOCTL device driver to:
- Change the parent process of a given PID so that it receives a
SIGCHLD
signal on exit.
- Kernel Module:
signal_ioctl.c
- Central Station Program: Provided in the linked repository.
- Soldier Program: Provided in the linked repository.
- Script:
station_soldier.sh
-
Kernel Module (
signal_ioctl.c
):- Implement an IOCTL command to change the parent process of a given PID.
-
Script (
station_soldier.sh
):- Compile the kernel module and provided programs.
- Launch the central station and soldier programs.
- Cleanly remove the IOCTL device.
./station_soldier.sh
Create kernel modules to introduce new entries in the /proc
filesystem.
- Hello World Proc Entry:
hello_procfs.c
- Page Faults Proc Entry:
get_pgfaults.c
-
Hello World Proc Entry (
hello_procfs.c
):- Create a
/proc/hello_procfs
entry that displays "Hello World!".
- Create a
-
Page Faults Proc Entry (
get_pgfaults.c
):- Create a
/proc/get_pgfaults
entry that displays the total number of page faults since system boot.
- Create a
# Insert the module
sudo insmod hello_procfs.ko
# Display the message
cat /proc/hello_procfs
# Remove the module
sudo rmmod hello_procfs
# Insert the module
sudo insmod get_pgfaults.ko
# Display page faults
cat /proc/get_pgfaults
# Remove the module
sudo rmmod get_pgfaults
To compile and install the kernel modules and user space programs, use the provided scripts.
# For Task 1
./spock.sh
# For Task 2
./station_soldier.sh
Ensure you have the necessary kernel headers and build tools installed on your system.
This project demonstrates the use of IOCTL for interacting between user space and kernel space, managing process signaling, and creating /proc
filesystem entries for custom information. These tasks highlight key aspects of Linux kernel module programming and memory management.
Linux-INternals-Understanding-and-eXploration/
├── 1/
│ ├── lkm1.c
│ ├── lkm2.c
│ ├── lkm3.c
│ ├── lkm4.c
│ ├── Makefile
│ └── Kbuild* (if used)
├── 2_I/
│ ├── spock.sh
│ └── ... (solution specific files/dirs)
├── 2_II/
│ ├── control_station.c
│ ├── soldier.c
│ ├── run_dr_doom.sh
│ ├── Makefile
│ └── ... (solution specific files/dirs)
└── 3/
├── hello_procfs.c
├── get_pgfaults.c
├── Makefile
└── Kbuild* (if needed)
For any questions or support, please contact aratrik1999@gmail.com
Aratrik Chandra