惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
WordPress大学
WordPress大学
雷峰网
雷峰网
L
LangChain Blog
Y
Y Combinator Blog
I
Intezer
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
P
Privacy International News Feed
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
AI
AI
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
K
Kaspersky official blog
U
Unit 42
S
Schneier on Security
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
T
Tor Project blog
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
美团技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
爱范儿
爱范儿
F
Fortinet All Blogs
有赞技术团队
有赞技术团队

博客园 - 靖意风

使用QT 将可执行程序 进行打包 对使用的屏幕的整理 对mmc 设备进行分区 开发板启动时间优化 nmcli 设置网络配置 虚拟机下 安装 ubuntu 18.04 硬件基础知识__串口 字节对齐问题和 小端格式 在ubuntu下 编译23位的测试文件 31_了解 x6818开发板是 如何使用/dev/ttySAC0 进行打印log 10_linux dirver platform 框架 ubuntu24.04.02 下安装软件 记录 ubuntu 更新源 vmware 虚拟机的三种网卡 vmware 安装 ubuntu 24.04.02 07_linux 字符设备了解 05_使用linux内核的gpio库函数 03_uboot 命令整理 01_stm32 裸板程序 01_了解嵌入式开发
04_kernel编程框架和 printk传参
靖意风 · 2025-06-01 · via 博客园 - 靖意风

1. 内核编程框架

1.1 代码

$cat hello.c 
#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void) { printk("%s\n", __func__); return 0; } static void hello_exit(void) { printk("%s\n", __func__); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");

1.2 Makefile

obj-m += hello.o

all:
    make -C /opt/kernel SUBDIRS=$(PWD) modules

clean:
    make -C  /opt/kernel SUBIDRS=$(PWD) clean

说明:

1. 表示将hello.c单独编译生成hello.ko  m=module=模块

2. 当执行make或者make all时,执行对应的编译命令:make -C /opt/kernel SUBDIRS=$(PWD) modules

-C /opt/kernel:到/opt/kernel目录下,类似:cd /opt/kernel

SUBDIRS=$(PWD):告诉linux内核,在内核源码之外还有一个需要编译成 ko文件

1.3 测试

1> 将ko文件拷贝到开发板上
2> echo 8 > /proc/sys/kernel/printk //将默认打印级别调低
3> insmod hello.ko //安装内核程序到uImage,此时内核uImage执行入口函数hello_init
4> lsmod //查看内核程序是否安装完毕
5> rmmod hello //从内核uImage中卸载内核程序
           //此时内核执行出口函数helloworld_exit

2. 内核传参和printk

2.1 执行时传递参数

1.内核程序接收参数的变量只能全局变量
2.内核程序接收参数的全局变量的数据类型必须是以下类型:
bool,invbool
char,uchar
short,ushort
int,uint
long,ulong //ulong=unsigned long
charp(char *)
注意:内核程序不建议处理浮点数(float,double)
如果内核程序要处理浮点数,两个方法:
1.浮点数变整数(缺失精度)
3.2*2.3=>32*23/100
2.浮点数运算可以让应用程序完成

3.内核程序接收参数的全局变量必须用以下宏进行传参声明:
module_param(name, type, perm);
说明:
1.name:全局变量名
2.type: 全局变量的数据类型
3.perm:全局变量的访问权限,一般用8进制数表示
例如:0664
注意:不允许有可执行权限(r/w/x)
注意:如果权限非0,将来在/sys/module/内核程序名/parameters
目录下会创建一个同名的文件,文件里面的内容就是变量的值
将来内核程序安装之后可以通过修改文件的内容来间接修改
变量的值(非常灵活)
如果权限为0,不会出现同名的文件,此变量只能在安装时传递参数
如果没有安装之后传递参数的需求,权限一律给0,目的节省内存资源
因为/sys目录下的内容存储在于内存中!

2.2 linux内核程序符号导出

符号(symbol):就是程序中的变量名和函数名
符号导出目的:就是让别的内核程序能够访问到变量和函数
也就是多文件之间的混合调用

2.2.1  linux内核多文件之间的访问调用(符号导出)
思路:跟应用程序多文件之间的访问调用一模一样
只是多一个步骤:显示的将函数或者变量导出
导出的方法如下:
EXPORT_SYMBOL(函数名或者变量名);
或者
EXPORT_SYMBOL_GPL(函数名或者变量名);
说明:
前者导出的函数或者变量,不管这个内核程序是否遵循GPL协议,都可以访问
后者导出的函数或者变量,只能给那些遵循GPL协议的内核程序访问调用

2.3 printk

printk 可以设置级别,打印log