Add a system call in MIPS Linux. Take Linux kernel 2.6.18 as an example.

Source: Internet
Author: User

To add a system call, follow these steps:
1. Add your system call entry to the system call table sys_call_table of the kernel.

Select the file to be modified based on the number of kernel bits (32/64 bits:
ARCH/MIPS/kernel/scall32-o32.S ------> 32bit Kernel
ARCH/MIPS/kernel/scall64-64.S ------> 64bit Kernel

64bit kernel, if compatible with 32 bit Abi (o32, n32), you also need to modify scall64-o32.S or scall64-n32.S
Taking 32bit kernel as an example, find macro definition in arch/MIPS/kernel/scall32-o32.S

. Macro syscalltable/* system call number starting from 4000 */
......
......
Sys sys_faccessat 3/* 4300 */
Sys sys_pselect6 6
Sys sys_ppoll 5
Sys sys_unshare 1
Sys sys_splice 4
Sys sys_sync_file_range 7/* 4305 */
Sys sys_tee 4
Sys sys_vmsplice 4
Sys sys_move_pages 6
Sys sys_set_robust_list 2
Sys sys_get_robust_list 3/* 4310 */
Sys sys_ni_syscall 0
Sys sys_ni_syscall 0
Sys sys_ni_syscall 0
Sys sys_ioprio_set 3
Sys sys_ioprio_get 2/* 4315 */
Sys sys_wenxy_test 0/* system call ID is 4316, wenxy add, 20091120 */
. Endm
......

Finally, the custom system call sys_wenxy_test is added to sys_call_table, and sys is used as a helper macro. The value 0 after the call name indicates the number of parameters required for the system call.

It can only be added at the end of sys_call_table. Otherwise, standard system calls will be disrupted.

2. Implement system calls

You can provide an implementation in arch/MIPS/kernel/syscall. C, for example:
......
/* Wenxy add begin, 20091119, A.M .*/
# Include "/opt/bcm97405/kernel/stblinux-2.6.18/ARCH/MIPS/SiByte/CFE/cfe_api.h"
Asmlinkage int sys_wenxy_test (void)
{
Uint64_t cfe_ept, cfe_handle;
Int N;
// N = cfe_init (cfe_handle, cfe_ept );

Printk (kern_emerg "kernel, wenxy Debug: into [% s], call [% s ()]/n", _ file __, _ function __);
Return 100;
}
/* Wenxy add end */

3. Modify include/ASM-MIPS/unistd. h.

Modify the macro definition based on your kernel bit and the supported Abi version. Modify the 32bit kernel:

# DEFINE _ nr_linux_syscils 320 ---------> 321 (the number of Linux syscils)
# DEFINE _ nr_o32_linux_syscils 320 ---------> 321 (the number of Linux o32 syscils)

Add the corresponding macro set to the end:
.......
# DEFINE _ nr_ppoll (_ nr_linux ++ 302)
# DEFINE _ nr_unshare (_ nr_linux ++ 303)
# DEFINE _ nr_splice (_ nr_linux ++ 304)
# DEFINE _ nr_sync_file_range (_ nr_linux + 305)
# DEFINE _ nr_tee (_ nr_linux ++ 306)
# DEFINE _ nr_vmsplice (_ nr_linux + 307)
# DEFINE _ nr_move_pages (_ nr_linux + 308)
# DEFINE _ nr_set_robust_list (_ nr_linux + 309)
# DEFINE _ nr_get_robust_list (_ nr_linux + 310)
# DEFINE _ nr_ioprio_set (_ nr_linux + 314)
# DEFINE _ nr_ioprio_get (_ nr_linux + 315)

/* Wenxy add begin, 20091119, A.M .*/
# DEFINE _ nr_wenxy_test (_ nr_linux + 316)
/* Wenxy add end */

......

4. test the new system call wenxy_test
Test. c file, C program:
# If 1
# Include <stdio. h>
# Include <Linux/unistd. h>
# Include <sys/syscall. h>
# Include <errno. h>
# Else
# Include "/opt/bcm97405/kernel/stblinux-2.6.18/include/Linux/unistd. H"
# Include "/opt/bcm97405/kernel/stblinux-2.6.18/include/sys/syscall. H"
# Endif

// # DEFINE _ nr_getjiffies 320
// # DEFINE _ nr_wenxy_test 319
# DEFINE _ nr_wenxy_test (_ nr_linux + 316)

_ Syscall0 (INT, wenxy_test );

Int main ()
{
Long jiffies;
Int N;
// Jiffies = syscall (_ nr_getjiffies );

// Printf ("Current jiffies is % LX/N", jiffies );

N = 0;
// N = syscall (_ nr_wenxy_test );
N = wenxy_test ();
Printf ("wenxy: syscall return value is [% d], system call ID = [% d]/n", N, _ nr_wenxy_test );

Return 0;
}
Command for compiling test. C: mipsel-Linux-gcc-I/opt/bcm97405/kernel/stblinux-2.6.18 test. c

Or write the assembler yourself:
Run the following program to test the re-compiled kernel:

. Text
. Globl main
. Ent main

Main:

Li $2, 4316/* system call number of wenxy_test */
Syscall

. End main

The system call convention in MIPs is as follows:

V0: used to set the system call number
A0 ~ A3: set the first four parameters.
Syscall System Call trigger command

Compile GCC cat. S-o a. Out

Run./A. Out

Correct output should be as follows:
# A. Out
Kernel, wenxy Debug: into [ARCH/MIPS/kernel/syscall. C], call [sys_wenxy_test ()]
Wenxy: syscall return value is [100], system call ID = [4316]

PS.
A. You can use dmesg to view kernel logs or use strace a. Out to track system call Information.
B. References:
Http://www.ibm.com/developerworks/cn/linux/l-system-calls/index.html
Http://www.diybl.com/course/3_program/hb/hbjs/200798/70400.html #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.