如何写于Debian / Ubuntu系统调用 [英] How to write system calls on debian/ubuntu

查看:133
本文介绍了如何写于Debian / Ubuntu系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写我自己的系统调用。它只是返回当前时间。我知道我应该怎么办这个概念,我也经历了几个这样的链接:


  • 实现在Linux 2.6系统调用I386


  • <一个href=\"http://www.cs.rochester.edu/users/faculty/sandhya/csc256/assignments/adding-a-system-call.html\"相对=nofollow>另一个系统调用实现的


但我仍然困惑,并没有得到预期的效果。内核没有编译和其因问题崩溃。我曾尝试它3.X.X的debian的最新的稳定版

可能有人点我到一个干净的的Hello World 样的程序来开发系统调用?

修改

要回答以下中的,这里是我的问题:


  1. 文件3:Linux的X.X.X /弓/ 86 /内核/ syscall_table_32.S 不在我的Linux文件夹中找到。我只好即兴,因此修改了以下文件: Linux的X.X.X /弓/ 86 /系统调用/ syscall_64.tbl


  2. 以上(1)新文件提到过的&LT不同的模式;数字&GT; &LT; 64 / X32 /常见&GT; &LT;名称&gt; &LT;入口点&GT; 和我进入了'313共同


  3. 内核映像没有编译成功,但我不能调用该函数。它给出了一个未定义的引用的错误当我用gcc,为什么编译它?



解决方案

