backup: Syscalls and problems encountered
In the previous article, we finished setting up the kernel environment. Now let’s try to write a system call to modify or read the nice value of a given process and return the latest nice value and priority prio of the process.
Successful method
This is a successful method and seems to be the one most people use
First, go to include/linux/syscalls.h
and add the following function prototype
After that, go to arch/x86/entry/syscalls/syscall_32.tbl
and arch/x86/entry/syscalls/syscall_64.tbl
respectively and add the system call number
ps:Make sure to add __ia32_
to syscall_32.tbl
and __x64_
to syscall_64.tbl
, just like the picture above, otherwise you may get an error like underfined reference to xxx
.
Add code in kernel/sys.c
After that just make -j8 bzImage
and wait for a few minutes
After compiling, write a demo to see if it works
Then use the script to make the rootfs.img and launch qemu
Let’s talk about this syscall
1 | SYSCALL_DEFINE5(lab1, pid_t, pid, int, flag, int, nicevalue, void __user *, prio, void __user *, nice) { |
There seems to be a lot of questions though…. Didn’t think about the range of nice values when I first wrote it (didn’t even know nice values had a range).
It’s an error way that somehow goes wrong
At first, I wanted to follow a tutorial on the Internet and try to write a method with parameters, similar to the one I wrote in the last blog about adding system calls.
Create lab1_v2
folder in the root of the source code and add lab1_v2.c
and Makefile
Modify the Makefile
file in the root directory of the source code
Complete the regular three-step include/linux/syscalls.h
, arch/x86/entry/syscalls/syscall_32.tbl
and arch/x86/entry/syscalls/syscall_64.tbl
Then compile the kernel make -j8 bzImage
Written a demo as usual
Then an error occurs when running
Probably something went wrong when passing parameters?
Related Source Code
find_get_pid(int nr)
1 | struct pid |
pid_task()
1 | //Find pid_task by process descriptor |
asmlinkage
Inform the compiler to extract the function parameters from the stack only, not from the registers, because the system has already pressed the parameter values passed through the registers into the kernel stack before executing the service routine
set_user_nice()
1 | void set_user_nice(struct task_struct *p, long nice) |
backup: Syscalls and problems encountered
http://aslin.site/2022/03/09/backup-Syscalls-and-problems-encountered/