The discovered bit in PGCCSR register indicates if the device has been
[linux-2.6/next.git] / tools / kvm / builtin-run.c
blobb9efde2006e6cf3ed5a4cb7bf2f560dfa803d77e
1 #include "kvm/builtin-run.h"
3 #include "kvm/virtio-balloon.h"
4 #include "kvm/virtio-console.h"
5 #include "kvm/parse-options.h"
6 #include "kvm/8250-serial.h"
7 #include "kvm/framebuffer.h"
8 #include "kvm/disk-image.h"
9 #include "kvm/threadpool.h"
10 #include "kvm/virtio-blk.h"
11 #include "kvm/virtio-net.h"
12 #include "kvm/virtio-rng.h"
13 #include "kvm/ioeventfd.h"
14 #include "kvm/virtio-9p.h"
15 #include "kvm/barrier.h"
16 #include "kvm/kvm-cpu.h"
17 #include "kvm/ioport.h"
18 #include "kvm/symbol.h"
19 #include "kvm/i8042.h"
20 #include "kvm/mutex.h"
21 #include "kvm/term.h"
22 #include "kvm/util.h"
23 #include "kvm/vesa.h"
24 #include "kvm/irq.h"
25 #include "kvm/kvm.h"
26 #include "kvm/pci.h"
27 #include "kvm/rtc.h"
28 #include "kvm/sdl.h"
29 #include "kvm/vnc.h"
30 #include "kvm/guest_compat.h"
31 #include "kvm/pci-shmem.h"
33 #include <linux/types.h>
35 #include <sys/utsname.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <termios.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <ctype.h>
44 #include <stdio.h>
46 #define DEFAULT_KVM_DEV "/dev/kvm"
47 #define DEFAULT_CONSOLE "serial"
48 #define DEFAULT_NETWORK "user"
49 #define DEFAULT_HOST_ADDR "192.168.33.1"
50 #define DEFAULT_GUEST_ADDR "192.168.33.15"
51 #define DEFAULT_GUEST_MAC "02:15:15:15:15:15"
52 #define DEFAULT_HOST_MAC "02:01:01:01:01:01"
53 #define DEFAULT_SCRIPT "none"
55 #define MB_SHIFT (20)
56 #define KB_SHIFT (10)
57 #define GB_SHIFT (30)
58 #define MIN_RAM_SIZE_MB (64ULL)
59 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)
61 struct kvm *kvm;
62 struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
63 __thread struct kvm_cpu *current_kvm_cpu;
65 static u64 ram_size;
66 static u8 image_count;
67 static bool virtio_rng;
68 static const char *kernel_cmdline;
69 static const char *kernel_filename;
70 static const char *vmlinux_filename;
71 static const char *initrd_filename;
72 static const char *image_filename[MAX_DISK_IMAGES];
73 static const char *console;
74 static const char *dev;
75 static const char *network;
76 static const char *host_ip;
77 static const char *guest_ip;
78 static const char *guest_mac;
79 static const char *host_mac;
80 static const char *script;
81 static const char *guest_name;
82 static bool single_step;
83 static bool readonly_image[MAX_DISK_IMAGES];
84 static bool vnc;
85 static bool sdl;
86 static bool balloon;
87 static bool using_rootfs;
88 extern bool ioport_debug;
89 extern int active_console;
90 extern int debug_iodelay;
92 bool do_debug_print = false;
94 static int nrcpus;
95 static int vidmode = -1;
97 static const char * const run_usage[] = {
98 "kvm run [<options>] [<kernel image>]",
99 NULL
102 static int img_name_parser(const struct option *opt, const char *arg, int unset)
104 char *sep;
105 struct stat st;
107 if (stat(arg, &st) == 0 &&
108 S_ISDIR(st.st_mode)) {
109 char tmp[PATH_MAX];
111 if (realpath(arg, tmp) == 0 ||
112 virtio_9p__register(kvm, tmp, "/dev/root") < 0)
113 die("Unable to initialize virtio 9p");
114 using_rootfs = 1;
115 return 0;
118 if (image_count >= MAX_DISK_IMAGES)
119 die("Currently only 4 images are supported");
121 image_filename[image_count] = arg;
122 sep = strstr(arg, ",");
123 if (sep) {
124 if (strcmp(sep + 1, "ro") == 0)
125 readonly_image[image_count] = 1;
126 *sep = 0;
129 image_count++;
131 return 0;
134 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
136 char *tag_name;
137 char tmp[PATH_MAX];
140 * 9p dir can be of the form dirname,tag_name or
141 * just dirname. In the later case we use the
142 * default tag name
144 tag_name = strstr(arg, ",");
145 if (tag_name) {
146 *tag_name = '\0';
147 tag_name++;
149 if (realpath(arg, tmp)) {
150 if (virtio_9p__register(kvm, tmp, tag_name) < 0)
151 die("Unable to initialize virtio 9p");
152 } else
153 die("Failed resolving 9p path");
154 return 0;
157 static int shmem_parser(const struct option *opt, const char *arg, int unset)
159 const uint64_t default_size = SHMEM_DEFAULT_SIZE;
160 const uint64_t default_phys_addr = SHMEM_DEFAULT_ADDR;
161 const char *default_handle = SHMEM_DEFAULT_HANDLE;
162 struct shmem_info *si = malloc(sizeof(struct shmem_info));
163 enum { PCI, UNK } addr_type = PCI;
164 uint64_t phys_addr;
165 uint64_t size;
166 char *handle = NULL;
167 int create = 0;
168 const char *p = arg;
169 char *next;
170 int base = 10;
171 int verbose = 0;
173 const int skip_pci = strlen("pci:");
174 if (verbose)
175 pr_info("shmem_parser(%p,%s,%d)", opt, arg, unset);
176 /* parse out optional addr family */
177 if (strcasestr(p, "pci:")) {
178 p += skip_pci;
179 addr_type = PCI;
180 } else if (strcasestr(p, "mem:")) {
181 die("I can't add to E820 map yet.\n");
183 /* parse out physical addr */
184 base = 10;
185 if (strcasestr(p, "0x"))
186 base = 16;
187 phys_addr = strtoll(p, &next, base);
188 if (next == p && phys_addr == 0) {
189 pr_info("shmem: no physical addr specified, using default.");
190 phys_addr = default_phys_addr;
192 if (*next != ':' && *next != '\0')
193 die("shmem: unexpected chars after phys addr.\n");
194 if (*next == '\0')
195 p = next;
196 else
197 p = next + 1;
198 /* parse out size */
199 base = 10;
200 if (strcasestr(p, "0x"))
201 base = 16;
202 size = strtoll(p, &next, base);
203 if (next == p && size == 0) {
204 pr_info("shmem: no size specified, using default.");
205 size = default_size;
207 /* look for [KMGkmg][Bb]* uses base 2. */
208 int skip_B = 0;
209 if (strspn(next, "KMGkmg")) { /* might have a prefix */
210 if (*(next + 1) == 'B' || *(next + 1) == 'b')
211 skip_B = 1;
212 switch (*next) {
213 case 'K':
214 case 'k':
215 size = size << KB_SHIFT;
216 break;
217 case 'M':
218 case 'm':
219 size = size << MB_SHIFT;
220 break;
221 case 'G':
222 case 'g':
223 size = size << GB_SHIFT;
224 break;
225 default:
226 die("shmem: bug in detecting size prefix.");
227 break;
229 next += 1 + skip_B;
231 if (*next != ':' && *next != '\0') {
232 die("shmem: unexpected chars after phys size. <%c><%c>\n",
233 *next, *p);
235 if (*next == '\0')
236 p = next;
237 else
238 p = next + 1;
239 /* parse out optional shmem handle */
240 const int skip_handle = strlen("handle=");
241 next = strcasestr(p, "handle=");
242 if (*p && next) {
243 if (p != next)
244 die("unexpected chars before handle\n");
245 p += skip_handle;
246 next = strchrnul(p, ':');
247 if (next - p) {
248 handle = malloc(next - p + 1);
249 strncpy(handle, p, next - p);
250 handle[next - p] = '\0'; /* just in case. */
252 if (*next == '\0')
253 p = next;
254 else
255 p = next + 1;
257 /* parse optional create flag to see if we should create shm seg. */
258 if (*p && strcasestr(p, "create")) {
259 create = 1;
260 p += strlen("create");
262 if (*p != '\0')
263 die("shmem: unexpected trailing chars\n");
264 if (handle == NULL) {
265 handle = malloc(strlen(default_handle) + 1);
266 strcpy(handle, default_handle);
268 if (verbose) {
269 pr_info("shmem: phys_addr = %lx", phys_addr);
270 pr_info("shmem: size = %lx", size);
271 pr_info("shmem: handle = %s", handle);
272 pr_info("shmem: create = %d", create);
275 si->phys_addr = phys_addr;
276 si->size = size;
277 si->handle = handle;
278 si->create = create;
279 pci_shmem__register_mem(si); /* ownership of si, etc. passed on. */
280 return 0;
283 static const struct option options[] = {
284 OPT_GROUP("Basic options:"),
285 OPT_STRING('\0', "name", &guest_name, "guest name",
286 "A name for the guest"),
287 OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"),
288 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
289 OPT_CALLBACK('\0', "shmem", NULL,
290 "[pci:]<addr>:<size>[:handle=<handle>][:create]",
291 "Share host shmem with guest via pci device",
292 shmem_parser),
293 OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser),
294 OPT_BOOLEAN('\0', "balloon", &balloon, "Enable virtio balloon"),
295 OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"),
296 OPT_BOOLEAN('\0', "sdl", &sdl, "Enable SDL framebuffer"),
297 OPT_BOOLEAN('\0', "rng", &virtio_rng, "Enable virtio Random Number Generator"),
298 OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",
299 "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser),
300 OPT_STRING('\0', "console", &console, "serial or virtio",
301 "Console to use"),
302 OPT_STRING('\0', "dev", &dev, "device_file", "KVM device file"),
304 OPT_GROUP("Kernel options:"),
305 OPT_STRING('k', "kernel", &kernel_filename, "kernel",
306 "Kernel to boot in virtual machine"),
307 OPT_STRING('i', "initrd", &initrd_filename, "initrd",
308 "Initial RAM disk image"),
309 OPT_STRING('p', "params", &kernel_cmdline, "params",
310 "Kernel command line arguments"),
312 OPT_GROUP("Networking options:"),
313 OPT_STRING('n', "network", &network, "user, tap, none",
314 "Network to use"),
315 OPT_STRING('\0', "host-ip", &host_ip, "a.b.c.d",
316 "Assign this address to the host side networking"),
317 OPT_STRING('\0', "guest-ip", &guest_ip, "a.b.c.d",
318 "Assign this address to the guest side networking"),
319 OPT_STRING('\0', "host-mac", &host_mac, "aa:bb:cc:dd:ee:ff",
320 "Assign this address to the host side NIC"),
321 OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
322 "Assign this address to the guest side NIC"),
323 OPT_STRING('\0', "tapscript", &script, "Script path",
324 "Assign a script to process created tap device"),
326 OPT_GROUP("BIOS options:"),
327 OPT_INTEGER('\0', "vidmode", &vidmode,
328 "Video mode"),
330 OPT_GROUP("Debug options:"),
331 OPT_BOOLEAN('\0', "debug", &do_debug_print,
332 "Enable debug messages"),
333 OPT_BOOLEAN('\0', "debug-single-step", &single_step,
334 "Enable single stepping"),
335 OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug,
336 "Enable ioport debugging"),
337 OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay,
338 "Delay IO by millisecond"),
339 OPT_END()
343 * Serialize debug printout so that the output of multiple vcpus does not
344 * get mixed up:
346 static int printout_done;
348 static void handle_sigusr1(int sig)
350 struct kvm_cpu *cpu = current_kvm_cpu;
352 if (!cpu)
353 return;
355 printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
356 kvm_cpu__show_registers(cpu);
357 kvm_cpu__show_code(cpu);
358 kvm_cpu__show_page_tables(cpu);
359 fflush(stdout);
360 printout_done = 1;
361 mb();
364 /* Pause/resume the guest using SIGUSR2 */
365 static int is_paused;
367 static void handle_sigusr2(int sig)
369 if (sig == SIGKVMRESUME && is_paused)
370 kvm__continue();
371 else if (sig == SIGUSR2 && !is_paused)
372 kvm__pause();
373 else
374 return;
376 is_paused = !is_paused;
377 pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
380 static void handle_sigquit(int sig)
382 int i;
384 for (i = 0; i < nrcpus; i++) {
385 struct kvm_cpu *cpu = kvm_cpus[i];
387 if (!cpu)
388 continue;
390 printout_done = 0;
391 pthread_kill(cpu->thread, SIGUSR1);
393 * Wait for the vCPU to dump state before signalling
394 * the next thread. Since this is debug code it does
395 * not matter that we are burning CPU time a bit:
397 while (!printout_done)
398 mb();
401 serial8250__inject_sysrq(kvm);
404 static void handle_sigalrm(int sig)
406 serial8250__inject_interrupt(kvm);
407 virtio_console__inject_interrupt(kvm);
410 static void handle_sigstop(int sig)
412 kvm_cpu__reboot();
415 static void *kvm_cpu_thread(void *arg)
417 current_kvm_cpu = arg;
419 if (kvm_cpu__start(current_kvm_cpu))
420 goto panic_kvm;
422 kvm_cpu__delete(current_kvm_cpu);
424 return (void *) (intptr_t) 0;
426 panic_kvm:
427 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
428 current_kvm_cpu->kvm_run->exit_reason,
429 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
430 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
431 fprintf(stderr, "KVM exit code: 0x%Lu\n",
432 current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
434 kvm_cpu__show_registers(current_kvm_cpu);
435 kvm_cpu__show_code(current_kvm_cpu);
436 kvm_cpu__show_page_tables(current_kvm_cpu);
438 kvm_cpu__delete(current_kvm_cpu);
440 return (void *) (intptr_t) 1;
443 static char kernel[PATH_MAX];
445 static const char *host_kernels[] = {
446 "/boot/vmlinuz",
447 "/boot/bzImage",
448 NULL
451 static const char *default_kernels[] = {
452 "./bzImage",
453 "../../arch/x86/boot/bzImage",
454 NULL
457 static const char *default_vmlinux[] = {
458 "../../../vmlinux",
459 "../../vmlinux",
460 NULL
463 static void kernel_usage_with_options(void)
465 const char **k;
466 struct utsname uts;
468 fprintf(stderr, "Fatal: could not find default kernel image in:\n");
469 k = &default_kernels[0];
470 while (*k) {
471 fprintf(stderr, "\t%s\n", *k);
472 k++;
475 if (uname(&uts) < 0)
476 return;
478 k = &host_kernels[0];
479 while (*k) {
480 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
481 return;
482 fprintf(stderr, "\t%s\n", kernel);
483 k++;
485 fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
488 static u64 host_ram_size(void)
490 long page_size;
491 long nr_pages;
493 nr_pages = sysconf(_SC_PHYS_PAGES);
494 if (nr_pages < 0) {
495 pr_warning("sysconf(_SC_PHYS_PAGES) failed");
496 return 0;
499 page_size = sysconf(_SC_PAGE_SIZE);
500 if (page_size < 0) {
501 pr_warning("sysconf(_SC_PAGE_SIZE) failed");
502 return 0;
505 return (nr_pages * page_size) >> MB_SHIFT;
509 * If user didn't specify how much memory it wants to allocate for the guest,
510 * avoid filling the whole host RAM.
512 #define RAM_SIZE_RATIO 0.8
514 static u64 get_ram_size(int nr_cpus)
516 u64 available;
517 u64 ram_size;
519 ram_size = 64 * (nr_cpus + 3);
521 available = host_ram_size() * RAM_SIZE_RATIO;
522 if (!available)
523 available = MIN_RAM_SIZE_MB;
525 if (ram_size > available)
526 ram_size = available;
528 return ram_size;
531 static const char *find_kernel(void)
533 const char **k;
534 struct stat st;
535 struct utsname uts;
537 k = &default_kernels[0];
538 while (*k) {
539 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
540 k++;
541 continue;
543 strncpy(kernel, *k, PATH_MAX);
544 return kernel;
547 if (uname(&uts) < 0)
548 return NULL;
550 k = &host_kernels[0];
551 while (*k) {
552 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
553 return NULL;
555 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
556 k++;
557 continue;
559 return kernel;
562 return NULL;
565 static const char *find_vmlinux(void)
567 const char **vmlinux;
569 vmlinux = &default_vmlinux[0];
570 while (*vmlinux) {
571 struct stat st;
573 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
574 vmlinux++;
575 continue;
577 return *vmlinux;
579 return NULL;
582 void kvm_run_help(void)
584 usage_with_options(run_usage, options);
587 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
589 struct virtio_net_parameters net_params;
590 static char real_cmdline[2048], default_name[20];
591 struct framebuffer *fb = NULL;
592 unsigned int nr_online_cpus;
593 int exit_code = 0;
594 int max_cpus, recommended_cpus;
595 int i;
596 void *ret;
598 signal(SIGALRM, handle_sigalrm);
599 signal(SIGQUIT, handle_sigquit);
600 signal(SIGUSR1, handle_sigusr1);
601 signal(SIGUSR2, handle_sigusr2);
602 signal(SIGKVMSTOP, handle_sigstop);
603 signal(SIGKVMRESUME, handle_sigusr2);
604 /* ignore balloon signal by default if not enable balloon optiion */
605 signal(SIGKVMADDMEM, SIG_IGN);
606 signal(SIGKVMDELMEM, SIG_IGN);
608 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
610 while (argc != 0) {
611 argc = parse_options(argc, argv, options, run_usage,
612 PARSE_OPT_STOP_AT_NON_OPTION);
613 if (argc != 0) {
614 if (kernel_filename) {
615 fprintf(stderr, "Cannot handle parameter: "
616 "%s\n", argv[0]);
617 usage_with_options(run_usage, options);
618 return EINVAL;
620 /* first unhandled parameter is treated as a kernel
621 image
623 kernel_filename = argv[0];
624 argv++;
625 argc--;
630 if (!kernel_filename)
631 kernel_filename = find_kernel();
633 if (!kernel_filename) {
634 kernel_usage_with_options();
635 return EINVAL;
638 vmlinux_filename = find_vmlinux();
640 if (nrcpus == 0)
641 nrcpus = nr_online_cpus;
642 else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
643 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
645 if (!ram_size)
646 ram_size = get_ram_size(nrcpus);
648 if (ram_size < MIN_RAM_SIZE_MB)
649 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
651 if (ram_size > host_ram_size())
652 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size());
654 ram_size <<= MB_SHIFT;
656 if (!dev)
657 dev = DEFAULT_KVM_DEV;
659 if (!console)
660 console = DEFAULT_CONSOLE;
662 if (!strncmp(console, "virtio", 6))
663 active_console = CONSOLE_VIRTIO;
664 else
665 active_console = CONSOLE_8250;
667 if (!host_ip)
668 host_ip = DEFAULT_HOST_ADDR;
670 if (!guest_ip)
671 guest_ip = DEFAULT_GUEST_ADDR;
673 if (!guest_mac)
674 guest_mac = DEFAULT_GUEST_MAC;
676 if (!host_mac)
677 host_mac = DEFAULT_HOST_MAC;
679 if (!script)
680 script = DEFAULT_SCRIPT;
682 symbol__init(vmlinux_filename);
684 term_init();
686 if (!guest_name) {
687 sprintf(default_name, "guest-%u", getpid());
688 guest_name = default_name;
691 kvm = kvm__init(dev, ram_size, guest_name);
693 irq__init(kvm);
695 kvm->single_step = single_step;
697 ioeventfd__init();
699 max_cpus = kvm__max_cpus(kvm);
700 recommended_cpus = kvm__recommended_cpus(kvm);
702 if (nrcpus > max_cpus) {
703 printf(" # Limit the number of CPUs to %d\n", max_cpus);
704 kvm->nrcpus = max_cpus;
705 } else if (nrcpus > recommended_cpus) {
706 printf(" # Warning: The maximum recommended amount of VCPUs"
707 " is %d\n", recommended_cpus);
710 kvm->nrcpus = nrcpus;
713 * vidmode should be either specified
714 * either set by default
716 if (vnc || sdl) {
717 if (vidmode == -1)
718 vidmode = 0x312;
719 } else
720 vidmode = 0;
722 memset(real_cmdline, 0, sizeof(real_cmdline));
723 strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 reboot=k panic=1 i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1");
724 if (vnc || sdl) {
725 strcat(real_cmdline, " video=vesafb console=tty0");
726 } else
727 strcat(real_cmdline, " console=ttyS0 earlyprintk=serial");
728 strcat(real_cmdline, " ");
729 if (kernel_cmdline)
730 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
732 if (!using_rootfs && !image_filename[0]) {
733 if (virtio_9p__register(kvm, "/", "/dev/root") < 0)
734 die("Unable to initialize virtio 9p");
736 using_rootfs = 1;
738 if (!strstr(real_cmdline, "init="))
739 strlcat(real_cmdline, " init=/bin/sh ", sizeof(real_cmdline));
742 if (!strstr(real_cmdline, "root="))
743 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
745 if (using_rootfs)
746 strcat(real_cmdline, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
748 if (image_count) {
749 kvm->nr_disks = image_count;
750 kvm->disks = disk_image__open_all(image_filename, readonly_image, image_count);
751 if (!kvm->disks)
752 die("Unable to load all disk images.");
754 virtio_blk__init_all(kvm);
757 printf(" # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename, ram_size / 1024 / 1024, nrcpus, guest_name);
759 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
760 real_cmdline, vidmode))
761 die("unable to load kernel %s", kernel_filename);
763 kvm->vmlinux = vmlinux_filename;
765 ioport__setup_legacy();
767 rtc__init();
769 serial8250__init(kvm);
771 pci__init();
773 if (active_console == CONSOLE_VIRTIO)
774 virtio_console__init(kvm);
776 if (virtio_rng)
777 virtio_rng__init(kvm);
779 if (balloon)
780 virtio_bln__init(kvm);
782 if (!network)
783 network = DEFAULT_NETWORK;
785 virtio_9p__init(kvm);
787 if (strncmp(network, "none", 4)) {
788 net_params.guest_ip = guest_ip;
789 net_params.host_ip = host_ip;
790 net_params.kvm = kvm;
791 net_params.script = script;
792 sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
793 net_params.guest_mac,
794 net_params.guest_mac+1,
795 net_params.guest_mac+2,
796 net_params.guest_mac+3,
797 net_params.guest_mac+4,
798 net_params.guest_mac+5);
799 sscanf(host_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
800 net_params.host_mac,
801 net_params.host_mac+1,
802 net_params.host_mac+2,
803 net_params.host_mac+3,
804 net_params.host_mac+4,
805 net_params.host_mac+5);
807 if (!strncmp(network, "user", 4))
808 net_params.mode = NET_MODE_USER;
809 else if (!strncmp(network, "tap", 3))
810 net_params.mode = NET_MODE_TAP;
811 else
812 die("Unkown network mode %s, please use -network user, tap, none", network);
813 virtio_net__init(&net_params);
816 kvm__start_timer(kvm);
818 kvm__setup_bios(kvm);
820 for (i = 0; i < nrcpus; i++) {
821 kvm_cpus[i] = kvm_cpu__init(kvm, i);
822 if (!kvm_cpus[i])
823 die("unable to initialize KVM VCPU");
826 kvm__init_ram(kvm);
828 kbd__init(kvm);
830 pci_shmem__init(kvm);
832 if (vnc || sdl)
833 fb = vesa__init(kvm);
835 if (vnc) {
836 if (fb)
837 vnc__init(fb);
840 if (sdl) {
841 if (fb)
842 sdl__init(fb);
845 fb__start();
847 thread_pool__init(nr_online_cpus);
848 ioeventfd__start();
850 for (i = 0; i < nrcpus; i++) {
851 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
852 die("unable to create KVM VCPU thread");
855 /* Only VCPU #0 is going to exit by itself when shutting down */
856 if (pthread_join(kvm_cpus[0]->thread, &ret) != 0)
857 exit_code = 1;
859 for (i = 1; i < nrcpus; i++) {
860 if (kvm_cpus[i]->is_running) {
861 pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
862 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
863 die("pthread_join");
865 if (ret != NULL)
866 exit_code = 1;
869 compat__print_all_messages();
871 fb__stop();
873 virtio_blk__delete_all(kvm);
874 virtio_rng__delete_all(kvm);
876 disk_image__close_all(kvm->disks, image_count);
877 kvm__delete(kvm);
879 if (!exit_code)
880 printf("\n # KVM session ended normally.\n");
882 return exit_code;