在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. c语言构造类型之数组_01

    构造类型--constructed type.至于定义,笔者就省略了,有兴趣的同学可以百度搜索https://www.baidu.com/.今天我们要说的是c语言中最简单的构造类型--数组(array ...

  2. win10 uwp MVVM入门

    MVVM 是一个强大的架构,基本从 WPF 开始,wr(我说的就是微软)就提倡使用 MVVM.它可以将界面和后台分离,让开发人员可以不关心界面是怎样,全心投入到后台代码编写中. 然后在编写完后台代码后 ...

  3. win10 UWP 蜘蛛网效果

    我看见了知乎首页登录背景和普通的地球人写的博客,发现了个好看的效果. 那么我来告诉大家如何做这个效果. 第一步是在 Canvas 画点,第二步是让点移动,第三步是画线 在 Canvas 画一个点 我们 ...

  4. UVa1595,Symmetry

    这题居然是1A过的.....最近无比失落的心情顿时愉悦起来~ 将数据全部读入 先用二维数据来存储坐标(先把题做出来再说= =) 题目中的x,y的坐标范围是-1W到1W....在数组下标里是不能用负数保 ...

  5. 处理MySQL服务器无法启动的问题

    MySQL数据库在升级到5.7版本后,和之前的版本有些不一样,没有data文件夹,我们都知道MySQL数据库文件是保存在data文件夹中的,网上有人说把5.6版本的data文件夹拷贝一个,这种说法听听 ...

  6. 虚拟专用网VPN

    1. 三个专用地址块: (1)10.0.0.0到10.255.255.255 (2)172.16.0.0到172.31.255.255 (3)192.168.0.0到192.168.255.255 2 ...

  7. Element ui表格展示多张图片问题

    显示一张图片的方法: <el-table-column label="头像" width="100"> <template scope=&qu ...

  8. 最新数据库排行,Oracle略显疲惫

    9月份TOPDB Top Database Index排行榜出炉,TOPDB Top Database Index是根据数据库在谷歌上的搜索频率分析得出的,数据库被搜索的频率越大,表示数 9月份的TO ...

  9. K - Kia's Calculation (贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. Android 开发笔记___SD卡文件操作

    package com.example.alimjan.hello_world.Utils; import android.graphics.Bitmap; import android.graphi ...