在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   //这里的单位是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 ;
}

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

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

  成功调用了syscall:

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

LKD: Chapter 5 System Call的更多相关文章

  1. LKD: Chapter 9 An Introduction to Kernel Synchronization

    This chapter introduces some conception about kernel synchronization generally. Critical Regions: Co ...

  2. LKD: Chapter 8 Bottom Halves and Deferring Work

    In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. ...

  3. LKD: Chapter 7 Interrupts and Interrupt Handlers

    Recently I realized my English is still far from good. So in order to improve my English, I must not ...

  4. LKD: Chapter 6 Kernel Data Structures

    这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees. Linked Lists:(<linux/list.h>) 在lin ...

  5. imx6 system boot

    imx6开机启动就进入download模式,有的板子进入文件系统之后会进入download模式.查看datasheet,Chapter 8 System Boot查找原因,记录于此. freescal ...

  6. halcon算子

    halcon的算子列表   Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样 ...

  7. halcon的算子列表

    Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...

  8. s3c6410_MMU地址映射过程详述

    参考: 1)<ARM1176 JZF-S Technical Reference Manual>: Chapter 3 System Control Coprocessor Chapter ...

  9. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

随机推荐

  1. IDEA Maven 三层架构 2、运行 springMVC

    运行 SpringMVC 首先要理解 SpringMVC 应用程序的入口是配置文件 web.xml,其路径为"src/main/webapp/WEB-INF/web.xml",通过 ...

  2. nginx的反向代理功能和缓存功能

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  3. UVa816,Ordering Tasks,WA

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  4. 测试中出现ERROR StatusLogger No log4j2 configuration file

    概述 在hibernate框架搭建完成用log4j2进行测试时,总是出现ERROR StatusLogger No log4j2 configuration file found. Using def ...

  5. C 其他一些

    1.联合,语法 union{成员1;成员2;} 联合间的成员共享同一个空间,也就是说,成员1赋值如果没有被清除,会体现在成员2上 2.位字段,结合结构体瓜分完整的类型存储空间,如 struct{ ; ...

  6. Spark: Job in detail

    1.sc.runJob -> dagScheduler.runJob -> submitJob 2.DAGSchedueler::submitJob 会创建 JobSubmitted 的 ...

  7. 简易RPC框架-上下文

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. 用编写一个简单的记事本(C#实现)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. caffe源码分析 vector<Blob<Dtype>*>& bottom

    Blob:4个维度 n x c x h x w: bottom[0] .bottom[1]代表该层有几个输入. bottom[0]->count(): 输入中,元素的总维数(个数) bottom ...

  10. javascript中自定义事件

    自定义事件:用户可以指定事件类型,这个类型实际上就是一个字符串,然后为这个类型的事件指定事件处理函数,可以注册多个事件处理函数(用数组管理),调用时,从多个事件处理函数中找到再调用. function ...