Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 1 | /*P:100 This is the Launcher code, a simple program which lays out the |
| 2 | * "physical" memory for the new Guest by mapping the kernel image and the |
| 3 | * virtual devices, then reads repeatedly from /dev/lguest to run the Guest. |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 4 | :*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 5 | #define _LARGEFILE64_SOURCE |
| 6 | #define _GNU_SOURCE |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | #include <err.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <elf.h> |
| 14 | #include <sys/mman.h> |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 15 | #include <sys/param.h> |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/wait.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <errno.h> |
| 22 | #include <ctype.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <time.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <net/if.h> |
| 29 | #include <linux/sockios.h> |
| 30 | #include <linux/if_tun.h> |
| 31 | #include <sys/uio.h> |
| 32 | #include <termios.h> |
| 33 | #include <getopt.h> |
| 34 | #include <zlib.h> |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 35 | /*L:110 We can ignore the 28 include files we need for this program, but I do |
| 36 | * want to draw attention to the use of kernel-style types. |
| 37 | * |
| 38 | * As Linus said, "C is a Spartan language, and so should your naming be." I |
| 39 | * like these abbreviations and the header we need uses them, so we define them |
| 40 | * here. |
| 41 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 42 | typedef unsigned long long u64; |
| 43 | typedef uint32_t u32; |
| 44 | typedef uint16_t u16; |
| 45 | typedef uint8_t u8; |
Rusty Russell | b45d8cb | 2007-10-22 10:56:24 +1000 | [diff] [blame] | 46 | #include "linux/lguest_launcher.h" |
| 47 | #include "asm-x86/e820.h" |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 48 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 49 | |
| 50 | #define PAGE_PRESENT 0x7 /* Present, RW, Execute */ |
| 51 | #define NET_PEERNUM 1 |
| 52 | #define BRIDGE_PFX "bridge:" |
| 53 | #ifndef SIOCBRADDIF |
| 54 | #define SIOCBRADDIF 0x89a2 /* add interface to bridge */ |
| 55 | #endif |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 56 | /* We can have up to 256 pages for devices. */ |
| 57 | #define DEVICE_PAGES 256 |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 58 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 59 | /*L:120 verbose is both a global flag and a macro. The C preprocessor allows |
| 60 | * this, and although I wouldn't recommend it, it works quite nicely here. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 61 | static bool verbose; |
| 62 | #define verbose(args...) \ |
| 63 | do { if (verbose) printf(args); } while(0) |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 64 | /*:*/ |
| 65 | |
| 66 | /* The pipe to send commands to the waker process */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 67 | static int waker_fd; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 68 | /* The pointer to the start of guest memory. */ |
| 69 | static void *guest_base; |
| 70 | /* The maximum guest physical address allowed, and maximum possible. */ |
| 71 | static unsigned long guest_limit, guest_max; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 72 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 73 | /* This is our list of devices. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 74 | struct device_list |
| 75 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 76 | /* Summary information about the devices in our list: ready to pass to |
| 77 | * select() to ask which need servicing.*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 78 | fd_set infds; |
| 79 | int max_infd; |
| 80 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 81 | /* The descriptor page for the devices. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 82 | struct lguest_device_desc *descs; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 83 | |
| 84 | /* A single linked list of devices. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 85 | struct device *dev; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 86 | /* ... And an end pointer so we can easily append new devices */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 87 | struct device **lastdev; |
| 88 | }; |
| 89 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 90 | /* The device structure describes a single device. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 91 | struct device |
| 92 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 93 | /* The linked-list pointer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 94 | struct device *next; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 95 | /* The descriptor for this device, as mapped into the Guest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 96 | struct lguest_device_desc *desc; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 97 | /* The memory page(s) of this device, if any. Also mapped in Guest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 98 | void *mem; |
| 99 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 100 | /* If handle_input is set, it wants to be called when this file |
| 101 | * descriptor is ready. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 102 | int fd; |
| 103 | bool (*handle_input)(int fd, struct device *me); |
| 104 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 105 | /* If handle_output is set, it wants to be called when the Guest sends |
| 106 | * DMA to this key. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 107 | unsigned long watch_key; |
| 108 | u32 (*handle_output)(int fd, const struct iovec *iov, |
| 109 | unsigned int num, struct device *me); |
| 110 | |
| 111 | /* Device-specific data. */ |
| 112 | void *priv; |
| 113 | }; |
| 114 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 115 | /*L:100 The Launcher code itself takes us out into userspace, that scary place |
| 116 | * where pointers run wild and free! Unfortunately, like most userspace |
| 117 | * programs, it's quite boring (which is why everyone likes to hack on the |
| 118 | * kernel!). Perhaps if you make up an Lguest Drinking Game at this point, it |
| 119 | * will get you through this section. Or, maybe not. |
| 120 | * |
| 121 | * The Launcher sets up a big chunk of memory to be the Guest's "physical" |
| 122 | * memory and stores it in "guest_base". In other words, Guest physical == |
| 123 | * Launcher virtual with an offset. |
| 124 | * |
| 125 | * This can be tough to get your head around, but usually it just means that we |
| 126 | * use these trivial conversion functions when the Guest gives us it's |
| 127 | * "physical" addresses: */ |
| 128 | static void *from_guest_phys(unsigned long addr) |
| 129 | { |
| 130 | return guest_base + addr; |
| 131 | } |
| 132 | |
| 133 | static unsigned long to_guest_phys(const void *addr) |
| 134 | { |
| 135 | return (addr - guest_base); |
| 136 | } |
| 137 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 138 | /*L:130 |
| 139 | * Loading the Kernel. |
| 140 | * |
| 141 | * We start with couple of simple helper routines. open_or_die() avoids |
| 142 | * error-checking code cluttering the callers: */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 143 | static int open_or_die(const char *name, int flags) |
| 144 | { |
| 145 | int fd = open(name, flags); |
| 146 | if (fd < 0) |
| 147 | err(1, "Failed to open %s", name); |
| 148 | return fd; |
| 149 | } |
| 150 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 151 | /* map_zeroed_pages() takes a number of pages. */ |
| 152 | static void *map_zeroed_pages(unsigned int num) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 153 | { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 154 | int fd = open_or_die("/dev/zero", O_RDONLY); |
| 155 | void *addr; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 156 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 157 | /* We use a private mapping (ie. if we write to the page, it will be |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 158 | * copied). */ |
| 159 | addr = mmap(NULL, getpagesize() * num, |
| 160 | PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0); |
| 161 | if (addr == MAP_FAILED) |
| 162 | err(1, "Mmaping %u pages of /dev/zero", num); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 163 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 164 | return addr; |
| 165 | } |
| 166 | |
| 167 | /* Get some more pages for a device. */ |
| 168 | static void *get_pages(unsigned int num) |
| 169 | { |
| 170 | void *addr = from_guest_phys(guest_limit); |
| 171 | |
| 172 | guest_limit += num * getpagesize(); |
| 173 | if (guest_limit > guest_max) |
| 174 | errx(1, "Not enough memory for devices"); |
| 175 | return addr; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 178 | /* To find out where to start we look for the magic Guest string, which marks |
| 179 | * the code we see in lguest_asm.S. This is a hack which we are currently |
| 180 | * plotting to replace with the normal Linux entry point. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 181 | static unsigned long entry_point(const void *start, const void *end, |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 182 | unsigned long page_offset) |
| 183 | { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 184 | const void *p; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 185 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 186 | /* The scan gives us the physical starting address. We want the |
| 187 | * virtual address in this case, and fortunately, we already figured |
| 188 | * out the physical-virtual difference and passed it here in |
| 189 | * "page_offset". */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 190 | for (p = start; p < end; p++) |
| 191 | if (memcmp(p, "GenuineLguest", strlen("GenuineLguest")) == 0) |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 192 | return to_guest_phys(p + strlen("GenuineLguest")) |
| 193 | + page_offset; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 194 | |
Glauber de Oliveira Costa | babed5c | 2007-10-22 10:56:21 +1000 | [diff] [blame] | 195 | errx(1, "Is this image a genuine lguest?"); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 198 | /* This routine is used to load the kernel or initrd. It tries mmap, but if |
| 199 | * that fails (Plan 9's kernel file isn't nicely aligned on page boundaries), |
| 200 | * it falls back to reading the memory in. */ |
| 201 | static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) |
| 202 | { |
| 203 | ssize_t r; |
| 204 | |
| 205 | /* We map writable even though for some segments are marked read-only. |
| 206 | * The kernel really wants to be writable: it patches its own |
| 207 | * instructions. |
| 208 | * |
| 209 | * MAP_PRIVATE means that the page won't be copied until a write is |
| 210 | * done to it. This allows us to share untouched memory between |
| 211 | * Guests. */ |
| 212 | if (mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC, |
| 213 | MAP_FIXED|MAP_PRIVATE, fd, offset) != MAP_FAILED) |
| 214 | return; |
| 215 | |
| 216 | /* pread does a seek and a read in one shot: saves a few lines. */ |
| 217 | r = pread(fd, addr, len, offset); |
| 218 | if (r != len) |
| 219 | err(1, "Reading offset %lu len %lu gave %zi", offset, len, r); |
| 220 | } |
| 221 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 222 | /* This routine takes an open vmlinux image, which is in ELF, and maps it into |
| 223 | * the Guest memory. ELF = Embedded Linking Format, which is the format used |
| 224 | * by all modern binaries on Linux including the kernel. |
| 225 | * |
| 226 | * The ELF headers give *two* addresses: a physical address, and a virtual |
| 227 | * address. The Guest kernel expects to be placed in memory at the physical |
| 228 | * address, and the page tables set up so it will correspond to that virtual |
| 229 | * address. We return the difference between the virtual and physical |
| 230 | * addresses in the "page_offset" pointer. |
| 231 | * |
| 232 | * We return the starting address. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 233 | static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr, |
| 234 | unsigned long *page_offset) |
| 235 | { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 236 | void *start = (void *)-1, *end = NULL; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 237 | Elf32_Phdr phdr[ehdr->e_phnum]; |
| 238 | unsigned int i; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 239 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 240 | /* Sanity checks on the main ELF header: an x86 executable with a |
| 241 | * reasonable number of correctly-sized program headers. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 242 | if (ehdr->e_type != ET_EXEC |
| 243 | || ehdr->e_machine != EM_386 |
| 244 | || ehdr->e_phentsize != sizeof(Elf32_Phdr) |
| 245 | || ehdr->e_phnum < 1 || ehdr->e_phnum > 65536U/sizeof(Elf32_Phdr)) |
| 246 | errx(1, "Malformed elf header"); |
| 247 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 248 | /* An ELF executable contains an ELF header and a number of "program" |
| 249 | * headers which indicate which parts ("segments") of the program to |
| 250 | * load where. */ |
| 251 | |
| 252 | /* We read in all the program headers at once: */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 253 | if (lseek(elf_fd, ehdr->e_phoff, SEEK_SET) < 0) |
| 254 | err(1, "Seeking to program headers"); |
| 255 | if (read(elf_fd, phdr, sizeof(phdr)) != sizeof(phdr)) |
| 256 | err(1, "Reading program headers"); |
| 257 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 258 | /* We don't know page_offset yet. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 259 | *page_offset = 0; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 260 | |
| 261 | /* Try all the headers: there are usually only three. A read-only one, |
| 262 | * a read-write one, and a "note" section which isn't loadable. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 263 | for (i = 0; i < ehdr->e_phnum; i++) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 264 | /* If this isn't a loadable segment, we ignore it */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 265 | if (phdr[i].p_type != PT_LOAD) |
| 266 | continue; |
| 267 | |
| 268 | verbose("Section %i: size %i addr %p\n", |
| 269 | i, phdr[i].p_memsz, (void *)phdr[i].p_paddr); |
| 270 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 271 | /* We expect a simple linear address space: every segment must |
| 272 | * have the same difference between virtual (p_vaddr) and |
| 273 | * physical (p_paddr) address. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 274 | if (!*page_offset) |
| 275 | *page_offset = phdr[i].p_vaddr - phdr[i].p_paddr; |
| 276 | else if (*page_offset != phdr[i].p_vaddr - phdr[i].p_paddr) |
| 277 | errx(1, "Page offset of section %i different", i); |
| 278 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 279 | /* We track the first and last address we mapped, so we can |
| 280 | * tell entry_point() where to scan. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 281 | if (from_guest_phys(phdr[i].p_paddr) < start) |
| 282 | start = from_guest_phys(phdr[i].p_paddr); |
| 283 | if (from_guest_phys(phdr[i].p_paddr) + phdr[i].p_filesz > end) |
| 284 | end=from_guest_phys(phdr[i].p_paddr)+phdr[i].p_filesz; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 285 | |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 286 | /* We map this section of the file at its physical address. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 287 | map_at(elf_fd, from_guest_phys(phdr[i].p_paddr), |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 288 | phdr[i].p_offset, phdr[i].p_filesz); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 291 | return entry_point(start, end, *page_offset); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 294 | /*L:170 Prepare to be SHOCKED and AMAZED. And possibly a trifle nauseated. |
| 295 | * |
| 296 | * We know that CONFIG_PAGE_OFFSET sets what virtual address the kernel expects |
| 297 | * to be. We don't know what that option was, but we can figure it out |
| 298 | * approximately by looking at the addresses in the code. I chose the common |
| 299 | * case of reading a memory location into the %eax register: |
| 300 | * |
| 301 | * movl <some-address>, %eax |
| 302 | * |
| 303 | * This gets encoded as five bytes: "0xA1 <4-byte-address>". For example, |
| 304 | * "0xA1 0x18 0x60 0x47 0xC0" reads the address 0xC0476018 into %eax. |
| 305 | * |
| 306 | * In this example can guess that the kernel was compiled with |
| 307 | * CONFIG_PAGE_OFFSET set to 0xC0000000 (it's always a round number). If the |
| 308 | * kernel were larger than 16MB, we might see 0xC1 addresses show up, but our |
| 309 | * kernel isn't that bloated yet. |
| 310 | * |
| 311 | * Unfortunately, x86 has variable-length instructions, so finding this |
| 312 | * particular instruction properly involves writing a disassembler. Instead, |
| 313 | * we rely on statistics. We look for "0xA1" and tally the different bytes |
| 314 | * which occur 4 bytes later (the "0xC0" in our example above). When one of |
| 315 | * those bytes appears three times, we can be reasonably confident that it |
| 316 | * forms the start of CONFIG_PAGE_OFFSET. |
| 317 | * |
| 318 | * This is amazingly reliable. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 319 | static unsigned long intuit_page_offset(unsigned char *img, unsigned long len) |
| 320 | { |
| 321 | unsigned int i, possibilities[256] = { 0 }; |
| 322 | |
| 323 | for (i = 0; i + 4 < len; i++) { |
| 324 | /* mov 0xXXXXXXXX,%eax */ |
| 325 | if (img[i] == 0xA1 && ++possibilities[img[i+4]] > 3) |
| 326 | return (unsigned long)img[i+4] << 24; |
| 327 | } |
| 328 | errx(1, "could not determine page offset"); |
| 329 | } |
| 330 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 331 | /*L:160 Unfortunately the entire ELF image isn't compressed: the segments |
| 332 | * which need loading are extracted and compressed raw. This denies us the |
| 333 | * information we need to make a fully-general loader. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 334 | static unsigned long unpack_bzimage(int fd, unsigned long *page_offset) |
| 335 | { |
| 336 | gzFile f; |
| 337 | int ret, len = 0; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 338 | /* A bzImage always gets loaded at physical address 1M. This is |
| 339 | * actually configurable as CONFIG_PHYSICAL_START, but as the comment |
| 340 | * there says, "Don't change this unless you know what you are doing". |
| 341 | * Indeed. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 342 | void *img = from_guest_phys(0x100000); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 343 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 344 | /* gzdopen takes our file descriptor (carefully placed at the start of |
| 345 | * the GZIP header we found) and returns a gzFile. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 346 | f = gzdopen(fd, "rb"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 347 | /* We read it into memory in 64k chunks until we hit the end. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 348 | while ((ret = gzread(f, img + len, 65536)) > 0) |
| 349 | len += ret; |
| 350 | if (ret < 0) |
| 351 | err(1, "reading image from bzImage"); |
| 352 | |
| 353 | verbose("Unpacked size %i addr %p\n", len, img); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 354 | |
| 355 | /* Without the ELF header, we can't tell virtual-physical gap. This is |
| 356 | * CONFIG_PAGE_OFFSET, and people do actually change it. Fortunately, |
| 357 | * I have a clever way of figuring it out from the code itself. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 358 | *page_offset = intuit_page_offset(img, len); |
| 359 | |
| 360 | return entry_point(img, img + len, *page_offset); |
| 361 | } |
| 362 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 363 | /*L:150 A bzImage, unlike an ELF file, is not meant to be loaded. You're |
| 364 | * supposed to jump into it and it will unpack itself. We can't do that |
| 365 | * because the Guest can't run the unpacking code, and adding features to |
| 366 | * lguest kills puppies, so we don't want to. |
| 367 | * |
| 368 | * The bzImage is formed by putting the decompressing code in front of the |
| 369 | * compressed kernel code. So we can simple scan through it looking for the |
| 370 | * first "gzip" header, and start decompressing from there. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 371 | static unsigned long load_bzimage(int fd, unsigned long *page_offset) |
| 372 | { |
| 373 | unsigned char c; |
| 374 | int state = 0; |
| 375 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 376 | /* GZIP header is 0x1F 0x8B <method> <flags>... <compressed-by>. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 377 | while (read(fd, &c, 1) == 1) { |
| 378 | switch (state) { |
| 379 | case 0: |
| 380 | if (c == 0x1F) |
| 381 | state++; |
| 382 | break; |
| 383 | case 1: |
| 384 | if (c == 0x8B) |
| 385 | state++; |
| 386 | else |
| 387 | state = 0; |
| 388 | break; |
| 389 | case 2 ... 8: |
| 390 | state++; |
| 391 | break; |
| 392 | case 9: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 393 | /* Seek back to the start of the gzip header. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 394 | lseek(fd, -10, SEEK_CUR); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 395 | /* One final check: "compressed under UNIX". */ |
| 396 | if (c != 0x03) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 397 | state = -1; |
| 398 | else |
| 399 | return unpack_bzimage(fd, page_offset); |
| 400 | } |
| 401 | } |
| 402 | errx(1, "Could not find kernel in bzImage"); |
| 403 | } |
| 404 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 405 | /*L:140 Loading the kernel is easy when it's a "vmlinux", but most kernels |
| 406 | * come wrapped up in the self-decompressing "bzImage" format. With some funky |
| 407 | * coding, we can load those, too. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 408 | static unsigned long load_kernel(int fd, unsigned long *page_offset) |
| 409 | { |
| 410 | Elf32_Ehdr hdr; |
| 411 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 412 | /* Read in the first few bytes. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 413 | if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
| 414 | err(1, "Reading kernel"); |
| 415 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 416 | /* If it's an ELF file, it starts with "\177ELF" */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 417 | if (memcmp(hdr.e_ident, ELFMAG, SELFMAG) == 0) |
| 418 | return map_elf(fd, &hdr, page_offset); |
| 419 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 420 | /* Otherwise we assume it's a bzImage, and try to unpack it */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 421 | return load_bzimage(fd, page_offset); |
| 422 | } |
| 423 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 424 | /* This is a trivial little helper to align pages. Andi Kleen hated it because |
| 425 | * it calls getpagesize() twice: "it's dumb code." |
| 426 | * |
| 427 | * Kernel guys get really het up about optimization, even when it's not |
| 428 | * necessary. I leave this code as a reaction against that. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 429 | static inline unsigned long page_align(unsigned long addr) |
| 430 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 431 | /* Add upwards and truncate downwards. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 432 | return ((addr + getpagesize()-1) & ~(getpagesize()-1)); |
| 433 | } |
| 434 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 435 | /*L:180 An "initial ram disk" is a disk image loaded into memory along with |
| 436 | * the kernel which the kernel can use to boot from without needing any |
| 437 | * drivers. Most distributions now use this as standard: the initrd contains |
| 438 | * the code to load the appropriate driver modules for the current machine. |
| 439 | * |
| 440 | * Importantly, James Morris works for RedHat, and Fedora uses initrds for its |
| 441 | * kernels. He sent me this (and tells me when I break it). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 442 | static unsigned long load_initrd(const char *name, unsigned long mem) |
| 443 | { |
| 444 | int ifd; |
| 445 | struct stat st; |
| 446 | unsigned long len; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 447 | |
| 448 | ifd = open_or_die(name, O_RDONLY); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 449 | /* fstat() is needed to get the file size. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 450 | if (fstat(ifd, &st) < 0) |
| 451 | err(1, "fstat() on initrd '%s'", name); |
| 452 | |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 453 | /* We map the initrd at the top of memory, but mmap wants it to be |
| 454 | * page-aligned, so we round the size up for that. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 455 | len = page_align(st.st_size); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 456 | map_at(ifd, from_guest_phys(mem - len), 0, st.st_size); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 457 | /* Once a file is mapped, you can close the file descriptor. It's a |
| 458 | * little odd, but quite useful. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 459 | close(ifd); |
Ronald G. Minnich | 6649bb7 | 2007-08-28 14:35:59 -0700 | [diff] [blame] | 460 | 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] | 461 | |
| 462 | /* We return the initrd size. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 463 | return len; |
| 464 | } |
| 465 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 466 | /* Once we know the address the Guest kernel expects, we can construct simple |
| 467 | * linear page tables for all of memory which will get the Guest far enough |
| 468 | * into the boot to create its own. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 469 | * |
| 470 | * We lay them out of the way, just below the initrd (which is why we need to |
| 471 | * know its size). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 472 | static unsigned long setup_pagetables(unsigned long mem, |
| 473 | unsigned long initrd_size, |
| 474 | unsigned long page_offset) |
| 475 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 476 | unsigned long *pgdir, *linear; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 477 | unsigned int mapped_pages, i, linear_pages; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 478 | unsigned int ptes_per_page = getpagesize()/sizeof(void *); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 479 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 480 | /* Ideally we map all physical memory starting at page_offset. |
| 481 | * However, if page_offset is 0xC0000000 we can only map 1G of physical |
| 482 | * (0xC0000000 + 1G overflows). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 483 | if (mem <= -page_offset) |
| 484 | mapped_pages = mem/getpagesize(); |
| 485 | else |
| 486 | mapped_pages = -page_offset/getpagesize(); |
| 487 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 488 | /* Each PTE page can map ptes_per_page pages: how many do we need? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 489 | linear_pages = (mapped_pages + ptes_per_page-1)/ptes_per_page; |
| 490 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 491 | /* We put the toplevel page directory page at the top of memory. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 492 | pgdir = from_guest_phys(mem) - initrd_size - getpagesize(); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 493 | |
| 494 | /* Now we use the next linear_pages pages as pte pages */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 495 | linear = (void *)pgdir - linear_pages*getpagesize(); |
| 496 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 497 | /* Linear mapping is easy: put every page's address into the mapping in |
| 498 | * order. PAGE_PRESENT contains the flags Present, Writable and |
| 499 | * Executable. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 500 | for (i = 0; i < mapped_pages; i++) |
| 501 | linear[i] = ((i * getpagesize()) | PAGE_PRESENT); |
| 502 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 503 | /* The top level points to the linear page table pages above. The |
| 504 | * entry representing page_offset points to the first one, and they |
| 505 | * continue from there. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 506 | for (i = 0; i < mapped_pages; i += ptes_per_page) { |
| 507 | pgdir[(i + page_offset/getpagesize())/ptes_per_page] |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 508 | = ((to_guest_phys(linear) + i*sizeof(void *)) |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 509 | | PAGE_PRESENT); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 512 | verbose("Linear mapping of %u pages in %u pte pages at %#lx\n", |
| 513 | mapped_pages, linear_pages, to_guest_phys(linear)); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 514 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 515 | /* We return the top level (guest-physical) address: the kernel needs |
| 516 | * to know where it is. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 517 | return to_guest_phys(pgdir); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 520 | /* Simple routine to roll all the commandline arguments together with spaces |
| 521 | * between them. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 522 | static void concat(char *dst, char *args[]) |
| 523 | { |
| 524 | unsigned int i, len = 0; |
| 525 | |
| 526 | for (i = 0; args[i]; i++) { |
| 527 | strcpy(dst+len, args[i]); |
| 528 | strcat(dst+len, " "); |
| 529 | len += strlen(args[i]) + 1; |
| 530 | } |
| 531 | /* In case it's empty. */ |
| 532 | dst[len] = '\0'; |
| 533 | } |
| 534 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 535 | /* This is where we actually tell the kernel to initialize the Guest. We saw |
| 536 | * the arguments it expects when we looked at initialize() in lguest_user.c: |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 537 | * the base of guest "physical" memory, the top physical page to allow, the |
| 538 | * top level pagetable, the entry point and the page_offset constant for the |
| 539 | * Guest. */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 540 | static int tell_kernel(unsigned long pgdir, unsigned long start, |
| 541 | unsigned long page_offset) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 542 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 543 | unsigned long args[] = { LHREQ_INITIALIZE, |
| 544 | (unsigned long)guest_base, |
| 545 | guest_limit / getpagesize(), |
| 546 | pgdir, start, page_offset }; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 547 | int fd; |
| 548 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 549 | verbose("Guest: %p - %p (%#lx)\n", |
| 550 | guest_base, guest_base + guest_limit, guest_limit); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 551 | fd = open_or_die("/dev/lguest", O_RDWR); |
| 552 | if (write(fd, args, sizeof(args)) < 0) |
| 553 | err(1, "Writing to /dev/lguest"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 554 | |
| 555 | /* We return the /dev/lguest file descriptor to control this Guest */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 556 | return fd; |
| 557 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 558 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 559 | |
| 560 | static void set_fd(int fd, struct device_list *devices) |
| 561 | { |
| 562 | FD_SET(fd, &devices->infds); |
| 563 | if (fd > devices->max_infd) |
| 564 | devices->max_infd = fd; |
| 565 | } |
| 566 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 567 | /*L:200 |
| 568 | * The Waker. |
| 569 | * |
| 570 | * With a console and network devices, we can have lots of input which we need |
| 571 | * to process. We could try to tell the kernel what file descriptors to watch, |
| 572 | * but handing a file descriptor mask through to the kernel is fairly icky. |
| 573 | * |
| 574 | * Instead, we fork off a process which watches the file descriptors and writes |
| 575 | * the LHREQ_BREAK command to the /dev/lguest filedescriptor to tell the Host |
| 576 | * loop to stop running the Guest. This causes it to return from the |
| 577 | * /dev/lguest read with -EAGAIN, where it will write to /dev/lguest to reset |
| 578 | * the LHREQ_BREAK and wake us up again. |
| 579 | * |
| 580 | * This, of course, is merely a different *kind* of icky. |
| 581 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 582 | static void wake_parent(int pipefd, int lguest_fd, struct device_list *devices) |
| 583 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 584 | /* Add the pipe from the Launcher to the fdset in the device_list, so |
| 585 | * we watch it, too. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 586 | set_fd(pipefd, devices); |
| 587 | |
| 588 | for (;;) { |
| 589 | fd_set rfds = devices->infds; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 590 | unsigned long args[] = { LHREQ_BREAK, 1 }; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 591 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 592 | /* Wait until input is ready from one of the devices. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 593 | select(devices->max_infd+1, &rfds, NULL, NULL, NULL); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 594 | /* Is it a message from the Launcher? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 595 | if (FD_ISSET(pipefd, &rfds)) { |
| 596 | int ignorefd; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 597 | /* If read() returns 0, it means the Launcher has |
| 598 | * exited. We silently follow. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 599 | if (read(pipefd, &ignorefd, sizeof(ignorefd)) == 0) |
| 600 | exit(0); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 601 | /* Otherwise it's telling us there's a problem with one |
| 602 | * of the devices, and we should ignore that file |
| 603 | * descriptor from now on. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 604 | FD_CLR(ignorefd, &devices->infds); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 605 | } else /* Send LHREQ_BREAK command. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 606 | write(lguest_fd, args, sizeof(args)); |
| 607 | } |
| 608 | } |
| 609 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 610 | /* This routine just sets up a pipe to the Waker process. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 611 | static int setup_waker(int lguest_fd, struct device_list *device_list) |
| 612 | { |
| 613 | int pipefd[2], child; |
| 614 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 615 | /* We create a pipe to talk to the waker, and also so it knows when the |
| 616 | * Launcher dies (and closes pipe). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 617 | pipe(pipefd); |
| 618 | child = fork(); |
| 619 | if (child == -1) |
| 620 | err(1, "forking"); |
| 621 | |
| 622 | if (child == 0) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 623 | /* Close the "writing" end of our copy of the pipe */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 624 | close(pipefd[1]); |
| 625 | wake_parent(pipefd[0], lguest_fd, device_list); |
| 626 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 627 | /* Close the reading end of our copy of the pipe. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 628 | close(pipefd[0]); |
| 629 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 630 | /* Here is the fd used to talk to the waker. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 631 | return pipefd[1]; |
| 632 | } |
| 633 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 634 | /*L:210 |
| 635 | * Device Handling. |
| 636 | * |
| 637 | * When the Guest sends DMA to us, it sends us an array of addresses and sizes. |
| 638 | * We need to make sure it's not trying to reach into the Launcher itself, so |
| 639 | * we have a convenient routine which check it and exits with an error message |
| 640 | * if something funny is going on: |
| 641 | */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 642 | static void *_check_pointer(unsigned long addr, unsigned int size, |
| 643 | unsigned int line) |
| 644 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 645 | /* We have to separately check addr and addr+size, because size could |
| 646 | * be huge and addr + size might wrap around. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 647 | if (addr >= guest_limit || addr + size >= guest_limit) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 648 | errx(1, "%s:%i: Invalid address %li", __FILE__, line, addr); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 649 | /* We return a pointer for the caller's convenience, now we know it's |
| 650 | * safe to use. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 651 | return from_guest_phys(addr); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 652 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 653 | /* A macro which transparently hands the line number to the real function. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 654 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) |
| 655 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 656 | /* The Guest has given us the address of a "struct lguest_dma". We check it's |
| 657 | * OK and convert it to an iovec (which is a simple array of ptr/size |
| 658 | * pairs). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 659 | static u32 *dma2iov(unsigned long dma, struct iovec iov[], unsigned *num) |
| 660 | { |
| 661 | unsigned int i; |
| 662 | struct lguest_dma *udma; |
| 663 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 664 | /* First we make sure that the array memory itself is valid. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 665 | udma = check_pointer(dma, sizeof(*udma)); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 666 | /* Now we check each element */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 667 | for (i = 0; i < LGUEST_MAX_DMA_SECTIONS; i++) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 668 | /* A zero length ends the array. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 669 | if (!udma->len[i]) |
| 670 | break; |
| 671 | |
| 672 | iov[i].iov_base = check_pointer(udma->addr[i], udma->len[i]); |
| 673 | iov[i].iov_len = udma->len[i]; |
| 674 | } |
| 675 | *num = i; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 676 | |
| 677 | /* We return the pointer to where the caller should write the amount of |
| 678 | * the buffer used. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 679 | return &udma->used_len; |
| 680 | } |
| 681 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 682 | /* This routine gets a DMA buffer from the Guest for a given key, and converts |
| 683 | * it to an iovec array. It returns the interrupt the Guest wants when we're |
| 684 | * finished, and a pointer to the "used_len" field to fill in. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 685 | static u32 *get_dma_buffer(int fd, void *key, |
| 686 | struct iovec iov[], unsigned int *num, u32 *irq) |
| 687 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 688 | unsigned long buf[] = { LHREQ_GETDMA, to_guest_phys(key) }; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 689 | unsigned long udma; |
| 690 | u32 *res; |
| 691 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 692 | /* Ask the kernel for a DMA buffer corresponding to this key. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 693 | udma = write(fd, buf, sizeof(buf)); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 694 | /* They haven't registered any, or they're all used? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 695 | if (udma == (unsigned long)-1) |
| 696 | return NULL; |
| 697 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 698 | /* Convert it into our iovec array */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 699 | res = dma2iov(udma, iov, num); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 700 | /* The kernel stashes irq in ->used_len to get it out to us. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 701 | *irq = *res; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 702 | /* Return a pointer to ((struct lguest_dma *)udma)->used_len. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 703 | return res; |
| 704 | } |
| 705 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 706 | /* This is a convenient routine to send the Guest an interrupt. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 707 | static void trigger_irq(int fd, u32 irq) |
| 708 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 709 | unsigned long buf[] = { LHREQ_IRQ, irq }; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 710 | if (write(fd, buf, sizeof(buf)) != 0) |
| 711 | err(1, "Triggering irq %i", irq); |
| 712 | } |
| 713 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 714 | /* This simply sets up an iovec array where we can put data to be discarded. |
| 715 | * This happens when the Guest doesn't want or can't handle the input: we have |
| 716 | * to get rid of it somewhere, and if we bury it in the ceiling space it will |
| 717 | * start to smell after a week. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 718 | static void discard_iovec(struct iovec *iov, unsigned int *num) |
| 719 | { |
| 720 | static char discard_buf[1024]; |
| 721 | *num = 1; |
| 722 | iov->iov_base = discard_buf; |
| 723 | iov->iov_len = sizeof(discard_buf); |
| 724 | } |
| 725 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 726 | /* Here is the input terminal setting we save, and the routine to restore them |
| 727 | * on exit so the user can see what they type next. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 728 | static struct termios orig_term; |
| 729 | static void restore_term(void) |
| 730 | { |
| 731 | tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); |
| 732 | } |
| 733 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 734 | /* We associate some data with the console for our exit hack. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 735 | struct console_abort |
| 736 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 737 | /* How many times have they hit ^C? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 738 | int count; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 739 | /* When did they start? */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 740 | struct timeval start; |
| 741 | }; |
| 742 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 743 | /* This is the routine which handles console input (ie. stdin). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 744 | static bool handle_console_input(int fd, struct device *dev) |
| 745 | { |
| 746 | u32 irq = 0, *lenp; |
| 747 | int len; |
| 748 | unsigned int num; |
| 749 | struct iovec iov[LGUEST_MAX_DMA_SECTIONS]; |
| 750 | struct console_abort *abort = dev->priv; |
| 751 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 752 | /* First we get the console buffer from the Guest. The key is dev->mem |
| 753 | * which was set to 0 in setup_console(). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 754 | lenp = get_dma_buffer(fd, dev->mem, iov, &num, &irq); |
| 755 | if (!lenp) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 756 | /* If it's not ready for input, warn and set up to discard. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 757 | warn("console: no dma buffer!"); |
| 758 | discard_iovec(iov, &num); |
| 759 | } |
| 760 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 761 | /* This is why we convert to iovecs: the readv() call uses them, and so |
| 762 | * it reads straight into the Guest's buffer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 763 | len = readv(dev->fd, iov, num); |
| 764 | if (len <= 0) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 765 | /* This implies that the console is closed, is /dev/null, or |
| 766 | * something went terribly wrong. We still go through the rest |
| 767 | * of the logic, though, especially the exit handling below. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 768 | warnx("Failed to get console input, ignoring console."); |
| 769 | len = 0; |
| 770 | } |
| 771 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 772 | /* If we read the data into the Guest, fill in the length and send the |
| 773 | * interrupt. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 774 | if (lenp) { |
| 775 | *lenp = len; |
| 776 | trigger_irq(fd, irq); |
| 777 | } |
| 778 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 779 | /* Three ^C within one second? Exit. |
| 780 | * |
| 781 | * This is such a hack, but works surprisingly well. Each ^C has to be |
| 782 | * in a buffer by itself, so they can't be too fast. But we check that |
| 783 | * we get three within about a second, so they can't be too slow. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 784 | if (len == 1 && ((char *)iov[0].iov_base)[0] == 3) { |
| 785 | if (!abort->count++) |
| 786 | gettimeofday(&abort->start, NULL); |
| 787 | else if (abort->count == 3) { |
| 788 | struct timeval now; |
| 789 | gettimeofday(&now, NULL); |
| 790 | if (now.tv_sec <= abort->start.tv_sec+1) { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 791 | unsigned long args[] = { LHREQ_BREAK, 0 }; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 792 | /* Close the fd so Waker will know it has to |
| 793 | * exit. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 794 | close(waker_fd); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 795 | /* Just in case waker is blocked in BREAK, send |
| 796 | * unbreak now. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 797 | write(fd, args, sizeof(args)); |
| 798 | exit(2); |
| 799 | } |
| 800 | abort->count = 0; |
| 801 | } |
| 802 | } else |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 803 | /* Any other key resets the abort counter. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 804 | abort->count = 0; |
| 805 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 806 | /* Now, if we didn't read anything, put the input terminal back and |
| 807 | * return failure (meaning, don't call us again). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 808 | if (!len) { |
| 809 | restore_term(); |
| 810 | return false; |
| 811 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 812 | /* Everything went OK! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 813 | return true; |
| 814 | } |
| 815 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 816 | /* Handling console output is much simpler than input. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 817 | static u32 handle_console_output(int fd, const struct iovec *iov, |
| 818 | unsigned num, struct device*dev) |
| 819 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 820 | /* Whatever the Guest sends, write it to standard output. Return the |
| 821 | * number of bytes written. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 822 | return writev(STDOUT_FILENO, iov, num); |
| 823 | } |
| 824 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 825 | /* Guest->Host network output is also pretty easy. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 826 | static u32 handle_tun_output(int fd, const struct iovec *iov, |
| 827 | unsigned num, struct device *dev) |
| 828 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 829 | /* We put a flag in the "priv" pointer of the network device, and set |
| 830 | * it as soon as we see output. We'll see why in handle_tun_input() */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 831 | *(bool *)dev->priv = true; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 832 | /* Whatever packet the Guest sent us, write it out to the tun |
| 833 | * device. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 834 | return writev(dev->fd, iov, num); |
| 835 | } |
| 836 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 837 | /* This matches the peer_key() in lguest_net.c. The key for any given slot |
| 838 | * is the address of the network device's page plus 4 * the slot number. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 839 | static unsigned long peer_offset(unsigned int peernum) |
| 840 | { |
| 841 | return 4 * peernum; |
| 842 | } |
| 843 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 844 | /* This is where we handle a packet coming in from the tun device */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 845 | static bool handle_tun_input(int fd, struct device *dev) |
| 846 | { |
| 847 | u32 irq = 0, *lenp; |
| 848 | int len; |
| 849 | unsigned num; |
| 850 | struct iovec iov[LGUEST_MAX_DMA_SECTIONS]; |
| 851 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 852 | /* First we get a buffer the Guest has bound to its key. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 853 | lenp = get_dma_buffer(fd, dev->mem+peer_offset(NET_PEERNUM), iov, &num, |
| 854 | &irq); |
| 855 | if (!lenp) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 856 | /* Now, it's expected that if we try to send a packet too |
| 857 | * early, the Guest won't be ready yet. This is why we set a |
| 858 | * flag when the Guest sends its first packet. If it's sent a |
| 859 | * packet we assume it should be ready to receive them. |
| 860 | * |
| 861 | * Actually, this is what the status bits in the descriptor are |
| 862 | * for: we should *use* them. FIXME! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 863 | if (*(bool *)dev->priv) |
| 864 | warn("network: no dma buffer!"); |
| 865 | discard_iovec(iov, &num); |
| 866 | } |
| 867 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 868 | /* Read the packet from the device directly into the Guest's buffer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 869 | len = readv(dev->fd, iov, num); |
| 870 | if (len <= 0) |
| 871 | err(1, "reading network"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 872 | |
| 873 | /* Write the used_len, and trigger the interrupt for the Guest */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 874 | if (lenp) { |
| 875 | *lenp = len; |
| 876 | trigger_irq(fd, irq); |
| 877 | } |
| 878 | verbose("tun input packet len %i [%02x %02x] (%s)\n", len, |
| 879 | ((u8 *)iov[0].iov_base)[0], ((u8 *)iov[0].iov_base)[1], |
| 880 | lenp ? "sent" : "discarded"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 881 | /* All good. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 882 | return true; |
| 883 | } |
| 884 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 885 | /* The last device handling routine is block output: the Guest has sent a DMA |
| 886 | * to the block device. It will have placed the command it wants in the |
| 887 | * "struct lguest_block_page". */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 888 | static u32 handle_block_output(int fd, const struct iovec *iov, |
| 889 | unsigned num, struct device *dev) |
| 890 | { |
| 891 | struct lguest_block_page *p = dev->mem; |
| 892 | u32 irq, *lenp; |
| 893 | unsigned int len, reply_num; |
| 894 | struct iovec reply[LGUEST_MAX_DMA_SECTIONS]; |
| 895 | off64_t device_len, off = (off64_t)p->sector * 512; |
| 896 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 897 | /* First we extract the device length from the dev->priv pointer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 898 | device_len = *(off64_t *)dev->priv; |
| 899 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 900 | /* We first check that the read or write is within the length of the |
| 901 | * block file. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 902 | if (off >= device_len) |
Glauber de Oliveira Costa | babed5c | 2007-10-22 10:56:21 +1000 | [diff] [blame] | 903 | errx(1, "Bad offset %llu vs %llu", off, device_len); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 904 | /* Move to the right location in the block file. This shouldn't fail, |
| 905 | * but best to check. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 906 | if (lseek64(dev->fd, off, SEEK_SET) != off) |
| 907 | err(1, "Bad seek to sector %i", p->sector); |
| 908 | |
| 909 | verbose("Block: %s at offset %llu\n", p->type ? "WRITE" : "READ", off); |
| 910 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 911 | /* They were supposed to bind a reply buffer at key equal to the start |
| 912 | * of the block device memory. We need this to tell them when the |
| 913 | * request is finished. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 914 | lenp = get_dma_buffer(fd, dev->mem, reply, &reply_num, &irq); |
| 915 | if (!lenp) |
| 916 | err(1, "Block request didn't give us a dma buffer"); |
| 917 | |
| 918 | if (p->type) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 919 | /* A write request. The DMA they sent contained the data, so |
| 920 | * write it out. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 921 | len = writev(dev->fd, iov, num); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 922 | /* Grr... Now we know how long the "struct lguest_dma" they |
| 923 | * sent was, we make sure they didn't try to write over the end |
| 924 | * of the block file (possibly extending it). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 925 | if (off + len > device_len) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 926 | /* Trim it back to the correct length */ |
Chris Malley | f6a592e | 2007-09-26 14:19:18 +1000 | [diff] [blame] | 927 | ftruncate64(dev->fd, device_len); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 928 | /* Die, bad Guest, die. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 929 | errx(1, "Write past end %llu+%u", off, len); |
| 930 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 931 | /* The reply length is 0: we just send back an empty DMA to |
| 932 | * interrupt them and tell them the write is finished. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 933 | *lenp = 0; |
| 934 | } else { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 935 | /* A read request. They sent an empty DMA to start the |
| 936 | * request, and we put the read contents into the reply |
| 937 | * buffer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 938 | len = readv(dev->fd, reply, reply_num); |
| 939 | *lenp = len; |
| 940 | } |
| 941 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 942 | /* The result is 1 (done), 2 if there was an error (short read or |
| 943 | * write). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 944 | p->result = 1 + (p->bytes != len); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 945 | /* Now tell them we've used their reply buffer. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 946 | trigger_irq(fd, irq); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 947 | |
| 948 | /* We're supposed to return the number of bytes of the output buffer we |
| 949 | * used. But the block device uses the "result" field instead, so we |
| 950 | * don't bother. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 954 | /* This is the generic routine we call when the Guest sends some DMA out. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 955 | static void handle_output(int fd, unsigned long dma, unsigned long key, |
| 956 | struct device_list *devices) |
| 957 | { |
| 958 | struct device *i; |
| 959 | u32 *lenp; |
| 960 | struct iovec iov[LGUEST_MAX_DMA_SECTIONS]; |
| 961 | unsigned num = 0; |
| 962 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 963 | /* Convert the "struct lguest_dma" they're sending to a "struct |
| 964 | * iovec". */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 965 | lenp = dma2iov(dma, iov, &num); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 966 | |
| 967 | /* Check each device: if they expect output to this key, tell them to |
| 968 | * handle it. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 969 | for (i = devices->dev; i; i = i->next) { |
| 970 | if (i->handle_output && key == i->watch_key) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 971 | /* We write the result straight into the used_len field |
| 972 | * for them. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 973 | *lenp = i->handle_output(fd, iov, num, i); |
| 974 | return; |
| 975 | } |
| 976 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 977 | |
| 978 | /* This can happen: the kernel sends any SEND_DMA which doesn't match |
| 979 | * another Guest to us. It could be that another Guest just left a |
| 980 | * network, for example. But it's unusual. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 981 | warnx("Pending dma %p, key %p", (void *)dma, (void *)key); |
| 982 | } |
| 983 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 984 | /* This is called when the waker wakes us up: check for incoming file |
| 985 | * descriptors. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 986 | static void handle_input(int fd, struct device_list *devices) |
| 987 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 988 | /* select() wants a zeroed timeval to mean "don't wait". */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 989 | struct timeval poll = { .tv_sec = 0, .tv_usec = 0 }; |
| 990 | |
| 991 | for (;;) { |
| 992 | struct device *i; |
| 993 | fd_set fds = devices->infds; |
| 994 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 995 | /* If nothing is ready, we're done. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 996 | if (select(devices->max_infd+1, &fds, NULL, NULL, &poll) == 0) |
| 997 | break; |
| 998 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 999 | /* Otherwise, call the device(s) which have readable |
| 1000 | * file descriptors and a method of handling them. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1001 | for (i = devices->dev; i; i = i->next) { |
| 1002 | if (i->handle_input && FD_ISSET(i->fd, &fds)) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1003 | /* If handle_input() returns false, it means we |
| 1004 | * should no longer service it. |
| 1005 | * handle_console_input() does this. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1006 | if (!i->handle_input(fd, i)) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1007 | /* Clear it from the set of input file |
| 1008 | * descriptors kept at the head of the |
| 1009 | * device list. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1010 | FD_CLR(i->fd, &devices->infds); |
| 1011 | /* Tell waker to ignore it too... */ |
| 1012 | write(waker_fd, &i->fd, sizeof(i->fd)); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1019 | /*L:190 |
| 1020 | * Device Setup |
| 1021 | * |
| 1022 | * All devices need a descriptor so the Guest knows it exists, and a "struct |
| 1023 | * device" so the Launcher can keep track of it. We have common helper |
| 1024 | * routines to allocate them. |
| 1025 | * |
| 1026 | * This routine allocates a new "struct lguest_device_desc" from descriptor |
| 1027 | * table in the devices array just above the Guest's normal memory. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1028 | static struct lguest_device_desc * |
| 1029 | new_dev_desc(struct lguest_device_desc *descs, |
| 1030 | u16 type, u16 features, u16 num_pages) |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1031 | { |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1032 | unsigned int i; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1033 | |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1034 | for (i = 0; i < LGUEST_MAX_DEVICES; i++) { |
| 1035 | if (!descs[i].type) { |
| 1036 | descs[i].type = type; |
| 1037 | descs[i].features = features; |
| 1038 | descs[i].num_pages = num_pages; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1039 | /* If they said the device needs memory, we allocate |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1040 | * that now. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1041 | if (num_pages) { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1042 | unsigned long pa; |
| 1043 | pa = to_guest_phys(get_pages(num_pages)); |
| 1044 | descs[i].pfn = pa / getpagesize(); |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1045 | } |
| 1046 | return &descs[i]; |
| 1047 | } |
| 1048 | } |
| 1049 | errx(1, "too many devices"); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1052 | /* This monster routine does all the creation and setup of a new device, |
| 1053 | * including caling new_dev_desc() to allocate the descriptor and device |
| 1054 | * memory. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1055 | static struct device *new_device(struct device_list *devices, |
| 1056 | u16 type, u16 num_pages, u16 features, |
| 1057 | int fd, |
| 1058 | bool (*handle_input)(int, struct device *), |
| 1059 | unsigned long watch_off, |
| 1060 | u32 (*handle_output)(int, |
| 1061 | const struct iovec *, |
| 1062 | unsigned, |
| 1063 | struct device *)) |
| 1064 | { |
| 1065 | struct device *dev = malloc(sizeof(*dev)); |
| 1066 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1067 | /* Append to device list. Prepending to a single-linked list is |
| 1068 | * easier, but the user expects the devices to be arranged on the bus |
| 1069 | * in command-line order. The first network device on the command line |
| 1070 | * is eth0, the first block device /dev/lgba, etc. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1071 | *devices->lastdev = dev; |
| 1072 | dev->next = NULL; |
| 1073 | devices->lastdev = &dev->next; |
| 1074 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1075 | /* Now we populate the fields one at a time. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1076 | dev->fd = fd; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1077 | /* If we have an input handler for this file descriptor, then we add it |
| 1078 | * to the device_list's fdset and maxfd. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1079 | if (handle_input) |
| 1080 | set_fd(dev->fd, devices); |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1081 | dev->desc = new_dev_desc(devices->descs, type, features, num_pages); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1082 | dev->mem = from_guest_phys(dev->desc->pfn * getpagesize()); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1083 | dev->handle_input = handle_input; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1084 | dev->watch_key = to_guest_phys(dev->mem) + watch_off; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1085 | dev->handle_output = handle_output; |
| 1086 | return dev; |
| 1087 | } |
| 1088 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1089 | /* Our first setup routine is the console. It's a fairly simple device, but |
| 1090 | * UNIX tty handling makes it uglier than it could be. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1091 | static void setup_console(struct device_list *devices) |
| 1092 | { |
| 1093 | struct device *dev; |
| 1094 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1095 | /* If we can save the initial standard input settings... */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1096 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { |
| 1097 | struct termios term = orig_term; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1098 | /* Then we turn off echo, line buffering and ^C etc. We want a |
| 1099 | * raw input stream to the Guest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1100 | term.c_lflag &= ~(ISIG|ICANON|ECHO); |
| 1101 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1102 | /* If we exit gracefully, the original settings will be |
| 1103 | * restored so the user can see what they're typing. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1104 | atexit(restore_term); |
| 1105 | } |
| 1106 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1107 | /* We don't currently require any memory for the console, so we ask for |
| 1108 | * 0 pages. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1109 | dev = new_device(devices, LGUEST_DEVICE_T_CONSOLE, 0, 0, |
| 1110 | STDIN_FILENO, handle_console_input, |
| 1111 | LGUEST_CONSOLE_DMA_KEY, handle_console_output); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1112 | /* We store the console state in dev->priv, and initialize it. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1113 | dev->priv = malloc(sizeof(struct console_abort)); |
| 1114 | ((struct console_abort *)dev->priv)->count = 0; |
| 1115 | verbose("device %p: console\n", |
| 1116 | (void *)(dev->desc->pfn * getpagesize())); |
| 1117 | } |
| 1118 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1119 | /* Setting up a block file is also fairly straightforward. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1120 | static void setup_block_file(const char *filename, struct device_list *devices) |
| 1121 | { |
| 1122 | int fd; |
| 1123 | struct device *dev; |
| 1124 | off64_t *device_len; |
| 1125 | struct lguest_block_page *p; |
| 1126 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1127 | /* We open with O_LARGEFILE because otherwise we get stuck at 2G. We |
| 1128 | * open with O_DIRECT because otherwise our benchmarks go much too |
| 1129 | * fast. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1130 | fd = open_or_die(filename, O_RDWR|O_LARGEFILE|O_DIRECT); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1131 | |
| 1132 | /* We want one page, and have no input handler (the block file never |
| 1133 | * has anything interesting to say to us). Our timing will be quite |
| 1134 | * random, so it should be a reasonable randomness source. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1135 | dev = new_device(devices, LGUEST_DEVICE_T_BLOCK, 1, |
| 1136 | LGUEST_DEVICE_F_RANDOMNESS, |
| 1137 | fd, NULL, 0, handle_block_output); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1138 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1139 | /* We store the device size in the private area */ |
| 1140 | device_len = dev->priv = malloc(sizeof(*device_len)); |
| 1141 | /* This is the safe way of establishing the size of our device: it |
| 1142 | * might be a normal file or an actual block device like /dev/hdb. */ |
| 1143 | *device_len = lseek64(fd, 0, SEEK_END); |
| 1144 | |
| 1145 | /* The device memory is a "struct lguest_block_page". It's zeroed |
| 1146 | * already, we just need to put in the device size. Block devices |
| 1147 | * think in sectors (ie. 512 byte chunks), so we translate here. */ |
| 1148 | p = dev->mem; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1149 | p->num_sectors = *device_len/512; |
| 1150 | verbose("device %p: block %i sectors\n", |
| 1151 | (void *)(dev->desc->pfn * getpagesize()), p->num_sectors); |
| 1152 | } |
| 1153 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1154 | /* |
| 1155 | * Network Devices. |
| 1156 | * |
| 1157 | * Setting up network devices is quite a pain, because we have three types. |
| 1158 | * First, we have the inter-Guest network. This is a file which is mapped into |
| 1159 | * the address space of the Guests who are on the network. Because it is a |
| 1160 | * shared mapping, the same page underlies all the devices, and they can send |
| 1161 | * DMA to each other. |
| 1162 | * |
| 1163 | * Remember from our network driver, the Guest is told what slot in the page it |
| 1164 | * is to use. We use exclusive fnctl locks to reserve a slot. If another |
| 1165 | * Guest is using a slot, the lock will fail and we try another. Because fnctl |
| 1166 | * locks are cleaned up automatically when we die, this cleverly means that our |
| 1167 | * reservation on the slot will vanish if we crash. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1168 | static unsigned int find_slot(int netfd, const char *filename) |
| 1169 | { |
| 1170 | struct flock fl; |
| 1171 | |
| 1172 | fl.l_type = F_WRLCK; |
| 1173 | fl.l_whence = SEEK_SET; |
| 1174 | fl.l_len = 1; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1175 | /* Try a 1 byte lock in each possible position number */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1176 | for (fl.l_start = 0; |
| 1177 | fl.l_start < getpagesize()/sizeof(struct lguest_net); |
| 1178 | fl.l_start++) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1179 | /* If we succeed, return the slot number. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1180 | if (fcntl(netfd, F_SETLK, &fl) == 0) |
| 1181 | return fl.l_start; |
| 1182 | } |
| 1183 | errx(1, "No free slots in network file %s", filename); |
| 1184 | } |
| 1185 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1186 | /* This function sets up the network file */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1187 | static void setup_net_file(const char *filename, |
| 1188 | struct device_list *devices) |
| 1189 | { |
| 1190 | int netfd; |
| 1191 | struct device *dev; |
| 1192 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1193 | /* We don't use open_or_die() here: for friendliness we create the file |
| 1194 | * if it doesn't already exist. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1195 | netfd = open(filename, O_RDWR, 0); |
| 1196 | if (netfd < 0) { |
| 1197 | if (errno == ENOENT) { |
| 1198 | netfd = open(filename, O_RDWR|O_CREAT, 0600); |
| 1199 | if (netfd >= 0) { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1200 | /* If we succeeded, initialize the file with a |
| 1201 | * blank page. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1202 | char page[getpagesize()]; |
| 1203 | memset(page, 0, sizeof(page)); |
| 1204 | write(netfd, page, sizeof(page)); |
| 1205 | } |
| 1206 | } |
| 1207 | if (netfd < 0) |
| 1208 | err(1, "cannot open net file '%s'", filename); |
| 1209 | } |
| 1210 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1211 | /* We need 1 page, and the features indicate the slot to use and that |
| 1212 | * no checksum is needed. We never touch this device again; it's |
| 1213 | * between the Guests on the network, so we don't register input or |
| 1214 | * output handlers. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1215 | dev = new_device(devices, LGUEST_DEVICE_T_NET, 1, |
| 1216 | find_slot(netfd, filename)|LGUEST_NET_F_NOCSUM, |
| 1217 | -1, NULL, 0, NULL); |
| 1218 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1219 | /* Map the shared file. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1220 | if (mmap(dev->mem, getpagesize(), PROT_READ|PROT_WRITE, |
| 1221 | MAP_FIXED|MAP_SHARED, netfd, 0) != dev->mem) |
| 1222 | err(1, "could not mmap '%s'", filename); |
| 1223 | verbose("device %p: shared net %s, peer %i\n", |
| 1224 | (void *)(dev->desc->pfn * getpagesize()), filename, |
| 1225 | dev->desc->features & ~LGUEST_NET_F_NOCSUM); |
| 1226 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1227 | /*:*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1228 | |
| 1229 | static u32 str2ip(const char *ipaddr) |
| 1230 | { |
| 1231 | unsigned int byte[4]; |
| 1232 | |
| 1233 | sscanf(ipaddr, "%u.%u.%u.%u", &byte[0], &byte[1], &byte[2], &byte[3]); |
| 1234 | return (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3]; |
| 1235 | } |
| 1236 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1237 | /* This code is "adapted" from libbridge: it attaches the Host end of the |
| 1238 | * network device to the bridge device specified by the command line. |
| 1239 | * |
| 1240 | * This is yet another James Morris contribution (I'm an IP-level guy, so I |
| 1241 | * dislike bridging), and I just try not to break it. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1242 | static void add_to_bridge(int fd, const char *if_name, const char *br_name) |
| 1243 | { |
| 1244 | int ifidx; |
| 1245 | struct ifreq ifr; |
| 1246 | |
| 1247 | if (!*br_name) |
| 1248 | errx(1, "must specify bridge name"); |
| 1249 | |
| 1250 | ifidx = if_nametoindex(if_name); |
| 1251 | if (!ifidx) |
| 1252 | errx(1, "interface %s does not exist!", if_name); |
| 1253 | |
| 1254 | strncpy(ifr.ifr_name, br_name, IFNAMSIZ); |
| 1255 | ifr.ifr_ifindex = ifidx; |
| 1256 | if (ioctl(fd, SIOCBRADDIF, &ifr) < 0) |
| 1257 | err(1, "can't add %s to bridge %s", if_name, br_name); |
| 1258 | } |
| 1259 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1260 | /* This sets up the Host end of the network device with an IP address, brings |
| 1261 | * it up so packets will flow, the copies the MAC address into the hwaddr |
| 1262 | * pointer (in practice, the Host's slot in the network device's memory). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1263 | static void configure_device(int fd, const char *devname, u32 ipaddr, |
| 1264 | unsigned char hwaddr[6]) |
| 1265 | { |
| 1266 | struct ifreq ifr; |
| 1267 | struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; |
| 1268 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1269 | /* Don't read these incantations. Just cut & paste them like I did! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1270 | memset(&ifr, 0, sizeof(ifr)); |
| 1271 | strcpy(ifr.ifr_name, devname); |
| 1272 | sin->sin_family = AF_INET; |
| 1273 | sin->sin_addr.s_addr = htonl(ipaddr); |
| 1274 | if (ioctl(fd, SIOCSIFADDR, &ifr) != 0) |
| 1275 | err(1, "Setting %s interface address", devname); |
| 1276 | ifr.ifr_flags = IFF_UP; |
| 1277 | if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) |
| 1278 | err(1, "Bringing interface %s up", devname); |
| 1279 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1280 | /* SIOC stands for Socket I/O Control. G means Get (vs S for Set |
| 1281 | * above). IF means Interface, and HWADDR is hardware address. |
| 1282 | * Simple! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1283 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) |
| 1284 | err(1, "getting hw address for %s", devname); |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1285 | memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, 6); |
| 1286 | } |
| 1287 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1288 | /*L:195 The other kind of network is a Host<->Guest network. This can either |
| 1289 | * use briding or routing, but the principle is the same: it uses the "tun" |
| 1290 | * device to inject packets into the Host as if they came in from a normal |
| 1291 | * network card. We just shunt packets between the Guest and the tun |
| 1292 | * device. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1293 | static void setup_tun_net(const char *arg, struct device_list *devices) |
| 1294 | { |
| 1295 | struct device *dev; |
| 1296 | struct ifreq ifr; |
| 1297 | int netfd, ipfd; |
| 1298 | u32 ip; |
| 1299 | const char *br_name = NULL; |
| 1300 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1301 | /* We open the /dev/net/tun device and tell it we want a tap device. A |
| 1302 | * tap device is like a tun device, only somehow different. To tell |
| 1303 | * the truth, I completely blundered my way through this code, but it |
| 1304 | * works now! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1305 | netfd = open_or_die("/dev/net/tun", O_RDWR); |
| 1306 | memset(&ifr, 0, sizeof(ifr)); |
| 1307 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 1308 | strcpy(ifr.ifr_name, "tap%d"); |
| 1309 | if (ioctl(netfd, TUNSETIFF, &ifr) != 0) |
| 1310 | err(1, "configuring /dev/net/tun"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1311 | /* We don't need checksums calculated for packets coming in this |
| 1312 | * device: trust us! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1313 | ioctl(netfd, TUNSETNOCSUM, 1); |
| 1314 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1315 | /* We create the net device with 1 page, using the features field of |
| 1316 | * the descriptor to tell the Guest it is in slot 1 (NET_PEERNUM), and |
| 1317 | * that the device has fairly random timing. We do *not* specify |
| 1318 | * LGUEST_NET_F_NOCSUM: these packets can reach the real world. |
| 1319 | * |
| 1320 | * We will put our MAC address is slot 0 for the Guest to see, so |
| 1321 | * it will send packets to us using the key "peer_offset(0)": */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1322 | dev = new_device(devices, LGUEST_DEVICE_T_NET, 1, |
| 1323 | NET_PEERNUM|LGUEST_DEVICE_F_RANDOMNESS, netfd, |
| 1324 | handle_tun_input, peer_offset(0), handle_tun_output); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1325 | |
| 1326 | /* We keep a flag which says whether we've seen packets come out from |
| 1327 | * this network device. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1328 | dev->priv = malloc(sizeof(bool)); |
| 1329 | *(bool *)dev->priv = false; |
| 1330 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1331 | /* We need a socket to perform the magic network ioctls to bring up the |
| 1332 | * tap interface, connect to the bridge etc. Any socket will do! */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1333 | ipfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 1334 | if (ipfd < 0) |
| 1335 | err(1, "opening IP socket"); |
| 1336 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1337 | /* If the command line was --tunnet=bridge:<name> do bridging. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1338 | if (!strncmp(BRIDGE_PFX, arg, strlen(BRIDGE_PFX))) { |
| 1339 | ip = INADDR_ANY; |
| 1340 | br_name = arg + strlen(BRIDGE_PFX); |
| 1341 | add_to_bridge(ipfd, ifr.ifr_name, br_name); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1342 | } else /* It is an IP address to set up the device with */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1343 | ip = str2ip(arg); |
| 1344 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1345 | /* We are peer 0, ie. first slot, so we hand dev->mem to this routine |
| 1346 | * to write the MAC address at the start of the device memory. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1347 | configure_device(ipfd, ifr.ifr_name, ip, dev->mem); |
| 1348 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1349 | /* Set "promisc" bit: we want every single packet if we're going to |
| 1350 | * bridge to other machines (and otherwise it doesn't matter). */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1351 | *((u8 *)dev->mem) |= 0x1; |
| 1352 | |
| 1353 | close(ipfd); |
| 1354 | |
| 1355 | verbose("device %p: tun net %u.%u.%u.%u\n", |
| 1356 | (void *)(dev->desc->pfn * getpagesize()), |
| 1357 | (u8)(ip>>24), (u8)(ip>>16), (u8)(ip>>8), (u8)ip); |
| 1358 | if (br_name) |
| 1359 | verbose("attached to bridge: %s\n", br_name); |
| 1360 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1361 | /* That's the end of device setup. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1362 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1363 | /*L:220 Finally we reach the core of the Launcher, which runs the Guest, serves |
| 1364 | * its input and output, and finally, lays it to rest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1365 | static void __attribute__((noreturn)) |
| 1366 | run_guest(int lguest_fd, struct device_list *device_list) |
| 1367 | { |
| 1368 | for (;;) { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 1369 | unsigned long args[] = { LHREQ_BREAK, 0 }; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1370 | unsigned long arr[2]; |
| 1371 | int readval; |
| 1372 | |
| 1373 | /* We read from the /dev/lguest device to run the Guest. */ |
| 1374 | readval = read(lguest_fd, arr, sizeof(arr)); |
| 1375 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1376 | /* The read can only really return sizeof(arr) (the Guest did a |
| 1377 | * SEND_DMA to us), or an error. */ |
| 1378 | |
| 1379 | /* For a successful read, arr[0] is the address of the "struct |
| 1380 | * lguest_dma", and arr[1] is the key the Guest sent to. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1381 | if (readval == sizeof(arr)) { |
| 1382 | handle_output(lguest_fd, arr[0], arr[1], device_list); |
| 1383 | continue; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1384 | /* ENOENT means the Guest died. Reading tells us why. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1385 | } else if (errno == ENOENT) { |
| 1386 | char reason[1024] = { 0 }; |
| 1387 | read(lguest_fd, reason, sizeof(reason)-1); |
| 1388 | errx(1, "%s", reason); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1389 | /* EAGAIN means the waker wanted us to look at some input. |
| 1390 | * Anything else means a bug or incompatible change. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1391 | } else if (errno != EAGAIN) |
| 1392 | err(1, "Running guest failed"); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1393 | |
| 1394 | /* Service input, then unset the BREAK which releases |
| 1395 | * the Waker. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1396 | handle_input(lguest_fd, device_list); |
| 1397 | if (write(lguest_fd, args, sizeof(args)) < 0) |
| 1398 | err(1, "Resetting break"); |
| 1399 | } |
| 1400 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1401 | /* |
| 1402 | * This is the end of the Launcher. |
| 1403 | * |
| 1404 | * But wait! We've seen I/O from the Launcher, and we've seen I/O from the |
| 1405 | * Drivers. If we were to see the Host kernel I/O code, our understanding |
| 1406 | * would be complete... :*/ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1407 | |
| 1408 | static struct option opts[] = { |
| 1409 | { "verbose", 0, NULL, 'v' }, |
| 1410 | { "sharenet", 1, NULL, 's' }, |
| 1411 | { "tunnet", 1, NULL, 't' }, |
| 1412 | { "block", 1, NULL, 'b' }, |
| 1413 | { "initrd", 1, NULL, 'i' }, |
| 1414 | { NULL }, |
| 1415 | }; |
| 1416 | static void usage(void) |
| 1417 | { |
| 1418 | errx(1, "Usage: lguest [--verbose] " |
| 1419 | "[--sharenet=<filename>|--tunnet=(<ipaddr>|bridge:<bridgename>)\n" |
| 1420 | "|--block=<filename>|--initrd=<filename>]...\n" |
| 1421 | "<mem-in-mb> vmlinux [args...]"); |
| 1422 | } |
| 1423 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1424 | /*L:105 The main routine is where the real work begins: */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1425 | int main(int argc, char *argv[]) |
| 1426 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1427 | /* Memory, top-level pagetable, code startpoint, PAGE_OFFSET and size |
| 1428 | * of the (optional) initrd. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1429 | unsigned long mem = 0, pgdir, start, page_offset, initrd_size = 0; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1430 | /* A temporary and the /dev/lguest file descriptor. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1431 | int i, c, lguest_fd; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1432 | /* The list of Guest devices, based on command line arguments. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1433 | struct device_list device_list; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1434 | /* The boot information for the Guest. */ |
| 1435 | void *boot; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1436 | /* If they specify an initrd file to load. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1437 | const char *initrd_name = NULL; |
| 1438 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1439 | /* First we initialize the device list. Since console and network |
| 1440 | * device receive input from a file descriptor, we keep an fdset |
| 1441 | * (infds) and the maximum fd number (max_infd) with the head of the |
| 1442 | * list. We also keep a pointer to the last device, for easy appending |
| 1443 | * to the list. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1444 | device_list.max_infd = -1; |
| 1445 | device_list.dev = NULL; |
| 1446 | device_list.lastdev = &device_list.dev; |
| 1447 | FD_ZERO(&device_list.infds); |
| 1448 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1449 | /* We need to know how much memory so we can set up the device |
| 1450 | * descriptor and memory pages for the devices as we parse the command |
| 1451 | * line. So we quickly look through the arguments to find the amount |
| 1452 | * of memory now. */ |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1453 | for (i = 1; i < argc; i++) { |
| 1454 | if (argv[i][0] != '-') { |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1455 | mem = atoi(argv[i]) * 1024 * 1024; |
| 1456 | /* We start by mapping anonymous pages over all of |
| 1457 | * guest-physical memory range. This fills it with 0, |
| 1458 | * and ensures that the Guest won't be killed when it |
| 1459 | * tries to access it. */ |
| 1460 | guest_base = map_zeroed_pages(mem / getpagesize() |
| 1461 | + DEVICE_PAGES); |
| 1462 | guest_limit = mem; |
| 1463 | guest_max = mem + DEVICE_PAGES*getpagesize(); |
| 1464 | device_list.descs = get_pages(1); |
Rusty Russell | 6570c4599 | 2007-07-23 18:43:56 -0700 | [diff] [blame] | 1465 | break; |
| 1466 | } |
| 1467 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1468 | |
| 1469 | /* The options are fairly straight-forward */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1470 | while ((c = getopt_long(argc, argv, "v", opts, NULL)) != EOF) { |
| 1471 | switch (c) { |
| 1472 | case 'v': |
| 1473 | verbose = true; |
| 1474 | break; |
| 1475 | case 's': |
| 1476 | setup_net_file(optarg, &device_list); |
| 1477 | break; |
| 1478 | case 't': |
| 1479 | setup_tun_net(optarg, &device_list); |
| 1480 | break; |
| 1481 | case 'b': |
| 1482 | setup_block_file(optarg, &device_list); |
| 1483 | break; |
| 1484 | case 'i': |
| 1485 | initrd_name = optarg; |
| 1486 | break; |
| 1487 | default: |
| 1488 | warnx("Unknown argument %s", argv[optind]); |
| 1489 | usage(); |
| 1490 | } |
| 1491 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1492 | /* After the other arguments we expect memory and kernel image name, |
| 1493 | * followed by command line arguments for the kernel. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1494 | if (optind + 2 > argc) |
| 1495 | usage(); |
| 1496 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1497 | verbose("Guest base is at %p\n", guest_base); |
| 1498 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1499 | /* We always have a console device */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1500 | setup_console(&device_list); |
| 1501 | |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1502 | /* Now we load the kernel */ |
| 1503 | start = load_kernel(open_or_die(argv[optind+1], O_RDONLY), |
| 1504 | &page_offset); |
| 1505 | |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1506 | /* Boot information is stashed at physical address 0 */ |
| 1507 | boot = from_guest_phys(0); |
| 1508 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1509 | /* Map the initrd image if requested (at top of physical memory) */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1510 | if (initrd_name) { |
| 1511 | initrd_size = load_initrd(initrd_name, mem); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1512 | /* These are the location in the Linux boot header where the |
| 1513 | * start and size of the initrd are expected to be found. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1514 | *(unsigned long *)(boot+0x218) = mem - initrd_size; |
| 1515 | *(unsigned long *)(boot+0x21c) = initrd_size; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1516 | /* The bootloader type 0xFF means "unknown"; that's OK. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1517 | *(unsigned char *)(boot+0x210) = 0xFF; |
| 1518 | } |
| 1519 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1520 | /* Set up the initial linear pagetables, starting below the initrd. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1521 | pgdir = setup_pagetables(mem, initrd_size, page_offset); |
| 1522 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1523 | /* The Linux boot header contains an "E820" memory map: ours is a |
| 1524 | * simple, single region. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1525 | *(char*)(boot+E820NR) = 1; |
| 1526 | *((struct e820entry *)(boot+E820MAP)) |
| 1527 | = ((struct e820entry) { 0, mem, E820_RAM }); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1528 | /* The boot header contains a command line pointer: we put the command |
| 1529 | * line after the boot header (at address 4096) */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 1530 | *(u32 *)(boot + 0x228) = 4096; |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1531 | concat(boot + 4096, argv+optind+2); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1532 | |
| 1533 | /* The guest type value of "1" tells the Guest it's under lguest. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1534 | *(int *)(boot + 0x23c) = 1; |
| 1535 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1536 | /* We tell the kernel to initialize the Guest: this returns the open |
| 1537 | * /dev/lguest file descriptor. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1538 | lguest_fd = tell_kernel(pgdir, start, page_offset); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1539 | |
| 1540 | /* We fork off a child process, which wakes the Launcher whenever one |
| 1541 | * of the input file descriptors needs attention. Otherwise we would |
| 1542 | * run the Guest until it tries to output something. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1543 | waker_fd = setup_waker(lguest_fd, &device_list); |
| 1544 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 1545 | /* Finally, run the Guest. This doesn't return. */ |
Rusty Russell | 8ca47e0 | 2007-07-19 01:49:29 -0700 | [diff] [blame] | 1546 | run_guest(lguest_fd, &device_list); |
| 1547 | } |
Rusty Russell | f56a384 | 2007-07-26 10:41:05 -0700 | [diff] [blame] | 1548 | /*:*/ |
| 1549 | |
| 1550 | /*M:999 |
| 1551 | * Mastery is done: you now know everything I do. |
| 1552 | * |
| 1553 | * But surely you have seen code, features and bugs in your wanderings which |
| 1554 | * you now yearn to attack? That is the real game, and I look forward to you |
| 1555 | * patching and forking lguest into the Your-Name-Here-visor. |
| 1556 | * |
| 1557 | * Farewell, and good coding! |
| 1558 | * Rusty Russell. |
| 1559 | */ |