Skip to content

Commit 4f5c4e5

Browse files
committed
Implement a virtual device in qemu and the driver
1 parent eca1d0a commit 4f5c4e5

File tree

5 files changed

+1537
-0
lines changed

5 files changed

+1537
-0
lines changed

debug/crypto/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Add your debugging flag (or not) to CFLAGS
2+
ifeq ($(DEBUG),y)
3+
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
4+
else
5+
DEBFLAGS = -O2
6+
endif
7+
8+
obj-m := crypto-drv.o
9+
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
10+
CC ?= gcc
11+
PWD := $(shell pwd)
12+
13+
modules:
14+
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
15+
16+
clean:
17+
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order Module.symvers
18+
19+
depend .depend dep:
20+
$(CC) $(CFLAGS) -M *.c > .depend
21+
22+
ifeq (.depend,$(wildcard .depend))
23+
include .depend
24+
endif

debug/crypto/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# qemu中虚拟设备实现
2+
3+
参考书:How to Develop Embedded Software Using The QEMU Machine Emulator.pdf
4+
5+
## 设备和驱动对应的代码
6+
7+
crypto.c : virtual device in qemu
8+
crypto-drv.c : driver for crypto
9+
10+
## 代码准备
11+
12+
### 将设备添加到qemu中
13+
14+
qemu code(将crypto.c放入hw/misc/下进行编译)
15+
16+
git checkout 59e1b8a22e
17+
18+
Compile qemu and run
19+
20+
./configure --target-list=x86_64-softmmu \
21+
--extra-ldflags="`pkg-config --libs openssl`"
22+
23+
qemu/build/x86_64-softmmu/qemu-system-x86_64 \
24+
-drive file=/path/to/system.qcow2 \
25+
-smp 2 -m 1024 -enable-kvm \
26+
-device pci-crypto,aes_cbc_256="abc" \
27+
-netdev tap,id=nd0,ifname=tap0,script=./nat_up.py,downscript=./nat_down.py \
28+
-device e1000,netdev=nd0,mac=52:54:00:12:34:27
29+
30+
### 直接在当前目录编译对应的设备驱动模块(crypto-drv.c, Makefile)
31+
32+
make
33+
34+
## 调试
35+
36+
### PCI寄存器的查看和修改(使用setpci)
37+
38+
进系统后查看pci设备寄存器(通用)
39+
40+
setpci --dumpregs
41+
42+
00 W VENDOR_ID
43+
02 W DEVICE_ID
44+
04 W COMMAND
45+
06 W STATUS
46+
08 B REVISION
47+
09 B CLASS_PROG
48+
...
49+
10 L BASE_ADDRESS_0
50+
51+
用lspci -n 找到对应设备的BDF值(在驱动中定义了vendorId 0x2222, deviceId 0x1111)
52+
53+
00:03.0 00ff: 1111:2222
54+
55+
查看设备对应寄存器的值(比如查看VENDOR_ID, 由dumpregs可知寄存器偏移量为00)
56+
57+
setpci -s 00:03.0 00.w
58+
59+
再比如查看device id
60+
61+
setpci -s 00:03.0 02.w
62+
63+
查看设备的bar0的地址
64+
65+
setpci -s 00:03.0 10.l
66+
67+
或者使用lspci查看(下面的0xfebf1000就是bar0地址, 即mmio的地址)
68+
69+
lspci -vvvv -xxxx -s 00:03.0
70+
Region 0: Memory at febf1000 (32-bit, non-prefetchable) [size=4K]
71+
72+
或者通过qemu monitor查看也能查看到该地址
73+
74+
(qemu) info qtree
75+
(qemu) info mtree
76+
77+
### 使用[devmem](https://github.com/VCTLabs/devmem2)工具来调试
78+
79+
编译工具
80+
81+
gcc devmem2.c -o devmem
82+
83+
下面命令会调用到mmio的read函数
84+
85+
./devmem 0xfebf1000 b
86+
./devmem 0xfebf1000 w
87+
./devmem 0xfebf1000 l
88+
89+
写mmio空间的第二个地址0xfebf1000 + 2,命令是1:对应的是reset
90+
91+
./devmem 0xfebf1002 b 1
92+
93+
Encrypt:命令是2
94+
95+
./devmem 0xfebf1002 b 2
96+
97+
Decrypt:命令是3
98+
99+
./devmem 0xfebf1002 b 3
100+
101+
Enable interrupt
102+
103+
./devmem 0xfebf1003 b 2
104+
105+
Disable interrupt
106+
107+
./devmem 0xfebf1003 b 0
108+
109+
### 其它信息查看
110+
111+
查看设备对应的io空间
112+
113+
grep mycrypto /proc/iomem
114+
febf1000-febf1fff : mycrypto
115+
116+
查看驱动对应的符号表
117+
118+
grep crypto_drv /proc/kallsyms

0 commit comments

Comments
 (0)