1ece09f8fSPekka Enberg #include "kvm/builtin-run.h" 2ece09f8fSPekka Enberg 3c8675741SSasha Levin #include "kvm/builtin-setup.h" 4ece09f8fSPekka Enberg #include "kvm/virtio-balloon.h" 5ece09f8fSPekka Enberg #include "kvm/virtio-console.h" 6ece09f8fSPekka Enberg #include "kvm/parse-options.h" 7ece09f8fSPekka Enberg #include "kvm/8250-serial.h" 8ece09f8fSPekka Enberg #include "kvm/framebuffer.h" 9ece09f8fSPekka Enberg #include "kvm/disk-image.h" 10ece09f8fSPekka Enberg #include "kvm/threadpool.h" 11ece09f8fSPekka Enberg #include "kvm/virtio-blk.h" 12ece09f8fSPekka Enberg #include "kvm/virtio-net.h" 13ece09f8fSPekka Enberg #include "kvm/virtio-rng.h" 14ece09f8fSPekka Enberg #include "kvm/ioeventfd.h" 15ece09f8fSPekka Enberg #include "kvm/virtio-9p.h" 16ece09f8fSPekka Enberg #include "kvm/barrier.h" 17ece09f8fSPekka Enberg #include "kvm/kvm-cpu.h" 18ece09f8fSPekka Enberg #include "kvm/ioport.h" 19ece09f8fSPekka Enberg #include "kvm/symbol.h" 20ece09f8fSPekka Enberg #include "kvm/i8042.h" 21ece09f8fSPekka Enberg #include "kvm/mutex.h" 22ece09f8fSPekka Enberg #include "kvm/term.h" 23ece09f8fSPekka Enberg #include "kvm/util.h" 24ece09f8fSPekka Enberg #include "kvm/vesa.h" 25ece09f8fSPekka Enberg #include "kvm/irq.h" 26ece09f8fSPekka Enberg #include "kvm/kvm.h" 27ece09f8fSPekka Enberg #include "kvm/pci.h" 28ece09f8fSPekka Enberg #include "kvm/rtc.h" 29ece09f8fSPekka Enberg #include "kvm/sdl.h" 30ece09f8fSPekka Enberg #include "kvm/vnc.h" 31e6694207SSasha Levin #include "kvm/guest_compat.h" 3295d13a52SSasha Levin #include "kvm/pci-shmem.h" 33ece09f8fSPekka Enberg 34ece09f8fSPekka Enberg #include <linux/types.h> 35ece09f8fSPekka Enberg 368329f30bSPekka Enberg #include <sys/utsname.h> 378329f30bSPekka Enberg #include <sys/types.h> 388329f30bSPekka Enberg #include <sys/stat.h> 39ece09f8fSPekka Enberg #include <termios.h> 40ece09f8fSPekka Enberg #include <signal.h> 41ece09f8fSPekka Enberg #include <stdlib.h> 42ece09f8fSPekka Enberg #include <string.h> 43ece09f8fSPekka Enberg #include <unistd.h> 4426c853e4SPrasad Joshi #include <ctype.h> 45ece09f8fSPekka Enberg #include <stdio.h> 46f967c427SPrasad Joshi 47f967c427SPrasad Joshi #define DEFAULT_KVM_DEV "/dev/kvm" 489aa4a0ebSAsias He #define DEFAULT_CONSOLE "serial" 49bd30e200SAsias He #define DEFAULT_NETWORK "user" 50745c7231SAsias He #define DEFAULT_HOST_ADDR "192.168.33.1" 51bb8ffd2fSAsias He #define DEFAULT_GUEST_ADDR "192.168.33.15" 527c88c87eSSasha Levin #define DEFAULT_GUEST_MAC "02:15:15:15:15:15" 537c88c87eSSasha Levin #define DEFAULT_HOST_MAC "02:01:01:01:01:01" 5473b7d038SAmos Kong #define DEFAULT_SCRIPT "none" 55f967c427SPrasad Joshi 56f967c427SPrasad Joshi #define MB_SHIFT (20) 5795d13a52SSasha Levin #define KB_SHIFT (10) 5895d13a52SSasha Levin #define GB_SHIFT (30) 59a2a002f9SIngo Molnar #define MIN_RAM_SIZE_MB (64ULL) 60f967c427SPrasad Joshi #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 61f967c427SPrasad Joshi 624298ddadSSasha Levin struct kvm *kvm; 634298ddadSSasha Levin struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 64656be1b8SSasha Levin __thread struct kvm_cpu *current_kvm_cpu; 65f967c427SPrasad Joshi 66fd834defSPekka Enberg static u64 ram_size; 67a33979d8SSasha Levin static u8 image_count; 68cf55d6aeSLiming Wang static bool virtio_rng; 69f967c427SPrasad Joshi static const char *kernel_cmdline; 70f967c427SPrasad Joshi static const char *kernel_filename; 71b0b42ba0SPekka Enberg static const char *vmlinux_filename; 72f967c427SPrasad Joshi static const char *initrd_filename; 73a33979d8SSasha Levin static const char *image_filename[MAX_DISK_IMAGES]; 749aa4a0ebSAsias He static const char *console; 755929637bSAsias He static const char *dev; 764f56d42cSAsias He static const char *network; 77e80ddf6aSAsias He static const char *host_ip; 78bb8ffd2fSAsias He static const char *guest_ip; 79a4e724ddSSasha Levin static const char *guest_mac; 80d7098b9bSAsias He static const char *host_mac; 8173b7d038SAmos Kong static const char *script; 825358b0e6SSasha Levin static const char *guest_name; 830ea58e5bSPekka Enberg static bool single_step; 84a33979d8SSasha Levin static bool readonly_image[MAX_DISK_IMAGES]; 855ffd4178SJohn Floren static bool vnc; 863f838fecSPekka Enberg static bool sdl; 8782d2f21eSSasha Levin static bool balloon; 8859aa2d30SSasha Levin static bool using_rootfs; 8982d65b5eSSasha Levin static bool custom_rootfs; 90f967c427SPrasad Joshi extern bool ioport_debug; 91f967c427SPrasad Joshi extern int active_console; 92aa400b00SPrasad Joshi extern int debug_iodelay; 93f967c427SPrasad Joshi 94ed036f03SCyrill Gorcunov bool do_debug_print = false; 95ed036f03SCyrill Gorcunov 96cfd63bbbSSasha Levin static int nrcpus; 97890364f8SCyrill Gorcunov static int vidmode = -1; 98d77a9efaSCyrill Gorcunov 99f967c427SPrasad Joshi static const char * const run_usage[] = { 1008329f30bSPekka Enberg "kvm run [<options>] [<kernel image>]", 101f967c427SPrasad Joshi NULL 102f967c427SPrasad Joshi }; 103f967c427SPrasad Joshi 104a33979d8SSasha Levin static int img_name_parser(const struct option *opt, const char *arg, int unset) 105a33979d8SSasha Levin { 106a33979d8SSasha Levin char *sep; 10759aa2d30SSasha Levin struct stat st; 10882d65b5eSSasha Levin char path[PATH_MAX]; 10959aa2d30SSasha Levin 11059aa2d30SSasha Levin if (stat(arg, &st) == 0 && 11159aa2d30SSasha Levin S_ISDIR(st.st_mode)) { 11259aa2d30SSasha Levin char tmp[PATH_MAX]; 11359aa2d30SSasha Levin 11459aa2d30SSasha Levin if (realpath(arg, tmp) == 0 || 115c7838fbdSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 11659aa2d30SSasha Levin die("Unable to initialize virtio 9p"); 11759aa2d30SSasha Levin using_rootfs = 1; 11859aa2d30SSasha Levin return 0; 11959aa2d30SSasha Levin } 120a33979d8SSasha Levin 12182d65b5eSSasha Levin snprintf(path, PATH_MAX, "%s%s%s", HOME_DIR, KVM_PID_FILE_PATH, arg); 12282d65b5eSSasha Levin 12382d65b5eSSasha Levin if (stat(path, &st) == 0 && 12482d65b5eSSasha Levin S_ISDIR(st.st_mode)) { 12582d65b5eSSasha Levin char tmp[PATH_MAX]; 12682d65b5eSSasha Levin 12782d65b5eSSasha Levin if (realpath(path, tmp) == 0 || 12882d65b5eSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 12982d65b5eSSasha Levin die("Unable to initialize virtio 9p"); 13082d65b5eSSasha Levin if (virtio_9p__register(kvm, "/", "hostfs") < 0) 13182d65b5eSSasha Levin die("Unable to initialize virtio 9p"); 132*69c88b95SSasha Levin kvm_setup_resolv(arg); 13382d65b5eSSasha Levin using_rootfs = custom_rootfs = 1; 13482d65b5eSSasha Levin return 0; 13582d65b5eSSasha Levin } 13682d65b5eSSasha Levin 137a33979d8SSasha Levin if (image_count >= MAX_DISK_IMAGES) 138a33979d8SSasha Levin die("Currently only 4 images are supported"); 139a33979d8SSasha Levin 140a33979d8SSasha Levin image_filename[image_count] = arg; 141a33979d8SSasha Levin sep = strstr(arg, ","); 142a33979d8SSasha Levin if (sep) { 143a33979d8SSasha Levin if (strcmp(sep + 1, "ro") == 0) 144a33979d8SSasha Levin readonly_image[image_count] = 1; 145a33979d8SSasha Levin *sep = 0; 146a33979d8SSasha Levin } 147a33979d8SSasha Levin 148a33979d8SSasha Levin image_count++; 149a33979d8SSasha Levin 150a33979d8SSasha Levin return 0; 151a33979d8SSasha Levin } 152a33979d8SSasha Levin 153b4422bf3SAneesh Kumar K.V static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 154b4422bf3SAneesh Kumar K.V { 155b4422bf3SAneesh Kumar K.V char *tag_name; 156b4422bf3SAneesh Kumar K.V char tmp[PATH_MAX]; 157b4422bf3SAneesh Kumar K.V 158b4422bf3SAneesh Kumar K.V /* 159b4422bf3SAneesh Kumar K.V * 9p dir can be of the form dirname,tag_name or 160b4422bf3SAneesh Kumar K.V * just dirname. In the later case we use the 161b4422bf3SAneesh Kumar K.V * default tag name 162b4422bf3SAneesh Kumar K.V */ 163b4422bf3SAneesh Kumar K.V tag_name = strstr(arg, ","); 164b4422bf3SAneesh Kumar K.V if (tag_name) { 165b4422bf3SAneesh Kumar K.V *tag_name = '\0'; 166b4422bf3SAneesh Kumar K.V tag_name++; 167b4422bf3SAneesh Kumar K.V } 16854f6802dSPekka Enberg if (realpath(arg, tmp)) { 169c7838fbdSSasha Levin if (virtio_9p__register(kvm, tmp, tag_name) < 0) 17054f6802dSPekka Enberg die("Unable to initialize virtio 9p"); 17154f6802dSPekka Enberg } else 172b4422bf3SAneesh Kumar K.V die("Failed resolving 9p path"); 173b4422bf3SAneesh Kumar K.V return 0; 174b4422bf3SAneesh Kumar K.V } 175b4422bf3SAneesh Kumar K.V 1761add4b76SSasha Levin static int tty_parser(const struct option *opt, const char *arg, int unset) 1771add4b76SSasha Levin { 1781add4b76SSasha Levin int tty = atoi(arg); 1791add4b76SSasha Levin 1801add4b76SSasha Levin term_set_tty(tty); 1811add4b76SSasha Levin 1821add4b76SSasha Levin return 0; 1831add4b76SSasha Levin } 1841add4b76SSasha Levin 18595d13a52SSasha Levin static int shmem_parser(const struct option *opt, const char *arg, int unset) 18695d13a52SSasha Levin { 18772f1ad6dSSasha Levin const u64 default_size = SHMEM_DEFAULT_SIZE; 18872f1ad6dSSasha Levin const u64 default_phys_addr = SHMEM_DEFAULT_ADDR; 18995d13a52SSasha Levin const char *default_handle = SHMEM_DEFAULT_HANDLE; 19095d13a52SSasha Levin struct shmem_info *si = malloc(sizeof(struct shmem_info)); 19172f1ad6dSSasha Levin u64 phys_addr; 19272f1ad6dSSasha Levin u64 size; 19395d13a52SSasha Levin char *handle = NULL; 19495d13a52SSasha Levin int create = 0; 19595d13a52SSasha Levin const char *p = arg; 19695d13a52SSasha Levin char *next; 19795d13a52SSasha Levin int base = 10; 19895d13a52SSasha Levin int verbose = 0; 19995d13a52SSasha Levin 20095d13a52SSasha Levin const int skip_pci = strlen("pci:"); 20195d13a52SSasha Levin if (verbose) 20295d13a52SSasha Levin pr_info("shmem_parser(%p,%s,%d)", opt, arg, unset); 20395d13a52SSasha Levin /* parse out optional addr family */ 20495d13a52SSasha Levin if (strcasestr(p, "pci:")) { 20595d13a52SSasha Levin p += skip_pci; 20695d13a52SSasha Levin } else if (strcasestr(p, "mem:")) { 20795d13a52SSasha Levin die("I can't add to E820 map yet.\n"); 20895d13a52SSasha Levin } 20995d13a52SSasha Levin /* parse out physical addr */ 21095d13a52SSasha Levin base = 10; 21195d13a52SSasha Levin if (strcasestr(p, "0x")) 21295d13a52SSasha Levin base = 16; 21395d13a52SSasha Levin phys_addr = strtoll(p, &next, base); 21495d13a52SSasha Levin if (next == p && phys_addr == 0) { 21595d13a52SSasha Levin pr_info("shmem: no physical addr specified, using default."); 21695d13a52SSasha Levin phys_addr = default_phys_addr; 21795d13a52SSasha Levin } 21895d13a52SSasha Levin if (*next != ':' && *next != '\0') 21995d13a52SSasha Levin die("shmem: unexpected chars after phys addr.\n"); 22095d13a52SSasha Levin if (*next == '\0') 22195d13a52SSasha Levin p = next; 22295d13a52SSasha Levin else 22395d13a52SSasha Levin p = next + 1; 22495d13a52SSasha Levin /* parse out size */ 22595d13a52SSasha Levin base = 10; 22695d13a52SSasha Levin if (strcasestr(p, "0x")) 22795d13a52SSasha Levin base = 16; 22895d13a52SSasha Levin size = strtoll(p, &next, base); 22995d13a52SSasha Levin if (next == p && size == 0) { 23095d13a52SSasha Levin pr_info("shmem: no size specified, using default."); 23195d13a52SSasha Levin size = default_size; 23295d13a52SSasha Levin } 23395d13a52SSasha Levin /* look for [KMGkmg][Bb]* uses base 2. */ 23495d13a52SSasha Levin int skip_B = 0; 23595d13a52SSasha Levin if (strspn(next, "KMGkmg")) { /* might have a prefix */ 23695d13a52SSasha Levin if (*(next + 1) == 'B' || *(next + 1) == 'b') 23795d13a52SSasha Levin skip_B = 1; 23895d13a52SSasha Levin switch (*next) { 23995d13a52SSasha Levin case 'K': 24095d13a52SSasha Levin case 'k': 24195d13a52SSasha Levin size = size << KB_SHIFT; 24295d13a52SSasha Levin break; 24395d13a52SSasha Levin case 'M': 24495d13a52SSasha Levin case 'm': 24595d13a52SSasha Levin size = size << MB_SHIFT; 24695d13a52SSasha Levin break; 24795d13a52SSasha Levin case 'G': 24895d13a52SSasha Levin case 'g': 24995d13a52SSasha Levin size = size << GB_SHIFT; 25095d13a52SSasha Levin break; 25195d13a52SSasha Levin default: 25295d13a52SSasha Levin die("shmem: bug in detecting size prefix."); 25395d13a52SSasha Levin break; 25495d13a52SSasha Levin } 25595d13a52SSasha Levin next += 1 + skip_B; 25695d13a52SSasha Levin } 25795d13a52SSasha Levin if (*next != ':' && *next != '\0') { 25895d13a52SSasha Levin die("shmem: unexpected chars after phys size. <%c><%c>\n", 25995d13a52SSasha Levin *next, *p); 26095d13a52SSasha Levin } 26195d13a52SSasha Levin if (*next == '\0') 26295d13a52SSasha Levin p = next; 26395d13a52SSasha Levin else 26495d13a52SSasha Levin p = next + 1; 26595d13a52SSasha Levin /* parse out optional shmem handle */ 26695d13a52SSasha Levin const int skip_handle = strlen("handle="); 26795d13a52SSasha Levin next = strcasestr(p, "handle="); 26895d13a52SSasha Levin if (*p && next) { 26995d13a52SSasha Levin if (p != next) 27095d13a52SSasha Levin die("unexpected chars before handle\n"); 27195d13a52SSasha Levin p += skip_handle; 27295d13a52SSasha Levin next = strchrnul(p, ':'); 27395d13a52SSasha Levin if (next - p) { 27495d13a52SSasha Levin handle = malloc(next - p + 1); 27595d13a52SSasha Levin strncpy(handle, p, next - p); 27695d13a52SSasha Levin handle[next - p] = '\0'; /* just in case. */ 27795d13a52SSasha Levin } 27895d13a52SSasha Levin if (*next == '\0') 27995d13a52SSasha Levin p = next; 28095d13a52SSasha Levin else 28195d13a52SSasha Levin p = next + 1; 28295d13a52SSasha Levin } 28395d13a52SSasha Levin /* parse optional create flag to see if we should create shm seg. */ 28495d13a52SSasha Levin if (*p && strcasestr(p, "create")) { 28595d13a52SSasha Levin create = 1; 28695d13a52SSasha Levin p += strlen("create"); 28795d13a52SSasha Levin } 28895d13a52SSasha Levin if (*p != '\0') 28995d13a52SSasha Levin die("shmem: unexpected trailing chars\n"); 29095d13a52SSasha Levin if (handle == NULL) { 29195d13a52SSasha Levin handle = malloc(strlen(default_handle) + 1); 29295d13a52SSasha Levin strcpy(handle, default_handle); 29395d13a52SSasha Levin } 29495d13a52SSasha Levin if (verbose) { 29572f1ad6dSSasha Levin pr_info("shmem: phys_addr = %llx", phys_addr); 29672f1ad6dSSasha Levin pr_info("shmem: size = %llx", size); 29795d13a52SSasha Levin pr_info("shmem: handle = %s", handle); 29895d13a52SSasha Levin pr_info("shmem: create = %d", create); 29995d13a52SSasha Levin } 30095d13a52SSasha Levin 30195d13a52SSasha Levin si->phys_addr = phys_addr; 30295d13a52SSasha Levin si->size = size; 30395d13a52SSasha Levin si->handle = handle; 30495d13a52SSasha Levin si->create = create; 30595d13a52SSasha Levin pci_shmem__register_mem(si); /* ownership of si, etc. passed on. */ 30695d13a52SSasha Levin return 0; 30795d13a52SSasha Levin } 308b4422bf3SAneesh Kumar K.V 309f967c427SPrasad Joshi static const struct option options[] = { 3105e3af62dSSasha Levin OPT_GROUP("Basic options:"), 3115358b0e6SSasha Levin OPT_STRING('\0', "name", &guest_name, "guest name", 3125358b0e6SSasha Levin "A name for the guest"), 31380b1f72fSPekka Enberg OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"), 314f967c427SPrasad Joshi OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 31595d13a52SSasha Levin OPT_CALLBACK('\0', "shmem", NULL, 31695d13a52SSasha Levin "[pci:]<addr>:<size>[:handle=<handle>][:create]", 31795d13a52SSasha Levin "Share host shmem with guest via pci device", 31895d13a52SSasha Levin shmem_parser), 319cadb9ab7SAsias He OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser), 32082d2f21eSSasha Levin OPT_BOOLEAN('\0', "balloon", &balloon, "Enable virtio balloon"), 3215ffd4178SJohn Floren OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"), 3223f838fecSPekka Enberg OPT_BOOLEAN('\0', "sdl", &sdl, "Enable SDL framebuffer"), 323cf55d6aeSLiming Wang OPT_BOOLEAN('\0', "rng", &virtio_rng, "Enable virtio Random Number Generator"), 3248b7565e8SAsias He OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", 3258b7565e8SAsias He "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser), 3268b7565e8SAsias He OPT_STRING('\0', "console", &console, "serial or virtio", 3278b7565e8SAsias He "Console to use"), 3288b7565e8SAsias He OPT_STRING('\0', "dev", &dev, "device_file", "KVM device file"), 3291add4b76SSasha Levin OPT_CALLBACK('\0', "tty", NULL, "tty id", 3301add4b76SSasha Levin "Remap guest TTY into a pty on the host", 3311add4b76SSasha Levin tty_parser), 3325e3af62dSSasha Levin 3335e3af62dSSasha Levin OPT_GROUP("Kernel options:"), 3345e3af62dSSasha Levin OPT_STRING('k', "kernel", &kernel_filename, "kernel", 3355e3af62dSSasha Levin "Kernel to boot in virtual machine"), 336d6c19027SAsias He OPT_STRING('i', "initrd", &initrd_filename, "initrd", 3375e3af62dSSasha Levin "Initial RAM disk image"), 3385e3af62dSSasha Levin OPT_STRING('p', "params", &kernel_cmdline, "params", 3395e3af62dSSasha Levin "Kernel command line arguments"), 3405e3af62dSSasha Levin 3414d67c820SSasha Levin OPT_GROUP("Networking options:"), 342ec7674d7SAsias He OPT_STRING('n', "network", &network, "user, tap, none", 3434d67c820SSasha Levin "Network to use"), 344e80ddf6aSAsias He OPT_STRING('\0', "host-ip", &host_ip, "a.b.c.d", 3454d67c820SSasha Levin "Assign this address to the host side networking"), 346bb8ffd2fSAsias He OPT_STRING('\0', "guest-ip", &guest_ip, "a.b.c.d", 347bb8ffd2fSAsias He "Assign this address to the guest side networking"), 348d7098b9bSAsias He OPT_STRING('\0', "host-mac", &host_mac, "aa:bb:cc:dd:ee:ff", 349d7098b9bSAsias He "Assign this address to the host side NIC"), 350a4e724ddSSasha Levin OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff", 351a4e724ddSSasha Levin "Assign this address to the guest side NIC"), 35273b7d038SAmos Kong OPT_STRING('\0', "tapscript", &script, "Script path", 35373b7d038SAmos Kong "Assign a script to process created tap device"), 354d6c19027SAsias He 355890364f8SCyrill Gorcunov OPT_GROUP("BIOS options:"), 356890364f8SCyrill Gorcunov OPT_INTEGER('\0', "vidmode", &vidmode, 357890364f8SCyrill Gorcunov "Video mode"), 358890364f8SCyrill Gorcunov 3595e3af62dSSasha Levin OPT_GROUP("Debug options:"), 360ed036f03SCyrill Gorcunov OPT_BOOLEAN('\0', "debug", &do_debug_print, 361ed036f03SCyrill Gorcunov "Enable debug messages"), 362d6c19027SAsias He OPT_BOOLEAN('\0', "debug-single-step", &single_step, 363d6c19027SAsias He "Enable single stepping"), 364001690a4SSasha Levin OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug, 365d6c19027SAsias He "Enable ioport debugging"), 366001690a4SSasha Levin OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay, 367aa400b00SPrasad Joshi "Delay IO by millisecond"), 368f967c427SPrasad Joshi OPT_END() 369f967c427SPrasad Joshi }; 370f967c427SPrasad Joshi 371b5b501ccSIngo Molnar /* 372b5b501ccSIngo Molnar * Serialize debug printout so that the output of multiple vcpus does not 373b5b501ccSIngo Molnar * get mixed up: 374b5b501ccSIngo Molnar */ 375cffeab25SIngo Molnar static int printout_done; 376b5b501ccSIngo Molnar 37743119af7SPekka Enberg static void handle_sigusr1(int sig) 37843119af7SPekka Enberg { 37943119af7SPekka Enberg struct kvm_cpu *cpu = current_kvm_cpu; 38043119af7SPekka Enberg 38143119af7SPekka Enberg if (!cpu) 38243119af7SPekka Enberg return; 38343119af7SPekka Enberg 384b5b501ccSIngo Molnar printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id); 38543119af7SPekka Enberg kvm_cpu__show_registers(cpu); 38643119af7SPekka Enberg kvm_cpu__show_code(cpu); 38743119af7SPekka Enberg kvm_cpu__show_page_tables(cpu); 388b5b501ccSIngo Molnar fflush(stdout); 389cffeab25SIngo Molnar printout_done = 1; 390cffeab25SIngo Molnar mb(); 39143119af7SPekka Enberg } 39243119af7SPekka Enberg 393d94e22b9SSasha Levin /* Pause/resume the guest using SIGUSR2 */ 394d94e22b9SSasha Levin static int is_paused; 395d94e22b9SSasha Levin 396d94e22b9SSasha Levin static void handle_sigusr2(int sig) 397d94e22b9SSasha Levin { 39802317b74SSasha Levin if (sig == SIGKVMRESUME && is_paused) 399d94e22b9SSasha Levin kvm__continue(); 40002317b74SSasha Levin else if (sig == SIGUSR2 && !is_paused) 401d94e22b9SSasha Levin kvm__pause(); 40202317b74SSasha Levin else 40302317b74SSasha Levin return; 404d94e22b9SSasha Levin 405d94e22b9SSasha Levin is_paused = !is_paused; 406d94e22b9SSasha Levin pr_info("Guest %s\n", is_paused ? "paused" : "resumed"); 407d94e22b9SSasha Levin } 408d94e22b9SSasha Levin 4099e854d1aSPekka Enberg static void handle_sigquit(int sig) 4109e854d1aSPekka Enberg { 4119e854d1aSPekka Enberg int i; 4129e854d1aSPekka Enberg 4139e854d1aSPekka Enberg for (i = 0; i < nrcpus; i++) { 4149e854d1aSPekka Enberg struct kvm_cpu *cpu = kvm_cpus[i]; 4159e854d1aSPekka Enberg 41643119af7SPekka Enberg if (!cpu) 41743119af7SPekka Enberg continue; 41843119af7SPekka Enberg 419cffeab25SIngo Molnar printout_done = 0; 42043119af7SPekka Enberg pthread_kill(cpu->thread, SIGUSR1); 421cffeab25SIngo Molnar /* 422cffeab25SIngo Molnar * Wait for the vCPU to dump state before signalling 423cffeab25SIngo Molnar * the next thread. Since this is debug code it does 424cffeab25SIngo Molnar * not matter that we are burning CPU time a bit: 425cffeab25SIngo Molnar */ 426cffeab25SIngo Molnar while (!printout_done) 427cffeab25SIngo Molnar mb(); 4289e854d1aSPekka Enberg } 4299e854d1aSPekka Enberg 4309e854d1aSPekka Enberg serial8250__inject_sysrq(kvm); 4319e854d1aSPekka Enberg } 4329e854d1aSPekka Enberg 4339e854d1aSPekka Enberg static void handle_sigalrm(int sig) 4349e854d1aSPekka Enberg { 4359e854d1aSPekka Enberg serial8250__inject_interrupt(kvm); 4369e854d1aSPekka Enberg virtio_console__inject_interrupt(kvm); 4379e854d1aSPekka Enberg } 4389e854d1aSPekka Enberg 439daf4cb5aSSasha Levin static void handle_sigstop(int sig) 440daf4cb5aSSasha Levin { 441daf4cb5aSSasha Levin kvm_cpu__reboot(); 442daf4cb5aSSasha Levin } 443daf4cb5aSSasha Levin 4445ee154d1SPekka Enberg static void *kvm_cpu_thread(void *arg) 4455ee154d1SPekka Enberg { 446d77a9efaSCyrill Gorcunov current_kvm_cpu = arg; 4475ee154d1SPekka Enberg 448d77a9efaSCyrill Gorcunov if (kvm_cpu__start(current_kvm_cpu)) 4495ee154d1SPekka Enberg goto panic_kvm; 4505ee154d1SPekka Enberg 451d77a9efaSCyrill Gorcunov kvm_cpu__delete(current_kvm_cpu); 4525ee154d1SPekka Enberg 4535ee154d1SPekka Enberg return (void *) (intptr_t) 0; 4545ee154d1SPekka Enberg 4555ee154d1SPekka Enberg panic_kvm: 4563fdf659dSSasha Levin fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 457d77a9efaSCyrill Gorcunov current_kvm_cpu->kvm_run->exit_reason, 458d77a9efaSCyrill Gorcunov kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 459d77a9efaSCyrill Gorcunov if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 4605ee154d1SPekka Enberg fprintf(stderr, "KVM exit code: 0x%Lu\n", 461d77a9efaSCyrill Gorcunov current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 4628e5accedSPekka Enberg 463d77a9efaSCyrill Gorcunov kvm_cpu__show_registers(current_kvm_cpu); 464d77a9efaSCyrill Gorcunov kvm_cpu__show_code(current_kvm_cpu); 465d77a9efaSCyrill Gorcunov kvm_cpu__show_page_tables(current_kvm_cpu); 4665ee154d1SPekka Enberg 467d77a9efaSCyrill Gorcunov kvm_cpu__delete(current_kvm_cpu); 4685ee154d1SPekka Enberg 4695ee154d1SPekka Enberg return (void *) (intptr_t) 1; 4705ee154d1SPekka Enberg } 4715ee154d1SPekka Enberg 472e08c0896SPrasad Joshi static char kernel[PATH_MAX]; 473b0b42ba0SPekka Enberg 474b0b42ba0SPekka Enberg static const char *host_kernels[] = { 475e08c0896SPrasad Joshi "/boot/vmlinuz", 476e08c0896SPrasad Joshi "/boot/bzImage", 477e08c0896SPrasad Joshi NULL 478e08c0896SPrasad Joshi }; 479b0b42ba0SPekka Enberg 480b0b42ba0SPekka Enberg static const char *default_kernels[] = { 481e08c0896SPrasad Joshi "./bzImage", 482e08c0896SPrasad Joshi "../../arch/x86/boot/bzImage", 483e08c0896SPrasad Joshi NULL 484e08c0896SPrasad Joshi }; 4858329f30bSPekka Enberg 486b0b42ba0SPekka Enberg static const char *default_vmlinux[] = { 487b0b42ba0SPekka Enberg "../../../vmlinux", 488b0b42ba0SPekka Enberg "../../vmlinux", 489b0b42ba0SPekka Enberg NULL 490b0b42ba0SPekka Enberg }; 491b0b42ba0SPekka Enberg 492e08c0896SPrasad Joshi static void kernel_usage_with_options(void) 4938329f30bSPekka Enberg { 494e08c0896SPrasad Joshi const char **k; 4958329f30bSPekka Enberg struct utsname uts; 496e08c0896SPrasad Joshi 497e08c0896SPrasad Joshi fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 49865182f37SPrasad Joshi k = &default_kernels[0]; 499e08c0896SPrasad Joshi while (*k) { 500e08c0896SPrasad Joshi fprintf(stderr, "\t%s\n", *k); 501e08c0896SPrasad Joshi k++; 502e08c0896SPrasad Joshi } 503e08c0896SPrasad Joshi 504e08c0896SPrasad Joshi if (uname(&uts) < 0) 505e08c0896SPrasad Joshi return; 506e08c0896SPrasad Joshi 507e08c0896SPrasad Joshi k = &host_kernels[0]; 508e08c0896SPrasad Joshi while (*k) { 509e08c0896SPrasad Joshi if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 510e08c0896SPrasad Joshi return; 511e08c0896SPrasad Joshi fprintf(stderr, "\t%s\n", kernel); 512e08c0896SPrasad Joshi k++; 513e08c0896SPrasad Joshi } 514a33389d8SPrasad Joshi fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n"); 515e08c0896SPrasad Joshi } 516e08c0896SPrasad Joshi 51760ded003SPekka Enberg static u64 host_ram_size(void) 51860ded003SPekka Enberg { 51960ded003SPekka Enberg long page_size; 52060ded003SPekka Enberg long nr_pages; 52160ded003SPekka Enberg 52260ded003SPekka Enberg nr_pages = sysconf(_SC_PHYS_PAGES); 523d63c5ce6SPekka Enberg if (nr_pages < 0) { 5244542f276SCyrill Gorcunov pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 525d63c5ce6SPekka Enberg return 0; 526d63c5ce6SPekka Enberg } 52760ded003SPekka Enberg 52860ded003SPekka Enberg page_size = sysconf(_SC_PAGE_SIZE); 529d63c5ce6SPekka Enberg if (page_size < 0) { 5304542f276SCyrill Gorcunov pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 531d63c5ce6SPekka Enberg return 0; 532d63c5ce6SPekka Enberg } 53360ded003SPekka Enberg 53460ded003SPekka Enberg return (nr_pages * page_size) >> MB_SHIFT; 53560ded003SPekka Enberg } 53660ded003SPekka Enberg 53718bd8c3bSPekka Enberg /* 53818bd8c3bSPekka Enberg * If user didn't specify how much memory it wants to allocate for the guest, 53918bd8c3bSPekka Enberg * avoid filling the whole host RAM. 54018bd8c3bSPekka Enberg */ 54118bd8c3bSPekka Enberg #define RAM_SIZE_RATIO 0.8 54218bd8c3bSPekka Enberg 543fd834defSPekka Enberg static u64 get_ram_size(int nr_cpus) 544fd834defSPekka Enberg { 54506761c76SPekka Enberg u64 available; 54606761c76SPekka Enberg u64 ram_size; 547fd834defSPekka Enberg 548fd834defSPekka Enberg ram_size = 64 * (nr_cpus + 3); 549fd834defSPekka Enberg 55060ded003SPekka Enberg available = host_ram_size() * RAM_SIZE_RATIO; 551d63c5ce6SPekka Enberg if (!available) 552d63c5ce6SPekka Enberg available = MIN_RAM_SIZE_MB; 553fd834defSPekka Enberg 554fd834defSPekka Enberg if (ram_size > available) 555fd834defSPekka Enberg ram_size = available; 556fd834defSPekka Enberg 557fd834defSPekka Enberg return ram_size; 558fd834defSPekka Enberg } 559fd834defSPekka Enberg 560e08c0896SPrasad Joshi static const char *find_kernel(void) 561e08c0896SPrasad Joshi { 562e08c0896SPrasad Joshi const char **k; 5638329f30bSPekka Enberg struct stat st; 564e08c0896SPrasad Joshi struct utsname uts; 565e08c0896SPrasad Joshi 56665182f37SPrasad Joshi k = &default_kernels[0]; 567e08c0896SPrasad Joshi while (*k) { 568e08c0896SPrasad Joshi if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 569e08c0896SPrasad Joshi k++; 570e08c0896SPrasad Joshi continue; 571e08c0896SPrasad Joshi } 572e08c0896SPrasad Joshi strncpy(kernel, *k, PATH_MAX); 573e08c0896SPrasad Joshi return kernel; 574e08c0896SPrasad Joshi } 5758329f30bSPekka Enberg 5768329f30bSPekka Enberg if (uname(&uts) < 0) 5778329f30bSPekka Enberg return NULL; 5788329f30bSPekka Enberg 579e08c0896SPrasad Joshi k = &host_kernels[0]; 580e08c0896SPrasad Joshi while (*k) { 581e08c0896SPrasad Joshi if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 5828329f30bSPekka Enberg return NULL; 5838329f30bSPekka Enberg 584e08c0896SPrasad Joshi if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 585e08c0896SPrasad Joshi k++; 586e08c0896SPrasad Joshi continue; 587e08c0896SPrasad Joshi } 588e08c0896SPrasad Joshi return kernel; 5898329f30bSPekka Enberg 590e08c0896SPrasad Joshi } 5918329f30bSPekka Enberg return NULL; 5928329f30bSPekka Enberg } 5938329f30bSPekka Enberg 594b0b42ba0SPekka Enberg static const char *find_vmlinux(void) 595b0b42ba0SPekka Enberg { 596b0b42ba0SPekka Enberg const char **vmlinux; 597b0b42ba0SPekka Enberg 598b0b42ba0SPekka Enberg vmlinux = &default_vmlinux[0]; 599b0b42ba0SPekka Enberg while (*vmlinux) { 600b0b42ba0SPekka Enberg struct stat st; 601b0b42ba0SPekka Enberg 602b0b42ba0SPekka Enberg if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 603b0b42ba0SPekka Enberg vmlinux++; 604b0b42ba0SPekka Enberg continue; 605b0b42ba0SPekka Enberg } 606b0b42ba0SPekka Enberg return *vmlinux; 607b0b42ba0SPekka Enberg } 608b0b42ba0SPekka Enberg return NULL; 609b0b42ba0SPekka Enberg } 610b0b42ba0SPekka Enberg 611f6677a1dSAmerigo Wang void kvm_run_help(void) 612f6677a1dSAmerigo Wang { 613f6677a1dSAmerigo Wang usage_with_options(run_usage, options); 614f6677a1dSAmerigo Wang } 615f6677a1dSAmerigo Wang 616f967c427SPrasad Joshi int kvm_cmd_run(int argc, const char **argv, const char *prefix) 617f967c427SPrasad Joshi { 618b0b42ba0SPekka Enberg struct virtio_net_parameters net_params; 6192d96f6b6SSasha Levin static char real_cmdline[2048], default_name[20]; 620aba1efa5SPekka Enberg struct framebuffer *fb = NULL; 621384922b3SPekka Enberg unsigned int nr_online_cpus; 6225ee154d1SPekka Enberg int exit_code = 0; 6238259b8ccSSasha Levin int max_cpus, recommended_cpus; 624b0b42ba0SPekka Enberg int i; 62549e5227dSSasha Levin void *ret; 626f967c427SPrasad Joshi 6275ee154d1SPekka Enberg signal(SIGALRM, handle_sigalrm); 628f967c427SPrasad Joshi signal(SIGQUIT, handle_sigquit); 62943119af7SPekka Enberg signal(SIGUSR1, handle_sigusr1); 630d94e22b9SSasha Levin signal(SIGUSR2, handle_sigusr2); 631daf4cb5aSSasha Levin signal(SIGKVMSTOP, handle_sigstop); 63202317b74SSasha Levin signal(SIGKVMRESUME, handle_sigusr2); 63354a4c4a6SLiming Wang /* ignore balloon signal by default if not enable balloon optiion */ 63454a4c4a6SLiming Wang signal(SIGKVMADDMEM, SIG_IGN); 63554a4c4a6SLiming Wang signal(SIGKVMDELMEM, SIG_IGN); 636f967c427SPrasad Joshi 637cfd63bbbSSasha Levin nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 638cfd63bbbSSasha Levin 639f967c427SPrasad Joshi while (argc != 0) { 640f967c427SPrasad Joshi argc = parse_options(argc, argv, options, run_usage, 641f967c427SPrasad Joshi PARSE_OPT_STOP_AT_NON_OPTION); 642f967c427SPrasad Joshi if (argc != 0) { 643f967c427SPrasad Joshi if (kernel_filename) { 644f967c427SPrasad Joshi fprintf(stderr, "Cannot handle parameter: " 645f967c427SPrasad Joshi "%s\n", argv[0]); 646f967c427SPrasad Joshi usage_with_options(run_usage, options); 647f967c427SPrasad Joshi return EINVAL; 648f967c427SPrasad Joshi } 649f967c427SPrasad Joshi /* first unhandled parameter is treated as a kernel 650f967c427SPrasad Joshi image 651f967c427SPrasad Joshi */ 652f967c427SPrasad Joshi kernel_filename = argv[0]; 653f967c427SPrasad Joshi argv++; 654f967c427SPrasad Joshi argc--; 655f967c427SPrasad Joshi } 656f967c427SPrasad Joshi 657f967c427SPrasad Joshi } 658f967c427SPrasad Joshi 65945bc2f02SPekka Enberg if (!kernel_filename) 660e08c0896SPrasad Joshi kernel_filename = find_kernel(); 6618329f30bSPekka Enberg 6628329f30bSPekka Enberg if (!kernel_filename) { 663e08c0896SPrasad Joshi kernel_usage_with_options(); 6648329f30bSPekka Enberg return EINVAL; 6658329f30bSPekka Enberg } 6668329f30bSPekka Enberg 667b0b42ba0SPekka Enberg vmlinux_filename = find_vmlinux(); 668b0b42ba0SPekka Enberg 669cfd63bbbSSasha Levin if (nrcpus == 0) 670cfd63bbbSSasha Levin nrcpus = nr_online_cpus; 671cfd63bbbSSasha Levin else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 672d77a9efaSCyrill Gorcunov die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 673d77a9efaSCyrill Gorcunov 674fd834defSPekka Enberg if (!ram_size) 675fd834defSPekka Enberg ram_size = get_ram_size(nrcpus); 676fd834defSPekka Enberg 677a2a002f9SIngo Molnar if (ram_size < MIN_RAM_SIZE_MB) 678a2a002f9SIngo Molnar die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 679a2a002f9SIngo Molnar 68060ded003SPekka Enberg if (ram_size > host_ram_size()) 6814542f276SCyrill Gorcunov pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size()); 68260ded003SPekka Enberg 683f967c427SPrasad Joshi ram_size <<= MB_SHIFT; 684f967c427SPrasad Joshi 6855929637bSAsias He if (!dev) 6865929637bSAsias He dev = DEFAULT_KVM_DEV; 687f967c427SPrasad Joshi 6889aa4a0ebSAsias He if (!console) 6899aa4a0ebSAsias He console = DEFAULT_CONSOLE; 6909aa4a0ebSAsias He 6919aa4a0ebSAsias He if (!strncmp(console, "virtio", 6)) 692f967c427SPrasad Joshi active_console = CONSOLE_VIRTIO; 6939aa4a0ebSAsias He else 6949aa4a0ebSAsias He active_console = CONSOLE_8250; 695f967c427SPrasad Joshi 696e80ddf6aSAsias He if (!host_ip) 697e80ddf6aSAsias He host_ip = DEFAULT_HOST_ADDR; 6984d67c820SSasha Levin 699bb8ffd2fSAsias He if (!guest_ip) 700bb8ffd2fSAsias He guest_ip = DEFAULT_GUEST_ADDR; 701bb8ffd2fSAsias He 702a4e724ddSSasha Levin if (!guest_mac) 703a4e724ddSSasha Levin guest_mac = DEFAULT_GUEST_MAC; 704a4e724ddSSasha Levin 705d7098b9bSAsias He if (!host_mac) 706d7098b9bSAsias He host_mac = DEFAULT_HOST_MAC; 707d7098b9bSAsias He 70873b7d038SAmos Kong if (!script) 70973b7d038SAmos Kong script = DEFAULT_SCRIPT; 71073b7d038SAmos Kong 711b0b42ba0SPekka Enberg symbol__init(vmlinux_filename); 712b0b42ba0SPekka Enberg 713f967c427SPrasad Joshi term_init(); 714f967c427SPrasad Joshi 7152d96f6b6SSasha Levin if (!guest_name) { 7162d96f6b6SSasha Levin sprintf(default_name, "guest-%u", getpid()); 7172d96f6b6SSasha Levin guest_name = default_name; 7182d96f6b6SSasha Levin } 7192d96f6b6SSasha Levin 7205929637bSAsias He kvm = kvm__init(dev, ram_size, guest_name); 721f967c427SPrasad Joshi 7225f0a22b7SSasha Levin irq__init(kvm); 7235f0a22b7SSasha Levin 7246d6a4d41SPekka Enberg kvm->single_step = single_step; 7256d6a4d41SPekka Enberg 72637f3d50eSSasha Levin ioeventfd__init(); 72737f3d50eSSasha Levin 728384922b3SPekka Enberg max_cpus = kvm__max_cpus(kvm); 7298259b8ccSSasha Levin recommended_cpus = kvm__recommended_cpus(kvm); 730384922b3SPekka Enberg 731384922b3SPekka Enberg if (nrcpus > max_cpus) { 732384922b3SPekka Enberg printf(" # Limit the number of CPUs to %d\n", max_cpus); 733384922b3SPekka Enberg kvm->nrcpus = max_cpus; 7348259b8ccSSasha Levin } else if (nrcpus > recommended_cpus) { 7358259b8ccSSasha Levin printf(" # Warning: The maximum recommended amount of VCPUs" 7368259b8ccSSasha Levin " is %d\n", recommended_cpus); 737384922b3SPekka Enberg } 738384922b3SPekka Enberg 7390c7c14a7SCyrill Gorcunov kvm->nrcpus = nrcpus; 7400c7c14a7SCyrill Gorcunov 741890364f8SCyrill Gorcunov /* 742890364f8SCyrill Gorcunov * vidmode should be either specified 743890364f8SCyrill Gorcunov * either set by default 744890364f8SCyrill Gorcunov */ 745890364f8SCyrill Gorcunov if (vnc || sdl) { 746890364f8SCyrill Gorcunov if (vidmode == -1) 747890364f8SCyrill Gorcunov vidmode = 0x312; 748890364f8SCyrill Gorcunov } else 749890364f8SCyrill Gorcunov vidmode = 0; 750890364f8SCyrill Gorcunov 75126c853e4SPrasad Joshi memset(real_cmdline, 0, sizeof(real_cmdline)); 752d1f3131dSSasha Levin strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 reboot=k panic=1 i8042.direct=1 " 753d1f3131dSSasha Levin "i8042.dumbkbd=1 i8042.nopnp=1"); 7543f838fecSPekka Enberg if (vnc || sdl) { 7555ffd4178SJohn Floren strcat(real_cmdline, " video=vesafb console=tty0"); 756890364f8SCyrill Gorcunov } else 757d1f3131dSSasha Levin strcat(real_cmdline, " console=ttyS0 earlyprintk=serial i8042.noaux=1"); 758d74181ccSSasha Levin strcat(real_cmdline, " "); 75926c853e4SPrasad Joshi if (kernel_cmdline) 76026c853e4SPrasad Joshi strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 76126c853e4SPrasad Joshi 76259aa2d30SSasha Levin if (!using_rootfs && !image_filename[0]) { 763c8675741SSasha Levin char tmp[PATH_MAX]; 7646df1471eSPekka Enberg 765c8675741SSasha Levin kvm_setup_create_new("default"); 766*69c88b95SSasha Levin kvm_setup_resolv("default"); 767c8675741SSasha Levin 768c8675741SSasha Levin snprintf(tmp, PATH_MAX, "%s%s%s", HOME_DIR, KVM_PID_FILE_PATH, "default"); 769c8675741SSasha Levin if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 770c8675741SSasha Levin die("Unable to initialize virtio 9p"); 771c8675741SSasha Levin if (virtio_9p__register(kvm, "/", "hostfs") < 0) 772c8675741SSasha Levin die("Unable to initialize virtio 9p"); 773c8675741SSasha Levin using_rootfs = custom_rootfs = 1; 7746df1471eSPekka Enberg 7756df1471eSPekka Enberg if (!strstr(real_cmdline, "init=")) 7765fd5abefSPekka Enberg strlcat(real_cmdline, " init=/bin/sh ", sizeof(real_cmdline)); 77726c853e4SPrasad Joshi } 77826c853e4SPrasad Joshi 77982d65b5eSSasha Levin if (using_rootfs) { 780ff42603fSSasha Levin strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); 78182d65b5eSSasha Levin if (custom_rootfs) 782575a9e3fSSasha Levin strcat(real_cmdline, " init=/virt/init ip=dhcp"); 78382d65b5eSSasha Levin } else if (!strstr(real_cmdline, "root=")) { 784ff42603fSSasha Levin strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 78582d65b5eSSasha Levin } 78659aa2d30SSasha Levin 787c1ed214eSPrasad Joshi if (image_count) { 788bcb6aacaSPrasad Joshi kvm->nr_disks = image_count; 789c1ed214eSPrasad Joshi kvm->disks = disk_image__open_all(image_filename, readonly_image, image_count); 790c1ed214eSPrasad Joshi if (!kvm->disks) 791c1ed214eSPrasad Joshi die("Unable to load all disk images."); 79238605e1cSSasha Levin 793bcb6aacaSPrasad Joshi virtio_blk__init_all(kvm); 794f967c427SPrasad Joshi } 795c1ed214eSPrasad Joshi 7962d96f6b6SSasha Levin printf(" # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename, ram_size / 1024 / 1024, nrcpus, guest_name); 797471c6facSPekka Enberg 798f967c427SPrasad Joshi if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 7995ffd4178SJohn Floren real_cmdline, vidmode)) 800f967c427SPrasad Joshi die("unable to load kernel %s", kernel_filename); 801f967c427SPrasad Joshi 802b0b42ba0SPekka Enberg kvm->vmlinux = vmlinux_filename; 803b0b42ba0SPekka Enberg 804ac38f433SPekka Enberg ioport__setup_legacy(); 805ac38f433SPekka Enberg 80664136c1cSPekka Enberg rtc__init(); 80764136c1cSPekka Enberg 808f967c427SPrasad Joshi serial8250__init(kvm); 809f967c427SPrasad Joshi 810f967c427SPrasad Joshi pci__init(); 811f967c427SPrasad Joshi 8121621292eSSasha Levin if (active_console == CONSOLE_VIRTIO) 813f967c427SPrasad Joshi virtio_console__init(kvm); 814f967c427SPrasad Joshi 81553eca082SSasha Levin if (virtio_rng) 81653eca082SSasha Levin virtio_rng__init(kvm); 81753eca082SSasha Levin 81882d2f21eSSasha Levin if (balloon) 81982d2f21eSSasha Levin virtio_bln__init(kvm); 82082d2f21eSSasha Levin 8214f56d42cSAsias He if (!network) 8224f56d42cSAsias He network = DEFAULT_NETWORK; 8234f56d42cSAsias He 824c7838fbdSSasha Levin virtio_9p__init(kvm); 825c7838fbdSSasha Levin 826ec7674d7SAsias He if (strncmp(network, "none", 4)) { 827bb8ffd2fSAsias He net_params.guest_ip = guest_ip; 828e80ddf6aSAsias He net_params.host_ip = host_ip; 829ec7674d7SAsias He net_params.kvm = kvm; 830ec7674d7SAsias He net_params.script = script; 831a4e724ddSSasha Levin sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 832a4e724ddSSasha Levin net_params.guest_mac, 833a4e724ddSSasha Levin net_params.guest_mac+1, 834a4e724ddSSasha Levin net_params.guest_mac+2, 835a4e724ddSSasha Levin net_params.guest_mac+3, 836a4e724ddSSasha Levin net_params.guest_mac+4, 837a4e724ddSSasha Levin net_params.guest_mac+5); 838d7098b9bSAsias He sscanf(host_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 839d7098b9bSAsias He net_params.host_mac, 840d7098b9bSAsias He net_params.host_mac+1, 841d7098b9bSAsias He net_params.host_mac+2, 842d7098b9bSAsias He net_params.host_mac+3, 843d7098b9bSAsias He net_params.host_mac+4, 844d7098b9bSAsias He net_params.host_mac+5); 845a4e724ddSSasha Levin 846ec7674d7SAsias He if (!strncmp(network, "user", 4)) 847ec7674d7SAsias He net_params.mode = NET_MODE_USER; 848ec7674d7SAsias He else if (!strncmp(network, "tap", 3)) 849ec7674d7SAsias He net_params.mode = NET_MODE_TAP; 850ec7674d7SAsias He else 851ec7674d7SAsias He die("Unkown network mode %s, please use -network user, tap, none", network); 852bdfcfca6SSasha Levin virtio_net__init(&net_params); 853bdfcfca6SSasha Levin } 8544f56d42cSAsias He 855f967c427SPrasad Joshi kvm__start_timer(kvm); 856f967c427SPrasad Joshi 8578ec7e042SSasha Levin kvm__setup_bios(kvm); 8588ec7e042SSasha Levin 859d77a9efaSCyrill Gorcunov for (i = 0; i < nrcpus; i++) { 860d77a9efaSCyrill Gorcunov kvm_cpus[i] = kvm_cpu__init(kvm, i); 861d77a9efaSCyrill Gorcunov if (!kvm_cpus[i]) 8625ee154d1SPekka Enberg die("unable to initialize KVM VCPU"); 863839051d9SSasha Levin } 8645ee154d1SPekka Enberg 865839051d9SSasha Levin kvm__init_ram(kvm); 866839051d9SSasha Levin 867714e5b7fSSasha Levin kbd__init(kvm); 868714e5b7fSSasha Levin 86995d13a52SSasha Levin pci_shmem__init(kvm); 87095d13a52SSasha Levin 8713f838fecSPekka Enberg if (vnc || sdl) 8723f838fecSPekka Enberg fb = vesa__init(kvm); 8733f838fecSPekka Enberg 874e342083cSJohn Floren if (vnc) { 875aba1efa5SPekka Enberg if (fb) 876aba1efa5SPekka Enberg vnc__init(fb); 8773f838fecSPekka Enberg } 8783f838fecSPekka Enberg 8793f838fecSPekka Enberg if (sdl) { 8803f838fecSPekka Enberg if (fb) 8813f838fecSPekka Enberg sdl__init(fb); 8823f838fecSPekka Enberg } 883aba1efa5SPekka Enberg 884aba1efa5SPekka Enberg fb__start(); 885aba1efa5SPekka Enberg 886d60bafe5SSasha Levin thread_pool__init(nr_online_cpus); 88737f3d50eSSasha Levin ioeventfd__start(); 888d60bafe5SSasha Levin 889839051d9SSasha Levin for (i = 0; i < nrcpus; i++) { 890d77a9efaSCyrill Gorcunov if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 8915ee154d1SPekka Enberg die("unable to create KVM VCPU thread"); 8925ee154d1SPekka Enberg } 8935ee154d1SPekka Enberg 89449e5227dSSasha Levin /* Only VCPU #0 is going to exit by itself when shutting down */ 89549e5227dSSasha Levin if (pthread_join(kvm_cpus[0]->thread, &ret) != 0) 89649e5227dSSasha Levin exit_code = 1; 8975ee154d1SPekka Enberg 89849e5227dSSasha Levin for (i = 1; i < nrcpus; i++) { 899c23d9748SSasha Levin if (kvm_cpus[i]->is_running) { 90049e5227dSSasha Levin pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT); 901d77a9efaSCyrill Gorcunov if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 9025ee154d1SPekka Enberg die("pthread_join"); 903c23d9748SSasha Levin } 9045ee154d1SPekka Enberg if (ret != NULL) 9055ee154d1SPekka Enberg exit_code = 1; 9065ee154d1SPekka Enberg } 907f967c427SPrasad Joshi 908e6694207SSasha Levin compat__print_all_messages(); 909e6694207SSasha Levin 910aba1efa5SPekka Enberg fb__stop(); 911aba1efa5SPekka Enberg 912a0a1e3c2SPrasad Joshi virtio_blk__delete_all(kvm); 91380ac1d05SSasha Levin virtio_rng__delete_all(kvm); 914a0a1e3c2SPrasad Joshi 9159df47d00SPrasad Joshi disk_image__close_all(kvm->disks, image_count); 916f967c427SPrasad Joshi kvm__delete(kvm); 917f967c427SPrasad Joshi 9185ee154d1SPekka Enberg if (!exit_code) 919f967c427SPrasad Joshi printf("\n # KVM session ended normally.\n"); 920f967c427SPrasad Joshi 9215ee154d1SPekka Enberg return exit_code; 922f967c427SPrasad Joshi } 923