syscall_add

案例 新增内核系统调用

案例目的

 理解linux内核系统调用

 掌握增加系统调用的方法

硬件设备

  PC机(X86兼容机)

 CW210开发板

软件环境

  Ubuntu12.04

  arm-none-linux-gnueabi-gcc

案例内容

在linux内核中增加新的系统调用,在用户空间编程实现对新增系统调用的使用。

案例步骤

1、进入内核源码根目录cd /opt/kernel

2、执行“vim arch/arm/kernel/sys_arm.c”,在文件最后增加

asmlinkageint sys_add(int x, int y)

{

        printk("call sys_add\n");

        return x+y;

}

asmlinkage:声明c语言函数给汇编语言函数传递参数,是通过栈而不是通过R0~R3寄存器!默认是通过寄存器来传递的!

3、执行vi arch/arm/include/asm/unistd.h,在“#define __NR_recvmmsg                   (__NR_SYSCALL_BASE+365)”后增加如下代码

#define __NR_add (__NR_SYSCALL_BASE+366)

4、执行“vi  arch/arm/kernel/calls.S”,在

“/*365 */       CALL(sys_recvmmsg)”后增加“CALL(sys_add)”。

5、在内核源码根目录下执行 make zImage

6、拷贝生成的镜像文件到tftp主目录

“cparch/arm/boot/zImage  /tftpboot/”

用新内核重新引导系统

7、创建测试调用该系统调用的应用程序newsyscall.c。

8、生成可执行文件

“arm-linux-gccnewsyscall.c -o newsyscall”

9、拷贝newsyscall到NFS目录。

10、在开发板终端执行“./newsyscall”

案例代码

newsyscall.c

 

#include<stdio.h>

#include <linux/unistd.h>

 

int main(int argc,char **argv)

{

     int ret;

 

     ret = syscall( 366, 100, 200 );

     printf("The result of new syscall: %d!\n", ret);

 

     return 0;

}

 

以下是一个简单的C++代码示例,使用LLVM框架中的API生成系统调用的IR: ```c++ #include <llvm/IR/LLVMContext.h> #include <llvm/IR/Module.h> #include <llvm/IR/IRBuilder.h> using namespace llvm; int main() { LLVMContext context; Module module("syscall_module", context); IRBuilder<> builder(context); // Define the function signature Type *syscall_args[] = {builder.getInt64Ty(), builder.getInt64PtrTy()}; FunctionType *syscall_type = FunctionType::get(builder.getInt64Ty(), syscall_args, false); // Declare the function in the module Function *syscall_func = Function::Create(syscall_type, Function::ExternalLinkage, "syscall", &module); // Create a basic block to contain the instructions BasicBlock *entry = BasicBlock::Create(context, "entry", syscall_func); // Add instructions to the basic block Value *syscall_num = builder.getInt64(1); Value *syscall_args_ptr = builder.getInt64PtrTy()->getPointerTo(builder.getInt64(0)); Value *syscall_result = builder.CreateCall(syscall_func, {syscall_num, syscall_args_ptr}); // Add return instruction to the basic block builder.CreateRet(syscall_result); // Print the IR code to stdout module.print(outs(), nullptr); return 0; } ``` 该代码创建了一个名为`syscall_module`的LLVM模块,并在其中声明了一个名为`syscall`的外部函数。接下来,它创建了一个名为`entry`的基本块,并在其中添加了一个调用`syscall`函数的指令,该指令传递了系统调用号和参数指针。最后,它添加了一个`ret`指令,将系统调用结果作为函数返回值。 输出的IR代码类似于以下内容: ``` ; ModuleID = 'syscall_module' source_filename = "syscall_module" define i64 @syscall(i64, i64*) { entry: %0 = call i64 @syscall(i64 1, i64* null) ret i64 %0 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值