Add a syscall to kernel and replace linux kernel of RPi.


Prepare:

  1. Cross compiler
  2. Linux Kernel for RPi

Reference:

Official guide


Firstly, get the latest kernel:

git clone https://github.com/raspberrypi/linux

Assume that the kernel directory is 'linux' and you have already installed cross-compiler tool.


Secondly, modify the source to add a simple syscall.

1. linux/arch/arm/kernel/sys_arm.c

Add syscall definition.

In order to simplify the process, we add the definition directly in the source that exists, so that we need not to modify the Makefile. And sys_arm.c is what we need.

Add the following definition to sys_arm.c

asmlinkage long sys_mysyscall(int num)
{
printk("My syscall with argument: %d\n",num);
return 0;
}

There is a second file(Call.S) in the same directory that we need to modify. But, in order to make the process more clear, we change another file first.

2.linux/arch/arm/include/unistd.h

Add macro define of our syscall to this file.

In this file, __NR_SYSCALL_BASE define the base address of syscall. And we will use this macro to define the address of our own function. Like this:

#define __NR_mysyscall (__NR_SYSCALL_BASE+223)

We use the 223th address, because this address is unused.

3.linux/arch/arm/kernel/call.S

Bind the definition and the address of our syscall function.

We have function definition in sys_arm.c and function address in unistd.h. Then we should tell the system, these two is associated.

Add this line in the file:

CALL(sys_mysyscall)

Be sure that this line is added in the 223th entry.

4. linux/include/linux/syscalls.h

Add the declaration of the syscall.

We must let system know 'ther is' a syscall 223. As we usually do, add the feclaration of the function to *.h:

asmlinkage long sys_mysyscall(int num);

Now the syscall is added in the linux kernel. Begin to compile the kernel.


Compile kernel

Personally, I create a new directory kernel-build for output.

That is /home/darren/opt/raspberry/kernel-build. It is not necessary.

Clean

Firstly, clean the project.

#Do you know that who is Mr.Proper? Ha...
make mrproper

Configure

Secondly, configure for your Raspberry.

There are some differences between RPi1 and RPi2. But luckily, the official offer us a template. We need not to do this by our own.

#RPi1
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
O=/home/darren/opt/raspberry/kernel-build bcm_defconfig #RPi2
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
O=/home/darren/opt/raspberry/kernel-build bcm2709_defconfig

Okay, that is so easy...

Make

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
O=/home/darren/opt/raspberry/kernel-build -j 12

Here '-j n' is the number of thread (Is it right? ). To speed up, let it be the 1.5 * the number of processors of your pc.

And you may know the nomber of processor by

cat /proc/cpuinfo | grep processor | wc -l

Install

Insert the sd card to computer. You may get two directories--root and boot.

Denote them like these two:

/media/boot/
/media/root/

Then run this command:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- O=/home/darren/opt/raspberry/kernel-build -j 12 INSTALL_MOD_PATH=/media/root/ modules

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- O=/home/darren/opt/raspberry/kernel-build -j 12 INSTALL_MOD_PATH=/media/root/ modules_install

Your could replace the kernel.img(or kernel7.img for RPi2) with linux/arch/arm/boot/Image

cp linux/arch/arm/boot/Image /media/boot/

Reboot and all is well.


Syscall

Finally, write a function to call our function:

void inline_asm(int num)
{
asm volatile (
"mov r7, #223\n" //系统调用号
"mov r0, %[value]\n" //参数
"svc #0\n" //监督调用
:: [value] "r" (num) //return 留空,并将 num 作为传入参
);
} int main()
{
int num = 10;
syscall(223, num); //直接使用 223 号系统调用
num = num << 2;
inline_asm(num);
return 0;
}

Add a Syscall的更多相关文章

  1. golang调用c++的dll库文件

    最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...

  2. Go win32 - 1

    上次说到,我们的GO可以执行系统调用,嘿嘿 不假,但如果你认为你已经掌握了,哈哈,那么不然 网上的例子,总是不深入,不彻底,除非是官网上的demo,也就是说只有设计者才知道告诉你什么才是它设计的正真意 ...

  3. 栈溢出之rop到syscall

    当程序开启了nx,但程序有syscall调用的时候.这时栈溢出的利用就可以通过rop来执行syscall的59号调用execve('/bin/sh',null,null),这是这次alictf一道pw ...

  4. syscall to rop

    前言 hitcon 2017 的 start 题,比较简单,练练手. 题目链接: https://gitee.com/hac425/blog_data/tree/master/hitcon2017 正 ...

  5. [Fw]How to Add a System Call(Fedora Core 6 Kernel : 2.6.18)

    How to Add a System Call Kernel : 2.6.18編譯環境 : Fedora Core 6 假設要加的system call為 sys_project, 有一個int的輸 ...

  6. AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...

  7. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  8. ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0

    ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...

  9. Syscall,API,ABI

    系统调用(Syscall):Linux2.6之前是使用int0x80(中断)来实现系统调用的,在2.6之后的内核是使用sysentry/sysexit(32位机器)指令来实现的系统调用,这两条指令是C ...

随机推荐

  1. Python中的高级特性

    1.切片.使用“[”和“]”即可,类似Matlab,可以切list,tuple,字符串等. 2.迭代.Python内置的enumerate函数可以把一个list变成索引-元素对. 3.列表生成式.列表 ...

  2. SpringMVC介绍之Validation

    对于任何一个应用而言在客户端做的数据有效性验证都不是安全有效的,这时候就要求我们在开发的时候在服务端也对数据的有效性进行验证.SpringMVC自身对数据在服务端的校验有一个比较好的支持,它能将我们提 ...

  3. 摘要: Linux下which、whereis、locate、find命令的区别

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了,所以放到这里方便使用. which    ...

  4. Ext之ExtGrid增删改查询回顾总结

    学习Ext已经有些许时间了,发现实际运用过程中ExtGrid系列还是最为常用的,本来想自己写些话语来总结的,无意间看到有位仁兄早就总结了,故冒犯贴在此处,以便以后翻阅,还望见谅 Ext - Grid  ...

  5. JVM中对象的创建过程

    JVM中对象的创建过程如以下流程图中所示: 对其主要步骤进行详细阐述: 为新生对象分配内存: 内存的分配方式: 指针碰撞:假设Java堆中内存是绝对规整的,所有用过的内存放在一边,空闲的内存在另一边, ...

  6. Ubuntu 14.04 安装pdf阅读器

    1. 个人推荐 okular. 关于安装okular的原因,可以很好的做到护眼功能. Ubuntu 14.04 自带的阅读器,因为白色太刺眼,长时间使用对眼睛不好. 对于,长时间编程的朋友们习惯夜间模 ...

  7. javascript平时小例子⑨(小型抽奖功能)

    <!doctype html><html lang="en"> <head> <meta charset="utf-8" ...

  8. Unity学习疑问记录之图片画质

    http://blog.csdn.net/candycat1992/article/details/22794773

  9. CSS3的高级特性

    CSS3对响应式设计非常有用:使用CSS3替代图片,在有带宽限制的网页中可有效减少http请求(从而使网页加载更快),并可使网页更灵活.更容易维护. 在开发CSS3时,要记住添加相关的浏览器私有前缀以 ...

  10. 每天php函数 - 数组最后一个元素取出

    复制代码代码如下: $array=array(1,2,3,4,5);    echo $array[count($array)-1];//计算数组长度,然后获取数组最后一个元素,如果数组中最后一个元素 ...