Select Page

Toggle LED using custom syscall

ABSTRACT

The main aim of the experiment is to be able to create custom syscall and let the kernel perform the task through the syscall method. In this method instead of user program calling some program or library, the user program ask the CPU to switch to kernel mode and then go to predefined location to perform the systemcall. Unlike other linux distributions where the GRUB (Grand Unified bootloader) can automatically find different kernel images, Raspberrypi OS always boots from kernel8 for 64 bit system and kernel7 for 32 bit system.Hence its important to replace the kernel image each time after kernel is recompiled. In this experiment two custom syscall will be created and registered with a syscall numbers and the kernel will be recompiled. When the new kernel image is loaded the user program calls the
syscall function to blink the LED’s.

I   INTRODUCTION

The difference between SYSCALL and SYSFS:
In the previous lab session sysfs was used to blink the LED’s. The sysfs is a virtual filesystem in LINUX which provides a way to interact with kernel objects by exposing the underlying kernel datastructures.The user can then read and write to these datastructure attributes in a structured manner. : Sysfs is often used for querying and configuring device parameters, reading status information, or changing system configurations. It’s more about providing a convenient interface to kernel data rather than executing operations. In syscall when a user-space application needs to perform an operation that requires kernel intervention (like reading from a file or allocating memory), it makes a system call. The kernel then executes the requested operation and returns the result to the application.

Examples: Common syscalls include read(), write(), open(), close(), fork(), execve(), and ioctl(). The interaction between the kernel mode and user mode is shown in the figure 1.

Figure 1: User and kernel space interaction during a syscall

 

II  STEPS FOR CREATING  SYSTEM CALL .

2.1Finding the systemcall number :
1. In the Raspberrypi OS linux architecture “linux/include/uapi/asm-generic/unistd.h” contains the syscall number, in other variants of linux this can be found in syscall.tbl. The file location is shown in the figure 3.
2. Figure 2 shows the two syscall number and its location inside the unistd.h . Here LED gpio and LED toggle are the two fuctions in c with extension .c inside the new directory created as /linux/LED.
2.2 Configuring the Makefile:
Inside the dierectories which contain the LED gpio.c and LED toggle.c, the Makefile needs to be edited any number of .c files can be added and the new objects file can be added as shown in the figure 4. This is the new Makefile.
2.3 Completing the SYSCALL registration:
The “asmlinkage” is a macro used in the Linux kernel to indicate how function arguments are passed. In the context of system calls, it tells the compiler that the function arguments will be passed through the stack (or registers depending on the architecture) rather than through general purpose registers (which is typical for normal functions). It is architecture-specific and used to ensure consistency in how system call parameters are handled. This is shown in figure 5. This edit has to be done in syscalls.h file which is inside /linux/include/linux.

Figure 4: Configuring the Makefile

                                                            

Figure 5: asmlinkage in syscalls.h

                                                  

Figure 2: Entrying new syscall numbers

Figure 3: Looking for syscall numbers

 III  WRITING THE SYSCALL ROUTINE 

The new syscall routine resides in /linux/LED . The code uses SYS DEFINEN method to define syscall where N is the number of parameters to be passed. the two C codes are LED gpio.c and LED toggle.c The first code is to switch on any three GPIO’s which is passed as paramter when calling a function. The second one asks for three colors and three states. the color option being RED,GREEN and YELLOW  and the states bein ON, OFF and TOGGLE. The C codes are attached along with the pdf.

IV  COMPILING THE NEW KERNEL :

To compile the new kernel after the registration of syscall and completion of code has to be done using the following steps:
1. make menuconfig. // do this in the main directory
2. In the general setup and add a new kernel name as shown in figure 6
2. Start compiling the kernel. Using the command make -j6 Image.gz modules dtbs
This steps takes around two to three hours in native compilation.
3. Install the kernel modules using the following commands:
sudo make -j6 modules install
4. Rename the kernel variable:
KERNEL=kernel8
5. Built backup and replace kernel module with custom compiled kernel with syscall method:
sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img
sudo cp arch/arm64/boot/Image.gz /boot/firmware/$KERNEL.img
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/

Complete the new kernel setup by rebooting the system:

Figure 6: New Custom kernel

                               

V  USER CODE TO TEST THE SYSCALL FUNCTIONALITY:

Two user codes LED syscall.c and test LED toggle.c are written to make system calls and toggle LED’s.

VI  CONCLUSION

LED’s switch on/off could be controlled using syscall. though the toggle state made the LED’s continiously on this may be due to the loss of cuurent state information when LED toggle is used.