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