幽兰上默认附带了一些用户空间及内核空间的小实验,这些实验可以让大家快速方便地了解Linux系统的主要特性。

本篇文章以自带的llaolao驱动为引子,带领用户进入内核空间的世界。

1. 进入llaolao目录

geduer@ulan:~$ cd /home/geduer/gelabs/llaolao2/
geduer@ulan:~/gelabs/llaolao2$ pwd
/home/geduer/gelabs/llaolao2
geduer@ulan:~/gelabs/llaolao2$ ls
baner.c  llaolao2.sln           llaolao.cpp  main.c
gearm.c  llaolao2.vcxproj       llaolao.h    Makefile
gearm.h  llaolao2.vcxproj.user  llaolao.mod  README.txt
geduer@ulan:~/gelabs/llaolao2$

2. 编译驱动

geduer@ulan:~/gelabs/llaolao2$ make clean
rm -rf *.o *~ .*.cmd *.ko *.mod.c *.order *.symvers .tmp_versions built-in.o
geduer@ulan:~/gelabs/llaolao2$ make
make -C /lib/modules/5.10.110/build M=/home/geduer/gelabs/llaolao2 modules
make[1]: Entering directory '/usr/src/5.10.110'
  CC [M]  /home/geduer/gelabs/llaolao2/main.o
  CC [M]  /home/geduer/gelabs/llaolao2/gearm.o
  LD [M]  /home/geduer/gelabs/llaolao2/llaolao.o
  MODPOST /home/geduer/gelabs/llaolao2/Module.symvers
  CC [M]  /home/geduer/gelabs/llaolao2/llaolao.mod.o
  LD [M]  /home/geduer/gelabs/llaolao2/llaolao.ko
make[1]: Leaving directory '/usr/src/5.10.110'

3. 加载驱动

先执行ls -l命令观察确认已经构建出新的ko文件,文件名可能是llaolao.ko或者llaolao2.ko,然后通过sudo insmod llaolao.ko或者sudo insmod llaolao2.ko命令加载刚刚编译好的驱动,完成加载后,可以在模块列表及dmesg内看到llaolao或者llaolao2
驱动的身影。

geduer@ulan:~/gelabs/llaolao2$ lsmod
Module                  Size  Used by
llaolao                24576  0
br_netfilter           28672  0
bpfilter               16384  0
tun                    53248  0
bridge                262144  1 br_netfilter
stp                    16384  1 bridge
llc                    16384  2 bridge,stp
ndb                    20480  0
bcmdhd               1609728  0
dhd_static_buf         16384  1 bcmdhd
ipv6                  606208  85 bridge,br_netfilter
autofs4                45056  0

geduer@ulan:~/gelabs/llaolao2$ dmesg
[ 4020.929075] llaolao: loading out-of-tree module taints kernel.
[ 4020.931234] NGB module head is updated 0000000092451803 on self 00000000537bf900 val 1, data 00000000d308fc47
[ 4020.931991] Hi, I am llaolao at address: 00000000d5ec09c2
                symbol: 0x00000000d5ec09c2
                stack: 0x00000000e13b4d65
                first 16 bytes: fd:7b:ba:a9:fd:03:00:91:f3:53:01:a9:f5:5b:02:a9
[ 4020.933681] NGB module head is updated 0000000092451803 on self 00000000537bf900 val 0, data 00000000d308fc47
geduer@ulan:~/gelabs/llaolao2$
作者:admin  创建时间:2023-05-02 10:15
最后编辑:Zhang Yinkui  更新时间:2024-11-15 17:44