后面编译的是 4.2.2 与 4.14.14 在 gcc4.8 上编译4.14.14 时报不支持堆栈保护 反正都差不多就先编译了 4.2.2 再编译4.14.14 1.下载 linux 内核源码 2.解压放到 /usr/src 3.创建软连接 (不一定非得把文件放到 /usr/src 看个人我直接放桌面)

root@ubuntu:~# cd /usr/src
root@ubuntu:/usr/src# ls
linux-4.14.14           linux-headers-4.2.0-27-generic
linux-headers-4.2.0-27
root@ubuntu:/usr/src# ln -s linux-4.14.14 linux
root@ubuntu:/usr/src# 


4.安装工具(这些可以不要安装)

root@ubuntu:/usr/src# sudo apt-get install build-essential kernel-package libncurses5-dev fakeroot
sudo aptitude install libqt3-headers libqt3-mt-dev libqt3-compat-headers libqt3-mt

(如果找不到命令,请先sudo apt-get install aptitude) 5.内核修改

# cd /usr/include/ 
# rm -r asm linux scsi 
# ln -s /usr/src/linux/include/asm-generic asm 
# ln -s /usr/src/linux/include/linux linux 
# ln -s /usr/src/linux/include/scsi scsi 


root@ubuntu:/usr/src# cd /usr/include/
root@ubuntu:/usr/include# rm -r asm linux scsi
root@ubuntu:/usr/include# ln -s /usr/src/linux/include/asm-generic asm
root@ubuntu:/usr/include# ln -s /usr/src/linux/include/linux linux 
root@ubuntu:/usr/include#  ln -s /usr/src/linux/include/scsi scsi 
root@ubuntu:/usr/include#

6.添加新的系统调用 找到系统调用号的表(下面的linux 是一个刚才设置的软连接 ) 注意版本不同文件地方不同 linux-2.6.32.65 在文件:/usr/src/linux/arch/x86/kernel/syscall_table_32.S linux-3.5.4 在文件:/usr/src/linux/arch/x86/syscalls/syscall_64.tbl linux-4.14 在文件:/usr/src/linux/arch/x86/entry/syscalls/syscall_64.tbl 这里是 4.14.14 添加一个系统调用号 (548)

548     x32      hello                   sys_hello

声明系统调用服务例程 打开:include/linux/syscalls.h 也要注意版本 不同版本不同 添加

asmlinkage long sys_hello(const char __user *name);

定义系统调用服务例程

asmlinkage long sys_hello(const char __user *name)
{
    char *name_kd;
    long ret;
    name_kd = strndup_user(name, PAGE_SIZE);
    if (IS_ERR(name_kd))
    {
        ret = PTR_ERR(name);
        goto error;
        
    }
    printk("Hello, %s!\n", name_kd);
    ret = 0;
    error:
    return ret;
    
}

kernel/sys.c 只要不插函数 头包含中间 其它随便 开始编译内核: cd /usr/src/linux

root@ubuntu:/usr/include# cd /usr/src/linux
root@ubuntu:/usr/src/linux# ls
arch     Documentation  ipc          Makefile  security
block    drivers        Kbuild       mm        sound
certs    firmware       Kconfig      net       tools
COPYING  fs             kernel       README    usr
CREDITS  include        lib          samples   virt
crypto   init           MAINTAINERS  scripts
root@ubuntu:/usr/src/linux# 

输入 make mrproper 输入 make menuconfig (根据需要选择相关配置)可能报错 找不到linux/limits.h 直接安装apt-get install linux-libc-dev:amd64 来自 :https://blog.csdn.net/u014655590/article/details/38854555 可能又报错 scripts/kconfig/lxdialog/dialog.h:32: fatal error: curses.h: No such file or directory 解决: sudo apt-get install libncurses5-dev 出现 不想搞直接保存就好 来自:https://blog.csdn.net/mrzhouxiaofei/article/details/79140435 这里由于直接在 4.2.0 上编译 4.14.14 会报 gcc 不支持 所以先编译的 4.2.2 后面在编译的 4.14.14 调用号在 4.14.14 与 4.2.2上 都可以测试的 make 编译 开始编译 编译完后

root@ubuntu:~/Desktop/linux-4.2.2# ls
arch     crypto         fs       Kbuild   MAINTAINERS      modules.order   REPORTING-BUGS  signing_key.priv  tools    vmlinux-gdb.py
block    Documentation  include  Kconfig  Makefile         Module.symvers  samples         signing_key.x509  usr      vmlinux.o
COPYING  drivers        init     kernel   mm               net             scripts         sound             virt     x509.genkey
CREDITS  firmware       ipc      lib      modules.builtin  README          security        System.map        vmlinux
root@ubuntu:~/Desktop/linux-4.2.2# 


安装: make modules_install install 可能报错:要安装 sudo apt install libssl-dev

scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
scripts/extract-cert.c:21:25: fatal error: openssl/bio.h: No such file or directory
 #include <openssl/bio.h>
                         ^
compilation terminated.
make[1]: *** [scripts/extract-cert] Error 1
make[1]: *** Waiting for unfinished jobs....
  CC      arch/x86/kernel/asm-offsets.s
make[1]: *** [scripts/sign-file] Error 1
  HOSTLD  scripts/mod/modpost
make: *** [scripts] Error 2
make: *** Waiting for unfinished jobs....
  CHK     include/generated/asm-offsets.h
  UPD     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
root@ubuntu:~/Desktop/linux-4.14.14# sudo apt install libssl-dev

修改 grub 文件 gedit /etc/default/grub 注释掉: #GRUB_HIDDEN_TIMEOUT=0 更新 sudo update-grub2

root@ubuntu:~/Desktop/linux-4.2.2# gedit /etc/default/grub
root@ubuntu:~/Desktop/linux-4.2.2# sudo update-grub2
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.2.2
Found initrd image: /boot/initrd.img-4.2.2
Found linux image: /boot/vmlinuz-4.2.0-27-generic
Found initrd image: /boot/initrd.img-4.2.0-27-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done
root@ubuntu:~/Desktop/linux-4.2.2# 

更换内核: 以下为 4.14.14 调用:

#include <sys/syscall.h>
 #include <stdio.h>
 #include <unistd.h> 
int main()
{ 
long ret = syscall(548, "lw");
printf("ret: %d\n", (int)ret);
return 0;
 }

root@ubuntu:~/Desktop# g++ allen.c -o test
root@ubuntu:~/Desktop# ./test lw
ret: 0
root@ubuntu:~/Desktop# dmesg

ation="profile_load" profile="unconfined" name="/usr/lib/telepathy/mission-control-5" pid=987 comm="apparmor_parser"
[   20.827182] audit: type=1400 audit(1536418840.947:23): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*" pid=987 comm="apparmor_parser"
[   20.829930] audit: type=1400 audit(1536418840.951:24): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*//pxgsettings" pid=987 comm="apparmor_parser"
[   20.832445] audit: type=1400 audit(1536418840.955:25): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*//sanitized_helper" pid=987 comm="apparmor_parser"
[   23.628220] init: plymouth-upstart-bridge main process ended, respawning
[   23.656900] init: plymouth-upstart-bridge main process (1266) terminated with status 1
[   23.656918] init: plymouth-upstart-bridge main process ended, respawning
[  421.177187] Hello, lw!
root@ubuntu:~/Desktop# 

参考: https://www.jianshu.com/p/b2d5fa8af581 https://www.jianshu.com/p/a6c33bf39067