LKD: Chapter 5 System Call
在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的更多相关文章
- LKD: Chapter 9 An Introduction to Kernel Synchronization
This chapter introduces some conception about kernel synchronization generally. Critical Regions: Co ...
- 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. ...
- 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 ...
- LKD: Chapter 6 Kernel Data Structures
这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees. Linked Lists:(<linux/list.h>) 在lin ...
- imx6 system boot
imx6开机启动就进入download模式,有的板子进入文件系统之后会进入download模式.查看datasheet,Chapter 8 System Boot查找原因,记录于此. freescal ...
- halcon算子
halcon的算子列表 Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样 ...
- halcon的算子列表
Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...
- s3c6410_MMU地址映射过程详述
参考: 1)<ARM1176 JZF-S Technical Reference Manual>: Chapter 3 System Control Coprocessor Chapter ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
随机推荐
- NDK Jni 开发(1)
1. 学习地址 http://my.oschina.net/lifj/blog/177087 http://www.cnblogs.com/devinzhang/archive/2012/02/29/ ...
- 张高兴的 Xamarin.Android 学习笔记:(三)活动生命周期
本文将直接解释我写的一个示例.示例目的在于展示 Android 活动在 Xamarin 中的用法.如果有朋友对基础知识不太了解建议先学 Android . 新建一个 Xamarin.Android 项 ...
- table插件
//动态添加一行function addRow(){ var firstrow=document.getElementById('firstrow'); var firstCopy=firstrow. ...
- [mysql使用(2)] mysql的一些语法与Oracle的差别
一.表空间 mysql的表空间有共享表空间和独占表空间,独占表空间,其实就是一张表一个表空间,其实也就是一张表一个数据文件,共享表空间似乎有点类似oracle的表空间,不同的表可以保存在同一个数据文件 ...
- opencv之从视频帧中截取图片
最近在训练一个人脸识别的模型,而项目训练需要大量真实人脸图片样本. 刚好项目用到opencv识别人脸,可以把每一帧图片保存下来,用此方法可以方便的获取大量的脸部样本,大约20分钟可以获取到10000张 ...
- HttpComponents 发送post get 请求
1.场景描述 使用Apache开源组织中的HttpComponents,完成对http服务器的访问功能. 2.HttpComponents项目的介绍 HttpComponents项目就是专门设计来简化 ...
- JAVA基础知识总结:四
一.方法 1.什么是方法? 对于功能相同的代码段,为了简化代码,会把功能相同的代码抽取出来,方便多次使用,Java中,我们使用[方法],也被称为函数 2.函数的声明 语法: 访问权限修饰符 其他修饰符 ...
- mysql 外键的几种约束
restrict方式 同no action, 都是立即检查外键约束 --限制,指的是如果字表引用父表的某个字段的值,那么不允许直接删除父表的该值: cascade方式 在父表上update/de ...
- Redis环境搭建
一.准备的安装包 windows虚拟机软件:VMware Workstation Pro 12 linux安装文件:CentOS-7-x86_64-Minimal-1511.iso 远程登录软件:pu ...
- mongo+mongoose+express
直接上指令: //*代表自定义名字 //使用数据库 use * //检查当前数据库 db //查询数据库列表 show dbs //查询当前数据库集合 show collections //插入文档自 ...