通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了。
1. 了解我们的外设情况。
2. 为在guest中开发我们自己的linux PCI驱动程序做准备。
考虑到ubuntu对于 spice的支持不好。我们采用VNC显示。
-vnc 127.0.0.1:21 -vga cirrus -chardev stdio,id=mon0 -mon chardev=mon0,mode=readline
$ sudo x86_64-softmmu/qemu-system-x86_64 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 -device cstl-watchdog -device virtio-blk-pci,scsi=off,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive file=/home/shhfeng/iso/image/ubuntu.12.04.img,if=none,id=drive-virtio-disk0,format=qcow2,cache=none -net bridge,br=virbr2,helper=/home/shhfeng/qemu/debug/qemu-bridge-helper -net nic,model=virtio -vnc 127.0.0.1:21 -vga cirrus -chardev stdio,id=mon0 -mon chardev=mon0,mode=readline
(qemu) info qtree
1 (qemu) info qtree 2 bus: main-system-bus 3 type System 4 dev: hpet, id "" 5 gpio-in 2 6 gpio-out 1 7 timers = 3 (0x3) 8 msi = false 9 hpet-intcap = 4 (0x4) 10 irq 32 11 mmio 00000000fed00000/0000000000000400 12 dev: kvm-ioapic, id "" 13 gpio-in 24 14 gsi_base = 0 (0) 15 irq 0 16 mmio 00000000fec00000/0000000000001000 17 dev: i440FX-pcihost, id "" 18 pci-hole64-size = 18446744073709551615 (16 EiB) 19 short_root_bus = 0 (0) 20 irq 0 21 bus: pci.0 22 type PCI 23 dev: cstl-watchdog, id "" 24 expiration-ticks = 10 (0xa) 25 addr = 05.0 26 romfile = "" 27 rombar = 1 (0x1) 28 multifunction = false 29 command_serr_enable = true 30 class Class 0880, addr 00:05.0, pci id 1af4:0101 (sub 1af4:1100) 31 bar 0: i/o at 0xc070 [0xc07f] 32 dev: virtio-blk-pci, id "virtio-disk0" 33 class = 0 (0) 34 ioeventfd = true 35 vectors = 2 (0x2) 36 indirect_desc = true 37 event_idx = true 38 drive = "drive-virtio-disk0" 39 logical_block_size = 512 (0x200) 40 physical_block_size = 512 (0x200) 41 min_io_size = 0 (0) 42 opt_io_size = 0 (0) 43 bootindex = 2 (0x2) 44 discard_granularity = 4294967295 (0xffffffff) 45 cyls = 0 (0) 46 heads = 0 (0) 47 secs = 0 (0) 48 serial = "" 49 config-wce = true 50 scsi = false 51 x-iothread = "" 52 addr = 04.0 53 romfile = "" 54 rombar = 1 (0x1) 55 multifunction = false 56 command_serr_enable = true 57 class SCSI controller, addr 00:04.0, pci id 1af4:1001 (sub 1af4:0002) 58 bar 0: i/o at 0xc000 [0xc03f] 59 bar 1: mem at 0xfebd2000 [0xfebd2fff] 60 bus: virtio-bus 61 type virtio-pci-bus 62 dev: virtio-blk-device, id "" 63 drive = "drive-virtio-disk0" 64 logical_block_size = 512 (0x200) 65 physical_block_size = 512 (0x200) 66 min_io_size = 0 (0) 67 opt_io_size = 0 (0) 68 bootindex = 2 (0x2) 69 discard_granularity = 4294967295 (0xffffffff) 70 cyls = 16383 (0x3fff) 71 heads = 16 (0x10) 72 secs = 63 (0x3f) 73 serial = "" 74 config-wce = true 75 scsi = false 76 x-iothread = "" 77 dev: PIIX4_PM, id "" 78 smb_io_base = 45312 (0xb100) 79 disable_s3 = 0 (0) 80 disable_s4 = 0 (0) 81 s4_val = 2 (0x2) 82 acpi-pci-hotplug-with-bridge-support = true 83 addr = 01.3 84 romfile = "" 85 rombar = 1 (0x1) 86 multifunction = false 87 command_serr_enable = true 88 class Bridge, addr 00:01.3, pci id 8086:7113 (sub 1af4:1100) 89 bus: i2c 90 type i2c-bus 91 dev: smbus-eeprom, id "" 92 address = 87 (0x57) 93 dev: smbus-eeprom, id "" 94 address = 86 (0x56) 95 dev: smbus-eeprom, id "" 96 address = 85 (0x55) 97 dev: smbus-eeprom, id "" 98 address = 84 (0x54) 99 dev: smbus-eeprom, id "" 100 address = 83 (0x53) 101 dev: smbus-eeprom, id "" 102 address = 82 (0x52) 103 dev: smbus-eeprom, id "" 104 address = 81 (0x51) 105 dev: smbus-eeprom, id "" 106 address = 80 (0x50) 107 dev: piix3-ide, id "" 108 addr = 01.1 109 romfile = "" 110 rombar = 1 (0x1) 111 multifunction = false 112 command_serr_enable = true 113 class IDE controller, addr 00:01.1, pci id 8086:7010 (sub 1af4:1100) 114 bar 4: i/o at 0xc060 [0xc06f] 115 bus: ide.1 116 type IDE 117 dev: ide-cd, id "ide0-1-0" 118 drive = "drive-ide0-1-0" 119 logical_block_size = 512 (0x200) 120 physical_block_size = 512 (0x200) 121 min_io_size = 0 (0) 122 opt_io_size = 0 (0) 123 bootindex = 1 (0x1) 124 discard_granularity = 512 (0x200) 125 ver = "1.7.90" 126 wwn = 0 (0) 127 serial = "QM00003" 128 model = "" 129 unit = 0 (0) 130 bus: ide.0 131 type IDE 132 dev: virtio-net-pci, id "" 133 ioeventfd = false 134 vectors = 3 (0x3) 135 indirect_desc = true 136 event_idx = true 137 any_layout = true 138 csum = false 139 guest_csum = false 140 gso = true 141 guest_tso4 = false 142 guest_tso6 = false 143 guest_ecn = false 144 guest_ufo = false 145 host_tso4 = false 146 host_tso6 = false 147 host_ecn = false 148 host_ufo = false 149 mrg_rxbuf = true 150 status = true 151 ctrl_vq = true 152 ctrl_rx = true 153 ctrl_vlan = true 154 ctrl_rx_extra = true 155 ctrl_mac_addr = true 156 ctrl_guest_offloads = true 157 mq = false 158 mac = "52:54:00:12:34:56" 159 vlan = 0 160 netdev = "hub0port1" 161 bootindex = -1 (0xffffffffffffffff) 162 x-txtimer = 150000 (0x249f0) 163 x-txburst = 256 (0x100) 164 tx = "" 165 addr = 03.0 166 romfile = "efi-virtio.rom" 167 rombar = 1 (0x1) 168 multifunction = false 169 command_serr_enable = true 170 class Ethernet controller, addr 00:03.0, pci id 1af4:1000 (sub 1af4:0001) 171 bar 0: i/o at 0xc040 [0xc05f] 172 bar 1: mem at 0xfebd1000 [0xfebd1fff] 173 bar 6: mem at 0xffffffffffffffff [0x3fffe] 174 bus: virtio-bus 175 type virtio-pci-bus 176 dev: virtio-net-device, id "" 177 mac = "52:54:00:12:34:56" 178 vlan = 0 179 netdev = "hub0port1" 180 bootindex = -1 (0xffffffffffffffff) 181 x-txtimer = 150000 (0x249f0) 182 x-txburst = 256 (0x100) 183 tx = "" 184 dev: cirrus-vga, id "" 185 vgamem_mb = 8 (0x8) 186 addr = 02.0 187 romfile = "vgabios-cirrus.bin" 188 rombar = 1 (0x1) 189 multifunction = false 190 command_serr_enable = true 191 class VGA controller, addr 00:02.0, pci id 1013:00b8 (sub 1af4:1100) 192 bar 0: mem at 0xfc000000 [0xfdffffff] 193 bar 1: mem at 0xfebd0000 [0xfebd0fff] 194 bar 6: mem at 0xffffffffffffffff [0xfffe] 195 dev: PIIX3, id "" 196 addr = 01.0 197 romfile = "" 198 rombar = 1 (0x1) 199 multifunction = true 200 command_serr_enable = true 201 class ISA bridge, addr 00:01.0, pci id 8086:7000 (sub 1af4:1100) 202 bus: isa.0 203 type ISA 204 dev: isa-fdc, id "" 205 iobase = 1008 (0x3f0) 206 irq = 6 (0x6) 207 dma = 2 (0x2) 208 driveA = "floppy0" 209 driveB = "" 210 bootindexA = -1 (0xffffffffffffffff) 211 bootindexB = -1 (0xffffffffffffffff) 212 check_media_rate = true 213 isa irq 6 214 dev: port92, id "" 215 dev: vmmouse, id "" 216 dev: vmport, id "" 217 dev: i8042, id "" 218 isa irqs 1,12 219 dev: isa-parallel, id "" 220 index = 0 (0) 221 iobase = 888 (0x378) 222 irq = 7 (0x7) 223 chardev = "parallel0" 224 isa irq 7 225 dev: isa-serial, id "" 226 index = 0 (0) 227 iobase = 1016 (0x3f8) 228 irq = 4 (0x4) 229 chardev = "serial0" 230 wakeup = 0 (0) 231 isa irq 4 232 dev: isa-pcspk, id "" 233 iobase = 97 (0x61) 234 dev: kvm-pit, id "" 235 gpio-in 1 236 iobase = 64 (0x40) 237 lost_tick_policy = "delay" 238 dev: mc146818rtc, id "" 239 base_year = 0 (0) 240 lost_tick_policy = "discard" 241 dev: kvm-i8259, id "" 242 iobase = 160 (0xa0) 243 elcr_addr = 1233 (0x4d1) 244 elcr_mask = 222 (0xde) 245 master = false 246 dev: kvm-i8259, id "" 247 iobase = 32 (0x20) 248 elcr_addr = 1232 (0x4d0) 249 elcr_mask = 248 (0xf8) 250 master = true 251 dev: i440FX, id "" 252 addr = 00.0 253 romfile = "" 254 rombar = 1 (0x1) 255 multifunction = false 256 command_serr_enable = true 257 class Host bridge, addr 00:00.0, pci id 8086:1237 (sub 1af4:1100) 258 dev: fw_cfg, id "" 259 ctl_iobase = 1296 (0x510) 260 data_iobase = 1297 (0x511) 261 irq 0 262 mmio ffffffffffffffff/0000000000000002 263 mmio ffffffffffffffff/0000000000000001 264 dev: kvmclock, id "" 265 irq 0 266 dev: kvmvapic, id "" 267 irq 0 268 dev: icc-bridge, id "" 269 irq 0 270 mmio 00000000fee00000/0000000000100000 271 bus: icc 272 type icc-bus 273 dev: kvm-apic, id "" 274 id = 0 (0) 275 vapic = true 276 dev: qemu64-x86_64-cpu, id "" 277 pmu = false 278 hv-spinlocks = -1 (0xffffffffffffffff) 279 hv-relaxed = false 280 hv-vapic = false 281 hv-time = false 282 check = false 283 enforce = false
可以找到我们的watchdog设备:
dev: cstl-watchdog, id ""
expiration-ticks = 10 (0xa)
addr = 05.0
romfile = ""
rombar = 1 (0x1)
multifunction = false
command_serr_enable = true
class Class 0880, addr 00:05.0, pci id 1af4:0101 (sub 1af4:1100)
bar 0: i/o at 0xc090 [0xc09f]
有些激动。
$ lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II]
00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03)
00:02.0 VGA compatible controller: Cirrus Logic GD 5446
00:03.0 Ethernet controller: Red Hat, Inc Virtio network device
00:04.0 Ethernet controller: Red Hat, Inc Virtio network device
00:05.0 System peripheral: Red Hat, Inc Device 0101 (rev 01)
00:06.0 SCSI storage controller: Red Hat, Inc Virtio block device
可以看到我们的device设备, Red Hat, Inc Device 0101 (rev 01),这个我们的代码中定义的是一致的。
static void cwd_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
k->init = cwd_realize;
k->exit = cwd_unrealize;
k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
k->device_id = 0x0101;
k->revision = 0x01;
k->class_id = PCI_CLASS_SYSTEM_OTHER;
dc->reset = cwd_reset;
dc->vmsd = &vmstate_cwd;
dc->props = cwd_properties;
}
$ lspci -x
00:05.0 System peripheral: Red Hat, Inc Device 0101 (rev 01)
00: f4 1a 01 01 03 01 00 00 01 00 80 08 00 00 00 00
10: 91 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 f4 1a 00 11
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00