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 ...
随机推荐
- commonjs模块和es6模块的区别
commonjs模块与es6模块的区别 到目前为止,已经实习了3个月的时间了.最近在面试,在面试题里面有题目涉及到模块循环加载的知识.趁着这个机会,将commonjs模块与es6模块之间一些重要的的区 ...
- sqlite3基本相关使用
闲来无事,复习和总结了一下之前学习到的关于sqlite3数据库的相关知识: [1] sqlite3的安装:1.离线安装:sudo dpkg -i *.deb2.在线安装:sudo apt-get in ...
- php实现伪静态的方法
mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面.下面我详细说说它的使用方法 1.检测Apache是否支持mod_rewrite 通过php提供的phpinfo()函数查 ...
- faster-rcnn中ROI_POOIING层的解读
在没有出现sppnet之前,RCNN使用corp和warp来对图片进行大小调整,这种操作会造成图片信息失真和信息丢失.sppnet这个模型推出来之后(关于这个网络的描述,可以看看之前写的一篇理解:ht ...
- Nginx-OpenResty安装配置
上两篇中介绍了: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 Ngnix技术研究系列2-基于Redis实现动态路由 发现,应该加一篇OpenResty的安装部署说明,方便大家按图索骥 ...
- LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- linux虚拟机局域网网卡配置
1:配置虚拟机 1-1:打开:虚拟机下编辑->虚拟网络编辑器 选择VMnet信息下的桥接模式,在“桥接到”下拉列表里选择自己的网卡. ...
- Codeforces Round #378 (Div. 2)-C. Epidemic in Monstropolis
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- .NET Core跨平台的奥秘[上篇]:历史的枷锁
微软推出的第一个版本的.NET Framework是一个面向Windows桌面和服务器的基础框架,在此之后,为此微软根据设备自身的需求对.NET Framework进行裁剪,不断推出了针对具体设备类型 ...
- 在centos上安装jenkins
摘要: 本篇介绍了如何在linux服务器上安装jenkins 一:使用war安装 官网地址:https://jenkins.io/doc/ Guided Tour This guided tour w ...