Date: Wed, 8 Jul 2015 12:49:49 GMT From: mihai@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r288089 - in soc2015/mihai/bhyve-on-arm-head/usr.sbin: . bhyveloadarm Message-ID: <201507081249.t68CnnFV008215@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: mihai Date: Wed Jul 8 12:49:48 2015 New Revision: 288089 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=288089 Log: soc2015: mihai: bhyve: usr.sbin: bhyveloadarm: loading a guest at a given GPA and set PC at that address Added: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/ soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/Makefile soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/bhyveloadarm.c Modified: soc2015/mihai/bhyve-on-arm-head/usr.sbin/Makefile.arm Modified: soc2015/mihai/bhyve-on-arm-head/usr.sbin/Makefile.arm ============================================================================== --- soc2015/mihai/bhyve-on-arm-head/usr.sbin/Makefile.arm Wed Jul 8 12:45:24 2015 (r288088) +++ soc2015/mihai/bhyve-on-arm-head/usr.sbin/Makefile.arm Wed Jul 8 12:49:48 2015 (r288089) @@ -4,4 +4,5 @@ SUBDIR+= kgmon .if ${MK_BHYVE} != "no" SUBDIR+= bhyvearm +SUBDIR+= bhyveloadarm .endif Added: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/Makefile Wed Jul 8 12:49:48 2015 (r288089) @@ -0,0 +1,15 @@ +# $FreeBSD: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveload/Makefile 283905 2015-04-09 21:38:40Z bapt $ + +PROG= bhyveloadarm +SRCS= bhyveloadarm.c + +.PATH: ${.CURDIR}/../../sys/arm/vmm + +NO_MAN= + +DPADD+= ${LIBVMMAPIARM} +LDADD+= -lvmmapiarm + +WARNS?= 3 + +.include <bsd.prog.mk> Added: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/bhyveloadarm.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyveloadarm/bhyveloadarm.c Wed Jul 8 12:49:48 2015 (r288089) @@ -0,0 +1,167 @@ +#include <sys/cdefs.h> + +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/disk.h> +#include <sys/queue.h> +#include <sys/mman.h> + +#include <machine/vmm.h> + +#include <dirent.h> +#include <dlfcn.h> +#include <errno.h> +#include <err.h> +#include <fcntl.h> +#include <getopt.h> +#include <libgen.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <termios.h> +#include <unistd.h> + +#include <vmmapi.h> + +#define MB (1024 * 1024UL) +#define GB (1024 * 1024 * 1024UL) +#define BSP 0 + +static char *vmname, *progname; +static struct vmctx *ctx; + + +/* + * Guest virtual machinee + */ +static int +guest_copyin(const void *from, uint64_t to, size_t size) +{ + char *ptr; + + ptr = vm_map_gpa(ctx, to, size); + if (ptr == NULL) + return (EFAULT); + + memcpy(ptr, from, size); + return (0); +} + +static int +guest_copyout(uint64_t from, void *to, size_t size) +{ + char *ptr; + + ptr = vm_map_gpa(ctx, from, size); + if (ptr == NULL) + return (EFAULT); + + memcpy(to, ptr, size); + return (0); +} + +static void +guest_setreg(enum vm_reg_name vmreg, uint64_t v) +{ + int error; + + error = vm_set_register(ctx, BSP, vmreg, v); + if (error) { + perror("vm_set_register"); + } +} + +static void +usage(void) +{ + + fprintf(stderr, + "usage: %s [-k <kernel-image>] -l <kernel-load-address>, -b <memory-base-address>\n" + " %*s [-m mem-size] <vmname>\n", + progname, + (int)strlen(progname), ""); + exit(1); +} + +int +main(int argc, char** argv) +{ + uint64_t mem_size; + int opt, error; + int kernel_image_fd; + uint64_t kernel_load_address, memory_base_address; + struct stat st; + void *addr; + + progname = basename(argv[0]); + + mem_size = 256 * MB; + + while ((opt = getopt(argc, argv, "k:l:b:m:")) != -1) { + switch (opt) { + case 'k': + kernel_image_fd = open(optarg, O_RDONLY); + if (kernel_image_fd == -1) { + errx(EX_USAGE, "Could not open '%s'", optarg); + } + break; + case 'l': + kernel_load_address = strtoul(optarg, NULL, 0); + break; + case 'b': + memory_base_address = strtoul(optarg, NULL, 0); + break; + case 'm': + mem_size = strtoul(optarg, NULL, 0) * MB; + break; + case '?': + usage(); + } + } + + argc -= optind; + argv += optind; + + if (argc != 1) + usage(); + + vmname = argv[0]; + + error = vm_create(vmname); + if (error) { + perror("vm_create"); + exit(1); + } + + ctx = vm_open(vmname); + if (ctx == NULL) { + perror("vm_open"); + exit(1); + } + + error = vm_setup_memory(ctx, memory_base_address, mem_size, VM_MMAP_ALL); + if (error) { + perror("vm_setup_memory"); + exit(1); + } + + error = fstat(kernel_image_fd, &st); + if (error) { + perror("fstat"); + exit(1); + } + + addr = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, kernel_image_fd, 0); + if (!addr) { + perror("mmap kernel_image_fd"); + exit(1); + } + + guest_copyin(addr, kernel_load_address, st.st_size); + munmap(addr, st.st_size); + + guest_setreg(VM_REG_GUEST_PC, kernel_load_address); + return 0; +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201507081249.t68CnnFV008215>