码迷,mamicode.com
首页 > 其他好文 > 详细

LKD: Chapter 5 System Call

时间:2015-12-20 14:42:11      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

  在Linux中,处理器所作的事可以归纳为3种情况:

1、In user-space, executing user code in a process;

2、In kernel-space, in process context, executing on behalf of a specific process;

3、In kernel-space, in interrupt context, not associated with a process, handling an interrupt.

  而在user-space中想要和内核交互的唯一办法就是通过system call,简称syscall。

System Call Implementation:

  A meme related to interfaces in Unix is "Provide mechanism, not policy."

  参数检测:内核代码不应随意接受指向user-space的指针,此时必须使用一下两种方法中的一种来检测。

    1、写入user-space时,使用copy_to_user();

    2、从user-space读入时,使用copy_from_user()。

 

下面来尝试实现一个system call:

  1、首先在arch/x86/ia32/ia32entry.S最后添加上自己的syscall:

技术分享

  可见我们这里sys_foo的编号是337。

  2、在arch/x86/include/asm/unistd_32.h中定义syscall number:

技术分享

  这里出了点意外,不知下面怎么多了两行#ifdef .... #define ...,还把我的337给用了在前面添加我的#define __NR_foo   337后,将后面NR_syscalls的337改成338.  

  3、最后,我们在kernel/sys.c中定义实际的foo()这个syscall:

技术分享

  这就算完成了。

 

  接着编译安装内核:

# cd linux-2.6.32.68
# make menuconfig(按默认配置就行)
# make all(要等超级久的啊。。)
# make modules_install
# make install
# update-grub
# reboot

  结果编译不通过,才明白在第3步kernel/sys.c中添加的代码不应该在开头添加。将其移动到最后后,继续编译!

  可是昨晚已经弄到了23点多了,干脆睡觉去了。一早醒来提示说磁盘空间不够了。对于virtualbox的vdi映像,可以用如下命令扩容:

VBoxManage modifyhd "cloned.vdi" --resize 16000  //这里的单位是M

  但是我一直觉得centos不太满足我的要求,所以索性重装了个ubuntu 14.04,虚拟机就是方便!

 

  接下来我们要从user-space中调用这个syscall。我们通过linux提供的macros来包装这个syscall。

  这些macros命名为_syscalln(),其中最后一个n是指参数个数,在0到6间。

  新建文件test.c:

#define __NR_foo 337
__syscall0(long, foo)

int main()
{
    long stack_size;

    stack_size = foo();
    printf("The kernel stack size is %ld\n", stack_size);

    return 0;
}

  编译显示__syscall0那里出错了:unknown type name ‘foo‘

  网上查是说syscall0这个macro已经过时了,使用syscall()代替。

技术分享

  成功调用了syscall:

技术分享

  但是我虚拟机中的ubuntu不知怎么不能和宿主机共享剪贴板,干脆再装个32位的fedora23算了。

LKD: Chapter 5 System Call

标签:

原文地址:http://www.cnblogs.com/justforfun12/p/5058664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!