分享

documents:system

 astrotycoon 2013-08-29

Making system calls using kernel module

Building system calls in kernel module is more flexible than modifying kernel. When we want to use our system call, just install our kernel modules; and if we don’t need it right away, just remove modules. Modifying kernel is not necessary. (But you still need to modify your kernel for O.S. project one.)

Flow

Step 0.Download the latest kernel and compile it. Unzip it to /usr/src/

Step 1.Export sys_call_table For sys_call_table, your should extern it in a file such as <top directory to the kernel sources>/arch/x86/kernel/i386_ksyms_32.c.

note:

sys_call_table is read-only after kernel version 2.6.23. If you really want to try this method using kernel version which is higher than 2.6.23, you will have to modify your current kernel source and recompile it.

Firstly, check your compiled kernel version uname –a

In x86 32bit vim /usr/src/linux-2.6.x/arch/x86/kernel/ entry_32.S .section .rodata, “a” ?.section .data, “aw”

In x86 64bit vim /usr/src/linux-2.6.x/arch/x86/kernel/syscall_64.c line 22: delete the “const”

Add to export symbol vim /usr/src/linux-2.6.x/kernel/kallsyms.c extern void *sys_call_table; EXPORT_SYMBOL(sys_call_table);

Step 2. Write Your Makefile vim makefile vim myservices.c

Step 3.Write Kernel Module

Build your own module. You can put it in <top directory to your kernel sources>/drivers/misc/ 

Include and Define

Extern the “sys_call_table”

Write your own system call

Initialize the kernel module

Extern the “sys_call_table”

Step 4.Use Kernel Module

Compile

make

Insert the module to kernel

insmod ./myservice.ko

Remove the module from kernel

rmmod myservice

List the modules in kernel

lsmod

User program

Write an application to use your system call

vim ap.c

Compile and execute

gcc ap.c –o ap
./ap 10

Installing your module

If you success building your module, there will be a file named myservice.ko. This file is your kernel module.
Use insmod to install and rmmod to remove it.

insmod myservices.ko
rmmod myservices

Notification

  • If your module want to use the variable in kernel, you have to use the function EXPORT_SYMBOL() to export it.

ex: int a; EXPORT_SYMBOL(a); /* in the kernel */

  • printk can print messages in kernel, use dmesg to check.
  • <top directory to the kernel sources> means the directory that put your kernel sources. It is usually /usr/src/linux-xxxx.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多