当前位置:网站首页>Add system calls by compiling the kernel

Add system calls by compiling the kernel

2022-06-21 14:46:00 User 6978604

Ubuntu edition :20.04 LTS Virtual machine configuration : Hard disk :40 GB Memory :4 GB Kernel version :5.4.0.26-generic VMware edition :15.5.0

Preparation

You need some libraries to compile , So install it in advance :

sudo apt-get install libncurses5-dev
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install libssl-dev
sudo apt-get install 
sudo apt-get install build-essential
sudo apt-install kernel-package

So much ?? ok , It doesn't have to be all , part Linux The distribution may come with , You can skip this step first , Do the next thing first , The premise is that you should do a good job first and show up halfway error To prepare ! If an error occurs halfway, you can install it according to the prompt .

Check your kernel version

uname -a

My is 5.4.0.26-generic

Download the kernel

Try to download a kernel similar to your own , Avoid incompatibility caused by too large version span . Here's the URL

http://cdn.kernel.org/pub/linux/kernel

I downloaded it linux-5.3.9.tar.xz

Unzip the kernel file

First, move the above kernel file to /usr/src/ Under the table of contents

stay /usr/src/ Open the terminal in the directory

decompression

sudo xz -d linux-5.3.9.tar.xz

Unpack

sudo tar -xvf linux-5.3.9.tar

Add functions

After unpacking, a linux-5.3.9 Folder , Enter the folder kernel In the folder

cd linux-5.3.9/kernel

edit sys.c file , Add the function at the end :

asmlinkage long sys_hello(void){
    prink("hello syscall!");
    return 1;
}

Add function declaration

Get into /usr/src/linux-5.3.9/arch/x86/include/asm

edit syscalls.h, Join us in sys_hello function :

Assign a to our system call id

open /usr/src/linux-5.3.9/arch/x86/entry/syscalls/syscall_64.tbl,

Be sure not to conflict with what you already have

I added

548 64 hello sys_hello

Configure the kernel

Enter into /usr/src/linux-5.3.9 Under the table of contents , Execute sequentially

sudo make mrproper
sudo make clean
sudo make menuconfig

If you execute sudo make menuconfig When the following error occurs

Full screen virtual machine ( Fill up the screen )

Then the page appears , direct save, then exit.

Then we can compile our kernel with the following commands

sudo make

The process takes about a few hours , Anyway, I put it backstage , Then go to bed first ~~.

however ! When I woke up, I found , The computer went to sleep automatically ! A wasted night , So remind to turn off the hibernation mode of the computer before compiling . In addition, the compilation process will produce a large number of intermediate files , It is recommended to give enough hard disk 40 GB

The above information indicates that the compilation is successful .

Generate modules

sudo make modules

This process also takes some time .

install modules

sudo make modules_install

Install the kernel into the system

sudo make install

Restart and test

Restart the process to enter advanced options

Just press... When restarting ESC( The Internet says yes ESC, But mine is a long press Shift

Choose advanced options , Then select the new kernel

If the process of entering the kernel occurs kernel panic not syncing:System is deadlocked on memory error , Just increase the memory .

Create a new test source program testMyCall.c

#include<stdio.h>
#include<sys/syscall.h>
#include<unistd.h>
#include<stdio.h>
int main(){
    // syscall  You need to pass a system call number 
    long int myRetutn = syscall(548);
    printf("syscall return: %ld",myRetutn);
    return 0;
}

Compile operation

gcc testMyCall.c -o test
./test
syscall return: 1

We found no output hello syscall!, Because of the log output level problem , Just call dmesg Command is enough .

原网站

版权声明
本文为[User 6978604]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211444493183.html