@@ -2381,6 +2381,109 @@ uint32_t kvm_dirty_ring_size(void)
2381
2381
return kvm_state -> kvm_dirty_ring_size ;
2382
2382
}
2383
2383
2384
+ static int do_kvm_create_vm (MachineState * ms , int type )
2385
+ {
2386
+ KVMState * s ;
2387
+ int ret ;
2388
+
2389
+ s = KVM_STATE (ms -> accelerator );
2390
+
2391
+ do {
2392
+ ret = kvm_ioctl (s , KVM_CREATE_VM , type );
2393
+ } while (ret == - EINTR );
2394
+
2395
+ if (ret < 0 ) {
2396
+ error_report ("ioctl(KVM_CREATE_VM) failed: %s" , strerror (- ret ));
2397
+
2398
+ #ifdef TARGET_S390X
2399
+ if (ret == - EINVAL ) {
2400
+ error_printf ("Host kernel setup problem detected."
2401
+ " Please verify:\n" );
2402
+ error_printf ("- for kernels supporting the"
2403
+ " switch_amode or user_mode parameters, whether" );
2404
+ error_printf (" user space is running in primary address space\n" );
2405
+ error_printf ("- for kernels supporting the vm.allocate_pgste"
2406
+ " sysctl, whether it is enabled\n" );
2407
+ }
2408
+ #elif defined(TARGET_PPC )
2409
+ if (ret == - EINVAL ) {
2410
+ error_printf ("PPC KVM module is not loaded. Try modprobe kvm_%s.\n" ,
2411
+ (type == 2 ) ? "pr" : "hv" );
2412
+ }
2413
+ #endif
2414
+ }
2415
+
2416
+ return ret ;
2417
+ }
2418
+
2419
+ static int find_kvm_machine_type (MachineState * ms )
2420
+ {
2421
+ MachineClass * mc = MACHINE_GET_CLASS (ms );
2422
+ int type ;
2423
+
2424
+ if (object_property_find (OBJECT (current_machine ), "kvm-type" )) {
2425
+ g_autofree char * kvm_type ;
2426
+ kvm_type = object_property_get_str (OBJECT (current_machine ),
2427
+ "kvm-type" ,
2428
+ & error_abort );
2429
+ type = mc -> kvm_type (ms , kvm_type );
2430
+ } else if (mc -> kvm_type ) {
2431
+ type = mc -> kvm_type (ms , NULL );
2432
+ } else {
2433
+ type = kvm_arch_get_default_type (ms );
2434
+ }
2435
+ return type ;
2436
+ }
2437
+
2438
+ static int kvm_setup_dirty_ring (KVMState * s )
2439
+ {
2440
+ uint64_t dirty_log_manual_caps ;
2441
+ int ret ;
2442
+
2443
+ /*
2444
+ * Enable KVM dirty ring if supported, otherwise fall back to
2445
+ * dirty logging mode
2446
+ */
2447
+ ret = kvm_dirty_ring_init (s );
2448
+ if (ret < 0 ) {
2449
+ return ret ;
2450
+ }
2451
+
2452
+ /*
2453
+ * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is not needed when dirty ring is
2454
+ * enabled. More importantly, KVM_DIRTY_LOG_INITIALLY_SET will assume no
2455
+ * page is wr-protected initially, which is against how kvm dirty ring is
2456
+ * usage - kvm dirty ring requires all pages are wr-protected at the very
2457
+ * beginning. Enabling this feature for dirty ring causes data corruption.
2458
+ *
2459
+ * TODO: Without KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 and kvm clear dirty log,
2460
+ * we may expect a higher stall time when starting the migration. In the
2461
+ * future we can enable KVM_CLEAR_DIRTY_LOG to work with dirty ring too:
2462
+ * instead of clearing dirty bit, it can be a way to explicitly wr-protect
2463
+ * guest pages.
2464
+ */
2465
+ if (!s -> kvm_dirty_ring_size ) {
2466
+ dirty_log_manual_caps =
2467
+ kvm_check_extension (s , KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 );
2468
+ dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
2469
+ KVM_DIRTY_LOG_INITIALLY_SET );
2470
+ s -> manual_dirty_log_protect = dirty_log_manual_caps ;
2471
+ if (dirty_log_manual_caps ) {
2472
+ ret = kvm_vm_enable_cap (s , KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 , 0 ,
2473
+ dirty_log_manual_caps );
2474
+ if (ret ) {
2475
+ warn_report ("Trying to enable capability %" PRIu64 " of "
2476
+ "KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. "
2477
+ "Falling back to the legacy mode. " ,
2478
+ dirty_log_manual_caps );
2479
+ s -> manual_dirty_log_protect = 0 ;
2480
+ }
2481
+ }
2482
+ }
2483
+
2484
+ return 0 ;
2485
+ }
2486
+
2384
2487
static int kvm_init (MachineState * ms )
2385
2488
{
2386
2489
MachineClass * mc = MACHINE_GET_CLASS (ms );
@@ -2400,7 +2503,6 @@ static int kvm_init(MachineState *ms)
2400
2503
const KVMCapabilityInfo * missing_cap ;
2401
2504
int ret ;
2402
2505
int type ;
2403
- uint64_t dirty_log_manual_caps ;
2404
2506
2405
2507
qemu_mutex_init (& kml_slots_lock );
2406
2508
@@ -2423,7 +2525,7 @@ static int kvm_init(MachineState *ms)
2423
2525
QLIST_INIT (& s -> kvm_parked_vcpus );
2424
2526
s -> fd = qemu_open_old (s -> device ?: "/dev/kvm" , O_RDWR );
2425
2527
if (s -> fd == -1 ) {
2426
- fprintf ( stderr , "Could not access KVM kernel module: %m\n " );
2528
+ error_report ( "Could not access KVM kernel module: %m" );
2427
2529
ret = - errno ;
2428
2530
goto err ;
2429
2531
}
@@ -2433,13 +2535,13 @@ static int kvm_init(MachineState *ms)
2433
2535
if (ret >= 0 ) {
2434
2536
ret = - EINVAL ;
2435
2537
}
2436
- fprintf ( stderr , "kvm version too old\n " );
2538
+ error_report ( "kvm version too old" );
2437
2539
goto err ;
2438
2540
}
2439
2541
2440
2542
if (ret > KVM_API_VERSION ) {
2441
2543
ret = - EINVAL ;
2442
- fprintf ( stderr , "kvm version not supported\n " );
2544
+ error_report ( "kvm version not supported" );
2443
2545
goto err ;
2444
2546
}
2445
2547
@@ -2463,49 +2565,14 @@ static int kvm_init(MachineState *ms)
2463
2565
}
2464
2566
s -> as = g_new0 (struct KVMAs , s -> nr_as );
2465
2567
2466
- if (object_property_find (OBJECT (current_machine ), "kvm-type" )) {
2467
- g_autofree char * kvm_type = object_property_get_str (OBJECT (current_machine ),
2468
- "kvm-type" ,
2469
- & error_abort );
2470
- type = mc -> kvm_type (ms , kvm_type );
2471
- } else if (mc -> kvm_type ) {
2472
- type = mc -> kvm_type (ms , NULL );
2473
- } else {
2474
- type = kvm_arch_get_default_type (ms );
2475
- }
2476
-
2568
+ type = find_kvm_machine_type (ms );
2477
2569
if (type < 0 ) {
2478
2570
ret = - EINVAL ;
2479
2571
goto err ;
2480
2572
}
2481
2573
2482
- do {
2483
- ret = kvm_ioctl (s , KVM_CREATE_VM , type );
2484
- } while (ret == - EINTR );
2485
-
2574
+ ret = do_kvm_create_vm (ms , type );
2486
2575
if (ret < 0 ) {
2487
- fprintf (stderr , "ioctl(KVM_CREATE_VM) failed: %d %s\n" , - ret ,
2488
- strerror (- ret ));
2489
-
2490
- #ifdef TARGET_S390X
2491
- if (ret == - EINVAL ) {
2492
- fprintf (stderr ,
2493
- "Host kernel setup problem detected. Please verify:\n" );
2494
- fprintf (stderr , "- for kernels supporting the switch_amode or"
2495
- " user_mode parameters, whether\n" );
2496
- fprintf (stderr ,
2497
- " user space is running in primary address space\n" );
2498
- fprintf (stderr ,
2499
- "- for kernels supporting the vm.allocate_pgste sysctl, "
2500
- "whether it is enabled\n" );
2501
- }
2502
- #elif defined(TARGET_PPC )
2503
- if (ret == - EINVAL ) {
2504
- fprintf (stderr ,
2505
- "PPC KVM module is not loaded. Try modprobe kvm_%s.\n" ,
2506
- (type == 2 ) ? "pr" : "hv" );
2507
- }
2508
- #endif
2509
2576
goto err ;
2510
2577
}
2511
2578
@@ -2522,9 +2589,9 @@ static int kvm_init(MachineState *ms)
2522
2589
nc -> name , nc -> num , soft_vcpus_limit );
2523
2590
2524
2591
if (nc -> num > hard_vcpus_limit ) {
2525
- fprintf ( stderr , "Number of %s cpus requested (%d) exceeds "
2526
- "the maximum cpus supported by KVM (%d)\n " ,
2527
- nc -> name , nc -> num , hard_vcpus_limit );
2592
+ error_report ( "Number of %s cpus requested (%d) exceeds "
2593
+ "the maximum cpus supported by KVM (%d)" ,
2594
+ nc -> name , nc -> num , hard_vcpus_limit );
2528
2595
exit (1 );
2529
2596
}
2530
2597
}
@@ -2538,56 +2605,20 @@ static int kvm_init(MachineState *ms)
2538
2605
}
2539
2606
if (missing_cap ) {
2540
2607
ret = - EINVAL ;
2541
- fprintf ( stderr , "kvm does not support %s\n%s" ,
2542
- missing_cap -> name , upgrade_note );
2608
+ error_report ( "kvm does not support %s" , missing_cap -> name );
2609
+ error_printf ( "%s" , upgrade_note );
2543
2610
goto err ;
2544
2611
}
2545
2612
2546
2613
s -> coalesced_mmio = kvm_check_extension (s , KVM_CAP_COALESCED_MMIO );
2547
2614
s -> coalesced_pio = s -> coalesced_mmio &&
2548
2615
kvm_check_extension (s , KVM_CAP_COALESCED_PIO );
2549
2616
2550
- /*
2551
- * Enable KVM dirty ring if supported, otherwise fall back to
2552
- * dirty logging mode
2553
- */
2554
- ret = kvm_dirty_ring_init (s );
2617
+ ret = kvm_setup_dirty_ring (s );
2555
2618
if (ret < 0 ) {
2556
2619
goto err ;
2557
2620
}
2558
2621
2559
- /*
2560
- * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is not needed when dirty ring is
2561
- * enabled. More importantly, KVM_DIRTY_LOG_INITIALLY_SET will assume no
2562
- * page is wr-protected initially, which is against how kvm dirty ring is
2563
- * usage - kvm dirty ring requires all pages are wr-protected at the very
2564
- * beginning. Enabling this feature for dirty ring causes data corruption.
2565
- *
2566
- * TODO: Without KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 and kvm clear dirty log,
2567
- * we may expect a higher stall time when starting the migration. In the
2568
- * future we can enable KVM_CLEAR_DIRTY_LOG to work with dirty ring too:
2569
- * instead of clearing dirty bit, it can be a way to explicitly wr-protect
2570
- * guest pages.
2571
- */
2572
- if (!s -> kvm_dirty_ring_size ) {
2573
- dirty_log_manual_caps =
2574
- kvm_check_extension (s , KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 );
2575
- dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
2576
- KVM_DIRTY_LOG_INITIALLY_SET );
2577
- s -> manual_dirty_log_protect = dirty_log_manual_caps ;
2578
- if (dirty_log_manual_caps ) {
2579
- ret = kvm_vm_enable_cap (s , KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 , 0 ,
2580
- dirty_log_manual_caps );
2581
- if (ret ) {
2582
- warn_report ("Trying to enable capability %" PRIu64 " of "
2583
- "KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. "
2584
- "Falling back to the legacy mode. " ,
2585
- dirty_log_manual_caps );
2586
- s -> manual_dirty_log_protect = 0 ;
2587
- }
2588
- }
2589
- }
2590
-
2591
2622
#ifdef KVM_CAP_VCPU_EVENTS
2592
2623
s -> vcpu_events = kvm_check_extension (s , KVM_CAP_VCPU_EVENTS );
2593
2624
#endif
@@ -2762,9 +2793,15 @@ void kvm_flush_coalesced_mmio_buffer(void)
2762
2793
static void do_kvm_cpu_synchronize_state (CPUState * cpu , run_on_cpu_data arg )
2763
2794
{
2764
2795
if (!cpu -> vcpu_dirty && !kvm_state -> guest_state_protected ) {
2765
- int ret = kvm_arch_get_registers (cpu );
2796
+ Error * err = NULL ;
2797
+ int ret = kvm_arch_get_registers (cpu , & err );
2766
2798
if (ret ) {
2767
- error_report ("Failed to get registers: %s" , strerror (- ret ));
2799
+ if (err ) {
2800
+ error_reportf_err (err , "Failed to synchronize CPU state: " );
2801
+ } else {
2802
+ error_report ("Failed to get registers: %s" , strerror (- ret ));
2803
+ }
2804
+
2768
2805
cpu_dump_state (cpu , stderr , CPU_DUMP_CODE );
2769
2806
vm_stop (RUN_STATE_INTERNAL_ERROR );
2770
2807
}
@@ -2782,9 +2819,15 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
2782
2819
2783
2820
static void do_kvm_cpu_synchronize_post_reset (CPUState * cpu , run_on_cpu_data arg )
2784
2821
{
2785
- int ret = kvm_arch_put_registers (cpu , KVM_PUT_RESET_STATE );
2822
+ Error * err = NULL ;
2823
+ int ret = kvm_arch_put_registers (cpu , KVM_PUT_RESET_STATE , & err );
2786
2824
if (ret ) {
2787
- error_report ("Failed to put registers after reset: %s" , strerror (- ret ));
2825
+ if (err ) {
2826
+ error_reportf_err (err , "Restoring resisters after reset: " );
2827
+ } else {
2828
+ error_report ("Failed to put registers after reset: %s" ,
2829
+ strerror (- ret ));
2830
+ }
2788
2831
cpu_dump_state (cpu , stderr , CPU_DUMP_CODE );
2789
2832
vm_stop (RUN_STATE_INTERNAL_ERROR );
2790
2833
}
@@ -2799,9 +2842,15 @@ void kvm_cpu_synchronize_post_reset(CPUState *cpu)
2799
2842
2800
2843
static void do_kvm_cpu_synchronize_post_init (CPUState * cpu , run_on_cpu_data arg )
2801
2844
{
2802
- int ret = kvm_arch_put_registers (cpu , KVM_PUT_FULL_STATE );
2845
+ Error * err = NULL ;
2846
+ int ret = kvm_arch_put_registers (cpu , KVM_PUT_FULL_STATE , & err );
2803
2847
if (ret ) {
2804
- error_report ("Failed to put registers after init: %s" , strerror (- ret ));
2848
+ if (err ) {
2849
+ error_reportf_err (err , "Putting registers after init: " );
2850
+ } else {
2851
+ error_report ("Failed to put registers after init: %s" ,
2852
+ strerror (- ret ));
2853
+ }
2805
2854
exit (1 );
2806
2855
}
2807
2856
@@ -2991,10 +3040,15 @@ int kvm_cpu_exec(CPUState *cpu)
2991
3040
MemTxAttrs attrs ;
2992
3041
2993
3042
if (cpu -> vcpu_dirty ) {
2994
- ret = kvm_arch_put_registers (cpu , KVM_PUT_RUNTIME_STATE );
3043
+ Error * err = NULL ;
3044
+ ret = kvm_arch_put_registers (cpu , KVM_PUT_RUNTIME_STATE , & err );
2995
3045
if (ret ) {
2996
- error_report ("Failed to put registers after init: %s" ,
2997
- strerror (- ret ));
3046
+ if (err ) {
3047
+ error_reportf_err (err , "Putting registers after init: " );
3048
+ } else {
3049
+ error_report ("Failed to put registers after init: %s" ,
3050
+ strerror (- ret ));
3051
+ }
2998
3052
ret = -1 ;
2999
3053
break ;
3000
3054
}
0 commit comments