Linux系统调用--系列一(添加)

系统调用–系列一(添加)

序言

    最近在研究Linux Kernel, 发现系统调用的整个过程不是很清楚, 决定自己动手弄懂它. 这次先添加一个系统调用, 把整个过程撸一遍.

 环境

system: Ubuntu 12.04 lts 32bit
tools: vim, make, apt-get ...

过程

1. 下载源码

apt-get source linux-image-$(uname -r)

2. 添加声明

/*
 * file : arch/x86/syscalls/syscall_32.tbl
 */
352     i386    sched_getattr           sys_ni_syscall
353     i386    renameat2               sys_ni_syscall
354     i386    seccomp                 sys_seccomp
// add a system call, which printf a string "hello, world"
355     i386    hello                   sys_hello

/*
 * file : include/linux/syscalls.h
 */
asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,
                            const char __user *uargs);
//added
asmlinkage long sys_hello(void);
#endif

3. 系统调用实现

在源代码根目录创建目录hello

/*
 * create a file, named syscalls.c
 */
 #include <linux/kernel.h>
 #include <linux/syscalls.h>
 asmlinkage long sys_hello(void){
   printk("hello, world\n");
   return 0;
 }

对应Makefile

/*
 * filename: Makefile
 */
 obj-y := syscalls.o

4.配置源代码根目录下的Makefile

/*
 * 大约785行
 */
-  core-y          += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
+  core-y          += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/

5. 编译, 安装

make -j4
make modules
make modules_install install
make install

之后选择你安装的内核重新启动系统.
6. 测试

/*
 * create a file: hello.c
 */
 #include <stdio.h>
 #include <linux/kernel.h>
 #include <sys/syscall.h>
 #include <unistd.h>

 int main(){
   long int result = syscall(355);
   printf("System call: %ld\n", result);
   return 0;
 }

编译:

gcc test.c -o test

运行:

./test
System call sys_hello returned 0

执行dmesg

[83996.842542] hello, world
[84477.164610] hello, world

问题

    Ubuntu 12.04启动菜单上面没有内核选择选项, 这里需要我们手动修改一下/etc/default/grub

/*
 * filename: /etc/default/grub
 */
GRUB_DEFAULT=0
#将下面一行注释掉, 执行update-grub命令
# GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值