这仅仅是例子,如何写一个简单的内核系统调用。
考虑下面的C函数system_strcpy(),简单地复制一个字符串到另一个。)类似什么的strcpy(确实

 #包括LT&;&stdio.h中GT;长system_strcpy(字符* DEST,为const char * SRC)
{
   INT I = 0;
   而(SRC [I]!= 0)
      DEST [I] = SRC [我++];   DEST [I] = 0;
   返回我;
}

写作之前,获得一个内核源代码的焦油和解压它得到一个Linux X.X.X目录。

文件1:Linux的X.X.X /测试/ system_strcpy.c
在linux-XXX内,名为测试创建一个目录,并保存code作为文件 system_strcpy.c 中吧。

 #包括LT&; Linux的/ linkage.h&GT;
#包括LT&;的Linux / kernel.h&GT;
asmlinkage长system_strcpy(字符* DEST,为const char * SRC)
{
   INT I = 0;
   而(SRC [I]!= 0)
      DEST [I] = SRC [我++];   DEST [I] = 0;
   返回我;
}

文件2:Linux的X.X.X /测试/ Makefile文件
创建您在上面创建一个的Makefile 相同试验中目录,并把此行是:

OBJ-Y:= system_strcpy.o

文件3:Linux的X.X.X /弓/ 86 /内核/ syscall_table_32.S
现在,你有你的系统调用添加到系统调用表。
追加到文件中的以下行:

。长system_strcpy

注:对于内核3.3及更高版本

*请参阅:Linux的3.3.xx /弓/ 86 /系统调用/ syscall_64.tbl *

,并在那里,现在下面的一系列行的末尾添加:

310 64 process_vm_readv sys_process_vm_readv

311 64 process_vm_writev sys_process_vm_writev

312 64 KCMP sys_kcmp

313 64 system_strcpy system_strcpy

对于3.3版本的格式为:
数量 ABI 名称 切入点

文件4:Linux的X.X.X /弓/ 86 /有/ ASM / unistd_32.h

注:本节是多余的3.3和更高版本的内核

在这个文件中,所有的系统调用的名称将有一个唯一的号码相关联。最后的系统调用数对后,添加一行

的#define __NR_system_strcpy 338

(如果337是与系统中的呼叫号码对最后一个系统呼叫相关联的号码)。

然后替换 NR_syscalls 值,说明系统的总数在此情况下(加1已有数),即调用 NR_syscalls 应该已经338和新的值是339。

的#define NR_syscalls 339

文件5:Linux的X.X.X /在include / linux / syscalls.h

附加到文件中我们的函数的原型。

asmlinkage长system_strcpy(字符* DEST,字符* SRC);

之前的文件中的 #ENDIF 行。

文件6:在Makefile文件源目录的根目录

打开的Makefile 并找到行,其中核心-Y 定义,并添加目录测试到该行的末尾。

核心-Y + =内核/ MM / FS /测试/

现在编译内核。问题:
使bzImage的-j4

执行以下命令根(或root权限)安装内核:
使安装

重新启动系统。

要使用最近创建的系统调用的使用:

系统调用(338,DEST,SRC); (或系统调用(313,DEST,SRC); 的内核3.3+),而不是常规的strcpy 库函数。

 的#includeunistd.h中
#包括SYS / syscall.h
诠释的main()
{
 字符* DEST = NULL,* SRC =你好;
 DEST =(字符*)malloc的(strlen的(SRC)+1);
 系统调用(338,DEST,SRC); //系统调用(313,DEST,SRC);内核3.3+
 的printf(%S \\ n%S \\ n,SRC,DEST);
 返回0;
}

而不是像313等号码系统调用,您也可以直接使用 __ NR_system_strcpy

这是一个普通的例子。你需要做一个小实验,看看您的特定内核版本的作品。

I am trying to write a system call of my own. It would just return the current time. I know the concept of what should I do and I did go through a couple of links like these:

But I am still confused and have not got the desired result. The kernel is not compiling and its crashing due to problems. I have tried it on debian latest stable release of 3.X.X

Could someone point me out to a clean hello world kind of program to develop system calls?

EDIT

To the below answer, here are my problems:

  1. File 3: linux-x.x.x/arch/x86/kernel/syscall_table_32.S is not found in my linux folder. I had to improvise and so modified the following file: linux-x.x.x/arch/x86/syscalls/syscall_64.tbl

  2. The above (1) new file mentioned had different pattern of <number> <64/x32/common> <name> <entry point> and my entry was `313 common

  3. The kernel image did compile successfully, but I couldnt call the function. It gives an undefined reference" error when i compile it with gcc. Why?

解决方案

This is just example how to write a simple kernel system call. Consider the following C function system_strcpy() that simply copies one string into another: similar to what strcpy() does.

#include<stdio.h>

long system_strcpy(char* dest, const char* src)
{
   int i=0;
   while(src[i]!=0)
      dest[i]=src[i++];

   dest[i]=0;
   return i;
}

Before writing, get a kernel source tar and untar it to get a linux-x.x.x directory.

File 1: linux-x.x.x/test/system_strcpy.c Create a directory within the linux-x.x.x, named test and save this code as file system_strcpy.c in it.

#include<linux/linkage.h>
#include<linux/kernel.h>
asmlinkage long system_strcpy(char*dest, const char* src)
{
   int i=0;
   while(src[i]!=0)
      dest[i]=src[i++];

   dest[i]=0;
   return i;
}

File 2: linux-x.x.x/test/Makefile Create a Makefile within the same test directory you created above and put this line in it:

obj-y := system_strcpy.o

File 3: linux-x.x.x/arch/x86/kernel/syscall_table_32.S Now, you have to add your system call to the system call table. Append to the file the following line:

.long system_strcpy

NOTE: For Kernel 3.3 and higher versions.

*Refer:linux-3.3.xx/arch/x86/syscalls/syscall_64.tbl*

And in there, now add at the end of the following series of lines:

310 64 process_vm_readv sys_process_vm_readv

311 64 process_vm_writev sys_process_vm_writev

312 64 kcmp sys_kcmp

313 64 system_strcpy system_strcpy

The format for the 3.3 version is in: number abi name entry point

File 4: linux-x.x.x/arch/x86/include/asm/unistd_32.h

NOTE: This section is redundant for 3.3 and higher kernel versions

In this file, the names of all the system calls will be associated with a unique number. After the last system call-number pair, add a line

#define __NR_system_strcpy 338

(if 337 was the number associated with the last system call in the system call-number pair).

Then replace NR_syscalls value, stating total number of system calls with (the existing number incremented by 1) i.e. in this case the NR_syscalls should've been 338 and the new value is 339.

#define NR_syscalls 339

File 5: linux-x.x.x/include/linux/syscalls.h

Append to the file the prototype of our function.

asmlinkage long system_strcpy(char *dest,char *src);

just before the #endif line in the file.

File 6: Makefile at the root of source directory.

Open Makefile and find the line where core-y is defined and add the directory test to the end of that line.

core-y += kernel/ mm/ fs/ test/

Now compile the kernel. Issue: make bzImage -j4

Install the kernel by executing the following command as root(or with root permissions): make install

Reboot the system.

To use the recently created system call use:

syscall(338,dest,src); (or syscall(313,dest,src); for kernel 3.3+) instead of the regular strcpy library function.

#include "unistd.h"
#include "sys/syscall.h"
int main()
{
 char *dest=NULL,*src="Hello";
 dest=(char*)malloc(strlen(src)+1);
 syscall(338,dest,src);//syscall(313,dest,src); for kernel 3.3+
 printf("%s \n %s\n",src,dest);
 return 0;
}

Instead of numbers like 313,etc in syscall, you can also directly use __NR_system_strcpy

This is a generic example. You will need to do a little experimentation to see what works for your specific kernel version.

这篇关于如何写于Debian / Ubuntu系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