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