]> Git Repo - qemu.git/blame - hw/virtio/virtio.c
virtio: avoid buffer overrun on incoming migration
[qemu.git] / hw / virtio / virtio.c
CommitLineData
967f97fa
AL
1/*
2 * Virtio Support
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <[email protected]>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include <inttypes.h>
967f97fa 15
64979a4d 16#include "trace.h"
fdfba1a2 17#include "exec/address-spaces.h"
1de7afc9 18#include "qemu/error-report.h"
0d09e41a 19#include "hw/virtio/virtio.h"
1de7afc9 20#include "qemu/atomic.h"
0d09e41a 21#include "hw/virtio/virtio-bus.h"
967f97fa 22
6ce69d1c
PM
23/*
24 * The alignment to use between consumer and producer parts of vring.
25 * x86 pagesize again. This is the default, used by transports like PCI
26 * which don't provide a means for the guest to tell the host the alignment.
27 */
f46f15bc
AL
28#define VIRTIO_PCI_VRING_ALIGN 4096
29
967f97fa
AL
30typedef struct VRingDesc
31{
32 uint64_t addr;
33 uint32_t len;
34 uint16_t flags;
35 uint16_t next;
36} VRingDesc;
37
38typedef struct VRingAvail
39{
40 uint16_t flags;
41 uint16_t idx;
42 uint16_t ring[0];
43} VRingAvail;
44
45typedef struct VRingUsedElem
46{
47 uint32_t id;
48 uint32_t len;
49} VRingUsedElem;
50
51typedef struct VRingUsed
52{
53 uint16_t flags;
54 uint16_t idx;
55 VRingUsedElem ring[0];
56} VRingUsed;
57
58typedef struct VRing
59{
60 unsigned int num;
6ce69d1c 61 unsigned int align;
a8170e5e
AK
62 hwaddr desc;
63 hwaddr avail;
64 hwaddr used;
967f97fa
AL
65} VRing;
66
67struct VirtQueue
68{
69 VRing vring;
a8170e5e 70 hwaddr pa;
967f97fa 71 uint16_t last_avail_idx;
bcbabae8
MT
72 /* Last used index value we have signalled on */
73 uint16_t signalled_used;
74
75 /* Last used index value we have signalled on */
76 bool signalled_used_valid;
77
78 /* Notification enabled? */
79 bool notification;
80
e78a2b42
JW
81 uint16_t queue_index;
82
967f97fa 83 int inuse;
bcbabae8 84
7055e687 85 uint16_t vector;
967f97fa 86 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
1cbdabe2
MT
87 VirtIODevice *vdev;
88 EventNotifier guest_notifier;
89 EventNotifier host_notifier;
967f97fa
AL
90};
91
967f97fa 92/* virt queue functions */
53c25cea 93static void virtqueue_init(VirtQueue *vq)
967f97fa 94{
a8170e5e 95 hwaddr pa = vq->pa;
53c25cea 96
967f97fa
AL
97 vq->vring.desc = pa;
98 vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
f46f15bc
AL
99 vq->vring.used = vring_align(vq->vring.avail +
100 offsetof(VRingAvail, ring[vq->vring.num]),
6ce69d1c 101 vq->vring.align);
967f97fa
AL
102}
103
a8170e5e 104static inline uint64_t vring_desc_addr(hwaddr desc_pa, int i)
967f97fa 105{
a8170e5e 106 hwaddr pa;
5774cf98 107 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
2c17449b 108 return ldq_phys(&address_space_memory, pa);
967f97fa
AL
109}
110
a8170e5e 111static inline uint32_t vring_desc_len(hwaddr desc_pa, int i)
967f97fa 112{
a8170e5e 113 hwaddr pa;
5774cf98 114 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
fdfba1a2 115 return ldl_phys(&address_space_memory, pa);
967f97fa
AL
116}
117
a8170e5e 118static inline uint16_t vring_desc_flags(hwaddr desc_pa, int i)
967f97fa 119{
a8170e5e 120 hwaddr pa;
5774cf98 121 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
41701aa4 122 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
123}
124
a8170e5e 125static inline uint16_t vring_desc_next(hwaddr desc_pa, int i)
967f97fa 126{
a8170e5e 127 hwaddr pa;
5774cf98 128 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
41701aa4 129 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
130}
131
132static inline uint16_t vring_avail_flags(VirtQueue *vq)
133{
a8170e5e 134 hwaddr pa;
967f97fa 135 pa = vq->vring.avail + offsetof(VRingAvail, flags);
41701aa4 136 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
137}
138
139static inline uint16_t vring_avail_idx(VirtQueue *vq)
140{
a8170e5e 141 hwaddr pa;
967f97fa 142 pa = vq->vring.avail + offsetof(VRingAvail, idx);
41701aa4 143 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
144}
145
146static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
147{
a8170e5e 148 hwaddr pa;
967f97fa 149 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
41701aa4 150 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
151}
152
bcbabae8
MT
153static inline uint16_t vring_used_event(VirtQueue *vq)
154{
155 return vring_avail_ring(vq, vq->vring.num);
156}
157
967f97fa
AL
158static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
159{
a8170e5e 160 hwaddr pa;
967f97fa 161 pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
ab1da857 162 stl_phys(&address_space_memory, pa, val);
967f97fa
AL
163}
164
165static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
166{
a8170e5e 167 hwaddr pa;
967f97fa 168 pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
ab1da857 169 stl_phys(&address_space_memory, pa, val);
967f97fa
AL
170}
171
172static uint16_t vring_used_idx(VirtQueue *vq)
173{
a8170e5e 174 hwaddr pa;
967f97fa 175 pa = vq->vring.used + offsetof(VRingUsed, idx);
41701aa4 176 return lduw_phys(&address_space_memory, pa);
967f97fa
AL
177}
178
bcbabae8 179static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
967f97fa 180{
a8170e5e 181 hwaddr pa;
967f97fa 182 pa = vq->vring.used + offsetof(VRingUsed, idx);
5ce5944d 183 stw_phys(&address_space_memory, pa, val);
967f97fa
AL
184}
185
186static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
187{
a8170e5e 188 hwaddr pa;
967f97fa 189 pa = vq->vring.used + offsetof(VRingUsed, flags);
5ce5944d
EI
190 stw_phys(&address_space_memory,
191 pa, lduw_phys(&address_space_memory, pa) | mask);
967f97fa
AL
192}
193
194static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
195{
a8170e5e 196 hwaddr pa;
967f97fa 197 pa = vq->vring.used + offsetof(VRingUsed, flags);
5ce5944d
EI
198 stw_phys(&address_space_memory,
199 pa, lduw_phys(&address_space_memory, pa) & ~mask);
967f97fa
AL
200}
201
bcbabae8
MT
202static inline void vring_avail_event(VirtQueue *vq, uint16_t val)
203{
a8170e5e 204 hwaddr pa;
bcbabae8
MT
205 if (!vq->notification) {
206 return;
207 }
208 pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
5ce5944d 209 stw_phys(&address_space_memory, pa, val);
bcbabae8
MT
210}
211
967f97fa
AL
212void virtio_queue_set_notification(VirtQueue *vq, int enable)
213{
bcbabae8
MT
214 vq->notification = enable;
215 if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
216 vring_avail_event(vq, vring_avail_idx(vq));
217 } else if (enable) {
967f97fa 218 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 219 } else {
967f97fa 220 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 221 }
92045d80
MT
222 if (enable) {
223 /* Expose avail event/used flags before caller checks the avail idx. */
224 smp_mb();
225 }
967f97fa
AL
226}
227
228int virtio_queue_ready(VirtQueue *vq)
229{
230 return vq->vring.avail != 0;
231}
232
233int virtio_queue_empty(VirtQueue *vq)
234{
235 return vring_avail_idx(vq) == vq->last_avail_idx;
236}
237
238void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
239 unsigned int len, unsigned int idx)
240{
241 unsigned int offset;
242 int i;
243
64979a4d
SH
244 trace_virtqueue_fill(vq, elem, len, idx);
245
967f97fa
AL
246 offset = 0;
247 for (i = 0; i < elem->in_num; i++) {
248 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
249
26b258e1
AL
250 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
251 elem->in_sg[i].iov_len,
252 1, size);
967f97fa 253
0cea71a2 254 offset += size;
967f97fa
AL
255 }
256
26b258e1
AL
257 for (i = 0; i < elem->out_num; i++)
258 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
259 elem->out_sg[i].iov_len,
260 0, elem->out_sg[i].iov_len);
261
967f97fa
AL
262 idx = (idx + vring_used_idx(vq)) % vq->vring.num;
263
264 /* Get a pointer to the next entry in the used ring. */
265 vring_used_ring_id(vq, idx, elem->index);
266 vring_used_ring_len(vq, idx, len);
267}
268
269void virtqueue_flush(VirtQueue *vq, unsigned int count)
270{
bcbabae8 271 uint16_t old, new;
967f97fa 272 /* Make sure buffer is written before we update index. */
b90d2f35 273 smp_wmb();
64979a4d 274 trace_virtqueue_flush(vq, count);
bcbabae8
MT
275 old = vring_used_idx(vq);
276 new = old + count;
277 vring_used_idx_set(vq, new);
967f97fa 278 vq->inuse -= count;
bcbabae8
MT
279 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
280 vq->signalled_used_valid = false;
967f97fa
AL
281}
282
283void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
284 unsigned int len)
285{
286 virtqueue_fill(vq, elem, len, 0);
287 virtqueue_flush(vq, 1);
288}
289
290static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
291{
292 uint16_t num_heads = vring_avail_idx(vq) - idx;
293
294 /* Check it isn't doing very strange things with descriptor numbers. */
bb6834cf 295 if (num_heads > vq->vring.num) {
ce67ed65
SH
296 error_report("Guest moved used index from %u to %u",
297 idx, vring_avail_idx(vq));
bb6834cf
AL
298 exit(1);
299 }
a821ce59
MT
300 /* On success, callers read a descriptor at vq->last_avail_idx.
301 * Make sure descriptor read does not bypass avail index read. */
302 if (num_heads) {
303 smp_rmb();
304 }
967f97fa
AL
305
306 return num_heads;
307}
308
309static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
310{
311 unsigned int head;
312
313 /* Grab the next descriptor number they're advertising, and increment
314 * the index we've seen. */
315 head = vring_avail_ring(vq, idx % vq->vring.num);
316
317 /* If their number is silly, that's a fatal mistake. */
bb6834cf 318 if (head >= vq->vring.num) {
ce67ed65 319 error_report("Guest says index %u is available", head);
bb6834cf
AL
320 exit(1);
321 }
967f97fa
AL
322
323 return head;
324}
325
a8170e5e 326static unsigned virtqueue_next_desc(hwaddr desc_pa,
5774cf98 327 unsigned int i, unsigned int max)
967f97fa
AL
328{
329 unsigned int next;
330
331 /* If this descriptor says it doesn't chain, we're done. */
5774cf98
MM
332 if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
333 return max;
967f97fa
AL
334
335 /* Check they're not leading us off end of descriptors. */
5774cf98 336 next = vring_desc_next(desc_pa, i);
967f97fa 337 /* Make sure compiler knows to grab that: we don't want it changing! */
b90d2f35 338 smp_wmb();
967f97fa 339
5774cf98 340 if (next >= max) {
ce67ed65 341 error_report("Desc next is %u", next);
bb6834cf
AL
342 exit(1);
343 }
967f97fa
AL
344
345 return next;
346}
347
0d8d7690 348void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
e1f7b481
MT
349 unsigned int *out_bytes,
350 unsigned max_in_bytes, unsigned max_out_bytes)
967f97fa 351{
efeea6d0 352 unsigned int idx;
385ce95d 353 unsigned int total_bufs, in_total, out_total;
967f97fa
AL
354
355 idx = vq->last_avail_idx;
356
efeea6d0 357 total_bufs = in_total = out_total = 0;
967f97fa 358 while (virtqueue_num_heads(vq, idx)) {
efeea6d0 359 unsigned int max, num_bufs, indirect = 0;
a8170e5e 360 hwaddr desc_pa;
967f97fa
AL
361 int i;
362
efeea6d0
MM
363 max = vq->vring.num;
364 num_bufs = total_bufs;
967f97fa 365 i = virtqueue_get_head(vq, idx++);
efeea6d0
MM
366 desc_pa = vq->vring.desc;
367
368 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
369 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
ce67ed65 370 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
371 exit(1);
372 }
373
374 /* If we've got too many, that implies a descriptor loop. */
375 if (num_bufs >= max) {
ce67ed65 376 error_report("Looped descriptor");
efeea6d0
MM
377 exit(1);
378 }
379
380 /* loop over the indirect descriptor table */
381 indirect = 1;
382 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
efeea6d0 383 desc_pa = vring_desc_addr(desc_pa, i);
1ae2757c 384 num_bufs = i = 0;
efeea6d0
MM
385 }
386
967f97fa
AL
387 do {
388 /* If we've got too many, that implies a descriptor loop. */
5774cf98 389 if (++num_bufs > max) {
ce67ed65 390 error_report("Looped descriptor");
bb6834cf
AL
391 exit(1);
392 }
967f97fa 393
5774cf98 394 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
0d8d7690 395 in_total += vring_desc_len(desc_pa, i);
967f97fa 396 } else {
0d8d7690 397 out_total += vring_desc_len(desc_pa, i);
967f97fa 398 }
e1f7b481
MT
399 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
400 goto done;
401 }
5774cf98 402 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
efeea6d0
MM
403
404 if (!indirect)
405 total_bufs = num_bufs;
406 else
407 total_bufs++;
967f97fa 408 }
e1f7b481 409done:
0d8d7690
AS
410 if (in_bytes) {
411 *in_bytes = in_total;
412 }
413 if (out_bytes) {
414 *out_bytes = out_total;
415 }
416}
967f97fa 417
0d8d7690
AS
418int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
419 unsigned int out_bytes)
420{
421 unsigned int in_total, out_total;
422
e1f7b481
MT
423 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
424 return in_bytes <= in_total && out_bytes <= out_total;
967f97fa
AL
425}
426
a8170e5e 427void virtqueue_map_sg(struct iovec *sg, hwaddr *addr,
42fb2e07
KW
428 size_t num_sg, int is_write)
429{
430 unsigned int i;
a8170e5e 431 hwaddr len;
42fb2e07
KW
432
433 for (i = 0; i < num_sg; i++) {
434 len = sg[i].iov_len;
435 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
436 if (sg[i].iov_base == NULL || len != sg[i].iov_len) {
ce67ed65 437 error_report("virtio: trying to map MMIO memory");
42fb2e07
KW
438 exit(1);
439 }
440 }
441}
442
967f97fa
AL
443int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
444{
5774cf98 445 unsigned int i, head, max;
a8170e5e 446 hwaddr desc_pa = vq->vring.desc;
967f97fa
AL
447
448 if (!virtqueue_num_heads(vq, vq->last_avail_idx))
449 return 0;
450
451 /* When we start there are none of either input nor output. */
452 elem->out_num = elem->in_num = 0;
453
5774cf98
MM
454 max = vq->vring.num;
455
967f97fa 456 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
bcbabae8
MT
457 if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
458 vring_avail_event(vq, vring_avail_idx(vq));
459 }
efeea6d0
MM
460
461 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
462 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
ce67ed65 463 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
464 exit(1);
465 }
466
467 /* loop over the indirect descriptor table */
468 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
469 desc_pa = vring_desc_addr(desc_pa, i);
470 i = 0;
471 }
472
42fb2e07 473 /* Collect all the descriptors */
967f97fa
AL
474 do {
475 struct iovec *sg;
476
5774cf98 477 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
c8eac1cf
MT
478 if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) {
479 error_report("Too many write descriptors in indirect table");
480 exit(1);
481 }
5774cf98 482 elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
967f97fa 483 sg = &elem->in_sg[elem->in_num++];
42fb2e07 484 } else {
c8eac1cf
MT
485 if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) {
486 error_report("Too many read descriptors in indirect table");
487 exit(1);
488 }
42fb2e07 489 elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i);
967f97fa 490 sg = &elem->out_sg[elem->out_num++];
42fb2e07 491 }
967f97fa 492
5774cf98 493 sg->iov_len = vring_desc_len(desc_pa, i);
967f97fa
AL
494
495 /* If we've got too many, that implies a descriptor loop. */
5774cf98 496 if ((elem->in_num + elem->out_num) > max) {
ce67ed65 497 error_report("Looped descriptor");
bb6834cf
AL
498 exit(1);
499 }
5774cf98 500 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
967f97fa 501
42fb2e07
KW
502 /* Now map what we have collected */
503 virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
504 virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
505
967f97fa
AL
506 elem->index = head;
507
508 vq->inuse++;
509
64979a4d 510 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
967f97fa
AL
511 return elem->in_num + elem->out_num;
512}
513
514/* virtio device */
7055e687
MT
515static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
516{
1c819449
FK
517 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
518 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
519
520 if (k->notify) {
521 k->notify(qbus->parent, vector);
7055e687
MT
522 }
523}
967f97fa 524
53c25cea 525void virtio_update_irq(VirtIODevice *vdev)
967f97fa 526{
7055e687 527 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
967f97fa
AL
528}
529
4e1837f8
SH
530void virtio_set_status(VirtIODevice *vdev, uint8_t val)
531{
181103cd 532 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
4e1837f8
SH
533 trace_virtio_set_status(vdev, val);
534
181103cd
FK
535 if (k->set_status) {
536 k->set_status(vdev, val);
4e1837f8
SH
537 }
538 vdev->status = val;
539}
540
53c25cea 541void virtio_reset(void *opaque)
967f97fa
AL
542{
543 VirtIODevice *vdev = opaque;
181103cd 544 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
545 int i;
546
e0c472d8
MT
547 virtio_set_status(vdev, 0);
548
181103cd
FK
549 if (k->reset) {
550 k->reset(vdev);
551 }
967f97fa 552
704a76fc 553 vdev->guest_features = 0;
967f97fa
AL
554 vdev->queue_sel = 0;
555 vdev->status = 0;
556 vdev->isr = 0;
7055e687
MT
557 vdev->config_vector = VIRTIO_NO_VECTOR;
558 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
559
560 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
561 vdev->vq[i].vring.desc = 0;
562 vdev->vq[i].vring.avail = 0;
563 vdev->vq[i].vring.used = 0;
564 vdev->vq[i].last_avail_idx = 0;
53c25cea 565 vdev->vq[i].pa = 0;
7055e687 566 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
bcbabae8
MT
567 vdev->vq[i].signalled_used = 0;
568 vdev->vq[i].signalled_used_valid = false;
569 vdev->vq[i].notification = true;
967f97fa
AL
570 }
571}
572
53c25cea 573uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
967f97fa 574{
181103cd 575 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
576 uint8_t val;
577
5f5a1318 578 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 579 return (uint32_t)-1;
5f5a1318
JW
580 }
581
582 k->get_config(vdev, vdev->config);
967f97fa 583
06dbfc6f 584 val = ldub_p(vdev->config + addr);
967f97fa
AL
585 return val;
586}
587
53c25cea 588uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
967f97fa 589{
181103cd 590 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
591 uint16_t val;
592
5f5a1318 593 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 594 return (uint32_t)-1;
5f5a1318
JW
595 }
596
597 k->get_config(vdev, vdev->config);
967f97fa 598
06dbfc6f 599 val = lduw_p(vdev->config + addr);
967f97fa
AL
600 return val;
601}
602
53c25cea 603uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
967f97fa 604{
181103cd 605 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
606 uint32_t val;
607
5f5a1318 608 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 609 return (uint32_t)-1;
5f5a1318
JW
610 }
611
612 k->get_config(vdev, vdev->config);
967f97fa 613
06dbfc6f 614 val = ldl_p(vdev->config + addr);
967f97fa
AL
615 return val;
616}
617
53c25cea 618void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 619{
181103cd 620 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
621 uint8_t val = data;
622
5f5a1318 623 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 624 return;
5f5a1318 625 }
967f97fa 626
06dbfc6f 627 stb_p(vdev->config + addr, val);
967f97fa 628
181103cd
FK
629 if (k->set_config) {
630 k->set_config(vdev, vdev->config);
631 }
967f97fa
AL
632}
633
53c25cea 634void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 635{
181103cd 636 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
637 uint16_t val = data;
638
5f5a1318 639 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 640 return;
5f5a1318 641 }
967f97fa 642
06dbfc6f 643 stw_p(vdev->config + addr, val);
967f97fa 644
181103cd
FK
645 if (k->set_config) {
646 k->set_config(vdev, vdev->config);
647 }
967f97fa
AL
648}
649
53c25cea 650void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 651{
181103cd 652 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
653 uint32_t val = data;
654
5f5a1318 655 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 656 return;
5f5a1318 657 }
967f97fa 658
06dbfc6f 659 stl_p(vdev->config + addr, val);
967f97fa 660
181103cd
FK
661 if (k->set_config) {
662 k->set_config(vdev, vdev->config);
663 }
967f97fa
AL
664}
665
a8170e5e 666void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
967f97fa 667{
7055e687
MT
668 vdev->vq[n].pa = addr;
669 virtqueue_init(&vdev->vq[n]);
53c25cea
PB
670}
671
a8170e5e 672hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
53c25cea
PB
673{
674 return vdev->vq[n].pa;
675}
676
e63c0ba1
PM
677void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
678{
f6049f44
PM
679 /* Don't allow guest to flip queue between existent and
680 * nonexistent states, or to set it to an invalid size.
681 */
682 if (!!num != !!vdev->vq[n].vring.num ||
683 num > VIRTQUEUE_MAX_SIZE ||
684 num < 0) {
685 return;
e63c0ba1 686 }
f6049f44
PM
687 vdev->vq[n].vring.num = num;
688 virtqueue_init(&vdev->vq[n]);
e63c0ba1
PM
689}
690
53c25cea
PB
691int virtio_queue_get_num(VirtIODevice *vdev, int n)
692{
693 return vdev->vq[n].vring.num;
694}
967f97fa 695
c80decdb
PB
696int virtio_queue_get_id(VirtQueue *vq)
697{
698 VirtIODevice *vdev = vq->vdev;
699 assert(vq >= &vdev->vq[0] && vq < &vdev->vq[VIRTIO_PCI_QUEUE_MAX]);
700 return vq - &vdev->vq[0];
701}
702
6ce69d1c
PM
703void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
704{
705 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
706 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
707
708 /* Check that the transport told us it was going to do this
709 * (so a buggy transport will immediately assert rather than
710 * silently failing to migrate this state)
711 */
712 assert(k->has_variable_vring_alignment);
713
714 vdev->vq[n].vring.align = align;
715 virtqueue_init(&vdev->vq[n]);
716}
717
25db9ebe
SH
718void virtio_queue_notify_vq(VirtQueue *vq)
719{
720 if (vq->vring.desc) {
721 VirtIODevice *vdev = vq->vdev;
722 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
723 vq->handle_output(vdev, vq);
724 }
725}
726
53c25cea
PB
727void virtio_queue_notify(VirtIODevice *vdev, int n)
728{
7157e2e2 729 virtio_queue_notify_vq(&vdev->vq[n]);
967f97fa
AL
730}
731
7055e687
MT
732uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
733{
734 return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
735 VIRTIO_NO_VECTOR;
736}
737
738void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
739{
740 if (n < VIRTIO_PCI_QUEUE_MAX)
741 vdev->vq[n].vector = vector;
742}
743
967f97fa
AL
744VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
745 void (*handle_output)(VirtIODevice *, VirtQueue *))
746{
747 int i;
748
749 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
750 if (vdev->vq[i].vring.num == 0)
751 break;
752 }
753
754 if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
755 abort();
756
757 vdev->vq[i].vring.num = queue_size;
6ce69d1c 758 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
967f97fa
AL
759 vdev->vq[i].handle_output = handle_output;
760
761 return &vdev->vq[i];
762}
763
f23fd811
JW
764void virtio_del_queue(VirtIODevice *vdev, int n)
765{
766 if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) {
767 abort();
768 }
769
770 vdev->vq[n].vring.num = 0;
771}
772
1cbdabe2
MT
773void virtio_irq(VirtQueue *vq)
774{
64979a4d 775 trace_virtio_irq(vq);
1cbdabe2
MT
776 vq->vdev->isr |= 0x01;
777 virtio_notify_vector(vq->vdev, vq->vector);
778}
779
bcbabae8
MT
780/* Assuming a given event_idx value from the other size, if
781 * we have just incremented index from old to new_idx,
782 * should we trigger an event? */
783static inline int vring_need_event(uint16_t event, uint16_t new, uint16_t old)
967f97fa 784{
bcbabae8
MT
785 /* Note: Xen has similar logic for notification hold-off
786 * in include/xen/interface/io/ring.h with req_event and req_prod
787 * corresponding to event_idx + 1 and new respectively.
788 * Note also that req_event and req_prod in Xen start at 1,
789 * event indexes in virtio start at 0. */
790 return (uint16_t)(new - event - 1) < (uint16_t)(new - old);
791}
792
793static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq)
794{
795 uint16_t old, new;
796 bool v;
a281ebc1
MT
797 /* We need to expose used array entries before checking used event. */
798 smp_mb();
97b83deb 799 /* Always notify when queue is empty (when feature acknowledge) */
bcbabae8
MT
800 if (((vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) &&
801 !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx)) {
802 return true;
803 }
804
805 if (!(vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
806 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
807 }
808
809 v = vq->signalled_used_valid;
810 vq->signalled_used_valid = true;
811 old = vq->signalled_used;
812 new = vq->signalled_used = vring_used_idx(vq);
813 return !v || vring_need_event(vring_used_event(vq), new, old);
814}
815
816void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
817{
818 if (!vring_notify(vdev, vq)) {
967f97fa 819 return;
bcbabae8 820 }
967f97fa 821
64979a4d 822 trace_virtio_notify(vdev, vq);
967f97fa 823 vdev->isr |= 0x01;
7055e687 824 virtio_notify_vector(vdev, vq->vector);
967f97fa
AL
825}
826
827void virtio_notify_config(VirtIODevice *vdev)
828{
7625162c
AL
829 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
830 return;
831
967f97fa 832 vdev->isr |= 0x03;
7055e687 833 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
834}
835
836void virtio_save(VirtIODevice *vdev, QEMUFile *f)
837{
1c819449
FK
838 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
839 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
967f97fa
AL
840 int i;
841
1c819449
FK
842 if (k->save_config) {
843 k->save_config(qbus->parent, f);
844 }
967f97fa 845
967f97fa
AL
846 qemu_put_8s(f, &vdev->status);
847 qemu_put_8s(f, &vdev->isr);
848 qemu_put_be16s(f, &vdev->queue_sel);
704a76fc 849 qemu_put_be32s(f, &vdev->guest_features);
967f97fa
AL
850 qemu_put_be32(f, vdev->config_len);
851 qemu_put_buffer(f, vdev->config, vdev->config_len);
852
853 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
854 if (vdev->vq[i].vring.num == 0)
855 break;
856 }
857
858 qemu_put_be32(f, i);
859
860 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
861 if (vdev->vq[i].vring.num == 0)
862 break;
863
864 qemu_put_be32(f, vdev->vq[i].vring.num);
6ce69d1c
PM
865 if (k->has_variable_vring_alignment) {
866 qemu_put_be32(f, vdev->vq[i].vring.align);
867 }
53c25cea 868 qemu_put_be64(f, vdev->vq[i].pa);
967f97fa 869 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1c819449
FK
870 if (k->save_queue) {
871 k->save_queue(qbus->parent, i, f);
872 }
967f97fa
AL
873 }
874}
875
ad0c9332
PB
876int virtio_set_features(VirtIODevice *vdev, uint32_t val)
877{
1c819449
FK
878 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
879 VirtioBusClass *vbusk = VIRTIO_BUS_GET_CLASS(qbus);
181103cd 880 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1c819449 881 uint32_t supported_features = vbusk->get_features(qbus->parent);
ad0c9332
PB
882 bool bad = (val & ~supported_features) != 0;
883
884 val &= supported_features;
181103cd
FK
885 if (k->set_features) {
886 k->set_features(vdev, val);
ad0c9332
PB
887 }
888 vdev->guest_features = val;
889 return bad ? -1 : 0;
890}
891
ff24bd58 892int virtio_load(VirtIODevice *vdev, QEMUFile *f)
967f97fa 893{
cc459952
MT
894 int i, ret;
895 uint32_t num;
6d74ca5a 896 uint32_t features;
ad0c9332 897 uint32_t supported_features;
1c819449
FK
898 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
899 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
967f97fa 900
1c819449
FK
901 if (k->load_config) {
902 ret = k->load_config(qbus->parent, f);
ff24bd58
MT
903 if (ret)
904 return ret;
905 }
967f97fa 906
967f97fa
AL
907 qemu_get_8s(f, &vdev->status);
908 qemu_get_8s(f, &vdev->isr);
909 qemu_get_be16s(f, &vdev->queue_sel);
4b53c2c7
MR
910 if (vdev->queue_sel >= VIRTIO_PCI_QUEUE_MAX) {
911 return -1;
912 }
6d74ca5a 913 qemu_get_be32s(f, &features);
ad0c9332
PB
914
915 if (virtio_set_features(vdev, features) < 0) {
1c819449 916 supported_features = k->get_features(qbus->parent);
ce67ed65
SH
917 error_report("Features 0x%x unsupported. Allowed features: 0x%x",
918 features, supported_features);
6d74ca5a
MT
919 return -1;
920 }
967f97fa
AL
921 vdev->config_len = qemu_get_be32(f);
922 qemu_get_buffer(f, vdev->config, vdev->config_len);
923
924 num = qemu_get_be32(f);
925
cc459952
MT
926 if (num > VIRTIO_PCI_QUEUE_MAX) {
927 error_report("Invalid number of PCI queues: 0x%x", num);
928 return -1;
929 }
930
967f97fa
AL
931 for (i = 0; i < num; i++) {
932 vdev->vq[i].vring.num = qemu_get_be32(f);
6ce69d1c
PM
933 if (k->has_variable_vring_alignment) {
934 vdev->vq[i].vring.align = qemu_get_be32(f);
935 }
53c25cea 936 vdev->vq[i].pa = qemu_get_be64(f);
967f97fa 937 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
bcbabae8
MT
938 vdev->vq[i].signalled_used_valid = false;
939 vdev->vq[i].notification = true;
967f97fa 940
53c25cea 941 if (vdev->vq[i].pa) {
1abeb5a6 942 uint16_t nheads;
53c25cea 943 virtqueue_init(&vdev->vq[i]);
1abeb5a6
MT
944 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
945 /* Check it isn't doing very strange things with descriptor numbers. */
946 if (nheads > vdev->vq[i].vring.num) {
947 error_report("VQ %d size 0x%x Guest index 0x%x "
6daf194d 948 "inconsistent with Host index 0x%x: delta 0x%x",
1abeb5a6
MT
949 i, vdev->vq[i].vring.num,
950 vring_avail_idx(&vdev->vq[i]),
951 vdev->vq[i].last_avail_idx, nheads);
952 return -1;
953 }
954 } else if (vdev->vq[i].last_avail_idx) {
955 error_report("VQ %d address 0x0 "
6daf194d 956 "inconsistent with Host index 0x%x",
1abeb5a6
MT
957 i, vdev->vq[i].last_avail_idx);
958 return -1;
258dc7c9 959 }
1c819449
FK
960 if (k->load_queue) {
961 ret = k->load_queue(qbus->parent, i, f);
ff24bd58
MT
962 if (ret)
963 return ret;
7055e687 964 }
967f97fa
AL
965 }
966
7055e687 967 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
ff24bd58 968 return 0;
967f97fa
AL
969}
970
6a1a8cc7 971void virtio_cleanup(VirtIODevice *vdev)
b946a153 972{
85cf2a8d 973 qemu_del_vm_change_state_handler(vdev->vmstate);
6f79e06b 974 g_free(vdev->config);
7267c094 975 g_free(vdev->vq);
8e05db92
FK
976}
977
1dfb4dd9 978static void virtio_vmstate_change(void *opaque, int running, RunState state)
85cf2a8d
MT
979{
980 VirtIODevice *vdev = opaque;
1c819449
FK
981 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
982 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
85cf2a8d
MT
983 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
984 vdev->vm_running = running;
985
986 if (backend_run) {
987 virtio_set_status(vdev, vdev->status);
988 }
989
1c819449
FK
990 if (k->vmstate_change) {
991 k->vmstate_change(qbus->parent, backend_run);
85cf2a8d
MT
992 }
993
994 if (!backend_run) {
995 virtio_set_status(vdev, vdev->status);
996 }
997}
998
8e05db92
FK
999void virtio_init(VirtIODevice *vdev, const char *name,
1000 uint16_t device_id, size_t config_size)
967f97fa 1001{
b8193adb 1002 int i;
53c25cea 1003 vdev->device_id = device_id;
967f97fa
AL
1004 vdev->status = 0;
1005 vdev->isr = 0;
1006 vdev->queue_sel = 0;
7055e687 1007 vdev->config_vector = VIRTIO_NO_VECTOR;
7267c094 1008 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
1354869c 1009 vdev->vm_running = runstate_is_running();
8e05db92 1010 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
b8193adb 1011 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1cbdabe2 1012 vdev->vq[i].vdev = vdev;
e78a2b42 1013 vdev->vq[i].queue_index = i;
1cbdabe2 1014 }
967f97fa 1015
967f97fa
AL
1016 vdev->name = name;
1017 vdev->config_len = config_size;
8e05db92 1018 if (vdev->config_len) {
7267c094 1019 vdev->config = g_malloc0(config_size);
8e05db92 1020 } else {
967f97fa 1021 vdev->config = NULL;
8e05db92
FK
1022 }
1023 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
1024 vdev);
1025}
967f97fa 1026
a8170e5e 1027hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1028{
1029 return vdev->vq[n].vring.desc;
1030}
1031
a8170e5e 1032hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1033{
1034 return vdev->vq[n].vring.avail;
1035}
1036
a8170e5e 1037hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1038{
1039 return vdev->vq[n].vring.used;
1040}
1041
a8170e5e 1042hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1043{
1044 return vdev->vq[n].vring.desc;
1045}
1046
a8170e5e 1047hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1048{
1049 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
1050}
1051
a8170e5e 1052hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1053{
1054 return offsetof(VRingAvail, ring) +
2b3af999 1055 sizeof(uint64_t) * vdev->vq[n].vring.num;
1cbdabe2
MT
1056}
1057
a8170e5e 1058hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1059{
1060 return offsetof(VRingUsed, ring) +
1061 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
1062}
1063
a8170e5e 1064hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1065{
1066 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
1067 virtio_queue_get_used_size(vdev, n);
1068}
1069
1070uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1071{
1072 return vdev->vq[n].last_avail_idx;
1073}
1074
1075void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
1076{
1077 vdev->vq[n].last_avail_idx = idx;
1078}
1079
6793dfd1
SH
1080void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
1081{
1082 vdev->vq[n].signalled_used_valid = false;
1083}
1084
1cbdabe2
MT
1085VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
1086{
1087 return vdev->vq + n;
1088}
1089
e78a2b42
JW
1090uint16_t virtio_get_queue_index(VirtQueue *vq)
1091{
1092 return vq->queue_index;
1093}
1094
15b2bd18
PB
1095static void virtio_queue_guest_notifier_read(EventNotifier *n)
1096{
1097 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
1098 if (event_notifier_test_and_clear(n)) {
1099 virtio_irq(vq);
1100 }
1101}
1102
1103void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
1104 bool with_irqfd)
1105{
1106 if (assign && !with_irqfd) {
1107 event_notifier_set_handler(&vq->guest_notifier,
1108 virtio_queue_guest_notifier_read);
1109 } else {
1110 event_notifier_set_handler(&vq->guest_notifier, NULL);
1111 }
1112 if (!assign) {
1113 /* Test and clear notifier before closing it,
1114 * in case poll callback didn't have time to run. */
1115 virtio_queue_guest_notifier_read(&vq->guest_notifier);
1116 }
1117}
1118
1cbdabe2
MT
1119EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
1120{
1121 return &vq->guest_notifier;
1122}
b1f416aa
PB
1123
1124static void virtio_queue_host_notifier_read(EventNotifier *n)
1125{
1126 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
1127 if (event_notifier_test_and_clear(n)) {
1128 virtio_queue_notify_vq(vq);
1129 }
1130}
1131
26b9b5fe
PB
1132void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
1133 bool set_handler)
b1f416aa 1134{
26b9b5fe 1135 if (assign && set_handler) {
b1f416aa
PB
1136 event_notifier_set_handler(&vq->host_notifier,
1137 virtio_queue_host_notifier_read);
1138 } else {
1139 event_notifier_set_handler(&vq->host_notifier, NULL);
26b9b5fe
PB
1140 }
1141 if (!assign) {
b1f416aa
PB
1142 /* Test and clear notifier before after disabling event,
1143 * in case poll callback didn't have time to run. */
1144 virtio_queue_host_notifier_read(&vq->host_notifier);
1145 }
1146}
1147
1cbdabe2
MT
1148EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
1149{
1150 return &vq->host_notifier;
1151}
8e05db92 1152
1034e9cf
FK
1153void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
1154{
1155 if (vdev->bus_name) {
1156 g_free(vdev->bus_name);
1157 vdev->bus_name = NULL;
1158 }
1159
1160 if (bus_name) {
1161 vdev->bus_name = g_strdup(bus_name);
1162 }
1163}
1164
1d244b42
AF
1165static void virtio_device_realize(DeviceState *dev, Error **errp)
1166{
1167 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1168 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1169 Error *err = NULL;
1170
1d244b42
AF
1171 if (vdc->realize != NULL) {
1172 vdc->realize(dev, &err);
1173 if (err != NULL) {
1174 error_propagate(errp, err);
1175 return;
1176 }
8e05db92 1177 }
5e96f5d2 1178 virtio_bus_device_plugged(vdev);
8e05db92
FK
1179}
1180
1d244b42 1181static void virtio_device_unrealize(DeviceState *dev, Error **errp)
1034e9cf 1182{
1d244b42 1183 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
306ec6c3
AF
1184 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1185 Error *err = NULL;
1d244b42 1186
83d07047
PB
1187 virtio_bus_device_unplugged(vdev);
1188
306ec6c3
AF
1189 if (vdc->unrealize != NULL) {
1190 vdc->unrealize(dev, &err);
1191 if (err != NULL) {
1192 error_propagate(errp, err);
1193 return;
1194 }
5e96f5d2 1195 }
1d244b42 1196
1034e9cf
FK
1197 if (vdev->bus_name) {
1198 g_free(vdev->bus_name);
1199 vdev->bus_name = NULL;
1200 }
1034e9cf
FK
1201}
1202
8e05db92
FK
1203static void virtio_device_class_init(ObjectClass *klass, void *data)
1204{
1205 /* Set the default value here. */
1206 DeviceClass *dc = DEVICE_CLASS(klass);
1d244b42
AF
1207
1208 dc->realize = virtio_device_realize;
1209 dc->unrealize = virtio_device_unrealize;
8e05db92
FK
1210 dc->bus_type = TYPE_VIRTIO_BUS;
1211}
1212
1213static const TypeInfo virtio_device_info = {
1214 .name = TYPE_VIRTIO_DEVICE,
1215 .parent = TYPE_DEVICE,
1216 .instance_size = sizeof(VirtIODevice),
1217 .class_init = virtio_device_class_init,
1218 .abstract = true,
1219 .class_size = sizeof(VirtioDeviceClass),
1220};
1221
1222static void virtio_register_types(void)
1223{
1224 type_register_static(&virtio_device_info);
1225}
1226
1227type_init(virtio_register_types)
This page took 0.969452 seconds and 4 git commands to generate.