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