1 /* Simple program to layout "physical" memory for new lguest guest.
2 * Linked high to avoid likely physical memory. */
3 #define _LARGEFILE64_SOURCE
13 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
24 #include <netinet/in.h>
26 #include <linux/sockios.h>
27 #include <linux/if_tun.h>
32 typedef unsigned long long u64
;
36 #include "../../include/linux/lguest_launcher.h"
37 #include "../../include/asm-i386/e820.h"
39 #define PAGE_PRESENT 0x7 /* Present, RW, Execute */
41 #define BRIDGE_PFX "bridge:"
43 #define SIOCBRADDIF 0x89a2 /* add interface to bridge */
47 #define verbose(args...) \
48 do { if (verbose) printf(args); } while(0)
57 struct lguest_device_desc
*descs
;
59 struct device
**lastdev
;
65 struct lguest_device_desc
*desc
;
68 /* Watch this fd if handle_input non-NULL. */
70 bool (*handle_input
)(int fd
, struct device
*me
);
72 /* Watch DMA to this key if handle_input non-NULL. */
73 unsigned long watch_key
;
74 u32 (*handle_output
)(int fd
, const struct iovec
*iov
,
75 unsigned int num
, struct device
*me
);
77 /* Device-specific data. */
81 static int open_or_die(const char *name
, int flags
)
83 int fd
= open(name
, flags
);
85 err(1, "Failed to open %s", name
);
89 static void *map_zeroed_pages(unsigned long addr
, unsigned int num
)
94 fd
= open_or_die("/dev/zero", O_RDONLY
);
96 if (mmap((void *)addr
, getpagesize() * num
,
97 PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_FIXED
|MAP_PRIVATE
, fd
, 0)
99 err(1, "Mmaping %u pages of /dev/zero @%p", num
, (void *)addr
);
103 /* Find magic string marking entry point, return entry point. */
104 static unsigned long entry_point(void *start
, void *end
,
105 unsigned long page_offset
)
109 for (p
= start
; p
< end
; p
++)
110 if (memcmp(p
, "GenuineLguest", strlen("GenuineLguest")) == 0)
111 return (long)p
+ strlen("GenuineLguest") + page_offset
;
113 err(1, "Is this image a genuine lguest?");
116 /* Returns the entry point */
117 static unsigned long map_elf(int elf_fd
, const Elf32_Ehdr
*ehdr
,
118 unsigned long *page_offset
)
121 Elf32_Phdr phdr
[ehdr
->e_phnum
];
123 unsigned long start
= -1UL, end
= 0;
126 if (ehdr
->e_type
!= ET_EXEC
127 || ehdr
->e_machine
!= EM_386
128 || ehdr
->e_phentsize
!= sizeof(Elf32_Phdr
)
129 || ehdr
->e_phnum
< 1 || ehdr
->e_phnum
> 65536U/sizeof(Elf32_Phdr
))
130 errx(1, "Malformed elf header");
132 if (lseek(elf_fd
, ehdr
->e_phoff
, SEEK_SET
) < 0)
133 err(1, "Seeking to program headers");
134 if (read(elf_fd
, phdr
, sizeof(phdr
)) != sizeof(phdr
))
135 err(1, "Reading program headers");
138 /* We map the loadable segments at virtual addresses corresponding
139 * to their physical addresses (our virtual == guest physical). */
140 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
141 if (phdr
[i
].p_type
!= PT_LOAD
)
144 verbose("Section %i: size %i addr %p\n",
145 i
, phdr
[i
].p_memsz
, (void *)phdr
[i
].p_paddr
);
147 /* We expect linear address space. */
149 *page_offset
= phdr
[i
].p_vaddr
- phdr
[i
].p_paddr
;
150 else if (*page_offset
!= phdr
[i
].p_vaddr
- phdr
[i
].p_paddr
)
151 errx(1, "Page offset of section %i different", i
);
153 if (phdr
[i
].p_paddr
< start
)
154 start
= phdr
[i
].p_paddr
;
155 if (phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
> end
)
156 end
= phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
;
158 /* We map everything private, writable. */
159 addr
= mmap((void *)phdr
[i
].p_paddr
,
161 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
162 MAP_FIXED
|MAP_PRIVATE
,
163 elf_fd
, phdr
[i
].p_offset
);
164 if (addr
!= (void *)phdr
[i
].p_paddr
)
165 err(1, "Mmaping vmlinux seg %i gave %p not %p",
166 i
, addr
, (void *)phdr
[i
].p_paddr
);
169 return entry_point((void *)start
, (void *)end
, *page_offset
);
172 /* This is amazingly reliable. */
173 static unsigned long intuit_page_offset(unsigned char *img
, unsigned long len
)
175 unsigned int i
, possibilities
[256] = { 0 };
177 for (i
= 0; i
+ 4 < len
; i
++) {
178 /* mov 0xXXXXXXXX,%eax */
179 if (img
[i
] == 0xA1 && ++possibilities
[img
[i
+4]] > 3)
180 return (unsigned long)img
[i
+4] << 24;
182 errx(1, "could not determine page offset");
185 static unsigned long unpack_bzimage(int fd
, unsigned long *page_offset
)
189 void *img
= (void *)0x100000;
191 f
= gzdopen(fd
, "rb");
192 while ((ret
= gzread(f
, img
+ len
, 65536)) > 0)
195 err(1, "reading image from bzImage");
197 verbose("Unpacked size %i addr %p\n", len
, img
);
198 *page_offset
= intuit_page_offset(img
, len
);
200 return entry_point(img
, img
+ len
, *page_offset
);
203 static unsigned long load_bzimage(int fd
, unsigned long *page_offset
)
208 /* Ugly brute force search for gzip header. */
209 while (read(fd
, &c
, 1) == 1) {
225 lseek(fd
, -10, SEEK_CUR
);
226 if (c
!= 0x03) /* Compressed under UNIX. */
229 return unpack_bzimage(fd
, page_offset
);
232 errx(1, "Could not find kernel in bzImage");
235 static unsigned long load_kernel(int fd
, unsigned long *page_offset
)
239 if (read(fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
240 err(1, "Reading kernel");
242 if (memcmp(hdr
.e_ident
, ELFMAG
, SELFMAG
) == 0)
243 return map_elf(fd
, &hdr
, page_offset
);
245 return load_bzimage(fd
, page_offset
);
248 static inline unsigned long page_align(unsigned long addr
)
250 return ((addr
+ getpagesize()-1) & ~(getpagesize()-1));
253 /* initrd gets loaded at top of memory: return length. */
254 static unsigned long load_initrd(const char *name
, unsigned long mem
)
261 ifd
= open_or_die(name
, O_RDONLY
);
262 if (fstat(ifd
, &st
) < 0)
263 err(1, "fstat() on initrd '%s'", name
);
265 len
= page_align(st
.st_size
);
266 iaddr
= mmap((void *)mem
- len
, st
.st_size
,
267 PROT_READ
|PROT_EXEC
|PROT_WRITE
,
268 MAP_FIXED
|MAP_PRIVATE
, ifd
, 0);
269 if (iaddr
!= (void *)mem
- len
)
270 err(1, "Mmaping initrd '%s' returned %p not %p",
271 name
, iaddr
, (void *)mem
- len
);
273 verbose("mapped initrd %s size=%lu @ %p\n", name
, st
.st_size
, iaddr
);
277 static unsigned long setup_pagetables(unsigned long mem
,
278 unsigned long initrd_size
,
279 unsigned long page_offset
)
282 unsigned int mapped_pages
, i
, linear_pages
;
283 unsigned int ptes_per_page
= getpagesize()/sizeof(u32
);
285 /* If we can map all of memory above page_offset, we do so. */
286 if (mem
<= -page_offset
)
287 mapped_pages
= mem
/getpagesize();
289 mapped_pages
= -page_offset
/getpagesize();
291 /* Each linear PTE page can map ptes_per_page pages. */
292 linear_pages
= (mapped_pages
+ ptes_per_page
-1)/ptes_per_page
;
294 /* We lay out top-level then linear mapping immediately below initrd */
295 pgdir
= (void *)mem
- initrd_size
- getpagesize();
296 linear
= (void *)pgdir
- linear_pages
*getpagesize();
298 for (i
= 0; i
< mapped_pages
; i
++)
299 linear
[i
] = ((i
* getpagesize()) | PAGE_PRESENT
);
301 /* Now set up pgd so that this memory is at page_offset */
302 for (i
= 0; i
< mapped_pages
; i
+= ptes_per_page
) {
303 pgdir
[(i
+ page_offset
/getpagesize())/ptes_per_page
]
304 = (((u32
)linear
+ i
*sizeof(u32
)) | PAGE_PRESENT
);
307 verbose("Linear mapping of %u pages in %u pte pages at %p\n",
308 mapped_pages
, linear_pages
, linear
);
310 return (unsigned long)pgdir
;
313 static void concat(char *dst
, char *args
[])
315 unsigned int i
, len
= 0;
317 for (i
= 0; args
[i
]; i
++) {
318 strcpy(dst
+len
, args
[i
]);
319 strcat(dst
+len
, " ");
320 len
+= strlen(args
[i
]) + 1;
322 /* In case it's empty. */
326 static int tell_kernel(u32 pgdir
, u32 start
, u32 page_offset
)
328 u32 args
[] = { LHREQ_INITIALIZE
,
329 top
/getpagesize(), pgdir
, start
, page_offset
};
332 fd
= open_or_die("/dev/lguest", O_RDWR
);
333 if (write(fd
, args
, sizeof(args
)) < 0)
334 err(1, "Writing to /dev/lguest");
338 static void set_fd(int fd
, struct device_list
*devices
)
340 FD_SET(fd
, &devices
->infds
);
341 if (fd
> devices
->max_infd
)
342 devices
->max_infd
= fd
;
345 /* When input arrives, we tell the kernel to kick lguest out with -EAGAIN. */
346 static void wake_parent(int pipefd
, int lguest_fd
, struct device_list
*devices
)
348 set_fd(pipefd
, devices
);
351 fd_set rfds
= devices
->infds
;
352 u32 args
[] = { LHREQ_BREAK
, 1 };
354 select(devices
->max_infd
+1, &rfds
, NULL
, NULL
, NULL
);
355 if (FD_ISSET(pipefd
, &rfds
)) {
357 if (read(pipefd
, &ignorefd
, sizeof(ignorefd
)) == 0)
359 FD_CLR(ignorefd
, &devices
->infds
);
361 write(lguest_fd
, args
, sizeof(args
));
365 static int setup_waker(int lguest_fd
, struct device_list
*device_list
)
367 int pipefd
[2], child
;
376 wake_parent(pipefd
[0], lguest_fd
, device_list
);
383 static void *_check_pointer(unsigned long addr
, unsigned int size
,
386 if (addr
>= top
|| addr
+ size
>= top
)
387 errx(1, "%s:%i: Invalid address %li", __FILE__
, line
, addr
);
390 #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__)
392 /* Returns pointer to dma->used_len */
393 static u32
*dma2iov(unsigned long dma
, struct iovec iov
[], unsigned *num
)
396 struct lguest_dma
*udma
;
398 udma
= check_pointer(dma
, sizeof(*udma
));
399 for (i
= 0; i
< LGUEST_MAX_DMA_SECTIONS
; i
++) {
403 iov
[i
].iov_base
= check_pointer(udma
->addr
[i
], udma
->len
[i
]);
404 iov
[i
].iov_len
= udma
->len
[i
];
407 return &udma
->used_len
;
410 static u32
*get_dma_buffer(int fd
, void *key
,
411 struct iovec iov
[], unsigned int *num
, u32
*irq
)
413 u32 buf
[] = { LHREQ_GETDMA
, (u32
)key
};
417 udma
= write(fd
, buf
, sizeof(buf
));
418 if (udma
== (unsigned long)-1)
421 /* Kernel stashes irq in ->used_len. */
422 res
= dma2iov(udma
, iov
, num
);
427 static void trigger_irq(int fd
, u32 irq
)
429 u32 buf
[] = { LHREQ_IRQ
, irq
};
430 if (write(fd
, buf
, sizeof(buf
)) != 0)
431 err(1, "Triggering irq %i", irq
);
434 static void discard_iovec(struct iovec
*iov
, unsigned int *num
)
436 static char discard_buf
[1024];
438 iov
->iov_base
= discard_buf
;
439 iov
->iov_len
= sizeof(discard_buf
);
442 static struct termios orig_term
;
443 static void restore_term(void)
445 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_term
);
451 struct timeval start
;
454 /* We DMA input to buffer bound at start of console page. */
455 static bool handle_console_input(int fd
, struct device
*dev
)
460 struct iovec iov
[LGUEST_MAX_DMA_SECTIONS
];
461 struct console_abort
*abort
= dev
->priv
;
463 lenp
= get_dma_buffer(fd
, dev
->mem
, iov
, &num
, &irq
);
465 warn("console: no dma buffer!");
466 discard_iovec(iov
, &num
);
469 len
= readv(dev
->fd
, iov
, num
);
471 warnx("Failed to get console input, ignoring console.");
477 trigger_irq(fd
, irq
);
480 /* Three ^C within one second? Exit. */
481 if (len
== 1 && ((char *)iov
[0].iov_base
)[0] == 3) {
483 gettimeofday(&abort
->start
, NULL
);
484 else if (abort
->count
== 3) {
486 gettimeofday(&now
, NULL
);
487 if (now
.tv_sec
<= abort
->start
.tv_sec
+1) {
488 /* Make sure waker is not blocked in BREAK */
489 u32 args
[] = { LHREQ_BREAK
, 0 };
491 write(fd
, args
, sizeof(args
));
506 static u32
handle_console_output(int fd
, const struct iovec
*iov
,
507 unsigned num
, struct device
*dev
)
509 return writev(STDOUT_FILENO
, iov
, num
);
512 static u32
handle_tun_output(int fd
, const struct iovec
*iov
,
513 unsigned num
, struct device
*dev
)
515 /* Now we've seen output, we should warn if we can't get buffers. */
516 *(bool *)dev
->priv
= true;
517 return writev(dev
->fd
, iov
, num
);
520 static unsigned long peer_offset(unsigned int peernum
)
525 static bool handle_tun_input(int fd
, struct device
*dev
)
530 struct iovec iov
[LGUEST_MAX_DMA_SECTIONS
];
532 lenp
= get_dma_buffer(fd
, dev
->mem
+peer_offset(NET_PEERNUM
), iov
, &num
,
535 if (*(bool *)dev
->priv
)
536 warn("network: no dma buffer!");
537 discard_iovec(iov
, &num
);
540 len
= readv(dev
->fd
, iov
, num
);
542 err(1, "reading network");
545 trigger_irq(fd
, irq
);
547 verbose("tun input packet len %i [%02x %02x] (%s)\n", len
,
548 ((u8
*)iov
[0].iov_base
)[0], ((u8
*)iov
[0].iov_base
)[1],
549 lenp
? "sent" : "discarded");
553 static u32
handle_block_output(int fd
, const struct iovec
*iov
,
554 unsigned num
, struct device
*dev
)
556 struct lguest_block_page
*p
= dev
->mem
;
558 unsigned int len
, reply_num
;
559 struct iovec reply
[LGUEST_MAX_DMA_SECTIONS
];
560 off64_t device_len
, off
= (off64_t
)p
->sector
* 512;
562 device_len
= *(off64_t
*)dev
->priv
;
564 if (off
>= device_len
)
565 err(1, "Bad offset %llu vs %llu", off
, device_len
);
566 if (lseek64(dev
->fd
, off
, SEEK_SET
) != off
)
567 err(1, "Bad seek to sector %i", p
->sector
);
569 verbose("Block: %s at offset %llu\n", p
->type
? "WRITE" : "READ", off
);
571 lenp
= get_dma_buffer(fd
, dev
->mem
, reply
, &reply_num
, &irq
);
573 err(1, "Block request didn't give us a dma buffer");
576 len
= writev(dev
->fd
, iov
, num
);
577 if (off
+ len
> device_len
) {
578 ftruncate(dev
->fd
, device_len
);
579 errx(1, "Write past end %llu+%u", off
, len
);
583 len
= readv(dev
->fd
, reply
, reply_num
);
587 p
->result
= 1 + (p
->bytes
!= len
);
588 trigger_irq(fd
, irq
);
592 static void handle_output(int fd
, unsigned long dma
, unsigned long key
,
593 struct device_list
*devices
)
597 struct iovec iov
[LGUEST_MAX_DMA_SECTIONS
];
600 lenp
= dma2iov(dma
, iov
, &num
);
601 for (i
= devices
->dev
; i
; i
= i
->next
) {
602 if (i
->handle_output
&& key
== i
->watch_key
) {
603 *lenp
= i
->handle_output(fd
, iov
, num
, i
);
607 warnx("Pending dma %p, key %p", (void *)dma
, (void *)key
);
610 static void handle_input(int fd
, struct device_list
*devices
)
612 struct timeval poll
= { .tv_sec
= 0, .tv_usec
= 0 };
616 fd_set fds
= devices
->infds
;
618 if (select(devices
->max_infd
+1, &fds
, NULL
, NULL
, &poll
) == 0)
621 for (i
= devices
->dev
; i
; i
= i
->next
) {
622 if (i
->handle_input
&& FD_ISSET(i
->fd
, &fds
)) {
623 if (!i
->handle_input(fd
, i
)) {
624 FD_CLR(i
->fd
, &devices
->infds
);
625 /* Tell waker to ignore it too... */
626 write(waker_fd
, &i
->fd
, sizeof(i
->fd
));
633 static struct lguest_device_desc
*
634 new_dev_desc(struct lguest_device_desc
*descs
,
635 u16 type
, u16 features
, u16 num_pages
)
639 for (i
= 0; i
< LGUEST_MAX_DEVICES
; i
++) {
640 if (!descs
[i
].type
) {
641 descs
[i
].type
= type
;
642 descs
[i
].features
= features
;
643 descs
[i
].num_pages
= num_pages
;
645 map_zeroed_pages(top
, num_pages
);
646 descs
[i
].pfn
= top
/getpagesize();
647 top
+= num_pages
*getpagesize();
652 errx(1, "too many devices");
655 static struct device
*new_device(struct device_list
*devices
,
656 u16 type
, u16 num_pages
, u16 features
,
658 bool (*handle_input
)(int, struct device
*),
659 unsigned long watch_off
,
660 u32 (*handle_output
)(int,
661 const struct iovec
*,
665 struct device
*dev
= malloc(sizeof(*dev
));
667 /* Append to device list. */
668 *devices
->lastdev
= dev
;
670 devices
->lastdev
= &dev
->next
;
674 set_fd(dev
->fd
, devices
);
675 dev
->desc
= new_dev_desc(devices
->descs
, type
, features
, num_pages
);
676 dev
->mem
= (void *)(dev
->desc
->pfn
* getpagesize());
677 dev
->handle_input
= handle_input
;
678 dev
->watch_key
= (unsigned long)dev
->mem
+ watch_off
;
679 dev
->handle_output
= handle_output
;
683 static void setup_console(struct device_list
*devices
)
687 if (tcgetattr(STDIN_FILENO
, &orig_term
) == 0) {
688 struct termios term
= orig_term
;
689 term
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
690 tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);
691 atexit(restore_term
);
694 /* We don't currently require a page for the console. */
695 dev
= new_device(devices
, LGUEST_DEVICE_T_CONSOLE
, 0, 0,
696 STDIN_FILENO
, handle_console_input
,
697 LGUEST_CONSOLE_DMA_KEY
, handle_console_output
);
698 dev
->priv
= malloc(sizeof(struct console_abort
));
699 ((struct console_abort
*)dev
->priv
)->count
= 0;
700 verbose("device %p: console\n",
701 (void *)(dev
->desc
->pfn
* getpagesize()));
704 static void setup_block_file(const char *filename
, struct device_list
*devices
)
709 struct lguest_block_page
*p
;
711 fd
= open_or_die(filename
, O_RDWR
|O_LARGEFILE
|O_DIRECT
);
712 dev
= new_device(devices
, LGUEST_DEVICE_T_BLOCK
, 1,
713 LGUEST_DEVICE_F_RANDOMNESS
,
714 fd
, NULL
, 0, handle_block_output
);
715 device_len
= dev
->priv
= malloc(sizeof(*device_len
));
716 *device_len
= lseek64(fd
, 0, SEEK_END
);
719 p
->num_sectors
= *device_len
/512;
720 verbose("device %p: block %i sectors\n",
721 (void *)(dev
->desc
->pfn
* getpagesize()), p
->num_sectors
);
724 /* We use fnctl locks to reserve network slots (autocleanup!) */
725 static unsigned int find_slot(int netfd
, const char *filename
)
730 fl
.l_whence
= SEEK_SET
;
733 fl
.l_start
< getpagesize()/sizeof(struct lguest_net
);
735 if (fcntl(netfd
, F_SETLK
, &fl
) == 0)
738 errx(1, "No free slots in network file %s", filename
);
741 static void setup_net_file(const char *filename
,
742 struct device_list
*devices
)
747 netfd
= open(filename
, O_RDWR
, 0);
749 if (errno
== ENOENT
) {
750 netfd
= open(filename
, O_RDWR
|O_CREAT
, 0600);
752 char page
[getpagesize()];
753 memset(page
, 0, sizeof(page
));
754 write(netfd
, page
, sizeof(page
));
758 err(1, "cannot open net file '%s'", filename
);
761 dev
= new_device(devices
, LGUEST_DEVICE_T_NET
, 1,
762 find_slot(netfd
, filename
)|LGUEST_NET_F_NOCSUM
,
765 /* We overwrite the /dev/zero mapping with the actual file. */
766 if (mmap(dev
->mem
, getpagesize(), PROT_READ
|PROT_WRITE
,
767 MAP_FIXED
|MAP_SHARED
, netfd
, 0) != dev
->mem
)
768 err(1, "could not mmap '%s'", filename
);
769 verbose("device %p: shared net %s, peer %i\n",
770 (void *)(dev
->desc
->pfn
* getpagesize()), filename
,
771 dev
->desc
->features
& ~LGUEST_NET_F_NOCSUM
);
774 static u32
str2ip(const char *ipaddr
)
776 unsigned int byte
[4];
778 sscanf(ipaddr
, "%u.%u.%u.%u", &byte
[0], &byte
[1], &byte
[2], &byte
[3]);
779 return (byte
[0] << 24) | (byte
[1] << 16) | (byte
[2] << 8) | byte
[3];
782 /* adapted from libbridge */
783 static void add_to_bridge(int fd
, const char *if_name
, const char *br_name
)
789 errx(1, "must specify bridge name");
791 ifidx
= if_nametoindex(if_name
);
793 errx(1, "interface %s does not exist!", if_name
);
795 strncpy(ifr
.ifr_name
, br_name
, IFNAMSIZ
);
796 ifr
.ifr_ifindex
= ifidx
;
797 if (ioctl(fd
, SIOCBRADDIF
, &ifr
) < 0)
798 err(1, "can't add %s to bridge %s", if_name
, br_name
);
801 static void configure_device(int fd
, const char *devname
, u32 ipaddr
,
802 unsigned char hwaddr
[6])
805 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
807 memset(&ifr
, 0, sizeof(ifr
));
808 strcpy(ifr
.ifr_name
, devname
);
809 sin
->sin_family
= AF_INET
;
810 sin
->sin_addr
.s_addr
= htonl(ipaddr
);
811 if (ioctl(fd
, SIOCSIFADDR
, &ifr
) != 0)
812 err(1, "Setting %s interface address", devname
);
813 ifr
.ifr_flags
= IFF_UP
;
814 if (ioctl(fd
, SIOCSIFFLAGS
, &ifr
) != 0)
815 err(1, "Bringing interface %s up", devname
);
817 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) != 0)
818 err(1, "getting hw address for %s", devname
);
820 memcpy(hwaddr
, ifr
.ifr_hwaddr
.sa_data
, 6);
823 static void setup_tun_net(const char *arg
, struct device_list
*devices
)
829 const char *br_name
= NULL
;
831 netfd
= open_or_die("/dev/net/tun", O_RDWR
);
832 memset(&ifr
, 0, sizeof(ifr
));
833 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
834 strcpy(ifr
.ifr_name
, "tap%d");
835 if (ioctl(netfd
, TUNSETIFF
, &ifr
) != 0)
836 err(1, "configuring /dev/net/tun");
837 ioctl(netfd
, TUNSETNOCSUM
, 1);
839 /* You will be peer 1: we should create enough jitter to randomize */
840 dev
= new_device(devices
, LGUEST_DEVICE_T_NET
, 1,
841 NET_PEERNUM
|LGUEST_DEVICE_F_RANDOMNESS
, netfd
,
842 handle_tun_input
, peer_offset(0), handle_tun_output
);
843 dev
->priv
= malloc(sizeof(bool));
844 *(bool *)dev
->priv
= false;
846 ipfd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
848 err(1, "opening IP socket");
850 if (!strncmp(BRIDGE_PFX
, arg
, strlen(BRIDGE_PFX
))) {
852 br_name
= arg
+ strlen(BRIDGE_PFX
);
853 add_to_bridge(ipfd
, ifr
.ifr_name
, br_name
);
857 /* We are peer 0, ie. first slot. */
858 configure_device(ipfd
, ifr
.ifr_name
, ip
, dev
->mem
);
860 /* Set "promisc" bit: we want every single packet. */
861 *((u8
*)dev
->mem
) |= 0x1;
865 verbose("device %p: tun net %u.%u.%u.%u\n",
866 (void *)(dev
->desc
->pfn
* getpagesize()),
867 (u8
)(ip
>>24), (u8
)(ip
>>16), (u8
)(ip
>>8), (u8
)ip
);
869 verbose("attached to bridge: %s\n", br_name
);
872 static void __attribute__((noreturn
))
873 run_guest(int lguest_fd
, struct device_list
*device_list
)
876 u32 args
[] = { LHREQ_BREAK
, 0 };
877 unsigned long arr
[2];
880 /* We read from the /dev/lguest device to run the Guest. */
881 readval
= read(lguest_fd
, arr
, sizeof(arr
));
883 if (readval
== sizeof(arr
)) {
884 handle_output(lguest_fd
, arr
[0], arr
[1], device_list
);
886 } else if (errno
== ENOENT
) {
887 char reason
[1024] = { 0 };
888 read(lguest_fd
, reason
, sizeof(reason
)-1);
889 errx(1, "%s", reason
);
890 } else if (errno
!= EAGAIN
)
891 err(1, "Running guest failed");
892 handle_input(lguest_fd
, device_list
);
893 if (write(lguest_fd
, args
, sizeof(args
)) < 0)
894 err(1, "Resetting break");
898 static struct option opts
[] = {
899 { "verbose", 0, NULL
, 'v' },
900 { "sharenet", 1, NULL
, 's' },
901 { "tunnet", 1, NULL
, 't' },
902 { "block", 1, NULL
, 'b' },
903 { "initrd", 1, NULL
, 'i' },
906 static void usage(void)
908 errx(1, "Usage: lguest [--verbose] "
909 "[--sharenet=<filename>|--tunnet=(<ipaddr>|bridge:<bridgename>)\n"
910 "|--block=<filename>|--initrd=<filename>]...\n"
911 "<mem-in-mb> vmlinux [args...]");
914 int main(int argc
, char *argv
[])
916 unsigned long mem
= 0, pgdir
, start
, page_offset
, initrd_size
= 0;
918 struct device_list device_list
;
919 void *boot
= (void *)0;
920 const char *initrd_name
= NULL
;
922 device_list
.max_infd
= -1;
923 device_list
.dev
= NULL
;
924 device_list
.lastdev
= &device_list
.dev
;
925 FD_ZERO(&device_list
.infds
);
927 /* We need to know how much memory so we can allocate devices. */
928 for (i
= 1; i
< argc
; i
++) {
929 if (argv
[i
][0] != '-') {
930 mem
= top
= atoi(argv
[i
]) * 1024 * 1024;
931 device_list
.descs
= map_zeroed_pages(top
, 1);
932 top
+= getpagesize();
936 while ((c
= getopt_long(argc
, argv
, "v", opts
, NULL
)) != EOF
) {
942 setup_net_file(optarg
, &device_list
);
945 setup_tun_net(optarg
, &device_list
);
948 setup_block_file(optarg
, &device_list
);
951 initrd_name
= optarg
;
954 warnx("Unknown argument %s", argv
[optind
]);
958 if (optind
+ 2 > argc
)
961 /* We need a console device */
962 setup_console(&device_list
);
964 /* First we map /dev/zero over all of guest-physical memory. */
965 map_zeroed_pages(0, mem
/ getpagesize());
967 /* Now we load the kernel */
968 start
= load_kernel(open_or_die(argv
[optind
+1], O_RDONLY
),
971 /* Map the initrd image if requested */
973 initrd_size
= load_initrd(initrd_name
, mem
);
974 *(unsigned long *)(boot
+0x218) = mem
- initrd_size
;
975 *(unsigned long *)(boot
+0x21c) = initrd_size
;
976 *(unsigned char *)(boot
+0x210) = 0xFF;
979 /* Set up the initial linar pagetables. */
980 pgdir
= setup_pagetables(mem
, initrd_size
, page_offset
);
982 /* E820 memory map: ours is a simple, single region. */
983 *(char*)(boot
+E820NR
) = 1;
984 *((struct e820entry
*)(boot
+E820MAP
))
985 = ((struct e820entry
) { 0, mem
, E820_RAM
});
986 /* Command line pointer and command line (at 4096) */
987 *(void **)(boot
+ 0x228) = boot
+ 4096;
988 concat(boot
+ 4096, argv
+optind
+2);
989 /* Paravirt type: 1 == lguest */
990 *(int *)(boot
+ 0x23c) = 1;
992 lguest_fd
= tell_kernel(pgdir
, start
, page_offset
);
993 waker_fd
= setup_waker(lguest_fd
, &device_list
);
995 run_guest(lguest_fd
, &device_list
);