Cleanups and automatic init/exit calls
cover Cleanups and automatic init/exit calls
This patch series is mostly about cleanups:
- Clean all the global variables we have to store configuration options.
- Remove externed config options between objects.
- Adding several exit routines to clean up on exit.
- Remove the global 'kvm' object.
- Contain arch specific init/exit calls within the corresponding non-arch specific code instead of the global init.
This patch series also adds a method to call init/exit functions automatically after we've finished intializing config options.
Doing so clears out a big chunk of repetetive code in our builtin-run init function, and makes adding new modules easier since there's now no need to link them to builtin-run.
Sasha Levin (33):
kvm tools: introduce kvm_config to store instance-specific config
options
kvm tools: generate command line options dynamically kvm tools: split struct kvm into arch specific part kvm tools: move kvm_config into struct kvm
kvm tools: remove redundancy between kvm_config and kvm kvm tools: move ioport_debug into struct kvm_config kvm tools: move mmio_debug into struct kvm_config
kvm tools: move active_console into struct kvm_config kvm tools: add private ptr to option parser
kvm tools: disk image related cleanup
kvm tools: move nrcpus into struct kvm_config
kvm tools: move kvm_cpus into struct kvm
kvm tools: improve framebuffer manager initialization kvm tools: improve term init/exit functions
kvm tools: threadpool exit routine
kvm tools: timer cleanup
kvm tools: virtio-console init/exit
kvm tools: virtio-rng init/exit
kvm tools: virtio-bln init/exit
kvm tools: pci-shmem init-exit
kvm tools: virtio-net init/exit
kvm tools: kvm-ipc cleanup
kvm tools: kbd initialization check
kvm tools: ui improvements
kvm tools: kernel load/firmware cleanup
kvm tools: ioport arch init
kvm tools: ram init
kvm tools: move the rest of the config initializations kvm tools: virtio-9p cleanup
kvm tools: add init/exit automatic calls
kvm tools: use init/exit where possible
kvm tools: pass kvm ptr directly to timer injection kvm tools: remove global kvm object
tools/kvm/Makefile | 3 +- tools/kvm/builtin-run.c | 1111 +++++------------------------- tools/kvm/disk/core.c | 73 +- tools/kvm/framebuffer.c | 9 +- tools/kvm/hw/i8042.c | 15 +- tools/kvm/hw/pci-shmem.c | 147 +++- tools/kvm/hw/rtc.c | 14 +- tools/kvm/hw/serial.c | 24 +- tools/kvm/hw/vesa.c | 6 +- tools/kvm/include/kvm/brlock.h | 16 +- tools/kvm/include/kvm/disk-image.h | 9 +- tools/kvm/include/kvm/framebuffer.h | 5 +- tools/kvm/include/kvm/i8042.h | 2 +- tools/kvm/include/kvm/ioport.h | 7 +- tools/kvm/include/kvm/kvm-config.h | 60 ++ tools/kvm/include/kvm/kvm-cpu.h | 6 +- tools/kvm/include/kvm/kvm-ipc.h | 8 +- tools/kvm/include/kvm/kvm.h | 38 +- tools/kvm/include/kvm/parse-options.h | 16 +- tools/kvm/include/kvm/pci-shmem.h | 6 +- tools/kvm/include/kvm/sdl.h | 8 +- tools/kvm/include/kvm/term.h | 16 +- tools/kvm/include/kvm/threadpool.h | 3 +- tools/kvm/include/kvm/util-init.h | 51 ++ tools/kvm/include/kvm/virtio-9p.h | 3 + tools/kvm/include/kvm/virtio-balloon.h | 3 +- tools/kvm/include/kvm/virtio-console.h | 3 +- tools/kvm/include/kvm/virtio-net.h | 6 +- tools/kvm/include/kvm/vnc.h | 10 +- tools/kvm/ioeventfd.c | 2 + tools/kvm/ioport.c | 23 +- tools/kvm/kvm-cpu.c | 81 ++- tools/kvm/kvm-ipc.c | 301 +++++++- tools/kvm/kvm.c | 229 ++---- tools/kvm/mmio.c | 13 +- tools/kvm/pci.c | 12 +- tools/kvm/powerpc/cpu_info.c | 2 +- tools/kvm/powerpc/include/kvm/kvm-arch.h | 24 +- tools/kvm/powerpc/irq.c | 2 +- tools/kvm/powerpc/kvm-cpu.c | 8 +- tools/kvm/powerpc/kvm.c | 42 +- tools/kvm/powerpc/spapr_hvcons.c | 14 +- tools/kvm/powerpc/spapr_pci.c | 2 +- tools/kvm/powerpc/xics.c | 24 +- tools/kvm/symbol.c | 4 +- tools/kvm/term.c | 61 +- tools/kvm/ui/sdl.c | 26 +- tools/kvm/ui/vnc.c | 26 +- tools/kvm/util/init.c | 69 ++ tools/kvm/util/threadpool.c | 36 +- tools/kvm/virtio/9p.c | 69 ++ tools/kvm/virtio/balloon.c | 23 +- tools/kvm/virtio/blk.c | 2 + tools/kvm/virtio/console.c | 26 +- tools/kvm/virtio/net.c | 142 +++- tools/kvm/virtio/pci.c | 6 +- tools/kvm/virtio/rng.c | 5 + tools/kvm/virtio/scsi.c | 2 + tools/kvm/x86/bios.c | 6 +- tools/kvm/x86/boot.c | 6 +- tools/kvm/x86/include/kvm/kvm-arch.h | 26 +- tools/kvm/x86/ioport.c | 20 +- tools/kvm/x86/irq.c | 2 + tools/kvm/x86/kvm-cpu.c | 32 +- tools/kvm/x86/kvm.c | 12 +- 65 files changed, 1632 insertions(+), 1426 deletions(-) create mode 100644 tools/kvm/include/kvm/kvm-config.h create mode 100644 tools/kvm/include/kvm/util-init.h create mode 100644 tools/kvm/util/init.c
Signature · 1 line
1.7.12
PATCH 1/33 kvm tools: introduce kvm_config to store instance-specific config options
Move all the configurable options from global static variables to a self-contained structure.
--- tools/kvm/builtin-run.c | 310 +++++++++++++++++++------------------ tools/kvm/include/kvm/kvm-config.h | 53 +++++++ 2 files changed, 210 insertions(+), 153 deletions(-) create mode 100644 tools/kvm/include/kvm/kvm-config.h
tools/kvm/builtin-run.c +157 -153
@@ -69,37 +69,40 @@ struct kvm *kvm;struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu; -static struct disk_image_params disk_image[MAX_DISK_IMAGES ]; -static u64 ram_size; -static u8 image_count; -static u8 num_net_devices; -static bool virtio_rng; -static const char *kernel_cmdline; -static const char *kernel_filename; -static const char *vmlinux_filename; -static const char *initrd_filename; -static const char *firmware_filename; -static const char *console; -static const char *dev; -static const char *network; -static const char *host_ip; -static const char *guest_ip; -static const char *guest_mac; -static const char *host_mac; -static const char *script; -static const char *guest_name; -static const char *sandbox; -static const char *hugetlbfs_path; -static const char *custom_rootfs_name ="default" ; -static struct virtio_net_params *net_params; -static bool single_step; -static bool vnc; -static bool sdl; -static bool balloon; -static bool using_rootfs; -static bool custom_rootfs; -static bool no_net; -static bool no_dhcp; +struct kvm_config { +struct disk_image_params disk_image[MAX_DISK_IMAGES ]; +u64 ram_size; +u8 image_count; +u8 num_net_devices; +bool virtio_rng; +const char *kernel_cmdline; +const char *kernel_filename; +const char *vmlinux_filename; +const char *initrd_filename; +const char *firmware_filename; +const char *console; +const char *dev; +const char *network; +const char *host_ip; +const char *guest_ip; +const char *guest_mac; +const char *host_mac; +const char *script; +const char *guest_name; +const char *sandbox; +const char *hugetlbfs_path; +const char *custom_rootfs_name; +struct virtio_net_params *net_params; +bool single_step; +bool vnc; +bool sdl; +bool balloon; +bool using_rootfs; +bool custom_rootfs; +bool no_net; +bool no_dhcp; +} cfg; +extern bool ioport_debug;extern bool mmio_debug;static int kvm_run_wrapper;
@@ -140,13 +143,13 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) S_ISDIR(st.st_mode)) {char tmp[PATH_MAX ]; -if (using_rootfs) +if (cfg.using_rootfs)die ("Please use only one rootfs directory atmost" );if (realpath (arg, tmp) ==0 ||virtio_9p__register (kvm, tmp,"/dev/root" ) <0 )die ("Unable to initialize virtio 9p" ); - using_rootfs =1 ; + cfg.using_rootfs =1 ;return 0 ; }
@@ -156,7 +159,7 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) S_ISDIR(st.st_mode)) {char tmp[PATH_MAX ]; -if (using_rootfs) +if (cfg.using_rootfs)die ("Please use only one rootfs directory atmost" ); if (realpath(path, tmp) == 0 ||
@@ -165,25 +168,25 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 )die ("Unable to initialize virtio 9p" );kvm_setup_resolv (arg); - using_rootfs = custom_rootfs =1 ; - custom_rootfs_name = arg; + cfg.using_rootfs = cfg.custom_rootfs =1 ; + cfg.custom_rootfs_name = arg;return 0 ; } -if (image_count >=MAX_DISK_IMAGES ) +if (cfg.image_count >=MAX_DISK_IMAGES )die ("Currently only 4 images are supported" ); - disk_image[image_count].filename = arg; + cfg.disk_image[cfg.image_count].filename = arg; cur = arg;if (strncmp (arg,"scsi:" ,5 ) ==0 ) { sep =strstr (arg,":" );if (sep) - disk_image[image_count].wwpn = sep +1 ; + cfg.disk_image[cfg.image_count].wwpn = sep +1 ; sep =strstr (sep +1 ,":" );if (sep) { *sep =0 ; - disk_image[image_count].tpgt = sep +1 ; + cfg.disk_image[cfg.image_count].tpgt = sep +1 ; } cur = sep +1 ; }
@@ -192,15 +195,15 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) sep =strstr (cur,"," );if (sep) {if (strncmp (sep +1 ,"ro" ,2 ) ==0 ) - disk_image[image_count].readonly = true; + cfg.disk_image[cfg.image_count].readonly = true;else if (strncmp (sep +1 ,"direct" ,6 ) ==0 ) - disk_image[image_count].direct = true; + cfg.disk_image[cfg.image_count].direct = true; *sep =0 ; cur = sep +1 ; } }while (sep); - image_count++; + cfg.image_count++;return 0 ; }
@@ -251,17 +254,17 @@ static int set_net_param(struct virtio_net_params *p, const char *param,if (!strncmp (val,"user" ,4 )) {int i; -for (i =0 ; i < num_net_devices; i++) -if (net_params[i].mode ==NET_MODE_USER ) +for (i =0 ; i < cfg.num_net_devices; i++) +if (cfg.net_params[i].mode ==NET_MODE_USER )die ("Only one usermode network device allowed at a time" ); p->mode =NET_MODE_USER ; }else if (!strncmp (val,"tap" ,3 )) { p->mode =NET_MODE_TAP ; }else if (!strncmp (val,"none" ,4 )) { - no_net =1 ; + cfg.no_net =1 ;return -1 ; }else -die ("Unkown network mode %s, please use user, tap or none" , network); +die ("Unknown network mode %s, please use user, tap or none" , cfg.network); }else if (strcmp (param,"script" ) ==0 ) { p->script =strdup (val); }else if (strcmp (param,"guest_ip" ) ==0 ) {
@@ -301,7 +304,7 @@ static int netdev_parser(const struct option *opt, const char *arg, int unset) };str_to_mac (DEFAULT_GUEST_MAC , p.guest_mac); - p.guest_mac[5 ] += num_net_devices; + p.guest_mac[5 ] += cfg.num_net_devices;while (cur) {if (on_cmd) {
@@ -315,13 +318,13 @@ static int netdev_parser(const struct option *opt, const char *arg, int unset) cur =strtok (NULL ,",=" ); }; - num_net_devices++; + cfg.num_net_devices++; - net_params =realloc (net_params, num_net_devices *sizeof (*net_params)); -if (net_params ==NULL ) + cfg.net_params =realloc (cfg.net_params, cfg.num_net_devices *sizeof (*cfg.net_params)); +if (cfg.net_params ==NULL )die ("Failed adding new network device" ); - net_params[num_net_devices -1 ] = p; + cfg.net_params[cfg.num_net_devices -1 ] = p; done:free (buf);
@@ -454,46 +457,46 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)static const struct option options[] = {OPT_GROUP ("Basic options:" ), -OPT_STRING ('\0' ,"name" , &guest_name,"guest name" , +OPT_STRING ('\0' ,"name" , &cfg.guest_name,"guest name" ,"A name for the guest" ),OPT_INTEGER ('c' ,"cpus" , &nrcpus,"Number of CPUs" ), -OPT_U64 ('m' ,"mem" , &ram_size,"Virtual machine memory size in MiB." ), +OPT_U64 ('m' ,"mem" , &cfg.ram_size,"Virtual machine memory size in MiB." ),OPT_CALLBACK ('\0' ,"shmem" ,NULL ,"[pci:]<addr>:<size>[:handle=<handle>][:create]" ,"Share host shmem with guest via pci device" , shmem_parser),OPT_CALLBACK ('d' ,"disk" ,NULL ,"image or rootfs_dir" ,"Disk image or rootfs directory" , img_name_parser), -OPT_BOOLEAN ('\0' ,"balloon" , &balloon,"Enable virtio balloon" ), -OPT_BOOLEAN ('\0' ,"vnc" , &vnc,"Enable VNC framebuffer" ), -OPT_BOOLEAN ('\0' ,"sdl" , &sdl,"Enable SDL framebuffer" ), -OPT_BOOLEAN ('\0' ,"rng" , &virtio_rng,"Enable virtio Random Number Generator" ), +OPT_BOOLEAN ('\0' ,"balloon" , &cfg.balloon,"Enable virtio balloon" ), +OPT_BOOLEAN ('\0' ,"vnc" , &cfg.vnc,"Enable VNC framebuffer" ), +OPT_BOOLEAN ('\0' ,"sdl" , &cfg.sdl,"Enable SDL framebuffer" ), +OPT_BOOLEAN ('\0' ,"rng" , &cfg.virtio_rng,"Enable virtio Random Number Generator" ),OPT_CALLBACK ('\0' ,"9p" ,NULL ,"dir_to_share,tag_name" ,"Enable virtio 9p to share files between host and guest" , virtio_9p_rootdir_parser), -OPT_STRING ('\0' ,"console" , &console,"serial, virtio or hv" , +OPT_STRING ('\0' ,"console" , &cfg.console,"serial, virtio or hv" ,"Console to use" ), -OPT_STRING ('\0' ,"dev" , &dev,"device_file" ,"KVM device file" ), +OPT_STRING ('\0' ,"dev" , &cfg.dev,"device_file" ,"KVM device file" ),OPT_CALLBACK ('\0' ,"tty" ,NULL ,"tty id" ,"Remap guest TTY into a pty on the host" , tty_parser), -OPT_STRING ('\0' ,"sandbox" , &sandbox,"script" , +OPT_STRING ('\0' ,"sandbox" , &cfg.sandbox,"script" ,"Run this script when booting into custom rootfs" ), -OPT_STRING ('\0' ,"hugetlbfs" , &hugetlbfs_path,"path" ,"Hugetlbfs path" ), +OPT_STRING ('\0' ,"hugetlbfs" , &cfg.hugetlbfs_path,"path" ,"Hugetlbfs path" ),OPT_GROUP ("Kernel options:" ), -OPT_STRING ('k' ,"kernel" , &kernel_filename,"kernel" , +OPT_STRING ('k' ,"kernel" , &cfg.kernel_filename,"kernel" ,"Kernel to boot in virtual machine" ), -OPT_STRING ('i' ,"initrd" , &initrd_filename,"initrd" , +OPT_STRING ('i' ,"initrd" , &cfg.initrd_filename,"initrd" ,"Initial RAM disk image" ), -OPT_STRING ('p' ,"params" , &kernel_cmdline,"params" , +OPT_STRING ('p' ,"params" , &cfg.kernel_cmdline,"params" ,"Kernel command line arguments" ), -OPT_STRING ('f' ,"firmware" , &firmware_filename,"firmware" , +OPT_STRING ('f' ,"firmware" , &cfg.firmware_filename,"firmware" ,"Firmware image to boot in virtual machine" ),OPT_GROUP ("Networking options:" ),OPT_CALLBACK_DEFAULT ('n' ,"network" ,NULL ,"network params" ,"Create a new guest NIC" , netdev_parser,NULL ), -OPT_BOOLEAN ('\0' ,"no-dhcp" , &no_dhcp,"Disable kernel DHCP in rootfs mode" ), +OPT_BOOLEAN ('\0' ,"no-dhcp" , &cfg.no_dhcp,"Disable kernel DHCP in rootfs mode" ),OPT_GROUP ("BIOS options:" ), OPT_INTEGER('\0', "vidmode", &vidmode,
@@ -502,7 +505,7 @@ static const struct option options[] = {OPT_GROUP ("Debug options:" ),OPT_BOOLEAN ('\0' ,"debug" , &do_debug_print,"Enable debug messages" ), -OPT_BOOLEAN ('\0' ,"debug-single-step" , &single_step, +OPT_BOOLEAN ('\0' ,"debug-single-step" , &cfg.single_step,"Enable single stepping" ),OPT_BOOLEAN ('\0' ,"debug-ioport" , &ioport_debug, "Enable ioport debugging"),
@@ -808,7 +811,7 @@ void kvm_run_help(void)static int kvm_setup_guest_init (void ) { -const char *rootfs = custom_rootfs_name; +const char *rootfs = cfg.custom_rootfs_name;char tmp[PATH_MAX ];size_t size;int fd, ret;
@@ -832,17 +835,17 @@ static int kvm_setup_guest_init(void)static int kvm_run_set_sandbox (void ) { -const char *guestfs_name = custom_rootfs_name; +const char *guestfs_name = cfg.custom_rootfs_name;char path[PATH_MAX ], script[PATH_MAX ], *tmp;snprintf (path,PATH_MAX ,"%s%s/virt/sandbox.sh" ,kvm__get_dir (), guestfs_name);remove (path); -if (sandbox ==NULL ) +if (cfg.sandbox ==NULL )return 0 ; - tmp =realpath (sandbox,NULL ); + tmp =realpath (cfg.sandbox,NULL );if (tmp ==NULL )return -ENOMEM ;
@@ -908,9 +911,9 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc)char program[PATH_MAX ];int fd; -remove (sandbox); +remove (cfg.sandbox); - fd =open (sandbox,O_RDWR |O_CREAT ,0777 ); + fd =open (cfg.sandbox,O_RDWR |O_CREAT ,0777 );if (fd <0 )die ("Failed creating sandbox script" );
@@ -954,6 +957,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)kvm_ipc__register_handler (KVM_IPC_VMSTATE , handle_vmstate); nr_online_cpus =sysconf (_SC_NPROCESSORS_ONLN); + cfg.custom_rootfs_name ="default" ;while (argc !=0 ) { argc = parse_options(argc, argv, options, run_usage,
@@ -963,14 +967,14 @@ static int kvm_cmd_run_init(int argc, const char **argv)/* Cusrom options, should have been handled elsewhere */ if (strcmp (argv[0 ],"--" ) ==0 ) {if (kvm_run_wrapper ==KVM_RUN_SANDBOX ) { - sandbox =DEFAULT_SANDBOX_FILENAME ; + cfg.sandbox =DEFAULT_SANDBOX_FILENAME ;kvm_run_write_sandbox_cmd (argv+1 , argc-1 );break ; } } -if ((kvm_run_wrapper ==KVM_RUN_DEFAULT && kernel_filename) || - (kvm_run_wrapper ==KVM_RUN_SANDBOX && sandbox)) { +if ((kvm_run_wrapper ==KVM_RUN_DEFAULT && cfg.kernel_filename) || + (kvm_run_wrapper ==KVM_RUN_SANDBOX && cfg.sandbox)) {fprintf (stderr,"Cannot handle parameter: " "%s\n" , argv[0 ]);usage_with_options (run_usage, options);
@@ -981,14 +985,14 @@ static int kvm_cmd_run_init(int argc, const char **argv)* first unhandled parameter is treated as * sandbox command */ - sandbox =DEFAULT_SANDBOX_FILENAME ; + cfg.sandbox =DEFAULT_SANDBOX_FILENAME ;kvm_run_write_sandbox_cmd (argv, argc); }else {/* * first unhandled parameter is treated as a kernel * image */ - kernel_filename = argv[0 ]; + cfg.kernel_filename = argv[0 ]; } argv++; argc--;
@@ -996,78 +1000,78 @@ static int kvm_cmd_run_init(int argc, const char **argv) } -if (!kernel_filename) - kernel_filename =find_kernel (); +if (!cfg.kernel_filename) + cfg.kernel_filename =find_kernel (); -if (!kernel_filename) { +if (!cfg.kernel_filename) {kernel_usage_with_options ();return -EINVAL ; } - vmlinux_filename =find_vmlinux (); + cfg.vmlinux_filename =find_vmlinux ();if (nrcpus ==0 ) nrcpus = nr_online_cpus; -if (!ram_size) - ram_size =get_ram_size (nrcpus); +if (!cfg.ram_size) + cfg.ram_size =get_ram_size (nrcpus); -if (ram_size <MIN_RAM_SIZE_MB ) -die ("Not enough memory specified: %lluMB (min %lluMB)" , ram_size,MIN_RAM_SIZE_MB ); +if (cfg.ram_size <MIN_RAM_SIZE_MB ) +die ("Not enough memory specified: %lluMB (min %lluMB)" , cfg.ram_size,MIN_RAM_SIZE_MB ); -if (ram_size >host_ram_size ()) -pr_warning ("Guest memory size %lluMB exceeds host physical RAM size %lluMB" , ram_size,host_ram_size ()); +if (cfg.ram_size >host_ram_size ()) +pr_warning ("Guest memory size %lluMB exceeds host physical RAM size %lluMB" , cfg.ram_size,host_ram_size ()); - ram_size <<=MB_SHIFT ; + cfg.ram_size <<=MB_SHIFT ; -if (!dev) - dev =DEFAULT_KVM_DEV ; +if (!cfg.dev) + cfg.dev =DEFAULT_KVM_DEV ; -if (!console) - console =DEFAULT_CONSOLE ; +if (!cfg.console) + cfg.console =DEFAULT_CONSOLE ; -if (!strncmp (console,"virtio" ,6 )) +if (!strncmp (cfg.console,"virtio" ,6 )) active_console =CONSOLE_VIRTIO ; -else if (!strncmp (console,"serial" ,6 )) +else if (!strncmp (cfg.console,"serial" ,6 )) active_console =CONSOLE_8250 ; -else if (!strncmp (console,"hv" ,2 )) +else if (!strncmp (cfg.console,"hv" ,2 )) active_console =CONSOLE_HV ;else pr_warning ("No console!" ); -if (!host_ip) - host_ip =DEFAULT_HOST_ADDR ; +if (!cfg.host_ip) + cfg.host_ip =DEFAULT_HOST_ADDR ; -if (!guest_ip) - guest_ip =DEFAULT_GUEST_ADDR ; +if (!cfg.guest_ip) + cfg.guest_ip =DEFAULT_GUEST_ADDR ; -if (!guest_mac) - guest_mac =DEFAULT_GUEST_MAC ; +if (!cfg.guest_mac) + cfg.guest_mac =DEFAULT_GUEST_MAC ; -if (!host_mac) - host_mac =DEFAULT_HOST_MAC ; +if (!cfg.host_mac) + cfg.host_mac =DEFAULT_HOST_MAC ; -if (!script) - script =DEFAULT_SCRIPT ; +if (!cfg.script) + cfg.script =DEFAULT_SCRIPT ;term_init (); -if (!guest_name) { -if (custom_rootfs) { - guest_name = custom_rootfs_name; +if (!cfg.guest_name) { +if (cfg.custom_rootfs) { + cfg.guest_name = cfg.custom_rootfs_name; }else {sprintf (default_name,"guest-%u" ,getpid ()); - guest_name = default_name; + cfg.guest_name = default_name; } } - kvm =kvm__init (dev, hugetlbfs_path, ram_size, guest_name); + kvm =kvm__init (cfg.dev, cfg.hugetlbfs_path, cfg.ram_size, cfg.guest_name);if (IS_ERR (kvm)) { r =PTR_ERR (kvm); goto fail; } - kvm->single_step = single_step; + kvm->single_step = cfg.single_step; r =ioeventfd__init (kvm);if (r <0 ) {
@@ -1115,7 +1119,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)* vidmode should be either specified * either set by default */ -if (vnc || sdl) { +if (cfg.vnc || cfg.sdl) {if (vidmode ==-1 ) vidmode =0x312 ; }else {
@@ -1123,36 +1127,36 @@ static int kvm_cmd_run_init(int argc, const char **argv) }memset (real_cmdline,0 ,sizeof (real_cmdline)); -kvm__arch_set_cmdline (real_cmdline, vnc || sdl); +kvm__arch_set_cmdline (real_cmdline, cfg.vnc || cfg.sdl);if (strlen (real_cmdline) >0 )strcat (real_cmdline," " ); -if (kernel_cmdline) -strlcat (real_cmdline, kernel_cmdline,sizeof (real_cmdline)); +if (cfg.kernel_cmdline) +strlcat (real_cmdline, cfg.kernel_cmdline,sizeof (real_cmdline)); -if (!using_rootfs && !disk_image[0 ].filename && !initrd_filename) { +if (!cfg.using_rootfs && !cfg.disk_image[0 ].filename && !cfg.initrd_filename) {char tmp[PATH_MAX ]; -kvm_setup_create_new (custom_rootfs_name); -kvm_setup_resolv (custom_rootfs_name); +kvm_setup_create_new (cfg.custom_rootfs_name); +kvm_setup_resolv (cfg.custom_rootfs_name);snprintf (tmp,PATH_MAX ,"%s%s" ,kvm__get_dir (),"default" );if (virtio_9p__register (kvm, tmp,"/dev/root" ) <0 )die ("Unable to initialize virtio 9p" );if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 )die ("Unable to initialize virtio 9p" ); - using_rootfs = custom_rootfs =1 ; + cfg.using_rootfs = cfg.custom_rootfs =1 ; } -if (using_rootfs) { +if (cfg.using_rootfs) {strcat (real_cmdline," root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p" ); -if (custom_rootfs) { +if (cfg.custom_rootfs) {kvm_run_set_sandbox ();strcat (real_cmdline," init=/virt/init" ); -if (!no_dhcp) +if (!cfg.no_dhcp)strcat (real_cmdline," ip=dhcp" );if (kvm_setup_guest_init ())die ("Failed to setup init for guest." );
@@ -1161,9 +1165,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)strlcat (real_cmdline," root=/dev/vda rw " ,sizeof (real_cmdline)); } -if (image_count) { - kvm->nr_disks = image_count; - kvm->disks =disk_image__open_all ((struct disk_image_params *)&disk_image, image_count); +if (cfg.image_count) { + kvm->nr_disks = cfg.image_count; + kvm->disks =disk_image__open_all ((struct disk_image_params *)&cfg.disk_image, cfg.image_count);if (IS_ERR (kvm->disks)) { r =PTR_ERR (kvm->disks); pr_err("disk_image__open_all() failed with error %ld\n",
@@ -1173,14 +1177,14 @@ static int kvm_cmd_run_init(int argc, const char **argv) }printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , - kernel_filename, ram_size /1024 /1024 , nrcpus, guest_name); + cfg.kernel_filename, cfg.ram_size /1024 /1024 , nrcpus, cfg.guest_name); -if (!firmware_filename) { -if (!kvm__load_kernel (kvm, kernel_filename, - initrd_filename, real_cmdline, vidmode)) -die ("unable to load kernel %s" , kernel_filename); +if (!cfg.firmware_filename) { +if (!kvm__load_kernel (kvm, cfg.kernel_filename, + cfg.initrd_filename, real_cmdline, vidmode)) +die ("unable to load kernel %s" , cfg.kernel_filename); - kvm->vmlinux = vmlinux_filename; + kvm->vmlinux = cfg.vmlinux_filename; r =symbol_init (kvm);if (r <0 )pr_debug ("symbol_init() failed with error %d\n" , r);
@@ -1216,34 +1220,34 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (active_console ==CONSOLE_VIRTIO )virtio_console__init (kvm); -if (virtio_rng) +if (cfg.virtio_rng)virtio_rng__init (kvm); -if (balloon) +if (cfg.balloon)virtio_bln__init (kvm); -if (!network) - network =DEFAULT_NETWORK ; +if (!cfg.network) + cfg.network =DEFAULT_NETWORK ;virtio_9p__init (kvm); -for (i =0 ; i < num_net_devices; i++) { - net_params[i].kvm = kvm; -virtio_net__init (&net_params[i]); +for (i =0 ; i < cfg.num_net_devices; i++) { + cfg.net_params[i].kvm = kvm; +virtio_net__init (&cfg.net_params[i]); } -if (num_net_devices ==0 && no_net ==0 ) { +if (cfg.num_net_devices ==0 && cfg.no_net ==0 ) {struct virtio_net_params net_params; net_params = (struct virtio_net_params ) { - .guest_ip = guest_ip, - .host_ip = host_ip, + .guest_ip = cfg.guest_ip, + .host_ip = cfg.host_ip, .kvm = kvm, - .script = script, + .script = cfg.script, .mode =NET_MODE_USER , }; -str_to_mac (guest_mac, net_params.guest_mac); -str_to_mac (host_mac, net_params.host_mac); +str_to_mac (cfg.guest_mac, net_params.guest_mac); +str_to_mac (cfg.host_mac, net_params.host_mac);virtio_net__init (&net_params); }
@@ -1256,7 +1260,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)pci_shmem__init (kvm); -if (vnc || sdl) { +if (cfg.vnc || cfg.sdl) { fb =vesa__init (kvm);if (IS_ERR (fb)) {pr_err ("vesa__init() failed with error %ld\n" ,PTR_ERR (fb));
@@ -1264,7 +1268,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -if (vnc && fb) { +if (cfg.vnc && fb) { r =vnc__init (fb);if (r <0 ) {pr_err ("vnc__init() failed with error %d\n" , r);
@@ -1272,7 +1276,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -if (sdl && fb) { +if (cfg.sdl && fb) {sdl__init (fb);if (r <0 ) {pr_err ("sdl__init() failed with error %d\n" , r);
@@ -1292,9 +1296,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)kvm__start_timer (kvm); -if (firmware_filename) { -if (!kvm__load_firmware (kvm, firmware_filename)) -die ("unable to load firmware image %s: %s" , firmware_filename,strerror (errno)); +if (cfg.firmware_filename) { +if (!kvm__load_firmware (kvm, cfg.firmware_filename)) +die ("unable to load firmware image %s: %s" , cfg.firmware_filename,strerror (errno)); }else {kvm__arch_setup_firmware (kvm);if (r <0 ) {
@@ -1373,7 +1377,7 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_rng__exit() failed with error %d\n" , r); - r =disk_image__close_all (kvm->disks, image_count); + r =disk_image__close_all (kvm->disks, cfg.image_count);if (r <0 )pr_warning ("disk_image__close_all() failed with error %d\n" , r);
tools/kvm/include/kvm/kvm-config.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
1.7.12
PATCH 2/33 kvm tools: generate command line options dynamically
In reply to Sasha LevinSince we now store options in a struct, we should generate the command line options dynamically. This is a pre-requisite to the following patch moving the options into struct kvm.
--- tools/kvm/builtin-run.c | 132 ++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 60 deletions(-)
tools/kvm/builtin-run.c +72 -60
@@ -455,66 +455,76 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)return 0 ; } -static const struct option options[] = { -OPT_GROUP ("Basic options:" ), -OPT_STRING ('\0' ,"name" , &cfg.guest_name,"guest name" , -"A name for the guest" ), -OPT_INTEGER ('c' ,"cpus" , &nrcpus,"Number of CPUs" ), -OPT_U64 ('m' ,"mem" , &cfg.ram_size,"Virtual machine memory size in MiB." ), -OPT_CALLBACK ('\0' ,"shmem" ,NULL , -"[pci:]<addr>:<size>[:handle=<handle>][:create]" , -"Share host shmem with guest via pci device" , - shmem_parser), -OPT_CALLBACK ('d' ,"disk" ,NULL ,"image or rootfs_dir" ,"Disk image or rootfs directory" , img_name_parser), -OPT_BOOLEAN ('\0' ,"balloon" , &cfg.balloon,"Enable virtio balloon" ), -OPT_BOOLEAN ('\0' ,"vnc" , &cfg.vnc,"Enable VNC framebuffer" ), -OPT_BOOLEAN ('\0' ,"sdl" , &cfg.sdl,"Enable SDL framebuffer" ), -OPT_BOOLEAN ('\0' ,"rng" , &cfg.virtio_rng,"Enable virtio Random Number Generator" ), -OPT_CALLBACK ('\0' ,"9p" ,NULL ,"dir_to_share,tag_name" , -"Enable virtio 9p to share files between host and guest" , virtio_9p_rootdir_parser), -OPT_STRING ('\0' ,"console" , &cfg.console,"serial, virtio or hv" , -"Console to use" ), -OPT_STRING ('\0' ,"dev" , &cfg.dev,"device_file" ,"KVM device file" ), -OPT_CALLBACK ('\0' ,"tty" ,NULL ,"tty id" , -"Remap guest TTY into a pty on the host" , - tty_parser), -OPT_STRING ('\0' ,"sandbox" , &cfg.sandbox,"script" , -"Run this script when booting into custom rootfs" ), -OPT_STRING ('\0' ,"hugetlbfs" , &cfg.hugetlbfs_path,"path" ,"Hugetlbfs path" ), - -OPT_GROUP ("Kernel options:" ), -OPT_STRING ('k' ,"kernel" , &cfg.kernel_filename,"kernel" , -"Kernel to boot in virtual machine" ), -OPT_STRING ('i' ,"initrd" , &cfg.initrd_filename,"initrd" , -"Initial RAM disk image" ), -OPT_STRING ('p' ,"params" , &cfg.kernel_cmdline,"params" , -"Kernel command line arguments" ), -OPT_STRING ('f' ,"firmware" , &cfg.firmware_filename,"firmware" , -"Firmware image to boot in virtual machine" ), - -OPT_GROUP ("Networking options:" ), -OPT_CALLBACK_DEFAULT ('n' ,"network" ,NULL ,"network params" , -"Create a new guest NIC" , - netdev_parser,NULL ), -OPT_BOOLEAN ('\0' ,"no-dhcp" , &cfg.no_dhcp,"Disable kernel DHCP in rootfs mode" ), - -OPT_GROUP ("BIOS options:" ), -OPT_INTEGER ('\0' ,"vidmode" , &vidmode, -"Video mode" ), - -OPT_GROUP ("Debug options:" ), -OPT_BOOLEAN ('\0' ,"debug" , &do_debug_print, -"Enable debug messages" ), -OPT_BOOLEAN ('\0' ,"debug-single-step" , &cfg.single_step, -"Enable single stepping" ), -OPT_BOOLEAN ('\0' ,"debug-ioport" , &ioport_debug, -"Enable ioport debugging" ), -OPT_BOOLEAN ('\0' ,"debug-mmio" , &mmio_debug, -"Enable MMIO debugging" ), -OPT_INTEGER ('\0' ,"debug-iodelay" , &debug_iodelay, -"Delay IO by millisecond" ), -OPT_END () -}; +#define BUILD_OPTIONS (name, cfg) \ + struct option name[] = { \ + OPT_GROUP("Basic options:"), \ + OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ + "A name for the guest"), \ + OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"), \ + OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\ + in MiB."), \ + OPT_CALLBACK('\0', "shmem", NULL, \ + "[pci:]<addr>:<size>[:handle=<handle>][:create]", \ + "Share host shmem with guest via pci device", \ + shmem_parser), \ + OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk \ + image or rootfs directory", img_name_parser), \ + OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio \ + balloon"), \ + OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\ + OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\ + OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\ + Number Generator"), \ + OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ + "Enable virtio 9p to share files between host and \ + guest", virtio_9p_rootdir_parser), \ + OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or \ + hv", "Console to use"), \ + OPT_STRING('\0', "dev", &(cfg)->dev, "device_file", \ + "KVM device file"), \ + OPT_CALLBACK('\0', "tty", NULL, "tty id", \ + "Remap guest TTY into a pty on the host", \ + tty_parser), \ + OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script", \ + "Run this script when booting into custom \ + rootfs"), \ + OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path", \ + "Hugetlbfs path"), \ + \ + OPT_GROUP("Kernel options:"), \ + OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel", \ + "Kernel to boot in virtual machine"), \ + OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd", \ + "Initial RAM disk image"), \ + OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params", \ + "Kernel command line arguments"), \ + OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\ + "Firmware image to boot in virtual machine"), \ + \ + OPT_GROUP("Networking options:"), \ + OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params", \ + "Create a new guest NIC", \ + netdev_parser, NULL), \ + OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\ + in rootfs mode"), \ + \ + OPT_GROUP("BIOS options:"), \ + OPT_INTEGER('\0', "vidmode", &vidmode, \ + "Video mode"), \ + \ + OPT_GROUP("Debug options:"), \ + OPT_BOOLEAN('\0', "debug", &do_debug_print, \ + "Enable debug messages"), \ + OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step, \ + "Enable single stepping"), \ + OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug, \ + "Enable ioport debugging"), \ + OPT_BOOLEAN('\0', "debug-mmio", &mmio_debug, \ + "Enable MMIO debugging"), \ + OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay, \ + "Delay IO by millisecond"), \ + OPT_END() \ + };/* * Serialize debug printout so that the output of multiple vcpus does not
@@ -806,6 +816,7 @@ static const char *find_vmlinux(void)void kvm_run_help (void ) { +BUILD_OPTIONS (options, &cfg);usage_with_options (run_usage, options); }
@@ -947,6 +958,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)unsigned int nr_online_cpus;int max_cpus, recommended_cpus;int i, r; +BUILD_OPTIONS (options, &cfg);signal (SIGALRM , handle_sigalrm);kvm_ipc__register_handler (KVM_IPC_DEBUG , handle_debug);
Signature · 1 line
1.7.12
PATCH 3/33 kvm tools: split struct kvm into arch specific part
In reply to Sasha LevinMove all the non-arch specific members into a generic struct, and the arch specific members into a arch specific kvm_arch. This prevents code duplication across different archs.
--- tools/kvm/include/kvm/kvm.h | 27 ++++++++++++++++++++ tools/kvm/powerpc/cpu_info.c | 2 +- tools/kvm/powerpc/include/kvm/kvm-arch.h | 24 +----------------- tools/kvm/powerpc/irq.c | 2 +- tools/kvm/powerpc/kvm-cpu.c | 6 ++--- tools/kvm/powerpc/kvm.c | 42 ++++++++++++++++---------------- tools/kvm/powerpc/spapr_pci.c | 2 +- tools/kvm/powerpc/xics.c | 24 +++++++++--------- tools/kvm/x86/bios.c | 6 ++--- tools/kvm/x86/boot.c | 6 ++--- tools/kvm/x86/include/kvm/kvm-arch.h | 26 +------------------- tools/kvm/x86/kvm-cpu.c | 30 +++++++++++------------ tools/kvm/x86/kvm.c | 12 ++++----- 13 files changed, 95 insertions(+), 114 deletions(-)
tools/kvm/include/kvm/kvm.h +27 -0
@@ -31,6 +31,33 @@ struct kvm_ext {int code; }; +struct kvm { +struct kvm_arch arch; +int sys_fd;/* For system ioctls(), i.e. /dev/kvm */ +int vm_fd;/* For VM ioctls() */ +timer_t timerid;/* Posix timer for interrupts */ + +int nrcpus;/* Number of cpus to run */ + +u32 mem_slots;/* for KVM_SET_USER_MEMORY_REGION */ + +u64 ram_size; +void *ram_start; +u64 ram_pagesize; + +bool nmi_disabled; + +bool single_step; + +const char *vmlinux; +struct disk_image **disks; +int nr_disks; + +char *name; + +int vm_state; +}; +void kvm__set_dir (const char *fmt, ...);const char *kvm__get_dir (void );
tools/kvm/powerpc/cpu_info.c +1 -1
@@ -174,7 +174,7 @@ struct cpu_info *find_cpu_info(struct kvm *kvm) {struct cpu_info *info;unsigned int i; -u32 pvr = kvm->pvr; +u32 pvr = kvm->arch.pvr;for (info =NULL , i =0 ; i <ARRAY_SIZE (host_pvr_info); i++) {if ((pvr & host_pvr_info[i].pvr_mask) == host_pvr_info[i].pvr) {
tools/kvm/powerpc/include/kvm/kvm-arch.h +1 -23
@@ -43,36 +43,14 @@struct spapr_phb ; -struct kvm { -int sys_fd;/* For system ioctls(), i.e. /dev/kvm */ -int vm_fd;/* For VM ioctls() */ -timer_t timerid;/* Posix timer for interrupts */ - -int nrcpus;/* Number of cpus to run */ - -u32 mem_slots;/* for KVM_SET_USER_MEMORY_REGION */ - -u64 ram_size; -void *ram_start; -u64 ram_pagesize; - +struct kvm_arch {u64 sdr1;u32 pvr; - -bool nmi_disabled; - -bool single_step; - -const char *vmlinux; -struct disk_image **disks; -int nr_disks;unsigned long rtas_gra;unsigned long rtas_size;unsigned long fdt_gra;unsigned long initrd_gra;unsigned long initrd_size; -char *name; -int vm_state;struct icp_state *icp;struct spapr_phb *phb; };
tools/kvm/powerpc/irq.c +1 -1
@@ -59,7 +59,7 @@ int irq__init(struct kvm *kvm)* are numbered 0..nrcpus. This may not really be true, * but it is OK currently. */ - kvm->icp =xics_system_init (XICS_IRQS , kvm->nrcpus); + kvm->arch.icp =xics_system_init (XICS_IRQS , kvm->nrcpus);return 0 ; }
tools/kvm/powerpc/kvm-cpu.c +3 -3
@@ -115,7 +115,7 @@ static void kvm_cpu__setup_regs(struct kvm_cpu *vcpu)if (vcpu->cpu_id ==0 ) { r->pc =KERNEL_START_ADDR ; - r->gpr[3 ] = vcpu->kvm->fdt_gra; + r->gpr[3 ] = vcpu->kvm->arch.fdt_gra; r->gpr[5 ] =0 ; }else { r->pc =KERNEL_SECONDARY_START_ADDR ;
@@ -142,8 +142,8 @@ static void kvm_cpu__setup_sregs(struct kvm_cpu *vcpu)if (ioctl (vcpu->vcpu_fd,KVM_GET_SREGS , &sregs) <0 )die ("KVM_GET_SREGS failed" ); - sregs.u.s.sdr1 = vcpu->kvm->sdr1; - sregs.pvr = vcpu->kvm->pvr; + sregs.u.s.sdr1 = vcpu->kvm->arch.sdr1; + sregs.pvr = vcpu->kvm->arch.pvr;if (ioctl (vcpu->vcpu_fd,KVM_SET_SREGS , &sregs) <0 )die ("KVM_SET_SREGS failed" );
tools/kvm/powerpc/kvm.c +21 -21
107 lines not on this page · raw message
tools/kvm/powerpc/spapr_pci.c +1 -1
@@ -220,7 +220,7 @@ void spapr_create_phb(struct kvm *kvm, phb.io_addr = io_win_addr; phb.io_size = io_win_size; - kvm->phb = &phb; + kvm->arch.phb = &phb; }static uint32_t bar_to_ss (unsigned long bar)
tools/kvm/powerpc/xics.c +12 -12
@@ -302,7 +302,7 @@ static target_ulong h_cppr(struct kvm_cpu *vcpu,target_ulong cppr = args[0 ];xics_dprintf ("h_cppr(%lx)\n" , cppr); -icp_set_cppr (vcpu->kvm->icp, vcpu->cpu_id, cppr); +icp_set_cppr (vcpu->kvm->arch.icp, vcpu->cpu_id, cppr);return H_SUCCESS ; }
@@ -313,18 +313,18 @@ static target_ulong h_ipi(struct kvm_cpu *vcpu,target_ulong mfrr = args[1 ];xics_dprintf ("h_ipi(%lx, %lx)\n" , server, mfrr); -if (server >= vcpu->kvm->icp->nr_servers) { +if (server >= vcpu->kvm->arch.icp->nr_servers) {return H_PARAMETER ; } -icp_set_mfrr (vcpu->kvm->icp, server, mfrr); +icp_set_mfrr (vcpu->kvm->arch.icp, server, mfrr);return H_SUCCESS ; }static target_ulong h_xirr (struct kvm_cpu *vcpu,target_ulong opcode,target_ulong *args) { -uint32_t xirr =icp_accept (vcpu->kvm->icp->ss + vcpu->cpu_id); +uint32_t xirr =icp_accept (vcpu->kvm->arch.icp->ss + vcpu->cpu_id);xics_dprintf ("h_xirr() = %x\n" , xirr); args[0 ] = xirr;
@@ -337,7 +337,7 @@ static target_ulong h_eoi(struct kvm_cpu *vcpu,target_ulong xirr = args[0 ];xics_dprintf ("h_eoi(%lx)\n" , xirr); -icp_eoi (vcpu->kvm->icp, vcpu->cpu_id, xirr); +icp_eoi (vcpu->kvm->arch.icp, vcpu->cpu_id, xirr);return H_SUCCESS ; }
@@ -345,7 +345,7 @@ static void rtas_set_xive(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { - struct ics_state *ics = vcpu->kvm->icp->ics; + struct ics_state *ics = vcpu->kvm->arch.icp->ics;uint32_t nr, server, priority;if ((nargs !=3 ) || (nret !=1 )) {
@@ -373,7 +373,7 @@ static void rtas_get_xive(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { - struct ics_state *ics = vcpu->kvm->icp->ics; + struct ics_state *ics = vcpu->kvm->arch.icp->ics;uint32_t nr;if ((nargs !=1 ) || (nret !=3 )) {
@@ -397,7 +397,7 @@ static void rtas_int_off(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { - struct ics_state *ics = vcpu->kvm->icp->ics; + struct ics_state *ics = vcpu->kvm->arch.icp->ics;uint32_t nr;if ((nargs !=1 ) || (nret !=1 )) {
@@ -421,7 +421,7 @@ static void rtas_int_on(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { - struct ics_state *ics = vcpu->kvm->icp->ics; + struct ics_state *ics = vcpu->kvm->arch.icp->ics;uint32_t nr;if ((nargs !=1 ) || (nret !=1 )) {
@@ -443,8 +443,8 @@ static void rtas_int_on(struct kvm_cpu *vcpu, uint32_t token,void xics_cpu_register (struct kvm_cpu *vcpu) { -if (vcpu->cpu_id < vcpu->kvm->icp->nr_servers) - vcpu->kvm->icp->ss[vcpu->cpu_id].cpu = vcpu; +if (vcpu->cpu_id < vcpu->kvm->arch.icp->nr_servers) + vcpu->kvm->arch.icp->ss[vcpu->cpu_id].cpu = vcpu;else die ("Setting invalid server for cpuid %ld\n" , vcpu->cpu_id); }
@@ -510,5 +510,5 @@ void kvm__irq_line(struct kvm *kvm, int irq, int level)* kvm_cpu__irq(vcpu, POWER7_EXT_IRQ, 1) */ xics_dprintf ("Raising IRQ %d -> %d\n" , irq, level); -ics_set_irq_msi (kvm->icp->ics, irq - kvm->icp->ics->offset, level); +ics_set_irq_msi (kvm->arch.icp->ics, irq - kvm->arch.icp->ics->offset, level); }
tools/kvm/x86/bios.c +3 -3
@@ -48,7 +48,7 @@ static void setup_irq_handler(struct kvm *kvm, struct irq_handler *handler)DIE_IF ((handler->address -MB_BIOS_BEGIN ) >0xffffUL ); -interrupt_table__set (&kvm->interrupt_table, &intr_desc, handler->irq); +interrupt_table__set (&kvm->arch.interrupt_table, &intr_desc, handler->irq); }/**
@@ -163,12 +163,12 @@ void setup_bios(struct kvm *kvm) .segment = REAL_SEGMENT(MB_BIOS_BEGIN), .offset = address - MB_BIOS_BEGIN, }; -interrupt_table__setup (&kvm->interrupt_table, &intr_desc); +interrupt_table__setup (&kvm->arch.interrupt_table, &intr_desc);for (i =0 ; i <ARRAY_SIZE (bios_irq_handlers); i++)setup_irq_handler (kvm, &bios_irq_handlers[i]);/* we almost done */ p =guest_flat_to_host (kvm,0 ); -interrupt_table__copy (&kvm->interrupt_table, p,REAL_INTR_SIZE ); +interrupt_table__copy (&kvm->arch.interrupt_table, p,REAL_INTR_SIZE ); }
tools/kvm/x86/boot.c +3 -3
@@ -33,9 +33,9 @@ bool kvm__load_firmware(struct kvm *kvm, const char *firmware_filename)while ((nr =read (fd, p, st.st_size)) >0 ) p += nr; - kvm->boot_selector =BIOS_SELECTOR ; - kvm->boot_ip =BIOS_IP ; - kvm->boot_sp =BIOS_SP ; + kvm->arch.boot_selector =BIOS_SELECTOR ; + kvm->arch.boot_ip =BIOS_IP ; + kvm->arch.boot_sp =BIOS_SP ;return true; }
tools/kvm/x86/include/kvm/kvm-arch.h +1 -25
@@ -23,36 +23,12 @@#define KVM_PCI_MMIO_AREA (KVM_MMIO_START + 0x1000000)#define KVM_VIRTIO_MMIO_AREA (KVM_MMIO_START + 0x2000000) -struct kvm { -int sys_fd;/* For system ioctls(), i.e. /dev/kvm */ -int vm_fd;/* For VM ioctls() */ -timer_t timerid;/* Posix timer for interrupts */ - -int nrcpus;/* Number of cpus to run */ - -u32 mem_slots;/* for KVM_SET_USER_MEMORY_REGION */ - -u64 ram_size; -void *ram_start; -u64 ram_pagesize; - -bool nmi_disabled; - -bool single_step; - +struct kvm_arch {u16 boot_selector;u16 boot_ip;u16 boot_sp;struct interrupt_table interrupt_table; - -const char *vmlinux; -struct disk_image **disks; -int nr_disks; - -char *name; - -int vm_state; };static inline void *guest_flat_to_host (struct kvm *kvm,unsigned long offset);/* In kvm.h */
tools/kvm/x86/kvm-cpu.c +15 -15
@@ -182,9 +182,9 @@ static void kvm_cpu__setup_regs(struct kvm_cpu *vcpu)/* We start the guest in 16-bit real mode */ .rflags = 0x0000000000000002ULL, - .rip = vcpu->kvm->boot_ip, - .rsp = vcpu->kvm->boot_sp, - .rbp = vcpu->kvm->boot_sp, + .rip = vcpu->kvm->arch.boot_ip, + .rsp = vcpu->kvm->arch.boot_sp, + .rbp = vcpu->kvm->arch.boot_sp, };if (vcpu->regs.rip >USHRT_MAX )
@@ -199,18 +199,18 @@ static void kvm_cpu__setup_sregs(struct kvm_cpu *vcpu)if (ioctl (vcpu->vcpu_fd,KVM_GET_SREGS , &vcpu->sregs) <0 )die_perror ("KVM_GET_SREGS failed" ); - vcpu->sregs.cs.selector = vcpu->kvm->boot_selector; - vcpu->sregs.cs.base =selector_to_base (vcpu->kvm->boot_selector); - vcpu->sregs.ss.selector = vcpu->kvm->boot_selector; - vcpu->sregs.ss.base =selector_to_base (vcpu->kvm->boot_selector); - vcpu->sregs.ds.selector = vcpu->kvm->boot_selector; - vcpu->sregs.ds.base =selector_to_base (vcpu->kvm->boot_selector); - vcpu->sregs.es.selector = vcpu->kvm->boot_selector; - vcpu->sregs.es.base =selector_to_base (vcpu->kvm->boot_selector); - vcpu->sregs.fs.selector = vcpu->kvm->boot_selector; - vcpu->sregs.fs.base =selector_to_base (vcpu->kvm->boot_selector); - vcpu->sregs.gs.selector = vcpu->kvm->boot_selector; - vcpu->sregs.gs.base =selector_to_base (vcpu->kvm->boot_selector); + vcpu->sregs.cs.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.cs.base =selector_to_base (vcpu->kvm->arch.boot_selector); + vcpu->sregs.ss.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.ss.base =selector_to_base (vcpu->kvm->arch.boot_selector); + vcpu->sregs.ds.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.ds.base =selector_to_base (vcpu->kvm->arch.boot_selector); + vcpu->sregs.es.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.es.base =selector_to_base (vcpu->kvm->arch.boot_selector); + vcpu->sregs.fs.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.fs.base =selector_to_base (vcpu->kvm->arch.boot_selector); + vcpu->sregs.gs.selector = vcpu->kvm->arch.boot_selector; + vcpu->sregs.gs.base =selector_to_base (vcpu->kvm->arch.boot_selector);if (ioctl (vcpu->vcpu_fd,KVM_SET_SREGS , &vcpu->sregs) <0 )die_perror ("KVM_SET_SREGS failed" );
tools/kvm/x86/kvm.c +6 -6
@@ -219,9 +219,9 @@ int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *while ((nr =read (fd_kernel, p,65536 )) >0 ) p += nr; - kvm->boot_selector =BOOT_LOADER_SELECTOR ; - kvm->boot_ip =BOOT_LOADER_IP ; - kvm->boot_sp =BOOT_LOADER_SP ; + kvm->arch.boot_selector =BOOT_LOADER_SELECTOR ; + kvm->arch.boot_ip =BOOT_LOADER_IP ; + kvm->arch.boot_sp =BOOT_LOADER_SP ;return true; }
@@ -322,13 +322,13 @@ bool load_bzimage(struct kvm *kvm, int fd_kernel, kern_boot->hdr.ramdisk_size = initrd_stat.st_size; } - kvm->boot_selector =BOOT_LOADER_SELECTOR ; + kvm->arch.boot_selector =BOOT_LOADER_SELECTOR ;/* * The real-mode setup code starts at offset 0x200 of a bzImage. See * Documentation/x86/boot.txt for details. */ - kvm->boot_ip =BOOT_LOADER_IP +0x200 ; - kvm->boot_sp =BOOT_LOADER_SP ; + kvm->arch.boot_ip =BOOT_LOADER_IP +0x200 ; + kvm->arch.boot_sp =BOOT_LOADER_SP ;return true; }
Signature · 1 line
1.7.12
PATCH 4/33 kvm tools: move kvm_config into struct kvm
In reply to Sasha LevinContain the options within struct kvm itself. This way options are specific to a given struct kvm and not just global.
--- tools/kvm/builtin-run.c | 273 +++++++++++++++++++------------------------- tools/kvm/include/kvm/kvm.h | 7 +- tools/kvm/kvm.c | 37 ++---- 3 files changed, 130 insertions(+), 187 deletions(-)
tools/kvm/builtin-run.c +115 -158
@@ -49,60 +49,14 @@#include <ctype.h> #include <stdio.h> -#define DEFAULT_KVM_DEV "/dev/kvm" -#define DEFAULT_CONSOLE "serial" -#define DEFAULT_NETWORK "user" -#define DEFAULT_HOST_ADDR "192.168.33.1" -#define DEFAULT_GUEST_ADDR "192.168.33.15" -#define DEFAULT_GUEST_MAC "02:15:15:15:15:15" -#define DEFAULT_HOST_MAC "02:01:01:01:01:01" -#define DEFAULT_SCRIPT "none" -const char *DEFAULT_SANDBOX_FILENAME ="guest/sandbox.sh" ; -#define MB_SHIFT (20)#define KB_SHIFT (10)#define GB_SHIFT (30) -#define MIN_RAM_SIZE_MB (64ULL) -#define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)struct kvm *kvm;struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu; -struct kvm_config { -struct disk_image_params disk_image[MAX_DISK_IMAGES ]; -u64 ram_size; -u8 image_count; -u8 num_net_devices; -bool virtio_rng; -const char *kernel_cmdline; -const char *kernel_filename; -const char *vmlinux_filename; -const char *initrd_filename; -const char *firmware_filename; -const char *console; -const char *dev; -const char *network; -const char *host_ip; -const char *guest_ip; -const char *guest_mac; -const char *host_mac; -const char *script; -const char *guest_name; -const char *sandbox; -const char *hugetlbfs_path; -const char *custom_rootfs_name; -struct virtio_net_params *net_params; -bool single_step; -bool vnc; -bool sdl; -bool balloon; -bool using_rootfs; -bool custom_rootfs; -bool no_net; -bool no_dhcp; -} cfg; -extern bool ioport_debug;extern bool mmio_debug;static int kvm_run_wrapper;
@@ -143,13 +97,13 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) S_ISDIR(st.st_mode)) {char tmp[PATH_MAX ]; -if (cfg.using_rootfs) +if (kvm->cfg.using_rootfs)die ("Please use only one rootfs directory atmost" );if (realpath (arg, tmp) ==0 ||virtio_9p__register (kvm, tmp,"/dev/root" ) <0 )die ("Unable to initialize virtio 9p" ); - cfg.using_rootfs =1 ; + kvm->cfg.using_rootfs =1 ;return 0 ; }
@@ -159,7 +113,7 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) S_ISDIR(st.st_mode)) {char tmp[PATH_MAX ]; -if (cfg.using_rootfs) +if (kvm->cfg.using_rootfs)die ("Please use only one rootfs directory atmost" ); if (realpath(path, tmp) == 0 ||
@@ -168,25 +122,25 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 )die ("Unable to initialize virtio 9p" );kvm_setup_resolv (arg); - cfg.using_rootfs = cfg.custom_rootfs =1 ; - cfg.custom_rootfs_name = arg; + kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs =1 ; + kvm->cfg.custom_rootfs_name = arg;return 0 ; } -if (cfg.image_count >=MAX_DISK_IMAGES ) +if (kvm->cfg.image_count >=MAX_DISK_IMAGES )die ("Currently only 4 images are supported" ); - cfg.disk_image[cfg.image_count].filename = arg; + kvm->cfg.disk_image[kvm->cfg.image_count].filename = arg; cur = arg;if (strncmp (arg,"scsi:" ,5 ) ==0 ) { sep =strstr (arg,":" );if (sep) - cfg.disk_image[cfg.image_count].wwpn = sep +1 ; + kvm->cfg.disk_image[kvm->cfg.image_count].wwpn = sep +1 ; sep =strstr (sep +1 ,":" );if (sep) { *sep =0 ; - cfg.disk_image[cfg.image_count].tpgt = sep +1 ; + kvm->cfg.disk_image[kvm->cfg.image_count].tpgt = sep +1 ; } cur = sep +1 ; }
@@ -195,15 +149,15 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) sep =strstr (cur,"," );if (sep) {if (strncmp (sep +1 ,"ro" ,2 ) ==0 ) - cfg.disk_image[cfg.image_count].readonly = true; + kvm->cfg.disk_image[kvm->cfg.image_count].readonly = true;else if (strncmp (sep +1 ,"direct" ,6 ) ==0 ) - cfg.disk_image[cfg.image_count].direct = true; + kvm->cfg.disk_image[kvm->cfg.image_count].direct = true; *sep =0 ; cur = sep +1 ; } }while (sep); - cfg.image_count++; + kvm->cfg.image_count++;return 0 ; }
@@ -254,17 +208,17 @@ static int set_net_param(struct virtio_net_params *p, const char *param,if (!strncmp (val,"user" ,4 )) {int i; -for (i =0 ; i < cfg.num_net_devices; i++) -if (cfg.net_params[i].mode ==NET_MODE_USER ) +for (i =0 ; i < kvm->cfg.num_net_devices; i++) +if (kvm->cfg.net_params[i].mode ==NET_MODE_USER )die ("Only one usermode network device allowed at a time" ); p->mode =NET_MODE_USER ; }else if (!strncmp (val,"tap" ,3 )) { p->mode =NET_MODE_TAP ; }else if (!strncmp (val,"none" ,4 )) { - cfg.no_net =1 ; + kvm->cfg.no_net =1 ;return -1 ; }else -die ("Unknown network mode %s, please use user, tap or none" , cfg.network); +die ("Unknown network mode %s, please use user, tap or none" , kvm->cfg.network); }else if (strcmp (param,"script" ) ==0 ) { p->script =strdup (val); }else if (strcmp (param,"guest_ip" ) ==0 ) {
@@ -304,7 +258,7 @@ static int netdev_parser(const struct option *opt, const char *arg, int unset) };str_to_mac (DEFAULT_GUEST_MAC , p.guest_mac); - p.guest_mac[5 ] += cfg.num_net_devices; + p.guest_mac[5 ] += kvm->cfg.num_net_devices;while (cur) {if (on_cmd) {
@@ -318,13 +272,13 @@ static int netdev_parser(const struct option *opt, const char *arg, int unset) cur =strtok (NULL ,",=" ); }; - cfg.num_net_devices++; + kvm->cfg.num_net_devices++; - cfg.net_params =realloc (cfg.net_params, cfg.num_net_devices *sizeof (*cfg.net_params)); -if (cfg.net_params ==NULL ) + kvm->cfg.net_params =realloc (kvm->cfg.net_params, kvm->cfg.num_net_devices *sizeof (*kvm->cfg.net_params)); +if (kvm->cfg.net_params ==NULL )die ("Failed adding new network device" ); - cfg.net_params[cfg.num_net_devices -1 ] = p; + kvm->cfg.net_params[kvm->cfg.num_net_devices -1 ] = p; done:free (buf);
@@ -816,13 +770,13 @@ static const char *find_vmlinux(void)void kvm_run_help (void ) { -BUILD_OPTIONS (options, &cfg); +BUILD_OPTIONS (options, &kvm->cfg);usage_with_options (run_usage, options); }static int kvm_setup_guest_init (void ) { -const char *rootfs = cfg.custom_rootfs_name; +const char *rootfs = kvm->cfg.custom_rootfs_name;char tmp[PATH_MAX ];size_t size;int fd, ret;
@@ -846,17 +800,17 @@ static int kvm_setup_guest_init(void)static int kvm_run_set_sandbox (void ) { -const char *guestfs_name = cfg.custom_rootfs_name; +const char *guestfs_name = kvm->cfg.custom_rootfs_name;char path[PATH_MAX ], script[PATH_MAX ], *tmp;snprintf (path,PATH_MAX ,"%s%s/virt/sandbox.sh" ,kvm__get_dir (), guestfs_name);remove (path); -if (cfg.sandbox ==NULL ) +if (kvm->cfg.sandbox ==NULL )return 0 ; - tmp =realpath (cfg.sandbox,NULL ); + tmp =realpath (kvm->cfg.sandbox,NULL );if (tmp ==NULL )return -ENOMEM ;
@@ -922,9 +876,9 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc)char program[PATH_MAX ];int fd; -remove (cfg.sandbox); +remove (kvm->cfg.sandbox); - fd =open (cfg.sandbox,O_RDWR |O_CREAT ,0777 ); + fd =open (kvm->cfg.sandbox,O_RDWR |O_CREAT ,0777 );if (fd <0 )die ("Failed creating sandbox script" );
@@ -958,7 +912,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)unsigned int nr_online_cpus;int max_cpus, recommended_cpus;int i, r; -BUILD_OPTIONS (options, &cfg); + + kvm =kvm__new (); +if (IS_ERR (kvm)) +return PTR_ERR (kvm);signal (SIGALRM , handle_sigalrm);kvm_ipc__register_handler (KVM_IPC_DEBUG , handle_debug);
@@ -969,9 +926,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)kvm_ipc__register_handler (KVM_IPC_VMSTATE , handle_vmstate); nr_online_cpus =sysconf (_SC_NPROCESSORS_ONLN); - cfg.custom_rootfs_name ="default" ; + kvm->cfg.custom_rootfs_name ="default" ;while (argc !=0 ) { +BUILD_OPTIONS (options, &kvm->cfg); argc =parse_options (argc, argv, options, run_usage,PARSE_OPT_STOP_AT_NON_OPTION |PARSE_OPT_KEEP_DASHDASH );
@@ -979,17 +937,18 @@ static int kvm_cmd_run_init(int argc, const char **argv)/* Cusrom options, should have been handled elsewhere */ if (strcmp (argv[0 ],"--" ) ==0 ) {if (kvm_run_wrapper ==KVM_RUN_SANDBOX ) { - cfg.sandbox =DEFAULT_SANDBOX_FILENAME ; + kvm->cfg.sandbox =DEFAULT_SANDBOX_FILENAME ;kvm_run_write_sandbox_cmd (argv+1 , argc-1 );break ; } } -if ((kvm_run_wrapper ==KVM_RUN_DEFAULT && cfg.kernel_filename) || - (kvm_run_wrapper ==KVM_RUN_SANDBOX && cfg.sandbox)) { +if ((kvm_run_wrapper ==KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || + (kvm_run_wrapper ==KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {fprintf (stderr,"Cannot handle parameter: " "%s\n" , argv[0 ]);usage_with_options (run_usage, options); +free (kvm);return -EINVAL ; }if (kvm_run_wrapper ==KVM_RUN_SANDBOX ) {
@@ -997,14 +956,14 @@ static int kvm_cmd_run_init(int argc, const char **argv)* first unhandled parameter is treated as * sandbox command */ - cfg.sandbox =DEFAULT_SANDBOX_FILENAME ; + kvm->cfg.sandbox =DEFAULT_SANDBOX_FILENAME ;kvm_run_write_sandbox_cmd (argv, argc); }else {/* * first unhandled parameter is treated as a kernel * image */ - cfg.kernel_filename = argv[0 ]; + kvm->cfg.kernel_filename = argv[0 ]; } argv++; argc--;
@@ -1012,78 +971,76 @@ static int kvm_cmd_run_init(int argc, const char **argv) } -if (!cfg.kernel_filename) - cfg.kernel_filename =find_kernel (); +if (!kvm->cfg.kernel_filename) + kvm->cfg.kernel_filename =find_kernel (); -if (!cfg.kernel_filename) { +if (!kvm->cfg.kernel_filename) {kernel_usage_with_options ();return -EINVAL ; } - cfg.vmlinux_filename =find_vmlinux (); + kvm->cfg.vmlinux_filename =find_vmlinux ();if (nrcpus ==0 ) nrcpus = nr_online_cpus; -if (!cfg.ram_size) - cfg.ram_size =get_ram_size (nrcpus); +if (!kvm->cfg.ram_size) + kvm->cfg.ram_size =get_ram_size (nrcpus); -if (cfg.ram_size <MIN_RAM_SIZE_MB ) -die ("Not enough memory specified: %lluMB (min %lluMB)" , cfg.ram_size,MIN_RAM_SIZE_MB ); +if (kvm->cfg.ram_size <MIN_RAM_SIZE_MB ) +die ("Not enough memory specified: %lluMB (min %lluMB)" , kvm->cfg.ram_size,MIN_RAM_SIZE_MB ); -if (cfg.ram_size >host_ram_size ()) -pr_warning ("Guest memory size %lluMB exceeds host physical RAM size %lluMB" , cfg.ram_size,host_ram_size ()); +if (kvm->cfg.ram_size >host_ram_size ()) +pr_warning ("Guest memory size %lluMB exceeds host physical RAM size %lluMB" , kvm->cfg.ram_size,host_ram_size ()); - cfg.ram_size <<=MB_SHIFT ; + kvm->cfg.ram_size <<=MB_SHIFT ; -if (!cfg.dev) - cfg.dev =DEFAULT_KVM_DEV ; +if (!kvm->cfg.dev) + kvm->cfg.dev =DEFAULT_KVM_DEV ; -if (!cfg.console) - cfg.console =DEFAULT_CONSOLE ; +if (!kvm->cfg.console) + kvm->cfg.console =DEFAULT_CONSOLE ; -if (!strncmp (cfg.console,"virtio" ,6 )) +if (!strncmp (kvm->cfg.console,"virtio" ,6 )) active_console =CONSOLE_VIRTIO ; -else if (!strncmp (cfg.console,"serial" ,6 )) +else if (!strncmp (kvm->cfg.console,"serial" ,6 )) active_console =CONSOLE_8250 ; -else if (!strncmp (cfg.console,"hv" ,2 )) +else if (!strncmp (kvm->cfg.console,"hv" ,2 )) active_console =CONSOLE_HV ;else pr_warning ("No console!" ); -if (!cfg.host_ip) - cfg.host_ip =DEFAULT_HOST_ADDR ; +if (!kvm->cfg.host_ip) + kvm->cfg.host_ip =DEFAULT_HOST_ADDR ; -if (!cfg.guest_ip) - cfg.guest_ip =DEFAULT_GUEST_ADDR ; +if (!kvm->cfg.guest_ip) + kvm->cfg.guest_ip =DEFAULT_GUEST_ADDR ; -if (!cfg.guest_mac) - cfg.guest_mac =DEFAULT_GUEST_MAC ; +if (!kvm->cfg.guest_mac) + kvm->cfg.guest_mac =DEFAULT_GUEST_MAC ; -if (!cfg.host_mac) - cfg.host_mac =DEFAULT_HOST_MAC ; +if (!kvm->cfg.host_mac) + kvm->cfg.host_mac =DEFAULT_HOST_MAC ; -if (!cfg.script) - cfg.script =DEFAULT_SCRIPT ; +if (!kvm->cfg.script) + kvm->cfg.script =DEFAULT_SCRIPT ;term_init (); -if (!cfg.guest_name) { -if (cfg.custom_rootfs) { - cfg.guest_name = cfg.custom_rootfs_name; +if (!kvm->cfg.guest_name) { +if (kvm->cfg.custom_rootfs) { + kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; }else {sprintf (default_name,"guest-%u" ,getpid ()); - cfg.guest_name = default_name; + kvm->cfg.guest_name = default_name; } } - kvm =kvm__init (cfg.dev, cfg.hugetlbfs_path, cfg.ram_size, cfg.guest_name); -if (IS_ERR (kvm)) { - r =PTR_ERR (kvm); + r =kvm__init (kvm); +if (r) goto fail; - } - kvm->single_step = cfg.single_step; + kvm->single_step = kvm->cfg.single_step; r =ioeventfd__init (kvm);if (r <0 ) {
@@ -1131,7 +1088,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)* vidmode should be either specified * either set by default */ -if (cfg.vnc || cfg.sdl) { +if (kvm->cfg.vnc || kvm->cfg.sdl) {if (vidmode ==-1 ) vidmode =0x312 ; }else {
@@ -1139,36 +1096,36 @@ static int kvm_cmd_run_init(int argc, const char **argv) }memset (real_cmdline,0 ,sizeof (real_cmdline)); -kvm__arch_set_cmdline (real_cmdline, cfg.vnc || cfg.sdl); +kvm__arch_set_cmdline (real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);if (strlen (real_cmdline) >0 )strcat (real_cmdline," " ); -if (cfg.kernel_cmdline) -strlcat (real_cmdline, cfg.kernel_cmdline,sizeof (real_cmdline)); +if (kvm->cfg.kernel_cmdline) +strlcat (real_cmdline, kvm->cfg.kernel_cmdline,sizeof (real_cmdline)); -if (!cfg.using_rootfs && !cfg.disk_image[0 ].filename && !cfg.initrd_filename) { +if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0 ].filename && !kvm->cfg.initrd_filename) {char tmp[PATH_MAX ]; -kvm_setup_create_new (cfg.custom_rootfs_name); -kvm_setup_resolv (cfg.custom_rootfs_name); +kvm_setup_create_new (kvm->cfg.custom_rootfs_name); +kvm_setup_resolv (kvm->cfg.custom_rootfs_name);snprintf (tmp,PATH_MAX ,"%s%s" ,kvm__get_dir (),"default" );if (virtio_9p__register (kvm, tmp,"/dev/root" ) <0 )die ("Unable to initialize virtio 9p" );if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 )die ("Unable to initialize virtio 9p" ); - cfg.using_rootfs = cfg.custom_rootfs =1 ; + kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs =1 ; } -if (cfg.using_rootfs) { +if (kvm->cfg.using_rootfs) {strcat (real_cmdline," root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p" ); -if (cfg.custom_rootfs) { +if (kvm->cfg.custom_rootfs) {kvm_run_set_sandbox ();strcat (real_cmdline," init=/virt/init" ); -if (!cfg.no_dhcp) +if (!kvm->cfg.no_dhcp)strcat (real_cmdline," ip=dhcp" );if (kvm_setup_guest_init ())die ("Failed to setup init for guest." );
@@ -1177,9 +1134,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)strlcat (real_cmdline," root=/dev/vda rw " ,sizeof (real_cmdline)); } -if (cfg.image_count) { - kvm->nr_disks = cfg.image_count; - kvm->disks =disk_image__open_all ((struct disk_image_params *)&cfg.disk_image, cfg.image_count); +if (kvm->cfg.image_count) { + kvm->nr_disks = kvm->cfg.image_count; + kvm->disks =disk_image__open_all ((struct disk_image_params *)&kvm->cfg.disk_image, kvm->cfg.image_count);if (IS_ERR (kvm->disks)) { r =PTR_ERR (kvm->disks); pr_err("disk_image__open_all() failed with error %ld\n",
@@ -1189,14 +1146,14 @@ static int kvm_cmd_run_init(int argc, const char **argv) }printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , - cfg.kernel_filename, cfg.ram_size /1024 /1024 , nrcpus, cfg.guest_name); + kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , nrcpus, kvm->cfg.guest_name); -if (!cfg.firmware_filename) { -if (!kvm__load_kernel (kvm, cfg.kernel_filename, - cfg.initrd_filename, real_cmdline, vidmode)) -die ("unable to load kernel %s" , cfg.kernel_filename); +if (!kvm->cfg.firmware_filename) { +if (!kvm__load_kernel (kvm, kvm->cfg.kernel_filename, + kvm->cfg.initrd_filename, real_cmdline, vidmode)) +die ("unable to load kernel %s" , kvm->cfg.kernel_filename); - kvm->vmlinux = cfg.vmlinux_filename; + kvm->vmlinux = kvm->cfg.vmlinux_filename; r =symbol_init (kvm);if (r <0 )pr_debug ("symbol_init() failed with error %d\n" , r);
@@ -1232,34 +1189,34 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (active_console ==CONSOLE_VIRTIO )virtio_console__init (kvm); -if (cfg.virtio_rng) +if (kvm->cfg.virtio_rng)virtio_rng__init (kvm); -if (cfg.balloon) +if (kvm->cfg.balloon)virtio_bln__init (kvm); -if (!cfg.network) - cfg.network =DEFAULT_NETWORK ; +if (!kvm->cfg.network) + kvm->cfg.network =DEFAULT_NETWORK ;virtio_9p__init (kvm); -for (i =0 ; i < cfg.num_net_devices; i++) { - cfg.net_params[i].kvm = kvm; -virtio_net__init (&cfg.net_params[i]); +for (i =0 ; i < kvm->cfg.num_net_devices; i++) { + kvm->cfg.net_params[i].kvm = kvm; +virtio_net__init (&kvm->cfg.net_params[i]); } -if (cfg.num_net_devices ==0 && cfg.no_net ==0 ) { +if (kvm->cfg.num_net_devices ==0 && kvm->cfg.no_net ==0 ) {struct virtio_net_params net_params; net_params = (struct virtio_net_params ) { - .guest_ip = cfg.guest_ip, - .host_ip = cfg.host_ip, + .guest_ip = kvm->cfg.guest_ip, + .host_ip = kvm->cfg.host_ip, .kvm = kvm, - .script = cfg.script, + .script = kvm->cfg.script, .mode =NET_MODE_USER , }; -str_to_mac (cfg.guest_mac, net_params.guest_mac); -str_to_mac (cfg.host_mac, net_params.host_mac); +str_to_mac (kvm->cfg.guest_mac, net_params.guest_mac); +str_to_mac (kvm->cfg.host_mac, net_params.host_mac);virtio_net__init (&net_params); }
@@ -1272,7 +1229,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)pci_shmem__init (kvm); -if (cfg.vnc || cfg.sdl) { +if (kvm->cfg.vnc || kvm->cfg.sdl) { fb =vesa__init (kvm);if (IS_ERR (fb)) {pr_err ("vesa__init() failed with error %ld\n" ,PTR_ERR (fb));
@@ -1280,7 +1237,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -if (cfg.vnc && fb) { +if (kvm->cfg.vnc && fb) { r =vnc__init (fb);if (r <0 ) {pr_err ("vnc__init() failed with error %d\n" , r);
@@ -1288,7 +1245,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -if (cfg.sdl && fb) { +if (kvm->cfg.sdl && fb) {sdl__init (fb);if (r <0 ) {pr_err ("sdl__init() failed with error %d\n" , r);
@@ -1308,9 +1265,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)kvm__start_timer (kvm); -if (cfg.firmware_filename) { -if (!kvm__load_firmware (kvm, cfg.firmware_filename)) -die ("unable to load firmware image %s: %s" , cfg.firmware_filename,strerror (errno)); +if (kvm->cfg.firmware_filename) { +if (!kvm__load_firmware (kvm, kvm->cfg.firmware_filename)) +die ("unable to load firmware image %s: %s" , kvm->cfg.firmware_filename,strerror (errno)); }else {kvm__arch_setup_firmware (kvm);if (r <0 ) {
@@ -1389,7 +1346,7 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_rng__exit() failed with error %d\n" , r); - r =disk_image__close_all (kvm->disks, cfg.image_count); + r =disk_image__close_all (kvm->disks, kvm->cfg.image_count);if (r <0 )pr_warning ("disk_image__close_all() failed with error %d\n" , r);
tools/kvm/include/kvm/kvm.h +4 -3
@@ -2,6 +2,7 @@#define KVM__KVM_H #include "kvm/kvm-arch.h" +#include "kvm/kvm-config.h" #include <stdbool.h> #include <linux/types.h>
@@ -33,6 +34,7 @@ struct kvm_ext {struct kvm {struct kvm_arch arch; +struct kvm_config cfg;int sys_fd;/* For system ioctls(), i.e. /dev/kvm */ int vm_fd;/* For VM ioctls() */ timer_t timerid;/* Posix timer for interrupts */
@@ -53,15 +55,14 @@ struct kvm {struct disk_image **disks;int nr_disks; -char *name; -int vm_state; };void kvm__set_dir (const char *fmt, ...);const char *kvm__get_dir (void ); -struct kvm *kvm__init (const char *kvm_dev,const char *hugetlbfs_path,u64 ram_size,const char *name); +int kvm__init (struct kvm *kvm); +struct kvm *kvm__new (void );int kvm__recommended_cpus (struct kvm *kvm);int kvm__max_cpus (struct kvm *kvm);void kvm__init_ram (struct kvm *kvm);
tools/kvm/kvm.c +11 -26
@@ -121,7 +121,7 @@ static int kvm__check_extensions(struct kvm *kvm)return 0 ; } -static struct kvm *kvm__new (void ) +struct kvm *kvm__new (void ) {struct kvm *kvm =calloc (1 ,sizeof (*kvm));if (!kvm)
@@ -146,11 +146,8 @@ static int kvm__create_socket(struct kvm *kvm)/* This usually 108 bytes long */ BUILD_BUG_ON (sizeof (local.sun_path) <32 ); -if (!kvm->name) -return -EINVAL ; -snprintf (full_name,sizeof (full_name),"%s/%s%s" , -kvm__get_dir (), kvm->name,KVM_SOCK_SUFFIX ); +kvm__get_dir (), kvm->cfg.guest_name,KVM_SOCK_SUFFIX );if (access (full_name,F_OK ) ==0 ) {pr_err ("Socket file %s already exist" , full_name);return -EEXIST ;
@@ -261,8 +258,7 @@ int kvm__exit(struct kvm *kvm)kvm__arch_delete_ram (kvm);kvm_ipc__stop (); -kvm__remove_socket (kvm->name); -free (kvm->name); +kvm__remove_socket (kvm->cfg.guest_name);free (kvm);return 0 ;
@@ -338,9 +334,8 @@ int kvm__max_cpus(struct kvm *kvm)return ret; } -struct kvm *kvm__init (const char *kvm_dev,const char *hugetlbfs_path,u64 ram_size,const char *name) +int kvm__init (struct kvm *kvm) { -struct kvm *kvm;int ret;if (!kvm__arch_cpu_supports_vm ()) {
@@ -349,21 +344,17 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s goto err; } - kvm =kvm__new (); -if (IS_ERR (kvm)) -return kvm; - - kvm->sys_fd =open (kvm_dev,O_RDWR ); + kvm->sys_fd =open (kvm->cfg.dev,O_RDWR );if (kvm->sys_fd <0 ) {if (errno ==ENOENT )pr_err ("'%s' not found. Please make sure your kernel has CONFIG_KVM " -"enabled and that the KVM modules are loaded." , kvm_dev); +"enabled and that the KVM modules are loaded." , kvm->cfg.dev);else if (errno ==ENODEV )pr_err ("'%s' KVM driver not available.\n # (If the KVM " "module is loaded then 'dmesg' may offer further clues " -"about the failure.)" , kvm_dev); +"about the failure.)" , kvm->cfg.dev);else -pr_err ("Could not open %s: " , kvm_dev); +pr_err ("Could not open %s: " , kvm->cfg.dev); ret = -errno; goto err_free;
@@ -382,19 +373,13 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s goto err_sys_fd; } - kvm->name =strdup (name); -if (!kvm->name) { - ret = -ENOMEM ; - goto err_vm_fd; - } -if (kvm__check_extensions (kvm)) {pr_err ("A required KVM extention is not supported by OS" ); ret = -ENOSYS ; goto err_vm_fd; } -kvm__arch_init (kvm, hugetlbfs_path, ram_size); +kvm__arch_init (kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); ret =kvm_ipc__start (kvm__create_socket (kvm));if (ret <0 ) {
@@ -408,7 +393,7 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s goto err_ipc; } -return kvm; +return 0 ; err_ipc:kvm_ipc__stop ();
@@ -419,7 +404,7 @@ err_sys_fd: err_free:free (kvm); err: -return ERR_PTR (ret); +return ret; }/* RFC 1952 */
Signature · 1 line
1.7.12
PATCH 5/33 kvm tools: remove redundancy between kvm_config and kvm
In reply to Sasha LevinRemove some redundant members between struct kvm_config and struct kvm since options are now contained within struct kvm.
--- tools/kvm/builtin-run.c | 2 -- tools/kvm/hw/rtc.c | 2 +- tools/kvm/include/kvm/kvm.h | 3 --- tools/kvm/kvm-cpu.c | 2 +- 4 files changed, 2 insertions(+), 7 deletions(-)
tools/kvm/builtin-run.c +0 -2
@@ -1040,8 +1040,6 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (r) goto fail; - kvm->single_step = kvm->cfg.single_step; - r =ioeventfd__init (kvm);if (r <0 ) {pr_err ("ioeventfd__init() failed with error %d\n" , r);
tools/kvm/hw/rtc.c +1 -1
@@ -134,4 +134,4 @@ int rtc__exit(struct kvm *kvm)ioport__unregister (0x0071 );return 0 ; -} +}
tools/kvm/include/kvm/kvm.h +0 -3
@@ -42,15 +42,12 @@ struct kvm {int nrcpus;/* Number of cpus to run */ u32 mem_slots;/* for KVM_SET_USER_MEMORY_REGION */ -u64 ram_size;void *ram_start;u64 ram_pagesize;bool nmi_disabled; -bool single_step; -const char *vmlinux;struct disk_image **disks;int nr_disks;
tools/kvm/kvm-cpu.c +1 -1
@@ -92,7 +92,7 @@ int kvm_cpu__start(struct kvm_cpu *cpu)kvm_cpu__reset_vcpu (cpu); -if (cpu->kvm->single_step) +if (cpu->kvm->cfg.single_step)kvm_cpu__enable_singlestep (cpu);while (cpu->is_running) {
Signature · 1 line
1.7.12
PATCH 6/33 kvm tools: move ioport_debug into struct kvm_config
In reply to Sasha LevinThis config option was 'extern'ed between different objects. Clean it up and move it into struct kvm_config.
--- tools/kvm/builtin-run.c | 3 +-- tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/ioport.c | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-)
tools/kvm/builtin-run.c +1 -2
@@ -57,7 +57,6 @@ struct kvm *kvm;struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu; -extern bool ioport_debug;extern bool mmio_debug;static int kvm_run_wrapper;extern int active_console;
@@ -471,7 +470,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset) "Enable debug messages"), \OPT_BOOLEAN ('\0' ,"debug-single-step" , &(cfg)->single_step, \"Enable single stepping" ), \ -OPT_BOOLEAN ('\0' ,"debug-ioport" , &ioport_debug, \ +OPT_BOOLEAN ('\0' ,"debug-ioport" , &(cfg)->ioport_debug, \"Enable ioport debugging" ), \OPT_BOOLEAN ('\0' ,"debug-mmio" , &mmio_debug, \ "Enable MMIO debugging"), \
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -48,6 +48,7 @@ struct kvm_config {bool custom_rootfs;bool no_net;bool no_dhcp; + bool ioport_debug; };#endif
tools/kvm/ioport.c +2 -3
@@ -21,7 +21,6 @@ DEFINE_MUTEX(ioport_mutex);static u16 free_io_port_idx;/* protected by ioport_mutex */ static struct rb_root ioport_tree =RB_ROOT ; -bool ioport_debug;static u16 ioport__find_free_port (void ) {
@@ -177,10 +176,10 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s error:br_read_unlock (); -if (ioport_debug) +if (kvm->cfg.ioport_debug)ioport_error (port, data, direction, size, count); -return !ioport_debug; +return !kvm->cfg.ioport_debug; }int ioport__init (struct kvm *kvm)
Signature · 1 line
1.7.12
PATCH 7/33 kvm tools: move mmio_debug into struct kvm_config
In reply to Sasha LevinThis config option was 'extern'ed between different objects. Clean it up and move it into struct kvm_config.
--- tools/kvm/builtin-run.c | 3 +-- tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/mmio.c | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-)
tools/kvm/builtin-run.c +1 -2
@@ -57,7 +57,6 @@ struct kvm *kvm;struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu; -extern bool mmio_debug;static int kvm_run_wrapper;extern int active_console;extern int debug_iodelay;
@@ -472,7 +471,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset) "Enable single stepping"), \OPT_BOOLEAN ('\0' ,"debug-ioport" , &(cfg)->ioport_debug, \"Enable ioport debugging" ), \ -OPT_BOOLEAN ('\0' ,"debug-mmio" , &mmio_debug, \ +OPT_BOOLEAN ('\0' ,"debug-mmio" , &(cfg)->mmio_debug, \"Enable MMIO debugging" ), \OPT_INTEGER ('\0' ,"debug-iodelay" , &debug_iodelay, \ "Delay IO by millisecond"), \
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -49,6 +49,7 @@ struct kvm_config {bool no_net;bool no_dhcp;bool ioport_debug; + bool mmio_debug; };#endif
tools/kvm/mmio.c +1 -2
@@ -21,7 +21,6 @@ struct mmio_mapping { };static struct rb_root mmio_tree =RB_ROOT ; -bool mmio_debug = false;static struct mmio_mapping *mmio_search (struct rb_root *root,u64 addr,u64 len) {
@@ -130,7 +129,7 @@ bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_if (mmio) mmio->mmio_fn (phys_addr, data, len, is_write, mmio->ptr);else { -if (mmio_debug) +if (kvm->cfg.mmio_debug)fprintf (stderr,"Warning: Ignoring MMIO %s at %016llx (length %u)\n" ,to_direction (is_write), phys_addr, len); }
Signature · 1 line
1.7.12
PATCH 8/33 kvm tools: move active_console into struct kvm_config
In reply to Sasha LevinThis config option was 'extern'ed between different objects. Clean it up and move it into struct kvm_config.
--- tools/kvm/builtin-run.c | 9 ++++----- tools/kvm/hw/serial.c | 16 ++++++++++------ tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/include/kvm/term.h | 10 +++++----- tools/kvm/powerpc/spapr_hvcons.c | 14 ++++++++++---- tools/kvm/term.c | 28 ++++++---------------------- tools/kvm/virtio/console.c | 12 +++++++++--- 7 files changed, 45 insertions(+), 45 deletions(-)
tools/kvm/builtin-run.c +4 -5
@@ -58,7 +58,6 @@ struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu;static int kvm_run_wrapper; -extern int active_console;extern int debug_iodelay;bool do_debug_print = false;
@@ -1000,11 +999,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) kvm->cfg.console =DEFAULT_CONSOLE ;if (!strncmp (kvm->cfg.console,"virtio" ,6 )) - active_console =CONSOLE_VIRTIO ; + kvm->cfg.active_console =CONSOLE_VIRTIO ;else if (!strncmp (kvm->cfg.console,"serial" ,6 )) - active_console =CONSOLE_8250 ; + kvm->cfg.active_console =CONSOLE_8250 ;else if (!strncmp (kvm->cfg.console,"hv" ,2 )) - active_console =CONSOLE_HV ; + kvm->cfg.active_console =CONSOLE_HV ;else pr_warning ("No console!" );
@@ -1182,7 +1181,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } -if (active_console ==CONSOLE_VIRTIO ) +if (kvm->cfg.active_console ==CONSOLE_VIRTIO )virtio_console__init (kvm);if (kvm->cfg.virtio_rng)
tools/kvm/hw/serial.c +10 -6
@@ -95,12 +95,13 @@ static struct serial8250_device devices[] = { }, }; -static void serial8250_flush_tx (struct serial8250_device *dev) +static void serial8250_flush_tx (struct kvm *kvm,struct serial8250_device *dev) { dev->lsr |=UART_LSR_TEMT |UART_LSR_THRE ;if (dev->txcnt) { -term_putc (CONSOLE_8250 , dev->txbuf, dev->txcnt, dev->id); +if (kvm->cfg.active_console ==CONSOLE_8250 ) +term_putc (dev->txbuf, dev->txcnt, dev->id); dev->txcnt =0 ; } }
@@ -149,7 +150,7 @@ static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev* here. */ if (!(dev->ier &UART_IER_THRI )) -serial8250_flush_tx (dev); +serial8250_flush_tx (kvm, dev); }#define SYSRQ_PENDING_NONE 0
@@ -175,7 +176,7 @@ static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,* should give the kernel the desired pause. That also flushes * the tx fifo to the terminal. */ -serial8250_flush_tx (dev); +serial8250_flush_tx (kvm, dev);if (dev->mcr &UART_MCR_LOOP )return ;
@@ -188,10 +189,13 @@ static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,return ; } -while (term_readable (CONSOLE_8250 , dev->id) && +if (kvm->cfg.active_console !=CONSOLE_8250 ) +return ; + +while (term_readable (dev->id) && dev->rxcnt <FIFO_LEN ) { - c =term_getc (CONSOLE_8250 , dev->id); + c =term_getc (dev->id);if (c <0 )break ;
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -22,6 +22,7 @@ struct kvm_config {u8 image_count;u8 num_net_devices;bool virtio_rng; +int active_console;const char *kernel_cmdline;const char *kernel_filename;const char *vmlinux_filename;
tools/kvm/include/kvm/term.h +5 -5
@@ -8,12 +8,12 @@#define CONSOLE_VIRTIO 2#define CONSOLE_HV 3 -int term_putc_iov (int who,struct iovec *iov,int iovcnt,int term); -int term_getc_iov (int who,struct iovec *iov,int iovcnt,int term); -int term_putc (int who,char *addr,int cnt,int term); -int term_getc (int who,int term); +int term_putc_iov (struct iovec *iov,int iovcnt,int term); +int term_getc_iov (struct iovec *iov,int iovcnt,int term); +int term_putc (char *addr,int cnt,int term); +int term_getc (int term); -bool term_readable (int who,int term); +bool term_readable (int term);void term_set_tty (int term);void term_init (void );
tools/kvm/powerpc/spapr_hvcons.c +10 -4
@@ -50,7 +50,10 @@ static unsigned long h_put_term_char(struct kvm_cpu *vcpu, unsigned long opcode, do { int ret; - ret = term_putc_iov(CONSOLE_HV, &iov, 1, 0); + if (kvm->cfg.active_console == CONSOLE_HV) + ret = term_putc_iov(&iov, 1, 0); + else + ret = 0; if (ret < 0) { die("term_putc_iov error %d!\n", errno); }
@@ -71,11 +74,14 @@ static unsigned long h_get_term_char(struct kvm_cpu *vcpu, unsigned long opcode,union hv_chario data;struct iovec iov; -if (term_readable (CONSOLE_HV ,0 )) { +if (kvm->cfg.active_console !=CONSOLE_HV ) +return H_SUCCESS ; + +if (term_readable (0 )) { iov.iov_base = data.buf; iov.iov_len =16 ; - *len =term_getc_iov (CONSOLE_HV , &iov,1 ,0 ); + *len =term_getc_iov (&iov,1 ,0 ); *char0_7 =be64_to_cpu (data.a.char0_7); *char8_15 =be64_to_cpu (data.a.char8_15); }else {
@@ -87,7 +93,7 @@ static unsigned long h_get_term_char(struct kvm_cpu *vcpu, unsigned long opcode,void spapr_hvcons_poll (struct kvm *kvm) { -if (term_readable (CONSOLE_HV ,0 )) { +if (term_readable (0 )) {/* * We can inject an IRQ to guest here if we want. The guest * will happily poll, though, so not required.
tools/kvm/term.c +6 -22
@@ -23,16 +23,12 @@ static struct termios orig_term;int term_escape_char =0x01 ;/* ctrl-a is used for escape */ bool term_got_escape = false; -int active_console; -int term_fds[4 ][2 ]; -int term_getc (int who,int term) +int term_getc (int term) {unsigned char c; -if (who != active_console) -return -1 ;if (read_in_full (term_fds[term][TERM_FD_IN ], &c,1 ) <0 )return -1 ;
@@ -52,13 +48,10 @@ int term_getc(int who, int term)return c; } -int term_putc (int who,char *addr,int cnt,int term) +int term_putc (char *addr,int cnt,int term) {int ret; -if (who != active_console) -return -1 ; -while (cnt--) { ret =write (term_fds[term][TERM_FD_OUT ], addr++,1 );if (ret <0 )
@@ -68,14 +61,11 @@ int term_putc(int who, char *addr, int cnt, int term)return cnt; } -int term_getc_iov (int who,struct iovec *iov,int iovcnt,int term) +int term_getc_iov (struct iovec *iov,int iovcnt,int term) {int c; -if (who != active_console) -return 0 ; - - c =term_getc (who, term); + c =term_getc (term);if (c <0 )return 0 ;
@@ -85,15 +75,12 @@ int term_getc_iov(int who, struct iovec *iov, int iovcnt, int term)return sizeof (char ); } -int term_putc_iov (int who,struct iovec *iov,int iovcnt,int term) +int term_putc_iov (struct iovec *iov,int iovcnt,int term) { -if (who != active_console) -return 0 ; -return writev (term_fds[term][TERM_FD_OUT ], iov, iovcnt); } -bool term_readable (int who,int term) +bool term_readable (int term) {struct pollfd pollfd = (struct pollfd ) { .fd = term_fds[term][TERM_FD_IN ],
@@ -101,9 +88,6 @@ bool term_readable(int who, int term) .revents = 0, }; -if (who != active_console) -return false; -return poll (&pollfd,1 ,0 ) >0 ; }
tools/kvm/virtio/console.c +9 -3
@@ -62,13 +62,16 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *paru16 head;int len; +if (kvm->cfg.active_console !=CONSOLE_VIRTIO ) +return ; +mutex_lock (&cdev.mutex); vq = param; -if (term_readable (CONSOLE_VIRTIO ,0 ) &&virt_queue__available (vq)) { +if (term_readable (0 ) &&virt_queue__available (vq)) { head =virt_queue__get_iov (vq, iov, &out, &in, kvm); - len =term_getc_iov (CONSOLE_VIRTIO , iov, in,0 ); + len =term_getc_iov (iov, in,0 );virt_queue__set_used_elem (vq, head, len); cdev.vdev.ops->signal_vq (kvm, &cdev.vdev, vq - cdev.vqs); }
@@ -99,7 +102,10 @@ static void virtio_console_handle_callback(struct kvm *kvm, void *param)while (virt_queue__available (vq)) { head =virt_queue__get_iov (vq, iov, &out, &in, kvm); - len =term_putc_iov (CONSOLE_VIRTIO , iov, out,0 ); +if (kvm->cfg.active_console ==CONSOLE_VIRTIO ) + len =term_putc_iov (iov, out,0 ); +else + len =0 ;virt_queue__set_used_elem (vq, head, len); }
Signature · 1 line
1.7.12
PATCH 9/33 kvm tools: add private ptr to option parser
In reply to Sasha LevinSupport passing a private ptr to CALLBACK options. This will make it possible assigning options into specific struct kvms by passing them directly to parsers.
--- tools/kvm/builtin-run.c | 13 +++++++------ tools/kvm/include/kvm/parse-options.h | 16 ++++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-)
tools/kvm/builtin-run.c +7 -6
@@ -417,9 +417,10 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)OPT_CALLBACK ('\0' ,"shmem" ,NULL , \"[pci:]<addr>:<size>[:handle=<handle>][:create]" , \"Share host shmem with guest via pci device" , \ - shmem_parser), \ -OPT_CALLBACK ('d' ,"disk" ,NULL ,"image or rootfs_dir" ,"Disk \ -image or rootfs directory" , img_name_parser), \ + shmem_parser,NULL ), \ +OPT_CALLBACK ('d' ,"disk" , kvm,"image or rootfs_dir" ,"Disk \ +image or rootfs directory" , img_name_parser, \ +NULL ), \OPT_BOOLEAN ('\0' ,"balloon" , &(cfg)->balloon,"Enable virtio \ balloon" ), \ OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
@@ -428,14 +429,14 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset) Number Generator"), \ OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ "Enable virtio 9p to share files between host and \ - guest", virtio_9p_rootdir_parser), \ + guest", virtio_9p_rootdir_parser, NULL), \ OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or \ hv", " Console to use"), \ OPT_STRING('\0', " dev", &(cfg)->dev, " device_file", \ " KVM device file"), \ OPT_CALLBACK('\0', " tty", NULL, " tty id", \ " Remap guestTTY into a pty on the host", \ -tty_parser), \ +tty_parser, NULL), \ OPT_STRING('\0', " sandbox", &(cfg)->sandbox, " script", \ " Run this script when booting into custom \ rootfs"), \
@@ -455,7 +456,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)OPT_GROUP ("Networking options:" ), \OPT_CALLBACK_DEFAULT ('n' ,"network" ,NULL ,"network params" , \"Create a new guest NIC" , \ - netdev_parser,NULL ), \ + netdev_parser,NULL ,NULL ), \OPT_BOOLEAN ('\0' ,"no-dhcp" , &(cfg)->no_dhcp,"Disable kernel DHCP\ in rootfs mode"), \ \
tools/kvm/include/kvm/parse-options.h +10 -6
@@ -89,6 +89,7 @@ const char *long_name;void *value;const char *argh;const char *help; +void *ptr;int flags;parse_opt_cb *callback;
@@ -150,7 +151,7 @@ intptr_t defval; .help = (h) \ } -#define OPT_CALLBACK (s, l, v, a, h, f) \ +#define OPT_CALLBACK (s, l, v, a, h, f, p) \ { \ .type = OPTION_CALLBACK, \ .short_name = (s), \
@@ -158,10 +159,11 @@ intptr_t defval; .value = (v), \ (a), \ .help = (h), \ - .callback = (f) \ + .callback = (f), \ + .ptr = (p), \ } -#define OPT_CALLBACK_NOOPT (s, l, v, a, h, f) \ +#define OPT_CALLBACK_NOOPT (s, l, v, a, h, f, p) \ { \ .type = OPTION_CALLBACK, \ .short_name = (s), \
@@ -170,10 +172,11 @@ intptr_t defval; (a), \ .help = (h), \ .callback = (f), \ - .flags = PARSE_OPT_NOARG \ + .flags = PARSE_OPT_NOARG, \ + .ptr = (p), \ } -#define OPT_CALLBACK_DEFAULT (s, l, v, a, h, f, d) \ +#define OPT_CALLBACK_DEFAULT (s, l, v, a, h, f, d, p) \ { \ .type = OPTION_CALLBACK, \ .short_name = (s), \
@@ -182,7 +185,8 @@ intptr_t defval; .help = (h), \ .callback = (f), \ .defval = (intptr_t)d, \ - .flags = PARSE_OPT_LASTARG_DEFAULT \ + .flags = PARSE_OPT_LASTARG_DEFAULT, \ + .ptr = (p) \ }#define OPT_END () { .type = OPTION_END }
Signature · 1 line
1.7.12
PATCH 10/33 kvm tools: disk image related cleanup
In reply to Sasha LevinMove io debug delay into kvm_config, the parser out of builtin-run into the disk code and make the init/exit functions match the rest of the code in style.
--- tools/kvm/builtin-run.c | 75 ++++++++++---------------------------- tools/kvm/disk/core.c | 71 ++++++++++++++++++++++++++++++++++-- tools/kvm/include/kvm/disk-image.h | 9 +++-- tools/kvm/include/kvm/kvm-config.h | 1 + 4 files changed, 92 insertions(+), 64 deletions(-)
tools/kvm/builtin-run.c +19 -56
@@ -58,7 +58,6 @@ struct kvm_cpu **kvm_cpus; __threadstruct kvm_cpu *current_kvm_cpu;static int kvm_run_wrapper; -extern int debug_iodelay;bool do_debug_print = false;
@@ -78,17 +77,11 @@ enum { KVM_RUN_SANDBOX, }; -void kvm_run_set_wrapper_sandbox (void ) -{ - kvm_run_wrapper =KVM_RUN_SANDBOX ; -} -static int img_name_parser (const struct option *opt,const char *arg,int unset) {char path[PATH_MAX ]; -const char *cur;struct stat st; -char *sep; +struct kvm *kvm = opt->ptr;if (stat (arg, &st) ==0 &&S_ISDIR (st.st_mode)) {
@@ -124,39 +117,12 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)return 0 ; } -if (kvm->cfg.image_count >=MAX_DISK_IMAGES ) -die ("Currently only 4 images are supported" ); - - kvm->cfg.disk_image[kvm->cfg.image_count].filename = arg; - cur = arg; - -if (strncmp (arg,"scsi:" ,5 ) ==0 ) { - sep =strstr (arg,":" ); -if (sep) - kvm->cfg.disk_image[kvm->cfg.image_count].wwpn = sep +1 ; - sep =strstr (sep +1 ,":" ); -if (sep) { - *sep =0 ; - kvm->cfg.disk_image[kvm->cfg.image_count].tpgt = sep +1 ; - } - cur = sep +1 ; - } - -do { - sep =strstr (cur,"," ); -if (sep) { -if (strncmp (sep +1 ,"ro" ,2 ) ==0 ) - kvm->cfg.disk_image[kvm->cfg.image_count].readonly = true; -else if (strncmp (sep +1 ,"direct" ,6 ) ==0 ) - kvm->cfg.disk_image[kvm->cfg.image_count].direct = true; - *sep =0 ; - cur = sep +1 ; - } - }while (sep); - - kvm->cfg.image_count++; +return disk_img_name_parser (opt, arg, unset); +} -return 0 ; +void kvm_run_set_wrapper_sandbox (void ) +{ + kvm_run_wrapper =KVM_RUN_SANDBOX ; }static int virtio_9p_rootdir_parser (const struct option *opt,const char *arg,int unset)
@@ -406,7 +372,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)return 0 ; } -#define BUILD_OPTIONS (name, cfg) \ +#define BUILD_OPTIONS (name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \ OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \
@@ -420,7 +386,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset) shmem_parser, NULL), \OPT_CALLBACK ('d' ,"disk" , kvm,"image or rootfs_dir" ,"Disk \ image or rootfs directory" , img_name_parser, \ -NULL ), \ + kvm), \OPT_BOOLEAN ('\0' ,"balloon" , &(cfg)->balloon,"Enable virtio \ balloon" ), \ OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
@@ -473,7 +439,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset) "Enable ioport debugging"), \OPT_BOOLEAN ('\0' ,"debug-mmio" , &(cfg)->mmio_debug, \"Enable MMIO debugging" ), \ -OPT_INTEGER ('\0' ,"debug-iodelay" , &debug_iodelay, \ +OPT_INTEGER ('\0' ,"debug-iodelay" , &(cfg)->debug_iodelay, \"Delay IO by millisecond" ), \ OPT_END() \ };
@@ -768,7 +734,7 @@ static const char *find_vmlinux(void)void kvm_run_help (void ) { -BUILD_OPTIONS (options, &kvm->cfg); +BUILD_OPTIONS (options, &kvm->cfg, kvm);usage_with_options (run_usage, options); }
@@ -927,7 +893,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) kvm->cfg.custom_rootfs_name ="default" ;while (argc !=0 ) { -BUILD_OPTIONS (options, &kvm->cfg); +BUILD_OPTIONS (options, &kvm->cfg, kvm); argc =parse_options (argc, argv, options, run_usage,PARSE_OPT_STOP_AT_NON_OPTION |PARSE_OPT_KEEP_DASHDASH );
@@ -969,6 +935,8 @@ static int kvm_cmd_run_init(int argc, const char **argv) } + kvm->nr_disks = kvm->cfg.image_count; +if (!kvm->cfg.kernel_filename) kvm->cfg.kernel_filename =find_kernel ();
@@ -1130,15 +1098,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)strlcat (real_cmdline," root=/dev/vda rw " ,sizeof (real_cmdline)); } -if (kvm->cfg.image_count) { - kvm->nr_disks = kvm->cfg.image_count; - kvm->disks =disk_image__open_all ((struct disk_image_params *)&kvm->cfg.disk_image, kvm->cfg.image_count); -if (IS_ERR (kvm->disks)) { - r =PTR_ERR (kvm->disks); -pr_err ("disk_image__open_all() failed with error %ld\n" , -PTR_ERR (kvm->disks)); - goto fail; - } + r =disk_image__init (kvm); +if (r <0 ) { +pr_err ("disk_image__init() failed with error %d\n" , r); + goto fail; } printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
@@ -1342,9 +1305,9 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_rng__exit() failed with error %d\n" , r); - r =disk_image__close_all (kvm->disks, kvm->cfg.image_count); + r =disk_image__exit (kvm);if (r <0 ) -pr_warning ("disk_image__close_all() failed with error %d\n" , r); +pr_warning ("disk_image__exit() failed with error %d\n" , r); r =serial8250__exit (kvm);if (r <0 )
tools/kvm/disk/core.c +67 -4
122 lines not on this page · raw message
tools/kvm/include/kvm/disk-image.h +5 -4
@@ -3,6 +3,7 @@#include "kvm/read-write.h" #include "kvm/util.h" +#include "kvm/parse-options.h" #include <linux/types.h> #include <linux/fs.h> /* for BLKGETSIZE64 */
@@ -65,13 +66,13 @@ struct disk_image {#endif const char *wwpn;const char *tpgt; +int debug_iodelay; }; -struct disk_image *disk_image__open (const char *filename,bool readonly,bool direct); -struct disk_image **disk_image__open_all (struct disk_image_params *params,int count); +int disk_img_name_parser (const struct option *opt,const char *arg,int unset); +int disk_image__init (struct kvm *kvm); +int disk_image__exit (struct kvm *kvm);struct disk_image *disk_image__new (int fd,u64 size,struct disk_image_operations *ops,int mmap); -int disk_image__close (struct disk_image *disk); -int disk_image__close_all (struct disk_image **disks,int count);int disk_image__flush (struct disk_image *disk);ssize_t disk_image__read (struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount,void *param);
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -23,6 +23,7 @@ struct kvm_config {u8 num_net_devices;bool virtio_rng;int active_console; +int debug_iodelay;const char *kernel_cmdline;const char *kernel_filename;const char *vmlinux_filename;
Signature · 1 line
1.7.12
PATCH 11/33 kvm tools: move nrcpus into struct kvm_config
In reply to Sasha LevinThis no longer has to be a global since we now have kvm_config.
--- tools/kvm/builtin-run.c | 31 +++++++++++++++---------------- tools/kvm/include/kvm/kvm-config.h | 1 + 2 files changed, 16 insertions(+), 16 deletions(-)
tools/kvm/builtin-run.c +15 -16
@@ -61,7 +61,6 @@ static int kvm_run_wrapper;bool do_debug_print = false; -static int nrcpus;static int vidmode =-1 ;extern char _binary_guest_init_start;
@@ -377,7 +376,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)OPT_GROUP ("Basic options:" ), \OPT_STRING ('\0' ,"name" , &(cfg)->guest_name,"guest name" , \"A name for the guest" ), \ -OPT_INTEGER ('c' ,"cpus" , &nrcpus,"Number of CPUs" ), \ +OPT_INTEGER ('c' ,"cpus" , &(cfg)->nrcpus,"Number of CPUs" ), \OPT_U64 ('m' ,"mem" , &(cfg)->ram_size,"Virtual machine memory size\ in MiB." ), \ OPT_CALLBACK('\0', "shmem", NULL, \
@@ -528,7 +527,7 @@ static void handle_debug(int fd, u32 type, u32 len, u8 *msg)if (!(dbg_type &KVM_DEBUG_CMD_TYPE_DUMP ))return ; -for (i =0 ; i < nrcpus; i++) { +for (i =0 ; i < kvm->nrcpus; i++) {struct kvm_cpu *cpu = kvm_cpus[i];if (!cpu)
@@ -947,11 +946,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) kvm->cfg.vmlinux_filename =find_vmlinux (); -if (nrcpus ==0 ) - nrcpus = nr_online_cpus; +if (kvm->cfg.nrcpus ==0 ) + kvm->cfg.nrcpus = nr_online_cpus;if (!kvm->cfg.ram_size) - kvm->cfg.ram_size =get_ram_size (nrcpus); + kvm->cfg.ram_size =get_ram_size (kvm->cfg.nrcpus);if (kvm->cfg.ram_size <MIN_RAM_SIZE_MB )die ("Not enough memory specified: %lluMB (min %lluMB)" , kvm->cfg.ram_size,MIN_RAM_SIZE_MB );
@@ -1015,20 +1014,20 @@ static int kvm_cmd_run_init(int argc, const char **argv) max_cpus =kvm__max_cpus (kvm); recommended_cpus =kvm__recommended_cpus (kvm); -if (nrcpus > max_cpus) { +if (kvm->cfg.nrcpus > max_cpus) {printf (" # Limit the number of CPUs to %d\n" , max_cpus); - nrcpus = max_cpus; - }else if (nrcpus > recommended_cpus) { + kvm->cfg.nrcpus = max_cpus; + }else if (kvm->cfg.nrcpus > recommended_cpus) {printf (" # Warning: The maximum recommended amount of VCPUs" " is %d\n" , recommended_cpus); } - kvm->nrcpus = nrcpus; + kvm->nrcpus = kvm->cfg.nrcpus;/* Alloc one pointer too many, so array ends up 0-terminated */ - kvm_cpus =calloc (nrcpus +1 ,sizeof (void *)); + kvm_cpus =calloc (kvm->nrcpus +1 ,sizeof (void *));if (!kvm_cpus) -die ("Couldn't allocate array for %d CPUs" , nrcpus); +die ("Couldn't allocate array for %d CPUs" , kvm->nrcpus); r =irq__init (kvm);if (r <0 ) {
@@ -1105,7 +1104,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) }printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , - kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , nrcpus, kvm->cfg.guest_name); + kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , kvm->cfg.nrcpus, kvm->cfg.guest_name); if (!kvm->cfg.firmware_filename) { if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename,
@@ -1235,7 +1234,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -for (i =0 ; i < nrcpus; i++) { +for (i =0 ; i < kvm->nrcpus; i++) { kvm_cpus[i] =kvm_cpu__init (kvm, i);if (!kvm_cpus[i])die ("unable to initialize KVM VCPU" );
@@ -1251,7 +1250,7 @@ static int kvm_cmd_run_work(void)int i, r =-1 ;void *ret =NULL ; -for (i =0 ; i < nrcpus; i++) { +for (i =0 ; i < kvm->nrcpus; i++) {if (pthread_create (&kvm_cpus[i]->thread,NULL , kvm_cpu_thread, kvm_cpus[i]) !=0 )die ("unable to create KVM VCPU thread" ); }
@@ -1263,7 +1262,7 @@ static int kvm_cmd_run_work(void)kvm_cpu__delete (kvm_cpus[0 ]); kvm_cpus[0 ] =NULL ; -for (i =1 ; i < nrcpus; i++) { +for (i =1 ; i < kvm->nrcpus; i++) {if (kvm_cpus[i]->is_running) {pthread_kill (kvm_cpus[i]->thread,SIGKVMEXIT );if (pthread_join (kvm_cpus[i]->thread, &ret) !=0 )
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -24,6 +24,7 @@ struct kvm_config {bool virtio_rng;int active_console;int debug_iodelay; +int nrcpus;const char *kernel_cmdline;const char *kernel_filename;const char *vmlinux_filename;
Signature · 1 line
1.7.12
PATCH 12/33 kvm tools: move kvm_cpus into struct kvm
In reply to Sasha LevinThere's no reason the array of guest specific vcpus is global. Move it into struct kvm.
Also split up arch specific vcpu init from the generic code and call it from the kvm_cpu initializer.
--- tools/kvm/Makefile | 2 +- tools/kvm/builtin-run.c | 70 +++++++++------------------------- tools/kvm/hw/i8042.c | 2 +- tools/kvm/hw/vesa.c | 1 + tools/kvm/include/kvm/framebuffer.h | 1 + tools/kvm/include/kvm/kvm-cpu.h | 6 ++- tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm-cpu.c | 75 ++++++++++++++++++++++++++++++++++--- tools/kvm/kvm.c | 6 +-- tools/kvm/powerpc/kvm-cpu.c | 2 +- tools/kvm/term.c | 2 +- tools/kvm/ui/sdl.c | 2 +- tools/kvm/x86/kvm-cpu.c | 2 +- 13 files changed, 104 insertions(+), 68 deletions(-)
tools/kvm/Makefile +1 -1
@@ -243,7 +243,7 @@ DEFINES += -DKVMTOOLS_VERSION='"$(KVMTOOLS_VERSION)"' DEFINES += -DBUILD_ARCH='"$(ARCH)"' KVM_INCLUDE := include -CFLAGS += $(CPPFLAGS) $(DEFINES) -I$(KVM_INCLUDE) -I$(ARCH_INCLUDE) -I$(KINCL_PATH)/include -I$(KINCL_PATH)/arch/$(ARCH)/include/ -O2 -fno-strict-aliasing -g -flto +CFLAGS += $(CPPFLAGS) $(DEFINES) -I$(KVM_INCLUDE) -I$(ARCH_INCLUDE) -I$(KINCL_PATH)/include -I$(KINCL_PATH)/arch/$(ARCH)/include/ -O0 -fno-strict-aliasing -g -flto WARNINGS += -Wall WARNINGS += -Wcast-align
tools/kvm/builtin-run.c +18 -52
144 lines not on this page · raw message
tools/kvm/hw/i8042.c +1 -1
@@ -162,7 +162,7 @@ static void kbd_write_command(struct kvm *kvm, u8 val) state.mode &= ~MODE_DISABLE_AUX ;break ;case I8042_CMD_SYSTEM_RESET : -kvm_cpu__reboot (); +kvm_cpu__reboot (kvm);break ;default :break ;
tools/kvm/hw/vesa.c +1 -0
@@ -80,6 +80,7 @@ struct framebuffer *vesa__init(struct kvm *kvm) .mem = mem, .mem_addr = VESA_MEM_ADDR, .mem_size = VESA_MEM_SIZE, + .kvm = kvm, };return fb__register (&vesafb); }
tools/kvm/include/kvm/framebuffer.h +1 -0
@@ -22,6 +22,7 @@ struct framebuffer {char *mem;u64 mem_addr;u64 mem_size; +struct kvm *kvm;unsigned long nr_targets;struct fb_target_operations *targets[FB_MAX_TARGETS ];
tools/kvm/include/kvm/kvm-cpu.h +4 -2
@@ -4,13 +4,15 @@#include "kvm/kvm-cpu-arch.h" #include <stdbool.h> -struct kvm_cpu *kvm_cpu__init (struct kvm *kvm,unsigned long cpu_id); +int kvm_cpu__init (struct kvm *kvm); +int kvm_cpu__exit (struct kvm *kvm); +struct kvm_cpu *kvm_cpu__arch_init (struct kvm *kvm,unsigned long cpu_id);void kvm_cpu__delete (struct kvm_cpu *vcpu);void kvm_cpu__reset_vcpu (struct kvm_cpu *vcpu);void kvm_cpu__setup_cpuid (struct kvm_cpu *vcpu);void kvm_cpu__enable_singlestep (struct kvm_cpu *vcpu);void kvm_cpu__run (struct kvm_cpu *vcpu); -void kvm_cpu__reboot (void ); +void kvm_cpu__reboot (struct kvm *kvm);int kvm_cpu__start (struct kvm_cpu *cpu);bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu);
tools/kvm/include/kvm/kvm.h +1 -0
@@ -40,6 +40,7 @@ struct kvm {timer_t timerid;/* Posix timer for interrupts */ int nrcpus;/* Number of cpus to run */ +struct kvm_cpu **cpus;u32 mem_slots;/* for KVM_SET_USER_MEMORY_REGION */ u64 ram_size;
tools/kvm/kvm-cpu.c +70 -5
94 lines not on this page · raw message
tools/kvm/kvm.c +3 -3
@@ -536,7 +536,7 @@ void kvm__pause(void)int i, paused_vcpus =0 ;/* Check if the guest is running */ -if (!kvm_cpus[0 ] || kvm_cpus[0 ]->thread ==0 ) +if (!kvm->cpus[0 ] || kvm->cpus[0 ]->thread ==0 )return ;mutex_lock (&pause_lock);
@@ -545,7 +545,7 @@ void kvm__pause(void)if (pause_event <0 )die ("Failed creating pause notification event" );for (i =0 ; i < kvm->nrcpus; i++) -pthread_kill (kvm_cpus[i]->thread,SIGKVMPAUSE ); +pthread_kill (kvm->cpus[i]->thread,SIGKVMPAUSE );while (paused_vcpus < kvm->nrcpus) {u64 cur_read;
@@ -560,7 +560,7 @@ void kvm__pause(void)void kvm__continue (void ) {/* Check if the guest is running */ -if (!kvm_cpus[0 ] || kvm_cpus[0 ]->thread ==0 ) +if (!kvm->cpus[0 ] || kvm->cpus[0 ]->thread ==0 )return ;mutex_unlock (&pause_lock);
tools/kvm/powerpc/kvm-cpu.c +1 -1
@@ -57,7 +57,7 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)free (vcpu); } -struct kvm_cpu *kvm_cpu__init (struct kvm *kvm,unsigned long cpu_id) +struct kvm_cpu *kvm_cpu__arch_init (struct kvm *kvm,unsigned long cpu_id) {struct kvm_cpu *vcpu;int mmap_size;
tools/kvm/term.c +1 -1
@@ -35,7 +35,7 @@ int term_getc(int term)if (term_got_escape) { term_got_escape = false;if (c =='x' ) -kvm_cpu__reboot (); +kvm_cpu__reboot (kvm);if (c == term_escape_char)return c; }
tools/kvm/ui/sdl.c +1 -1
@@ -261,7 +261,7 @@ static void *sdl__thread(void *p)return NULL ; } exit: -kvm_cpu__reboot (); +kvm_cpu__reboot (fb->kvm);return NULL ; }
tools/kvm/x86/kvm-cpu.c +1 -1
@@ -90,7 +90,7 @@ static int kvm_cpu__set_lint(struct kvm_cpu *vcpu)return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &lapic); } -struct kvm_cpu *kvm_cpu__init (struct kvm *kvm,unsigned long cpu_id) +struct kvm_cpu *kvm_cpu__arch_init (struct kvm *kvm,unsigned long cpu_id) {struct kvm_cpu *vcpu;int mmap_size;
Signature · 1 line
1.7.12
PATCH 13/33 kvm tools: improve framebuffer manager initialization
In reply to Sasha LevinMake the init and exit functions of the framebuffer similar to the rest of the code.
--- tools/kvm/builtin-run.c | 6 ++++-- tools/kvm/framebuffer.c | 6 ++++-- tools/kvm/include/kvm/framebuffer.h | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-)
tools/kvm/builtin-run.c +4 -2
@@ -1197,7 +1197,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } - r =fb__start (); + r =fb__init (kvm);if (r <0 ) {pr_err ("fb__init() failed with error %d\n" , r); goto fail;
@@ -1258,7 +1258,9 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("irq__exit() failed with error %d\n" , r); -fb__stop (); + r =fb__exit (kvm); +if (r <0 ) +pr_warning ("fb__exit() failed with error %d\n" , r); r =virtio_scsi_exit (kvm);if (r <0 )
tools/kvm/framebuffer.c +4 -2
@@ -44,7 +44,7 @@ static int start_targets(struct framebuffer *fb)return 0 ; } -int fb__start (void ) +int fb__init (struct kvm *kvm) {struct framebuffer *fb;
@@ -59,7 +59,7 @@ int fb__start(void)return 0 ; } -void fb__stop (void ) +int fb__exit (struct kvm *kvm) {struct framebuffer *fb;
@@ -72,4 +72,6 @@ void fb__stop(void)munmap (fb->mem, fb->mem_size); } + +return 0 ; }
tools/kvm/include/kvm/framebuffer.h +2 -2
@@ -30,7 +30,7 @@ struct framebuffer {struct framebuffer *fb__register (struct framebuffer *fb);int fb__attach (struct framebuffer *fb,struct fb_target_operations *ops); -int fb__start (void ); -void fb__stop (void ); +int fb__init (struct kvm *kvm); +int fb__exit (struct kvm *kvm);#endif /* KVM__FRAMEBUFFER_H */
Signature · 1 line
1.7.12
PATCH 14/33 kvm tools: improve term init/exit functions
In reply to Sasha LevinMake the init and exit functions of the term code similar to the rest of the code.
Also move in the pty parser into the term code out of builtin-run.
--- tools/kvm/builtin-run.c | 19 +++++++++---------- tools/kvm/include/kvm/term.h | 6 +++++- tools/kvm/term.c | 28 ++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-)
tools/kvm/builtin-run.c +9 -10
@@ -146,15 +146,6 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, ireturn 0 ; } -static int tty_parser (const struct option *opt,const char *arg,int unset) -{ -int tty =atoi (arg); - -term_set_tty (tty); - -return 0 ; -} -static inline void str_to_mac (const char *str,char *mac) { sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
@@ -988,7 +979,11 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.script) kvm->cfg.script =DEFAULT_SCRIPT ; -term_init (); + r =term_init (kvm); +if (r <0 ) { +pr_err ("term_init() failed with error %d\n" , r); + goto fail; + }if (!kvm->cfg.guest_name) {if (kvm->cfg.custom_rootfs) {
@@ -1302,6 +1297,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("pci__exit() failed with error %d\n" , r); + r =term_exit (kvm); +if (r <0 ) +pr_warning ("pci__exit() failed with error %d\n" , r); + r =kvm__exit (kvm);if (r <0 )pr_warning ("pci__exit() failed with error %d\n" , r);
tools/kvm/include/kvm/term.h +5 -1
@@ -1,6 +1,8 @@#ifndef KVM__TERM_H #define KVM__TERM_H +#include "kvm/kvm.h" +#include <sys/uio.h> #include <stdbool.h>
@@ -15,6 +17,8 @@ int term_getc(int term);bool term_readable (int term);void term_set_tty (int term); -void term_init (void ); +int term_init (struct kvm *kvm); +int term_exit (struct kvm *kvm); +int tty_parser (const struct option *opt,const char *arg,int unset);#endif /* KVM__TERM_H */
tools/kvm/term.c +24 -4
@@ -127,13 +127,26 @@ void term_set_tty(int term) term_fds[term][TERM_FD_IN ] = term_fds[term][TERM_FD_OUT ] = master; } -void term_init (void ) +int tty_parser (const struct option *opt,const char *arg,int unset) +{ +int tty =atoi (arg); + +term_set_tty (tty); + +return 0 ; +} + +int term_init (struct kvm *kvm) {struct termios term; -int i; +int i, r; + + r =tcgetattr (STDIN_FILENO , &orig_term); +if (r <0 ) { +pr_warning ("unable to save initial standard input settings" ); +return r; + } -if (tcgetattr (STDIN_FILENO , &orig_term) <0 ) -die ("unable to save initial standard input settings" ); term = orig_term; term.c_lflag &= ~(ICANON |ECHO |ISIG );
@@ -147,4 +160,11 @@ void term_init(void)signal (SIGTERM , term_sig_cleanup);atexit (term_cleanup); + +return 0 ; +} + +int term_exit (struct kvm *kvm) +{ +return 0 ; }
Signature · 1 line
1.7.12
PATCH 15/33 kvm tools: threadpool exit routine
In reply to Sasha LevinAdd an exit function for the threadpool which will stop all running threads in the pool. Also clean up the init code a bit.
--- tools/kvm/builtin-run.c | 11 ++++++++++- tools/kvm/include/kvm/threadpool.h | 3 ++- tools/kvm/util/threadpool.c | 33 +++++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-)
tools/kvm/builtin-run.c +10 -1
@@ -1216,7 +1216,12 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } -thread_pool__init (nr_online_cpus); + r =thread_pool__init (kvm); +if (r <0 ) { +pr_err ("thread_pool__init() failed with error %d\n" , r); + goto fail; + } + fail:return r; }
@@ -1301,6 +1306,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("pci__exit() failed with error %d\n" , r); + r =thread_pool__exit (kvm); +if (r <0 ) +pr_warning ("thread_pool__exit() failed with error %d\n" , r); + r =kvm__exit (kvm);if (r <0 )pr_warning ("pci__exit() failed with error %d\n" , r);
tools/kvm/include/kvm/threadpool.h +2 -1
@@ -30,7 +30,8 @@ static inline void thread_pool__init_job(struct thread_pool__job *job, struct kv }; } -int thread_pool__init (unsigned long thread_count); +int thread_pool__init (struct kvm *kvm); +int thread_pool__exit (struct kvm *kvm);void thread_pool__do_job (struct thread_pool__job *job);
tools/kvm/util/threadpool.c +29 -4
@@ -14,6 +14,7 @@ static LIST_HEAD(head);static pthread_t *threads;static long threadcount; +static bool running;static struct thread_pool__job *thread_pool__job_pop_locked (void ) {
@@ -76,15 +77,16 @@ static void *thread_pool__threadfunc(void *param) {pthread_cleanup_push (thread_pool__threadfunc_cleanup,NULL ); -for (;;) { +while (running) {struct thread_pool__job *curjob;mutex_lock (&job_mutex); -while ((curjob =thread_pool__job_pop_locked ()) ==NULL ) +while (running && (curjob =thread_pool__job_pop_locked ()) ==NULL )pthread_cond_wait (&job_cond, &job_mutex);mutex_unlock (&job_mutex); -thread_pool__handle_job (curjob); +if (running) +thread_pool__handle_job (curjob); }pthread_cleanup_pop (0 );
@@ -116,9 +118,12 @@ static int thread_pool__addthread(void)return res; } -int thread_pool__init (unsigned long thread_count) +int thread_pool__init (struct kvm *kvm) {unsigned long i; +unsigned int thread_count =sysconf (_SC_NPROCESSORS_ONLN); + + running = true;for (i =0 ; i < thread_count; i++)if (thread_pool__addthread () <0 )
@@ -127,6 +132,26 @@ int thread_pool__init(unsigned long thread_count)return i; } +int thread_pool__exit (struct kvm *kvm) +{ +int i; +void *NUL =NULL ; + + running = false; + +for (i =0 ; i < threadcount; i++) { +mutex_lock (&job_mutex); +pthread_cond_signal (&job_cond); +mutex_unlock (&job_mutex); + } + +for (i =0 ; i < threadcount; i++) { +pthread_join (threads[i],NUL ); + } + +return 0 ; +} +void thread_pool__do_job (struct thread_pool__job *job) {struct thread_pool__job *jobinfo = job;
Signature · 1 line
1.7.12
PATCH 16/33 kvm tools: timer cleanup
In reply to Sasha LevinMake the timer init/exit follow the rest of the code, and move it out of builtin-run.
--- tools/kvm/builtin-run.c | 12 ++++++++++-- tools/kvm/include/kvm/kvm.h | 4 ++-- tools/kvm/kvm.c | 23 +++++++++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-)
tools/kvm/builtin-run.c +10 -2
@@ -1203,7 +1203,11 @@ static int kvm_cmd_run_init(int argc, const char **argv)* come after this (it may set up device trees etc.) */ -kvm__start_timer (kvm); + r =kvm_timer__init (kvm); +if (r <0 ) { +pr_err ("kvm_timer__init() failed with error %d\n" , r); + goto fail; + }if (kvm->cfg.firmware_filename) {if (!kvm__load_firmware (kvm, kvm->cfg.firmware_filename))
@@ -1258,9 +1262,13 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("irq__exit() failed with error %d\n" , r); + r =kvm_timer__exit (kvm); +if (r <0 ) +pr_warning ("kvm_timer__exit() failed with error %d\n" , r); + r =fb__exit (kvm);if (r <0 ) -pr_warning ("fb__exit() failed with error %d\n" , r); +pr_warning ("kvm_timer__exit() failed with error %d\n" , r); r =virtio_scsi_exit (kvm);if (r <0 )
tools/kvm/include/kvm/kvm.h +2 -2
@@ -68,8 +68,8 @@ int kvm__exit(struct kvm *kvm);bool kvm__load_firmware (struct kvm *kvm,const char *firmware_filename);bool kvm__load_kernel (struct kvm *kvm,const char *kernel_filename,const char *initrd_filename,const char *kernel_cmdline,u16 vidmode); -void kvm__start_timer (struct kvm *kvm); -void kvm__stop_timer (struct kvm *kvm); +int kvm_timer__init (struct kvm *kvm); +int kvm_timer__exit (struct kvm *kvm);void kvm__irq_line (struct kvm *kvm,int irq,int level);void kvm__irq_trigger (struct kvm *kvm,int irq);bool kvm__emulate_io (struct kvm *kvm,u16 port,void *data,int direction,int size,u32 count);
tools/kvm/kvm.c +15 -8
@@ -254,8 +254,6 @@ int kvm__enumerate_instances(int (*callback)(const char *name, int fd))int kvm__exit (struct kvm *kvm) { -kvm__stop_timer (kvm); -kvm__arch_delete_ram (kvm);kvm_ipc__stop ();kvm__remove_socket (kvm->cfg.guest_name);
@@ -478,10 +476,11 @@ found_kernel:* userspace hypervisor into the guest at periodical intervals. Please note * that clock interrupt, for example, is not handled here. */ -void kvm__start_timer (struct kvm *kvm) +int kvm_timer__init (struct kvm *kvm) {struct itimerspec its;struct sigevent sev; +int r;memset (&sev,0 ,sizeof (struct sigevent )); sev.sigev_value.sival_int =0 ;
@@ -489,25 +488,33 @@ void kvm__start_timer(struct kvm *kvm) sev.sigev_signo =SIGALRM ; sev._sigev_un._tid =syscall (__NR_gettid); -if (timer_create (CLOCK_REALTIME , &sev, &kvm->timerid) <0 ) -die ("timer_create()" ); + r =timer_create (CLOCK_REALTIME , &sev, &kvm->timerid); +if (r <0 ) +return r; its.it_value.tv_sec =TIMER_INTERVAL_NS /1000000000 ; its.it_value.tv_nsec =TIMER_INTERVAL_NS %1000000000 ; its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; -if (timer_settime (kvm->timerid,0 , &its,NULL ) <0 ) -die ("timer_settime()" ); + r =timer_settime (kvm->timerid,0 , &its,NULL ); +if (r <0 ) { +timer_delete (kvm->timerid); +return r; + } + +return 0 ; } -void kvm__stop_timer (struct kvm *kvm) +int kvm_timer__exit (struct kvm *kvm) {if (kvm->timerid)if (timer_delete (kvm->timerid) <0 )die ("timer_delete()" ); kvm->timerid =0 ; + +return 0 ; }void kvm__dump_mem (struct kvm *kvm,unsigned long addr,unsigned long size)
Signature · 1 line
1.7.12
PATCH 17/33 kvm tools: virtio-console init/exit
In reply to Sasha LevinMake the init/exit of virtio-console self-contained, so the global init code won't need to check if it was selected or not.
--- tools/kvm/builtin-run.c | 12 +++++++++--- tools/kvm/include/kvm/virtio-console.h | 3 ++- tools/kvm/virtio/console.c | 12 +++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-)
tools/kvm/builtin-run.c +9 -3
@@ -1124,9 +1124,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } - -if (kvm->cfg.active_console ==CONSOLE_VIRTIO ) -virtio_console__init (kvm); + r =virtio_console__init (kvm); +if (r <0 ) { +pr_err ("virtio_console__init() failed with error %d\n" , r); + goto fail; + }if (kvm->cfg.virtio_rng)virtio_rng__init (kvm);
@@ -1282,6 +1284,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_rng__exit() failed with error %d\n" , r); + r =virtio_console__exit (kvm); +if (r <0 ) +pr_warning ("virtio_console__exit() failed with error %d\n" , r); + r =disk_image__exit (kvm);if (r <0 )pr_warning ("disk_image__exit() failed with error %d\n" , r);
tools/kvm/include/kvm/virtio-console.h +2 -1
@@ -3,7 +3,8 @@struct kvm ; -void virtio_console__init (struct kvm *kvm); +int virtio_console__init (struct kvm *kvm);void virtio_console__inject_interrupt (struct kvm *kvm); +int virtio_console__exit (struct kvm *kvm);#endif /* KVM__CONSOLE_VIRTIO_H */
tools/kvm/virtio/console.c +11 -1
@@ -182,10 +182,20 @@ static struct virtio_ops con_dev_virtio_ops = (struct virtio_ops) { .get_size_vq = get_size_vq, }; -void virtio_console__init (struct kvm *kvm) +int virtio_console__init (struct kvm *kvm) { +if (kvm->cfg.active_console !=CONSOLE_VIRTIO ) +return 0 ; +virtio_init (kvm, &cdev, &cdev.vdev, &con_dev_virtio_ops,VIRTIO_PCI ,PCI_DEVICE_ID_VIRTIO_CONSOLE ,VIRTIO_ID_CONSOLE ,PCI_CLASS_CONSOLE );if (compat_id ==-1 ) compat_id =virtio_compat_add_message ("virtio-console" ,"CONFIG_VIRTIO_CONSOLE" ); + +return 0 ; +} + +int virtio_console__exit (struct kvm *kvm) +{ +return 0 ; }
Signature · 1 line
1.7.12
PATCH 18/33 kvm tools: virtio-rng init/exit
In reply to Sasha LevinMake the init/exit of virtio-rng self-contained, so the global init code won't need to check if it was selected or not.
--- tools/kvm/builtin-run.c | 7 +++++-- tools/kvm/virtio/rng.c | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-)
tools/kvm/builtin-run.c +5 -2
@@ -1130,8 +1130,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -if (kvm->cfg.virtio_rng) -virtio_rng__init (kvm); + r =virtio_rng__init (kvm); +if (r <0 ) { +pr_err ("virtio_rng__init() failed with error %d\n" , r); + goto fail; + }if (kvm->cfg.balloon)virtio_bln__init (kvm);
tools/kvm/virtio/rng.c +3 -0
@@ -147,6 +147,9 @@ int virtio_rng__init(struct kvm *kvm)struct rng_dev *rdev;int r; +if (!kvm->cfg.virtio_rng) +return 0 ; + rdev =malloc (sizeof (*rdev));if (rdev ==NULL )return -ENOMEM ;
Signature · 1 line
1.7.12
PATCH 19/33 kvm tools: virtio-bln init/exit
In reply to Sasha LevinMake the init/exit of virtio-balloon self-contained, so the global init code won't need to check if it was selected or not.
--- tools/kvm/builtin-run.c | 11 +++++++++-- tools/kvm/include/kvm/virtio-balloon.h | 3 ++- tools/kvm/virtio/balloon.c | 12 +++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-)
tools/kvm/builtin-run.c +9 -2
@@ -1136,8 +1136,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -if (kvm->cfg.balloon) -virtio_bln__init (kvm); + r =virtio_bln__init (kvm); +if (r <0 ) { +pr_err ("virtio_rng__init() failed with error %d\n" , r); + goto fail; + }if (!kvm->cfg.network) kvm->cfg.network =DEFAULT_NETWORK ;
@@ -1287,6 +1290,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_rng__exit() failed with error %d\n" , r); + r =virtio_bln__exit (kvm); +if (r <0 ) +pr_warning ("virtio_bln__exit() failed with error %d\n" , r); + r =virtio_console__exit (kvm);if (r <0 )pr_warning ("virtio_console__exit() failed with error %d\n" , r);
tools/kvm/include/kvm/virtio-balloon.h +2 -1
@@ -3,6 +3,7 @@struct kvm ; -void virtio_bln__init (struct kvm *kvm); +int virtio_bln__init (struct kvm *kvm); +int virtio_bln__exit (struct kvm *kvm);#endif /* KVM__BLN_VIRTIO_H */
tools/kvm/virtio/balloon.c +11 -1
@@ -243,8 +243,11 @@ struct virtio_ops bln_dev_virtio_ops = (struct virtio_ops) { .get_size_vq = get_size_vq, }; -void virtio_bln__init (struct kvm *kvm) +int virtio_bln__init (struct kvm *kvm) { +if (!kvm->cfg.balloon) +return 0 ; +kvm_ipc__register_handler (KVM_IPC_BALLOON , handle_mem);kvm_ipc__register_handler (KVM_IPC_STAT , virtio_bln__print_stats);
@@ -256,4 +259,11 @@ void virtio_bln__init(struct kvm *kvm)if (compat_id ==-1 ) compat_id =virtio_compat_add_message ("virtio-balloon" ,"CONFIG_VIRTIO_BALLOON" ); + +return 0 ; +} + +int virtio_bln__exit (struct kvm *kvm) +{ +return 0 ; }
Signature · 1 line
1.7.12
PATCH 20/33 kvm tools: pci-shmem init-exit
In reply to Sasha LevinMake the init/exit of pci-shmem self-contained, so the global init code won't need to check if it was selected or not.
Also move the parser out of builtin-run into the pci-shmem code.
--- tools/kvm/builtin-run.c | 134 +++-------------------------------- tools/kvm/hw/pci-shmem.c | 143 ++++++++++++++++++++++++++++++++++++-- tools/kvm/include/kvm/pci-shmem.h | 6 +- 3 files changed, 153 insertions(+), 130 deletions(-)
tools/kvm/builtin-run.c +9 -125
@@ -237,130 +237,6 @@ done:return 0 ; } -static int shmem_parser (const struct option *opt,const char *arg,int unset) -{ -const u64 default_size =SHMEM_DEFAULT_SIZE ; -const u64 default_phys_addr =SHMEM_DEFAULT_ADDR ; -const char *default_handle =SHMEM_DEFAULT_HANDLE ; -struct shmem_info *si =malloc (sizeof (struct shmem_info )); -u64 phys_addr; -u64 size; -char *handle =NULL ; -int create =0 ; -const char *p = arg; -char *next; -int base =10 ; -int verbose =0 ; - -const int skip_pci =strlen ("pci:" ); -if (verbose) -pr_info ("shmem_parser(%p,%s,%d)" , opt, arg, unset); -/* parse out optional addr family */ -if (strcasestr (p,"pci:" )) { - p += skip_pci; - }else if (strcasestr (p,"mem:" )) { -die ("I can't add to E820 map yet.\n" ); - } -/* parse out physical addr */ - base =10 ; -if (strcasestr (p,"0x" )) - base =16 ; - phys_addr =strtoll (p, &next, base); -if (next == p && phys_addr ==0 ) { -pr_info ("shmem: no physical addr specified, using default." ); - phys_addr = default_phys_addr; - } -if (*next !=':' && *next !='\0' ) -die ("shmem: unexpected chars after phys addr.\n" ); -if (*next =='\0' ) - p = next; -else - p = next +1 ; -/* parse out size */ - base =10 ; -if (strcasestr (p,"0x" )) - base =16 ; - size =strtoll (p, &next, base); -if (next == p && size ==0 ) { -pr_info ("shmem: no size specified, using default." ); - size = default_size; - } -/* look for [KMGkmg][Bb]* uses base 2. */ -int skip_B =0 ; -if (strspn (next,"KMGkmg" )) {/* might have a prefix */ -if (*(next +1 ) =='B' || *(next +1 ) =='b' ) - skip_B =1 ; -switch (*next) { -case 'K' : -case 'k' : - size = size <<KB_SHIFT ; -break ; -case 'M' : -case 'm' : - size = size <<MB_SHIFT ; -break ; -case 'G' : -case 'g' : - size = size <<GB_SHIFT ; -break ; -default : -die ("shmem: bug in detecting size prefix." ); -break ; - } - next +=1 + skip_B; - } -if (*next !=':' && *next !='\0' ) { -die ("shmem: unexpected chars after phys size. <%c><%c>\n" , - *next, *p); - } -if (*next =='\0' ) - p = next; -else - p = next +1 ; -/* parse out optional shmem handle */ -const int skip_handle =strlen ("handle=" ); - next =strcasestr (p,"handle=" ); -if (*p && next) { -if (p != next) -die ("unexpected chars before handle\n" ); - p += skip_handle; - next =strchrnul (p,':' ); -if (next - p) { - handle =malloc (next - p +1 ); -strncpy (handle, p, next - p); - handle[next - p] ='\0' ;/* just in case. */ - } -if (*next =='\0' ) - p = next; -else - p = next +1 ; - } -/* parse optional create flag to see if we should create shm seg. */ -if (*p &&strcasestr (p,"create" )) { - create =1 ; - p +=strlen ("create" ); - } -if (*p !='\0' ) -die ("shmem: unexpected trailing chars\n" ); -if (handle ==NULL ) { - handle =malloc (strlen (default_handle) +1 ); -strcpy (handle, default_handle); - } -if (verbose) { -pr_info ("shmem: phys_addr = %llx" , phys_addr); -pr_info ("shmem: size = %llx" , size); -pr_info ("shmem: handle = %s" , handle); -pr_info ("shmem: create = %d" , create); - } - - si->phys_addr = phys_addr; - si->size = size; - si->handle = handle; - si->create = create; -pci_shmem__register_mem (si);/* ownership of si, etc. passed on. */ -return 0 ; -} -#define BUILD_OPTIONS (name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \
@@ -1174,7 +1050,11 @@ static int kvm_cmd_run_init(int argc, const char **argv)kbd__init (kvm);#endif -pci_shmem__init (kvm); + r =pci_shmem__init (kvm); +if (r <0 ) { +pr_err ("pci_shmem__init() failed with error %d\n" , r); + goto fail; + }if (kvm->cfg.vnc || kvm->cfg.sdl) { fb =vesa__init (kvm);
@@ -1298,6 +1178,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("virtio_console__exit() failed with error %d\n" , r); + r =pci_shmem__exit (kvm); +if (r <0 ) +pr_warning ("pci_shmem__exit() failed with error %d\n" , r); + r =disk_image__exit (kvm);if (r <0 )pr_warning ("disk_image__exit() failed with error %d\n" , r);
tools/kvm/hw/pci-shmem.c +139 -4
167 lines not on this page · raw message
tools/kvm/include/kvm/pci-shmem.h +5 -1
@@ -4,6 +4,8 @@#include <linux/types.h> #include <linux/list.h> +#include "kvm/parse-options.h" +#define SHMEM_DEFAULT_SIZE (16 << MB_SHIFT)#define SHMEM_DEFAULT_ADDR (0xc8000000)#define SHMEM_DEFAULT_HANDLE "/kvm_shmem"
@@ -18,8 +20,10 @@ struct shmem_info {int create; }; -int pci_shmem__init (struct kvm *self); +int pci_shmem__init (struct kvm *kvm); +int pci_shmem__exit (struct kvm *kvm);int pci_shmem__register_mem (struct shmem_info *si); +int shmem_parser (const struct option *opt,const char *arg,int unset);int pci_shmem__get_local_irqfd (struct kvm *kvm);int pci_shmem__add_client (struct kvm *kvm,u32 id,int fd);
Signature · 1 line
1.7.12
PATCH 21/33 kvm tools: virtio-net init/exit
In reply to Sasha LevinMake the init/exit of virtio-net self-contained, so the global init code won't need to check if it was selected or not.
This also moves the bulk of the net-specific initialization code, including the parser, into virtio-net itself.
--- tools/kvm/builtin-run.c | 122 +++------------------------------ tools/kvm/include/kvm/virtio-net.h | 6 +- tools/kvm/virtio/net.c | 134 +++++++++++++++++++++++++++++++++++-- 3 files changed, 144 insertions(+), 118 deletions(-)
tools/kvm/builtin-run.c +10 -112
@@ -146,97 +146,6 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, ireturn 0 ; } -static inline void str_to_mac (const char *str,char *mac) -{ -sscanf (str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx" , - mac, mac+1 , mac+2 , mac+3 , mac+4 , mac+5 ); -} -static int set_net_param (struct virtio_net_params *p,const char *param, -const char *val) -{ -if (strcmp (param,"guest_mac" ) ==0 ) { -str_to_mac (val, p->guest_mac); - }else if (strcmp (param,"mode" ) ==0 ) { -if (!strncmp (val,"user" ,4 )) { -int i; - -for (i =0 ; i < kvm->cfg.num_net_devices; i++) -if (kvm->cfg.net_params[i].mode ==NET_MODE_USER ) -die ("Only one usermode network device allowed at a time" ); - p->mode =NET_MODE_USER ; - }else if (!strncmp (val,"tap" ,3 )) { - p->mode =NET_MODE_TAP ; - }else if (!strncmp (val,"none" ,4 )) { - kvm->cfg.no_net =1 ; -return -1 ; - }else -die ("Unknown network mode %s, please use user, tap or none" , kvm->cfg.network); - }else if (strcmp (param,"script" ) ==0 ) { - p->script =strdup (val); - }else if (strcmp (param,"guest_ip" ) ==0 ) { - p->guest_ip =strdup (val); - }else if (strcmp (param,"host_ip" ) ==0 ) { - p->host_ip =strdup (val); - }else if (strcmp (param,"trans" ) ==0 ) { - p->trans =strdup (val); - }else if (strcmp (param,"vhost" ) ==0 ) { - p->vhost =atoi (val); - }else if (strcmp (param,"fd" ) ==0 ) { - p->fd =atoi (val); - }else -die ("Unknown network parameter %s" , param); - -return 0 ; -} - -static int netdev_parser (const struct option *opt,const char *arg,int unset) -{ -struct virtio_net_params p; -char *buf =NULL , *cmd =NULL , *cur =NULL ; -bool on_cmd = true; - -if (arg) { - buf =strdup (arg); -if (buf ==NULL ) -die ("Failed allocating new net buffer" ); - cur =strtok (buf,",=" ); - } - - p = (struct virtio_net_params ) { - .guest_ip =DEFAULT_GUEST_ADDR , - .host_ip =DEFAULT_HOST_ADDR , - .script =DEFAULT_SCRIPT , - .mode =NET_MODE_TAP , - }; - -str_to_mac (DEFAULT_GUEST_MAC , p.guest_mac); - p.guest_mac[5 ] += kvm->cfg.num_net_devices; - -while (cur) { -if (on_cmd) { - cmd = cur; - }else { -if (set_net_param (&p, cmd, cur) <0 ) - goto done; - } - on_cmd = !on_cmd; - - cur =strtok (NULL ,",=" ); - }; - - kvm->cfg.num_net_devices++; - - kvm->cfg.net_params =realloc (kvm->cfg.net_params, kvm->cfg.num_net_devices *sizeof (*kvm->cfg.net_params)); -if (kvm->cfg.net_params ==NULL ) -die ("Failed adding new network device" ); - - kvm->cfg.net_params[kvm->cfg.num_net_devices -1 ] = p; - -done: -free (buf); -return 0 ; -} -#define BUILD_OPTIONS (name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \
@@ -287,7 +196,7 @@ done:OPT_GROUP ("Networking options:" ), \OPT_CALLBACK_DEFAULT ('n' ,"network" ,NULL ,"network params" , \"Create a new guest NIC" , \ - netdev_parser,NULL ,NULL ), \ + netdev_parser,NULL , kvm), \OPT_BOOLEAN ('\0' ,"no-dhcp" , &(cfg)->no_dhcp,"Disable kernel DHCP\ in rootfs mode"), \ \
@@ -739,7 +648,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)static char real_cmdline[2048 ], default_name[20 ];struct framebuffer *fb =NULL ;unsigned int nr_online_cpus; -int i, r; +int r; kvm =kvm__new ();if (IS_ERR (kvm))
@@ -1023,25 +932,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)virtio_9p__init (kvm); -for (i =0 ; i < kvm->cfg.num_net_devices; i++) { - kvm->cfg.net_params[i].kvm = kvm; -virtio_net__init (&kvm->cfg.net_params[i]); - } - -if (kvm->cfg.num_net_devices ==0 && kvm->cfg.no_net ==0 ) { -struct virtio_net_params net_params; - - net_params = (struct virtio_net_params ) { - .guest_ip = kvm->cfg.guest_ip, - .host_ip = kvm->cfg.host_ip, - .kvm = kvm, - .script = kvm->cfg.script, - .mode =NET_MODE_USER , - }; -str_to_mac (kvm->cfg.guest_mac, net_params.guest_mac); -str_to_mac (kvm->cfg.host_mac, net_params.host_mac); - -virtio_net__init (&net_params); + r =virtio_net__init (kvm); +if (r <0 ) { +pr_err ("virtio_net__init() failed with error %d\n" , r); + goto fail; }kvm__init_ram (kvm);
@@ -1158,6 +1052,10 @@ static void kvm_cmd_run_exit(int guest_ret)if (r <0 )pr_warning ("kvm_timer__exit() failed with error %d\n" , r); + r =virtio_net__exit (kvm); +if (r <0 ) +pr_warning ("virtio_net__exit() failed with error %d\n" , r); + r =virtio_scsi_exit (kvm);if (r <0 )pr_warning ("virtio_scsi_exit() failed with error %d\n" , r);
tools/kvm/include/kvm/virtio-net.h +5 -1
@@ -1,6 +1,8 @@#ifndef KVM__VIRTIO_NET_H #define KVM__VIRTIO_NET_H +#include "kvm/parse-options.h" +struct kvm ; struct virtio_net_params {
@@ -16,7 +18,9 @@ struct virtio_net_params {int fd; }; -void virtio_net__init (const struct virtio_net_params *params); +int virtio_net__init (struct kvm *kvm); +int virtio_net__exit (struct kvm *kvm); +int netdev_parser (const struct option *opt,const char *arg,int unset);enum {NET_MODE_USER ,
tools/kvm/virtio/net.c +129 -5
150 lines not on this page · raw message
Signature · 1 line
1.7.12
PATCH 22/33 kvm tools: kvm-ipc cleanup
In reply to Sasha LevinMove all the kvm-ipc specific code into the relevant file, and modify the ipc callback to pass a ptr to struct kvm as well.
--- tools/kvm/builtin-run.c | 122 ---------------- tools/kvm/include/kvm/kvm-ipc.h | 8 +- tools/kvm/kvm-ipc.c | 299 +++++++++++++++++++++++++++++++++++++--- tools/kvm/kvm.c | 147 -------------------- tools/kvm/virtio/balloon.c | 4 +- 5 files changed, 289 insertions(+), 291 deletions(-)
tools/kvm/builtin-run.c +0 -122
@@ -218,127 +218,11 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i OPT_END() \ }; -/* -* Serialize debug printout so that the output of multiple vcpus does not -* get mixed up: -*/ -static int printout_done; - -static void handle_sigusr1 (int sig) -{ -struct kvm_cpu *cpu = current_kvm_cpu; -int fd =kvm_cpu__get_debug_fd (); - -if (!cpu || cpu->needs_nmi) -return ; - -dprintf (fd,"\n #\n # vCPU #%ld's dump:\n #\n" , cpu->cpu_id); -kvm_cpu__show_registers (cpu); -kvm_cpu__show_code (cpu); -kvm_cpu__show_page_tables (cpu); -fflush (stdout); - printout_done =1 ; -mb (); -} - -/* Pause/resume the guest using SIGUSR2 */ -static int is_paused; - -static void handle_pause (int fd,u32 type,u32 len,u8 *msg) -{ -if (WARN_ON (len)) -return ; - -if (type ==KVM_IPC_RESUME && is_paused) { - kvm->vm_state =KVM_VMSTATE_RUNNING ; -kvm__continue (); - }else if (type ==KVM_IPC_PAUSE && !is_paused) { - kvm->vm_state =KVM_VMSTATE_PAUSED ; -ioctl (kvm->vm_fd,KVM_KVMCLOCK_CTRL ); -kvm__pause (); - }else { -return ; - } - - is_paused = !is_paused; -} - -static void handle_vmstate (int fd,u32 type,u32 len,u8 *msg) -{ -int r =0 ; - -if (type ==KVM_IPC_VMSTATE ) - r =write (fd, &kvm->vm_state,sizeof (kvm->vm_state)); - -if (r <0 ) -pr_warning ("Failed sending VMSTATE" ); -} - -static void handle_debug (int fd,u32 type,u32 len,u8 *msg) -{ -int i; -struct debug_cmd_params *params; -u32 dbg_type; -u32 vcpu; - -if (WARN_ON (type !=KVM_IPC_DEBUG || len !=sizeof (*params))) -return ; - - params = (void *)msg; - dbg_type = params->dbg_type; - vcpu = params->cpu; - -if (dbg_type &KVM_DEBUG_CMD_TYPE_SYSRQ ) -serial8250__inject_sysrq (kvm, params->sysrq); - -if (dbg_type &KVM_DEBUG_CMD_TYPE_NMI ) { -if ((int )vcpu >= kvm->nrcpus) -return ; - - kvm->cpus[vcpu]->needs_nmi =1 ; -pthread_kill (kvm->cpus[vcpu]->thread,SIGUSR1 ); - } - -if (!(dbg_type &KVM_DEBUG_CMD_TYPE_DUMP )) -return ; - -for (i =0 ; i < kvm->nrcpus; i++) { -struct kvm_cpu *cpu = kvm->cpus[i]; - -if (!cpu) -continue ; - - printout_done =0 ; - -kvm_cpu__set_debug_fd (fd); -pthread_kill (cpu->thread,SIGUSR1 ); -/* -* Wait for the vCPU to dump state before signalling -* the next thread. Since this is debug code it does -* not matter that we are burning CPU time a bit: -*/ -while (!printout_done) -mb (); - } - -close (fd); - -serial8250__inject_sysrq (kvm,'p' ); -} -static void handle_sigalrm (int sig) {kvm__arch_periodic_poll (kvm); } -static void handle_stop (int fd,u32 type,u32 len,u8 *msg) -{ -if (WARN_ON (type !=KVM_IPC_STOP || len)) -return ; - -kvm_cpu__reboot (kvm); -} -static void *kvm_cpu_thread (void *arg) { current_kvm_cpu = arg;
@@ -655,12 +539,6 @@ static int kvm_cmd_run_init(int argc, const char **argv)return PTR_ERR (kvm);signal (SIGALRM , handle_sigalrm); -kvm_ipc__register_handler (KVM_IPC_DEBUG , handle_debug); -signal (SIGUSR1 , handle_sigusr1); -kvm_ipc__register_handler (KVM_IPC_PAUSE , handle_pause); -kvm_ipc__register_handler (KVM_IPC_RESUME , handle_pause); -kvm_ipc__register_handler (KVM_IPC_STOP , handle_stop); -kvm_ipc__register_handler (KVM_IPC_VMSTATE , handle_vmstate); nr_online_cpus =sysconf (_SC_NPROCESSORS_ONLN); kvm->cfg.custom_rootfs_name ="default" ;
tools/kvm/include/kvm/kvm-ipc.h +5 -3
@@ -2,6 +2,7 @@#define KVM__IPC_H_ #include <linux/types.h> +#include "kvm/kvm.h" enum {KVM_IPC_BALLOON =1 ,
@@ -14,9 +15,10 @@ enum { KVM_IPC_VMSTATE = 8, }; -int kvm_ipc__register_handler (u32 type,void (*cb)(int fd,u32 type,u32 len,u8 *msg)); -int kvm_ipc__start (int sock); -int kvm_ipc__stop (void ); +int kvm_ipc__register_handler (u32 type,void (*cb)(struct kvm *kvm, +int fd,u32 type,u32 len,u8 *msg)); +int kvm_ipc__init (struct kvm *kvm); +int kvm_ipc__exit (struct kvm *kvm);int kvm_ipc__send (int fd,u32 type);int kvm_ipc__send_msg (int fd,u32 type,u32 len,u8 *msg);
tools/kvm/kvm-ipc.c +282 -17
389 lines not on this page · raw message
tools/kvm/kvm.c +0 -147
170 lines not on this page · raw message
tools/kvm/virtio/balloon.c +2 -2
@@ -139,7 +139,7 @@ static int virtio_bln__collect_stats(void)return 0 ; } -static void virtio_bln__print_stats (int fd,u32 type,u32 len,u8 *msg) +static void virtio_bln__print_stats (struct kvm *kvm,int fd,u32 type,u32 len,u8 *msg) {int r;
@@ -154,7 +154,7 @@ static void virtio_bln__print_stats(int fd, u32 type, u32 len, u8 *msg)pr_warning ("Failed sending memory stats" ); } -static void handle_mem (int fd,u32 type,u32 len,u8 *msg) +static void handle_mem (struct kvm *kvm,int fd,u32 type,u32 len,u8 *msg) {int mem;
Signature · 1 line
1.7.12
PATCH 23/33 kvm tools: kbd initialization check
In reply to Sasha LevinCheck if i8042 is supported only within the initialization call itself, so that builtin-run won't need to know which archs are supported by it.
--- tools/kvm/builtin-run.c | 8 +++++--- tools/kvm/hw/i8042.c | 8 +++++++- tools/kvm/include/kvm/i8042.h | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-)
tools/kvm/builtin-run.c +5 -3
@@ -818,9 +818,11 @@ static int kvm_cmd_run_init(int argc, const char **argv)kvm__init_ram (kvm); -#ifdef CONFIG_X86 -kbd__init (kvm); -#endif + r =kbd__init (kvm); +if (r <0 ) { +pr_err ("kbd__init() failed with error %d\n" , r); + goto fail; + } r =pci_shmem__init (kvm);if (r <0 ) {
tools/kvm/hw/i8042.c +7 -1
@@ -339,10 +339,16 @@ static struct ioport_operations kbd_ops = { .io_out = kbd_out, }; -void kbd__init (struct kvm *kvm) +int kbd__init (struct kvm *kvm) { +#ifndef CONFIG_X86 +return 0 ; +#endif +kbd_reset (); state.kvm = kvm;ioport__register (I8042_DATA_REG , &kbd_ops,2 ,NULL );ioport__register (I8042_COMMAND_REG , &kbd_ops,2 ,NULL ); + +return 0 ; }
tools/kvm/include/kvm/i8042.h +1 -1
@@ -7,6 +7,6 @@ struct kvm;void mouse_queue (u8 c);void kbd_queue (u8 c); -void kbd__init (struct kvm *kvm); +int kbd__init (struct kvm *kvm);#endif
Signature · 1 line
1.7.12
PATCH 24/33 kvm tools: ui improvements
In reply to Sasha LevinMove the vesa initialization logic into sdl__init() and vnc__init(), builtin-run shouldn't have to know about the conditions for initializing vesa on it's own.
--- tools/kvm/builtin-run.c | 49 ++++++++++---------------------------- tools/kvm/hw/vesa.c | 3 +++ tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/include/kvm/sdl.h | 8 +++---- tools/kvm/include/kvm/vnc.h | 10 ++++---- tools/kvm/ui/sdl.c | 19 ++++++++++++--- tools/kvm/ui/vnc.c | 21 ++++++++++++---- 7 files changed, 60 insertions(+), 51 deletions(-)
tools/kvm/builtin-run.c +13 -36
@@ -60,8 +60,6 @@ static int kvm_run_wrapper;bool do_debug_print = false; -static int vidmode =-1 ; -extern char _binary_guest_init_start;extern char _binary_guest_init_size;
@@ -201,7 +199,7 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i in rootfs mode"), \\ OPT_GROUP("BIOS options:"), \ -OPT_INTEGER ('\0' ,"vidmode" , &vidmode, \ +OPT_INTEGER ('\0' ,"vidmode" , &(cfg)->vidmode, \"Video mode" ), \ \ OPT_GROUP("Debug options:"), \
@@ -530,7 +528,6 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc)static int kvm_cmd_run_init (int argc,const char **argv) {static char real_cmdline[2048 ], default_name[20 ]; -struct framebuffer *fb =NULL ;unsigned int nr_online_cpus;int r;
@@ -642,6 +639,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.script) kvm->cfg.script =DEFAULT_SCRIPT ; +if (!kvm->cfg.vnc && !kvm->cfg.sdl) + kvm->cfg.vidmode =-1 ; + r =term_init (kvm);if (r <0 ) {pr_err ("term_init() failed with error %d\n" , r);
@@ -691,17 +691,6 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -/* -* vidmode should be either specified -* either set by default -*/ -if (kvm->cfg.vnc || kvm->cfg.sdl) { -if (vidmode ==-1 ) - vidmode =0x312 ; - }else { - vidmode =0 ; - } -memset (real_cmdline,0 ,sizeof (real_cmdline));kvm__arch_set_cmdline (real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
@@ -752,7 +741,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.firmware_filename) {if (!kvm__load_kernel (kvm, kvm->cfg.kernel_filename, - kvm->cfg.initrd_filename, real_cmdline, vidmode)) + kvm->cfg.initrd_filename, real_cmdline, kvm->cfg.vidmode))die ("unable to load kernel %s" , kvm->cfg.kernel_filename); kvm->vmlinux = kvm->cfg.vmlinux_filename;
@@ -830,28 +819,16 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -if (kvm->cfg.vnc || kvm->cfg.sdl) { - fb =vesa__init (kvm); -if (IS_ERR (fb)) { -pr_err ("vesa__init() failed with error %ld\n" ,PTR_ERR (fb)); - goto fail; - } - } - -if (kvm->cfg.vnc && fb) { - r =vnc__init (fb); -if (r <0 ) { -pr_err ("vnc__init() failed with error %d\n" , r); - goto fail; - } + r =vnc__init (kvm); +if (r <0 ) { +pr_err ("vnc__init() failed with error %d\n" , r); + goto fail; } -if (kvm->cfg.sdl && fb) { -sdl__init (fb); -if (r <0 ) { -pr_err ("sdl__init() failed with error %d\n" , r); - goto fail; - } + r =sdl__init (kvm); +if (r <0 ) { +pr_err ("sdl__init() failed with error %d\n" , r); + goto fail; } r =fb__init (kvm);
tools/kvm/hw/vesa.c +3 -0
@@ -53,6 +53,9 @@ struct framebuffer *vesa__init(struct kvm *kvm)char *mem;int r; +if (!kvm->cfg.vnc && !kvm->cfg.sdl) +return NULL ; + r =irq__register_device (PCI_DEVICE_ID_VESA , &dev, &pin, &line);if (r <0 )return ERR_PTR (r);
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -25,6 +25,7 @@ struct kvm_config {int active_console;int debug_iodelay;int nrcpus; +int vidmode;const char *kernel_cmdline;const char *kernel_filename;const char *vmlinux_filename;
tools/kvm/include/kvm/sdl.h +4 -4
@@ -6,14 +6,14 @@struct framebuffer ;#ifdef CONFIG_HAS_SDL -int sdl__init (struct framebuffer *fb); -int sdl__exit (struct framebuffer *fb); +int sdl__init (struct kvm *kvm); +int sdl__exit (struct kvm *kvm);#else -static inline void sdl__init (struct framebuffer *fb) +static inline void sdl__init (struct kvm *kvm) {die ("SDL support not compiled in. (install the SDL-dev[el] package)" ); } -static inline void sdl__exit (struct framebuffer *fb) +static inline void sdl__exit (struct kvm *kvm) {die ("SDL support not compiled in. (install the SDL-dev[el] package)" ); }
tools/kvm/include/kvm/vnc.h +6 -4
@@ -1,17 +1,19 @@#ifndef KVM__VNC_H #define KVM__VNC_H +#include "kvm/kvm.h" +struct framebuffer ;#ifdef CONFIG_HAS_VNCSERVER -int vnc__init (struct framebuffer *fb); -int vnc__exit (struct framebuffer *fb); +int vnc__init (struct kvm *kvm); +int vnc__exit (struct kvm *kvm);#else -static inline int vnc__init (struct framebuffer *fb) +static inline int vnc__init (struct kvm *kvm) {return 0 ; } -static inline int vnc__exit (struct framebuffer *fb) +static inline int vnc__exit (struct kvm *kvm) {return 0 ; }
tools/kvm/ui/sdl.c +16 -3
@@ -5,10 +5,12 @@#include "kvm/util.h" #include "kvm/kvm.h" #include "kvm/kvm-cpu.h" +#include "kvm/vesa.h" #include <SDL/SDL.h> #include <pthread.h> #include <signal.h> +#include <linux/err.h> #define FRAME_RATE 25
@@ -292,12 +294,23 @@ static struct fb_target_operations sdl_ops = { .stop = sdl__stop, }; -int sdl__init (struct framebuffer *fb) +int sdl__init (struct kvm *kvm) { +struct framebuffer *fb; + +if (!kvm->cfg.sdl) +return 0 ; + + fb =vesa__init (kvm); +if (IS_ERR (fb)) { +pr_err ("vesa__init() failed with error %ld\n" ,PTR_ERR (fb)); +return PTR_ERR (fb); + } +return fb__attach (fb, &sdl_ops); } -int sdl__exit (struct framebuffer *fb) +int sdl__exit (struct kvm *kvm) { -return sdl__stop (fb); +return sdl__stop (NULL ); }
tools/kvm/ui/vnc.c +17 -4
@@ -2,11 +2,13 @@#include "kvm/framebuffer.h" #include "kvm/i8042.h" +#include "kvm/vesa.h" #include <linux/types.h> #include <rfb/keysym.h> #include <rfb/rfb.h> #include <pthread.h> +#include <linux/err.h> #define VESA_QUEUE_SIZE 128#define VESA_IRQ 14
@@ -219,12 +221,23 @@ static struct fb_target_operations vnc_ops = { .stop = vnc__stop, }; -int vnc__init (struct framebuffer *fb) +int vnc__init (struct kvm *kvm) { +struct framebuffer *fb; + +if (!kvm->cfg.vnc) +return 0 ; + + fb =vesa__init (kvm); +if (IS_ERR (fb)) { +pr_err ("vesa__init() failed with error %ld\n" ,PTR_ERR (fb)); +return PTR_ERR (fb); + } +return fb__attach (fb, &vnc_ops); } -int vnc__exit (struct framebuffer *fb) +int vnc__exit (struct kvm *kvm) { -return vnc__stop (fb); -} +return vnc__stop (NULL ); +}
Signature · 1 line
1.7.12
PATCH 25/33 kvm tools: kernel load/firmware cleanup
In reply to Sasha LevinSort out the config initialization order so that configuration is fully initialized before init functions start running, and move the firmware initialization code into kvm.c.
--- tools/kvm/builtin-run.c | 122 ++++++++++++++++--------------------- tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/kvm.c | 15 +++++ 3 files changed, 70 insertions(+), 68 deletions(-)
tools/kvm/builtin-run.c +54 -68
@@ -594,6 +594,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } kvm->cfg.vmlinux_filename =find_vmlinux (); + kvm->vmlinux = kvm->cfg.vmlinux_filename;if (kvm->cfg.nrcpus ==0 ) kvm->cfg.nrcpus = nr_online_cpus;
@@ -642,11 +643,14 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.vnc && !kvm->cfg.sdl) kvm->cfg.vidmode =-1 ; - r =term_init (kvm); -if (r <0 ) { -pr_err ("term_init() failed with error %d\n" , r); - goto fail; - } +memset (real_cmdline,0 ,sizeof (real_cmdline)); +kvm__arch_set_cmdline (real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl); + +if (strlen (real_cmdline) >0 ) +strcat (real_cmdline," " ); + +if (kvm->cfg.kernel_cmdline) +strlcat (real_cmdline, kvm->cfg.kernel_cmdline,sizeof (real_cmdline));if (!kvm->cfg.guest_name) {if (kvm->cfg.custom_rootfs) {
@@ -657,10 +661,52 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } +if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0 ].filename && !kvm->cfg.initrd_filename) { +char tmp[PATH_MAX ]; + +kvm_setup_create_new (kvm->cfg.custom_rootfs_name); +kvm_setup_resolv (kvm->cfg.custom_rootfs_name); + +snprintf (tmp,PATH_MAX ,"%s%s" ,kvm__get_dir (),"default" ); +if (virtio_9p__register (kvm, tmp,"/dev/root" ) <0 ) +die ("Unable to initialize virtio 9p" ); +if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 ) +die ("Unable to initialize virtio 9p" ); + kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs =1 ; + } + +if (kvm->cfg.using_rootfs) { +strcat (real_cmdline," root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p" ); +if (kvm->cfg.custom_rootfs) { +kvm_run_set_sandbox (); + +strcat (real_cmdline," init=/virt/init" ); + +if (!kvm->cfg.no_dhcp) +strcat (real_cmdline," ip=dhcp" ); +if (kvm_setup_guest_init ()) +die ("Failed to setup init for guest." ); + } + }else if (!strstr (real_cmdline,"root=" )) { +strlcat (real_cmdline," root=/dev/vda rw " ,sizeof (real_cmdline)); + } + + kvm->cfg.real_cmdline = real_cmdline; + +printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , + kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , kvm->cfg.nrcpus, kvm->cfg.guest_name); + r =kvm__init (kvm);if (r) goto fail; + r =term_init (kvm); +if (r <0 ) { +pr_err ("term_init() failed with error %d\n" , r); + goto fail; + } + + r =ioeventfd__init (kvm);if (r <0 ) {pr_err ("ioeventfd__init() failed with error %d\n" , r);
@@ -691,64 +737,15 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -memset (real_cmdline,0 ,sizeof (real_cmdline)); -kvm__arch_set_cmdline (real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl); - -if (strlen (real_cmdline) >0 ) -strcat (real_cmdline," " ); - -if (kvm->cfg.kernel_cmdline) -strlcat (real_cmdline, kvm->cfg.kernel_cmdline,sizeof (real_cmdline)); - -if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0 ].filename && !kvm->cfg.initrd_filename) { -char tmp[PATH_MAX ]; - -kvm_setup_create_new (kvm->cfg.custom_rootfs_name); -kvm_setup_resolv (kvm->cfg.custom_rootfs_name); - -snprintf (tmp,PATH_MAX ,"%s%s" ,kvm__get_dir (),"default" ); -if (virtio_9p__register (kvm, tmp,"/dev/root" ) <0 ) -die ("Unable to initialize virtio 9p" ); -if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 ) -die ("Unable to initialize virtio 9p" ); - kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs =1 ; - } - -if (kvm->cfg.using_rootfs) { -strcat (real_cmdline," root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p" ); -if (kvm->cfg.custom_rootfs) { -kvm_run_set_sandbox (); - -strcat (real_cmdline," init=/virt/init" ); - -if (!kvm->cfg.no_dhcp) -strcat (real_cmdline," ip=dhcp" ); -if (kvm_setup_guest_init ()) -die ("Failed to setup init for guest." ); - } - }else if (!strstr (real_cmdline,"root=" )) { -strlcat (real_cmdline," root=/dev/vda rw " ,sizeof (real_cmdline)); - } - r =disk_image__init (kvm);if (r <0 ) {pr_err ("disk_image__init() failed with error %d\n" , r); goto fail; } -printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , - kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , kvm->cfg.nrcpus, kvm->cfg.guest_name); - -if (!kvm->cfg.firmware_filename) { -if (!kvm__load_kernel (kvm, kvm->cfg.kernel_filename, - kvm->cfg.initrd_filename, real_cmdline, kvm->cfg.vidmode)) -die ("unable to load kernel %s" , kvm->cfg.kernel_filename); - - kvm->vmlinux = kvm->cfg.vmlinux_filename; - r =symbol_init (kvm); -if (r <0 ) -pr_debug ("symbol_init() failed with error %d\n" , r); - } + r =symbol_init (kvm); +if (r <0 ) +pr_debug ("symbol_init() failed with error %d\n" , r);ioport__setup_arch ();
@@ -848,17 +845,6 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -if (kvm->cfg.firmware_filename) { -if (!kvm__load_firmware (kvm, kvm->cfg.firmware_filename)) -die ("unable to load firmware image %s: %s" , kvm->cfg.firmware_filename,strerror (errno)); - }else { -kvm__arch_setup_firmware (kvm); -if (r <0 ) { -pr_err ("kvm__arch_setup_firmware() failed with error %d\n" , r); - goto fail; - } - } - r =thread_pool__init (kvm);if (r <0 ) {pr_err ("thread_pool__init() failed with error %d\n" , r);
tools/kvm/include/kvm/kvm-config.h +1 -0
@@ -43,6 +43,7 @@ struct kvm_config {const char *sandbox;const char *hugetlbfs_path;const char *custom_rootfs_name; +const char *real_cmdline;struct virtio_net_params *net_params;bool single_step;bool vnc;
tools/kvm/kvm.c +15 -0
@@ -246,6 +246,21 @@ int kvm__init(struct kvm *kvm)kvm__arch_init (kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); +if (!kvm->cfg.firmware_filename) { +if (!kvm__load_kernel (kvm, kvm->cfg.kernel_filename, + kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode)) +die ("unable to load kernel %s" , kvm->cfg.kernel_filename); + } + +if (kvm->cfg.firmware_filename) { +if (!kvm__load_firmware (kvm, kvm->cfg.firmware_filename)) +die ("unable to load firmware image %s: %s" , kvm->cfg.firmware_filename,strerror (errno)); + }else { + ret =kvm__arch_setup_firmware (kvm); +if (ret <0 ) +die ("kvm__arch_setup_firmware() failed with error %d\n" , ret); + } +return 0 ; err_vm_fd:
Signature · 1 line
1.7.12
PATCH 26/33 kvm tools: ioport arch init
In reply to Sasha LevinMove ioport arch init into ioport init, which is the logical place for that instead of a random place in the global init code.
--- tools/kvm/builtin-run.c | 2 -- tools/kvm/ioport.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
tools/kvm/builtin-run.c +0 -2
@@ -747,8 +747,6 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (r <0 )pr_debug ("symbol_init() failed with error %d\n" , r); -ioport__setup_arch (); - r =rtc__init (kvm);if (r <0 ) {pr_err ("rtc__init() failed with error %d\n" , r);
tools/kvm/ioport.c +2 -0
@@ -184,6 +184,8 @@ error:int ioport__init (struct kvm *kvm) { +ioport__setup_arch (); +return 0 ; }
Signature · 1 line
1.7.12
PATCH 27/33 kvm tools: ram init
In reply to Sasha LevinRAM should be initialized as part of kvm__init, and not somewhere random in the global init code.
--- tools/kvm/builtin-run.c | 2 -- tools/kvm/kvm.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
tools/kvm/builtin-run.c +0 -2
@@ -800,8 +800,6 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -kvm__init_ram (kvm); - r =kbd__init (kvm);if (r <0 ) {pr_err ("kbd__init() failed with error %d\n" , r);
tools/kvm/kvm.c +2 -0
@@ -246,6 +246,8 @@ int kvm__init(struct kvm *kvm)kvm__arch_init (kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); +kvm__init_ram (kvm); +if (!kvm->cfg.firmware_filename) {if (!kvm__load_kernel (kvm, kvm->cfg.kernel_filename, kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode))
Signature · 1 line
1.7.12
PATCH 28/33 kvm tools: move the rest of the config initializations
In reply to Sasha LevinThese should appear before running any init calls.
--- tools/kvm/builtin-run.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/kvm/builtin-run.c +3 -3
@@ -643,6 +643,9 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.vnc && !kvm->cfg.sdl) kvm->cfg.vidmode =-1 ; +if (!kvm->cfg.network) + kvm->cfg.network =DEFAULT_NETWORK ; +memset (real_cmdline,0 ,sizeof (real_cmdline));kvm__arch_set_cmdline (real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
@@ -789,9 +792,6 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -if (!kvm->cfg.network) - kvm->cfg.network =DEFAULT_NETWORK ; -virtio_9p__init (kvm); r =virtio_net__init (kvm);
Signature · 1 line
1.7.12
PATCH 29/33 kvm tools: virtio-9p cleanup
In reply to Sasha LevinSort out init/exit calls, move parser into the 9p code and make sure rootfs config is initialized before virtio-9p (or any other init func) is called.
--- tools/kvm/builtin-run.c | 67 ++++++-------------------------------- tools/kvm/include/kvm/virtio-9p.h | 3 ++ tools/kvm/virtio/9p.c | 68 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 58 deletions(-)
tools/kvm/builtin-run.c +9 -58
@@ -77,42 +77,12 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) {char path[PATH_MAX ];struct stat st; -struct kvm *kvm = opt->ptr; - -if (stat (arg, &st) ==0 && -S_ISDIR (st.st_mode)) { -char tmp[PATH_MAX ]; - -if (kvm->cfg.using_rootfs) -die ("Please use only one rootfs directory atmost" ); - -if (realpath (arg, tmp) ==0 || -virtio_9p__register (kvm, tmp,"/dev/root" ) <0 ) -die ("Unable to initialize virtio 9p" ); - kvm->cfg.using_rootfs =1 ; -return 0 ; - }snprintf (path,PATH_MAX ,"%s%s" ,kvm__get_dir (), arg); -if (stat (path, &st) ==0 && -S_ISDIR (st.st_mode)) { -char tmp[PATH_MAX ]; - -if (kvm->cfg.using_rootfs) -die ("Please use only one rootfs directory atmost" ); - -if (realpath (path, tmp) ==0 || -virtio_9p__register (kvm, tmp,"/dev/root" ) <0 ) -die ("Unable to initialize virtio 9p" ); -if (virtio_9p__register (kvm,"/" ,"hostfs" ) <0 ) -die ("Unable to initialize virtio 9p" ); -kvm_setup_resolv (arg); - kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs =1 ; - kvm->cfg.custom_rootfs_name = arg; -return 0 ; - } - +if ((stat (arg, &st) ==0 &&S_ISDIR (st.st_mode)) || + (stat (path, &st) ==0 &&S_ISDIR (st.st_mode))) +return virtio_9p_img_name_parser (opt, arg, unset);return disk_img_name_parser (opt, arg, unset); }
@@ -121,29 +91,6 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper =KVM_RUN_SANDBOX ; } -static int virtio_9p_rootdir_parser (const struct option *opt,const char *arg,int unset) -{ -char *tag_name; -char tmp[PATH_MAX ]; - -/* -* 9p dir can be of the form dirname,tag_name or -* just dirname. In the later case we use the -* default tag name -*/ - tag_name =strstr (arg,"," ); -if (tag_name) { - *tag_name ='\0' ; - tag_name++; - } -if (realpath (arg, tmp)) { -if (virtio_9p__register (kvm, tmp, tag_name) <0 ) -die ("Unable to initialize virtio 9p" ); - }else -die ("Failed resolving 9p path" ); -return 0 ; -} -#define BUILD_OPTIONS (name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \
@@ -167,7 +114,7 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i Number Generator"), \ OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ "Enable virtio 9p to share files between host and \ - guest", virtio_9p_rootdir_parser, NULL), \ + guest", virtio_9p_rootdir_parser, kvm), \ OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or \ hv", " Console to use"), \ OPT_STRING('\0', " dev", &(cfg)->dev, " device_file", \
@@ -792,7 +739,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } -virtio_9p__init (kvm); + r =virtio_9p__init (kvm); +if (r <0 ) { +pr_err ("virtio_9p__init() failed with error %d\n" , r); + goto fail; + } r =virtio_net__init (kvm);if (r <0 ) {
tools/kvm/include/kvm/virtio-9p.h +3 -0
@@ -3,6 +3,7 @@#include "kvm/virtio.h" #include "kvm/pci.h" #include "kvm/threadpool.h" +#include "kvm/parse-options.h" #include <sys/types.h> #include <dirent.h>
@@ -65,6 +66,8 @@ struct p9_pdu {struct kvm ; +int virtio_9p_rootdir_parser (const struct option *opt,const char *arg,int unset); +int virtio_9p_img_name_parser (const struct option *opt,const char *arg,int unset);int virtio_9p__register (struct kvm *kvm,const char *root,const char *tag_name);int virtio_9p__init (struct kvm *kvm);int virtio_p9_pdu_readf (struct p9_pdu *pdu,const char *fmt, ...);
tools/kvm/virtio/9p.c +68 -0
80 lines not on this page · raw message
Signature · 1 line
1.7.12
PATCH 30/33 kvm tools: add init/exit automatic calls
In reply to Sasha LevinThis adds a method to call init/exit functions similar to the kernel's init functions.
--- tools/kvm/include/kvm/util-init.h | 51 +++++++++++++++++++++++++++++ tools/kvm/util/init.c | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tools/kvm/include/kvm/util-init.h create mode 100644 tools/kvm/util/init.c
tools/kvm/include/kvm/util-init.h new file +51 -0
@@ -0,0 +1,51 @@ +#ifndef KVM__UTIL_INIT_H +#define KVM__UTIL_INIT_H + +struct kvm ; + +struct init_item { +struct hlist_node n; +const char *fn_name; +int (*init)(struct kvm *); +}; + +int init_list__init (struct kvm *kvm); +int init_list__exit (struct kvm *kvm); + +int init_list_add (struct init_item *t,int (*init)(struct kvm *), +int priority,const char *name); +int exit_list_add (struct init_item *t,int (*init)(struct kvm *), +int priority,const char *name); + +#define __init_list_add (cb, l) \ +static void __attribute__ ((constructor)) __init__##cb(void) \ +{ \ + static char name[] = #cb; \ + static struct init_item t; \ + init_list_add(&t, cb, l, name); \ +} + +#define __exit_list_add (cb, l) \ +static void __attribute__ ((constructor)) __init__##cb(void) \ +{ \ + static char name[] = #cb; \ + static struct init_item t; \ + exit_list_add(&t, cb, l, name); \ +} + +#define core_init (cb) __init_list_add(cb, 0) +#define base_init (cb) __init_list_add(cb, 2) +#define dev_base_init (cb) __init_list_add(cb, 4) +#define dev_init (cb) __init_list_add(cb, 5) +#define virtio_dev_init (cb) __init_list_add(cb, 6) +#define firmware_init (cb) __init_list_add(cb, 7) +#define late_init (cb) __init_list_add(cb, 9) + +#define core_exit (cb) __exit_list_add(cb, 0) +#define base_exit (cb) __exit_list_add(cb, 2) +#define dev_base_exit (cb) __exit_list_add(cb, 4) +#define dev_exit (cb) __exit_list_add(cb, 5) +#define virtio_dev_exit (cb) __exit_list_add(cb, 6) +#define firmware_exit (cb) __exit_list_add(cb, 7) +#define late_exit (cb) __exit_list_add(cb, 9) +#endif
tools/kvm/util/init.c new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
1.7.12
PATCH 31/33 kvm tools: use init/exit where possible
In reply to Sasha LevinSwitch to using init/exit calls instead of the repeating call blocks in builtin-run.
--- tools/kvm/Makefile | 1 + tools/kvm/builtin-run.c | 246 +------------------------------------------- tools/kvm/disk/core.c | 2 + tools/kvm/framebuffer.c | 3 + tools/kvm/hw/i8042.c | 1 + tools/kvm/hw/pci-shmem.c | 2 + tools/kvm/hw/rtc.c | 2 + tools/kvm/hw/serial.c | 2 + tools/kvm/include/kvm/kvm.h | 2 + tools/kvm/ioeventfd.c | 2 + tools/kvm/ioport.c | 2 + tools/kvm/kvm-cpu.c | 2 + tools/kvm/kvm-ipc.c | 2 + tools/kvm/kvm.c | 4 + tools/kvm/pci.c | 2 + tools/kvm/symbol.c | 4 +- tools/kvm/term.c | 2 + tools/kvm/ui/sdl.c | 7 +- tools/kvm/ui/vnc.c | 7 +- tools/kvm/util/threadpool.c | 3 + tools/kvm/virtio/9p.c | 1 + tools/kvm/virtio/balloon.c | 2 + tools/kvm/virtio/blk.c | 2 + tools/kvm/virtio/console.c | 2 + tools/kvm/virtio/net.c | 2 + tools/kvm/virtio/rng.c | 2 + tools/kvm/virtio/scsi.c | 2 + tools/kvm/x86/irq.c | 2 + 28 files changed, 66 insertions(+), 247 deletions(-)
tools/kvm/Makefile +1 -0
@@ -84,6 +84,7 @@ OBJS += net/uip/buf.o OBJS += net/uip/csum.o OBJS += net/uip/dhcp.o OBJS += kvm-cmd.o +OBJS += util/init.o OBJS += util/rbtree.o OBJS += util/threadpool.o OBJS += util/parse-options.o
tools/kvm/builtin-run.c +2 -244
266 lines not on this page · raw message
tools/kvm/disk/core.c +2 -0
@@ -345,8 +345,10 @@ int disk_image__init(struct kvm *kvm)return 0 ; } +dev_base_init (disk_image__init);int disk_image__exit (struct kvm *kvm) {return disk_image__close_all (kvm->disks, kvm->nr_disks); } +dev_base_exit (disk_image__exit);
tools/kvm/framebuffer.c +3 -0
@@ -1,4 +1,5 @@#include "kvm/framebuffer.h" +#include "kvm/kvm.h" #include <linux/kernel.h> #include <linux/list.h>
@@ -58,6 +59,7 @@ int fb__init(struct kvm *kvm)return 0 ; } +firmware_init (fb__init);int fb__exit (struct kvm *kvm) {
@@ -75,3 +77,4 @@ int fb__exit(struct kvm *kvm)return 0 ; } +firmware_exit (fb__exit);
tools/kvm/hw/i8042.c +1 -0
@@ -352,3 +352,4 @@ int kbd__init(struct kvm *kvm)return 0 ; } +dev_init (kbd__init);
tools/kvm/hw/pci-shmem.c +2 -0
@@ -396,8 +396,10 @@ int pci_shmem__init(struct kvm *kvm) mem);return 0 ; } +dev_init (pci_shmem__init);int pci_shmem__exit (struct kvm *kvm) {return 0 ; } +dev_exit (pci_shmem__exit);
tools/kvm/hw/rtc.c +2 -0
@@ -126,6 +126,7 @@ int rtc__init(struct kvm *kvm)return r; } +dev_init (rtc__init);int rtc__exit (struct kvm *kvm) {
@@ -135,3 +136,4 @@ int rtc__exit(struct kvm *kvm)return 0 ; } +dev_exit (rtc__exit);
tools/kvm/hw/serial.c +2 -0
@@ -432,6 +432,7 @@ cleanup:return r; } +dev_init (serial8250__init);int serial8250__exit (struct kvm *kvm) {
@@ -448,3 +449,4 @@ int serial8250__exit(struct kvm *kvm)return 0 ; } +dev_exit (serial8250__exit);
tools/kvm/include/kvm/kvm.h +2 -0
@@ -3,6 +3,8 @@#include "kvm/kvm-arch.h" #include "kvm/kvm-config.h" +#include "kvm/util-init.h" +#include "kvm/kvm.h" #include <stdbool.h> #include <linux/types.h>
tools/kvm/ioeventfd.c +2 -0
@@ -93,6 +93,7 @@ cleanup:return r; } +base_init (ioeventfd__init);int ioeventfd__exit (struct kvm *kvm) {
@@ -115,6 +116,7 @@ int ioeventfd__exit(struct kvm *kvm)return 0 ; } +base_exit (ioeventfd__exit);int ioeventfd__add_event (struct ioevent *ioevent,bool is_pio,bool poll_in_userspace) {
tools/kvm/ioport.c +2 -0
@@ -188,9 +188,11 @@ int ioport__init(struct kvm *kvm)return 0 ; } +dev_base_init (ioport__init);int ioport__exit (struct kvm *kvm) {ioport__unregister_all ();return 0 ; } +dev_base_exit (ioport__exit);
tools/kvm/kvm-cpu.c +2 -0
@@ -212,6 +212,7 @@ fail_alloc:free (kvm->cpus[i]);return -ENOMEM ; } +base_init (kvm_cpu__init);int kvm_cpu__exit (struct kvm *kvm) {
@@ -238,3 +239,4 @@ int kvm_cpu__exit(struct kvm *kvm)return r; } +late_exit (kvm_cpu__exit);
tools/kvm/kvm-ipc.c +2 -0
@@ -477,6 +477,7 @@ err_epoll: err:return ret; } +base_init (kvm_ipc__init);int kvm_ipc__exit (struct kvm *kvm) {
@@ -494,3 +495,4 @@ int kvm_ipc__exit(struct kvm *kvm)return ret; } +base_exit (kvm_ipc__exit);
tools/kvm/kvm.c +4 -0
@@ -140,6 +140,7 @@ int kvm__exit(struct kvm *kvm)return 0 ; } +core_exit (kvm__exit);/* * Note: KVM_SET_USER_MEMORY_REGION assumes that we don't pass overlapping
@@ -274,6 +275,7 @@ err_free: err:return ret; } +core_init (kvm__init);/* RFC 1952 */ #define GZIP_ID1 0x1f
@@ -375,6 +377,7 @@ int kvm_timer__init(struct kvm *kvm)return 0 ; } +firmware_init (kvm_timer__init);int kvm_timer__exit (struct kvm *kvm) {
@@ -386,6 +389,7 @@ int kvm_timer__exit(struct kvm *kvm)return 0 ; } +firmware_exit (kvm_timer__exit);void kvm__dump_mem (struct kvm *kvm,unsigned long addr,unsigned long size) {
tools/kvm/pci.c +2 -0
@@ -203,6 +203,7 @@ int pci__init(struct kvm *kvm)return 0 ; } +base_init (pci__init);int pci__exit (struct kvm *kvm) {
@@ -211,3 +212,4 @@ int pci__exit(struct kvm *kvm)return 0 ; } +base_exit (pci__exit);
tools/kvm/symbol.c +3 -1
@@ -15,7 +15,7 @@ int symbol_init(struct kvm *kvm)int ret =0 ;if (!kvm->vmlinux) -return -EINVAL ; +return 0 ;bfd_init ();
@@ -38,6 +38,7 @@ int symbol_init(struct kvm *kvm)return ret; } +late_init (symbol_init);static asymbol *lookup (asymbol **symbols,int nr_symbols,const char *symbol_name) {
@@ -129,3 +130,4 @@ int symbol_exit(struct kvm *kvm)return -EFAULT ; } +late_exit (symbol_exit);
tools/kvm/term.c +2 -0
@@ -163,8 +163,10 @@ int term_init(struct kvm *kvm)return 0 ; } +dev_init (term_init);int term_exit (struct kvm *kvm) {return 0 ; } +dev_exit (term_exit);
tools/kvm/ui/sdl.c +6 -1
@@ -309,8 +309,13 @@ int sdl__init(struct kvm *kvm)return fb__attach (fb, &sdl_ops); } +dev_init (sdl__init);int sdl__exit (struct kvm *kvm) { -return sdl__stop (NULL ); +if (kvm->cfg.sdl) +return sdl__stop (NULL ); + +return 0 ; } +dev_exit (sdl__exit);
tools/kvm/ui/vnc.c +6 -1
@@ -236,8 +236,13 @@ int vnc__init(struct kvm *kvm)return fb__attach (fb, &vnc_ops); } +dev_init (vnc__init);int vnc__exit (struct kvm *kvm) { -return vnc__stop (NULL ); +if (kvm->cfg.vnc) +return vnc__stop (NULL ); + +return 0 ; } +dev_exit (vnc__exit);
tools/kvm/util/threadpool.c +3 -0
@@ -1,5 +1,6 @@#include "kvm/threadpool.h" #include "kvm/mutex.h" +#include "kvm/kvm.h" #include <linux/kernel.h> #include <linux/list.h>
@@ -131,6 +132,7 @@ int thread_pool__init(struct kvm *kvm)return i; } +late_init (thread_pool__init);int thread_pool__exit (struct kvm *kvm) {
@@ -151,6 +153,7 @@ int thread_pool__exit(struct kvm *kvm)return 0 ; } +late_exit (thread_pool__exit);void thread_pool__do_job (struct thread_pool__job *job) {
tools/kvm/virtio/9p.c +1 -0
@@ -1404,6 +1404,7 @@ int virtio_9p__init(struct kvm *kvm)return 0 ; } +virtio_dev_init (virtio_9p__init);int virtio_9p__register (struct kvm *kvm,const char *root,const char *tag_name) {
tools/kvm/virtio/balloon.c +2 -0
@@ -262,8 +262,10 @@ int virtio_bln__init(struct kvm *kvm)return 0 ; } +virtio_dev_init (virtio_bln__init);int virtio_bln__exit (struct kvm *kvm) {return 0 ; } +virtio_dev_exit (virtio_bln__exit);
tools/kvm/virtio/blk.c +2 -0
@@ -299,6 +299,7 @@ int virtio_blk__init(struct kvm *kvm) cleanup:return virtio_blk__exit (kvm); } +virtio_dev_init (virtio_blk__init);int virtio_blk__exit (struct kvm *kvm) {
@@ -311,3 +312,4 @@ int virtio_blk__exit(struct kvm *kvm)return 0 ; } +virtio_dev_exit (virtio_blk__exit);
tools/kvm/virtio/console.c +2 -0
@@ -194,8 +194,10 @@ int virtio_console__init(struct kvm *kvm)return 0 ; } +virtio_dev_init (virtio_console__init);int virtio_console__exit (struct kvm *kvm) {return 0 ; } +virtio_dev_exit (virtio_console__exit);
tools/kvm/virtio/net.c +2 -0
@@ -663,8 +663,10 @@ int virtio_net__init(struct kvm *kvm)return 0 ; } +virtio_dev_init (virtio_net__init);int virtio_net__exit (struct kvm *kvm) {return 0 ; } +virtio_dev_exit (virtio_net__exit);
tools/kvm/virtio/rng.c +2 -0
@@ -176,6 +176,7 @@ cleanup:return r; } +virtio_dev_init (virtio_rng__init);int virtio_rng__exit (struct kvm *kvm) {
@@ -189,3 +190,4 @@ int virtio_rng__exit(struct kvm *kvm)return 0 ; } +virtio_dev_exit (virtio_rng__exit);
tools/kvm/virtio/scsi.c +2 -0
@@ -293,6 +293,7 @@ int virtio_scsi_init(struct kvm *kvm) cleanup:return virtio_scsi_exit (kvm); } +virtio_dev_init (virtio_scsi_init);int virtio_scsi_exit (struct kvm *kvm) {
@@ -305,3 +306,4 @@ int virtio_scsi_exit(struct kvm *kvm)return 0 ; } +virtio_dev_exit (virtio_scsi_exit);
tools/kvm/x86/irq.c +2 -0
@@ -172,6 +172,7 @@ int irq__init(struct kvm *kvm)return 0 ; } +dev_base_init (irq__init);int irq__exit (struct kvm *kvm) {
@@ -195,6 +196,7 @@ int irq__exit(struct kvm *kvm)return 0 ; } +dev_base_exit (irq__exit);int irq__add_msix_route (struct kvm *kvm,struct msi_msg *msg) {
Signature · 1 line
1.7.12
PATCH 32/33 kvm tools: pass kvm ptr directly to timer injection
In reply to Sasha LevinThis will help us get rid of the global kvm object.
--- tools/kvm/builtin-run.c | 10 ++++++++-- tools/kvm/kvm.c | 1 + 2 files changed, 9 insertions(+), 2 deletions(-)
tools/kvm/builtin-run.c +8 -2
@@ -163,8 +163,10 @@ void kvm_run_set_wrapper_sandbox(void) OPT_END() \ }; -static void handle_sigalrm (int sig) +static void handle_sigalrm (int sig,siginfo_t *si,void *uc) { +struct kvm *kvm = uc; +kvm__arch_periodic_poll (kvm); }
@@ -476,12 +478,16 @@ static int kvm_cmd_run_init(int argc, const char **argv) {static char real_cmdline[2048 ], default_name[20 ];unsigned int nr_online_cpus; +struct sigaction sa; kvm =kvm__new ();if (IS_ERR (kvm))return PTR_ERR (kvm); -signal (SIGALRM , handle_sigalrm); + sa.sa_flags =SA_SIGINFO ; + sa.sa_sigaction = handle_sigalrm; +sigemptyset (&sa.sa_mask); +sigaction (SIGALRM , &sa,NULL ); nr_online_cpus =sysconf (_SC_NPROCESSORS_ONLN); kvm->cfg.custom_rootfs_name ="default" ;
tools/kvm/kvm.c +1 -0
@@ -358,6 +358,7 @@ int kvm_timer__init(struct kvm *kvm) sev.sigev_value.sival_int =0 ; sev.sigev_notify =SIGEV_THREAD_ID ; sev.sigev_signo =SIGALRM ; + sev.sigev_value.sival_ptr = kvm; sev._sigev_un._tid =syscall (__NR_gettid); r =timer_create (CLOCK_REALTIME , &sev, &kvm->timerid);
Signature · 1 line
1.7.12
PATCH 33/33 kvm tools: remove global kvm object
In reply to Sasha LevinThis was ugly, and now we get rid of it.
--- tools/kvm/builtin-run.c | 48 +++++++++++++++++++++++------------------- tools/kvm/hw/i8042.c | 4 ++-- tools/kvm/hw/pci-shmem.c | 2 +- tools/kvm/hw/rtc.c | 10 ++++----- tools/kvm/hw/serial.c | 8 +++---- tools/kvm/hw/vesa.c | 2 +- tools/kvm/include/kvm/brlock.h | 16 +++++++------- tools/kvm/include/kvm/ioport.h | 7 +++--- tools/kvm/include/kvm/kvm.h | 4 ++-- tools/kvm/include/kvm/term.h | 4 ++-- tools/kvm/ioport.c | 16 +++++++------- tools/kvm/kvm-cpu.c | 2 +- tools/kvm/kvm-ipc.c | 4 ++-- tools/kvm/kvm.c | 6 ++---- tools/kvm/mmio.c | 10 ++++----- tools/kvm/pci.c | 10 ++++----- tools/kvm/term.c | 7 +++--- tools/kvm/virtio/balloon.c | 5 ++--- tools/kvm/virtio/console.c | 2 +- tools/kvm/virtio/net.c | 12 +++++------ tools/kvm/virtio/pci.c | 6 +++--- tools/kvm/x86/ioport.c | 20 +++++++++--------- 22 files changed, 102 insertions(+), 103 deletions(-)
tools/kvm/builtin-run.c +26 -22
@@ -53,7 +53,6 @@#define KB_SHIFT (10)#define GB_SHIFT (30) -struct kvm *kvm; __threadstruct kvm_cpu *current_kvm_cpu;static int kvm_run_wrapper;
@@ -339,11 +338,13 @@ static const char *find_vmlinux(void)void kvm_run_help (void ) { +struct kvm *kvm =NULL ; +BUILD_OPTIONS (options, &kvm->cfg, kvm);usage_with_options (run_usage, options); } -static int kvm_setup_guest_init (void ) +static int kvm_setup_guest_init (struct kvm *kvm) {const char *rootfs = kvm->cfg.custom_rootfs_name;char tmp[PATH_MAX ];
@@ -367,7 +368,7 @@ static int kvm_setup_guest_init(void)return 0 ; } -static int kvm_run_set_sandbox (void ) +static int kvm_run_set_sandbox (struct kvm *kvm) {const char *guestfs_name = kvm->cfg.custom_rootfs_name;char path[PATH_MAX ], script[PATH_MAX ], *tmp;
@@ -439,7 +440,7 @@ static void resolve_program(const char *src, char *dst, size_t len)strncpy (dst, src, len); } -static void kvm_run_write_sandbox_cmd (const char **argv,int argc) +static void kvm_run_write_sandbox_cmd (struct kvm *kvm,const char **argv,int argc) {const char script_hdr[] ="#! /bin/bash\n\n" ;char program[PATH_MAX ];
@@ -474,15 +475,15 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc)close (fd); } -static int kvm_cmd_run_init (int argc,const char **argv) +static struct kvm *kvm_cmd_run_init (int argc,const char **argv) {static char real_cmdline[2048 ], default_name[20 ];unsigned int nr_online_cpus;struct sigaction sa; +struct kvm *kvm =kvm__new (); - kvm =kvm__new ();if (IS_ERR (kvm)) -return PTR_ERR (kvm); +return kvm; sa.sa_flags =SA_SIGINFO ; sa.sa_sigaction = handle_sigalrm;
@@ -502,7 +503,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (strcmp (argv[0 ],"--" ) ==0 ) {if (kvm_run_wrapper ==KVM_RUN_SANDBOX ) { kvm->cfg.sandbox =DEFAULT_SANDBOX_FILENAME ; -kvm_run_write_sandbox_cmd (argv+1 , argc-1 ); +kvm_run_write_sandbox_cmd (kvm, argv+1 , argc-1 );break ; } }
@@ -513,7 +514,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) "%s\n", argv[0]);usage_with_options (run_usage, options);free (kvm); -return -EINVAL ; +return ERR_PTR (-EINVAL ); }if (kvm_run_wrapper ==KVM_RUN_SANDBOX ) {/*
@@ -521,7 +522,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)* sandbox command */ kvm->cfg.sandbox =DEFAULT_SANDBOX_FILENAME ; -kvm_run_write_sandbox_cmd (argv, argc); +kvm_run_write_sandbox_cmd (kvm, argv, argc); }else {/* * first unhandled parameter is treated as a kernel
@@ -542,7 +543,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (!kvm->cfg.kernel_filename) {kernel_usage_with_options (); -return -EINVAL ; +return ERR_PTR (-EINVAL ); } kvm->cfg.vmlinux_filename =find_vmlinux ();
@@ -633,13 +634,13 @@ static int kvm_cmd_run_init(int argc, const char **argv)if (kvm->cfg.using_rootfs) {strcat (real_cmdline," root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p" );if (kvm->cfg.custom_rootfs) { -kvm_run_set_sandbox (); +kvm_run_set_sandbox (kvm);strcat (real_cmdline," init=/virt/init" );if (!kvm->cfg.no_dhcp)strcat (real_cmdline," ip=dhcp" ); -if (kvm_setup_guest_init ()) +if (kvm_setup_guest_init (kvm))die ("Failed to setup init for guest." ); } }else if (!strstr (real_cmdline,"root=" )) {
@@ -651,10 +652,12 @@ static int kvm_cmd_run_init(int argc, const char **argv)printf (" # %s run -k %s -m %Lu -c %d --name %s\n" ,KVM_BINARY_NAME , kvm->cfg.kernel_filename, kvm->cfg.ram_size /1024 /1024 , kvm->cfg.nrcpus, kvm->cfg.guest_name); -return init_list__init (kvm); +init_list__init (kvm); + +return kvm; } -static int kvm_cmd_run_work (void ) +static int kvm_cmd_run_work (struct kvm *kvm) {int i;void *ret =NULL ;
@@ -668,7 +671,7 @@ static int kvm_cmd_run_work(void)return pthread_join (kvm->cpus[0 ]->thread, &ret); } -static void kvm_cmd_run_exit (int guest_ret) +static void kvm_cmd_run_exit (struct kvm *kvm,int guest_ret) {compat__print_all_messages ();
@@ -680,14 +683,15 @@ static void kvm_cmd_run_exit(int guest_ret)int kvm_cmd_run (int argc,const char **argv,const char *prefix) { -int r, ret = -EFAULT ; +int ret = -EFAULT ; +struct kvm *kvm; - r =kvm_cmd_run_init (argc, argv); -if (r <0 ) -return r; + kvm =kvm_cmd_run_init (argc, argv); +if (IS_ERR (kvm)) +return PTR_ERR (kvm); - ret =kvm_cmd_run_work (); -kvm_cmd_run_exit (ret); + ret =kvm_cmd_run_work (kvm); +kvm_cmd_run_exit (kvm, ret);return ret; }
tools/kvm/hw/i8042.c +2 -2
@@ -347,8 +347,8 @@ int kbd__init(struct kvm *kvm)kbd_reset (); state.kvm = kvm; -ioport__register (I8042_DATA_REG , &kbd_ops,2 ,NULL ); -ioport__register (I8042_COMMAND_REG , &kbd_ops,2 ,NULL ); +ioport__register (kvm,I8042_DATA_REG , &kbd_ops,2 ,NULL ); +ioport__register (kvm,I8042_COMMAND_REG , &kbd_ops,2 ,NULL );return 0 ; }
tools/kvm/hw/pci-shmem.c +1 -1
@@ -362,7 +362,7 @@ int pci_shmem__init(struct kvm *kvm) pci_shmem_pci_device.irq_line = line;/* Register MMIO space for MSI-X */ - r =ioport__register (IOPORT_EMPTY , &shmem_pci__io_ops,IOPORT_SIZE ,NULL ); + r =ioport__register (kvm,IOPORT_EMPTY , &shmem_pci__io_ops,IOPORT_SIZE ,NULL );if (r <0 )return r; ivshmem_registers = (u16 )r;
tools/kvm/hw/rtc.c +5 -5
@@ -114,13 +114,13 @@ int rtc__init(struct kvm *kvm)int r =0 ;/* PORT 0070-007F - CMOS RAM/RTC (REAL TIME CLOCK) */ - r =ioport__register (0x0070 , &cmos_ram_index_ioport_ops,1 ,NULL ); + r =ioport__register (kvm,0x0070 , &cmos_ram_index_ioport_ops,1 ,NULL );if (r <0 )return r; - r =ioport__register (0x0071 , &cmos_ram_data_ioport_ops,1 ,NULL ); + r =ioport__register (kvm,0x0071 , &cmos_ram_data_ioport_ops,1 ,NULL );if (r <0 ) { -ioport__unregister (0x0071 ); +ioport__unregister (kvm,0x0071 );return r; }
@@ -131,8 +131,8 @@ dev_init(rtc__init);int rtc__exit (struct kvm *kvm) {/* PORT 0070-007F - CMOS RAM/RTC (REAL TIME CLOCK) */ -ioport__unregister (0x0070 ); -ioport__unregister (0x0071 ); +ioport__unregister (kvm,0x0070 ); +ioport__unregister (kvm,0x0071 );return 0 ; }
tools/kvm/hw/serial.c +4 -4
@@ -195,7 +195,7 @@ static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,while (term_readable (dev->id) && dev->rxcnt <FIFO_LEN ) { - c =term_getc (dev->id); + c =term_getc (kvm, dev->id);if (c <0 )break ;
@@ -403,7 +403,7 @@ static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *de {int r; - r =ioport__register (dev->iobase, &serial8250_ops,8 ,NULL ); + r =ioport__register (kvm, dev->iobase, &serial8250_ops,8 ,NULL );kvm__irq_line (kvm, dev->irq,0 );return r;
@@ -427,7 +427,7 @@ cleanup:for (j =0 ; j <= i; j++) {struct serial8250_device *dev = &devices[j]; -ioport__unregister (dev->iobase); +ioport__unregister (kvm, dev->iobase); }return r;
@@ -442,7 +442,7 @@ int serial8250__exit(struct kvm *kvm)for (i =0 ; i <ARRAY_SIZE (devices); i++) {struct serial8250_device *dev = &devices[i]; - r =ioport__unregister (dev->iobase); + r =ioport__unregister (kvm, dev->iobase);if (r <0 )return r; }
tools/kvm/hw/vesa.c +1 -1
@@ -60,7 +60,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)if (r <0 )return ERR_PTR (r); - r =ioport__register (IOPORT_EMPTY , &vesa_io_ops,IOPORT_SIZE ,NULL ); + r =ioport__register (kvm,IOPORT_EMPTY , &vesa_io_ops,IOPORT_SIZE ,NULL );if (r <0 )return ERR_PTR (r);
tools/kvm/include/kvm/brlock.h +8 -8
@@ -23,19 +23,19 @@DECLARE_RWSEM (brlock_sem); -#define br_read_lock () down_read(&brlock_sem); -#define br_read_unlock () up_read(&brlock_sem); +#define br_read_lock (kvm) down_read(&brlock_sem); +#define br_read_unlock (kvm) up_read(&brlock_sem); -#define br_write_lock () down_write(&brlock_sem); -#define br_write_unlock () up_write(&brlock_sem); +#define br_write_lock (kvm) down_write(&brlock_sem); +#define br_write_unlock (kvm) up_write(&brlock_sem);#else -#define br_read_lock () barrier() -#define br_read_unlock () barrier() +#define br_read_lock (kvm) barrier() +#define br_read_unlock (kvm) barrier() -#define br_write_lock () kvm__pause() -#define br_write_unlock () kvm__continue() +#define br_write_lock (kvm) kvm__pause(kvm) +#define br_write_unlock (kvm) kvm__continue(kvm)#endif #endif
tools/kvm/include/kvm/ioport.h +4 -3
@@ -29,10 +29,11 @@ struct ioport_operations {bool (*io_out)(struct ioport *ioport,struct kvm *kvm,u16 port,void *data,int size); }; -void ioport__setup_arch (void ); +void ioport__setup_arch (struct kvm *kvm); -int ioport__register (u16 port,struct ioport_operations *ops,int count,void *param); -int ioport__unregister (u16 port); +int ioport__register (struct kvm *kvm,u16 port,struct ioport_operations *ops, +int count,void *param); +int ioport__unregister (struct kvm *kvm,u16 port);int ioport__init (struct kvm *kvm);int ioport__exit (struct kvm *kvm);
tools/kvm/include/kvm/kvm.h +2 -2
@@ -81,8 +81,8 @@ int kvm__register_mmio(struct kvm *kvm, u64 phys_addr, u64 phys_addr_len, bool cvoid (*mmio_fn)(u64 addr,u8 *data,u32 len,u8 is_write,void *ptr), void *ptr);bool kvm__deregister_mmio (struct kvm *kvm,u64 phys_addr); -void kvm__pause (void ); -void kvm__continue (void ); +void kvm__pause (struct kvm *kvm); +void kvm__continue (struct kvm *kvm);void kvm__notify_paused (void );int kvm__get_sock_by_instance (const char *name);int kvm__enumerate_instances (int (*callback)(const char *name,int pid));
tools/kvm/include/kvm/term.h +2 -2
@@ -11,9 +11,9 @@#define CONSOLE_HV 3int term_putc_iov (struct iovec *iov,int iovcnt,int term); -int term_getc_iov (struct iovec *iov,int iovcnt,int term); +int term_getc_iov (struct kvm *kvm,struct iovec *iov,int iovcnt,int term);int term_putc (char *addr,int cnt,int term); -int term_getc (int term); +int term_getc (struct kvm *kvm,int term);bool term_readable (int term);void term_set_tty (int term);
tools/kvm/ioport.c +8 -8
@@ -55,12 +55,12 @@ static void ioport_remove(struct rb_root *root, struct ioport *data)rb_int_erase (root, &data->node); } -int ioport__register (u16 port,struct ioport_operations *ops,int count,void *param) +int ioport__register (struct kvm *kvm,u16 port,struct ioport_operations *ops,int count,void *param) {struct ioport *entry;int r; -br_write_lock (); +br_write_lock (kvm);if (port ==IOPORT_EMPTY ) port =ioport__find_free_port ();
@@ -83,20 +83,20 @@ int ioport__register(u16 port, struct ioport_operations *ops, int count, void *p r =ioport_insert (&ioport_tree, entry);if (r <0 ) {free (entry); -br_write_unlock (); +br_write_unlock (kvm);return r; } -br_write_unlock (); +br_write_unlock (kvm);return port; } -int ioport__unregister (u16 port) +int ioport__unregister (struct kvm *kvm,u16 port) {struct ioport *entry;int r; -br_write_lock (); +br_write_lock (kvm); r = -ENOENT ; entry =ioport_search (&ioport_tree, port);
@@ -110,7 +110,7 @@ int ioport__unregister(u16 port) r =0 ; done: -br_write_unlock (); +br_write_unlock (kvm);return r; }
@@ -184,7 +184,7 @@ error:int ioport__init (struct kvm *kvm) { -ioport__setup_arch (); +ioport__setup_arch (kvm);return 0 ; }
tools/kvm/kvm-cpu.c +1 -1
@@ -41,7 +41,7 @@ static void kvm_cpu_signal_handler(int signum)if (signum ==SIGKVMEXIT ) {if (current_kvm_cpu && current_kvm_cpu->is_running) { current_kvm_cpu->is_running = false; -kvm__continue (); +kvm__continue (current_kvm_cpu->kvm); } }else if (signum ==SIGKVMPAUSE ) { current_kvm_cpu->paused =1 ;
tools/kvm/kvm-ipc.c +2 -2
@@ -321,11 +321,11 @@ static void handle_pause(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg)if (type ==KVM_IPC_RESUME && is_paused) { kvm->vm_state =KVM_VMSTATE_RUNNING ; -kvm__continue (); +kvm__continue (kvm); }else if (type ==KVM_IPC_PAUSE && !is_paused) { kvm->vm_state =KVM_VMSTATE_PAUSED ;ioctl (kvm->vm_fd,KVM_KVMCLOCK_CTRL ); -kvm__pause (); +kvm__pause (kvm); }else {return ; }
tools/kvm/kvm.c +2 -4
@@ -55,8 +55,6 @@ const char *kvm_exit_reasons[] = {#endif }; -extern struct kvm *kvm; -extern struct kvm_cpu **kvm_cpus;static int pause_event;static DEFINE_MUTEX (pause_lock);extern struct kvm_ext kvm_req_ext[];
@@ -413,7 +411,7 @@ void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size) } } -void kvm__pause (void ) +void kvm__pause (struct kvm *kvm) {int i, paused_vcpus =0 ;
@@ -439,7 +437,7 @@ void kvm__pause(void)close (pause_event); } -void kvm__continue (void ) +void kvm__continue (struct kvm *kvm) {/* Check if the guest is running */ if (!kvm->cpus[0 ] || kvm->cpus[0 ]->thread ==0 )
tools/kvm/mmio.c +5 -5
@@ -87,9 +87,9 @@ int kvm__register_mmio(struct kvm *kvm, u64 phys_addr, u64 phys_addr_len, bool creturn -errno; } } -br_write_lock (); +br_write_lock (kvm); ret =mmio_insert (&mmio_tree, mmio); -br_write_unlock (); +br_write_unlock (kvm);return ret; }
@@ -99,10 +99,10 @@ bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr)struct mmio_mapping *mmio;struct kvm_coalesced_mmio_zone zone; -br_write_lock (); +br_write_lock (kvm); mmio =mmio_search_single (&mmio_tree, phys_addr);if (mmio ==NULL ) { -br_write_unlock (); +br_write_unlock (kvm);return false; }
@@ -113,7 +113,7 @@ bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr)ioctl (kvm->vm_fd,KVM_UNREGISTER_COALESCED_MMIO , &zone);rb_int_erase (&mmio_tree, &mmio->node); -br_write_unlock (); +br_write_unlock (kvm);free (mmio);return true;
tools/kvm/pci.c +5 -5
@@ -191,13 +191,13 @@ int pci__init(struct kvm *kvm) {int r; - r =ioport__register (PCI_CONFIG_DATA +0 , &pci_config_data_ops,4 ,NULL ); + r =ioport__register (kvm,PCI_CONFIG_DATA +0 , &pci_config_data_ops,4 ,NULL );if (r <0 )return r; - r =ioport__register (PCI_CONFIG_ADDRESS +0 , &pci_config_address_ops,4 ,NULL ); + r =ioport__register (kvm,PCI_CONFIG_ADDRESS +0 , &pci_config_address_ops,4 ,NULL );if (r <0 ) { -ioport__unregister (PCI_CONFIG_DATA ); +ioport__unregister (kvm,PCI_CONFIG_DATA );return r; }
@@ -207,8 +207,8 @@ base_init(pci__init);int pci__exit (struct kvm *kvm) { -ioport__unregister (PCI_CONFIG_DATA ); -ioport__unregister (PCI_CONFIG_ADDRESS ); +ioport__unregister (kvm,PCI_CONFIG_DATA ); +ioport__unregister (kvm,PCI_CONFIG_ADDRESS );return 0 ; }
tools/kvm/term.c +3 -4
@@ -17,7 +17,6 @@#define TERM_FD_IN 0#define TERM_FD_OUT 1 -extern struct kvm *kvm;static struct termios orig_term;int term_escape_char =0x01 ;/* ctrl-a is used for escape */
@@ -25,7 +24,7 @@ bool term_got_escape = false;int term_fds[4 ][2 ]; -int term_getc (int term) +int term_getc (struct kvm *kvm,int term) {unsigned char c;
@@ -61,11 +60,11 @@ int term_putc(char *addr, int cnt, int term)return cnt; } -int term_getc_iov (struct iovec *iov,int iovcnt,int term) +int term_getc_iov (struct kvm *kvm,struct iovec *iov,int iovcnt,int term) {int c; - c =term_getc (term); + c =term_getc (kvm, term);if (c <0 )return 0 ;
tools/kvm/virtio/balloon.c +2 -3
@@ -48,7 +48,6 @@ struct bln_dev { };static struct bln_dev bdev; -extern struct kvm *kvm;static int compat_id =-1 ;static bool virtio_bln_do_io_request (struct kvm *kvm,struct bln_dev *bdev,struct virt_queue *queue)
@@ -125,7 +124,7 @@ static void virtio_bln_do_io(struct kvm *kvm, void *param) } } -static int virtio_bln__collect_stats (void ) +static int virtio_bln__collect_stats (struct kvm *kvm) {u64 tmp;
@@ -146,7 +145,7 @@ static void virtio_bln__print_stats(struct kvm *kvm, int fd, u32 type, u32 len,if (WARN_ON (type !=KVM_IPC_STAT || len))return ; -if (virtio_bln__collect_stats () <0 ) +if (virtio_bln__collect_stats (kvm) <0 )return ; r =write (fd, bdev.stats,sizeof (bdev.stats));
tools/kvm/virtio/console.c +1 -1
@@ -71,7 +71,7 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *parif (term_readable (0 ) &&virt_queue__available (vq)) { head =virt_queue__get_iov (vq, iov, &out, &in, kvm); - len =term_getc_iov (iov, in,0 ); + len =term_getc_iov (kvm, iov, in,0 );virt_queue__set_used_elem (vq, head, len); cdev.vdev.ops->signal_vq (kvm, &cdev.vdev, vq - cdev.vqs); }
tools/kvm/virtio/net.c +5 -7
@@ -33,8 +33,6 @@struct net_dev ; -extern struct kvm *kvm; -struct net_dev_operations {int (*rx)(struct iovec *iov,u16 in,struct net_dev *ndev);int (*tx)(struct iovec *iov,u16 in,struct net_dev *ndev);
@@ -495,8 +493,8 @@ static inline void str_to_mac(const char *str, char *mac)sscanf (str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx" , mac, mac+1 , mac+2 , mac+3 , mac+4 , mac+5 ); } -static int set_net_param (struct virtio_net_params *p,const char *param, -const char *val) +static int set_net_param (struct kvm *kvm,struct virtio_net_params *p, +const char *param,const char *val) {if (strcmp (param,"guest_mac" ) ==0 ) {str_to_mac (val, p->guest_mac);
@@ -561,7 +559,7 @@ int netdev_parser(const struct option *opt, const char *arg, int unset)if (on_cmd) { cmd = cur; }else { -if (set_net_param (&p, cmd, cur) <0 ) +if (set_net_param (kvm, &p, cmd, cur) <0 ) goto done; } on_cmd = !on_cmd;
@@ -619,10 +617,10 @@ static int virtio_net__init_one(struct virtio_net_params *params) }if (params->trans &&strcmp (params->trans,"mmio" ) ==0 ) -virtio_init (kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, +virtio_init (params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,VIRTIO_MMIO ,PCI_DEVICE_ID_VIRTIO_NET ,VIRTIO_ID_NET ,PCI_CLASS_NET );else -virtio_init (kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, +virtio_init (params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,VIRTIO_PCI ,PCI_DEVICE_ID_VIRTIO_NET ,VIRTIO_ID_NET ,PCI_CLASS_NET );if (params->vhost)
tools/kvm/virtio/pci.c +3 -3
@@ -307,7 +307,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, vpci->dev = dev; vpci->msix_io_block =pci_get_io_space_block (PCI_IO_SIZE *2 ); - r =ioport__register (IOPORT_EMPTY , &virtio_pci__io_ops,IOPORT_SIZE , vdev); + r =ioport__register (kvm,IOPORT_EMPTY , &virtio_pci__io_ops,IOPORT_SIZE , vdev);if (r <0 )return r;
@@ -379,7 +379,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, free_mmio:kvm__deregister_mmio (kvm, vpci->msix_io_block); free_ioport: -ioport__unregister (vpci->base_addr); +ioport__unregister (kvm, vpci->base_addr);return r; }
@@ -389,7 +389,7 @@ int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev)int i;kvm__deregister_mmio (kvm, vpci->msix_io_block); -ioport__unregister (vpci->base_addr); +ioport__unregister (kvm, vpci->base_addr);for (i =0 ; i <VIRTIO_PCI_MAX_VQ ; i++)ioeventfd__del_event (vpci->base_addr +VIRTIO_PCI_QUEUE_NOTIFY , i);
tools/kvm/x86/ioport.c +10 -10
@@ -46,32 +46,32 @@ static struct ioport_operations dummy_write_only_ioport_ops = { .io_out = dummy_io_out, }; -void ioport__setup_arch (void ) +void ioport__setup_arch (struct kvm *kvm) {/* Legacy ioport setup */ /* 0x0020 - 0x003F - 8259A PIC 1 */ -ioport__register (0x0020 , &dummy_read_write_ioport_ops,2 ,NULL ); +ioport__register (kvm,0x0020 , &dummy_read_write_ioport_ops,2 ,NULL );/* PORT 0040-005F - PIT - PROGRAMMABLE INTERVAL TIMER (8253, 8254) */ -ioport__register (0x0040 , &dummy_read_write_ioport_ops,4 ,NULL ); +ioport__register (kvm,0x0040 , &dummy_read_write_ioport_ops,4 ,NULL );/* 0x00A0 - 0x00AF - 8259A PIC 2 */ -ioport__register (0x00A0 , &dummy_read_write_ioport_ops,2 ,NULL ); +ioport__register (kvm,0x00A0 , &dummy_read_write_ioport_ops,2 ,NULL );/* PORT 00E0-00EF are 'motherboard specific' so we use them for our internal debugging purposes. */ -ioport__register (IOPORT_DBG , &debug_ops,1 ,NULL ); +ioport__register (kvm,IOPORT_DBG , &debug_ops,1 ,NULL );/* PORT 00ED - DUMMY PORT FOR DELAY??? */ -ioport__register (0x00ED , &dummy_write_only_ioport_ops,1 ,NULL ); +ioport__register (kvm,0x00ED , &dummy_write_only_ioport_ops,1 ,NULL );/* 0x00F0 - 0x00FF - Math co-processor */ -ioport__register (0x00F0 , &dummy_write_only_ioport_ops,2 ,NULL ); +ioport__register (kvm,0x00F0 , &dummy_write_only_ioport_ops,2 ,NULL );/* PORT 03D4-03D5 - COLOR VIDEO - CRT CONTROL REGISTERS */ -ioport__register (0x03D4 , &dummy_read_write_ioport_ops,1 ,NULL ); -ioport__register (0x03D5 , &dummy_write_only_ioport_ops,1 ,NULL ); +ioport__register (kvm,0x03D4 , &dummy_read_write_ioport_ops,1 ,NULL ); +ioport__register (kvm,0x03D5 , &dummy_write_only_ioport_ops,1 ,NULL ); -ioport__register (0x402 , &seabios_debug_ops,1 ,NULL ); +ioport__register (kvm,0x402 , &seabios_debug_ops,1 ,NULL ); }
Signature · 1 line
1.7.12
Quoted 9 lines · Sasha Levin
This patch series is mostly about cleanups:
- Clean all the global variables we have to store configuration options.
- Remove externed config options between objects.
- Adding several exit routines to clean up on exit.
- Remove the global 'kvm' object.
- Contain arch specific init/exit calls within the corresponding non-arch specific code instead of the global init.
Quoted 6 lines
This patch series also adds a method to call init/exit functions automatically after we've finished intializing config options.
Doing so clears out a big chunk of repetetive code in our builtin-run init function, and makes adding new modules easier since there's now no need to link them to builtin-run.
I'm getting an instant SIGSEGV for "vm run" with these patches:
[penberg@tux kvm]$ gdb --args ./lkvm run
GNU gdb (GDB) Fedora (7.3.50.20110722-13.fc16)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/penberg/linux/tools/kvm/lkvm...done. (gdb) r
Starting program: /home/penberg/linux/tools/kvm/lkvm run
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
# lkvm run -k ../../arch/x86/boot/bzImage -m 448 -c 4 --name guest-6703 [New Thread 0x7fffdbfd5700 (LWP 6707)] [New Thread 0x7fffdb7d4700 (LWP 6708)] warning: Range for type (null) has invalid bounds 0..-109
Program received signal SIGSEGV, Segmentation fault. 0x000000000040fb14 in kvm__pause (kvm=0x712010) at kvm.c:419
419 if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0)
Missing separate debuginfos, use: debuginfo-install
SDL-1.2.14-13.fc16.x86_64 glibc-2.14.90-24.fc16.7.x86_64 zlib-1.2.5-6.fc16.x86_64
(gdb) bt
#0 0x000000000040fb14 in kvm__pause (kvm=0x712010) at kvm.c:419 #1 0x000000000040f898 in ioport__register (kvm=0x712010, port=3324,
ops=0x638ce0, count=4, param=0x0) at ioport.c:63
#2 0x000000000040f6c4 in pci__init (kvm=0x712010) at pci.c:194 #3 0x0000000000414f94 in init_list__init (kvm=0x712010) at util/init.c:41 #4 0x000000000041dc4e in kvm_cmd_run_init.2726 (argc=0,
argv=0x7fffffffe168) at builtin-run.c:655
#5 0x0000000000419d8e in kvm_cmd_run (argc=0, argv=0x7fffffffe168,
prefix=0x0) at builtin-run.c:689
#6 0x00000000004198ce in handle_command (command=0x6396e0, argc=1,
argv=0x7fffffffe160) at kvm-cmd.c:84
#7 0x0000000000419797 in handle_kvm_command.5445 (argc=1,
argv=0x7fffffffe160) at main.c:11
#8 0x000000000042753e in main (argc=2, argv=0x7fffffffe158) at main.c:18