Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1 | /*P:100 |
| 2 | * This is the Launcher code, a simple program which lays out the "physical" |
| 3 | * memory for the new Guest by mapping the kernel image and the virtual |
| 4 | * devices, then opens /dev/lguest to tell the kernel about the Guest and |
| 5 | * control it. |
| 6 | :*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 7 | #define _LARGEFILE64_SOURCE |
| 8 | #define _GNU_SOURCE |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <unistd.h> |
| 12 | #include <err.h> |
| 13 | #include <stdint.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <elf.h> |
| 16 | #include <sys/mman.h> |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 17 | #include <sys/param.h> |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/wait.h> |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 21 | #include <sys/eventfd.h> |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <errno.h> |
| 25 | #include <ctype.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <net/if.h> |
| 32 | #include <linux/sockios.h> |
| 33 | #include <linux/if_tun.h> |
| 34 | #include <sys/uio.h> |
| 35 | #include <termios.h> |
| 36 | #include <getopt.h> |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 37 | #include <assert.h> |
| 38 | #include <sched.h> |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 39 | #include <limits.h> |
| 40 | #include <stddef.h> |
Rusty Russell | a161883 | 2008-07-29 09:58:35 -0500 | [diff] [blame] | 41 | #include <signal.h> |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 42 | #include <pwd.h> |
| 43 | #include <grp.h> |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 44 | #include <sys/user.h> |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 45 | #include <linux/pci_regs.h> |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 46 | |
Rusty Russell | 927cfb9 | 2013-07-15 10:50:13 +0930 | [diff] [blame] | 47 | #ifndef VIRTIO_F_ANY_LAYOUT |
| 48 | #define VIRTIO_F_ANY_LAYOUT 27 |
| 49 | #endif |
| 50 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 51 | /*L:110 |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 52 | * We can ignore the 43 include files we need for this program, but I do want |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 53 | * to draw attention to the use of kernel-style types. |
Rusty Russell | db24e8c | 2007-10-25 14:09:25 +1000 | [diff] [blame] | 54 | * |
| 55 | * As Linus said, "C is a Spartan language, and so should your naming be." I |
| 56 | * like these abbreviations, so we define them here. Note that u64 is always |
| 57 | * unsigned long long, which works on all Linux systems: this means that we can |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 58 | * use %llu in printf for any u64. |
| 59 | */ |
Rusty Russell | db24e8c | 2007-10-25 14:09:25 +1000 | [diff] [blame] | 60 | typedef unsigned long long u64; |
| 61 | typedef uint32_t u32; |
| 62 | typedef uint16_t u16; |
| 63 | typedef uint8_t u8; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 64 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 65 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 66 | #define VIRTIO_PCI_NO_LEGACY |
| 67 | |
| 68 | /* Use in-kernel ones, which defines VIRTIO_F_VERSION_1 */ |
| 69 | #include "../../include/uapi/linux/virtio_config.h" |
Rusty Russell | e6dc041 | 2013-07-04 11:22:58 +0930 | [diff] [blame] | 70 | #include <linux/virtio_net.h> |
| 71 | #include <linux/virtio_blk.h> |
| 72 | #include <linux/virtio_console.h> |
| 73 | #include <linux/virtio_rng.h> |
| 74 | #include <linux/virtio_ring.h> |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 75 | #include "../../include/uapi/linux/virtio_pci.h" |
Rusty Russell | e6dc041 | 2013-07-04 11:22:58 +0930 | [diff] [blame] | 76 | #include <asm/bootparam.h> |
| 77 | #include "../../include/linux/lguest_launcher.h" |
| 78 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 79 | #define BRIDGE_PFX "bridge:" |
| 80 | #ifndef SIOCBRADDIF |
| 81 | #define SIOCBRADDIF 0x89a2 /* add interface to bridge */ |
| 82 | #endif |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 83 | /* We can have up to 256 pages for devices. */ |
| 84 | #define DEVICE_PAGES 256 |
Rusty Russell | 0f0c4fa | 2008-07-29 09:58:37 -0500 | [diff] [blame] | 85 | /* This will occupy 3 pages: it must be a power of 2. */ |
| 86 | #define VIRTQUEUE_NUM 256 |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 87 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 88 | /*L:120 |
| 89 | * verbose is both a global flag and a macro. The C preprocessor allows |
| 90 | * this, and although I wouldn't recommend it, it works quite nicely here. |
| 91 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 92 | static bool verbose; |
| 93 | #define verbose(args...) \ |
| 94 | do { if (verbose) printf(args); } while(0) |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 95 | /*:*/ |
| 96 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 97 | /* The pointer to the start of guest memory. */ |
| 98 | static void *guest_base; |
| 99 | /* The maximum guest physical address allowed, and maximum possible. */ |
Rusty Russell | 0a6bcc1 | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 100 | static unsigned long guest_limit, guest_max, guest_mmio; |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 101 | /* The /dev/lguest file descriptor. */ |
| 102 | static int lguest_fd; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 103 | |
Glauber de Oliveira Costa | e3283fa | 2008-01-07 11:05:23 -0200 | [diff] [blame] | 104 | /* a per-cpu variable indicating whose vcpu is currently running */ |
| 105 | static unsigned int __thread cpu_id; |
| 106 | |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 107 | /* 5 bit device number in the PCI_CONFIG_ADDR => 32 only */ |
| 108 | #define MAX_PCI_DEVICES 32 |
| 109 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 110 | /* This is our list of devices. */ |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 111 | struct device_list { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 112 | /* Counter to assign interrupt numbers. */ |
| 113 | unsigned int next_irq; |
| 114 | |
| 115 | /* Counter to print out convenient device numbers. */ |
| 116 | unsigned int device_num; |
| 117 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 118 | /* The descriptor page for the devices. */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 119 | u8 *descpage; |
| 120 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 121 | /* A single linked list of devices. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 122 | struct device *dev; |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 123 | /* And a pointer to the last device for easy append. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 124 | struct device *lastdev; |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 125 | |
| 126 | /* PCI devices. */ |
| 127 | struct device *pci[MAX_PCI_DEVICES]; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 130 | /* The list of Guest devices, based on command line arguments. */ |
| 131 | static struct device_list devices; |
| 132 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 133 | struct virtio_pci_cfg_cap { |
| 134 | struct virtio_pci_cap cap; |
| 135 | u32 window; /* Data for BAR access. */ |
| 136 | }; |
| 137 | |
| 138 | struct virtio_pci_mmio { |
| 139 | struct virtio_pci_common_cfg cfg; |
| 140 | u16 notify; |
| 141 | u8 isr; |
| 142 | u8 padding; |
| 143 | /* Device-specific configuration follows this. */ |
| 144 | }; |
| 145 | |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 146 | /* This is the layout (little-endian) of the PCI config space. */ |
| 147 | struct pci_config { |
| 148 | u16 vendor_id, device_id; |
| 149 | u16 command, status; |
| 150 | u8 revid, prog_if, subclass, class; |
| 151 | u8 cacheline_size, lat_timer, header_type, bist; |
| 152 | u32 bar[6]; |
| 153 | u32 cardbus_cis_ptr; |
| 154 | u16 subsystem_vendor_id, subsystem_device_id; |
| 155 | u32 expansion_rom_addr; |
| 156 | u8 capabilities, reserved1[3]; |
| 157 | u32 reserved2; |
| 158 | u8 irq_line, irq_pin, min_grant, max_latency; |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 159 | |
| 160 | /* Now, this is the linked capability list. */ |
| 161 | struct virtio_pci_cap common; |
| 162 | struct virtio_pci_notify_cap notify; |
| 163 | struct virtio_pci_cap isr; |
| 164 | struct virtio_pci_cap device; |
| 165 | /* FIXME: Implement this! */ |
| 166 | struct virtio_pci_cfg_cap cfg_access; |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 167 | }; |
| 168 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 169 | /* The device structure describes a single device. */ |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 170 | struct device { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 171 | /* The linked-list pointer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 172 | struct device *next; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 173 | |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 174 | /* The device's descriptor, as mapped into the Guest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 175 | struct lguest_device_desc *desc; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 176 | |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 177 | /* We can't trust desc values once Guest has booted: we use these. */ |
| 178 | unsigned int feature_len; |
| 179 | unsigned int num_vq; |
| 180 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 181 | /* The name of this device, for --verbose. */ |
| 182 | const char *name; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 183 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 184 | /* Any queues attached to this device */ |
| 185 | struct virtqueue *vq; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 186 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 187 | /* Is it operational */ |
| 188 | bool running; |
Rusty Russell | a007a75 | 2008-05-02 21:50:53 -0500 | [diff] [blame] | 189 | |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 190 | /* PCI configuration */ |
| 191 | union { |
| 192 | struct pci_config config; |
| 193 | u32 config_words[sizeof(struct pci_config) / sizeof(u32)]; |
| 194 | }; |
| 195 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 196 | /* Features we offer, and those accepted. */ |
| 197 | u64 features, features_accepted; |
| 198 | |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 199 | /* Device-specific config hangs off the end of this. */ |
| 200 | struct virtio_pci_mmio *mmio; |
| 201 | |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 202 | /* PCI MMIO resources (all in BAR0) */ |
| 203 | size_t mmio_size; |
| 204 | u32 mmio_addr; |
| 205 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 206 | /* Device-specific data. */ |
| 207 | void *priv; |
| 208 | }; |
| 209 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 210 | /* The virtqueue structure describes a queue attached to a device. */ |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 211 | struct virtqueue { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 212 | struct virtqueue *next; |
| 213 | |
| 214 | /* Which device owns me. */ |
| 215 | struct device *dev; |
| 216 | |
| 217 | /* The configuration for this queue. */ |
| 218 | struct lguest_vqconfig config; |
| 219 | |
| 220 | /* The actual ring of buffers. */ |
| 221 | struct vring vring; |
| 222 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 223 | /* The information about this virtqueue (we only use queue_size on) */ |
| 224 | struct virtio_pci_common_cfg pci_config; |
| 225 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 226 | /* Last available index we saw. */ |
| 227 | u16 last_avail_idx; |
| 228 | |
Rusty Russell | 95c517c | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 229 | /* How many are used since we sent last irq? */ |
| 230 | unsigned int pending_used; |
| 231 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 232 | /* Eventfd where Guest notifications arrive. */ |
| 233 | int eventfd; |
Rusty Russell | 2088761 | 2008-05-30 15:09:46 -0500 | [diff] [blame] | 234 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 235 | /* Function for the thread which is servicing this virtqueue. */ |
| 236 | void (*service)(struct virtqueue *vq); |
| 237 | pid_t thread; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 238 | }; |
| 239 | |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 240 | /* Remember the arguments to the program so we can "reboot" */ |
| 241 | static char **main_args; |
| 242 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 243 | /* The original tty settings to restore on exit. */ |
| 244 | static struct termios orig_term; |
| 245 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 246 | /* |
| 247 | * We have to be careful with barriers: our devices are all run in separate |
Rusty Russell | f7027c6 | 2009-06-12 22:27:00 -0600 | [diff] [blame] | 248 | * threads and so we need to make sure that changes visible to the Guest happen |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 249 | * in precise order. |
| 250 | */ |
Rusty Russell | f7027c6 | 2009-06-12 22:27:00 -0600 | [diff] [blame] | 251 | #define wmb() __asm__ __volatile__("" : : : "memory") |
Rusty Russell | 0d69a65 | 2013-07-02 15:35:14 +0930 | [diff] [blame] | 252 | #define rmb() __asm__ __volatile__("lock; addl $0,0(%%esp)" : : : "memory") |
| 253 | #define mb() __asm__ __volatile__("lock; addl $0,0(%%esp)" : : : "memory") |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 254 | |
Rusty Russell | b511179 | 2008-07-29 09:58:34 -0500 | [diff] [blame] | 255 | /* Wrapper for the last available index. Makes it easier to change. */ |
| 256 | #define lg_last_avail(vq) ((vq)->last_avail_idx) |
| 257 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 258 | /* |
| 259 | * The virtio configuration space is defined to be little-endian. x86 is |
| 260 | * little-endian too, but it's nice to be explicit so we have these helpers. |
| 261 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 262 | #define cpu_to_le16(v16) (v16) |
| 263 | #define cpu_to_le32(v32) (v32) |
| 264 | #define cpu_to_le64(v64) (v64) |
| 265 | #define le16_to_cpu(v16) (v16) |
| 266 | #define le32_to_cpu(v32) (v32) |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 267 | #define le64_to_cpu(v64) (v64) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 268 | |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 269 | /* Is this iovec empty? */ |
| 270 | static bool iov_empty(const struct iovec iov[], unsigned int num_iov) |
| 271 | { |
| 272 | unsigned int i; |
| 273 | |
| 274 | for (i = 0; i < num_iov; i++) |
| 275 | if (iov[i].iov_len) |
| 276 | return false; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | /* Take len bytes from the front of this iovec. */ |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 281 | static void iov_consume(struct iovec iov[], unsigned num_iov, |
| 282 | void *dest, unsigned len) |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 283 | { |
| 284 | unsigned int i; |
| 285 | |
| 286 | for (i = 0; i < num_iov; i++) { |
| 287 | unsigned int used; |
| 288 | |
| 289 | used = iov[i].iov_len < len ? iov[i].iov_len : len; |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 290 | if (dest) { |
| 291 | memcpy(dest, iov[i].iov_base, used); |
| 292 | dest += used; |
| 293 | } |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 294 | iov[i].iov_base += used; |
| 295 | iov[i].iov_len -= used; |
| 296 | len -= used; |
| 297 | } |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 298 | if (len != 0) |
| 299 | errx(1, "iovec too short!"); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 302 | /* The device virtqueue descriptors are followed by feature bitmasks. */ |
| 303 | static u8 *get_feature_bits(struct device *dev) |
| 304 | { |
| 305 | return (u8 *)(dev->desc + 1) |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 306 | + dev->num_vq * sizeof(struct lguest_vqconfig); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 309 | /*L:100 |
| 310 | * The Launcher code itself takes us out into userspace, that scary place where |
| 311 | * pointers run wild and free! Unfortunately, like most userspace programs, |
| 312 | * it's quite boring (which is why everyone likes to hack on the kernel!). |
| 313 | * Perhaps if you make up an Lguest Drinking Game at this point, it will get |
| 314 | * you through this section. Or, maybe not. |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 315 | * |
| 316 | * The Launcher sets up a big chunk of memory to be the Guest's "physical" |
| 317 | * memory and stores it in "guest_base". In other words, Guest physical == |
| 318 | * Launcher virtual with an offset. |
| 319 | * |
| 320 | * This can be tough to get your head around, but usually it just means that we |
Francis Galiegue | a33f322 | 2010-04-23 00:08:02 +0200 | [diff] [blame] | 321 | * use these trivial conversion functions when the Guest gives us its |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 322 | * "physical" addresses: |
| 323 | */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 324 | static void *from_guest_phys(unsigned long addr) |
| 325 | { |
| 326 | return guest_base + addr; |
| 327 | } |
| 328 | |
| 329 | static unsigned long to_guest_phys(const void *addr) |
| 330 | { |
| 331 | return (addr - guest_base); |
| 332 | } |
| 333 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 334 | /*L:130 |
| 335 | * Loading the Kernel. |
| 336 | * |
| 337 | * We start with couple of simple helper routines. open_or_die() avoids |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 338 | * error-checking code cluttering the callers: |
| 339 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 340 | static int open_or_die(const char *name, int flags) |
| 341 | { |
| 342 | int fd = open(name, flags); |
| 343 | if (fd < 0) |
| 344 | err(1, "Failed to open %s", name); |
| 345 | return fd; |
| 346 | } |
| 347 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 348 | /* map_zeroed_pages() takes a number of pages. */ |
| 349 | static void *map_zeroed_pages(unsigned int num) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 350 | { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 351 | int fd = open_or_die("/dev/zero", O_RDONLY); |
| 352 | void *addr; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 353 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 354 | /* |
| 355 | * We use a private mapping (ie. if we write to the page, it will be |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 356 | * copied). We allocate an extra two pages PROT_NONE to act as guard |
| 357 | * pages against read/write attempts that exceed allocated space. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 358 | */ |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 359 | addr = mmap(NULL, getpagesize() * (num+2), |
| 360 | PROT_NONE, MAP_PRIVATE, fd, 0); |
| 361 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 362 | if (addr == MAP_FAILED) |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 363 | err(1, "Mmapping %u pages of /dev/zero", num); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 364 | |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 365 | if (mprotect(addr + getpagesize(), getpagesize() * num, |
| 366 | PROT_READ|PROT_WRITE) == -1) |
| 367 | err(1, "mprotect rw %u pages failed", num); |
| 368 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 369 | /* |
| 370 | * One neat mmap feature is that you can close the fd, and it |
| 371 | * stays mapped. |
| 372 | */ |
Mark McLoughlin | 34bdaab | 2008-06-13 14:04:58 +0100 | [diff] [blame] | 373 | close(fd); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 374 | |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 375 | /* Return address after PROT_NONE page */ |
| 376 | return addr + getpagesize(); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Get some more pages for a device. */ |
| 380 | static void *get_pages(unsigned int num) |
| 381 | { |
| 382 | void *addr = from_guest_phys(guest_limit); |
| 383 | |
| 384 | guest_limit += num * getpagesize(); |
| 385 | if (guest_limit > guest_max) |
| 386 | errx(1, "Not enough memory for devices"); |
| 387 | return addr; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Rusty Russell | 0a6bcc1 | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 390 | /* Get some bytes which won't be mapped into the guest. */ |
| 391 | static unsigned long get_mmio_region(size_t size) |
| 392 | { |
| 393 | unsigned long addr = guest_mmio; |
| 394 | size_t i; |
| 395 | |
| 396 | if (!size) |
| 397 | return addr; |
| 398 | |
| 399 | /* Size has to be a power of 2 (and multiple of 16) */ |
| 400 | for (i = 1; i < size; i <<= 1); |
| 401 | |
| 402 | guest_mmio += i; |
| 403 | |
| 404 | return addr; |
| 405 | } |
| 406 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 407 | /* |
| 408 | * This routine is used to load the kernel or initrd. It tries mmap, but if |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 409 | * that fails (Plan 9's kernel file isn't nicely aligned on page boundaries), |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 410 | * it falls back to reading the memory in. |
| 411 | */ |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 412 | static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) |
| 413 | { |
| 414 | ssize_t r; |
| 415 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 416 | /* |
| 417 | * We map writable even though for some segments are marked read-only. |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 418 | * The kernel really wants to be writable: it patches its own |
| 419 | * instructions. |
| 420 | * |
| 421 | * MAP_PRIVATE means that the page won't be copied until a write is |
| 422 | * done to it. This allows us to share untouched memory between |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 423 | * Guests. |
| 424 | */ |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 425 | if (mmap(addr, len, PROT_READ|PROT_WRITE, |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 426 | MAP_FIXED|MAP_PRIVATE, fd, offset) != MAP_FAILED) |
| 427 | return; |
| 428 | |
| 429 | /* pread does a seek and a read in one shot: saves a few lines. */ |
| 430 | r = pread(fd, addr, len, offset); |
| 431 | if (r != len) |
| 432 | err(1, "Reading offset %lu len %lu gave %zi", offset, len, r); |
| 433 | } |
| 434 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 435 | /* |
| 436 | * This routine takes an open vmlinux image, which is in ELF, and maps it into |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 437 | * the Guest memory. ELF = Embedded Linking Format, which is the format used |
| 438 | * by all modern binaries on Linux including the kernel. |
| 439 | * |
| 440 | * The ELF headers give *two* addresses: a physical address, and a virtual |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 441 | * address. We use the physical address; the Guest will map itself to the |
| 442 | * virtual address. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 443 | * |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 444 | * We return the starting address. |
| 445 | */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 446 | static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 447 | { |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 448 | Elf32_Phdr phdr[ehdr->e_phnum]; |
| 449 | unsigned int i; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 450 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 451 | /* |
| 452 | * Sanity checks on the main ELF header: an x86 executable with a |
| 453 | * reasonable number of correctly-sized program headers. |
| 454 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 455 | if (ehdr->e_type != ET_EXEC |
| 456 | || ehdr->e_machine != EM_386 |
| 457 | || ehdr->e_phentsize != sizeof(Elf32_Phdr) |
| 458 | || ehdr->e_phnum < 1 || ehdr->e_phnum > 65536U/sizeof(Elf32_Phdr)) |
| 459 | errx(1, "Malformed elf header"); |
| 460 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 461 | /* |
| 462 | * An ELF executable contains an ELF header and a number of "program" |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 463 | * headers which indicate which parts ("segments") of the program to |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 464 | * load where. |
| 465 | */ |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 466 | |
| 467 | /* We read in all the program headers at once: */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 468 | if (lseek(elf_fd, ehdr->e_phoff, SEEK_SET) < 0) |
| 469 | err(1, "Seeking to program headers"); |
| 470 | if (read(elf_fd, phdr, sizeof(phdr)) != sizeof(phdr)) |
| 471 | err(1, "Reading program headers"); |
| 472 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 473 | /* |
| 474 | * Try all the headers: there are usually only three. A read-only one, |
| 475 | * a read-write one, and a "note" section which we don't load. |
| 476 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 477 | for (i = 0; i < ehdr->e_phnum; i++) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 478 | /* If this isn't a loadable segment, we ignore it */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 479 | if (phdr[i].p_type != PT_LOAD) |
| 480 | continue; |
| 481 | |
| 482 | verbose("Section %i: size %i addr %p\n", |
| 483 | i, phdr[i].p_memsz, (void *)phdr[i].p_paddr); |
| 484 | |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 485 | /* We map this section of the file at its physical address. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 486 | map_at(elf_fd, from_guest_phys(phdr[i].p_paddr), |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 487 | phdr[i].p_offset, phdr[i].p_filesz); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Rusty Russell | 814a0e5 | 2007-10-22 11:29:44 +1000 | [diff] [blame] | 490 | /* The entry point is given in the ELF header. */ |
| 491 | return ehdr->e_entry; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 494 | /*L:150 |
| 495 | * A bzImage, unlike an ELF file, is not meant to be loaded. You're supposed |
| 496 | * to jump into it and it will unpack itself. We used to have to perform some |
| 497 | * hairy magic because the unpacking code scared me. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 498 | * |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 499 | * Fortunately, Jeremy Fitzhardinge convinced me it wasn't that hard and wrote |
| 500 | * a small patch to jump over the tricky bits in the Guest, so now we just read |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 501 | * the funky header so we know where in the file to load, and away we go! |
| 502 | */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 503 | static unsigned long load_bzimage(int fd) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 504 | { |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 505 | struct boot_params boot; |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 506 | int r; |
| 507 | /* Modern bzImages get loaded at 1M. */ |
| 508 | void *p = from_guest_phys(0x100000); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 509 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 510 | /* |
| 511 | * Go back to the start of the file and read the header. It should be |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 512 | * a Linux boot header (see Documentation/x86/boot.txt) |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 513 | */ |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 514 | lseek(fd, 0, SEEK_SET); |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 515 | read(fd, &boot, sizeof(boot)); |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 516 | |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 517 | /* Inside the setup_hdr, we expect the magic "HdrS" */ |
| 518 | if (memcmp(&boot.hdr.header, "HdrS", 4) != 0) |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 519 | errx(1, "This doesn't look like a bzImage to me"); |
| 520 | |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 521 | /* Skip over the extra sectors of the header. */ |
| 522 | lseek(fd, (boot.hdr.setup_sects+1) * 512, SEEK_SET); |
Rusty Russell | 5bbf89f | 2007-10-22 11:29:56 +1000 | [diff] [blame] | 523 | |
| 524 | /* Now read everything into memory. in nice big chunks. */ |
| 525 | while ((r = read(fd, p, 65536)) > 0) |
| 526 | p += r; |
| 527 | |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 528 | /* Finally, code32_start tells us where to enter the kernel. */ |
| 529 | return boot.hdr.code32_start; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 532 | /*L:140 |
| 533 | * Loading the kernel is easy when it's a "vmlinux", but most kernels |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 534 | * come wrapped up in the self-decompressing "bzImage" format. With a little |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 535 | * work, we can load those, too. |
| 536 | */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 537 | static unsigned long load_kernel(int fd) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 538 | { |
| 539 | Elf32_Ehdr hdr; |
| 540 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 541 | /* Read in the first few bytes. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 542 | if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
| 543 | err(1, "Reading kernel"); |
| 544 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 545 | /* If it's an ELF file, it starts with "\177ELF" */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 546 | if (memcmp(hdr.e_ident, ELFMAG, SELFMAG) == 0) |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 547 | return map_elf(fd, &hdr); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 548 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 549 | /* Otherwise we assume it's a bzImage, and try to load it. */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 550 | return load_bzimage(fd); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 553 | /* |
| 554 | * This is a trivial little helper to align pages. Andi Kleen hated it because |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 555 | * it calls getpagesize() twice: "it's dumb code." |
| 556 | * |
| 557 | * Kernel guys get really het up about optimization, even when it's not |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 558 | * necessary. I leave this code as a reaction against that. |
| 559 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 560 | static inline unsigned long page_align(unsigned long addr) |
| 561 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 562 | /* Add upwards and truncate downwards. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 563 | return ((addr + getpagesize()-1) & ~(getpagesize()-1)); |
| 564 | } |
| 565 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 566 | /*L:180 |
| 567 | * An "initial ram disk" is a disk image loaded into memory along with the |
| 568 | * kernel which the kernel can use to boot from without needing any drivers. |
| 569 | * Most distributions now use this as standard: the initrd contains the code to |
| 570 | * load the appropriate driver modules for the current machine. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 571 | * |
| 572 | * Importantly, James Morris works for RedHat, and Fedora uses initrds for its |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 573 | * kernels. He sent me this (and tells me when I break it). |
| 574 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 575 | static unsigned long load_initrd(const char *name, unsigned long mem) |
| 576 | { |
| 577 | int ifd; |
| 578 | struct stat st; |
| 579 | unsigned long len; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 580 | |
| 581 | ifd = open_or_die(name, O_RDONLY); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 582 | /* fstat() is needed to get the file size. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 583 | if (fstat(ifd, &st) < 0) |
| 584 | err(1, "fstat() on initrd '%s'", name); |
| 585 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 586 | /* |
| 587 | * We map the initrd at the top of memory, but mmap wants it to be |
| 588 | * page-aligned, so we round the size up for that. |
| 589 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 590 | len = page_align(st.st_size); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 591 | map_at(ifd, from_guest_phys(mem - len), 0, st.st_size); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 592 | /* |
| 593 | * Once a file is mapped, you can close the file descriptor. It's a |
| 594 | * little odd, but quite useful. |
| 595 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 596 | close(ifd); |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 597 | verbose("mapped initrd %s size=%lu @ %p\n", name, len, (void*)mem-len); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 598 | |
| 599 | /* We return the initrd size. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 600 | return len; |
| 601 | } |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 602 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 603 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 604 | /* |
| 605 | * Simple routine to roll all the commandline arguments together with spaces |
| 606 | * between them. |
| 607 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 608 | static void concat(char *dst, char *args[]) |
| 609 | { |
| 610 | unsigned int i, len = 0; |
| 611 | |
| 612 | for (i = 0; args[i]; i++) { |
Paul Bolle | 1ef36fa | 2008-03-10 16:39:03 +0100 | [diff] [blame] | 613 | if (i) { |
| 614 | strcat(dst+len, " "); |
| 615 | len++; |
| 616 | } |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 617 | strcpy(dst+len, args[i]); |
Paul Bolle | 1ef36fa | 2008-03-10 16:39:03 +0100 | [diff] [blame] | 618 | len += strlen(args[i]); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 619 | } |
| 620 | /* In case it's empty. */ |
| 621 | dst[len] = '\0'; |
| 622 | } |
| 623 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 624 | /*L:185 |
| 625 | * This is where we actually tell the kernel to initialize the Guest. We |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 626 | * saw the arguments it expects when we looked at initialize() in lguest_user.c: |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 627 | * the base of Guest "physical" memory, the top physical page to allow and the |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 628 | * entry point for the Guest. |
| 629 | */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 630 | static void tell_kernel(unsigned long start) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 631 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 632 | unsigned long args[] = { LHREQ_INITIALIZE, |
| 633 | (unsigned long)guest_base, |
Rusty Russell | 7313d52 | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 634 | guest_limit / getpagesize(), start, |
Rusty Russell | 0a6bcc1 | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 635 | (guest_mmio+getpagesize()-1) / getpagesize() }; |
| 636 | verbose("Guest: %p - %p (%#lx, MMIO %#lx)\n", |
| 637 | guest_base, guest_base + guest_limit, |
| 638 | guest_limit, guest_mmio); |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 639 | lguest_fd = open_or_die("/dev/lguest", O_RDWR); |
| 640 | if (write(lguest_fd, args, sizeof(args)) < 0) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 641 | err(1, "Writing to /dev/lguest"); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 642 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 643 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 644 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 645 | /*L:200 |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 646 | * Device Handling. |
| 647 | * |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 648 | * When the Guest gives us a buffer, it sends an array of addresses and sizes. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 649 | * We need to make sure it's not trying to reach into the Launcher itself, so |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 650 | * we have a convenient routine which checks it and exits with an error message |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 651 | * if something funny is going on: |
| 652 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 653 | static void *_check_pointer(unsigned long addr, unsigned int size, |
| 654 | unsigned int line) |
| 655 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 656 | /* |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 657 | * Check if the requested address and size exceeds the allocated memory, |
| 658 | * or addr + size wraps around. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 659 | */ |
Philip Sanderson | 5230ff0 | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 660 | if ((addr + size) > guest_limit || (addr + size) < addr) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 661 | errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 662 | /* |
| 663 | * We return a pointer for the caller's convenience, now we know it's |
| 664 | * safe to use. |
| 665 | */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 666 | return from_guest_phys(addr); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 667 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 668 | /* A macro which transparently hands the line number to the real function. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 669 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) |
| 670 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 671 | /* |
| 672 | * Each buffer in the virtqueues is actually a chain of descriptors. This |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 673 | * function returns the next descriptor in the chain, or vq->vring.num if we're |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 674 | * at the end. |
| 675 | */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 676 | static unsigned next_desc(struct vring_desc *desc, |
| 677 | unsigned int i, unsigned int max) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 678 | { |
| 679 | unsigned int next; |
| 680 | |
| 681 | /* If this descriptor says it doesn't chain, we're done. */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 682 | if (!(desc[i].flags & VRING_DESC_F_NEXT)) |
| 683 | return max; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 684 | |
| 685 | /* Check they're not leading us off end of descriptors. */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 686 | next = desc[i].next; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 687 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
| 688 | wmb(); |
| 689 | |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 690 | if (next >= max) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 691 | errx(1, "Desc next is %u", next); |
| 692 | |
| 693 | return next; |
| 694 | } |
| 695 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 696 | /* |
| 697 | * This actually sends the interrupt for this virtqueue, if we've used a |
| 698 | * buffer. |
| 699 | */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 700 | static void trigger_irq(struct virtqueue *vq) |
| 701 | { |
| 702 | unsigned long buf[] = { LHREQ_IRQ, vq->config.irq }; |
| 703 | |
Rusty Russell | 95c517c | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 704 | /* Don't inform them if nothing used. */ |
| 705 | if (!vq->pending_used) |
| 706 | return; |
| 707 | vq->pending_used = 0; |
| 708 | |
Rusty Russell | ca60a42 | 2009-09-23 22:26:47 -0600 | [diff] [blame] | 709 | /* If they don't want an interrupt, don't send one... */ |
| 710 | if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) { |
Rusty Russell | 990c91f | 2011-05-30 11:14:12 -0600 | [diff] [blame] | 711 | return; |
Rusty Russell | ca60a42 | 2009-09-23 22:26:47 -0600 | [diff] [blame] | 712 | } |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 713 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 714 | /* For a PCI device, set isr to 1 (queue interrupt pending) */ |
| 715 | if (vq->dev->mmio) |
| 716 | vq->dev->mmio->isr = 0x1; |
| 717 | |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 718 | /* Send the Guest an interrupt tell them we used something up. */ |
| 719 | if (write(lguest_fd, buf, sizeof(buf)) != 0) |
| 720 | err(1, "Triggering irq %i", vq->config.irq); |
| 721 | } |
| 722 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 723 | /* |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 724 | * This looks in the virtqueue for the first available buffer, and converts |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 725 | * it to an iovec for convenient access. Since descriptors consist of some |
| 726 | * number of output then some number of input descriptors, it's actually two |
| 727 | * iovecs, but we pack them into one and note how many of each there were. |
| 728 | * |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 729 | * This function waits if necessary, and returns the descriptor number found. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 730 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 731 | static unsigned wait_for_vq_desc(struct virtqueue *vq, |
| 732 | struct iovec iov[], |
| 733 | unsigned int *out_num, unsigned int *in_num) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 734 | { |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 735 | unsigned int i, head, max; |
| 736 | struct vring_desc *desc; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 737 | u16 last_avail = lg_last_avail(vq); |
| 738 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 739 | /* There's nothing available? */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 740 | while (last_avail == vq->vring.avail->idx) { |
| 741 | u64 event; |
| 742 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 743 | /* |
| 744 | * Since we're about to sleep, now is a good time to tell the |
| 745 | * Guest about what we've used up to now. |
| 746 | */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 747 | trigger_irq(vq); |
| 748 | |
Rusty Russell | b60da13 | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 749 | /* OK, now we need to know about added descriptors. */ |
| 750 | vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY; |
| 751 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 752 | /* |
| 753 | * They could have slipped one in as we were doing that: make |
| 754 | * sure it's written, then check again. |
| 755 | */ |
Rusty Russell | b60da13 | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 756 | mb(); |
| 757 | if (last_avail != vq->vring.avail->idx) { |
| 758 | vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY; |
| 759 | break; |
| 760 | } |
| 761 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 762 | /* Nothing new? Wait for eventfd to tell us they refilled. */ |
| 763 | if (read(vq->eventfd, &event, sizeof(event)) != sizeof(event)) |
| 764 | errx(1, "Event read failed?"); |
Rusty Russell | b60da13 | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 765 | |
| 766 | /* We don't need to be notified again. */ |
| 767 | vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 768 | } |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 769 | |
| 770 | /* Check it isn't doing very strange things with descriptor numbers. */ |
Rusty Russell | b511179 | 2008-07-29 09:58:34 -0500 | [diff] [blame] | 771 | if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 772 | errx(1, "Guest moved used index from %u to %u", |
Rusty Russell | b511179 | 2008-07-29 09:58:34 -0500 | [diff] [blame] | 773 | last_avail, vq->vring.avail->idx); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 774 | |
Rusty Russell | 8fd9a63 | 2013-07-02 15:35:13 +0930 | [diff] [blame] | 775 | /* |
| 776 | * Make sure we read the descriptor number *after* we read the ring |
| 777 | * update; don't let the cpu or compiler change the order. |
| 778 | */ |
| 779 | rmb(); |
| 780 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 781 | /* |
| 782 | * Grab the next descriptor number they're advertising, and increment |
| 783 | * the index we've seen. |
| 784 | */ |
Rusty Russell | b511179 | 2008-07-29 09:58:34 -0500 | [diff] [blame] | 785 | head = vq->vring.avail->ring[last_avail % vq->vring.num]; |
| 786 | lg_last_avail(vq)++; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 787 | |
| 788 | /* If their number is silly, that's a fatal mistake. */ |
| 789 | if (head >= vq->vring.num) |
| 790 | errx(1, "Guest says index %u is available", head); |
| 791 | |
| 792 | /* When we start there are none of either input nor output. */ |
| 793 | *out_num = *in_num = 0; |
| 794 | |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 795 | max = vq->vring.num; |
| 796 | desc = vq->vring.desc; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 797 | i = head; |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 798 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 799 | /* |
Rusty Russell | 8fd9a63 | 2013-07-02 15:35:13 +0930 | [diff] [blame] | 800 | * We have to read the descriptor after we read the descriptor number, |
| 801 | * but there's a data dependency there so the CPU shouldn't reorder |
| 802 | * that: no rmb() required. |
| 803 | */ |
| 804 | |
| 805 | /* |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 806 | * If this is an indirect entry, then this buffer contains a descriptor |
| 807 | * table which we handle as if it's any normal descriptor chain. |
| 808 | */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 809 | if (desc[i].flags & VRING_DESC_F_INDIRECT) { |
| 810 | if (desc[i].len % sizeof(struct vring_desc)) |
| 811 | errx(1, "Invalid size for indirect buffer table"); |
| 812 | |
| 813 | max = desc[i].len / sizeof(struct vring_desc); |
| 814 | desc = check_pointer(desc[i].addr, desc[i].len); |
| 815 | i = 0; |
| 816 | } |
| 817 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 818 | do { |
| 819 | /* Grab the first descriptor, and check it's OK. */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 820 | iov[*out_num + *in_num].iov_len = desc[i].len; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 821 | iov[*out_num + *in_num].iov_base |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 822 | = check_pointer(desc[i].addr, desc[i].len); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 823 | /* If this is an input descriptor, increment that count. */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 824 | if (desc[i].flags & VRING_DESC_F_WRITE) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 825 | (*in_num)++; |
| 826 | else { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 827 | /* |
| 828 | * If it's an output descriptor, they're all supposed |
| 829 | * to come before any input descriptors. |
| 830 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 831 | if (*in_num) |
| 832 | errx(1, "Descriptor has out after in"); |
| 833 | (*out_num)++; |
| 834 | } |
| 835 | |
| 836 | /* If we've got too many, that implies a descriptor loop. */ |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 837 | if (*out_num + *in_num > max) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 838 | errx(1, "Looped descriptor"); |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 839 | } while ((i = next_desc(desc, i, max)) != max); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 840 | |
| 841 | return head; |
| 842 | } |
| 843 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 844 | /* |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 845 | * After we've used one of their buffers, we tell the Guest about it. Sometime |
| 846 | * later we'll want to send them an interrupt using trigger_irq(); note that |
| 847 | * wait_for_vq_desc() does that for us if it has to wait. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 848 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 849 | static void add_used(struct virtqueue *vq, unsigned int head, int len) |
| 850 | { |
| 851 | struct vring_used_elem *used; |
| 852 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 853 | /* |
| 854 | * The virtqueue contains a ring of used buffers. Get a pointer to the |
| 855 | * next entry in that used ring. |
| 856 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 857 | used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num]; |
| 858 | used->id = head; |
| 859 | used->len = len; |
| 860 | /* Make sure buffer is written before we update index. */ |
| 861 | wmb(); |
| 862 | vq->vring.used->idx++; |
Rusty Russell | 95c517c | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 863 | vq->pending_used++; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 864 | } |
| 865 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 866 | /* And here's the combo meal deal. Supersize me! */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 867 | static void add_used_and_trigger(struct virtqueue *vq, unsigned head, int len) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 868 | { |
| 869 | add_used(vq, head, len); |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 870 | trigger_irq(vq); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 871 | } |
| 872 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 873 | /* |
| 874 | * The Console |
| 875 | * |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 876 | * We associate some data with the console for our exit hack. |
| 877 | */ |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 878 | struct console_abort { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 879 | /* How many times have they hit ^C? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 880 | int count; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 881 | /* When did they start? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 882 | struct timeval start; |
| 883 | }; |
| 884 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 885 | /* This is the routine which handles console input (ie. stdin). */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 886 | static void console_input(struct virtqueue *vq) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 887 | { |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 888 | int len; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 889 | unsigned int head, in_num, out_num; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 890 | struct console_abort *abort = vq->dev->priv; |
| 891 | struct iovec iov[vq->vring.num]; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 892 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 893 | /* Make sure there's a descriptor available. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 894 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
Rusty Russell | 56ae43d | 2007-10-22 11:24:23 +1000 | [diff] [blame] | 895 | if (out_num) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 896 | errx(1, "Output buffers in console in queue?"); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 897 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 898 | /* Read into it. This is where we usually wait. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 899 | len = readv(STDIN_FILENO, iov, in_num); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 900 | if (len <= 0) { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 901 | /* Ran out of input? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 902 | warnx("Failed to get console input, ignoring console."); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 903 | /* |
| 904 | * For simplicity, dying threads kill the whole Launcher. So |
| 905 | * just nap here. |
| 906 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 907 | for (;;) |
| 908 | pause(); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 911 | /* Tell the Guest we used a buffer. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 912 | add_used_and_trigger(vq, head, len); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 913 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 914 | /* |
| 915 | * Three ^C within one second? Exit. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 916 | * |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 917 | * This is such a hack, but works surprisingly well. Each ^C has to |
| 918 | * be in a buffer by itself, so they can't be too fast. But we check |
| 919 | * that we get three within about a second, so they can't be too |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 920 | * slow. |
| 921 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 922 | if (len != 1 || ((char *)iov[0].iov_base)[0] != 3) { |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 923 | abort->count = 0; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 924 | return; |
| 925 | } |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 926 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 927 | abort->count++; |
| 928 | if (abort->count == 1) |
| 929 | gettimeofday(&abort->start, NULL); |
| 930 | else if (abort->count == 3) { |
| 931 | struct timeval now; |
| 932 | gettimeofday(&now, NULL); |
| 933 | /* Kill all Launcher processes with SIGINT, like normal ^C */ |
| 934 | if (now.tv_sec <= abort->start.tv_sec+1) |
| 935 | kill(0, SIGINT); |
| 936 | abort->count = 0; |
| 937 | } |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 940 | /* This is the routine which handles console output (ie. stdout). */ |
| 941 | static void console_output(struct virtqueue *vq) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 942 | { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 943 | unsigned int head, out, in; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 944 | struct iovec iov[vq->vring.num]; |
| 945 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 946 | /* We usually wait in here, for the Guest to give us something. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 947 | head = wait_for_vq_desc(vq, iov, &out, &in); |
| 948 | if (in) |
| 949 | errx(1, "Input buffers in console output queue?"); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 950 | |
| 951 | /* writev can return a partial write, so we loop here. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 952 | while (!iov_empty(iov, out)) { |
| 953 | int len = writev(STDOUT_FILENO, iov, out); |
Sakari Ailus | e0377e2 | 2011-06-26 19:36:46 +0300 | [diff] [blame] | 954 | if (len <= 0) { |
| 955 | warn("Write to stdout gave %i (%d)", len, errno); |
| 956 | break; |
| 957 | } |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 958 | iov_consume(iov, out, NULL, len); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 959 | } |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 960 | |
| 961 | /* |
| 962 | * We're finished with that buffer: if we're going to sleep, |
| 963 | * wait_for_vq_desc() will prod the Guest with an interrupt. |
| 964 | */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 965 | add_used(vq, head, 0); |
Rusty Russell | a161883 | 2008-07-29 09:58:35 -0500 | [diff] [blame] | 966 | } |
| 967 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 968 | /* |
| 969 | * The Network |
| 970 | * |
| 971 | * Handling output for network is also simple: we get all the output buffers |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 972 | * and write them to /dev/net/tun. |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 973 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 974 | struct net_info { |
| 975 | int tunfd; |
| 976 | }; |
| 977 | |
| 978 | static void net_output(struct virtqueue *vq) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 979 | { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 980 | struct net_info *net_info = vq->dev->priv; |
| 981 | unsigned int head, out, in; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 982 | struct iovec iov[vq->vring.num]; |
| 983 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 984 | /* We usually wait in here for the Guest to give us a packet. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 985 | head = wait_for_vq_desc(vq, iov, &out, &in); |
| 986 | if (in) |
| 987 | errx(1, "Input buffers in net output queue?"); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 988 | /* |
| 989 | * Send the whole thing through to /dev/net/tun. It expects the exact |
| 990 | * same format: what a coincidence! |
| 991 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 992 | if (writev(net_info->tunfd, iov, out) < 0) |
Sakari Ailus | e0377e2 | 2011-06-26 19:36:46 +0300 | [diff] [blame] | 993 | warnx("Write to tun failed (%d)?", errno); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Done with that one; wait_for_vq_desc() will send the interrupt if |
| 997 | * all packets are processed. |
| 998 | */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 999 | add_used(vq, head, 0); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1002 | /* |
| 1003 | * Handling network input is a bit trickier, because I've tried to optimize it. |
| 1004 | * |
| 1005 | * First we have a helper routine which tells is if from this file descriptor |
| 1006 | * (ie. the /dev/net/tun device) will block: |
| 1007 | */ |
Rusty Russell | 4a8962e | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 1008 | static bool will_block(int fd) |
| 1009 | { |
| 1010 | fd_set fdset; |
| 1011 | struct timeval zero = { 0, 0 }; |
| 1012 | FD_ZERO(&fdset); |
| 1013 | FD_SET(fd, &fdset); |
| 1014 | return select(fd+1, &fdset, NULL, NULL, &zero) != 1; |
| 1015 | } |
| 1016 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1017 | /* |
| 1018 | * This handles packets coming in from the tun device to our Guest. Like all |
| 1019 | * service routines, it gets called again as soon as it returns, so you don't |
| 1020 | * see a while(1) loop here. |
| 1021 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1022 | static void net_input(struct virtqueue *vq) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1023 | { |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1024 | int len; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1025 | unsigned int head, out, in; |
| 1026 | struct iovec iov[vq->vring.num]; |
| 1027 | struct net_info *net_info = vq->dev->priv; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1028 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1029 | /* |
| 1030 | * Get a descriptor to write an incoming packet into. This will also |
| 1031 | * send an interrupt if they're out of descriptors. |
| 1032 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1033 | head = wait_for_vq_desc(vq, iov, &out, &in); |
| 1034 | if (out) |
| 1035 | errx(1, "Output buffers in net input queue?"); |
Rusty Russell | 4a8962e | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 1036 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1037 | /* |
| 1038 | * If it looks like we'll block reading from the tun device, send them |
| 1039 | * an interrupt. |
| 1040 | */ |
Rusty Russell | 4a8962e | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 1041 | if (vq->pending_used && will_block(net_info->tunfd)) |
| 1042 | trigger_irq(vq); |
| 1043 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1044 | /* |
| 1045 | * Read in the packet. This is where we normally wait (when there's no |
| 1046 | * incoming network traffic). |
| 1047 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1048 | len = readv(net_info->tunfd, iov, in); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1049 | if (len <= 0) |
Sakari Ailus | e0377e2 | 2011-06-26 19:36:46 +0300 | [diff] [blame] | 1050 | warn("Failed to read from tun (%d).", errno); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1051 | |
| 1052 | /* |
| 1053 | * Mark that packet buffer as used, but don't interrupt here. We want |
| 1054 | * to wait until we've done as much work as we can. |
| 1055 | */ |
Rusty Russell | 4a8962e | 2009-06-12 22:27:12 -0600 | [diff] [blame] | 1056 | add_used(vq, head, len); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1057 | } |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1058 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1059 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1060 | /* This is the helper to create threads: run the service routine in a loop. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1061 | static int do_thread(void *_vq) |
Rusty Russell | 56ae43d | 2007-10-22 11:24:23 +1000 | [diff] [blame] | 1062 | { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1063 | struct virtqueue *vq = _vq; |
| 1064 | |
| 1065 | for (;;) |
| 1066 | vq->service(vq); |
| 1067 | return 0; |
Rusty Russell | 56ae43d | 2007-10-22 11:24:23 +1000 | [diff] [blame] | 1068 | } |
| 1069 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1070 | /* |
| 1071 | * When a child dies, we kill our entire process group with SIGTERM. This |
| 1072 | * also has the side effect that the shell restores the console for us! |
| 1073 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1074 | static void kill_launcher(int signal) |
Rusty Russell | 5dae785 | 2008-07-29 09:58:35 -0500 | [diff] [blame] | 1075 | { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1076 | kill(0, SIGTERM); |
| 1077 | } |
| 1078 | |
| 1079 | static void reset_device(struct device *dev) |
| 1080 | { |
| 1081 | struct virtqueue *vq; |
| 1082 | |
| 1083 | verbose("Resetting device %s\n", dev->name); |
| 1084 | |
| 1085 | /* Clear any features they've acked. */ |
| 1086 | memset(get_feature_bits(dev) + dev->feature_len, 0, dev->feature_len); |
| 1087 | |
| 1088 | /* We're going to be explicitly killing threads, so ignore them. */ |
| 1089 | signal(SIGCHLD, SIG_IGN); |
| 1090 | |
| 1091 | /* Zero out the virtqueues, get rid of their threads */ |
| 1092 | for (vq = dev->vq; vq; vq = vq->next) { |
| 1093 | if (vq->thread != (pid_t)-1) { |
| 1094 | kill(vq->thread, SIGTERM); |
| 1095 | waitpid(vq->thread, NULL, 0); |
| 1096 | vq->thread = (pid_t)-1; |
| 1097 | } |
| 1098 | memset(vq->vring.desc, 0, |
| 1099 | vring_size(vq->config.num, LGUEST_VRING_ALIGN)); |
| 1100 | lg_last_avail(vq) = 0; |
| 1101 | } |
| 1102 | dev->running = false; |
| 1103 | |
| 1104 | /* Now we care if threads die. */ |
| 1105 | signal(SIGCHLD, (void *)kill_launcher); |
| 1106 | } |
| 1107 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1108 | /*L:216 |
| 1109 | * This actually creates the thread which services the virtqueue for a device. |
| 1110 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1111 | static void create_thread(struct virtqueue *vq) |
| 1112 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1113 | /* |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1114 | * Create stack for thread. Since the stack grows upwards, we point |
| 1115 | * the stack pointer to the end of this region. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1116 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1117 | char *stack = malloc(32768); |
| 1118 | unsigned long args[] = { LHREQ_EVENTFD, |
| 1119 | vq->config.pfn*getpagesize(), 0 }; |
| 1120 | |
| 1121 | /* Create a zero-initialized eventfd. */ |
| 1122 | vq->eventfd = eventfd(0, 0); |
| 1123 | if (vq->eventfd < 0) |
| 1124 | err(1, "Creating eventfd"); |
| 1125 | args[2] = vq->eventfd; |
| 1126 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1127 | /* |
| 1128 | * Attach an eventfd to this virtqueue: it will go off when the Guest |
| 1129 | * does an LHCALL_NOTIFY for this vq. |
| 1130 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1131 | if (write(lguest_fd, &args, sizeof(args)) != 0) |
| 1132 | err(1, "Attaching eventfd"); |
| 1133 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1134 | /* |
| 1135 | * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so |
| 1136 | * we get a signal if it dies. |
| 1137 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1138 | vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq); |
| 1139 | if (vq->thread == (pid_t)-1) |
| 1140 | err(1, "Creating clone"); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1141 | |
| 1142 | /* We close our local copy now the child has it. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1143 | close(vq->eventfd); |
| 1144 | } |
| 1145 | |
| 1146 | static void start_device(struct device *dev) |
| 1147 | { |
| 1148 | unsigned int i; |
| 1149 | struct virtqueue *vq; |
| 1150 | |
| 1151 | verbose("Device %s OK: offered", dev->name); |
| 1152 | for (i = 0; i < dev->feature_len; i++) |
| 1153 | verbose(" %02x", get_feature_bits(dev)[i]); |
| 1154 | verbose(", accepted"); |
| 1155 | for (i = 0; i < dev->feature_len; i++) |
| 1156 | verbose(" %02x", get_feature_bits(dev) |
| 1157 | [dev->feature_len+i]); |
| 1158 | |
| 1159 | for (vq = dev->vq; vq; vq = vq->next) { |
| 1160 | if (vq->service) |
| 1161 | create_thread(vq); |
| 1162 | } |
| 1163 | dev->running = true; |
| 1164 | } |
| 1165 | |
| 1166 | static void cleanup_devices(void) |
| 1167 | { |
| 1168 | struct device *dev; |
| 1169 | |
| 1170 | for (dev = devices.dev; dev; dev = dev->next) |
| 1171 | reset_device(dev); |
| 1172 | |
| 1173 | /* If we saved off the original terminal settings, restore them now. */ |
| 1174 | if (orig_term.c_lflag & (ISIG|ICANON|ECHO)) |
| 1175 | tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); |
Rusty Russell | 5dae785 | 2008-07-29 09:58:35 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
Rusty Russell | a007a75 | 2008-05-02 21:50:53 -0500 | [diff] [blame] | 1178 | /* When the Guest tells us they updated the status field, we handle it. */ |
| 1179 | static void update_device_status(struct device *dev) |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 1180 | { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1181 | /* A zero status is a reset, otherwise it's a set of flags. */ |
| 1182 | if (dev->desc->status == 0) |
| 1183 | reset_device(dev); |
| 1184 | else if (dev->desc->status & VIRTIO_CONFIG_S_FAILED) { |
Rusty Russell | a007a75 | 2008-05-02 21:50:53 -0500 | [diff] [blame] | 1185 | warnx("Device %s configuration FAILED", dev->name); |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1186 | if (dev->running) |
| 1187 | reset_device(dev); |
Rusty Russell | 3c3ed48 | 2011-07-22 14:39:49 +0930 | [diff] [blame] | 1188 | } else { |
| 1189 | if (dev->running) |
| 1190 | err(1, "Device %s features finalized twice", dev->name); |
| 1191 | start_device(dev); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1195 | /*L:215 |
| 1196 | * This is the generic routine we call when the Guest uses LHCALL_NOTIFY. In |
| 1197 | * particular, it's used to notify us of device status changes during boot. |
| 1198 | */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 1199 | static void handle_output(unsigned long addr) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1200 | { |
| 1201 | struct device *i; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1202 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1203 | /* Check each device. */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 1204 | for (i = devices.dev; i; i = i->next) { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1205 | struct virtqueue *vq; |
| 1206 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1207 | /* |
| 1208 | * Notifications to device descriptors mean they updated the |
| 1209 | * device status. |
| 1210 | */ |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 1211 | if (from_guest_phys(addr) == i->desc) { |
Rusty Russell | a007a75 | 2008-05-02 21:50:53 -0500 | [diff] [blame] | 1212 | update_device_status(i); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 1213 | return; |
| 1214 | } |
| 1215 | |
Rusty Russell | 3c3ed48 | 2011-07-22 14:39:49 +0930 | [diff] [blame] | 1216 | /* Devices should not be used before features are finalized. */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 1217 | for (vq = i->vq; vq; vq = vq->next) { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 1218 | if (addr != vq->config.pfn*getpagesize()) |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 1219 | continue; |
Rusty Russell | 3c3ed48 | 2011-07-22 14:39:49 +0930 | [diff] [blame] | 1220 | errx(1, "Notification on %s before setup!", i->name); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1223 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1224 | /* |
| 1225 | * Early console write is done using notify on a nul-terminated string |
| 1226 | * in Guest memory. It's also great for hacking debugging messages |
| 1227 | * into a Guest. |
| 1228 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 1229 | if (addr >= guest_limit) |
| 1230 | errx(1, "Bad NOTIFY %#lx", addr); |
| 1231 | |
| 1232 | write(STDOUT_FILENO, from_guest_phys(addr), |
| 1233 | strnlen(from_guest_phys(addr), guest_limit - addr)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1236 | /*L:217 |
| 1237 | * We do PCI. This is mainly done to let us test the kernel virtio PCI |
| 1238 | * code. |
| 1239 | */ |
| 1240 | |
| 1241 | /* The IO ports used to read the PCI config space. */ |
| 1242 | #define PCI_CONFIG_ADDR 0xCF8 |
| 1243 | #define PCI_CONFIG_DATA 0xCFC |
| 1244 | |
| 1245 | /* |
| 1246 | * Not really portable, but does help readability: this is what the Guest |
| 1247 | * writes to the PCI_CONFIG_ADDR IO port. |
| 1248 | */ |
| 1249 | union pci_config_addr { |
| 1250 | struct { |
| 1251 | unsigned mbz: 2; |
| 1252 | unsigned offset: 6; |
| 1253 | unsigned funcnum: 3; |
| 1254 | unsigned devnum: 5; |
| 1255 | unsigned busnum: 8; |
| 1256 | unsigned reserved: 7; |
| 1257 | unsigned enabled : 1; |
| 1258 | } bits; |
| 1259 | u32 val; |
| 1260 | }; |
| 1261 | |
| 1262 | /* |
| 1263 | * We cache what they wrote to the address port, so we know what they're |
| 1264 | * talking about when they access the data port. |
| 1265 | */ |
| 1266 | static union pci_config_addr pci_config_addr; |
| 1267 | |
| 1268 | static struct device *find_pci_device(unsigned int index) |
| 1269 | { |
| 1270 | return devices.pci[index]; |
| 1271 | } |
| 1272 | |
| 1273 | /* PCI can do 1, 2 and 4 byte reads; we handle that here. */ |
| 1274 | static void ioread(u16 off, u32 v, u32 mask, u32 *val) |
| 1275 | { |
| 1276 | assert(off < 4); |
| 1277 | assert(mask == 0xFF || mask == 0xFFFF || mask == 0xFFFFFFFF); |
| 1278 | *val = (v >> (off * 8)) & mask; |
| 1279 | } |
| 1280 | |
| 1281 | /* PCI can do 1, 2 and 4 byte writes; we handle that here. */ |
| 1282 | static void iowrite(u16 off, u32 v, u32 mask, u32 *dst) |
| 1283 | { |
| 1284 | assert(off < 4); |
| 1285 | assert(mask == 0xFF || mask == 0xFFFF || mask == 0xFFFFFFFF); |
| 1286 | *dst &= ~(mask << (off * 8)); |
| 1287 | *dst |= (v & mask) << (off * 8); |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * Where PCI_CONFIG_DATA accesses depends on the previous write to |
| 1292 | * PCI_CONFIG_ADDR. |
| 1293 | */ |
| 1294 | static struct device *dev_and_reg(u32 *reg) |
| 1295 | { |
| 1296 | if (!pci_config_addr.bits.enabled) |
| 1297 | return NULL; |
| 1298 | |
| 1299 | if (pci_config_addr.bits.funcnum != 0) |
| 1300 | return NULL; |
| 1301 | |
| 1302 | if (pci_config_addr.bits.busnum != 0) |
| 1303 | return NULL; |
| 1304 | |
| 1305 | if (pci_config_addr.bits.offset * 4 >= sizeof(struct pci_config)) |
| 1306 | return NULL; |
| 1307 | |
| 1308 | *reg = pci_config_addr.bits.offset; |
| 1309 | return find_pci_device(pci_config_addr.bits.devnum); |
| 1310 | } |
| 1311 | |
| 1312 | /* Is this accessing the PCI config address port?. */ |
| 1313 | static bool is_pci_addr_port(u16 port) |
| 1314 | { |
| 1315 | return port >= PCI_CONFIG_ADDR && port < PCI_CONFIG_ADDR + 4; |
| 1316 | } |
| 1317 | |
| 1318 | static bool pci_addr_iowrite(u16 port, u32 mask, u32 val) |
| 1319 | { |
| 1320 | iowrite(port - PCI_CONFIG_ADDR, val, mask, |
| 1321 | &pci_config_addr.val); |
| 1322 | verbose("PCI%s: %#x/%x: bus %u dev %u func %u reg %u\n", |
| 1323 | pci_config_addr.bits.enabled ? "" : " DISABLED", |
| 1324 | val, mask, |
| 1325 | pci_config_addr.bits.busnum, |
| 1326 | pci_config_addr.bits.devnum, |
| 1327 | pci_config_addr.bits.funcnum, |
| 1328 | pci_config_addr.bits.offset); |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | static void pci_addr_ioread(u16 port, u32 mask, u32 *val) |
| 1333 | { |
| 1334 | ioread(port - PCI_CONFIG_ADDR, pci_config_addr.val, mask, val); |
| 1335 | } |
| 1336 | |
| 1337 | /* Is this accessing the PCI config data port?. */ |
| 1338 | static bool is_pci_data_port(u16 port) |
| 1339 | { |
| 1340 | return port >= PCI_CONFIG_DATA && port < PCI_CONFIG_DATA + 4; |
| 1341 | } |
| 1342 | |
| 1343 | static bool pci_data_iowrite(u16 port, u32 mask, u32 val) |
| 1344 | { |
| 1345 | u32 reg, portoff; |
| 1346 | struct device *d = dev_and_reg(®); |
| 1347 | |
| 1348 | /* Complain if they don't belong to a device. */ |
| 1349 | if (!d) |
| 1350 | return false; |
| 1351 | |
| 1352 | /* They can do 1 byte writes, etc. */ |
| 1353 | portoff = port - PCI_CONFIG_DATA; |
| 1354 | |
| 1355 | /* |
| 1356 | * PCI uses a weird way to determine the BAR size: the OS |
| 1357 | * writes all 1's, and sees which ones stick. |
| 1358 | */ |
| 1359 | if (&d->config_words[reg] == &d->config.bar[0]) { |
| 1360 | int i; |
| 1361 | |
| 1362 | iowrite(portoff, val, mask, &d->config.bar[0]); |
| 1363 | for (i = 0; (1 << i) < d->mmio_size; i++) |
| 1364 | d->config.bar[0] &= ~(1 << i); |
| 1365 | return true; |
| 1366 | } else if ((&d->config_words[reg] > &d->config.bar[0] |
| 1367 | && &d->config_words[reg] <= &d->config.bar[6]) |
| 1368 | || &d->config_words[reg] == &d->config.expansion_rom_addr) { |
| 1369 | /* Allow writing to any other BAR, or expansion ROM */ |
| 1370 | iowrite(portoff, val, mask, &d->config_words[reg]); |
| 1371 | return true; |
| 1372 | /* We let them overide latency timer and cacheline size */ |
| 1373 | } else if (&d->config_words[reg] == (void *)&d->config.cacheline_size) { |
| 1374 | /* Only let them change the first two fields. */ |
| 1375 | if (mask == 0xFFFFFFFF) |
| 1376 | mask = 0xFFFF; |
| 1377 | iowrite(portoff, val, mask, &d->config_words[reg]); |
| 1378 | return true; |
| 1379 | } else if (&d->config_words[reg] == (void *)&d->config.command |
| 1380 | && mask == 0xFFFF) { |
| 1381 | /* Ignore command writes. */ |
| 1382 | return true; |
| 1383 | } |
| 1384 | |
| 1385 | /* Complain about other writes. */ |
| 1386 | return false; |
| 1387 | } |
| 1388 | |
| 1389 | static void pci_data_ioread(u16 port, u32 mask, u32 *val) |
| 1390 | { |
| 1391 | u32 reg; |
| 1392 | struct device *d = dev_and_reg(®); |
| 1393 | |
| 1394 | if (!d) |
| 1395 | return; |
| 1396 | ioread(port - PCI_CONFIG_DATA, d->config_words[reg], mask, val); |
| 1397 | } |
| 1398 | |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1399 | /*L:216 |
| 1400 | * This is where we emulate a handful of Guest instructions. It's ugly |
| 1401 | * and we used to do it in the kernel but it grew over time. |
| 1402 | */ |
| 1403 | |
| 1404 | /* |
| 1405 | * We use the ptrace syscall's pt_regs struct to talk about registers |
| 1406 | * to lguest: these macros convert the names to the offsets. |
| 1407 | */ |
| 1408 | #define getreg(name) getreg_off(offsetof(struct user_regs_struct, name)) |
| 1409 | #define setreg(name, val) \ |
| 1410 | setreg_off(offsetof(struct user_regs_struct, name), (val)) |
| 1411 | |
| 1412 | static u32 getreg_off(size_t offset) |
| 1413 | { |
| 1414 | u32 r; |
| 1415 | unsigned long args[] = { LHREQ_GETREG, offset }; |
| 1416 | |
| 1417 | if (pwrite(lguest_fd, args, sizeof(args), cpu_id) < 0) |
| 1418 | err(1, "Getting register %u", offset); |
| 1419 | if (pread(lguest_fd, &r, sizeof(r), cpu_id) != sizeof(r)) |
| 1420 | err(1, "Reading register %u", offset); |
| 1421 | |
| 1422 | return r; |
| 1423 | } |
| 1424 | |
| 1425 | static void setreg_off(size_t offset, u32 val) |
| 1426 | { |
| 1427 | unsigned long args[] = { LHREQ_SETREG, offset, val }; |
| 1428 | |
| 1429 | if (pwrite(lguest_fd, args, sizeof(args), cpu_id) < 0) |
| 1430 | err(1, "Setting register %u", offset); |
| 1431 | } |
| 1432 | |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1433 | /* Get register by instruction encoding */ |
| 1434 | static u32 getreg_num(unsigned regnum, u32 mask) |
| 1435 | { |
| 1436 | /* 8 bit ops use regnums 4-7 for high parts of word */ |
| 1437 | if (mask == 0xFF && (regnum & 0x4)) |
| 1438 | return getreg_num(regnum & 0x3, 0xFFFF) >> 8; |
| 1439 | |
| 1440 | switch (regnum) { |
| 1441 | case 0: return getreg(eax) & mask; |
| 1442 | case 1: return getreg(ecx) & mask; |
| 1443 | case 2: return getreg(edx) & mask; |
| 1444 | case 3: return getreg(ebx) & mask; |
| 1445 | case 4: return getreg(esp) & mask; |
| 1446 | case 5: return getreg(ebp) & mask; |
| 1447 | case 6: return getreg(esi) & mask; |
| 1448 | case 7: return getreg(edi) & mask; |
| 1449 | } |
| 1450 | abort(); |
| 1451 | } |
| 1452 | |
| 1453 | /* Set register by instruction encoding */ |
| 1454 | static void setreg_num(unsigned regnum, u32 val, u32 mask) |
| 1455 | { |
| 1456 | /* Don't try to set bits out of range */ |
| 1457 | assert(~(val & ~mask)); |
| 1458 | |
| 1459 | /* 8 bit ops use regnums 4-7 for high parts of word */ |
| 1460 | if (mask == 0xFF && (regnum & 0x4)) { |
| 1461 | /* Construct the 16 bits we want. */ |
| 1462 | val = (val << 8) | getreg_num(regnum & 0x3, 0xFF); |
| 1463 | setreg_num(regnum & 0x3, val, 0xFFFF); |
| 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | switch (regnum) { |
| 1468 | case 0: setreg(eax, val | (getreg(eax) & ~mask)); return; |
| 1469 | case 1: setreg(ecx, val | (getreg(ecx) & ~mask)); return; |
| 1470 | case 2: setreg(edx, val | (getreg(edx) & ~mask)); return; |
| 1471 | case 3: setreg(ebx, val | (getreg(ebx) & ~mask)); return; |
| 1472 | case 4: setreg(esp, val | (getreg(esp) & ~mask)); return; |
| 1473 | case 5: setreg(ebp, val | (getreg(ebp) & ~mask)); return; |
| 1474 | case 6: setreg(esi, val | (getreg(esi) & ~mask)); return; |
| 1475 | case 7: setreg(edi, val | (getreg(edi) & ~mask)); return; |
| 1476 | } |
| 1477 | abort(); |
| 1478 | } |
| 1479 | |
| 1480 | /* Get bytes of displacement appended to instruction, from r/m encoding */ |
| 1481 | static u32 insn_displacement_len(u8 mod_reg_rm) |
| 1482 | { |
| 1483 | /* Switch on the mod bits */ |
| 1484 | switch (mod_reg_rm >> 6) { |
| 1485 | case 0: |
| 1486 | /* If mod == 0, and r/m == 101, 16-bit displacement follows */ |
| 1487 | if ((mod_reg_rm & 0x7) == 0x5) |
| 1488 | return 2; |
| 1489 | /* Normally, mod == 0 means no literal displacement */ |
| 1490 | return 0; |
| 1491 | case 1: |
| 1492 | /* One byte displacement */ |
| 1493 | return 1; |
| 1494 | case 2: |
| 1495 | /* Four byte displacement */ |
| 1496 | return 4; |
| 1497 | case 3: |
| 1498 | /* Register mode */ |
| 1499 | return 0; |
| 1500 | } |
| 1501 | abort(); |
| 1502 | } |
| 1503 | |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1504 | static void emulate_insn(const u8 insn[]) |
| 1505 | { |
| 1506 | unsigned long args[] = { LHREQ_TRAP, 13 }; |
| 1507 | unsigned int insnlen = 0, in = 0, small_operand = 0, byte_access; |
| 1508 | unsigned int eax, port, mask; |
| 1509 | /* |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1510 | * Default is to return all-ones on IO port reads, which traditionally |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1511 | * means "there's nothing there". |
| 1512 | */ |
| 1513 | u32 val = 0xFFFFFFFF; |
| 1514 | |
| 1515 | /* |
| 1516 | * This must be the Guest kernel trying to do something, not userspace! |
| 1517 | * The bottom two bits of the CS segment register are the privilege |
| 1518 | * level. |
| 1519 | */ |
| 1520 | if ((getreg(xcs) & 3) != 0x1) |
| 1521 | goto no_emulate; |
| 1522 | |
| 1523 | /* Decoding x86 instructions is icky. */ |
| 1524 | |
| 1525 | /* |
| 1526 | * Around 2.6.33, the kernel started using an emulation for the |
| 1527 | * cmpxchg8b instruction in early boot on many configurations. This |
| 1528 | * code isn't paravirtualized, and it tries to disable interrupts. |
| 1529 | * Ignore it, which will Mostly Work. |
| 1530 | */ |
| 1531 | if (insn[insnlen] == 0xfa) { |
| 1532 | /* "cli", or Clear Interrupt Enable instruction. Skip it. */ |
| 1533 | insnlen = 1; |
| 1534 | goto skip_insn; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * 0x66 is an "operand prefix". It means a 16, not 32 bit in/out. |
| 1539 | */ |
| 1540 | if (insn[insnlen] == 0x66) { |
| 1541 | small_operand = 1; |
| 1542 | /* The instruction is 1 byte so far, read the next byte. */ |
| 1543 | insnlen = 1; |
| 1544 | } |
| 1545 | |
| 1546 | /* If the lower bit isn't set, it's a single byte access */ |
| 1547 | byte_access = !(insn[insnlen] & 1); |
| 1548 | |
| 1549 | /* |
| 1550 | * Now we can ignore the lower bit and decode the 4 opcodes |
| 1551 | * we need to emulate. |
| 1552 | */ |
| 1553 | switch (insn[insnlen] & 0xFE) { |
| 1554 | case 0xE4: /* in <next byte>,%al */ |
| 1555 | port = insn[insnlen+1]; |
| 1556 | insnlen += 2; |
| 1557 | in = 1; |
| 1558 | break; |
| 1559 | case 0xEC: /* in (%dx),%al */ |
| 1560 | port = getreg(edx) & 0xFFFF; |
| 1561 | insnlen += 1; |
| 1562 | in = 1; |
| 1563 | break; |
| 1564 | case 0xE6: /* out %al,<next byte> */ |
| 1565 | port = insn[insnlen+1]; |
| 1566 | insnlen += 2; |
| 1567 | break; |
| 1568 | case 0xEE: /* out %al,(%dx) */ |
| 1569 | port = getreg(edx) & 0xFFFF; |
| 1570 | insnlen += 1; |
| 1571 | break; |
| 1572 | default: |
| 1573 | /* OK, we don't know what this is, can't emulate. */ |
| 1574 | goto no_emulate; |
| 1575 | } |
| 1576 | |
| 1577 | /* Set a mask of the 1, 2 or 4 bytes, depending on size of IO */ |
| 1578 | if (byte_access) |
| 1579 | mask = 0xFF; |
| 1580 | else if (small_operand) |
| 1581 | mask = 0xFFFF; |
| 1582 | else |
| 1583 | mask = 0xFFFFFFFF; |
| 1584 | |
| 1585 | /* |
| 1586 | * If it was an "IN" instruction, they expect the result to be read |
| 1587 | * into %eax, so we change %eax. |
| 1588 | */ |
| 1589 | eax = getreg(eax); |
| 1590 | |
| 1591 | if (in) { |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1592 | /* This is the PS/2 keyboard status; 1 means ready for output */ |
| 1593 | if (port == 0x64) |
| 1594 | val = 1; |
| 1595 | else if (is_pci_addr_port(port)) |
| 1596 | pci_addr_ioread(port, mask, &val); |
| 1597 | else if (is_pci_data_port(port)) |
| 1598 | pci_data_ioread(port, mask, &val); |
| 1599 | |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1600 | /* Clear the bits we're about to read */ |
| 1601 | eax &= ~mask; |
| 1602 | /* Copy bits in from val. */ |
| 1603 | eax |= val & mask; |
| 1604 | /* Now update the register. */ |
| 1605 | setreg(eax, eax); |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1606 | } else { |
| 1607 | if (is_pci_addr_port(port)) { |
| 1608 | if (!pci_addr_iowrite(port, mask, eax)) |
| 1609 | goto bad_io; |
| 1610 | } else if (is_pci_data_port(port)) { |
| 1611 | if (!pci_data_iowrite(port, mask, eax)) |
| 1612 | goto bad_io; |
| 1613 | } |
| 1614 | /* There are many other ports, eg. CMOS clock, serial |
| 1615 | * and parallel ports, so we ignore them all. */ |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | verbose("IO %s of %x to %u: %#08x\n", |
| 1619 | in ? "IN" : "OUT", mask, port, eax); |
| 1620 | skip_insn: |
| 1621 | /* Finally, we've "done" the instruction, so move past it. */ |
| 1622 | setreg(eip, getreg(eip) + insnlen); |
| 1623 | return; |
| 1624 | |
Rusty Russell | d7fbf6e | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1625 | bad_io: |
| 1626 | warnx("Attempt to %s port %u (%#x mask)", |
| 1627 | in ? "read from" : "write to", port, mask); |
| 1628 | |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1629 | no_emulate: |
| 1630 | /* Inject trap into Guest. */ |
| 1631 | if (write(lguest_fd, args, sizeof(args)) < 0) |
| 1632 | err(1, "Reinjecting trap 13 for fault at %#x", getreg(eip)); |
| 1633 | } |
| 1634 | |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1635 | static struct device *find_mmio_region(unsigned long paddr, u32 *off) |
| 1636 | { |
| 1637 | unsigned int i; |
| 1638 | |
| 1639 | for (i = 1; i < MAX_PCI_DEVICES; i++) { |
| 1640 | struct device *d = devices.pci[i]; |
| 1641 | |
| 1642 | if (!d) |
| 1643 | continue; |
| 1644 | if (paddr < d->mmio_addr) |
| 1645 | continue; |
| 1646 | if (paddr >= d->mmio_addr + d->mmio_size) |
| 1647 | continue; |
| 1648 | *off = paddr - d->mmio_addr; |
| 1649 | return d; |
| 1650 | } |
| 1651 | return NULL; |
| 1652 | } |
| 1653 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 1654 | /* FIXME: Use vq array. */ |
| 1655 | static struct virtqueue *vq_by_num(struct device *d, u32 num) |
| 1656 | { |
| 1657 | struct virtqueue *vq = d->vq; |
| 1658 | |
| 1659 | while (num-- && vq) |
| 1660 | vq = vq->next; |
| 1661 | |
| 1662 | return vq; |
| 1663 | } |
| 1664 | |
| 1665 | static void save_vq_config(const struct virtio_pci_common_cfg *cfg, |
| 1666 | struct virtqueue *vq) |
| 1667 | { |
| 1668 | vq->pci_config = *cfg; |
| 1669 | } |
| 1670 | |
| 1671 | static void restore_vq_config(struct virtio_pci_common_cfg *cfg, |
| 1672 | struct virtqueue *vq) |
| 1673 | { |
| 1674 | /* Only restore the per-vq part */ |
| 1675 | size_t off = offsetof(struct virtio_pci_common_cfg, queue_size); |
| 1676 | |
| 1677 | memcpy((void *)cfg + off, (void *)&vq->pci_config + off, |
| 1678 | sizeof(*cfg) - off); |
| 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * When they enable the virtqueue, we check that their setup is valid. |
| 1683 | */ |
| 1684 | static void enable_virtqueue(struct device *d, struct virtqueue *vq) |
| 1685 | { |
| 1686 | /* |
| 1687 | * Create stack for thread. Since the stack grows upwards, we point |
| 1688 | * the stack pointer to the end of this region. |
| 1689 | */ |
| 1690 | char *stack = malloc(32768); |
| 1691 | |
| 1692 | /* Because lguest is 32 bit, all the descriptor high bits must be 0 */ |
| 1693 | if (vq->pci_config.queue_desc_hi |
| 1694 | || vq->pci_config.queue_avail_hi |
| 1695 | || vq->pci_config.queue_used_hi) |
| 1696 | errx(1, "%s: invalid 64-bit queue address", d->name); |
| 1697 | |
| 1698 | /* Initialize the virtqueue and check they're all in range. */ |
| 1699 | vq->vring.num = vq->pci_config.queue_size; |
| 1700 | vq->vring.desc = check_pointer(vq->pci_config.queue_desc_lo, |
| 1701 | sizeof(*vq->vring.desc) * vq->vring.num); |
| 1702 | vq->vring.avail = check_pointer(vq->pci_config.queue_avail_lo, |
| 1703 | sizeof(*vq->vring.avail) |
| 1704 | + (sizeof(vq->vring.avail->ring[0]) |
| 1705 | * vq->vring.num)); |
| 1706 | vq->vring.used = check_pointer(vq->pci_config.queue_used_lo, |
| 1707 | sizeof(*vq->vring.used) |
| 1708 | + (sizeof(vq->vring.used->ring[0]) |
| 1709 | * vq->vring.num)); |
| 1710 | |
| 1711 | |
| 1712 | /* Create a zero-initialized eventfd. */ |
| 1713 | vq->eventfd = eventfd(0, 0); |
| 1714 | if (vq->eventfd < 0) |
| 1715 | err(1, "Creating eventfd"); |
| 1716 | |
| 1717 | /* |
| 1718 | * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so |
| 1719 | * we get a signal if it dies. |
| 1720 | */ |
| 1721 | vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq); |
| 1722 | if (vq->thread == (pid_t)-1) |
| 1723 | err(1, "Creating clone"); |
| 1724 | } |
| 1725 | |
| 1726 | static void reset_pci_device(struct device *dev) |
| 1727 | { |
| 1728 | /* FIXME */ |
| 1729 | } |
| 1730 | |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1731 | static void emulate_mmio_write(struct device *d, u32 off, u32 val, u32 mask) |
| 1732 | { |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 1733 | struct virtqueue *vq; |
| 1734 | |
| 1735 | switch (off) { |
| 1736 | case offsetof(struct virtio_pci_mmio, cfg.device_feature_select): |
| 1737 | if (val == 0) |
| 1738 | d->mmio->cfg.device_feature = d->features; |
| 1739 | else if (val == 1) |
| 1740 | d->mmio->cfg.device_feature = (d->features >> 32); |
| 1741 | else |
| 1742 | d->mmio->cfg.device_feature = 0; |
| 1743 | goto write_through32; |
| 1744 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature_select): |
| 1745 | if (val > 1) |
| 1746 | errx(1, "%s: Unexpected driver select %u", |
| 1747 | d->name, val); |
| 1748 | goto write_through32; |
| 1749 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature): |
| 1750 | if (d->mmio->cfg.guest_feature_select == 0) { |
| 1751 | d->features_accepted &= ~((u64)0xFFFFFFFF); |
| 1752 | d->features_accepted |= val; |
| 1753 | } else { |
| 1754 | assert(d->mmio->cfg.guest_feature_select == 1); |
| 1755 | d->features_accepted &= ((u64)0xFFFFFFFF << 32); |
| 1756 | d->features_accepted |= ((u64)val) << 32; |
| 1757 | } |
| 1758 | if (d->features_accepted & ~d->features) |
| 1759 | errx(1, "%s: over-accepted features %#llx of %#llx", |
| 1760 | d->name, d->features_accepted, d->features); |
| 1761 | goto write_through32; |
| 1762 | case offsetof(struct virtio_pci_mmio, cfg.device_status): |
| 1763 | verbose("%s: device status -> %#x\n", d->name, val); |
| 1764 | if (val == 0) |
| 1765 | reset_pci_device(d); |
| 1766 | goto write_through8; |
| 1767 | case offsetof(struct virtio_pci_mmio, cfg.queue_select): |
| 1768 | vq = vq_by_num(d, val); |
| 1769 | /* Out of range? Return size 0 */ |
| 1770 | if (!vq) { |
| 1771 | d->mmio->cfg.queue_size = 0; |
| 1772 | goto write_through16; |
| 1773 | } |
| 1774 | /* Save registers for old vq, if it was a valid vq */ |
| 1775 | if (d->mmio->cfg.queue_size) |
| 1776 | save_vq_config(&d->mmio->cfg, |
| 1777 | vq_by_num(d, d->mmio->cfg.queue_select)); |
| 1778 | /* Restore the registers for the queue they asked for */ |
| 1779 | restore_vq_config(&d->mmio->cfg, vq); |
| 1780 | goto write_through16; |
| 1781 | case offsetof(struct virtio_pci_mmio, cfg.queue_size): |
| 1782 | if (val & (val-1)) |
| 1783 | errx(1, "%s: invalid queue size %u\n", d->name, val); |
| 1784 | if (d->mmio->cfg.queue_enable) |
| 1785 | errx(1, "%s: changing queue size on live device", |
| 1786 | d->name); |
| 1787 | goto write_through16; |
| 1788 | case offsetof(struct virtio_pci_mmio, cfg.queue_msix_vector): |
| 1789 | errx(1, "%s: attempt to set MSIX vector to %u", |
| 1790 | d->name, val); |
| 1791 | case offsetof(struct virtio_pci_mmio, cfg.queue_enable): |
| 1792 | if (val != 1) |
| 1793 | errx(1, "%s: setting queue_enable to %u", d->name, val); |
| 1794 | d->mmio->cfg.queue_enable = val; |
| 1795 | save_vq_config(&d->mmio->cfg, |
| 1796 | vq_by_num(d, d->mmio->cfg.queue_select)); |
| 1797 | enable_virtqueue(d, vq_by_num(d, d->mmio->cfg.queue_select)); |
| 1798 | goto write_through16; |
| 1799 | case offsetof(struct virtio_pci_mmio, cfg.queue_notify_off): |
| 1800 | errx(1, "%s: attempt to write to queue_notify_off", d->name); |
| 1801 | case offsetof(struct virtio_pci_mmio, cfg.queue_desc_lo): |
| 1802 | case offsetof(struct virtio_pci_mmio, cfg.queue_desc_hi): |
| 1803 | case offsetof(struct virtio_pci_mmio, cfg.queue_avail_lo): |
| 1804 | case offsetof(struct virtio_pci_mmio, cfg.queue_avail_hi): |
| 1805 | case offsetof(struct virtio_pci_mmio, cfg.queue_used_lo): |
| 1806 | case offsetof(struct virtio_pci_mmio, cfg.queue_used_hi): |
| 1807 | if (d->mmio->cfg.queue_enable) |
| 1808 | errx(1, "%s: changing queue on live device", |
| 1809 | d->name); |
| 1810 | goto write_through32; |
| 1811 | case offsetof(struct virtio_pci_mmio, notify): |
| 1812 | vq = vq_by_num(d, val); |
| 1813 | if (!vq) |
| 1814 | errx(1, "Invalid vq notification on %u", val); |
| 1815 | /* Notify the process handling this vq by adding 1 to eventfd */ |
| 1816 | write(vq->eventfd, "\1\0\0\0\0\0\0\0", 8); |
| 1817 | goto write_through16; |
| 1818 | case offsetof(struct virtio_pci_mmio, isr): |
| 1819 | errx(1, "%s: Unexpected write to isr", d->name); |
| 1820 | default: |
| 1821 | errx(1, "%s: Unexpected write to offset %u", d->name, off); |
| 1822 | } |
| 1823 | |
| 1824 | write_through32: |
| 1825 | if (mask != 0xFFFFFFFF) { |
| 1826 | errx(1, "%s: non-32-bit write to offset %u (%#x)", |
| 1827 | d->name, off, getreg(eip)); |
| 1828 | return; |
| 1829 | } |
| 1830 | memcpy((char *)d->mmio + off, &val, 4); |
| 1831 | return; |
| 1832 | |
| 1833 | write_through16: |
| 1834 | if (mask != 0xFFFF) |
| 1835 | errx(1, "%s: non-16-bit (%#x) write to offset %u (%#x)", |
| 1836 | d->name, mask, off, getreg(eip)); |
| 1837 | memcpy((char *)d->mmio + off, &val, 2); |
| 1838 | return; |
| 1839 | |
| 1840 | write_through8: |
| 1841 | if (mask != 0xFF) |
| 1842 | errx(1, "%s: non-8-bit write to offset %u (%#x)", |
| 1843 | d->name, off, getreg(eip)); |
| 1844 | memcpy((char *)d->mmio + off, &val, 1); |
| 1845 | return; |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | static u32 emulate_mmio_read(struct device *d, u32 off, u32 mask) |
| 1849 | { |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 1850 | u8 isr; |
| 1851 | u32 val = 0; |
| 1852 | |
| 1853 | switch (off) { |
| 1854 | case offsetof(struct virtio_pci_mmio, cfg.device_feature_select): |
| 1855 | case offsetof(struct virtio_pci_mmio, cfg.device_feature): |
| 1856 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature_select): |
| 1857 | case offsetof(struct virtio_pci_mmio, cfg.guest_feature): |
| 1858 | goto read_through32; |
| 1859 | case offsetof(struct virtio_pci_mmio, cfg.msix_config): |
| 1860 | errx(1, "%s: read of msix_config", d->name); |
| 1861 | case offsetof(struct virtio_pci_mmio, cfg.num_queues): |
| 1862 | goto read_through16; |
| 1863 | case offsetof(struct virtio_pci_mmio, cfg.device_status): |
| 1864 | case offsetof(struct virtio_pci_mmio, cfg.config_generation): |
| 1865 | goto read_through8; |
| 1866 | case offsetof(struct virtio_pci_mmio, notify): |
| 1867 | goto read_through16; |
| 1868 | case offsetof(struct virtio_pci_mmio, isr): |
| 1869 | if (mask != 0xFF) |
| 1870 | errx(1, "%s: non-8-bit read from offset %u (%#x)", |
| 1871 | d->name, off, getreg(eip)); |
| 1872 | /* Read resets the isr */ |
| 1873 | isr = d->mmio->isr; |
| 1874 | d->mmio->isr = 0; |
| 1875 | return isr; |
| 1876 | case offsetof(struct virtio_pci_mmio, padding): |
| 1877 | errx(1, "%s: read from padding (%#x)", |
| 1878 | d->name, getreg(eip)); |
| 1879 | default: |
| 1880 | /* Read from device config space, beware unaligned overflow */ |
| 1881 | if (off > d->mmio_size - 4) |
| 1882 | errx(1, "%s: read past end (%#x)", |
| 1883 | d->name, getreg(eip)); |
| 1884 | if (mask == 0xFFFFFFFF) |
| 1885 | goto read_through32; |
| 1886 | else if (mask == 0xFFFF) |
| 1887 | goto read_through16; |
| 1888 | else |
| 1889 | goto read_through8; |
| 1890 | } |
| 1891 | |
| 1892 | read_through32: |
| 1893 | if (mask != 0xFFFFFFFF) |
| 1894 | errx(1, "%s: non-32-bit read to offset %u (%#x)", |
| 1895 | d->name, off, getreg(eip)); |
| 1896 | memcpy(&val, (char *)d->mmio + off, 4); |
| 1897 | return val; |
| 1898 | |
| 1899 | read_through16: |
| 1900 | if (mask != 0xFFFF) |
| 1901 | errx(1, "%s: non-16-bit read to offset %u (%#x)", |
| 1902 | d->name, off, getreg(eip)); |
| 1903 | memcpy(&val, (char *)d->mmio + off, 2); |
| 1904 | return val; |
| 1905 | |
| 1906 | read_through8: |
| 1907 | if (mask != 0xFF) |
| 1908 | errx(1, "%s: non-8-bit read to offset %u (%#x)", |
| 1909 | d->name, off, getreg(eip)); |
| 1910 | memcpy(&val, (char *)d->mmio + off, 1); |
| 1911 | return val; |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | static void emulate_mmio(unsigned long paddr, const u8 *insn) |
| 1915 | { |
| 1916 | u32 val, off, mask = 0xFFFFFFFF, insnlen = 0; |
| 1917 | struct device *d = find_mmio_region(paddr, &off); |
| 1918 | unsigned long args[] = { LHREQ_TRAP, 14 }; |
| 1919 | |
| 1920 | if (!d) { |
| 1921 | warnx("MMIO touching %#08lx (not a device)", paddr); |
| 1922 | goto reinject; |
| 1923 | } |
| 1924 | |
| 1925 | /* Prefix makes it a 16 bit op */ |
| 1926 | if (insn[0] == 0x66) { |
| 1927 | mask = 0xFFFF; |
| 1928 | insnlen++; |
| 1929 | } |
| 1930 | |
| 1931 | /* iowrite */ |
| 1932 | if (insn[insnlen] == 0x89) { |
| 1933 | /* Next byte is r/m byte: bits 3-5 are register. */ |
| 1934 | val = getreg_num((insn[insnlen+1] >> 3) & 0x7, mask); |
| 1935 | emulate_mmio_write(d, off, val, mask); |
| 1936 | insnlen += 2 + insn_displacement_len(insn[insnlen+1]); |
| 1937 | } else if (insn[insnlen] == 0x8b) { /* ioread */ |
| 1938 | /* Next byte is r/m byte: bits 3-5 are register. */ |
| 1939 | val = emulate_mmio_read(d, off, mask); |
| 1940 | setreg_num((insn[insnlen+1] >> 3) & 0x7, val, mask); |
| 1941 | insnlen += 2 + insn_displacement_len(insn[insnlen+1]); |
| 1942 | } else if (insn[0] == 0x88) { /* 8-bit iowrite */ |
| 1943 | mask = 0xff; |
| 1944 | /* Next byte is r/m byte: bits 3-5 are register. */ |
| 1945 | val = getreg_num((insn[1] >> 3) & 0x7, mask); |
| 1946 | emulate_mmio_write(d, off, val, mask); |
| 1947 | insnlen = 2 + insn_displacement_len(insn[1]); |
| 1948 | } else if (insn[0] == 0x8a) { /* 8-bit ioread */ |
| 1949 | mask = 0xff; |
| 1950 | val = emulate_mmio_read(d, off, mask); |
| 1951 | setreg_num((insn[1] >> 3) & 0x7, val, mask); |
| 1952 | insnlen = 2 + insn_displacement_len(insn[1]); |
| 1953 | } else { |
| 1954 | warnx("Unknown MMIO instruction touching %#08lx:" |
| 1955 | " %02x %02x %02x %02x at %u", |
| 1956 | paddr, insn[0], insn[1], insn[2], insn[3], getreg(eip)); |
| 1957 | reinject: |
| 1958 | /* Inject trap into Guest. */ |
| 1959 | if (write(lguest_fd, args, sizeof(args)) < 0) |
| 1960 | err(1, "Reinjecting trap 14 for fault at %#x", |
| 1961 | getreg(eip)); |
| 1962 | return; |
| 1963 | } |
| 1964 | |
| 1965 | /* Finally, we've "done" the instruction, so move past it. */ |
| 1966 | setreg(eip, getreg(eip) + insnlen); |
| 1967 | } |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 1968 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1969 | /*L:190 |
| 1970 | * Device Setup |
| 1971 | * |
| 1972 | * All devices need a descriptor so the Guest knows it exists, and a "struct |
| 1973 | * device" so the Launcher can keep track of it. We have common helper |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 1974 | * routines to allocate and manage them. |
| 1975 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1976 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1977 | /* |
| 1978 | * The layout of the device page is a "struct lguest_device_desc" followed by a |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1979 | * number of virtqueue descriptors, then two sets of feature bits, then an |
| 1980 | * array of configuration bytes. This routine returns the configuration |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1981 | * pointer. |
| 1982 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1983 | static u8 *device_config(const struct device *dev) |
| 1984 | { |
| 1985 | return (void *)(dev->desc + 1) |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 1986 | + dev->num_vq * sizeof(struct lguest_vqconfig) |
| 1987 | + dev->feature_len * 2; |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1988 | } |
| 1989 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1990 | /* |
| 1991 | * This routine allocates a new "struct lguest_device_desc" from descriptor |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1992 | * table page just above the Guest's normal memory. It returns a pointer to |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1993 | * that descriptor. |
| 1994 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 1995 | static struct lguest_device_desc *new_dev_desc(u16 type) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1996 | { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 1997 | struct lguest_device_desc d = { .type = type }; |
| 1998 | void *p; |
| 1999 | |
| 2000 | /* Figure out where the next device config is, based on the last one. */ |
| 2001 | if (devices.lastdev) |
| 2002 | p = device_config(devices.lastdev) |
| 2003 | + devices.lastdev->desc->config_len; |
| 2004 | else |
| 2005 | p = devices.descpage; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2006 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2007 | /* We only have one page for all the descriptors. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2008 | if (p + sizeof(d) > (void *)devices.descpage + getpagesize()) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2009 | errx(1, "Too many devices"); |
| 2010 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2011 | /* p might not be aligned, so we memcpy in. */ |
| 2012 | return memcpy(p, &d, sizeof(d)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2015 | /* |
| 2016 | * Each device descriptor is followed by the description of its virtqueues. We |
| 2017 | * specify how many descriptors the virtqueue is to have. |
| 2018 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2019 | static void add_virtqueue(struct device *dev, unsigned int num_descs, |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2020 | void (*service)(struct virtqueue *)) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2021 | { |
| 2022 | unsigned int pages; |
| 2023 | struct virtqueue **i, *vq = malloc(sizeof(*vq)); |
| 2024 | void *p; |
| 2025 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 2026 | /* First we need some memory for this virtqueue. */ |
Rusty Russell | 2966af7 | 2008-12-30 09:25:58 -0600 | [diff] [blame] | 2027 | pages = (vring_size(num_descs, LGUEST_VRING_ALIGN) + getpagesize() - 1) |
Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 2028 | / getpagesize(); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2029 | p = get_pages(pages); |
| 2030 | |
Rusty Russell | d1c856e | 2007-11-19 11:20:40 -0500 | [diff] [blame] | 2031 | /* Initialize the virtqueue */ |
| 2032 | vq->next = NULL; |
| 2033 | vq->last_avail_idx = 0; |
| 2034 | vq->dev = dev; |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2035 | |
| 2036 | /* |
| 2037 | * This is the routine the service thread will run, and its Process ID |
| 2038 | * once it's running. |
| 2039 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2040 | vq->service = service; |
| 2041 | vq->thread = (pid_t)-1; |
Rusty Russell | d1c856e | 2007-11-19 11:20:40 -0500 | [diff] [blame] | 2042 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2043 | /* Initialize the configuration. */ |
| 2044 | vq->config.num = num_descs; |
| 2045 | vq->config.irq = devices.next_irq++; |
| 2046 | vq->config.pfn = to_guest_phys(p) / getpagesize(); |
| 2047 | |
| 2048 | /* Initialize the vring. */ |
Rusty Russell | 2966af7 | 2008-12-30 09:25:58 -0600 | [diff] [blame] | 2049 | vring_init(&vq->vring, num_descs, p, LGUEST_VRING_ALIGN); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2050 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2051 | /* |
| 2052 | * Append virtqueue to this device's descriptor. We use |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2053 | * device_config() to get the end of the device's current virtqueues; |
| 2054 | * we check that we haven't added any config or feature information |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2055 | * yet, otherwise we'd be overwriting them. |
| 2056 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2057 | assert(dev->desc->config_len == 0 && dev->desc->feature_len == 0); |
| 2058 | memcpy(device_config(dev), &vq->config, sizeof(vq->config)); |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 2059 | dev->num_vq++; |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2060 | dev->desc->num_vq++; |
| 2061 | |
| 2062 | verbose("Virtqueue page %#lx\n", to_guest_phys(p)); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2063 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2064 | /* |
| 2065 | * Add to tail of list, so dev->vq is first vq, dev->vq->next is |
| 2066 | * second. |
| 2067 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2068 | for (i = &dev->vq; *i; i = &(*i)->next); |
| 2069 | *i = vq; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2070 | } |
| 2071 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 2072 | static void add_pci_virtqueue(struct device *dev, |
| 2073 | void (*service)(struct virtqueue *)) |
| 2074 | { |
| 2075 | struct virtqueue **i, *vq = malloc(sizeof(*vq)); |
| 2076 | |
| 2077 | /* Initialize the virtqueue */ |
| 2078 | vq->next = NULL; |
| 2079 | vq->last_avail_idx = 0; |
| 2080 | vq->dev = dev; |
| 2081 | |
| 2082 | /* |
| 2083 | * This is the routine the service thread will run, and its Process ID |
| 2084 | * once it's running. |
| 2085 | */ |
| 2086 | vq->service = service; |
| 2087 | vq->thread = (pid_t)-1; |
| 2088 | |
| 2089 | /* Initialize the configuration. */ |
| 2090 | vq->pci_config.queue_size = VIRTQUEUE_NUM; |
| 2091 | vq->pci_config.queue_enable = 0; |
| 2092 | vq->pci_config.queue_notify_off = 0; |
| 2093 | |
| 2094 | /* Add one to the number of queues */ |
| 2095 | vq->dev->mmio->cfg.num_queues++; |
| 2096 | |
| 2097 | /* FIXME: Do irq per virtqueue, not per device. */ |
| 2098 | vq->config.irq = vq->dev->config.irq_line; |
| 2099 | |
| 2100 | /* |
| 2101 | * Add to tail of list, so dev->vq is first vq, dev->vq->next is |
| 2102 | * second. |
| 2103 | */ |
| 2104 | for (i = &dev->vq; *i; i = &(*i)->next); |
| 2105 | *i = vq; |
| 2106 | } |
| 2107 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2108 | /* |
| 2109 | * The first half of the feature bitmask is for us to advertise features. The |
| 2110 | * second half is for the Guest to accept features. |
| 2111 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2112 | static void add_feature(struct device *dev, unsigned bit) |
| 2113 | { |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 2114 | u8 *features = get_feature_bits(dev); |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2115 | |
| 2116 | /* We can't extend the feature bits once we've added config bytes */ |
| 2117 | if (dev->desc->feature_len <= bit / CHAR_BIT) { |
| 2118 | assert(dev->desc->config_len == 0); |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 2119 | dev->feature_len = dev->desc->feature_len = (bit/CHAR_BIT) + 1; |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2120 | } |
| 2121 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2122 | features[bit / CHAR_BIT] |= (1 << (bit % CHAR_BIT)); |
| 2123 | } |
| 2124 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 2125 | static void add_pci_feature(struct device *dev, unsigned bit) |
| 2126 | { |
| 2127 | dev->features |= (1ULL << bit); |
| 2128 | } |
| 2129 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2130 | /* |
| 2131 | * This routine sets the configuration fields for an existing device's |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2132 | * descriptor. It only works for the last device, but that's OK because that's |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2133 | * how we use it. |
| 2134 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2135 | static void set_config(struct device *dev, unsigned len, const void *conf) |
| 2136 | { |
| 2137 | /* Check we haven't overflowed our single page. */ |
| 2138 | if (device_config(dev) + len > devices.descpage + getpagesize()) |
| 2139 | errx(1, "Too many devices"); |
| 2140 | |
| 2141 | /* Copy in the config information, and store the length. */ |
| 2142 | memcpy(device_config(dev), conf, len); |
| 2143 | dev->desc->config_len = len; |
Rusty Russell | 8ef562d | 2009-07-30 16:03:43 -0600 | [diff] [blame] | 2144 | |
| 2145 | /* Size must fit in config_len field (8 bits)! */ |
| 2146 | assert(dev->desc->config_len == len); |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2147 | } |
| 2148 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 2149 | /* For devices with no config. */ |
| 2150 | static void no_device_config(struct device *dev) |
| 2151 | { |
| 2152 | dev->mmio_addr = get_mmio_region(dev->mmio_size); |
| 2153 | |
| 2154 | dev->config.bar[0] = dev->mmio_addr; |
| 2155 | /* Bottom 4 bits must be zero */ |
| 2156 | assert(~(dev->config.bar[0] & 0xF)); |
| 2157 | } |
| 2158 | |
| 2159 | /* This puts the device config into BAR0 */ |
| 2160 | static void set_device_config(struct device *dev, const void *conf, size_t len) |
| 2161 | { |
| 2162 | /* Set up BAR 0 */ |
| 2163 | dev->mmio_size += len; |
| 2164 | dev->mmio = realloc(dev->mmio, dev->mmio_size); |
| 2165 | memcpy(dev->mmio + 1, conf, len); |
| 2166 | |
| 2167 | /* Hook up device cfg */ |
| 2168 | dev->config.cfg_access.cap.cap_next |
| 2169 | = offsetof(struct pci_config, device); |
| 2170 | |
| 2171 | /* Fix up device cfg field length. */ |
| 2172 | dev->config.device.length = len; |
| 2173 | |
| 2174 | /* The rest is the same as the no-config case */ |
| 2175 | no_device_config(dev); |
| 2176 | } |
| 2177 | |
| 2178 | static void init_cap(struct virtio_pci_cap *cap, size_t caplen, int type, |
| 2179 | size_t bar_offset, size_t bar_bytes, u8 next) |
| 2180 | { |
| 2181 | cap->cap_vndr = PCI_CAP_ID_VNDR; |
| 2182 | cap->cap_next = next; |
| 2183 | cap->cap_len = caplen; |
| 2184 | cap->cfg_type = type; |
| 2185 | cap->bar = 0; |
| 2186 | memset(cap->padding, 0, sizeof(cap->padding)); |
| 2187 | cap->offset = bar_offset; |
| 2188 | cap->length = bar_bytes; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * This sets up the pci_config structure, as defined in the virtio 1.0 |
| 2193 | * standard (and PCI standard). |
| 2194 | */ |
| 2195 | static void init_pci_config(struct pci_config *pci, u16 type, |
| 2196 | u8 class, u8 subclass) |
| 2197 | { |
| 2198 | size_t bar_offset, bar_len; |
| 2199 | |
| 2200 | /* Save typing: most thing are happy being zero. */ |
| 2201 | memset(pci, 0, sizeof(*pci)); |
| 2202 | |
| 2203 | /* 4.1.2.1: Devices MUST have the PCI Vendor ID 0x1AF4 */ |
| 2204 | pci->vendor_id = 0x1AF4; |
| 2205 | /* 4.1.2.1: ... PCI Device ID calculated by adding 0x1040 ... */ |
| 2206 | pci->device_id = 0x1040 + type; |
| 2207 | |
| 2208 | /* |
| 2209 | * PCI have specific codes for different types of devices. |
| 2210 | * Linux doesn't care, but it's a good clue for people looking |
| 2211 | * at the device. |
| 2212 | * |
| 2213 | * eg : |
| 2214 | * VIRTIO_ID_CONSOLE: class = 0x07, subclass = 0x00 |
| 2215 | * VIRTIO_ID_NET: class = 0x02, subclass = 0x00 |
| 2216 | * VIRTIO_ID_BLOCK: class = 0x01, subclass = 0x80 |
| 2217 | * VIRTIO_ID_RNG: class = 0xff, subclass = 0 |
| 2218 | */ |
| 2219 | pci->class = class; |
| 2220 | pci->subclass = subclass; |
| 2221 | |
| 2222 | /* |
| 2223 | * 4.1.2.1 Non-transitional devices SHOULD have a PCI Revision |
| 2224 | * ID of 1 or higher |
| 2225 | */ |
| 2226 | pci->revid = 1; |
| 2227 | |
| 2228 | /* |
| 2229 | * 4.1.2.1 Non-transitional devices SHOULD have a PCI |
| 2230 | * Subsystem Device ID of 0x40 or higher. |
| 2231 | */ |
| 2232 | pci->subsystem_device_id = 0x40; |
| 2233 | |
| 2234 | /* We use our dummy interrupt controller, and irq_line is the irq */ |
| 2235 | pci->irq_line = devices.next_irq++; |
| 2236 | pci->irq_pin = 0; |
| 2237 | |
| 2238 | /* Support for extended capabilities. */ |
| 2239 | pci->status = (1 << 4); |
| 2240 | |
| 2241 | /* Link them in. */ |
| 2242 | pci->capabilities = offsetof(struct pci_config, common); |
| 2243 | |
| 2244 | bar_offset = offsetof(struct virtio_pci_mmio, cfg); |
| 2245 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->cfg); |
| 2246 | init_cap(&pci->common, sizeof(pci->common), VIRTIO_PCI_CAP_COMMON_CFG, |
| 2247 | bar_offset, bar_len, |
| 2248 | offsetof(struct pci_config, notify)); |
| 2249 | |
| 2250 | bar_offset += bar_len; |
| 2251 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->notify); |
| 2252 | /* FIXME: Use a non-zero notify_off, for per-queue notification? */ |
| 2253 | init_cap(&pci->notify.cap, sizeof(pci->notify), |
| 2254 | VIRTIO_PCI_CAP_NOTIFY_CFG, |
| 2255 | bar_offset, bar_len, |
| 2256 | offsetof(struct pci_config, isr)); |
| 2257 | |
| 2258 | bar_offset += bar_len; |
| 2259 | bar_len = sizeof(((struct virtio_pci_mmio *)0)->isr); |
| 2260 | init_cap(&pci->isr, sizeof(pci->isr), |
| 2261 | VIRTIO_PCI_CAP_ISR_CFG, |
| 2262 | bar_offset, bar_len, |
| 2263 | offsetof(struct pci_config, cfg_access)); |
| 2264 | |
| 2265 | /* This doesn't have any presence in the BAR */ |
| 2266 | init_cap(&pci->cfg_access.cap, sizeof(pci->cfg_access), |
| 2267 | VIRTIO_PCI_CAP_PCI_CFG, |
| 2268 | 0, 0, 0); |
| 2269 | |
| 2270 | bar_offset += bar_len + sizeof(((struct virtio_pci_mmio *)0)->padding); |
| 2271 | assert(bar_offset == sizeof(struct virtio_pci_mmio)); |
| 2272 | |
| 2273 | /* |
| 2274 | * This gets sewn in and length set in set_device_config(). |
| 2275 | * Some devices don't have a device configuration interface, so |
| 2276 | * we never expose this if we don't call set_device_config(). |
| 2277 | */ |
| 2278 | init_cap(&pci->device, sizeof(pci->device), VIRTIO_PCI_CAP_DEVICE_CFG, |
| 2279 | bar_offset, 0, 0); |
| 2280 | } |
| 2281 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2282 | /* |
| 2283 | * This routine does all the creation and setup of a new device, including |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2284 | * calling new_dev_desc() to allocate the descriptor and device memory. We |
| 2285 | * don't actually start the service threads until later. |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 2286 | * |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2287 | * See what I mean about userspace being boring? |
| 2288 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2289 | static struct device *new_device(const char *name, u16 type) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2290 | { |
| 2291 | struct device *dev = malloc(sizeof(*dev)); |
| 2292 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2293 | /* Now we populate the fields one at a time. */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2294 | dev->desc = new_dev_desc(type); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2295 | dev->name = name; |
Rusty Russell | d1c856e | 2007-11-19 11:20:40 -0500 | [diff] [blame] | 2296 | dev->vq = NULL; |
Rusty Russell | 713b15b | 2009-06-12 22:26:58 -0600 | [diff] [blame] | 2297 | dev->feature_len = 0; |
| 2298 | dev->num_vq = 0; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2299 | dev->running = false; |
Rusty Russell | ca16f58 | 2012-10-04 12:03:25 +0930 | [diff] [blame] | 2300 | dev->next = NULL; |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2301 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2302 | /* |
| 2303 | * Append to device list. Prepending to a single-linked list is |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2304 | * easier, but the user expects the devices to be arranged on the bus |
| 2305 | * in command-line order. The first network device on the command line |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2306 | * is eth0, the first block device /dev/vda, etc. |
| 2307 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2308 | if (devices.lastdev) |
| 2309 | devices.lastdev->next = dev; |
| 2310 | else |
| 2311 | devices.dev = dev; |
| 2312 | devices.lastdev = dev; |
| 2313 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2314 | return dev; |
| 2315 | } |
| 2316 | |
Rusty Russell | 9315307 | 2015-02-11 15:15:11 +1030 | [diff] [blame^] | 2317 | static struct device *new_pci_device(const char *name, u16 type, |
| 2318 | u8 class, u8 subclass) |
| 2319 | { |
| 2320 | struct device *dev = malloc(sizeof(*dev)); |
| 2321 | |
| 2322 | /* Now we populate the fields one at a time. */ |
| 2323 | dev->desc = NULL; |
| 2324 | dev->name = name; |
| 2325 | dev->vq = NULL; |
| 2326 | dev->feature_len = 0; |
| 2327 | dev->num_vq = 0; |
| 2328 | dev->running = false; |
| 2329 | dev->next = NULL; |
| 2330 | dev->mmio_size = sizeof(struct virtio_pci_mmio); |
| 2331 | dev->mmio = calloc(1, dev->mmio_size); |
| 2332 | dev->features = (u64)1 << VIRTIO_F_VERSION_1; |
| 2333 | dev->features_accepted = 0; |
| 2334 | |
| 2335 | if (devices.device_num + 1 >= 32) |
| 2336 | errx(1, "Can only handle 31 PCI devices"); |
| 2337 | |
| 2338 | init_pci_config(&dev->config, type, class, subclass); |
| 2339 | assert(!devices.pci[devices.device_num+1]); |
| 2340 | devices.pci[++devices.device_num] = dev; |
| 2341 | |
| 2342 | return dev; |
| 2343 | } |
| 2344 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2345 | /* |
| 2346 | * Our first setup routine is the console. It's a fairly simple device, but |
| 2347 | * UNIX tty handling makes it uglier than it could be. |
| 2348 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2349 | static void setup_console(void) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2350 | { |
| 2351 | struct device *dev; |
| 2352 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2353 | /* If we can save the initial standard input settings... */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2354 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { |
| 2355 | struct termios term = orig_term; |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2356 | /* |
| 2357 | * Then we turn off echo, line buffering and ^C etc: We want a |
| 2358 | * raw input stream to the Guest. |
| 2359 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2360 | term.c_lflag &= ~(ISIG|ICANON|ECHO); |
| 2361 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2364 | dev = new_device("console", VIRTIO_ID_CONSOLE); |
| 2365 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2366 | /* We store the console state in dev->priv, and initialize it. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2367 | dev->priv = malloc(sizeof(struct console_abort)); |
| 2368 | ((struct console_abort *)dev->priv)->count = 0; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2369 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2370 | /* |
| 2371 | * The console needs two virtqueues: the input then the output. When |
Rusty Russell | 56ae43d | 2007-10-22 11:24:23 +1000 | [diff] [blame] | 2372 | * they put something the input queue, we make sure we're listening to |
| 2373 | * stdin. When they put something in the output queue, we write it to |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2374 | * stdout. |
| 2375 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2376 | add_virtqueue(dev, VIRTQUEUE_NUM, console_input); |
| 2377 | add_virtqueue(dev, VIRTQUEUE_NUM, console_output); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2378 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2379 | verbose("device %u: console\n", ++devices.device_num); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2380 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2381 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2382 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2383 | /*M:010 |
| 2384 | * Inter-guest networking is an interesting area. Simplest is to have a |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2385 | * --sharenet=<name> option which opens or creates a named pipe. This can be |
| 2386 | * used to send packets to another guest in a 1:1 manner. |
| 2387 | * |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 2388 | * More sophisticated is to use one of the tools developed for project like UML |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2389 | * to do networking. |
| 2390 | * |
| 2391 | * Faster is to do virtio bonding in kernel. Doing this 1:1 would be |
| 2392 | * completely generic ("here's my vring, attach to your vring") and would work |
| 2393 | * for any traffic. Of course, namespace and permissions issues need to be |
| 2394 | * dealt with. A more sophisticated "multi-channel" virtio_net.c could hide |
| 2395 | * multiple inter-guest channels behind one interface, although it would |
| 2396 | * require some manner of hotplugging new virtio channels. |
| 2397 | * |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 2398 | * Finally, we could use a virtio network switch in the kernel, ie. vhost. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2399 | :*/ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2400 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2401 | static u32 str2ip(const char *ipaddr) |
| 2402 | { |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2403 | unsigned int b[4]; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2404 | |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2405 | if (sscanf(ipaddr, "%u.%u.%u.%u", &b[0], &b[1], &b[2], &b[3]) != 4) |
| 2406 | errx(1, "Failed to parse IP address '%s'", ipaddr); |
| 2407 | return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]; |
| 2408 | } |
| 2409 | |
| 2410 | static void str2mac(const char *macaddr, unsigned char mac[6]) |
| 2411 | { |
| 2412 | unsigned int m[6]; |
| 2413 | if (sscanf(macaddr, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 2414 | &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) != 6) |
| 2415 | errx(1, "Failed to parse mac address '%s'", macaddr); |
| 2416 | mac[0] = m[0]; |
| 2417 | mac[1] = m[1]; |
| 2418 | mac[2] = m[2]; |
| 2419 | mac[3] = m[3]; |
| 2420 | mac[4] = m[4]; |
| 2421 | mac[5] = m[5]; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2424 | /* |
| 2425 | * This code is "adapted" from libbridge: it attaches the Host end of the |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2426 | * network device to the bridge device specified by the command line. |
| 2427 | * |
| 2428 | * This is yet another James Morris contribution (I'm an IP-level guy, so I |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2429 | * dislike bridging), and I just try not to break it. |
| 2430 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2431 | static void add_to_bridge(int fd, const char *if_name, const char *br_name) |
| 2432 | { |
| 2433 | int ifidx; |
| 2434 | struct ifreq ifr; |
| 2435 | |
| 2436 | if (!*br_name) |
| 2437 | errx(1, "must specify bridge name"); |
| 2438 | |
| 2439 | ifidx = if_nametoindex(if_name); |
| 2440 | if (!ifidx) |
| 2441 | errx(1, "interface %s does not exist!", if_name); |
| 2442 | |
| 2443 | strncpy(ifr.ifr_name, br_name, IFNAMSIZ); |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2444 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2445 | ifr.ifr_ifindex = ifidx; |
| 2446 | if (ioctl(fd, SIOCBRADDIF, &ifr) < 0) |
| 2447 | err(1, "can't add %s to bridge %s", if_name, br_name); |
| 2448 | } |
| 2449 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2450 | /* |
| 2451 | * This sets up the Host end of the network device with an IP address, brings |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2452 | * it up so packets will flow, the copies the MAC address into the hwaddr |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2453 | * pointer. |
| 2454 | */ |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2455 | static void configure_device(int fd, const char *tapif, u32 ipaddr) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2456 | { |
| 2457 | struct ifreq ifr; |
Rusty Russell | f846619 | 2010-08-27 08:39:48 -0600 | [diff] [blame] | 2458 | struct sockaddr_in sin; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2459 | |
| 2460 | memset(&ifr, 0, sizeof(ifr)); |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2461 | strcpy(ifr.ifr_name, tapif); |
| 2462 | |
| 2463 | /* Don't read these incantations. Just cut & paste them like I did! */ |
Rusty Russell | f846619 | 2010-08-27 08:39:48 -0600 | [diff] [blame] | 2464 | sin.sin_family = AF_INET; |
| 2465 | sin.sin_addr.s_addr = htonl(ipaddr); |
| 2466 | memcpy(&ifr.ifr_addr, &sin, sizeof(sin)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2467 | if (ioctl(fd, SIOCSIFADDR, &ifr) != 0) |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2468 | err(1, "Setting %s interface address", tapif); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2469 | ifr.ifr_flags = IFF_UP; |
| 2470 | if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2471 | err(1, "Bringing interface %s up", tapif); |
| 2472 | } |
| 2473 | |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2474 | static int get_tun_device(char tapif[IFNAMSIZ]) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2475 | { |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2476 | struct ifreq ifr; |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2477 | int netfd; |
| 2478 | |
| 2479 | /* Start with this zeroed. Messy but sure. */ |
| 2480 | memset(&ifr, 0, sizeof(ifr)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2481 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2482 | /* |
| 2483 | * We open the /dev/net/tun device and tell it we want a tap device. A |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2484 | * tap device is like a tun device, only somehow different. To tell |
| 2485 | * the truth, I completely blundered my way through this code, but it |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2486 | * works now! |
| 2487 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2488 | netfd = open_or_die("/dev/net/tun", O_RDWR); |
Rusty Russell | 398f187 | 2008-07-29 09:58:37 -0500 | [diff] [blame] | 2489 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2490 | strcpy(ifr.ifr_name, "tap%d"); |
| 2491 | if (ioctl(netfd, TUNSETIFF, &ifr) != 0) |
| 2492 | err(1, "configuring /dev/net/tun"); |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2493 | |
Rusty Russell | 398f187 | 2008-07-29 09:58:37 -0500 | [diff] [blame] | 2494 | if (ioctl(netfd, TUNSETOFFLOAD, |
| 2495 | TUN_F_CSUM|TUN_F_TSO4|TUN_F_TSO6|TUN_F_TSO_ECN) != 0) |
| 2496 | err(1, "Could not set features for tun device"); |
| 2497 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2498 | /* |
| 2499 | * We don't need checksums calculated for packets coming in this |
| 2500 | * device: trust us! |
| 2501 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2502 | ioctl(netfd, TUNSETNOCSUM, 1); |
| 2503 | |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2504 | memcpy(tapif, ifr.ifr_name, IFNAMSIZ); |
| 2505 | return netfd; |
| 2506 | } |
| 2507 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2508 | /*L:195 |
| 2509 | * Our network is a Host<->Guest network. This can either use bridging or |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2510 | * routing, but the principle is the same: it uses the "tun" device to inject |
| 2511 | * packets into the Host as if they came in from a normal network card. We |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2512 | * just shunt packets between the Guest and the tun device. |
| 2513 | */ |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2514 | static void setup_tun_net(char *arg) |
| 2515 | { |
| 2516 | struct device *dev; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2517 | struct net_info *net_info = malloc(sizeof(*net_info)); |
| 2518 | int ipfd; |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2519 | u32 ip = INADDR_ANY; |
| 2520 | bool bridging = false; |
| 2521 | char tapif[IFNAMSIZ], *p; |
| 2522 | struct virtio_net_config conf; |
| 2523 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2524 | net_info->tunfd = get_tun_device(tapif); |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2525 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2526 | /* First we create a new network device. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2527 | dev = new_device("net", VIRTIO_ID_NET); |
| 2528 | dev->priv = net_info; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2529 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2530 | /* Network devices need a recv and a send queue, just like console. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2531 | add_virtqueue(dev, VIRTQUEUE_NUM, net_input); |
| 2532 | add_virtqueue(dev, VIRTQUEUE_NUM, net_output); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2533 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2534 | /* |
| 2535 | * We need a socket to perform the magic network ioctls to bring up the |
| 2536 | * tap interface, connect to the bridge etc. Any socket will do! |
| 2537 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2538 | ipfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 2539 | if (ipfd < 0) |
| 2540 | err(1, "opening IP socket"); |
| 2541 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2542 | /* If the command line was --tunnet=bridge:<name> do bridging. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2543 | if (!strncmp(BRIDGE_PFX, arg, strlen(BRIDGE_PFX))) { |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2544 | arg += strlen(BRIDGE_PFX); |
| 2545 | bridging = true; |
| 2546 | } |
| 2547 | |
| 2548 | /* A mac address may follow the bridge name or IP address */ |
| 2549 | p = strchr(arg, ':'); |
| 2550 | if (p) { |
| 2551 | str2mac(p+1, conf.mac); |
Rusty Russell | 40c4207 | 2008-08-12 17:52:51 -0500 | [diff] [blame] | 2552 | add_feature(dev, VIRTIO_NET_F_MAC); |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2553 | *p = '\0'; |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /* arg is now either an IP address or a bridge name */ |
| 2557 | if (bridging) |
| 2558 | add_to_bridge(ipfd, tapif, arg); |
| 2559 | else |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2560 | ip = str2ip(arg); |
| 2561 | |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2562 | /* Set up the tun device. */ |
| 2563 | configure_device(ipfd, tapif, ip); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2564 | |
Rusty Russell | 398f187 | 2008-07-29 09:58:37 -0500 | [diff] [blame] | 2565 | /* Expect Guest to handle everything except UFO */ |
| 2566 | add_feature(dev, VIRTIO_NET_F_CSUM); |
| 2567 | add_feature(dev, VIRTIO_NET_F_GUEST_CSUM); |
Rusty Russell | 398f187 | 2008-07-29 09:58:37 -0500 | [diff] [blame] | 2568 | add_feature(dev, VIRTIO_NET_F_GUEST_TSO4); |
| 2569 | add_feature(dev, VIRTIO_NET_F_GUEST_TSO6); |
| 2570 | add_feature(dev, VIRTIO_NET_F_GUEST_ECN); |
| 2571 | add_feature(dev, VIRTIO_NET_F_HOST_TSO4); |
| 2572 | add_feature(dev, VIRTIO_NET_F_HOST_TSO6); |
| 2573 | add_feature(dev, VIRTIO_NET_F_HOST_ECN); |
Mark McLoughlin | d1f0132 | 2009-05-11 18:11:46 +0100 | [diff] [blame] | 2574 | /* We handle indirect ring entries */ |
| 2575 | add_feature(dev, VIRTIO_RING_F_INDIRECT_DESC); |
Rusty Russell | 927cfb9 | 2013-07-15 10:50:13 +0930 | [diff] [blame] | 2576 | /* We're compliant with the damn spec. */ |
| 2577 | add_feature(dev, VIRTIO_F_ANY_LAYOUT); |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2578 | set_config(dev, sizeof(conf), &conf); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2579 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2580 | /* We don't need the socket any more; setup is done. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2581 | close(ipfd); |
| 2582 | |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2583 | devices.device_num++; |
| 2584 | |
| 2585 | if (bridging) |
| 2586 | verbose("device %u: tun %s attached to bridge: %s\n", |
| 2587 | devices.device_num, tapif, arg); |
| 2588 | else |
| 2589 | verbose("device %u: tun %s: %s\n", |
| 2590 | devices.device_num, tapif, arg); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2591 | } |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2592 | /*:*/ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2593 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2594 | /* This hangs off device->priv. */ |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 2595 | struct vblk_info { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2596 | /* The size of the file. */ |
| 2597 | off64_t len; |
| 2598 | |
| 2599 | /* The file descriptor for the file. */ |
| 2600 | int fd; |
| 2601 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2602 | }; |
| 2603 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2604 | /*L:210 |
| 2605 | * The Disk |
| 2606 | * |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2607 | * The disk only has one virtqueue, so it only has one thread. It is really |
| 2608 | * simple: the Guest asks for a block number and we read or write that position |
| 2609 | * in the file. |
| 2610 | * |
| 2611 | * Before we serviced each virtqueue in a separate thread, that was unacceptably |
| 2612 | * slow: the Guest waits until the read is finished before running anything |
| 2613 | * else, even if it could have been doing useful work. |
| 2614 | * |
| 2615 | * We could have used async I/O, except it's reputed to suck so hard that |
| 2616 | * characters actually go missing from your code when you try to use it. |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2617 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2618 | static void blk_request(struct virtqueue *vq) |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2619 | { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2620 | struct vblk_info *vblk = vq->dev->priv; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2621 | unsigned int head, out_num, in_num, wlen; |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2622 | int ret, i; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 2623 | u8 *in; |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2624 | struct virtio_blk_outhdr out; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2625 | struct iovec iov[vq->vring.num]; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2626 | off64_t off; |
| 2627 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2628 | /* |
| 2629 | * Get the next request, where we normally wait. It triggers the |
| 2630 | * interrupt to acknowledge previously serviced requests (if any). |
| 2631 | */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2632 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2633 | |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2634 | /* Copy the output header from the front of the iov (adjusts iov) */ |
| 2635 | iov_consume(iov, out_num, &out, sizeof(out)); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2636 | |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2637 | /* Find and trim end of iov input array, for our status byte. */ |
| 2638 | in = NULL; |
| 2639 | for (i = out_num + in_num - 1; i >= out_num; i--) { |
| 2640 | if (iov[i].iov_len > 0) { |
| 2641 | in = iov[i].iov_base + iov[i].iov_len - 1; |
| 2642 | iov[i].iov_len--; |
| 2643 | break; |
| 2644 | } |
| 2645 | } |
| 2646 | if (!in) |
| 2647 | errx(1, "Bad virtblk cmd with no room for status"); |
| 2648 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2649 | /* |
| 2650 | * For historical reasons, block operations are expressed in 512 byte |
| 2651 | * "sectors". |
| 2652 | */ |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2653 | off = out.sector * 512; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2654 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2655 | /* |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2656 | * In general the virtio block driver is allowed to try SCSI commands. |
| 2657 | * It'd be nice if we supported eject, for example, but we don't. |
| 2658 | */ |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2659 | if (out.type & VIRTIO_BLK_T_SCSI_CMD) { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2660 | fprintf(stderr, "Scsi commands unsupported\n"); |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 2661 | *in = VIRTIO_BLK_S_UNSUPP; |
Anthony Liguori | 1200e64 | 2007-11-08 21:13:44 -0600 | [diff] [blame] | 2662 | wlen = sizeof(*in); |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2663 | } else if (out.type & VIRTIO_BLK_T_OUT) { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2664 | /* |
| 2665 | * Write |
| 2666 | * |
| 2667 | * Move to the right location in the block file. This can fail |
| 2668 | * if they try to write past end. |
| 2669 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2670 | if (lseek64(vblk->fd, off, SEEK_SET) != off) |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2671 | err(1, "Bad seek to sector %llu", out.sector); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2672 | |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2673 | ret = writev(vblk->fd, iov, out_num); |
| 2674 | verbose("WRITE to sector %llu: %i\n", out.sector, ret); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2675 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2676 | /* |
| 2677 | * Grr... Now we know how long the descriptor they sent was, we |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2678 | * make sure they didn't try to write over the end of the block |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2679 | * file (possibly extending it). |
| 2680 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2681 | if (ret > 0 && off + ret > vblk->len) { |
| 2682 | /* Trim it back to the correct length */ |
| 2683 | ftruncate64(vblk->fd, vblk->len); |
| 2684 | /* Die, bad Guest, die. */ |
| 2685 | errx(1, "Write past end %llu+%u", off, ret); |
| 2686 | } |
Tejun Heo | 7bc9fdd | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 2687 | |
| 2688 | wlen = sizeof(*in); |
| 2689 | *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2690 | } else if (out.type & VIRTIO_BLK_T_FLUSH) { |
Tejun Heo | 7bc9fdd | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 2691 | /* Flush */ |
| 2692 | ret = fdatasync(vblk->fd); |
| 2693 | verbose("FLUSH fdatasync: %i\n", ret); |
Anthony Liguori | 1200e64 | 2007-11-08 21:13:44 -0600 | [diff] [blame] | 2694 | wlen = sizeof(*in); |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 2695 | *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2696 | } else { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2697 | /* |
| 2698 | * Read |
| 2699 | * |
| 2700 | * Move to the right location in the block file. This can fail |
| 2701 | * if they try to read past end. |
| 2702 | */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2703 | if (lseek64(vblk->fd, off, SEEK_SET) != off) |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2704 | err(1, "Bad seek to sector %llu", out.sector); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2705 | |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2706 | ret = readv(vblk->fd, iov + out_num, in_num); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2707 | if (ret >= 0) { |
Anthony Liguori | 1200e64 | 2007-11-08 21:13:44 -0600 | [diff] [blame] | 2708 | wlen = sizeof(*in) + ret; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 2709 | *in = VIRTIO_BLK_S_OK; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2710 | } else { |
Anthony Liguori | 1200e64 | 2007-11-08 21:13:44 -0600 | [diff] [blame] | 2711 | wlen = sizeof(*in); |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 2712 | *in = VIRTIO_BLK_S_IOERR; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2716 | /* Finished that request. */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 2717 | add_used(vq, head, wlen); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2718 | } |
| 2719 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2720 | /*L:198 This actually sets up a virtual block device. */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2721 | static void setup_block_file(const char *filename) |
| 2722 | { |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2723 | struct device *dev; |
| 2724 | struct vblk_info *vblk; |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2725 | struct virtio_blk_config conf; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2726 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2727 | /* Creat the device. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2728 | dev = new_device("block", VIRTIO_ID_BLOCK); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2729 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2730 | /* The device has one virtqueue, where the Guest places requests. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2731 | add_virtqueue(dev, VIRTQUEUE_NUM, blk_request); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2732 | |
| 2733 | /* Allocate the room for our own bookkeeping */ |
| 2734 | vblk = dev->priv = malloc(sizeof(*vblk)); |
| 2735 | |
| 2736 | /* First we open the file and store the length. */ |
| 2737 | vblk->fd = open_or_die(filename, O_RDWR|O_LARGEFILE); |
| 2738 | vblk->len = lseek64(vblk->fd, 0, SEEK_END); |
| 2739 | |
Tejun Heo | 7bc9fdd | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 2740 | /* We support FLUSH. */ |
| 2741 | add_feature(dev, VIRTIO_BLK_F_FLUSH); |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2742 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2743 | /* Tell Guest how many sectors this device has. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2744 | conf.capacity = cpu_to_le64(vblk->len / 512); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2745 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2746 | /* |
| 2747 | * Tell Guest not to put in too many descriptors at once: two are used |
| 2748 | * for the in and out elements. |
| 2749 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2750 | add_feature(dev, VIRTIO_BLK_F_SEG_MAX); |
| 2751 | conf.seg_max = cpu_to_le32(VIRTQUEUE_NUM - 2); |
| 2752 | |
Rusty Russell | 8ef562d | 2009-07-30 16:03:43 -0600 | [diff] [blame] | 2753 | /* Don't try to put whole struct: we have 8 bit limit. */ |
| 2754 | set_config(dev, offsetof(struct virtio_blk_config, geometry), &conf); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2755 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2756 | verbose("device %u: virtblock %llu sectors\n", |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2757 | ++devices.device_num, le64_to_cpu(conf.capacity)); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2758 | } |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2759 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2760 | /*L:211 |
Rusty Russell | a454bb3 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2761 | * Our random number generator device reads from /dev/urandom into the Guest's |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2762 | * input buffers. The usual case is that the Guest doesn't want random numbers |
Rusty Russell | a454bb3 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2763 | * and so has no buffers although /dev/urandom is still readable, whereas |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2764 | * console is the reverse. |
| 2765 | * |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2766 | * The same logic applies, however. |
| 2767 | */ |
| 2768 | struct rng_info { |
| 2769 | int rfd; |
| 2770 | }; |
| 2771 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2772 | static void rng_input(struct virtqueue *vq) |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2773 | { |
| 2774 | int len; |
| 2775 | unsigned int head, in_num, out_num, totlen = 0; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2776 | struct rng_info *rng_info = vq->dev->priv; |
| 2777 | struct iovec iov[vq->vring.num]; |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2778 | |
| 2779 | /* First we need a buffer from the Guests's virtqueue. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2780 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2781 | if (out_num) |
| 2782 | errx(1, "Output buffers in rng?"); |
| 2783 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2784 | /* |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2785 | * Just like the console write, we loop to cover the whole iovec. |
| 2786 | * In this case, short reads actually happen quite a bit. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2787 | */ |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2788 | while (!iov_empty(iov, in_num)) { |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2789 | len = readv(rng_info->rfd, iov, in_num); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2790 | if (len <= 0) |
Rusty Russell | a454bb3 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2791 | err(1, "Read from /dev/urandom gave %i", len); |
Rusty Russell | c0316a9 | 2012-10-16 23:56:13 +1030 | [diff] [blame] | 2792 | iov_consume(iov, in_num, NULL, len); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2793 | totlen += len; |
| 2794 | } |
| 2795 | |
| 2796 | /* Tell the Guest about the new input. */ |
Rusty Russell | 38bc2b8 | 2009-06-12 22:27:11 -0600 | [diff] [blame] | 2797 | add_used(vq, head, totlen); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2798 | } |
| 2799 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2800 | /*L:199 |
| 2801 | * This creates a "hardware" random number device for the Guest. |
| 2802 | */ |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2803 | static void setup_rng(void) |
| 2804 | { |
| 2805 | struct device *dev; |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2806 | struct rng_info *rng_info = malloc(sizeof(*rng_info)); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2807 | |
Rusty Russell | a454bb3 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2808 | /* Our device's private info simply contains the /dev/urandom fd. */ |
| 2809 | rng_info->rfd = open_or_die("/dev/urandom", O_RDONLY); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2810 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2811 | /* Create the new device. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2812 | dev = new_device("rng", VIRTIO_ID_RNG); |
| 2813 | dev->priv = rng_info; |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2814 | |
| 2815 | /* The device has one virtqueue, where the Guest places inbufs. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2816 | add_virtqueue(dev, VIRTQUEUE_NUM, rng_input); |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2817 | |
| 2818 | verbose("device %u: rng\n", devices.device_num++); |
| 2819 | } |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 2820 | /* That's the end of device setup. */ |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2821 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 2822 | /*L:230 Reboot is pretty easy: clean up and exec() the Launcher afresh. */ |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2823 | static void __attribute__((noreturn)) restart_guest(void) |
| 2824 | { |
| 2825 | unsigned int i; |
| 2826 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2827 | /* |
| 2828 | * Since we don't track all open fds, we simply close everything beyond |
| 2829 | * stderr. |
| 2830 | */ |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2831 | for (i = 3; i < FD_SETSIZE; i++) |
| 2832 | close(i); |
Rusty Russell | 8c79873 | 2008-07-29 09:58:38 -0500 | [diff] [blame] | 2833 | |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2834 | /* Reset all the devices (kills all threads). */ |
| 2835 | cleanup_devices(); |
| 2836 | |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2837 | execv(main_args[0], main_args); |
| 2838 | err(1, "Could not exec %s", main_args[0]); |
| 2839 | } |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2840 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2841 | /*L:220 |
| 2842 | * Finally we reach the core of the Launcher which runs the Guest, serves |
| 2843 | * its input and output, and finally, lays it to rest. |
| 2844 | */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 2845 | static void __attribute__((noreturn)) run_guest(void) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2846 | { |
| 2847 | for (;;) { |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2848 | struct lguest_pending notify; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2849 | int readval; |
| 2850 | |
| 2851 | /* We read from the /dev/lguest device to run the Guest. */ |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2852 | readval = pread(lguest_fd, ¬ify, sizeof(notify), cpu_id); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2853 | |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2854 | /* One unsigned long means the Guest did HCALL_NOTIFY */ |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2855 | if (readval == sizeof(notify)) { |
| 2856 | if (notify.trap == 0x1F) { |
| 2857 | verbose("Notify on address %#08x\n", |
| 2858 | notify.addr); |
| 2859 | handle_output(notify.addr); |
Rusty Russell | c565650b | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 2860 | } else if (notify.trap == 13) { |
| 2861 | verbose("Emulating instruction at %#x\n", |
| 2862 | getreg(eip)); |
| 2863 | emulate_insn(notify.insn); |
Rusty Russell | 6a54f9a | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 2864 | } else if (notify.trap == 14) { |
| 2865 | verbose("Emulating MMIO at %#x\n", |
| 2866 | getreg(eip)); |
| 2867 | emulate_mmio(notify.addr, notify.insn); |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 2868 | } else |
| 2869 | errx(1, "Unknown trap %i addr %#08x\n", |
| 2870 | notify.trap, notify.addr); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2871 | /* ENOENT means the Guest died. Reading tells us why. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2872 | } else if (errno == ENOENT) { |
| 2873 | char reason[1024] = { 0 }; |
Glauber de Oliveira Costa | e3283fa | 2008-01-07 11:05:23 -0200 | [diff] [blame] | 2874 | pread(lguest_fd, reason, sizeof(reason)-1, cpu_id); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2875 | errx(1, "%s", reason); |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2876 | /* ERESTART means that we need to reboot the guest */ |
| 2877 | } else if (errno == ERESTART) { |
| 2878 | restart_guest(); |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2879 | /* Anything else means a bug or incompatible change. */ |
| 2880 | } else |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2881 | err(1, "Running guest failed"); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2882 | } |
| 2883 | } |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 2884 | /*L:240 |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2885 | * This is the end of the Launcher. The good news: we are over halfway |
| 2886 | * through! The bad news: the most fiendish part of the code still lies ahead |
| 2887 | * of us. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2888 | * |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 2889 | * Are you ready? Take a deep breath and join me in the core of the Host, in |
| 2890 | * "make Host". |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2891 | :*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2892 | |
| 2893 | static struct option opts[] = { |
| 2894 | { "verbose", 0, NULL, 'v' }, |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2895 | { "tunnet", 1, NULL, 't' }, |
| 2896 | { "block", 1, NULL, 'b' }, |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2897 | { "rng", 0, NULL, 'r' }, |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2898 | { "initrd", 1, NULL, 'i' }, |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 2899 | { "username", 1, NULL, 'u' }, |
| 2900 | { "chroot", 1, NULL, 'c' }, |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2901 | { NULL }, |
| 2902 | }; |
| 2903 | static void usage(void) |
| 2904 | { |
| 2905 | errx(1, "Usage: lguest [--verbose] " |
Mark McLoughlin | dec6a2b | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2906 | "[--tunnet=(<ipaddr>:<macaddr>|bridge:<bridgename>:<macaddr>)\n" |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2907 | "|--block=<filename>|--initrd=<filename>]...\n" |
| 2908 | "<mem-in-mb> vmlinux [args...]"); |
| 2909 | } |
| 2910 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 2911 | /*L:105 The main routine is where the real work begins: */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2912 | int main(int argc, char *argv[]) |
| 2913 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2914 | /* Memory, code startpoint and size of the (optional) initrd. */ |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 2915 | unsigned long mem = 0, start, initrd_size = 0; |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 2916 | /* Two temporaries. */ |
| 2917 | int i, c; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 2918 | /* The boot information for the Guest. */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 2919 | struct boot_params *boot; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2920 | /* If they specify an initrd file to load. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2921 | const char *initrd_name = NULL; |
| 2922 | |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 2923 | /* Password structure for initgroups/setres[gu]id */ |
| 2924 | struct passwd *user_details = NULL; |
| 2925 | |
| 2926 | /* Directory to chroot to */ |
| 2927 | char *chroot_path = NULL; |
| 2928 | |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2929 | /* Save the args: we "reboot" by execing ourselves again. */ |
| 2930 | main_args = argv; |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 2931 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2932 | /* |
| 2933 | * First we initialize the device list. We keep a pointer to the last |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 2934 | * device, and the next interrupt number to use for devices (1: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2935 | * remember that 0 is used by the timer). |
| 2936 | */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 2937 | devices.lastdev = NULL; |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2938 | devices.next_irq = 1; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2939 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2940 | /* We're CPU 0. In fact, that's the only CPU possible right now. */ |
Glauber de Oliveira Costa | e3283fa | 2008-01-07 11:05:23 -0200 | [diff] [blame] | 2941 | cpu_id = 0; |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2942 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2943 | /* |
| 2944 | * We need to know how much memory so we can set up the device |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2945 | * descriptor and memory pages for the devices as we parse the command |
| 2946 | * line. So we quickly look through the arguments to find the amount |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2947 | * of memory now. |
| 2948 | */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 2949 | for (i = 1; i < argc; i++) { |
| 2950 | if (argv[i][0] != '-') { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 2951 | mem = atoi(argv[i]) * 1024 * 1024; |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2952 | /* |
| 2953 | * We start by mapping anonymous pages over all of |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 2954 | * guest-physical memory range. This fills it with 0, |
| 2955 | * and ensures that the Guest won't be killed when it |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2956 | * tries to access it. |
| 2957 | */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 2958 | guest_base = map_zeroed_pages(mem / getpagesize() |
| 2959 | + DEVICE_PAGES); |
| 2960 | guest_limit = mem; |
Rusty Russell | 0a6bcc1 | 2015-02-11 15:15:11 +1030 | [diff] [blame] | 2961 | guest_max = guest_mmio = mem + DEVICE_PAGES*getpagesize(); |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2962 | devices.descpage = get_pages(1); |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 2963 | break; |
| 2964 | } |
| 2965 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 2966 | |
| 2967 | /* The options are fairly straight-forward */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2968 | while ((c = getopt_long(argc, argv, "v", opts, NULL)) != EOF) { |
| 2969 | switch (c) { |
| 2970 | case 'v': |
| 2971 | verbose = true; |
| 2972 | break; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2973 | case 't': |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2974 | setup_tun_net(optarg); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2975 | break; |
| 2976 | case 'b': |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 2977 | setup_block_file(optarg); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2978 | break; |
Rusty Russell | 28fd6d7 | 2008-07-29 09:58:33 -0500 | [diff] [blame] | 2979 | case 'r': |
| 2980 | setup_rng(); |
| 2981 | break; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2982 | case 'i': |
| 2983 | initrd_name = optarg; |
| 2984 | break; |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 2985 | case 'u': |
| 2986 | user_details = getpwnam(optarg); |
| 2987 | if (!user_details) |
| 2988 | err(1, "getpwnam failed, incorrect username?"); |
| 2989 | break; |
| 2990 | case 'c': |
| 2991 | chroot_path = optarg; |
| 2992 | break; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 2993 | default: |
| 2994 | warnx("Unknown argument %s", argv[optind]); |
| 2995 | usage(); |
| 2996 | } |
| 2997 | } |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 2998 | /* |
| 2999 | * After the other arguments we expect memory and kernel image name, |
| 3000 | * followed by command line arguments for the kernel. |
| 3001 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3002 | if (optind + 2 > argc) |
| 3003 | usage(); |
| 3004 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 3005 | verbose("Guest base is at %p\n", guest_base); |
| 3006 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3007 | /* We always have a console device */ |
Rusty Russell | 17cbca2 | 2007-10-22 11:24:22 +1000 | [diff] [blame] | 3008 | setup_console(); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3009 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3010 | /* Now we load the kernel */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 3011 | start = load_kernel(open_or_die(argv[optind+1], O_RDONLY)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3012 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 3013 | /* Boot information is stashed at physical address 0 */ |
| 3014 | boot = from_guest_phys(0); |
| 3015 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3016 | /* Map the initrd image if requested (at top of physical memory) */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3017 | if (initrd_name) { |
| 3018 | initrd_size = load_initrd(initrd_name, mem); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 3019 | /* |
| 3020 | * These are the location in the Linux boot header where the |
| 3021 | * start and size of the initrd are expected to be found. |
| 3022 | */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3023 | boot->hdr.ramdisk_image = mem - initrd_size; |
| 3024 | boot->hdr.ramdisk_size = initrd_size; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3025 | /* The bootloader type 0xFF means "unknown"; that's OK. */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3026 | boot->hdr.type_of_loader = 0xFF; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 3029 | /* |
| 3030 | * The Linux boot header contains an "E820" memory map: ours is a |
| 3031 | * simple, single region. |
| 3032 | */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3033 | boot->e820_entries = 1; |
| 3034 | boot->e820_map[0] = ((struct e820entry) { 0, mem, E820_RAM }); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 3035 | /* |
| 3036 | * The boot header contains a command line pointer: we put the command |
| 3037 | * line after the boot header. |
| 3038 | */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3039 | boot->hdr.cmd_line_ptr = to_guest_phys(boot + 1); |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 3040 | /* We use a simple helper to copy the arguments separated by spaces. */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3041 | concat((char *)(boot + 1), argv+optind+2); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3042 | |
Rusty Russell | e22a539 | 2011-08-15 10:15:10 +0930 | [diff] [blame] | 3043 | /* Set kernel alignment to 16M (CONFIG_PHYSICAL_ALIGN) */ |
| 3044 | boot->hdr.kernel_alignment = 0x1000000; |
| 3045 | |
Rusty Russell | 814a0e5 | 2007-10-22 11:29:44 +1000 | [diff] [blame] | 3046 | /* Boot protocol version: 2.07 supports the fields for lguest. */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3047 | boot->hdr.version = 0x207; |
Rusty Russell | 814a0e5 | 2007-10-22 11:29:44 +1000 | [diff] [blame] | 3048 | |
| 3049 | /* The hardware_subarch value of "1" tells the Guest it's an lguest. */ |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3050 | boot->hdr.hardware_subarch = 1; |
Rusty Russell | 814a0e5 | 2007-10-22 11:29:44 +1000 | [diff] [blame] | 3051 | |
Rusty Russell | 43d33b2 | 2007-10-22 11:29:57 +1000 | [diff] [blame] | 3052 | /* Tell the entry path not to try to reload segment registers. */ |
| 3053 | boot->hdr.loadflags |= KEEP_SEGMENTS; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3054 | |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 3055 | /* We tell the kernel to initialize the Guest. */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 3056 | tell_kernel(start); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3057 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 3058 | /* Ensure that we terminate if a device-servicing child dies. */ |
Rusty Russell | 659a0e6 | 2009-06-12 22:27:10 -0600 | [diff] [blame] | 3059 | signal(SIGCHLD, kill_launcher); |
| 3060 | |
| 3061 | /* If we exit via err(), this kills all the threads, restores tty. */ |
| 3062 | atexit(cleanup_devices); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3063 | |
Philip Sanderson | 8aeb36e | 2011-01-20 21:37:28 -0600 | [diff] [blame] | 3064 | /* If requested, chroot to a directory */ |
| 3065 | if (chroot_path) { |
| 3066 | if (chroot(chroot_path) != 0) |
| 3067 | err(1, "chroot(\"%s\") failed", chroot_path); |
| 3068 | |
| 3069 | if (chdir("/") != 0) |
| 3070 | err(1, "chdir(\"/\") failed"); |
| 3071 | |
| 3072 | verbose("chroot done\n"); |
| 3073 | } |
| 3074 | |
| 3075 | /* If requested, drop privileges */ |
| 3076 | if (user_details) { |
| 3077 | uid_t u; |
| 3078 | gid_t g; |
| 3079 | |
| 3080 | u = user_details->pw_uid; |
| 3081 | g = user_details->pw_gid; |
| 3082 | |
| 3083 | if (initgroups(user_details->pw_name, g) != 0) |
| 3084 | err(1, "initgroups failed"); |
| 3085 | |
| 3086 | if (setresgid(g, g, g) != 0) |
| 3087 | err(1, "setresgid failed"); |
| 3088 | |
| 3089 | if (setresuid(u, u, u) != 0) |
| 3090 | err(1, "setresuid failed"); |
| 3091 | |
| 3092 | verbose("Dropping privileges completed\n"); |
| 3093 | } |
| 3094 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 3095 | /* Finally, run the Guest. This doesn't return. */ |
Rusty Russell | 56739c80 | 2009-06-12 22:26:59 -0600 | [diff] [blame] | 3096 | run_guest(); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 3097 | } |
Rusty Russell | f56a384 | 2007-07-26 10:41:05 -0700 | [diff] [blame] | 3098 | /*:*/ |
| 3099 | |
| 3100 | /*M:999 |
| 3101 | * Mastery is done: you now know everything I do. |
| 3102 | * |
| 3103 | * But surely you have seen code, features and bugs in your wanderings which |
| 3104 | * you now yearn to attack? That is the real game, and I look forward to you |
| 3105 | * patching and forking lguest into the Your-Name-Here-visor. |
| 3106 | * |
| 3107 | * Farewell, and good coding! |
| 3108 | * Rusty Russell. |
| 3109 | */ |