zoukankan      html  css  js  c++  java
  • Linux 内核编译 添加系统调用

    主机:Ubuntu 11.04

    现在内核版本:2.6.38.9

    编译的目标内核版本:2.6.39

    编译成功后修改内核,添加个系统调用,并重新编译内核,编写C程序,调用自己添加的系统调用

    首先编译内核

    1、从下载Linux内核版本2.6.39

    2、解压内核到任意目录(这里不用必须解压到/usr/src/下,Linus也说不必那样做,况且自己也试验成功)

    3、进入内核目录

    4、首先配置make menuconfig配置裁剪内核(如果没有什么必要可以复制/boot/config-***,这是现在的内核配置)

    cp /boot/config-***  ./.config

    5、开始编译,可以执行make all,当然也可以分步完成

    make dep

    make clean

    make bzImahe

    make modules

    6、安装内核驱动

    make modules_install

    7、安装内核

    make install

    8、生成引导镜像文件,并将其复制到/boot目录下

    mkinitramfs -o initrd.img-2.6.39 2.6.39

    cp initrd.img-2.6.39

    9、最后更新grub

    update-grub2

    启动新的内核,成功。

    下面是添加系统调用。

    1、首先打开文件./arch/x86/include/asm/unistd_32.h

    在最后添加系统调用号,如下:

    #define __NR_name_to_handle_at 341

    #define __NR_open_by_handle_at  342

    #define __NR_clock_adjtime 343

    #define __NR_syncfs             344

    #define __NR_my_call           345 /*This is added by yan,this is a test for system call*/

    2、打开./arch/x86/kernel/syscall_table_32.S

    末尾添加系统调用的函数地址项,如下:

    .long sys_prlimit64 /* 340 */

    .long sys_name_to_handle_at

    .long sys_open_by_handle_at

    .long sys_clock_adjtime

    .long sys_syncfs

    .long sys_my_call

    3、打开./include/linux/syscalls.h添加系统调用函数的声明

    asmlinkage long sys_my_call(int num); /*This is added by yan*/

    注意添加位置,因为其中有选择编译#ifdef和#ifndef

    4、打开./kernel/sys.c文件添加

    1. SYSCALL_DEFINE1(my_call,int,num)  
    2. {  
    3. prink("This is a sample test output from kernel!/n/tAnd this is added by yan/n/t");  
    4. return num;  
    5. }  
     

    这函数是宏定义,在./include/linux/syscalls.h里。

    5、重新编译内核,这次只需编译内核,驱动部分没有改变不用再编译一遍,以节省时间。

    执行make bzImage,然后参考上面的内核编译步骤即可。

    测试代码:

    1. #include <stdio.h>   
    2. #include <unistd.h>   
    3. #include <syscall.h>   
    4. #define __NR_my_sys_call  345   
    5. int main(void)  
    6. {  
    7.     int input;  
    8.     printf("Input a num");  
    9.     scanf("%d",&input);  
    10.     long a=syscall(__NR_my_sys_call,input);  
    11.     printf("The return from kernel is %ld",a);  
    12.     return 0;  
    13. }  
     

    编译执行

    结果如下:

  • 相关阅读:
    关于这个 blog
    P6499 [COCI2016-2017#2] Burza 题解
    CF1172F Nauuo and Bug 题解
    CF1479D Odd Mineral Resource 题解
    CF1442E Black, White and Grey Tree 题解
    CF1442D Sum 题解
    CF1025D Recovering BST 题解
    CF1056E Check Transcription 题解
    CF1025F Disjoint Triangles 题解
    红包算法的PHP实现
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6332888.html
Copyright © 2011-2022 走看看