正点原子RK3568学习日志10-向系统条件一个系统调用
1.基础:将驱动编译进内核 实验将驱动编译进内核的基础上
在实验:将驱动编译进内核的基础上,
绝对路径使用交叉编译器
/opt/atk-dlrk356x-toolchain/bin/aarch64-buildroot-linux-gnu-gcc -v
1.编译驱动进内核
字符驱动目录kernel/drives/char/
2.新建目录,03_helloworld, 包含helloworld.c Makefile Kconfig
3. helloworld.c写你自己的程序#include <linux/module.h> #include <linux/init.h>static int helloworld_init(void) {printk("hello world\n");return 0; }static void helloworld_exit(void) {printk("goodbye world\n"); }module_init(helloworld_init); module_exit(helloworld_exit);MODULE_LICENSE("GPL"); MODULE_AUTHOR("qiuiuiu"); MODULE_VERSION("V1.0"); MODULE_DESCRIPTION("A simple hello world module")
4.Kconfig config后面那个名字是自己定义的,决定了你Makefile里的名字
config HELLO tristate "hello world" helphello hello
三个状态: *驱动编译进内核 M驱动编译成模块 无就是什么都不干
*驱动编译进内核 helloworld.o 直接烧写镜像
M驱动编译成模块 helloworld.ko adb到rk3568上用insmod看 或者烧写到ubuntu上insmod查看,都可以或者
5.Makefileobj-$(CONFIG_HELLO)+=helloworld.o
或者
6.Kconfig要和 char目录(上一级目录)产生联系,在上一级目录 vim Kconfigsource “drivers/char/hello/Kconfig”
或者
7.Makefile 也要和 char目录(上一级目录)产生联系 在上一级目录 vim Makefile这个是你目录名字
obj-y += hello/
或者
8.查看 得加环境变量
- 查看到kernel目录 make ARCH=arm64 menuconfig 查看有没有Kconfig 注意保存
- 查看到kernel目录 vim .config 里面就是我的变量
9.覆盖加编译 kernel目录下 vim build.sh 这个是根目录一些东西make savedefconfig cp defconfig arch/arm64/configs/rockchip_linux_defconfig cd ../ ./build.sh kernel
2.实验:添加系统调用
系统调用 接口 服务员 在Linux源码kernel/include/uapi/asm-generic/unistd.h文件中添加系统调用号
syscall 根据系统调用号
宏定义 kernel/include/linux/syscalls.h 参数不同而已
1.在上面的基础上实验,也就是说只要修改helloworld.c
2.开始写系统调用helloworld.c
#include <linux/kernel.h> #include <linux/syscalls.h> SYSCALL_DEFINE0(helloworld){printk("This is helloworld syscall\n");return 0; }
3.添加系统调用号 kernel/include/uapi/asm-generic/unistd.h 注意改名字
4.编译./build.sh kernel
3.测试验证:用insmod命令
1.自己insmod来进行测试,就是insmod那个实验
2.syscall.c
执行交叉编译命令
aarch64-rockchip1031-linux-gnu-gcc syscall.c -o syscall.o
#include <stdio.h> #include <sys/syscall.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h>#define __NR_helloworld 435int main(int argc,char **argv){syscall(__NR_helloworld);return 0; }
3.然后boot.img烧写到rk3568, 这个syscall.o程序adb到rk3568
运行./syscall
就可以查看了
4.问题:
1.#undef __NR_syscalls 取消定义
2.在C语言中,当 static 用于修饰一个函数时,它的作用是将该函数的可见性(作用域)限制在当前这一个文件内部。
3.
4.向系统中添加一个系统调用 最多带6个参数的系统调用。 unisd.h 添加自己的系统调用