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