diff options
Diffstat (limited to 'tools/lguest/lguest.c')
-rw-r--r-- | tools/lguest/lguest.c | 2016 |
1 files changed, 1677 insertions, 339 deletions
diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c index 32cf2ce15d69..e44052483ed9 100644 --- a/tools/lguest/lguest.c +++ b/tools/lguest/lguest.c | |||
@@ -41,6 +41,8 @@ | |||
41 | #include <signal.h> | 41 | #include <signal.h> |
42 | #include <pwd.h> | 42 | #include <pwd.h> |
43 | #include <grp.h> | 43 | #include <grp.h> |
44 | #include <sys/user.h> | ||
45 | #include <linux/pci_regs.h> | ||
44 | 46 | ||
45 | #ifndef VIRTIO_F_ANY_LAYOUT | 47 | #ifndef VIRTIO_F_ANY_LAYOUT |
46 | #define VIRTIO_F_ANY_LAYOUT 27 | 48 | #define VIRTIO_F_ANY_LAYOUT 27 |
@@ -61,12 +63,19 @@ typedef uint16_t u16; | |||
61 | typedef uint8_t u8; | 63 | typedef uint8_t u8; |
62 | /*:*/ | 64 | /*:*/ |
63 | 65 | ||
64 | #include <linux/virtio_config.h> | 66 | #define VIRTIO_CONFIG_NO_LEGACY |
65 | #include <linux/virtio_net.h> | 67 | #define VIRTIO_PCI_NO_LEGACY |
66 | #include <linux/virtio_blk.h> | 68 | #define VIRTIO_BLK_NO_LEGACY |
67 | #include <linux/virtio_console.h> | 69 | #define VIRTIO_NET_NO_LEGACY |
68 | #include <linux/virtio_rng.h> | 70 | |
71 | /* Use in-kernel ones, which defines VIRTIO_F_VERSION_1 */ | ||
72 | #include "../../include/uapi/linux/virtio_config.h" | ||
73 | #include "../../include/uapi/linux/virtio_net.h" | ||
74 | #include "../../include/uapi/linux/virtio_blk.h" | ||
75 | #include "../../include/uapi/linux/virtio_console.h" | ||
76 | #include "../../include/uapi/linux/virtio_rng.h" | ||
69 | #include <linux/virtio_ring.h> | 77 | #include <linux/virtio_ring.h> |
78 | #include "../../include/uapi/linux/virtio_pci.h" | ||
70 | #include <asm/bootparam.h> | 79 | #include <asm/bootparam.h> |
71 | #include "../../include/linux/lguest_launcher.h" | 80 | #include "../../include/linux/lguest_launcher.h" |
72 | 81 | ||
@@ -91,13 +100,16 @@ static bool verbose; | |||
91 | /* The pointer to the start of guest memory. */ | 100 | /* The pointer to the start of guest memory. */ |
92 | static void *guest_base; | 101 | static void *guest_base; |
93 | /* The maximum guest physical address allowed, and maximum possible. */ | 102 | /* The maximum guest physical address allowed, and maximum possible. */ |
94 | static unsigned long guest_limit, guest_max; | 103 | static unsigned long guest_limit, guest_max, guest_mmio; |
95 | /* The /dev/lguest file descriptor. */ | 104 | /* The /dev/lguest file descriptor. */ |
96 | static int lguest_fd; | 105 | static int lguest_fd; |
97 | 106 | ||
98 | /* a per-cpu variable indicating whose vcpu is currently running */ | 107 | /* a per-cpu variable indicating whose vcpu is currently running */ |
99 | static unsigned int __thread cpu_id; | 108 | static unsigned int __thread cpu_id; |
100 | 109 | ||
110 | /* 5 bit device number in the PCI_CONFIG_ADDR => 32 only */ | ||
111 | #define MAX_PCI_DEVICES 32 | ||
112 | |||
101 | /* This is our list of devices. */ | 113 | /* This is our list of devices. */ |
102 | struct device_list { | 114 | struct device_list { |
103 | /* Counter to assign interrupt numbers. */ | 115 | /* Counter to assign interrupt numbers. */ |
@@ -106,30 +118,50 @@ struct device_list { | |||
106 | /* Counter to print out convenient device numbers. */ | 118 | /* Counter to print out convenient device numbers. */ |
107 | unsigned int device_num; | 119 | unsigned int device_num; |
108 | 120 | ||
109 | /* The descriptor page for the devices. */ | 121 | /* PCI devices. */ |
110 | u8 *descpage; | 122 | struct device *pci[MAX_PCI_DEVICES]; |
111 | |||
112 | /* A single linked list of devices. */ | ||
113 | struct device *dev; | ||
114 | /* And a pointer to the last device for easy append. */ | ||
115 | struct device *lastdev; | ||
116 | }; | 123 | }; |
117 | 124 | ||
118 | /* The list of Guest devices, based on command line arguments. */ | 125 | /* The list of Guest devices, based on command line arguments. */ |
119 | static struct device_list devices; | 126 | static struct device_list devices; |
120 | 127 | ||
121 | /* The device structure describes a single device. */ | 128 | struct virtio_pci_cfg_cap { |
122 | struct device { | 129 | struct virtio_pci_cap cap; |
123 | /* The linked-list pointer. */ | 130 | u32 pci_cfg_data; /* Data for BAR access. */ |
124 | struct device *next; | 131 | }; |
125 | 132 | ||
126 | /* The device's descriptor, as mapped into the Guest. */ | 133 | struct virtio_pci_mmio { |
127 | struct lguest_device_desc *desc; | 134 | struct virtio_pci_common_cfg cfg; |
135 | u16 notify; | ||
136 | u8 isr; | ||
137 | u8 padding; | ||
138 | /* Device-specific configuration follows this. */ | ||
139 | }; | ||
128 | 140 | ||
129 | /* We can't trust desc values once Guest has booted: we use these. */ | 141 | /* This is the layout (little-endian) of the PCI config space. */ |
130 | unsigned int feature_len; | 142 | struct pci_config { |
131 | unsigned int num_vq; | 143 | u16 vendor_id, device_id; |
144 | u16 command, status; | ||
145 | u8 revid, prog_if, subclass, class; | ||
146 | u8 cacheline_size, lat_timer, header_type, bist; | ||
147 | u32 bar[6]; | ||
148 | u32 cardbus_cis_ptr; | ||
149 | u16 subsystem_vendor_id, subsystem_device_id; | ||
150 | u32 expansion_rom_addr; | ||
151 | u8 capabilities, reserved1[3]; | ||
152 | u32 reserved2; | ||
153 | u8 irq_line, irq_pin, min_grant, max_latency; | ||
154 | |||
155 | /* Now, this is the linked capability list. */ | ||
156 | struct virtio_pci_cap common; | ||
157 | struct virtio_pci_notify_cap notify; | ||
158 | struct virtio_pci_cap isr; | ||
159 | struct virtio_pci_cap device; | ||
160 | struct virtio_pci_cfg_cap cfg_access; | ||
161 | }; | ||
132 | 162 | ||
163 | /* The device structure describes a single device. */ | ||
164 | struct device { | ||
133 | /* The name of this device, for --verbose. */ | 165 | /* The name of this device, for --verbose. */ |
134 | const char *name; | 166 | const char *name; |
135 | 167 | ||
@@ -139,6 +171,25 @@ struct device { | |||
139 | /* Is it operational */ | 171 | /* Is it operational */ |
140 | bool running; | 172 | bool running; |
141 | 173 | ||
174 | /* Has it written FEATURES_OK but not re-checked it? */ | ||
175 | bool wrote_features_ok; | ||
176 | |||
177 | /* PCI configuration */ | ||
178 | union { | ||
179 | struct pci_config config; | ||
180 | u32 config_words[sizeof(struct pci_config) / sizeof(u32)]; | ||
181 | }; | ||
182 | |||
183 | /* Features we offer, and those accepted. */ | ||
184 | u64 features, features_accepted; | ||
185 | |||
186 | /* Device-specific config hangs off the end of this. */ | ||
187 | struct virtio_pci_mmio *mmio; | ||
188 | |||
189 | /* PCI MMIO resources (all in BAR0) */ | ||
190 | size_t mmio_size; | ||
191 | u32 mmio_addr; | ||
192 | |||
142 | /* Device-specific data. */ | 193 | /* Device-specific data. */ |
143 | void *priv; | 194 | void *priv; |
144 | }; | 195 | }; |
@@ -150,12 +201,15 @@ struct virtqueue { | |||
150 | /* Which device owns me. */ | 201 | /* Which device owns me. */ |
151 | struct device *dev; | 202 | struct device *dev; |
152 | 203 | ||
153 | /* The configuration for this queue. */ | 204 | /* Name for printing errors. */ |
154 | struct lguest_vqconfig config; | 205 | const char *name; |
155 | 206 | ||
156 | /* The actual ring of buffers. */ | 207 | /* The actual ring of buffers. */ |
157 | struct vring vring; | 208 | struct vring vring; |
158 | 209 | ||
210 | /* The information about this virtqueue (we only use queue_size on) */ | ||
211 | struct virtio_pci_common_cfg pci_config; | ||
212 | |||
159 | /* Last available index we saw. */ | 213 | /* Last available index we saw. */ |
160 | u16 last_avail_idx; | 214 | u16 last_avail_idx; |
161 | 215 | ||
@@ -199,6 +253,16 @@ static struct termios orig_term; | |||
199 | #define le32_to_cpu(v32) (v32) | 253 | #define le32_to_cpu(v32) (v32) |
200 | #define le64_to_cpu(v64) (v64) | 254 | #define le64_to_cpu(v64) (v64) |
201 | 255 | ||
256 | /* | ||
257 | * A real device would ignore weird/non-compliant driver behaviour. We | ||
258 | * stop and flag it, to help debugging Linux problems. | ||
259 | */ | ||
260 | #define bad_driver(d, fmt, ...) \ | ||
261 | errx(1, "%s: bad driver: " fmt, (d)->name, ## __VA_ARGS__) | ||
262 | #define bad_driver_vq(vq, fmt, ...) \ | ||
263 | errx(1, "%s vq %s: bad driver: " fmt, (vq)->dev->name, \ | ||
264 | vq->name, ## __VA_ARGS__) | ||
265 | |||
202 | /* Is this iovec empty? */ | 266 | /* Is this iovec empty? */ |
203 | static bool iov_empty(const struct iovec iov[], unsigned int num_iov) | 267 | static bool iov_empty(const struct iovec iov[], unsigned int num_iov) |
204 | { | 268 | { |
@@ -211,7 +275,8 @@ static bool iov_empty(const struct iovec iov[], unsigned int num_iov) | |||
211 | } | 275 | } |
212 | 276 | ||
213 | /* Take len bytes from the front of this iovec. */ | 277 | /* Take len bytes from the front of this iovec. */ |
214 | static void iov_consume(struct iovec iov[], unsigned num_iov, | 278 | static void iov_consume(struct device *d, |
279 | struct iovec iov[], unsigned num_iov, | ||
215 | void *dest, unsigned len) | 280 | void *dest, unsigned len) |
216 | { | 281 | { |
217 | unsigned int i; | 282 | unsigned int i; |
@@ -229,14 +294,7 @@ static void iov_consume(struct iovec iov[], unsigned num_iov, | |||
229 | len -= used; | 294 | len -= used; |
230 | } | 295 | } |
231 | if (len != 0) | 296 | if (len != 0) |
232 | errx(1, "iovec too short!"); | 297 | bad_driver(d, "iovec too short!"); |
233 | } | ||
234 | |||
235 | /* The device virtqueue descriptors are followed by feature bitmasks. */ | ||
236 | static u8 *get_feature_bits(struct device *dev) | ||
237 | { | ||
238 | return (u8 *)(dev->desc + 1) | ||
239 | + dev->num_vq * sizeof(struct lguest_vqconfig); | ||
240 | } | 298 | } |
241 | 299 | ||
242 | /*L:100 | 300 | /*L:100 |
@@ -309,14 +367,20 @@ static void *map_zeroed_pages(unsigned int num) | |||
309 | return addr + getpagesize(); | 367 | return addr + getpagesize(); |
310 | } | 368 | } |
311 | 369 | ||
312 | /* Get some more pages for a device. */ | 370 | /* Get some bytes which won't be mapped into the guest. */ |
313 | static void *get_pages(unsigned int num) | 371 | static unsigned long get_mmio_region(size_t size) |
314 | { | 372 | { |
315 | void *addr = from_guest_phys(guest_limit); | 373 | unsigned long addr = guest_mmio; |
374 | size_t i; | ||
375 | |||
376 | if (!size) | ||
377 | return addr; | ||
378 | |||
379 | /* Size has to be a power of 2 (and multiple of 16) */ | ||
380 | for (i = 1; i < size; i <<= 1); | ||
381 | |||
382 | guest_mmio += i; | ||
316 | 383 | ||
317 | guest_limit += num * getpagesize(); | ||
318 | if (guest_limit > guest_max) | ||
319 | errx(1, "Not enough memory for devices"); | ||
320 | return addr; | 384 | return addr; |
321 | } | 385 | } |
322 | 386 | ||
@@ -547,9 +611,11 @@ static void tell_kernel(unsigned long start) | |||
547 | { | 611 | { |
548 | unsigned long args[] = { LHREQ_INITIALIZE, | 612 | unsigned long args[] = { LHREQ_INITIALIZE, |
549 | (unsigned long)guest_base, | 613 | (unsigned long)guest_base, |
550 | guest_limit / getpagesize(), start }; | 614 | guest_limit / getpagesize(), start, |
551 | verbose("Guest: %p - %p (%#lx)\n", | 615 | (guest_mmio+getpagesize()-1) / getpagesize() }; |
552 | guest_base, guest_base + guest_limit, guest_limit); | 616 | verbose("Guest: %p - %p (%#lx, MMIO %#lx)\n", |
617 | guest_base, guest_base + guest_limit, | ||
618 | guest_limit, guest_mmio); | ||
553 | lguest_fd = open_or_die("/dev/lguest", O_RDWR); | 619 | lguest_fd = open_or_die("/dev/lguest", O_RDWR); |
554 | if (write(lguest_fd, args, sizeof(args)) < 0) | 620 | if (write(lguest_fd, args, sizeof(args)) < 0) |
555 | err(1, "Writing to /dev/lguest"); | 621 | err(1, "Writing to /dev/lguest"); |
@@ -564,7 +630,8 @@ static void tell_kernel(unsigned long start) | |||
564 | * we have a convenient routine which checks it and exits with an error message | 630 | * we have a convenient routine which checks it and exits with an error message |
565 | * if something funny is going on: | 631 | * if something funny is going on: |
566 | */ | 632 | */ |
567 | static void *_check_pointer(unsigned long addr, unsigned int size, | 633 | static void *_check_pointer(struct device *d, |
634 | unsigned long addr, unsigned int size, | ||
568 | unsigned int line) | 635 | unsigned int line) |
569 | { | 636 | { |
570 | /* | 637 | /* |
@@ -572,7 +639,8 @@ static void *_check_pointer(unsigned long addr, unsigned int size, | |||
572 | * or addr + size wraps around. | 639 | * or addr + size wraps around. |
573 | */ | 640 | */ |
574 | if ((addr + size) > guest_limit || (addr + size) < addr) | 641 | if ((addr + size) > guest_limit || (addr + size) < addr) |
575 | errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr); | 642 | bad_driver(d, "%s:%i: Invalid address %#lx", |
643 | __FILE__, line, addr); | ||
576 | /* | 644 | /* |
577 | * We return a pointer for the caller's convenience, now we know it's | 645 | * We return a pointer for the caller's convenience, now we know it's |
578 | * safe to use. | 646 | * safe to use. |
@@ -580,14 +648,14 @@ static void *_check_pointer(unsigned long addr, unsigned int size, | |||
580 | return from_guest_phys(addr); | 648 | return from_guest_phys(addr); |
581 | } | 649 | } |
582 | /* A macro which transparently hands the line number to the real function. */ | 650 | /* A macro which transparently hands the line number to the real function. */ |
583 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) | 651 | #define check_pointer(d,addr,size) _check_pointer(d, addr, size, __LINE__) |
584 | 652 | ||
585 | /* | 653 | /* |
586 | * Each buffer in the virtqueues is actually a chain of descriptors. This | 654 | * Each buffer in the virtqueues is actually a chain of descriptors. This |
587 | * function returns the next descriptor in the chain, or vq->vring.num if we're | 655 | * function returns the next descriptor in the chain, or vq->vring.num if we're |
588 | * at the end. | 656 | * at the end. |
589 | */ | 657 | */ |
590 | static unsigned next_desc(struct vring_desc *desc, | 658 | static unsigned next_desc(struct device *d, struct vring_desc *desc, |
591 | unsigned int i, unsigned int max) | 659 | unsigned int i, unsigned int max) |
592 | { | 660 | { |
593 | unsigned int next; | 661 | unsigned int next; |
@@ -602,7 +670,7 @@ static unsigned next_desc(struct vring_desc *desc, | |||
602 | wmb(); | 670 | wmb(); |
603 | 671 | ||
604 | if (next >= max) | 672 | if (next >= max) |
605 | errx(1, "Desc next is %u", next); | 673 | bad_driver(d, "Desc next is %u", next); |
606 | 674 | ||
607 | return next; | 675 | return next; |
608 | } | 676 | } |
@@ -613,21 +681,48 @@ static unsigned next_desc(struct vring_desc *desc, | |||
613 | */ | 681 | */ |
614 | static void trigger_irq(struct virtqueue *vq) | 682 | static void trigger_irq(struct virtqueue *vq) |
615 | { | 683 | { |
616 | unsigned long buf[] = { LHREQ_IRQ, vq->config.irq }; | 684 | unsigned long buf[] = { LHREQ_IRQ, vq->dev->config.irq_line }; |
617 | 685 | ||
618 | /* Don't inform them if nothing used. */ | 686 | /* Don't inform them if nothing used. */ |
619 | if (!vq->pending_used) | 687 | if (!vq->pending_used) |
620 | return; | 688 | return; |
621 | vq->pending_used = 0; | 689 | vq->pending_used = 0; |
622 | 690 | ||
623 | /* If they don't want an interrupt, don't send one... */ | 691 | /* |
692 | * 2.4.7.1: | ||
693 | * | ||
694 | * If the VIRTIO_F_EVENT_IDX feature bit is not negotiated: | ||
695 | * The driver MUST set flags to 0 or 1. | ||
696 | */ | ||
697 | if (vq->vring.avail->flags > 1) | ||
698 | bad_driver_vq(vq, "avail->flags = %u\n", vq->vring.avail->flags); | ||
699 | |||
700 | /* | ||
701 | * 2.4.7.2: | ||
702 | * | ||
703 | * If the VIRTIO_F_EVENT_IDX feature bit is not negotiated: | ||
704 | * | ||
705 | * - The device MUST ignore the used_event value. | ||
706 | * - After the device writes a descriptor index into the used ring: | ||
707 | * - If flags is 1, the device SHOULD NOT send an interrupt. | ||
708 | * - If flags is 0, the device MUST send an interrupt. | ||
709 | */ | ||
624 | if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) { | 710 | if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) { |
625 | return; | 711 | return; |
626 | } | 712 | } |
627 | 713 | ||
714 | /* | ||
715 | * 4.1.4.5.1: | ||
716 | * | ||
717 | * If MSI-X capability is disabled, the device MUST set the Queue | ||
718 | * Interrupt bit in ISR status before sending a virtqueue notification | ||
719 | * to the driver. | ||
720 | */ | ||
721 | vq->dev->mmio->isr = 0x1; | ||
722 | |||
628 | /* Send the Guest an interrupt tell them we used something up. */ | 723 | /* Send the Guest an interrupt tell them we used something up. */ |
629 | if (write(lguest_fd, buf, sizeof(buf)) != 0) | 724 | if (write(lguest_fd, buf, sizeof(buf)) != 0) |
630 | err(1, "Triggering irq %i", vq->config.irq); | 725 | err(1, "Triggering irq %i", vq->dev->config.irq_line); |
631 | } | 726 | } |
632 | 727 | ||
633 | /* | 728 | /* |
@@ -646,6 +741,14 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
646 | struct vring_desc *desc; | 741 | struct vring_desc *desc; |
647 | u16 last_avail = lg_last_avail(vq); | 742 | u16 last_avail = lg_last_avail(vq); |
648 | 743 | ||
744 | /* | ||
745 | * 2.4.7.1: | ||
746 | * | ||
747 | * The driver MUST handle spurious interrupts from the device. | ||
748 | * | ||
749 | * That's why this is a while loop. | ||
750 | */ | ||
751 | |||
649 | /* There's nothing available? */ | 752 | /* There's nothing available? */ |
650 | while (last_avail == vq->vring.avail->idx) { | 753 | while (last_avail == vq->vring.avail->idx) { |
651 | u64 event; | 754 | u64 event; |
@@ -679,8 +782,8 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
679 | 782 | ||
680 | /* Check it isn't doing very strange things with descriptor numbers. */ | 783 | /* Check it isn't doing very strange things with descriptor numbers. */ |
681 | if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num) | 784 | if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num) |
682 | errx(1, "Guest moved used index from %u to %u", | 785 | bad_driver_vq(vq, "Guest moved used index from %u to %u", |
683 | last_avail, vq->vring.avail->idx); | 786 | last_avail, vq->vring.avail->idx); |
684 | 787 | ||
685 | /* | 788 | /* |
686 | * Make sure we read the descriptor number *after* we read the ring | 789 | * Make sure we read the descriptor number *after* we read the ring |
@@ -697,7 +800,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
697 | 800 | ||
698 | /* If their number is silly, that's a fatal mistake. */ | 801 | /* If their number is silly, that's a fatal mistake. */ |
699 | if (head >= vq->vring.num) | 802 | if (head >= vq->vring.num) |
700 | errx(1, "Guest says index %u is available", head); | 803 | bad_driver_vq(vq, "Guest says index %u is available", head); |
701 | 804 | ||
702 | /* When we start there are none of either input nor output. */ | 805 | /* When we start there are none of either input nor output. */ |
703 | *out_num = *in_num = 0; | 806 | *out_num = *in_num = 0; |
@@ -712,24 +815,73 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
712 | * that: no rmb() required. | 815 | * that: no rmb() required. |
713 | */ | 816 | */ |
714 | 817 | ||
715 | /* | 818 | do { |
716 | * If this is an indirect entry, then this buffer contains a descriptor | 819 | /* |
717 | * table which we handle as if it's any normal descriptor chain. | 820 | * If this is an indirect entry, then this buffer contains a |
718 | */ | 821 | * descriptor table which we handle as if it's any normal |
719 | if (desc[i].flags & VRING_DESC_F_INDIRECT) { | 822 | * descriptor chain. |
720 | if (desc[i].len % sizeof(struct vring_desc)) | 823 | */ |
721 | errx(1, "Invalid size for indirect buffer table"); | 824 | if (desc[i].flags & VRING_DESC_F_INDIRECT) { |
825 | /* 2.4.5.3.1: | ||
826 | * | ||
827 | * The driver MUST NOT set the VIRTQ_DESC_F_INDIRECT | ||
828 | * flag unless the VIRTIO_F_INDIRECT_DESC feature was | ||
829 | * negotiated. | ||
830 | */ | ||
831 | if (!(vq->dev->features_accepted & | ||
832 | (1<<VIRTIO_RING_F_INDIRECT_DESC))) | ||
833 | bad_driver_vq(vq, "vq indirect not negotiated"); | ||
722 | 834 | ||
723 | max = desc[i].len / sizeof(struct vring_desc); | 835 | /* |
724 | desc = check_pointer(desc[i].addr, desc[i].len); | 836 | * 2.4.5.3.1: |
725 | i = 0; | 837 | * |
726 | } | 838 | * The driver MUST NOT set the VIRTQ_DESC_F_INDIRECT |
839 | * flag within an indirect descriptor (ie. only one | ||
840 | * table per descriptor). | ||
841 | */ | ||
842 | if (desc != vq->vring.desc) | ||
843 | bad_driver_vq(vq, "Indirect within indirect"); | ||
844 | |||
845 | /* | ||
846 | * Proposed update VIRTIO-134 spells this out: | ||
847 | * | ||
848 | * A driver MUST NOT set both VIRTQ_DESC_F_INDIRECT | ||
849 | * and VIRTQ_DESC_F_NEXT in flags. | ||
850 | */ | ||
851 | if (desc[i].flags & VRING_DESC_F_NEXT) | ||
852 | bad_driver_vq(vq, "indirect and next together"); | ||
853 | |||
854 | if (desc[i].len % sizeof(struct vring_desc)) | ||
855 | bad_driver_vq(vq, | ||
856 | "Invalid size for indirect table"); | ||
857 | /* | ||
858 | * 2.4.5.3.2: | ||
859 | * | ||
860 | * The device MUST ignore the write-only flag | ||
861 | * (flags&VIRTQ_DESC_F_WRITE) in the descriptor that | ||
862 | * refers to an indirect table. | ||
863 | * | ||
864 | * We ignore it here: :) | ||
865 | */ | ||
866 | |||
867 | max = desc[i].len / sizeof(struct vring_desc); | ||
868 | desc = check_pointer(vq->dev, desc[i].addr, desc[i].len); | ||
869 | i = 0; | ||
870 | |||
871 | /* 2.4.5.3.1: | ||
872 | * | ||
873 | * A driver MUST NOT create a descriptor chain longer | ||
874 | * than the Queue Size of the device. | ||
875 | */ | ||
876 | if (max > vq->pci_config.queue_size) | ||
877 | bad_driver_vq(vq, | ||
878 | "indirect has too many entries"); | ||
879 | } | ||
727 | 880 | ||
728 | do { | ||
729 | /* Grab the first descriptor, and check it's OK. */ | 881 | /* Grab the first descriptor, and check it's OK. */ |
730 | iov[*out_num + *in_num].iov_len = desc[i].len; | 882 | iov[*out_num + *in_num].iov_len = desc[i].len; |
731 | iov[*out_num + *in_num].iov_base | 883 | iov[*out_num + *in_num].iov_base |
732 | = check_pointer(desc[i].addr, desc[i].len); | 884 | = check_pointer(vq->dev, desc[i].addr, desc[i].len); |
733 | /* If this is an input descriptor, increment that count. */ | 885 | /* If this is an input descriptor, increment that count. */ |
734 | if (desc[i].flags & VRING_DESC_F_WRITE) | 886 | if (desc[i].flags & VRING_DESC_F_WRITE) |
735 | (*in_num)++; | 887 | (*in_num)++; |
@@ -739,14 +891,15 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
739 | * to come before any input descriptors. | 891 | * to come before any input descriptors. |
740 | */ | 892 | */ |
741 | if (*in_num) | 893 | if (*in_num) |
742 | errx(1, "Descriptor has out after in"); | 894 | bad_driver_vq(vq, |
895 | "Descriptor has out after in"); | ||
743 | (*out_num)++; | 896 | (*out_num)++; |
744 | } | 897 | } |
745 | 898 | ||
746 | /* If we've got too many, that implies a descriptor loop. */ | 899 | /* If we've got too many, that implies a descriptor loop. */ |
747 | if (*out_num + *in_num > max) | 900 | if (*out_num + *in_num > max) |
748 | errx(1, "Looped descriptor"); | 901 | bad_driver_vq(vq, "Looped descriptor"); |
749 | } while ((i = next_desc(desc, i, max)) != max); | 902 | } while ((i = next_desc(vq->dev, desc, i, max)) != max); |
750 | 903 | ||
751 | return head; | 904 | return head; |
752 | } | 905 | } |
@@ -803,7 +956,7 @@ static void console_input(struct virtqueue *vq) | |||
803 | /* Make sure there's a descriptor available. */ | 956 | /* Make sure there's a descriptor available. */ |
804 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); | 957 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
805 | if (out_num) | 958 | if (out_num) |
806 | errx(1, "Output buffers in console in queue?"); | 959 | bad_driver_vq(vq, "Output buffers in console in queue?"); |
807 | 960 | ||
808 | /* Read into it. This is where we usually wait. */ | 961 | /* Read into it. This is where we usually wait. */ |
809 | len = readv(STDIN_FILENO, iov, in_num); | 962 | len = readv(STDIN_FILENO, iov, in_num); |
@@ -856,7 +1009,7 @@ static void console_output(struct virtqueue *vq) | |||
856 | /* We usually wait in here, for the Guest to give us something. */ | 1009 | /* We usually wait in here, for the Guest to give us something. */ |
857 | head = wait_for_vq_desc(vq, iov, &out, &in); | 1010 | head = wait_for_vq_desc(vq, iov, &out, &in); |
858 | if (in) | 1011 | if (in) |
859 | errx(1, "Input buffers in console output queue?"); | 1012 | bad_driver_vq(vq, "Input buffers in console output queue?"); |
860 | 1013 | ||
861 | /* writev can return a partial write, so we loop here. */ | 1014 | /* writev can return a partial write, so we loop here. */ |
862 | while (!iov_empty(iov, out)) { | 1015 | while (!iov_empty(iov, out)) { |
@@ -865,7 +1018,7 @@ static void console_output(struct virtqueue *vq) | |||
865 | warn("Write to stdout gave %i (%d)", len, errno); | 1018 | warn("Write to stdout gave %i (%d)", len, errno); |
866 | break; | 1019 | break; |
867 | } | 1020 | } |
868 | iov_consume(iov, out, NULL, len); | 1021 | iov_consume(vq->dev, iov, out, NULL, len); |
869 | } | 1022 | } |
870 | 1023 | ||
871 | /* | 1024 | /* |
@@ -894,7 +1047,7 @@ static void net_output(struct virtqueue *vq) | |||
894 | /* We usually wait in here for the Guest to give us a packet. */ | 1047 | /* We usually wait in here for the Guest to give us a packet. */ |
895 | head = wait_for_vq_desc(vq, iov, &out, &in); | 1048 | head = wait_for_vq_desc(vq, iov, &out, &in); |
896 | if (in) | 1049 | if (in) |
897 | errx(1, "Input buffers in net output queue?"); | 1050 | bad_driver_vq(vq, "Input buffers in net output queue?"); |
898 | /* | 1051 | /* |
899 | * Send the whole thing through to /dev/net/tun. It expects the exact | 1052 | * Send the whole thing through to /dev/net/tun. It expects the exact |
900 | * same format: what a coincidence! | 1053 | * same format: what a coincidence! |
@@ -942,7 +1095,7 @@ static void net_input(struct virtqueue *vq) | |||
942 | */ | 1095 | */ |
943 | head = wait_for_vq_desc(vq, iov, &out, &in); | 1096 | head = wait_for_vq_desc(vq, iov, &out, &in); |
944 | if (out) | 1097 | if (out) |
945 | errx(1, "Output buffers in net input queue?"); | 1098 | bad_driver_vq(vq, "Output buffers in net input queue?"); |
946 | 1099 | ||
947 | /* | 1100 | /* |
948 | * If it looks like we'll block reading from the tun device, send them | 1101 | * If it looks like we'll block reading from the tun device, send them |
@@ -986,6 +1139,12 @@ static void kill_launcher(int signal) | |||
986 | kill(0, SIGTERM); | 1139 | kill(0, SIGTERM); |
987 | } | 1140 | } |
988 | 1141 | ||
1142 | static void reset_vq_pci_config(struct virtqueue *vq) | ||
1143 | { | ||
1144 | vq->pci_config.queue_size = VIRTQUEUE_NUM; | ||
1145 | vq->pci_config.queue_enable = 0; | ||
1146 | } | ||
1147 | |||
989 | static void reset_device(struct device *dev) | 1148 | static void reset_device(struct device *dev) |
990 | { | 1149 | { |
991 | struct virtqueue *vq; | 1150 | struct virtqueue *vq; |
@@ -993,53 +1152,705 @@ static void reset_device(struct device *dev) | |||
993 | verbose("Resetting device %s\n", dev->name); | 1152 | verbose("Resetting device %s\n", dev->name); |
994 | 1153 | ||
995 | /* Clear any features they've acked. */ | 1154 | /* Clear any features they've acked. */ |
996 | memset(get_feature_bits(dev) + dev->feature_len, 0, dev->feature_len); | 1155 | dev->features_accepted = 0; |
997 | 1156 | ||
998 | /* We're going to be explicitly killing threads, so ignore them. */ | 1157 | /* We're going to be explicitly killing threads, so ignore them. */ |
999 | signal(SIGCHLD, SIG_IGN); | 1158 | signal(SIGCHLD, SIG_IGN); |
1000 | 1159 | ||
1001 | /* Zero out the virtqueues, get rid of their threads */ | 1160 | /* |
1161 | * 4.1.4.3.1: | ||
1162 | * | ||
1163 | * The device MUST present a 0 in queue_enable on reset. | ||
1164 | * | ||
1165 | * This means we set it here, and reset the saved ones in every vq. | ||
1166 | */ | ||
1167 | dev->mmio->cfg.queue_enable = 0; | ||
1168 | |||
1169 | /* Get rid of the virtqueue threads */ | ||
1002 | for (vq = dev->vq; vq; vq = vq->next) { | 1170 | for (vq = dev->vq; vq; vq = vq->next) { |
1171 | vq->last_avail_idx = 0; | ||
1172 | reset_vq_pci_config(vq); | ||
1003 | if (vq->thread != (pid_t)-1) { | 1173 | if (vq->thread != (pid_t)-1) { |
1004 | kill(vq->thread, SIGTERM); | 1174 | kill(vq->thread, SIGTERM); |
1005 | waitpid(vq->thread, NULL, 0); | 1175 | waitpid(vq->thread, NULL, 0); |
1006 | vq->thread = (pid_t)-1; | 1176 | vq->thread = (pid_t)-1; |
1007 | } | 1177 | } |
1008 | memset(vq->vring.desc, 0, | ||
1009 | vring_size(vq->config.num, LGUEST_VRING_ALIGN)); | ||
1010 | lg_last_avail(vq) = 0; | ||
1011 | } | 1178 | } |
1012 | dev->running = false; | 1179 | dev->running = false; |
1180 | dev->wrote_features_ok = false; | ||
1013 | 1181 | ||
1014 | /* Now we care if threads die. */ | 1182 | /* Now we care if threads die. */ |
1015 | signal(SIGCHLD, (void *)kill_launcher); | 1183 | signal(SIGCHLD, (void *)kill_launcher); |
1016 | } | 1184 | } |
1017 | 1185 | ||
1186 | static void cleanup_devices(void) | ||
1187 | { | ||
1188 | unsigned int i; | ||
1189 | |||
1190 | for (i = 1; i < MAX_PCI_DEVICES; i++) { | ||
1191 | struct device *d = devices.pci[i]; | ||
1192 | if (!d) | ||
1193 | continue; | ||
1194 | reset_device(d); | ||
1195 | } | ||
1196 | |||
1197 | /* If we saved off the original terminal settings, restore them now. */ | ||
1198 | if (orig_term.c_lflag & (ISIG|ICANON|ECHO)) | ||
1199 | tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); | ||
1200 | } | ||
1201 | |||
1202 | /*L:217 | ||
1203 | * We do PCI. This is mainly done to let us test the kernel virtio PCI | ||
1204 | * code. | ||
1205 | */ | ||
1206 | |||
1207 | /* Linux expects a PCI host bridge: ours is a dummy, and first on the bus. */ | ||
1208 | static struct device pci_host_bridge; | ||
1209 | |||
1210 | static void init_pci_host_bridge(void) | ||
1211 | { | ||
1212 | pci_host_bridge.name = "PCI Host Bridge"; | ||
1213 | pci_host_bridge.config.class = 0x06; /* bridge */ | ||
1214 | pci_host_bridge.config.subclass = 0; /* host bridge */ | ||
1215 | devices.pci[0] = &pci_host_bridge; | ||
1216 | } | ||
1217 | |||
1218 | /* The IO ports used to read the PCI config space. */ | ||
1219 | #define PCI_CONFIG_ADDR 0xCF8 | ||
1220 | #define PCI_CONFIG_DATA 0xCFC | ||
1221 | |||
1222 | /* | ||
1223 | * Not really portable, but does help readability: this is what the Guest | ||
1224 | * writes to the PCI_CONFIG_ADDR IO port. | ||
1225 | */ | ||
1226 | union pci_config_addr { | ||
1227 | struct { | ||
1228 | unsigned mbz: 2; | ||
1229 | unsigned offset: 6; | ||
1230 | unsigned funcnum: 3; | ||
1231 | unsigned devnum: 5; | ||
1232 | unsigned busnum: 8; | ||
1233 | unsigned reserved: 7; | ||
1234 | unsigned enabled : 1; | ||
1235 | } bits; | ||
1236 | u32 val; | ||
1237 | }; | ||
1238 | |||
1239 | /* | ||
1240 | * We cache what they wrote to the address port, so we know what they're | ||
1241 | * talking about when they access the data port. | ||
1242 | */ | ||
1243 | static union pci_config_addr pci_config_addr; | ||
1244 | |||
1245 | static struct device *find_pci_device(unsigned int index) | ||
1246 | { | ||
1247 | return devices.pci[index]; | ||
1248 | } | ||
1249 | |||
1250 | /* PCI can do 1, 2 and 4 byte reads; we handle that here. */ | ||
1251 | static void ioread(u16 off, u32 v, u32 mask, u32 *val) | ||
1252 | { | ||
1253 | assert(off < 4); | ||
1254 | assert(mask == 0xFF || mask == 0xFFFF || mask == 0xFFFFFFFF); | ||
1255 | *val = (v >> (off * 8)) & mask; | ||
1256 | } | ||
1257 | |||
1258 | /* PCI can do 1, 2 and 4 byte writes; we handle that here. */ | ||
1259 | static void iowrite(u16 off, u32 v, u32 mask, u32 *dst) | ||
1260 | { | ||
1261 | assert(off < 4); | ||
1262 | assert(mask == 0xFF || mask == 0xFFFF || mask == 0xFFFFFFFF); | ||
1263 | *dst &= ~(mask << (off * 8)); | ||
1264 | *dst |= (v & mask) << (off * 8); | ||
1265 | } | ||
1266 | |||
1267 | /* | ||
1268 | * Where PCI_CONFIG_DATA accesses depends on the previous write to | ||
1269 | * PCI_CONFIG_ADDR. | ||
1270 | */ | ||
1271 | static struct device *dev_and_reg(u32 *reg) | ||
1272 | { | ||
1273 | if (!pci_config_addr.bits.enabled) | ||
1274 | return NULL; | ||
1275 | |||
1276 | if (pci_config_addr.bits.funcnum != 0) | ||
1277 | return NULL; | ||
1278 | |||
1279 | if (pci_config_addr.bits.busnum != 0) | ||
1280 | return NULL; | ||
1281 | |||
1282 | if (pci_config_addr.bits.offset * 4 >= sizeof(struct pci_config)) | ||
1283 | return NULL; | ||
1284 | |||
1285 | *reg = pci_config_addr.bits.offset; | ||
1286 | return find_pci_device(pci_config_addr.bits.devnum); | ||
1287 | } | ||
1288 | |||
1289 | /* | ||
1290 | * We can get invalid combinations of values while they're writing, so we | ||
1291 | * only fault if they try to write with some invalid bar/offset/length. | ||
1292 | */ | ||
1293 | static bool valid_bar_access(struct device *d, | ||
1294 | struct virtio_pci_cfg_cap *cfg_access) | ||
1295 | { | ||
1296 | /* We only have 1 bar (BAR0) */ | ||
1297 | if (cfg_access->cap.bar != 0) | ||
1298 | return false; | ||
1299 | |||
1300 | /* Check it's within BAR0. */ | ||
1301 | if (cfg_access->cap.offset >= d->mmio_size | ||
1302 | || cfg_access->cap.offset + cfg_access->cap.length > d->mmio_size) | ||
1303 | return false; | ||
1304 | |||
1305 | /* Check length is 1, 2 or 4. */ | ||
1306 | if (cfg_access->cap.length != 1 | ||
1307 | && cfg_access->cap.length != 2 | ||
1308 | && cfg_access->cap.length != 4) | ||
1309 | return false; | ||
1310 | |||
1311 | /* | ||
1312 | * 4.1.4.7.2: | ||
1313 | * | ||
1314 | * The driver MUST NOT write a cap.offset which is not a multiple of | ||
1315 | * cap.length (ie. all accesses MUST be aligned). | ||
1316 | */ | ||
1317 | if (cfg_access->cap.offset % cfg_access->cap.length != 0) | ||
1318 | return false; | ||
1319 | |||
1320 | /* Return pointer into word in BAR0. */ | ||
1321 | return true; | ||
1322 | } | ||
1323 | |||
1324 | /* Is this accessing the PCI config address port?. */ | ||
1325 | static bool is_pci_addr_port(u16 port) | ||
1326 | { | ||
1327 | return port >= PCI_CONFIG_ADDR && port < PCI_CONFIG_ADDR + 4; | ||
1328 | } | ||
1329 | |||
1330 | static bool pci_addr_iowrite(u16 port, u32 mask, u32 val) | ||
1331 | { | ||
1332 | iowrite(port - PCI_CONFIG_ADDR, val, mask, | ||
1333 | &pci_config_addr.val); | ||
1334 | verbose("PCI%s: %#x/%x: bus %u dev %u func %u reg %u\n", | ||
1335 | pci_config_addr.bits.enabled ? "" : " DISABLED", | ||
1336 | val, mask, | ||
1337 | pci_config_addr.bits.busnum, | ||
1338 | pci_config_addr.bits.devnum, | ||
1339 | pci_config_addr.bits.funcnum, | ||
1340 | pci_config_addr.bits.offset); | ||
1341 | return true; | ||
1342 | } | ||
1343 | |||
1344 | static void pci_addr_ioread(u16 port, u32 mask, u32 *val) | ||
1345 | { | ||
1346 | ioread(port - PCI_CONFIG_ADDR, pci_config_addr.val, mask, val); | ||
1347 | } | ||
1348 | |||
1349 | /* Is this accessing the PCI config data port?. */ | ||
1350 | static bool is_pci_data_port(u16 port) | ||
1351 | { | ||
1352 | return port >= PCI_CONFIG_DATA && port < PCI_CONFIG_DATA + 4; | ||
1353 | } | ||
1354 | |||
1355 | static void emulate_mmio_write(struct device *d, u32 off, u32 val, u32 mask); | ||
1356 | |||
1357 | static bool pci_data_iowrite(u16 port, u32 mask, u32 val) | ||
1358 | { | ||
1359 | u32 reg, portoff; | ||
1360 | struct device *d = dev_and_reg(®); | ||
1361 | |||
1362 | /* Complain if they don't belong to a device. */ | ||
1363 | if (!d) | ||
1364 | return false; | ||
1365 | |||
1366 | /* They can do 1 byte writes, etc. */ | ||
1367 | portoff = port - PCI_CONFIG_DATA; | ||
1368 | |||
1369 | /* | ||
1370 | * PCI uses a weird way to determine the BAR size: the OS | ||
1371 | * writes all 1's, and sees which ones stick. | ||
1372 | */ | ||
1373 | if (&d->config_words[reg] == &d->config.bar[0]) { | ||
1374 | int i; | ||
1375 | |||
1376 | iowrite(portoff, val, mask, &d->config.bar[0]); | ||
1377 | for (i = 0; (1 << i) < d->mmio_size; i++) | ||
1378 | d->config.bar[0] &= ~(1 << i); | ||
1379 | return true; | ||
1380 | } else if ((&d->config_words[reg] > &d->config.bar[0] | ||
1381 | && &d->config_words[reg] <= &d->config.bar[6]) | ||
1382 | || &d->config_words[reg] == &d->config.expansion_rom_addr) { | ||
1383 | /* Allow writing to any other BAR, or expansion ROM */ | ||
1384 | iowrite(portoff, val, mask, &d->config_words[reg]); | ||
1385 | return true; | ||
1386 | /* We let them overide latency timer and cacheline size */ | ||
1387 | } else if (&d->config_words[reg] == (void *)&d->config.cacheline_size) { | ||
1388 | /* Only let them change the first two fields. */ | ||
1389 | if (mask == 0xFFFFFFFF) | ||
1390 | mask = 0xFFFF; | ||
1391 | iowrite(portoff, val, mask, &d->config_words[reg]); | ||
1392 | return true; | ||
1393 | } else if (&d->config_words[reg] == (void *)&d->config.command | ||
1394 | && mask == 0xFFFF) { | ||
1395 | /* Ignore command writes. */ | ||
1396 | return true; | ||
1397 | } else if (&d->config_words[reg] | ||
1398 | == (void *)&d->config.cfg_access.cap.bar | ||
1399 | || &d->config_words[reg] | ||
1400 | == &d->config.cfg_access.cap.length | ||
1401 | || &d->config_words[reg] | ||
1402 | == &d->config.cfg_access.cap.offset) { | ||
1403 | |||
1404 | /* | ||
1405 | * The VIRTIO_PCI_CAP_PCI_CFG capability | ||
1406 | * provides a backdoor to access the MMIO | ||
1407 | * regions without mapping them. Weird, but | ||
1408 | * useful. | ||
1409 | */ | ||
1410 | iowrite(portoff, val, mask, &d->config_words[reg]); | ||
1411 | return true; | ||
1412 | } else if (&d->config_words[reg] == &d->config.cfg_access.pci_cfg_data) { | ||
1413 | u32 write_mask; | ||
1414 | |||
1415 | /* | ||
1416 | * 4.1.4.7.1: | ||
1417 | * | ||
1418 | * Upon detecting driver write access to pci_cfg_data, the | ||
1419 | * device MUST execute a write access at offset cap.offset at | ||
1420 | * BAR selected by cap.bar using the first cap.length bytes | ||
1421 | * from pci_cfg_data. | ||
1422 | */ | ||
1423 | |||
1424 | /* Must be bar 0 */ | ||
1425 | if (!valid_bar_access(d, &d->config.cfg_access)) | ||
1426 | return false; | ||
1427 | |||
1428 | iowrite(portoff, val, mask, &d->config.cfg_access.pci_cfg_data); | ||
1429 | |||
1430 | /* | ||
1431 | * Now emulate a write. The mask we use is set by | ||
1432 | * len, *not* this write! | ||
1433 | */ | ||
1434 | write_mask = (1ULL<<(8*d->config.cfg_access.cap.length)) - 1; | ||
1435 | verbose("Window writing %#x/%#x to bar %u, offset %u len %u\n", | ||
1436 | d->config.cfg_access.pci_cfg_data, write_mask, | ||
1437 | d->config.cfg_access.cap.bar, | ||
1438 | d->config.cfg_access.cap.offset, | ||
1439 | d->config.cfg_access.cap.length); | ||
1440 | |||
1441 | emulate_mmio_write(d, d->config.cfg_access.cap.offset, | ||
1442 | d->config.cfg_access.pci_cfg_data, | ||
1443 | write_mask); | ||
1444 | return true; | ||
1445 | } | ||
1446 | |||
1447 | /* | ||
1448 | * 4.1.4.1: | ||
1449 | * | ||
1450 | * The driver MUST NOT write into any field of the capability | ||
1451 | * structure, with the exception of those with cap_type | ||
1452 | * VIRTIO_PCI_CAP_PCI_CFG... | ||
1453 | */ | ||
1454 | return false; | ||
1455 | } | ||
1456 | |||
1457 | static u32 emulate_mmio_read(struct device *d, u32 off, u32 mask); | ||
1458 | |||
1459 | static void pci_data_ioread(u16 port, u32 mask, u32 *val) | ||
1460 | { | ||
1461 | u32 reg; | ||
1462 | struct device *d = dev_and_reg(®); | ||
1463 | |||
1464 | if (!d) | ||
1465 | return; | ||
1466 | |||
1467 | /* Read through the PCI MMIO access window is special */ | ||
1468 | if (&d->config_words[reg] == &d->config.cfg_access.pci_cfg_data) { | ||
1469 | u32 read_mask; | ||
1470 | |||
1471 | /* | ||
1472 | * 4.1.4.7.1: | ||
1473 | * | ||
1474 | * Upon detecting driver read access to pci_cfg_data, the | ||
1475 | * device MUST execute a read access of length cap.length at | ||
1476 | * offset cap.offset at BAR selected by cap.bar and store the | ||
1477 | * first cap.length bytes in pci_cfg_data. | ||
1478 | */ | ||
1479 | /* Must be bar 0 */ | ||
1480 | if (!valid_bar_access(d, &d->config.cfg_access)) | ||
1481 | bad_driver(d, | ||
1482 | "Invalid cfg_access to bar%u, offset %u len %u", | ||
1483 | d->config.cfg_access.cap.bar, | ||
1484 | d->config.cfg_access.cap.offset, | ||
1485 | d->config.cfg_access.cap.length); | ||
1486 | |||
1487 | /* | ||
1488 | * Read into the window. The mask we use is set by | ||
1489 | * len, *not* this read! | ||
1490 | */ | ||
1491 | read_mask = (1ULL<<(8*d->config.cfg_access.cap.length))-1; | ||
1492 | d->config.cfg_access.pci_cfg_data | ||
1493 | = emulate_mmio_read(d, | ||
1494 | d->config.cfg_access.cap.offset, | ||
1495 | read_mask); | ||
1496 | verbose("Window read %#x/%#x from bar %u, offset %u len %u\n", | ||
1497 | d->config.cfg_access.pci_cfg_data, read_mask, | ||
1498 | d->config.cfg_access.cap.bar, | ||
1499 | d->config.cfg_access.cap.offset, | ||
1500 | d->config.cfg_access.cap.length); | ||
1501 | } | ||
1502 | ioread(port - PCI_CONFIG_DATA, d->config_words[reg], mask, val); | ||
1503 | } | ||
1504 | |||
1018 | /*L:216 | 1505 | /*L:216 |
1019 | * This actually creates the thread which services the virtqueue for a device. | 1506 | * This is where we emulate a handful of Guest instructions. It's ugly |
1507 | * and we used to do it in the kernel but it grew over time. | ||
1508 | */ | ||
1509 | |||
1510 | /* | ||
1511 | * We use the ptrace syscall's pt_regs struct to talk about registers | ||
1512 | * to lguest: these macros convert the names to the offsets. | ||
1513 | */ | ||
1514 | #define getreg(name) getreg_off(offsetof(struct user_regs_struct, name)) | ||
1515 | #define setreg(name, val) \ | ||
1516 | setreg_off(offsetof(struct user_regs_struct, name), (val)) | ||
1517 | |||
1518 | static u32 getreg_off(size_t offset) | ||
1519 | { | ||
1520 | u32 r; | ||
1521 | unsigned long args[] = { LHREQ_GETREG, offset }; | ||
1522 | |||
1523 | if (pwrite(lguest_fd, args, sizeof(args), cpu_id) < 0) | ||
1524 | err(1, "Getting register %u", offset); | ||
1525 | if (pread(lguest_fd, &r, sizeof(r), cpu_id) != sizeof(r)) | ||
1526 | err(1, "Reading register %u", offset); | ||
1527 | |||
1528 | return r; | ||
1529 | } | ||
1530 | |||
1531 | static void setreg_off(size_t offset, u32 val) | ||
1532 | { | ||
1533 | unsigned long args[] = { LHREQ_SETREG, offset, val }; | ||
1534 | |||
1535 | if (pwrite(lguest_fd, args, sizeof(args), cpu_id) < 0) | ||
1536 | err(1, "Setting register %u", offset); | ||
1537 | } | ||
1538 | |||
1539 | /* Get register by instruction encoding */ | ||
1540 | static u32 getreg_num(unsigned regnum, u32 mask) | ||
1541 | { | ||
1542 | /* 8 bit ops use regnums 4-7 for high parts of word */ | ||
1543 | if (mask == 0xFF && (regnum & 0x4)) | ||
1544 | return getreg_num(regnum & 0x3, 0xFFFF) >> 8; | ||
1545 | |||
1546 | switch (regnum) { | ||
1547 | case 0: return getreg(eax) & mask; | ||
1548 | case 1: return getreg(ecx) & mask; | ||
1549 | case 2: return getreg(edx) & mask; | ||
1550 | case 3: return getreg(ebx) & mask; | ||
1551 | case 4: return getreg(esp) & mask; | ||
1552 | case 5: return getreg(ebp) & mask; | ||
1553 | case 6: return getreg(esi) & mask; | ||
1554 | case 7: return getreg(edi) & mask; | ||
1555 | } | ||
1556 | abort(); | ||
1557 | } | ||
1558 | |||
1559 | /* Set register by instruction encoding */ | ||
1560 | static void setreg_num(unsigned regnum, u32 val, u32 mask) | ||
1561 | { | ||
1562 | /* Don't try to set bits out of range */ | ||
1563 | assert(~(val & ~mask)); | ||
1564 | |||
1565 | /* 8 bit ops use regnums 4-7 for high parts of word */ | ||
1566 | if (mask == 0xFF && (regnum & 0x4)) { | ||
1567 | /* Construct the 16 bits we want. */ | ||
1568 | val = (val << 8) | getreg_num(regnum & 0x3, 0xFF); | ||
1569 | setreg_num(regnum & 0x3, val, 0xFFFF); | ||
1570 | return; | ||
1571 | } | ||
1572 | |||
1573 | switch (regnum) { | ||
1574 | case 0: setreg(eax, val | (getreg(eax) & ~mask)); return; | ||
1575 | case 1: setreg(ecx, val | (getreg(ecx) & ~mask)); return; | ||
1576 | case 2: setreg(edx, val | (getreg(edx) & ~mask)); return; | ||
1577 | case 3: setreg(ebx, val | (getreg(ebx) & ~mask)); return; | ||
1578 | case 4: setreg(esp, val | (getreg(esp) & ~mask)); return; | ||
1579 | case 5: setreg(ebp, val | (getreg(ebp) & ~mask)); return; | ||
1580 | case 6: setreg(esi, val | (getreg(esi) & ~mask)); return; | ||
1581 | case 7: setreg(edi, val | (getreg(edi) & ~mask)); return; | ||
1582 | } | ||
1583 | abort(); | ||
1584 | } | ||
1585 | |||
1586 | /* Get bytes of displacement appended to instruction, from r/m encoding */ | ||
1587 | static u32 insn_displacement_len(u8 mod_reg_rm) | ||
1588 | { | ||
1589 | /* Switch on the mod bits */ | ||
1590 | switch (mod_reg_rm >> 6) { | ||
1591 | case 0: | ||
1592 | /* If mod == 0, and r/m == 101, 16-bit displacement follows */ | ||
1593 | if ((mod_reg_rm & 0x7) == 0x5) | ||
1594 | return 2; | ||
1595 | /* Normally, mod == 0 means no literal displacement */ | ||
1596 | return 0; | ||
1597 | case 1: | ||
1598 | /* One byte displacement */ | ||
1599 | return 1; | ||
1600 | case 2: | ||
1601 | /* Four byte displacement */ | ||
1602 | return 4; | ||
1603 | case 3: | ||
1604 | /* Register mode */ | ||
1605 | return 0; | ||
1606 | } | ||
1607 | abort(); | ||
1608 | } | ||
1609 | |||
1610 | static void emulate_insn(const u8 insn[]) | ||
1611 | { | ||
1612 | unsigned long args[] = { LHREQ_TRAP, 13 }; | ||
1613 | unsigned int insnlen = 0, in = 0, small_operand = 0, byte_access; | ||
1614 | unsigned int eax, port, mask; | ||
1615 | /* | ||
1616 | * Default is to return all-ones on IO port reads, which traditionally | ||
1617 | * means "there's nothing there". | ||
1618 | */ | ||
1619 | u32 val = 0xFFFFFFFF; | ||
1620 | |||
1621 | /* | ||
1622 | * This must be the Guest kernel trying to do something, not userspace! | ||
1623 | * The bottom two bits of the CS segment register are the privilege | ||
1624 | * level. | ||
1625 | */ | ||
1626 | if ((getreg(xcs) & 3) != 0x1) | ||
1627 | goto no_emulate; | ||
1628 | |||
1629 | /* Decoding x86 instructions is icky. */ | ||
1630 | |||
1631 | /* | ||
1632 | * Around 2.6.33, the kernel started using an emulation for the | ||
1633 | * cmpxchg8b instruction in early boot on many configurations. This | ||
1634 | * code isn't paravirtualized, and it tries to disable interrupts. | ||
1635 | * Ignore it, which will Mostly Work. | ||
1636 | */ | ||
1637 | if (insn[insnlen] == 0xfa) { | ||
1638 | /* "cli", or Clear Interrupt Enable instruction. Skip it. */ | ||
1639 | insnlen = 1; | ||
1640 | goto skip_insn; | ||
1641 | } | ||
1642 | |||
1643 | /* | ||
1644 | * 0x66 is an "operand prefix". It means a 16, not 32 bit in/out. | ||
1645 | */ | ||
1646 | if (insn[insnlen] == 0x66) { | ||
1647 | small_operand = 1; | ||
1648 | /* The instruction is 1 byte so far, read the next byte. */ | ||
1649 | insnlen = 1; | ||
1650 | } | ||
1651 | |||
1652 | /* If the lower bit isn't set, it's a single byte access */ | ||
1653 | byte_access = !(insn[insnlen] & 1); | ||
1654 | |||
1655 | /* | ||
1656 | * Now we can ignore the lower bit and decode the 4 opcodes | ||
1657 | * we need to emulate. | ||
1658 | */ | ||
1659 | switch (insn[insnlen] & 0xFE) { | ||
1660 | case 0xE4: /* in <next byte>,%al */ | ||
1661 | port = insn[insnlen+1]; | ||
1662 | insnlen += 2; | ||
1663 | in = 1; | ||
1664 | break; | ||
1665 | case 0xEC: /* in (%dx),%al */ | ||
1666 | port = getreg(edx) & 0xFFFF; | ||
1667 | insnlen += 1; | ||
1668 | in = 1; | ||
1669 | break; | ||
1670 | case 0xE6: /* out %al,<next byte> */ | ||
1671 | port = insn[insnlen+1]; | ||
1672 | insnlen += 2; | ||
1673 | break; | ||
1674 | case 0xEE: /* out %al,(%dx) */ | ||
1675 | port = getreg(edx) & 0xFFFF; | ||
1676 | insnlen += 1; | ||
1677 | break; | ||
1678 | default: | ||
1679 | /* OK, we don't know what this is, can't emulate. */ | ||
1680 | goto no_emulate; | ||
1681 | } | ||
1682 | |||
1683 | /* Set a mask of the 1, 2 or 4 bytes, depending on size of IO */ | ||
1684 | if (byte_access) | ||
1685 | mask = 0xFF; | ||
1686 | else if (small_operand) | ||
1687 | mask = 0xFFFF; | ||
1688 | else | ||
1689 | mask = 0xFFFFFFFF; | ||
1690 | |||
1691 | /* | ||
1692 | * If it was an "IN" instruction, they expect the result to be read | ||
1693 | * into %eax, so we change %eax. | ||
1694 | */ | ||
1695 | eax = getreg(eax); | ||
1696 | |||
1697 | if (in) { | ||
1698 | /* This is the PS/2 keyboard status; 1 means ready for output */ | ||
1699 | if (port == 0x64) | ||
1700 | val = 1; | ||
1701 | else if (is_pci_addr_port(port)) | ||
1702 | pci_addr_ioread(port, mask, &val); | ||
1703 | else if (is_pci_data_port(port)) | ||
1704 | pci_data_ioread(port, mask, &val); | ||
1705 | |||
1706 | /* Clear the bits we're about to read */ | ||
1707 | eax &= ~mask; | ||
1708 | /* Copy bits in from val. */ | ||
1709 | eax |= val & mask; | ||
1710 | /* Now update the register. */ | ||
1711 | setreg(eax, eax); | ||
1712 | } else { | ||
1713 | if (is_pci_addr_port(port)) { | ||
1714 | if (!pci_addr_iowrite(port, mask, eax)) | ||
1715 | goto bad_io; | ||
1716 | } else if (is_pci_data_port(port)) { | ||
1717 | if (!pci_data_iowrite(port, mask, eax)) | ||
1718 | goto bad_io; | ||
1719 | } | ||
1720 | /* There are many other ports, eg. CMOS clock, serial | ||
1721 | * and parallel ports, so we ignore them all. */ | ||
1722 | } | ||
1723 | |||
1724 | verbose("IO %s of %x to %u: %#08x\n", | ||
1725 | in ? "IN" : "OUT", mask, port, eax); | ||
1726 | skip_insn: | ||
1727 | /* Finally, we've "done" the instruction, so move past it. */ | ||
1728 | setreg(eip, getreg(eip) + insnlen); | ||
1729 | return; | ||
1730 | |||
1731 | bad_io: | ||
1732 | warnx("Attempt to %s port %u (%#x mask)", | ||
1733 | in ? "read from" : "write to", port, mask); | ||
1734 | |||
1735 | no_emulate: | ||
1736 | /* Inject trap into Guest. */ | ||
1737 | if (write(lguest_fd, args, sizeof(args)) < 0) | ||
1738 | err(1, "Reinjecting trap 13 for fault at %#x", getreg(eip)); | ||
1739 | } | ||
1740 | |||
1741 | static struct device *find_mmio_region(unsigned long paddr, u32 *off) | ||
1742 | { | ||
1743 | unsigned int i; | ||
1744 | |||
1745 | for (i = 1; i < MAX_PCI_DEVICES; i++) { | ||
1746 | struct device *d = devices.pci[i]; | ||
1747 | |||
1748 | if (!d) | ||
1749 | continue; | ||
1750 | if (paddr < d->mmio_addr) | ||
1751 | continue; | ||
1752 | if (paddr >= d->mmio_addr + d->mmio_size) | ||
1753 | continue; | ||
1754 | *off = paddr - d->mmio_addr; | ||
1755 | return d; | ||
1756 | } | ||
1757 | return NULL; | ||
1758 | } | ||
1759 | |||
1760 | /* FIXME: Use vq array. */ | ||
1761 | static struct virtqueue *vq_by_num(struct device *d, u32 num) | ||
1762 | { | ||
1763 | struct virtqueue *vq = d->vq; | ||
1764 | |||
1765 | while (num-- && vq) | ||
1766 | vq = vq->next; | ||
1767 | |||
1768 | return vq; | ||
1769 | } | ||
1770 | |||
1771 | static void save_vq_config(const struct virtio_pci_common_cfg *cfg, | ||
1772 | struct virtqueue *vq) | ||
1773 | { | ||
1774 | vq->pci_config = *cfg; | ||
1775 | } | ||
1776 | |||
1777 | static void restore_vq_config(struct virtio_pci_common_cfg *cfg, | ||
1778 | struct virtqueue *vq) | ||
1779 | { | ||
1780 | /* Only restore the per-vq part */ | ||
1781 | size_t off = offsetof(struct virtio_pci_common_cfg, queue_size); | ||
1782 | |||
1783 | memcpy((void *)cfg + off, (void *)&vq->pci_config + off, | ||
1784 | sizeof(*cfg) - off); | ||
1785 | } | ||
1786 | |||
1787 | /* | ||
1788 | * 4.1.4.3.2: | ||
1789 | * | ||
1790 | * The driver MUST configure the other virtqueue fields before | ||
1791 | * enabling the virtqueue with queue_enable. | ||
1792 | * | ||
1793 | * When they enable the virtqueue, we check that their setup is valid. | ||
1020 | */ | 1794 | */ |
1021 | static void create_thread(struct virtqueue *vq) | 1795 | static void check_virtqueue(struct device *d, struct virtqueue *vq) |
1796 | { | ||
1797 | /* Because lguest is 32 bit, all the descriptor high bits must be 0 */ | ||
1798 | if (vq->pci_config.queue_desc_hi | ||
1799 | || vq->pci_config.queue_avail_hi | ||
1800 | || vq->pci_config.queue_used_hi) | ||
1801 | bad_driver_vq(vq, "invalid 64-bit queue address"); | ||
1802 | |||
1803 | /* | ||
1804 | * 2.4.1: | ||
1805 | * | ||
1806 | * The driver MUST ensure that the physical address of the first byte | ||
1807 | * of each virtqueue part is a multiple of the specified alignment | ||
1808 | * value in the above table. | ||
1809 | */ | ||
1810 | if (vq->pci_config.queue_desc_lo % 16 | ||
1811 | || vq->pci_config.queue_avail_lo % 2 | ||
1812 | || vq->pci_config.queue_used_lo % 4) | ||
1813 | bad_driver_vq(vq, "invalid alignment in queue addresses"); | ||
1814 | |||
1815 | /* Initialize the virtqueue and check they're all in range. */ | ||
1816 | vq->vring.num = vq->pci_config.queue_size; | ||
1817 | vq->vring.desc = check_pointer(vq->dev, | ||
1818 | vq->pci_config.queue_desc_lo, | ||
1819 | sizeof(*vq->vring.desc) * vq->vring.num); | ||
1820 | vq->vring.avail = check_pointer(vq->dev, | ||
1821 | vq->pci_config.queue_avail_lo, | ||
1822 | sizeof(*vq->vring.avail) | ||
1823 | + (sizeof(vq->vring.avail->ring[0]) | ||
1824 | * vq->vring.num)); | ||
1825 | vq->vring.used = check_pointer(vq->dev, | ||
1826 | vq->pci_config.queue_used_lo, | ||
1827 | sizeof(*vq->vring.used) | ||
1828 | + (sizeof(vq->vring.used->ring[0]) | ||
1829 | * vq->vring.num)); | ||
1830 | |||
1831 | /* | ||
1832 | * 2.4.9.1: | ||
1833 | * | ||
1834 | * The driver MUST initialize flags in the used ring to 0 | ||
1835 | * when allocating the used ring. | ||
1836 | */ | ||
1837 | if (vq->vring.used->flags != 0) | ||
1838 | bad_driver_vq(vq, "invalid initial used.flags %#x", | ||
1839 | vq->vring.used->flags); | ||
1840 | } | ||
1841 | |||
1842 | static void start_virtqueue(struct virtqueue *vq) | ||
1022 | { | 1843 | { |
1023 | /* | 1844 | /* |
1024 | * Create stack for thread. Since the stack grows upwards, we point | 1845 | * Create stack for thread. Since the stack grows upwards, we point |
1025 | * the stack pointer to the end of this region. | 1846 | * the stack pointer to the end of this region. |
1026 | */ | 1847 | */ |
1027 | char *stack = malloc(32768); | 1848 | char *stack = malloc(32768); |
1028 | unsigned long args[] = { LHREQ_EVENTFD, | ||
1029 | vq->config.pfn*getpagesize(), 0 }; | ||
1030 | 1849 | ||
1031 | /* Create a zero-initialized eventfd. */ | 1850 | /* Create a zero-initialized eventfd. */ |
1032 | vq->eventfd = eventfd(0, 0); | 1851 | vq->eventfd = eventfd(0, 0); |
1033 | if (vq->eventfd < 0) | 1852 | if (vq->eventfd < 0) |
1034 | err(1, "Creating eventfd"); | 1853 | err(1, "Creating eventfd"); |
1035 | args[2] = vq->eventfd; | ||
1036 | |||
1037 | /* | ||
1038 | * Attach an eventfd to this virtqueue: it will go off when the Guest | ||
1039 | * does an LHCALL_NOTIFY for this vq. | ||
1040 | */ | ||
1041 | if (write(lguest_fd, &args, sizeof(args)) != 0) | ||
1042 | err(1, "Attaching eventfd"); | ||
1043 | 1854 | ||
1044 | /* | 1855 | /* |
1045 | * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so | 1856 | * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so |
@@ -1048,167 +1859,531 @@ static void create_thread(struct virtqueue *vq) | |||
1048 | vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq); | 1859 | vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq); |
1049 | if (vq->thread == (pid_t)-1) | 1860 | if (vq->thread == (pid_t)-1) |
1050 | err(1, "Creating clone"); | 1861 | err(1, "Creating clone"); |
1051 | |||
1052 | /* We close our local copy now the child has it. */ | ||
1053 | close(vq->eventfd); | ||
1054 | } | 1862 | } |
1055 | 1863 | ||
1056 | static void start_device(struct device *dev) | 1864 | static void start_virtqueues(struct device *d) |
1057 | { | 1865 | { |
1058 | unsigned int i; | ||
1059 | struct virtqueue *vq; | 1866 | struct virtqueue *vq; |
1060 | 1867 | ||
1061 | verbose("Device %s OK: offered", dev->name); | 1868 | for (vq = d->vq; vq; vq = vq->next) { |
1062 | for (i = 0; i < dev->feature_len; i++) | 1869 | if (vq->pci_config.queue_enable) |
1063 | verbose(" %02x", get_feature_bits(dev)[i]); | 1870 | start_virtqueue(vq); |
1064 | verbose(", accepted"); | ||
1065 | for (i = 0; i < dev->feature_len; i++) | ||
1066 | verbose(" %02x", get_feature_bits(dev) | ||
1067 | [dev->feature_len+i]); | ||
1068 | |||
1069 | for (vq = dev->vq; vq; vq = vq->next) { | ||
1070 | if (vq->service) | ||
1071 | create_thread(vq); | ||
1072 | } | 1871 | } |
1073 | dev->running = true; | ||
1074 | } | 1872 | } |
1075 | 1873 | ||
1076 | static void cleanup_devices(void) | 1874 | static void emulate_mmio_write(struct device *d, u32 off, u32 val, u32 mask) |
1077 | { | 1875 | { |
1078 | struct device *dev; | 1876 | struct virtqueue *vq; |
1079 | 1877 | ||
1080 | for (dev = devices.dev; dev; dev = dev->next) | 1878 | switch (off) { |
1081 | reset_device(dev); | 1879 | case offsetof(struct virtio_pci_mmio, cfg.device_feature_select): |
1880 | /* | ||
1881 | * 4.1.4.3.1: | ||
1882 | * | ||
1883 | * The device MUST present the feature bits it is offering in | ||
1884 | * device_feature, starting at bit device_feature_select ∗ 32 | ||
1885 | * for any device_feature_select written by the driver | ||
1886 | */ | ||
1887 | if (val == 0) | ||
1888 | d->mmio->cfg.device_feature = d->features; | ||
1889 | else if (val == 1) | ||
1890 | d->mmio->cfg.device_feature = (d->features >> 32); | ||
1891 | else | ||
1892 | d->mmio->cfg.device_feature = 0; | ||
1893 | goto feature_write_through32; | ||
1894 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature_select): | ||
1895 | if (val > 1) | ||
1896 | bad_driver(d, "Unexpected driver select %u", val); | ||
1897 | goto feature_write_through32; | ||
1898 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature): | ||
1899 | if (d->mmio->cfg.guest_feature_select == 0) { | ||
1900 | d->features_accepted &= ~((u64)0xFFFFFFFF); | ||
1901 | d->features_accepted |= val; | ||
1902 | } else { | ||
1903 | assert(d->mmio->cfg.guest_feature_select == 1); | ||
1904 | d->features_accepted &= 0xFFFFFFFF; | ||
1905 | d->features_accepted |= ((u64)val) << 32; | ||
1906 | } | ||
1907 | /* | ||
1908 | * 2.2.1: | ||
1909 | * | ||
1910 | * The driver MUST NOT accept a feature which the device did | ||
1911 | * not offer | ||
1912 | */ | ||
1913 | if (d->features_accepted & ~d->features) | ||
1914 | bad_driver(d, "over-accepted features %#llx of %#llx", | ||
1915 | d->features_accepted, d->features); | ||
1916 | goto feature_write_through32; | ||
1917 | case offsetof(struct virtio_pci_mmio, cfg.device_status): { | ||
1918 | u8 prev; | ||
1919 | |||
1920 | verbose("%s: device status -> %#x\n", d->name, val); | ||
1921 | /* | ||
1922 | * 4.1.4.3.1: | ||
1923 | * | ||
1924 | * The device MUST reset when 0 is written to device_status, | ||
1925 | * and present a 0 in device_status once that is done. | ||
1926 | */ | ||
1927 | if (val == 0) { | ||
1928 | reset_device(d); | ||
1929 | goto write_through8; | ||
1930 | } | ||
1082 | 1931 | ||
1083 | /* If we saved off the original terminal settings, restore them now. */ | 1932 | /* 2.1.1: The driver MUST NOT clear a device status bit. */ |
1084 | if (orig_term.c_lflag & (ISIG|ICANON|ECHO)) | 1933 | if (d->mmio->cfg.device_status & ~val) |
1085 | tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); | 1934 | bad_driver(d, "unset of device status bit %#x -> %#x", |
1086 | } | 1935 | d->mmio->cfg.device_status, val); |
1087 | 1936 | ||
1088 | /* When the Guest tells us they updated the status field, we handle it. */ | 1937 | /* |
1089 | static void update_device_status(struct device *dev) | 1938 | * 2.1.2: |
1090 | { | 1939 | * |
1091 | /* A zero status is a reset, otherwise it's a set of flags. */ | 1940 | * The device MUST NOT consume buffers or notify the driver |
1092 | if (dev->desc->status == 0) | 1941 | * before DRIVER_OK. |
1093 | reset_device(dev); | 1942 | */ |
1094 | else if (dev->desc->status & VIRTIO_CONFIG_S_FAILED) { | 1943 | if (val & VIRTIO_CONFIG_S_DRIVER_OK |
1095 | warnx("Device %s configuration FAILED", dev->name); | 1944 | && !(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER_OK)) |
1096 | if (dev->running) | 1945 | start_virtqueues(d); |
1097 | reset_device(dev); | 1946 | |
1098 | } else { | 1947 | /* |
1099 | if (dev->running) | 1948 | * 3.1.1: |
1100 | err(1, "Device %s features finalized twice", dev->name); | 1949 | * |
1101 | start_device(dev); | 1950 | * The driver MUST follow this sequence to initialize a device: |
1951 | * - Reset the device. | ||
1952 | * - Set the ACKNOWLEDGE status bit: the guest OS has | ||
1953 | * notice the device. | ||
1954 | * - Set the DRIVER status bit: the guest OS knows how | ||
1955 | * to drive the device. | ||
1956 | * - Read device feature bits, and write the subset | ||
1957 | * of feature bits understood by the OS and driver | ||
1958 | * to the device. During this step the driver MAY | ||
1959 | * read (but MUST NOT write) the device-specific | ||
1960 | * configuration fields to check that it can | ||
1961 | * support the device before accepting it. | ||
1962 | * - Set the FEATURES_OK status bit. The driver | ||
1963 | * MUST not accept new feature bits after this | ||
1964 | * step. | ||
1965 | * - Re-read device status to ensure the FEATURES_OK | ||
1966 | * bit is still set: otherwise, the device does | ||
1967 | * not support our subset of features and the | ||
1968 | * device is unusable. | ||
1969 | * - Perform device-specific setup, including | ||
1970 | * discovery of virtqueues for the device, | ||
1971 | * optional per-bus setup, reading and possibly | ||
1972 | * writing the device’s virtio configuration | ||
1973 | * space, and population of virtqueues. | ||
1974 | * - Set the DRIVER_OK status bit. At this point the | ||
1975 | * device is “live”. | ||
1976 | */ | ||
1977 | prev = 0; | ||
1978 | switch (val & ~d->mmio->cfg.device_status) { | ||
1979 | case VIRTIO_CONFIG_S_DRIVER_OK: | ||
1980 | prev |= VIRTIO_CONFIG_S_FEATURES_OK; /* fall thru */ | ||
1981 | case VIRTIO_CONFIG_S_FEATURES_OK: | ||
1982 | prev |= VIRTIO_CONFIG_S_DRIVER; /* fall thru */ | ||
1983 | case VIRTIO_CONFIG_S_DRIVER: | ||
1984 | prev |= VIRTIO_CONFIG_S_ACKNOWLEDGE; /* fall thru */ | ||
1985 | case VIRTIO_CONFIG_S_ACKNOWLEDGE: | ||
1986 | break; | ||
1987 | default: | ||
1988 | bad_driver(d, "unknown device status bit %#x -> %#x", | ||
1989 | d->mmio->cfg.device_status, val); | ||
1990 | } | ||
1991 | if (d->mmio->cfg.device_status != prev) | ||
1992 | bad_driver(d, "unexpected status transition %#x -> %#x", | ||
1993 | d->mmio->cfg.device_status, val); | ||
1994 | |||
1995 | /* If they just wrote FEATURES_OK, we make sure they read */ | ||
1996 | switch (val & ~d->mmio->cfg.device_status) { | ||
1997 | case VIRTIO_CONFIG_S_FEATURES_OK: | ||
1998 | d->wrote_features_ok = true; | ||
1999 | break; | ||
2000 | case VIRTIO_CONFIG_S_DRIVER_OK: | ||
2001 | if (d->wrote_features_ok) | ||
2002 | bad_driver(d, "did not re-read FEATURES_OK"); | ||
2003 | break; | ||
2004 | } | ||
2005 | goto write_through8; | ||
1102 | } | 2006 | } |
1103 | } | 2007 | case offsetof(struct virtio_pci_mmio, cfg.queue_select): |
2008 | vq = vq_by_num(d, val); | ||
2009 | /* | ||
2010 | * 4.1.4.3.1: | ||
2011 | * | ||
2012 | * The device MUST present a 0 in queue_size if the virtqueue | ||
2013 | * corresponding to the current queue_select is unavailable. | ||
2014 | */ | ||
2015 | if (!vq) { | ||
2016 | d->mmio->cfg.queue_size = 0; | ||
2017 | goto write_through16; | ||
2018 | } | ||
2019 | /* Save registers for old vq, if it was a valid vq */ | ||
2020 | if (d->mmio->cfg.queue_size) | ||
2021 | save_vq_config(&d->mmio->cfg, | ||
2022 | vq_by_num(d, d->mmio->cfg.queue_select)); | ||
2023 | /* Restore the registers for the queue they asked for */ | ||
2024 | restore_vq_config(&d->mmio->cfg, vq); | ||
2025 | goto write_through16; | ||
2026 | case offsetof(struct virtio_pci_mmio, cfg.queue_size): | ||
2027 | /* | ||
2028 | * 4.1.4.3.2: | ||
2029 | * | ||
2030 | * The driver MUST NOT write a value which is not a power of 2 | ||
2031 | * to queue_size. | ||
2032 | */ | ||
2033 | if (val & (val-1)) | ||
2034 | bad_driver(d, "invalid queue size %u", val); | ||
2035 | if (d->mmio->cfg.queue_enable) | ||
2036 | bad_driver(d, "changing queue size on live device"); | ||
2037 | goto write_through16; | ||
2038 | case offsetof(struct virtio_pci_mmio, cfg.queue_msix_vector): | ||
2039 | bad_driver(d, "attempt to set MSIX vector to %u", val); | ||
2040 | case offsetof(struct virtio_pci_mmio, cfg.queue_enable): { | ||
2041 | struct virtqueue *vq = vq_by_num(d, d->mmio->cfg.queue_select); | ||
1104 | 2042 | ||
1105 | /*L:215 | 2043 | /* |
1106 | * This is the generic routine we call when the Guest uses LHCALL_NOTIFY. In | 2044 | * 4.1.4.3.2: |
1107 | * particular, it's used to notify us of device status changes during boot. | 2045 | * |
1108 | */ | 2046 | * The driver MUST NOT write a 0 to queue_enable. |
1109 | static void handle_output(unsigned long addr) | 2047 | */ |
1110 | { | 2048 | if (val != 1) |
1111 | struct device *i; | 2049 | bad_driver(d, "setting queue_enable to %u", val); |
1112 | 2050 | ||
1113 | /* Check each device. */ | 2051 | /* |
1114 | for (i = devices.dev; i; i = i->next) { | 2052 | * 3.1.1: |
1115 | struct virtqueue *vq; | 2053 | * |
2054 | * 7. Perform device-specific setup, including discovery of | ||
2055 | * virtqueues for the device, optional per-bus setup, | ||
2056 | * reading and possibly writing the device’s virtio | ||
2057 | * configuration space, and population of virtqueues. | ||
2058 | * 8. Set the DRIVER_OK status bit. | ||
2059 | * | ||
2060 | * All our devices require all virtqueues to be enabled, so | ||
2061 | * they should have done that before setting DRIVER_OK. | ||
2062 | */ | ||
2063 | if (d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER_OK) | ||
2064 | bad_driver(d, "enabling vq after DRIVER_OK"); | ||
1116 | 2065 | ||
2066 | d->mmio->cfg.queue_enable = val; | ||
2067 | save_vq_config(&d->mmio->cfg, vq); | ||
2068 | check_virtqueue(d, vq); | ||
2069 | goto write_through16; | ||
2070 | } | ||
2071 | case offsetof(struct virtio_pci_mmio, cfg.queue_notify_off): | ||
2072 | bad_driver(d, "attempt to write to queue_notify_off"); | ||
2073 | case offsetof(struct virtio_pci_mmio, cfg.queue_desc_lo): | ||
2074 | case offsetof(struct virtio_pci_mmio, cfg.queue_desc_hi): | ||
2075 | case offsetof(struct virtio_pci_mmio, cfg.queue_avail_lo): | ||
2076 | case offsetof(struct virtio_pci_mmio, cfg.queue_avail_hi): | ||
2077 | case offsetof(struct virtio_pci_mmio, cfg.queue_used_lo): | ||
2078 | case offsetof(struct virtio_pci_mmio, cfg.queue_used_hi): | ||
1117 | /* | 2079 | /* |
1118 | * Notifications to device descriptors mean they updated the | 2080 | * 4.1.4.3.2: |
1119 | * device status. | 2081 | * |
2082 | * The driver MUST configure the other virtqueue fields before | ||
2083 | * enabling the virtqueue with queue_enable. | ||
1120 | */ | 2084 | */ |
1121 | if (from_guest_phys(addr) == i->desc) { | 2085 | if (d->mmio->cfg.queue_enable) |
1122 | update_device_status(i); | 2086 | bad_driver(d, "changing queue on live device"); |
1123 | return; | 2087 | |
1124 | } | 2088 | /* |
2089 | * 3.1.1: | ||
2090 | * | ||
2091 | * The driver MUST follow this sequence to initialize a device: | ||
2092 | *... | ||
2093 | * 5. Set the FEATURES_OK status bit. The driver MUST not | ||
2094 | * accept new feature bits after this step. | ||
2095 | */ | ||
2096 | if (!(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_FEATURES_OK)) | ||
2097 | bad_driver(d, "setting up vq before FEATURES_OK"); | ||
1125 | 2098 | ||
1126 | /* Devices should not be used before features are finalized. */ | 2099 | /* |
1127 | for (vq = i->vq; vq; vq = vq->next) { | 2100 | * 6. Re-read device status to ensure the FEATURES_OK bit is |
1128 | if (addr != vq->config.pfn*getpagesize()) | 2101 | * still set... |
1129 | continue; | 2102 | */ |
1130 | errx(1, "Notification on %s before setup!", i->name); | 2103 | if (d->wrote_features_ok) |
2104 | bad_driver(d, "didn't re-read FEATURES_OK before setup"); | ||
2105 | |||
2106 | goto write_through32; | ||
2107 | case offsetof(struct virtio_pci_mmio, notify): | ||
2108 | vq = vq_by_num(d, val); | ||
2109 | if (!vq) | ||
2110 | bad_driver(d, "Invalid vq notification on %u", val); | ||
2111 | /* Notify the process handling this vq by adding 1 to eventfd */ | ||
2112 | write(vq->eventfd, "\1\0\0\0\0\0\0\0", 8); | ||
2113 | goto write_through16; | ||
2114 | case offsetof(struct virtio_pci_mmio, isr): | ||
2115 | bad_driver(d, "Unexpected write to isr"); | ||
2116 | /* Weird corner case: write to emerg_wr of console */ | ||
2117 | case sizeof(struct virtio_pci_mmio) | ||
2118 | + offsetof(struct virtio_console_config, emerg_wr): | ||
2119 | if (strcmp(d->name, "console") == 0) { | ||
2120 | char c = val; | ||
2121 | write(STDOUT_FILENO, &c, 1); | ||
2122 | goto write_through32; | ||
1131 | } | 2123 | } |
2124 | /* Fall through... */ | ||
2125 | default: | ||
2126 | /* | ||
2127 | * 4.1.4.3.2: | ||
2128 | * | ||
2129 | * The driver MUST NOT write to device_feature, num_queues, | ||
2130 | * config_generation or queue_notify_off. | ||
2131 | */ | ||
2132 | bad_driver(d, "Unexpected write to offset %u", off); | ||
1132 | } | 2133 | } |
1133 | 2134 | ||
2135 | feature_write_through32: | ||
1134 | /* | 2136 | /* |
1135 | * Early console write is done using notify on a nul-terminated string | 2137 | * 3.1.1: |
1136 | * in Guest memory. It's also great for hacking debugging messages | 2138 | * |
1137 | * into a Guest. | 2139 | * The driver MUST follow this sequence to initialize a device: |
2140 | *... | ||
2141 | * - Set the DRIVER status bit: the guest OS knows how | ||
2142 | * to drive the device. | ||
2143 | * - Read device feature bits, and write the subset | ||
2144 | * of feature bits understood by the OS and driver | ||
2145 | * to the device. | ||
2146 | *... | ||
2147 | * - Set the FEATURES_OK status bit. The driver MUST not | ||
2148 | * accept new feature bits after this step. | ||
1138 | */ | 2149 | */ |
1139 | if (addr >= guest_limit) | 2150 | if (!(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER)) |
1140 | errx(1, "Bad NOTIFY %#lx", addr); | 2151 | bad_driver(d, "feature write before VIRTIO_CONFIG_S_DRIVER"); |
2152 | if (d->mmio->cfg.device_status & VIRTIO_CONFIG_S_FEATURES_OK) | ||
2153 | bad_driver(d, "feature write after VIRTIO_CONFIG_S_FEATURES_OK"); | ||
1141 | 2154 | ||
1142 | write(STDOUT_FILENO, from_guest_phys(addr), | 2155 | /* |
1143 | strnlen(from_guest_phys(addr), guest_limit - addr)); | 2156 | * 4.1.3.1: |
2157 | * | ||
2158 | * The driver MUST access each field using the “natural” access | ||
2159 | * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses for | ||
2160 | * 16-bit fields and 8-bit accesses for 8-bit fields. | ||
2161 | */ | ||
2162 | write_through32: | ||
2163 | if (mask != 0xFFFFFFFF) { | ||
2164 | bad_driver(d, "non-32-bit write to offset %u (%#x)", | ||
2165 | off, getreg(eip)); | ||
2166 | return; | ||
2167 | } | ||
2168 | memcpy((char *)d->mmio + off, &val, 4); | ||
2169 | return; | ||
2170 | |||
2171 | write_through16: | ||
2172 | if (mask != 0xFFFF) | ||
2173 | bad_driver(d, "non-16-bit write to offset %u (%#x)", | ||
2174 | off, getreg(eip)); | ||
2175 | memcpy((char *)d->mmio + off, &val, 2); | ||
2176 | return; | ||
2177 | |||
2178 | write_through8: | ||
2179 | if (mask != 0xFF) | ||
2180 | bad_driver(d, "non-8-bit write to offset %u (%#x)", | ||
2181 | off, getreg(eip)); | ||
2182 | memcpy((char *)d->mmio + off, &val, 1); | ||
2183 | return; | ||
1144 | } | 2184 | } |
1145 | 2185 | ||
1146 | /*L:190 | 2186 | static u32 emulate_mmio_read(struct device *d, u32 off, u32 mask) |
1147 | * Device Setup | ||
1148 | * | ||
1149 | * All devices need a descriptor so the Guest knows it exists, and a "struct | ||
1150 | * device" so the Launcher can keep track of it. We have common helper | ||
1151 | * routines to allocate and manage them. | ||
1152 | */ | ||
1153 | |||
1154 | /* | ||
1155 | * The layout of the device page is a "struct lguest_device_desc" followed by a | ||
1156 | * number of virtqueue descriptors, then two sets of feature bits, then an | ||
1157 | * array of configuration bytes. This routine returns the configuration | ||
1158 | * pointer. | ||
1159 | */ | ||
1160 | static u8 *device_config(const struct device *dev) | ||
1161 | { | 2187 | { |
1162 | return (void *)(dev->desc + 1) | 2188 | u8 isr; |
1163 | + dev->num_vq * sizeof(struct lguest_vqconfig) | 2189 | u32 val = 0; |
1164 | + dev->feature_len * 2; | 2190 | |
2191 | switch (off) { | ||
2192 | case offsetof(struct virtio_pci_mmio, cfg.device_feature_select): | ||
2193 | case offsetof(struct virtio_pci_mmio, cfg.device_feature): | ||
2194 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature_select): | ||
2195 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature): | ||
2196 | /* | ||
2197 | * 3.1.1: | ||
2198 | * | ||
2199 | * The driver MUST follow this sequence to initialize a device: | ||
2200 | *... | ||
2201 | * - Set the DRIVER status bit: the guest OS knows how | ||
2202 | * to drive the device. | ||
2203 | * - Read device feature bits, and write the subset | ||
2204 | * of feature bits understood by the OS and driver | ||
2205 | * to the device. | ||
2206 | */ | ||
2207 | if (!(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER)) | ||
2208 | bad_driver(d, | ||
2209 | "feature read before VIRTIO_CONFIG_S_DRIVER"); | ||
2210 | goto read_through32; | ||
2211 | case offsetof(struct virtio_pci_mmio, cfg.msix_config): | ||
2212 | bad_driver(d, "read of msix_config"); | ||
2213 | case offsetof(struct virtio_pci_mmio, cfg.num_queues): | ||
2214 | goto read_through16; | ||
2215 | case offsetof(struct virtio_pci_mmio, cfg.device_status): | ||
2216 | /* As they did read, any write of FEATURES_OK is now fine. */ | ||
2217 | d->wrote_features_ok = false; | ||
2218 | goto read_through8; | ||
2219 | case offsetof(struct virtio_pci_mmio, cfg.config_generation): | ||
2220 | /* | ||
2221 | * 4.1.4.3.1: | ||
2222 | * | ||
2223 | * The device MUST present a changed config_generation after | ||
2224 | * the driver has read a device-specific configuration value | ||
2225 | * which has changed since any part of the device-specific | ||
2226 | * configuration was last read. | ||
2227 | * | ||
2228 | * This is simple: none of our devices change config, so this | ||
2229 | * is always 0. | ||
2230 | */ | ||
2231 | goto read_through8; | ||
2232 | case offsetof(struct virtio_pci_mmio, notify): | ||
2233 | /* | ||
2234 | * 3.1.1: | ||
2235 | * | ||
2236 | * The driver MUST NOT notify the device before setting | ||
2237 | * DRIVER_OK. | ||
2238 | */ | ||
2239 | if (!(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER_OK)) | ||
2240 | bad_driver(d, "notify before VIRTIO_CONFIG_S_DRIVER_OK"); | ||
2241 | goto read_through16; | ||
2242 | case offsetof(struct virtio_pci_mmio, isr): | ||
2243 | if (mask != 0xFF) | ||
2244 | bad_driver(d, "non-8-bit read from offset %u (%#x)", | ||
2245 | off, getreg(eip)); | ||
2246 | isr = d->mmio->isr; | ||
2247 | /* | ||
2248 | * 4.1.4.5.1: | ||
2249 | * | ||
2250 | * The device MUST reset ISR status to 0 on driver read. | ||
2251 | */ | ||
2252 | d->mmio->isr = 0; | ||
2253 | return isr; | ||
2254 | case offsetof(struct virtio_pci_mmio, padding): | ||
2255 | bad_driver(d, "read from padding (%#x)", getreg(eip)); | ||
2256 | default: | ||
2257 | /* Read from device config space, beware unaligned overflow */ | ||
2258 | if (off > d->mmio_size - 4) | ||
2259 | bad_driver(d, "read past end (%#x)", getreg(eip)); | ||
2260 | |||
2261 | /* | ||
2262 | * 3.1.1: | ||
2263 | * The driver MUST follow this sequence to initialize a device: | ||
2264 | *... | ||
2265 | * 3. Set the DRIVER status bit: the guest OS knows how to | ||
2266 | * drive the device. | ||
2267 | * 4. Read device feature bits, and write the subset of | ||
2268 | * feature bits understood by the OS and driver to the | ||
2269 | * device. During this step the driver MAY read (but MUST NOT | ||
2270 | * write) the device-specific configuration fields to check | ||
2271 | * that it can support the device before accepting it. | ||
2272 | */ | ||
2273 | if (!(d->mmio->cfg.device_status & VIRTIO_CONFIG_S_DRIVER)) | ||
2274 | bad_driver(d, | ||
2275 | "config read before VIRTIO_CONFIG_S_DRIVER"); | ||
2276 | |||
2277 | if (mask == 0xFFFFFFFF) | ||
2278 | goto read_through32; | ||
2279 | else if (mask == 0xFFFF) | ||
2280 | goto read_through16; | ||
2281 | else | ||
2282 | goto read_through8; | ||
2283 | } | ||
2284 | |||
2285 | /* | ||
2286 | * 4.1.3.1: | ||
2287 | * | ||
2288 | * The driver MUST access each field using the “natural” access | ||
2289 | * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses for | ||
2290 | * 16-bit fields and 8-bit accesses for 8-bit fields. | ||
2291 | */ | ||
2292 | read_through32: | ||
2293 | if (mask != 0xFFFFFFFF) | ||
2294 | bad_driver(d, "non-32-bit read to offset %u (%#x)", | ||
2295 | off, getreg(eip)); | ||
2296 | memcpy(&val, (char *)d->mmio + off, 4); | ||
2297 | return val; | ||
2298 | |||
2299 | read_through16: | ||
2300 | if (mask != 0xFFFF) | ||
2301 | bad_driver(d, "non-16-bit read to offset %u (%#x)", | ||
2302 | off, getreg(eip)); | ||
2303 | memcpy(&val, (char *)d->mmio + off, 2); | ||
2304 | return val; | ||
2305 | |||
2306 | read_through8: | ||
2307 | if (mask != 0xFF) | ||
2308 | bad_driver(d, "non-8-bit read to offset %u (%#x)", | ||
2309 | off, getreg(eip)); | ||
2310 | memcpy(&val, (char *)d->mmio + off, 1); | ||
2311 | return val; | ||
1165 | } | 2312 | } |
1166 | 2313 | ||
1167 | /* | 2314 | static void emulate_mmio(unsigned long paddr, const u8 *insn) |
1168 | * This routine allocates a new "struct lguest_device_desc" from descriptor | ||
1169 | * table page just above the Guest's normal memory. It returns a pointer to | ||
1170 | * that descriptor. | ||
1171 | */ | ||
1172 | static struct lguest_device_desc *new_dev_desc(u16 type) | ||
1173 | { | 2315 | { |
1174 | struct lguest_device_desc d = { .type = type }; | 2316 | u32 val, off, mask = 0xFFFFFFFF, insnlen = 0; |
1175 | void *p; | 2317 | struct device *d = find_mmio_region(paddr, &off); |
2318 | unsigned long args[] = { LHREQ_TRAP, 14 }; | ||
1176 | 2319 | ||
1177 | /* Figure out where the next device config is, based on the last one. */ | 2320 | if (!d) { |
1178 | if (devices.lastdev) | 2321 | warnx("MMIO touching %#08lx (not a device)", paddr); |
1179 | p = device_config(devices.lastdev) | 2322 | goto reinject; |
1180 | + devices.lastdev->desc->config_len; | 2323 | } |
1181 | else | 2324 | |
1182 | p = devices.descpage; | 2325 | /* Prefix makes it a 16 bit op */ |
2326 | if (insn[0] == 0x66) { | ||
2327 | mask = 0xFFFF; | ||
2328 | insnlen++; | ||
2329 | } | ||
1183 | 2330 | ||
1184 | /* We only have one page for all the descriptors. */ | 2331 | /* iowrite */ |
1185 | if (p + sizeof(d) > (void *)devices.descpage + getpagesize()) | 2332 | if (insn[insnlen] == 0x89) { |
1186 | errx(1, "Too many devices"); | 2333 | /* Next byte is r/m byte: bits 3-5 are register. */ |
2334 | val = getreg_num((insn[insnlen+1] >> 3) & 0x7, mask); | ||
2335 | emulate_mmio_write(d, off, val, mask); | ||
2336 | insnlen += 2 + insn_displacement_len(insn[insnlen+1]); | ||
2337 | } else if (insn[insnlen] == 0x8b) { /* ioread */ | ||
2338 | /* Next byte is r/m byte: bits 3-5 are register. */ | ||
2339 | val = emulate_mmio_read(d, off, mask); | ||
2340 | setreg_num((insn[insnlen+1] >> 3) & 0x7, val, mask); | ||
2341 | insnlen += 2 + insn_displacement_len(insn[insnlen+1]); | ||
2342 | } else if (insn[0] == 0x88) { /* 8-bit iowrite */ | ||
2343 | mask = 0xff; | ||
2344 | /* Next byte is r/m byte: bits 3-5 are register. */ | ||
2345 | val = getreg_num((insn[1] >> 3) & 0x7, mask); | ||
2346 | emulate_mmio_write(d, off, val, mask); | ||
2347 | insnlen = 2 + insn_displacement_len(insn[1]); | ||
2348 | } else if (insn[0] == 0x8a) { /* 8-bit ioread */ | ||
2349 | mask = 0xff; | ||
2350 | val = emulate_mmio_read(d, off, mask); | ||
2351 | setreg_num((insn[1] >> 3) & 0x7, val, mask); | ||
2352 | insnlen = 2 + insn_displacement_len(insn[1]); | ||
2353 | } else { | ||
2354 | warnx("Unknown MMIO instruction touching %#08lx:" | ||
2355 | " %02x %02x %02x %02x at %u", | ||
2356 | paddr, insn[0], insn[1], insn[2], insn[3], getreg(eip)); | ||
2357 | reinject: | ||
2358 | /* Inject trap into Guest. */ | ||
2359 | if (write(lguest_fd, args, sizeof(args)) < 0) | ||
2360 | err(1, "Reinjecting trap 14 for fault at %#x", | ||
2361 | getreg(eip)); | ||
2362 | return; | ||
2363 | } | ||
1187 | 2364 | ||
1188 | /* p might not be aligned, so we memcpy in. */ | 2365 | /* Finally, we've "done" the instruction, so move past it. */ |
1189 | return memcpy(p, &d, sizeof(d)); | 2366 | setreg(eip, getreg(eip) + insnlen); |
1190 | } | 2367 | } |
1191 | 2368 | ||
1192 | /* | 2369 | /*L:190 |
1193 | * Each device descriptor is followed by the description of its virtqueues. We | 2370 | * Device Setup |
1194 | * specify how many descriptors the virtqueue is to have. | 2371 | * |
2372 | * All devices need a descriptor so the Guest knows it exists, and a "struct | ||
2373 | * device" so the Launcher can keep track of it. We have common helper | ||
2374 | * routines to allocate and manage them. | ||
1195 | */ | 2375 | */ |
1196 | static void add_virtqueue(struct device *dev, unsigned int num_descs, | 2376 | static void add_pci_virtqueue(struct device *dev, |
1197 | void (*service)(struct virtqueue *)) | 2377 | void (*service)(struct virtqueue *), |
2378 | const char *name) | ||
1198 | { | 2379 | { |
1199 | unsigned int pages; | ||
1200 | struct virtqueue **i, *vq = malloc(sizeof(*vq)); | 2380 | struct virtqueue **i, *vq = malloc(sizeof(*vq)); |
1201 | void *p; | ||
1202 | |||
1203 | /* First we need some memory for this virtqueue. */ | ||
1204 | pages = (vring_size(num_descs, LGUEST_VRING_ALIGN) + getpagesize() - 1) | ||
1205 | / getpagesize(); | ||
1206 | p = get_pages(pages); | ||
1207 | 2381 | ||
1208 | /* Initialize the virtqueue */ | 2382 | /* Initialize the virtqueue */ |
1209 | vq->next = NULL; | 2383 | vq->next = NULL; |
1210 | vq->last_avail_idx = 0; | 2384 | vq->last_avail_idx = 0; |
1211 | vq->dev = dev; | 2385 | vq->dev = dev; |
2386 | vq->name = name; | ||
1212 | 2387 | ||
1213 | /* | 2388 | /* |
1214 | * This is the routine the service thread will run, and its Process ID | 2389 | * This is the routine the service thread will run, and its Process ID |
@@ -1218,25 +2393,11 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, | |||
1218 | vq->thread = (pid_t)-1; | 2393 | vq->thread = (pid_t)-1; |
1219 | 2394 | ||
1220 | /* Initialize the configuration. */ | 2395 | /* Initialize the configuration. */ |
1221 | vq->config.num = num_descs; | 2396 | reset_vq_pci_config(vq); |
1222 | vq->config.irq = devices.next_irq++; | 2397 | vq->pci_config.queue_notify_off = 0; |
1223 | vq->config.pfn = to_guest_phys(p) / getpagesize(); | ||
1224 | |||
1225 | /* Initialize the vring. */ | ||
1226 | vring_init(&vq->vring, num_descs, p, LGUEST_VRING_ALIGN); | ||
1227 | |||
1228 | /* | ||
1229 | * Append virtqueue to this device's descriptor. We use | ||
1230 | * device_config() to get the end of the device's current virtqueues; | ||
1231 | * we check that we haven't added any config or feature information | ||
1232 | * yet, otherwise we'd be overwriting them. | ||
1233 | */ | ||
1234 | assert(dev->desc->config_len == 0 && dev->desc->feature_len == 0); | ||
1235 | memcpy(device_config(dev), &vq->config, sizeof(vq->config)); | ||
1236 | dev->num_vq++; | ||
1237 | dev->desc->num_vq++; | ||
1238 | 2398 | ||
1239 | verbose("Virtqueue page %#lx\n", to_guest_phys(p)); | 2399 | /* Add one to the number of queues */ |
2400 | vq->dev->mmio->cfg.num_queues++; | ||
1240 | 2401 | ||
1241 | /* | 2402 | /* |
1242 | * Add to tail of list, so dev->vq is first vq, dev->vq->next is | 2403 | * Add to tail of list, so dev->vq is first vq, dev->vq->next is |
@@ -1246,73 +2407,239 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, | |||
1246 | *i = vq; | 2407 | *i = vq; |
1247 | } | 2408 | } |
1248 | 2409 | ||
1249 | /* | 2410 | /* The Guest accesses the feature bits via the PCI common config MMIO region */ |
1250 | * The first half of the feature bitmask is for us to advertise features. The | 2411 | static void add_pci_feature(struct device *dev, unsigned bit) |
1251 | * second half is for the Guest to accept features. | ||
1252 | */ | ||
1253 | static void add_feature(struct device *dev, unsigned bit) | ||
1254 | { | 2412 | { |
1255 | u8 *features = get_feature_bits(dev); | 2413 | dev->features |= (1ULL << bit); |
2414 | } | ||
1256 | 2415 | ||
1257 | /* We can't extend the feature bits once we've added config bytes */ | 2416 | /* For devices with no config. */ |
1258 | if (dev->desc->feature_len <= bit / CHAR_BIT) { | 2417 | static void no_device_config(struct device *dev) |
1259 | assert(dev->desc->config_len == 0); | 2418 | { |
1260 | dev->feature_len = dev->desc->feature_len = (bit/CHAR_BIT) + 1; | 2419 | dev->mmio_addr = get_mmio_region(dev->mmio_size); |
1261 | } | ||
1262 | 2420 | ||
1263 | features[bit / CHAR_BIT] |= (1 << (bit % CHAR_BIT)); | 2421 | dev->config.bar[0] = dev->mmio_addr; |
2422 | /* Bottom 4 bits must be zero */ | ||
2423 | assert(~(dev->config.bar[0] & 0xF)); | ||
2424 | } | ||
2425 | |||
2426 | /* This puts the device config into BAR0 */ | ||
2427 | static void set_device_config(struct device *dev, const void *conf, size_t len) | ||
2428 | { | ||
2429 | /* Set up BAR 0 */ | ||
2430 | dev->mmio_size += len; | ||
2431 | dev->mmio = realloc(dev->mmio, dev->mmio_size); | ||
2432 | memcpy(dev->mmio + 1, conf, len); | ||
2433 | |||
2434 | /* | ||
2435 | * 4.1.4.6: | ||
2436 | * | ||
2437 | * The device MUST present at least one VIRTIO_PCI_CAP_DEVICE_CFG | ||
2438 | * capability for any device type which has a device-specific | ||
2439 | * configuration. | ||
2440 | */ | ||
2441 | /* Hook up device cfg */ | ||
2442 | dev->config.cfg_access.cap.cap_next | ||
2443 | = offsetof(struct pci_config, device); | ||
2444 | |||
2445 | /* | ||
2446 | * 4.1.4.6.1: | ||
2447 | * | ||
2448 | * The offset for the device-specific configuration MUST be 4-byte | ||
2449 | * aligned. | ||
2450 | */ | ||
2451 | assert(dev->config.cfg_access.cap.cap_next % 4 == 0); | ||
2452 | |||
2453 | /* Fix up device cfg field length. */ | ||
2454 | dev->config.device.length = len; | ||
2455 | |||
2456 | /* The rest is the same as the no-config case */ | ||
2457 | no_device_config(dev); | ||
2458 | } | ||
2459 | |||
2460 | static void init_cap(struct virtio_pci_cap *cap, size_t caplen, int type, | ||
2461 | size_t bar_offset, size_t bar_bytes, u8 next) | ||
2462 | { | ||
2463 | cap->cap_vndr = PCI_CAP_ID_VNDR; | ||
2464 | cap->cap_next = next; | ||
2465 | cap->cap_len = caplen; | ||
2466 | cap->cfg_type = type; | ||
2467 | cap->bar = 0; | ||
2468 | memset(cap->padding, 0, sizeof(cap->padding)); | ||
2469 | cap->offset = bar_offset; | ||
2470 | cap->length = bar_bytes; | ||
1264 | } | 2471 | } |
1265 | 2472 | ||
1266 | /* | 2473 | /* |
1267 | * This routine sets the configuration fields for an existing device's | 2474 | * This sets up the pci_config structure, as defined in the virtio 1.0 |
1268 | * descriptor. It only works for the last device, but that's OK because that's | 2475 | * standard (and PCI standard). |
1269 | * how we use it. | ||
1270 | */ | 2476 | */ |
1271 | static void set_config(struct device *dev, unsigned len, const void *conf) | 2477 | static void init_pci_config(struct pci_config *pci, u16 type, |
2478 | u8 class, u8 subclass) | ||
1272 | { | 2479 | { |
1273 | /* Check we haven't overflowed our single page. */ | 2480 | size_t bar_offset, bar_len; |
1274 | if (device_config(dev) + len > devices.descpage + getpagesize()) | 2481 | |
1275 | errx(1, "Too many devices"); | 2482 | /* |
2483 | * 4.1.4.4.1: | ||
2484 | * | ||
2485 | * The device MUST either present notify_off_multiplier as an even | ||
2486 | * power of 2, or present notify_off_multiplier as 0. | ||
2487 | * | ||
2488 | * 2.1.2: | ||
2489 | * | ||
2490 | * The device MUST initialize device status to 0 upon reset. | ||
2491 | */ | ||
2492 | memset(pci, 0, sizeof(*pci)); | ||
2493 | |||
2494 | /* 4.1.2.1: Devices MUST have the PCI Vendor ID 0x1AF4 */ | ||
2495 | pci->vendor_id = 0x1AF4; | ||
2496 | /* 4.1.2.1: ... PCI Device ID calculated by adding 0x1040 ... */ | ||
2497 | pci->device_id = 0x1040 + type; | ||
2498 | |||
2499 | /* | ||
2500 | * PCI have specific codes for different types of devices. | ||
2501 | * Linux doesn't care, but it's a good clue for people looking | ||
2502 | * at the device. | ||
2503 | */ | ||
2504 | pci->class = class; | ||
2505 | pci->subclass = subclass; | ||
2506 | |||
2507 | /* | ||
2508 | * 4.1.2.1: | ||
2509 | * | ||
2510 | * Non-transitional devices SHOULD have a PCI Revision ID of 1 or | ||
2511 | * higher | ||
2512 | */ | ||
2513 | pci->revid = 1; | ||
2514 | |||
2515 | /* | ||
2516 | * 4.1.2.1: | ||
2517 | * | ||
2518 | * Non-transitional devices SHOULD have a PCI Subsystem Device ID of | ||
2519 | * 0x40 or higher. | ||
2520 | */ | ||
2521 | pci->subsystem_device_id = 0x40; | ||
2522 | |||
2523 | /* We use our dummy interrupt controller, and irq_line is the irq */ | ||
2524 | pci->irq_line = devices.next_irq++; | ||
2525 | pci->irq_pin = 0; | ||
2526 | |||
2527 | /* Support for extended capabilities. */ | ||
2528 | pci->status = (1 << 4); | ||
2529 | |||
2530 | /* Link them in. */ | ||
2531 | /* | ||
2532 | * 4.1.4.3.1: | ||
2533 | * | ||
2534 | * The device MUST present at least one common configuration | ||
2535 | * capability. | ||
2536 | */ | ||
2537 | pci->capabilities = offsetof(struct pci_config, common); | ||
2538 | |||
2539 | /* 4.1.4.3.1 ... offset MUST be 4-byte aligned. */ | ||
2540 | assert(pci->capabilities % 4 == 0); | ||
2541 | |||
2542 | bar_offset = offsetof(struct virtio_pci_mmio, cfg); | ||
2543 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->cfg); | ||
2544 | init_cap(&pci->common, sizeof(pci->common), VIRTIO_PCI_CAP_COMMON_CFG, | ||
2545 | bar_offset, bar_len, | ||
2546 | offsetof(struct pci_config, notify)); | ||
2547 | |||
2548 | /* | ||
2549 | * 4.1.4.4.1: | ||
2550 | * | ||
2551 | * The device MUST present at least one notification capability. | ||
2552 | */ | ||
2553 | bar_offset += bar_len; | ||
2554 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->notify); | ||
2555 | |||
2556 | /* | ||
2557 | * 4.1.4.4.1: | ||
2558 | * | ||
2559 | * The cap.offset MUST be 2-byte aligned. | ||
2560 | */ | ||
2561 | assert(pci->common.cap_next % 2 == 0); | ||
2562 | |||
2563 | /* FIXME: Use a non-zero notify_off, for per-queue notification? */ | ||
2564 | /* | ||
2565 | * 4.1.4.4.1: | ||
2566 | * | ||
2567 | * The value cap.length presented by the device MUST be at least 2 and | ||
2568 | * MUST be large enough to support queue notification offsets for all | ||
2569 | * supported queues in all possible configurations. | ||
2570 | */ | ||
2571 | assert(bar_len >= 2); | ||
2572 | |||
2573 | init_cap(&pci->notify.cap, sizeof(pci->notify), | ||
2574 | VIRTIO_PCI_CAP_NOTIFY_CFG, | ||
2575 | bar_offset, bar_len, | ||
2576 | offsetof(struct pci_config, isr)); | ||
2577 | |||
2578 | bar_offset += bar_len; | ||
2579 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->isr); | ||
2580 | /* | ||
2581 | * 4.1.4.5.1: | ||
2582 | * | ||
2583 | * The device MUST present at least one VIRTIO_PCI_CAP_ISR_CFG | ||
2584 | * capability. | ||
2585 | */ | ||
2586 | init_cap(&pci->isr, sizeof(pci->isr), | ||
2587 | VIRTIO_PCI_CAP_ISR_CFG, | ||
2588 | bar_offset, bar_len, | ||
2589 | offsetof(struct pci_config, cfg_access)); | ||
2590 | |||
2591 | /* | ||
2592 | * 4.1.4.7.1: | ||
2593 | * | ||
2594 | * The device MUST present at least one VIRTIO_PCI_CAP_PCI_CFG | ||
2595 | * capability. | ||
2596 | */ | ||
2597 | /* This doesn't have any presence in the BAR */ | ||
2598 | init_cap(&pci->cfg_access.cap, sizeof(pci->cfg_access), | ||
2599 | VIRTIO_PCI_CAP_PCI_CFG, | ||
2600 | 0, 0, 0); | ||
1276 | 2601 | ||
1277 | /* Copy in the config information, and store the length. */ | 2602 | bar_offset += bar_len + sizeof(((struct virtio_pci_mmio *)0)->padding); |
1278 | memcpy(device_config(dev), conf, len); | 2603 | assert(bar_offset == sizeof(struct virtio_pci_mmio)); |
1279 | dev->desc->config_len = len; | ||
1280 | 2604 | ||
1281 | /* Size must fit in config_len field (8 bits)! */ | 2605 | /* |
1282 | assert(dev->desc->config_len == len); | 2606 | * This gets sewn in and length set in set_device_config(). |
2607 | * Some devices don't have a device configuration interface, so | ||
2608 | * we never expose this if we don't call set_device_config(). | ||
2609 | */ | ||
2610 | init_cap(&pci->device, sizeof(pci->device), VIRTIO_PCI_CAP_DEVICE_CFG, | ||
2611 | bar_offset, 0, 0); | ||
1283 | } | 2612 | } |
1284 | 2613 | ||
1285 | /* | 2614 | /* |
1286 | * This routine does all the creation and setup of a new device, including | 2615 | * This routine does all the creation and setup of a new device, but we don't |
1287 | * calling new_dev_desc() to allocate the descriptor and device memory. We | 2616 | * actually place the MMIO region until we know the size (if any) of the |
1288 | * don't actually start the service threads until later. | 2617 | * device-specific config. And we don't actually start the service threads |
2618 | * until later. | ||
1289 | * | 2619 | * |
1290 | * See what I mean about userspace being boring? | 2620 | * See what I mean about userspace being boring? |
1291 | */ | 2621 | */ |
1292 | static struct device *new_device(const char *name, u16 type) | 2622 | static struct device *new_pci_device(const char *name, u16 type, |
2623 | u8 class, u8 subclass) | ||
1293 | { | 2624 | { |
1294 | struct device *dev = malloc(sizeof(*dev)); | 2625 | struct device *dev = malloc(sizeof(*dev)); |
1295 | 2626 | ||
1296 | /* Now we populate the fields one at a time. */ | 2627 | /* Now we populate the fields one at a time. */ |
1297 | dev->desc = new_dev_desc(type); | ||
1298 | dev->name = name; | 2628 | dev->name = name; |
1299 | dev->vq = NULL; | 2629 | dev->vq = NULL; |
1300 | dev->feature_len = 0; | ||
1301 | dev->num_vq = 0; | ||
1302 | dev->running = false; | 2630 | dev->running = false; |
1303 | dev->next = NULL; | 2631 | dev->wrote_features_ok = false; |
2632 | dev->mmio_size = sizeof(struct virtio_pci_mmio); | ||
2633 | dev->mmio = calloc(1, dev->mmio_size); | ||
2634 | dev->features = (u64)1 << VIRTIO_F_VERSION_1; | ||
2635 | dev->features_accepted = 0; | ||
1304 | 2636 | ||
1305 | /* | 2637 | if (devices.device_num + 1 >= MAX_PCI_DEVICES) |
1306 | * Append to device list. Prepending to a single-linked list is | 2638 | errx(1, "Can only handle 31 PCI devices"); |
1307 | * easier, but the user expects the devices to be arranged on the bus | 2639 | |
1308 | * in command-line order. The first network device on the command line | 2640 | init_pci_config(&dev->config, type, class, subclass); |
1309 | * is eth0, the first block device /dev/vda, etc. | 2641 | assert(!devices.pci[devices.device_num+1]); |
1310 | */ | 2642 | devices.pci[++devices.device_num] = dev; |
1311 | if (devices.lastdev) | ||
1312 | devices.lastdev->next = dev; | ||
1313 | else | ||
1314 | devices.dev = dev; | ||
1315 | devices.lastdev = dev; | ||
1316 | 2643 | ||
1317 | return dev; | 2644 | return dev; |
1318 | } | 2645 | } |
@@ -1324,6 +2651,7 @@ static struct device *new_device(const char *name, u16 type) | |||
1324 | static void setup_console(void) | 2651 | static void setup_console(void) |
1325 | { | 2652 | { |
1326 | struct device *dev; | 2653 | struct device *dev; |
2654 | struct virtio_console_config conf; | ||
1327 | 2655 | ||
1328 | /* If we can save the initial standard input settings... */ | 2656 | /* If we can save the initial standard input settings... */ |
1329 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { | 2657 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { |
@@ -1336,7 +2664,7 @@ static void setup_console(void) | |||
1336 | tcsetattr(STDIN_FILENO, TCSANOW, &term); | 2664 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
1337 | } | 2665 | } |
1338 | 2666 | ||
1339 | dev = new_device("console", VIRTIO_ID_CONSOLE); | 2667 | dev = new_pci_device("console", VIRTIO_ID_CONSOLE, 0x07, 0x00); |
1340 | 2668 | ||
1341 | /* We store the console state in dev->priv, and initialize it. */ | 2669 | /* We store the console state in dev->priv, and initialize it. */ |
1342 | dev->priv = malloc(sizeof(struct console_abort)); | 2670 | dev->priv = malloc(sizeof(struct console_abort)); |
@@ -1348,10 +2676,14 @@ static void setup_console(void) | |||
1348 | * stdin. When they put something in the output queue, we write it to | 2676 | * stdin. When they put something in the output queue, we write it to |
1349 | * stdout. | 2677 | * stdout. |
1350 | */ | 2678 | */ |
1351 | add_virtqueue(dev, VIRTQUEUE_NUM, console_input); | 2679 | add_pci_virtqueue(dev, console_input, "input"); |
1352 | add_virtqueue(dev, VIRTQUEUE_NUM, console_output); | 2680 | add_pci_virtqueue(dev, console_output, "output"); |
2681 | |||
2682 | /* We need a configuration area for the emerg_wr early writes. */ | ||
2683 | add_pci_feature(dev, VIRTIO_CONSOLE_F_EMERG_WRITE); | ||
2684 | set_device_config(dev, &conf, sizeof(conf)); | ||
1353 | 2685 | ||
1354 | verbose("device %u: console\n", ++devices.device_num); | 2686 | verbose("device %u: console\n", devices.device_num); |
1355 | } | 2687 | } |
1356 | /*:*/ | 2688 | /*:*/ |
1357 | 2689 | ||
@@ -1449,6 +2781,7 @@ static void configure_device(int fd, const char *tapif, u32 ipaddr) | |||
1449 | static int get_tun_device(char tapif[IFNAMSIZ]) | 2781 | static int get_tun_device(char tapif[IFNAMSIZ]) |
1450 | { | 2782 | { |
1451 | struct ifreq ifr; | 2783 | struct ifreq ifr; |
2784 | int vnet_hdr_sz; | ||
1452 | int netfd; | 2785 | int netfd; |
1453 | 2786 | ||
1454 | /* Start with this zeroed. Messy but sure. */ | 2787 | /* Start with this zeroed. Messy but sure. */ |
@@ -1476,6 +2809,18 @@ static int get_tun_device(char tapif[IFNAMSIZ]) | |||
1476 | */ | 2809 | */ |
1477 | ioctl(netfd, TUNSETNOCSUM, 1); | 2810 | ioctl(netfd, TUNSETNOCSUM, 1); |
1478 | 2811 | ||
2812 | /* | ||
2813 | * In virtio before 1.0 (aka legacy virtio), we added a 16-bit | ||
2814 | * field at the end of the network header iff | ||
2815 | * VIRTIO_NET_F_MRG_RXBUF was negotiated. For virtio 1.0, | ||
2816 | * that became the norm, but we need to tell the tun device | ||
2817 | * about our expanded header (which is called | ||
2818 | * virtio_net_hdr_mrg_rxbuf in the legacy system). | ||
2819 | */ | ||
2820 | vnet_hdr_sz = sizeof(struct virtio_net_hdr_v1); | ||
2821 | if (ioctl(netfd, TUNSETVNETHDRSZ, &vnet_hdr_sz) != 0) | ||
2822 | err(1, "Setting tun header size to %u", vnet_hdr_sz); | ||
2823 | |||
1479 | memcpy(tapif, ifr.ifr_name, IFNAMSIZ); | 2824 | memcpy(tapif, ifr.ifr_name, IFNAMSIZ); |
1480 | return netfd; | 2825 | return netfd; |
1481 | } | 2826 | } |
@@ -1499,12 +2844,12 @@ static void setup_tun_net(char *arg) | |||
1499 | net_info->tunfd = get_tun_device(tapif); | 2844 | net_info->tunfd = get_tun_device(tapif); |
1500 | 2845 | ||
1501 | /* First we create a new network device. */ | 2846 | /* First we create a new network device. */ |
1502 | dev = new_device("net", VIRTIO_ID_NET); | 2847 | dev = new_pci_device("net", VIRTIO_ID_NET, 0x02, 0x00); |
1503 | dev->priv = net_info; | 2848 | dev->priv = net_info; |
1504 | 2849 | ||
1505 | /* Network devices need a recv and a send queue, just like console. */ | 2850 | /* Network devices need a recv and a send queue, just like console. */ |
1506 | add_virtqueue(dev, VIRTQUEUE_NUM, net_input); | 2851 | add_pci_virtqueue(dev, net_input, "rx"); |
1507 | add_virtqueue(dev, VIRTQUEUE_NUM, net_output); | 2852 | add_pci_virtqueue(dev, net_output, "tx"); |
1508 | 2853 | ||
1509 | /* | 2854 | /* |
1510 | * We need a socket to perform the magic network ioctls to bring up the | 2855 | * We need a socket to perform the magic network ioctls to bring up the |
@@ -1524,7 +2869,7 @@ static void setup_tun_net(char *arg) | |||
1524 | p = strchr(arg, ':'); | 2869 | p = strchr(arg, ':'); |
1525 | if (p) { | 2870 | if (p) { |
1526 | str2mac(p+1, conf.mac); | 2871 | str2mac(p+1, conf.mac); |
1527 | add_feature(dev, VIRTIO_NET_F_MAC); | 2872 | add_pci_feature(dev, VIRTIO_NET_F_MAC); |
1528 | *p = '\0'; | 2873 | *p = '\0'; |
1529 | } | 2874 | } |
1530 | 2875 | ||
@@ -1538,25 +2883,21 @@ static void setup_tun_net(char *arg) | |||
1538 | configure_device(ipfd, tapif, ip); | 2883 | configure_device(ipfd, tapif, ip); |
1539 | 2884 | ||
1540 | /* Expect Guest to handle everything except UFO */ | 2885 | /* Expect Guest to handle everything except UFO */ |
1541 | add_feature(dev, VIRTIO_NET_F_CSUM); | 2886 | add_pci_feature(dev, VIRTIO_NET_F_CSUM); |
1542 | add_feature(dev, VIRTIO_NET_F_GUEST_CSUM); | 2887 | add_pci_feature(dev, VIRTIO_NET_F_GUEST_CSUM); |
1543 | add_feature(dev, VIRTIO_NET_F_GUEST_TSO4); | 2888 | add_pci_feature(dev, VIRTIO_NET_F_GUEST_TSO4); |
1544 | add_feature(dev, VIRTIO_NET_F_GUEST_TSO6); | 2889 | add_pci_feature(dev, VIRTIO_NET_F_GUEST_TSO6); |
1545 | add_feature(dev, VIRTIO_NET_F_GUEST_ECN); | 2890 | add_pci_feature(dev, VIRTIO_NET_F_GUEST_ECN); |
1546 | add_feature(dev, VIRTIO_NET_F_HOST_TSO4); | 2891 | add_pci_feature(dev, VIRTIO_NET_F_HOST_TSO4); |
1547 | add_feature(dev, VIRTIO_NET_F_HOST_TSO6); | 2892 | add_pci_feature(dev, VIRTIO_NET_F_HOST_TSO6); |
1548 | add_feature(dev, VIRTIO_NET_F_HOST_ECN); | 2893 | add_pci_feature(dev, VIRTIO_NET_F_HOST_ECN); |
1549 | /* We handle indirect ring entries */ | 2894 | /* We handle indirect ring entries */ |
1550 | add_feature(dev, VIRTIO_RING_F_INDIRECT_DESC); | 2895 | add_pci_feature(dev, VIRTIO_RING_F_INDIRECT_DESC); |
1551 | /* We're compliant with the damn spec. */ | 2896 | set_device_config(dev, &conf, sizeof(conf)); |
1552 | add_feature(dev, VIRTIO_F_ANY_LAYOUT); | ||
1553 | set_config(dev, sizeof(conf), &conf); | ||
1554 | 2897 | ||
1555 | /* We don't need the socket any more; setup is done. */ | 2898 | /* We don't need the socket any more; setup is done. */ |
1556 | close(ipfd); | 2899 | close(ipfd); |
1557 | 2900 | ||
1558 | devices.device_num++; | ||
1559 | |||
1560 | if (bridging) | 2901 | if (bridging) |
1561 | verbose("device %u: tun %s attached to bridge: %s\n", | 2902 | verbose("device %u: tun %s attached to bridge: %s\n", |
1562 | devices.device_num, tapif, arg); | 2903 | devices.device_num, tapif, arg); |
@@ -1607,7 +2948,7 @@ static void blk_request(struct virtqueue *vq) | |||
1607 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); | 2948 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
1608 | 2949 | ||
1609 | /* Copy the output header from the front of the iov (adjusts iov) */ | 2950 | /* Copy the output header from the front of the iov (adjusts iov) */ |
1610 | iov_consume(iov, out_num, &out, sizeof(out)); | 2951 | iov_consume(vq->dev, iov, out_num, &out, sizeof(out)); |
1611 | 2952 | ||
1612 | /* Find and trim end of iov input array, for our status byte. */ | 2953 | /* Find and trim end of iov input array, for our status byte. */ |
1613 | in = NULL; | 2954 | in = NULL; |
@@ -1619,7 +2960,7 @@ static void blk_request(struct virtqueue *vq) | |||
1619 | } | 2960 | } |
1620 | } | 2961 | } |
1621 | if (!in) | 2962 | if (!in) |
1622 | errx(1, "Bad virtblk cmd with no room for status"); | 2963 | bad_driver_vq(vq, "Bad virtblk cmd with no room for status"); |
1623 | 2964 | ||
1624 | /* | 2965 | /* |
1625 | * For historical reasons, block operations are expressed in 512 byte | 2966 | * For historical reasons, block operations are expressed in 512 byte |
@@ -1627,15 +2968,7 @@ static void blk_request(struct virtqueue *vq) | |||
1627 | */ | 2968 | */ |
1628 | off = out.sector * 512; | 2969 | off = out.sector * 512; |
1629 | 2970 | ||
1630 | /* | 2971 | if (out.type & VIRTIO_BLK_T_OUT) { |
1631 | * In general the virtio block driver is allowed to try SCSI commands. | ||
1632 | * It'd be nice if we supported eject, for example, but we don't. | ||
1633 | */ | ||
1634 | if (out.type & VIRTIO_BLK_T_SCSI_CMD) { | ||
1635 | fprintf(stderr, "Scsi commands unsupported\n"); | ||
1636 | *in = VIRTIO_BLK_S_UNSUPP; | ||
1637 | wlen = sizeof(*in); | ||
1638 | } else if (out.type & VIRTIO_BLK_T_OUT) { | ||
1639 | /* | 2972 | /* |
1640 | * Write | 2973 | * Write |
1641 | * | 2974 | * |
@@ -1657,7 +2990,7 @@ static void blk_request(struct virtqueue *vq) | |||
1657 | /* Trim it back to the correct length */ | 2990 | /* Trim it back to the correct length */ |
1658 | ftruncate64(vblk->fd, vblk->len); | 2991 | ftruncate64(vblk->fd, vblk->len); |
1659 | /* Die, bad Guest, die. */ | 2992 | /* Die, bad Guest, die. */ |
1660 | errx(1, "Write past end %llu+%u", off, ret); | 2993 | bad_driver_vq(vq, "Write past end %llu+%u", off, ret); |
1661 | } | 2994 | } |
1662 | 2995 | ||
1663 | wlen = sizeof(*in); | 2996 | wlen = sizeof(*in); |
@@ -1699,11 +3032,11 @@ static void setup_block_file(const char *filename) | |||
1699 | struct vblk_info *vblk; | 3032 | struct vblk_info *vblk; |
1700 | struct virtio_blk_config conf; | 3033 | struct virtio_blk_config conf; |
1701 | 3034 | ||
1702 | /* Creat the device. */ | 3035 | /* Create the device. */ |
1703 | dev = new_device("block", VIRTIO_ID_BLOCK); | 3036 | dev = new_pci_device("block", VIRTIO_ID_BLOCK, 0x01, 0x80); |
1704 | 3037 | ||
1705 | /* The device has one virtqueue, where the Guest places requests. */ | 3038 | /* The device has one virtqueue, where the Guest places requests. */ |
1706 | add_virtqueue(dev, VIRTQUEUE_NUM, blk_request); | 3039 | add_pci_virtqueue(dev, blk_request, "request"); |
1707 | 3040 | ||
1708 | /* Allocate the room for our own bookkeeping */ | 3041 | /* Allocate the room for our own bookkeeping */ |
1709 | vblk = dev->priv = malloc(sizeof(*vblk)); | 3042 | vblk = dev->priv = malloc(sizeof(*vblk)); |
@@ -1712,9 +3045,6 @@ static void setup_block_file(const char *filename) | |||
1712 | vblk->fd = open_or_die(filename, O_RDWR|O_LARGEFILE); | 3045 | vblk->fd = open_or_die(filename, O_RDWR|O_LARGEFILE); |
1713 | vblk->len = lseek64(vblk->fd, 0, SEEK_END); | 3046 | vblk->len = lseek64(vblk->fd, 0, SEEK_END); |
1714 | 3047 | ||
1715 | /* We support FLUSH. */ | ||
1716 | add_feature(dev, VIRTIO_BLK_F_FLUSH); | ||
1717 | |||
1718 | /* Tell Guest how many sectors this device has. */ | 3048 | /* Tell Guest how many sectors this device has. */ |
1719 | conf.capacity = cpu_to_le64(vblk->len / 512); | 3049 | conf.capacity = cpu_to_le64(vblk->len / 512); |
1720 | 3050 | ||
@@ -1722,20 +3052,19 @@ static void setup_block_file(const char *filename) | |||
1722 | * Tell Guest not to put in too many descriptors at once: two are used | 3052 | * Tell Guest not to put in too many descriptors at once: two are used |
1723 | * for the in and out elements. | 3053 | * for the in and out elements. |
1724 | */ | 3054 | */ |
1725 | add_feature(dev, VIRTIO_BLK_F_SEG_MAX); | 3055 | add_pci_feature(dev, VIRTIO_BLK_F_SEG_MAX); |
1726 | conf.seg_max = cpu_to_le32(VIRTQUEUE_NUM - 2); | 3056 | conf.seg_max = cpu_to_le32(VIRTQUEUE_NUM - 2); |
1727 | 3057 | ||
1728 | /* Don't try to put whole struct: we have 8 bit limit. */ | 3058 | set_device_config(dev, &conf, sizeof(struct virtio_blk_config)); |
1729 | set_config(dev, offsetof(struct virtio_blk_config, geometry), &conf); | ||
1730 | 3059 | ||
1731 | verbose("device %u: virtblock %llu sectors\n", | 3060 | verbose("device %u: virtblock %llu sectors\n", |
1732 | ++devices.device_num, le64_to_cpu(conf.capacity)); | 3061 | devices.device_num, le64_to_cpu(conf.capacity)); |
1733 | } | 3062 | } |
1734 | 3063 | ||
1735 | /*L:211 | 3064 | /*L:211 |
1736 | * Our random number generator device reads from /dev/random into the Guest's | 3065 | * Our random number generator device reads from /dev/urandom into the Guest's |
1737 | * input buffers. The usual case is that the Guest doesn't want random numbers | 3066 | * input buffers. The usual case is that the Guest doesn't want random numbers |
1738 | * and so has no buffers although /dev/random is still readable, whereas | 3067 | * and so has no buffers although /dev/urandom is still readable, whereas |
1739 | * console is the reverse. | 3068 | * console is the reverse. |
1740 | * | 3069 | * |
1741 | * The same logic applies, however. | 3070 | * The same logic applies, however. |
@@ -1754,7 +3083,7 @@ static void rng_input(struct virtqueue *vq) | |||
1754 | /* First we need a buffer from the Guests's virtqueue. */ | 3083 | /* First we need a buffer from the Guests's virtqueue. */ |
1755 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); | 3084 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
1756 | if (out_num) | 3085 | if (out_num) |
1757 | errx(1, "Output buffers in rng?"); | 3086 | bad_driver_vq(vq, "Output buffers in rng?"); |
1758 | 3087 | ||
1759 | /* | 3088 | /* |
1760 | * Just like the console write, we loop to cover the whole iovec. | 3089 | * Just like the console write, we loop to cover the whole iovec. |
@@ -1763,8 +3092,8 @@ static void rng_input(struct virtqueue *vq) | |||
1763 | while (!iov_empty(iov, in_num)) { | 3092 | while (!iov_empty(iov, in_num)) { |
1764 | len = readv(rng_info->rfd, iov, in_num); | 3093 | len = readv(rng_info->rfd, iov, in_num); |
1765 | if (len <= 0) | 3094 | if (len <= 0) |
1766 | err(1, "Read from /dev/random gave %i", len); | 3095 | err(1, "Read from /dev/urandom gave %i", len); |
1767 | iov_consume(iov, in_num, NULL, len); | 3096 | iov_consume(vq->dev, iov, in_num, NULL, len); |
1768 | totlen += len; | 3097 | totlen += len; |
1769 | } | 3098 | } |
1770 | 3099 | ||
@@ -1780,17 +3109,20 @@ static void setup_rng(void) | |||
1780 | struct device *dev; | 3109 | struct device *dev; |
1781 | struct rng_info *rng_info = malloc(sizeof(*rng_info)); | 3110 | struct rng_info *rng_info = malloc(sizeof(*rng_info)); |
1782 | 3111 | ||
1783 | /* Our device's privat info simply contains the /dev/random fd. */ | 3112 | /* Our device's private info simply contains the /dev/urandom fd. */ |
1784 | rng_info->rfd = open_or_die("/dev/random", O_RDONLY); | 3113 | rng_info->rfd = open_or_die("/dev/urandom", O_RDONLY); |
1785 | 3114 | ||
1786 | /* Create the new device. */ | 3115 | /* Create the new device. */ |
1787 | dev = new_device("rng", VIRTIO_ID_RNG); | 3116 | dev = new_pci_device("rng", VIRTIO_ID_RNG, 0xff, 0); |
1788 | dev->priv = rng_info; | 3117 | dev->priv = rng_info; |
1789 | 3118 | ||
1790 | /* The device has one virtqueue, where the Guest places inbufs. */ | 3119 | /* The device has one virtqueue, where the Guest places inbufs. */ |
1791 | add_virtqueue(dev, VIRTQUEUE_NUM, rng_input); | 3120 | add_pci_virtqueue(dev, rng_input, "input"); |
1792 | 3121 | ||
1793 | verbose("device %u: rng\n", devices.device_num++); | 3122 | /* We don't have any configuration space */ |
3123 | no_device_config(dev); | ||
3124 | |||
3125 | verbose("device %u: rng\n", devices.device_num); | ||
1794 | } | 3126 | } |
1795 | /* That's the end of device setup. */ | 3127 | /* That's the end of device setup. */ |
1796 | 3128 | ||
@@ -1820,17 +3152,23 @@ static void __attribute__((noreturn)) restart_guest(void) | |||
1820 | static void __attribute__((noreturn)) run_guest(void) | 3152 | static void __attribute__((noreturn)) run_guest(void) |
1821 | { | 3153 | { |
1822 | for (;;) { | 3154 | for (;;) { |
1823 | unsigned long notify_addr; | 3155 | struct lguest_pending notify; |
1824 | int readval; | 3156 | int readval; |
1825 | 3157 | ||
1826 | /* We read from the /dev/lguest device to run the Guest. */ | 3158 | /* We read from the /dev/lguest device to run the Guest. */ |
1827 | readval = pread(lguest_fd, ¬ify_addr, | 3159 | readval = pread(lguest_fd, ¬ify, sizeof(notify), cpu_id); |
1828 | sizeof(notify_addr), cpu_id); | 3160 | if (readval == sizeof(notify)) { |
1829 | 3161 | if (notify.trap == 13) { | |
1830 | /* One unsigned long means the Guest did HCALL_NOTIFY */ | 3162 | verbose("Emulating instruction at %#x\n", |
1831 | if (readval == sizeof(notify_addr)) { | 3163 | getreg(eip)); |
1832 | verbose("Notify on address %#lx\n", notify_addr); | 3164 | emulate_insn(notify.insn); |
1833 | handle_output(notify_addr); | 3165 | } else if (notify.trap == 14) { |
3166 | verbose("Emulating MMIO at %#x\n", | ||
3167 | getreg(eip)); | ||
3168 | emulate_mmio(notify.addr, notify.insn); | ||
3169 | } else | ||
3170 | errx(1, "Unknown trap %i addr %#08x\n", | ||
3171 | notify.trap, notify.addr); | ||
1834 | /* ENOENT means the Guest died. Reading tells us why. */ | 3172 | /* ENOENT means the Guest died. Reading tells us why. */ |
1835 | } else if (errno == ENOENT) { | 3173 | } else if (errno == ENOENT) { |
1836 | char reason[1024] = { 0 }; | 3174 | char reason[1024] = { 0 }; |
@@ -1893,11 +3231,9 @@ int main(int argc, char *argv[]) | |||
1893 | main_args = argv; | 3231 | main_args = argv; |
1894 | 3232 | ||
1895 | /* | 3233 | /* |
1896 | * First we initialize the device list. We keep a pointer to the last | 3234 | * First we initialize the device list. We remember next interrupt |
1897 | * device, and the next interrupt number to use for devices (1: | 3235 | * number to use for devices (1: remember that 0 is used by the timer). |
1898 | * remember that 0 is used by the timer). | ||
1899 | */ | 3236 | */ |
1900 | devices.lastdev = NULL; | ||
1901 | devices.next_irq = 1; | 3237 | devices.next_irq = 1; |
1902 | 3238 | ||
1903 | /* We're CPU 0. In fact, that's the only CPU possible right now. */ | 3239 | /* We're CPU 0. In fact, that's the only CPU possible right now. */ |
@@ -1921,12 +3257,14 @@ int main(int argc, char *argv[]) | |||
1921 | guest_base = map_zeroed_pages(mem / getpagesize() | 3257 | guest_base = map_zeroed_pages(mem / getpagesize() |
1922 | + DEVICE_PAGES); | 3258 | + DEVICE_PAGES); |
1923 | guest_limit = mem; | 3259 | guest_limit = mem; |
1924 | guest_max = mem + DEVICE_PAGES*getpagesize(); | 3260 | guest_max = guest_mmio = mem + DEVICE_PAGES*getpagesize(); |
1925 | devices.descpage = get_pages(1); | ||
1926 | break; | 3261 | break; |
1927 | } | 3262 | } |
1928 | } | 3263 | } |
1929 | 3264 | ||
3265 | /* We always have a console device, and it's always device 1. */ | ||
3266 | setup_console(); | ||
3267 | |||
1930 | /* The options are fairly straight-forward */ | 3268 | /* The options are fairly straight-forward */ |
1931 | while ((c = getopt_long(argc, argv, "v", opts, NULL)) != EOF) { | 3269 | while ((c = getopt_long(argc, argv, "v", opts, NULL)) != EOF) { |
1932 | switch (c) { | 3270 | switch (c) { |
@@ -1967,8 +3305,8 @@ int main(int argc, char *argv[]) | |||
1967 | 3305 | ||
1968 | verbose("Guest base is at %p\n", guest_base); | 3306 | verbose("Guest base is at %p\n", guest_base); |
1969 | 3307 | ||
1970 | /* We always have a console device */ | 3308 | /* Initialize the (fake) PCI host bridge device. */ |
1971 | setup_console(); | 3309 | init_pci_host_bridge(); |
1972 | 3310 | ||
1973 | /* Now we load the kernel */ | 3311 | /* Now we load the kernel */ |
1974 | start = load_kernel(open_or_die(argv[optind+1], O_RDONLY)); | 3312 | start = load_kernel(open_or_die(argv[optind+1], O_RDONLY)); |