@@ -349,71 +349,73 @@ int32_t create_vcpu(uint16_t pcpu_id, struct acrn_vm *vm, struct acrn_vcpu **rtn
349
349
* vm->hw.created_vcpus++;
350
350
*/
351
351
vcpu_id = atomic_xadd16 (& vm -> hw .created_vcpus , 1U );
352
- if (vcpu_id >= CONFIG_MAX_VCPUS_PER_VM ) {
353
- vm -> hw .created_vcpus -- ;
354
- pr_err ("%s, vcpu id is invalid!\n" , __func__ );
355
- return - EINVAL ;
356
- }
357
- /* Allocate memory for VCPU */
358
- vcpu = & (vm -> hw .vcpu_array [vcpu_id ]);
359
- (void )memset ((void * )vcpu , 0U , sizeof (struct acrn_vcpu ));
352
+ if (vcpu_id < CONFIG_MAX_VCPUS_PER_VM ) {
353
+ /* Allocate memory for VCPU */
354
+ vcpu = & (vm -> hw .vcpu_array [vcpu_id ]);
355
+ (void )memset ((void * )vcpu , 0U , sizeof (struct acrn_vcpu ));
356
+
357
+ /* Initialize CPU ID for this VCPU */
358
+ vcpu -> vcpu_id = vcpu_id ;
359
+ vcpu -> pcpu_id = pcpu_id ;
360
+ per_cpu (ever_run_vcpu , pcpu_id ) = vcpu ;
361
+
362
+ /* Initialize the parent VM reference */
363
+ vcpu -> vm = vm ;
364
+
365
+ /* Initialize the virtual ID for this VCPU */
366
+ /* FIXME:
367
+ * We have assumption that we always destroys vcpus in one
368
+ * shot (like when vm is destroyed). If we need to support
369
+ * specific vcpu destroy on fly, this vcpu_id assignment
370
+ * needs revise.
371
+ */
360
372
361
- /* Initialize CPU ID for this VCPU */
362
- vcpu -> vcpu_id = vcpu_id ;
363
- vcpu -> pcpu_id = pcpu_id ;
364
- per_cpu (ever_run_vcpu , pcpu_id ) = vcpu ;
373
+ per_cpu (vcpu , pcpu_id ) = vcpu ;
365
374
366
- /* Initialize the parent VM reference */
367
- vcpu -> vm = vm ;
375
+ pr_info ("PCPU%d is working as VM%d VCPU%d, Role: %s" ,
376
+ vcpu -> pcpu_id , vcpu -> vm -> vm_id , vcpu -> vcpu_id ,
377
+ is_vcpu_bsp (vcpu ) ? "PRIMARY" : "SECONDARY" );
368
378
369
- /* Initialize the virtual ID for this VCPU */
370
- /* FIXME:
371
- * We have assumption that we always destroys vcpus in one
372
- * shot (like when vm is destroyed). If we need to support
373
- * specific vcpu destroy on fly, this vcpu_id assignment
374
- * needs revise.
375
- */
379
+ /*
380
+ * If the logical processor is in VMX non-root operation and
381
+ * the "enable VPID" VM-execution control is 1, the current VPID
382
+ * is the value of the VPID VM-execution control field in the VMCS.
383
+ *
384
+ * This assignment guarantees a unique non-zero per vcpu vpid in runtime.
385
+ */
386
+ vcpu -> arch .vpid = 1U + (vm -> vm_id * CONFIG_MAX_VCPUS_PER_VM ) + vcpu -> vcpu_id ;
376
387
377
- per_cpu (vcpu , pcpu_id ) = vcpu ;
388
+ /* Initialize exception field in VCPU context */
389
+ vcpu -> arch .exception_info .exception = VECTOR_INVALID ;
378
390
379
- pr_info ("PCPU%d is working as VM%d VCPU%d, Role: %s" ,
380
- vcpu -> pcpu_id , vcpu -> vm -> vm_id , vcpu -> vcpu_id ,
381
- is_vcpu_bsp (vcpu ) ? "PRIMARY" : "SECONDARY" );
391
+ /* Initialize cur context */
392
+ vcpu -> arch .cur_context = NORMAL_WORLD ;
382
393
383
- /*
384
- * If the logical processor is in VMX non-root operation and
385
- * the "enable VPID" VM-execution control is 1, the current VPID
386
- * is the value of the VPID VM-execution control field in the VMCS.
387
- *
388
- * This assignment guarantees a unique non-zero per vcpu vpid in runtime.
389
- */
390
- vcpu -> arch .vpid = 1U + (vm -> vm_id * CONFIG_MAX_VCPUS_PER_VM ) + vcpu -> vcpu_id ;
394
+ /* Create per vcpu vlapic */
395
+ vlapic_create (vcpu );
391
396
392
- /* Initialize exception field in VCPU context */
393
- vcpu -> arch .exception_info .exception = VECTOR_INVALID ;
397
+ if (!vm_hide_mtrr (vm )) {
398
+ init_vmtrr (vcpu );
399
+ }
394
400
395
- /* Initialize cur context */
396
- vcpu -> arch . cur_context = NORMAL_WORLD ;
401
+ /* Populate the return handle */
402
+ * rtn_vcpu_handle = vcpu ;
397
403
398
- /* Create per vcpu vlapic */
399
- vlapic_create (vcpu );
404
+ vcpu -> launched = false;
405
+ vcpu -> running = 0U ;
406
+ vcpu -> arch .nr_sipi = 0U ;
407
+ vcpu -> state = VCPU_INIT ;
400
408
401
- if (!vm_hide_mtrr (vm )) {
402
- init_vmtrr (vcpu );
409
+ reset_vcpu_regs (vcpu );
410
+ (void )memset ((void * )& vcpu -> req , 0U , sizeof (struct io_request ));
411
+ ret = 0 ;
412
+ } else {
413
+ vm -> hw .created_vcpus -= 1U ;
414
+ pr_err ("%s, vcpu id is invalid!\n" , __func__ );
415
+ ret = - EINVAL ;
403
416
}
404
417
405
- /* Populate the return handle */
406
- * rtn_vcpu_handle = vcpu ;
407
-
408
- vcpu -> launched = false;
409
- vcpu -> running = 0 ;
410
- vcpu -> arch .nr_sipi = 0 ;
411
- vcpu -> state = VCPU_INIT ;
412
-
413
- reset_vcpu_regs (vcpu );
414
- (void )memset (& vcpu -> req , 0U , sizeof (struct io_request ));
415
-
416
- return 0 ;
418
+ return ret ;
417
419
}
418
420
419
421
/*
@@ -561,31 +563,30 @@ void reset_vcpu(struct acrn_vcpu *vcpu)
561
563
ASSERT (vcpu -> state != VCPU_RUNNING ,
562
564
"reset vcpu when it's running" );
563
565
564
- if (vcpu -> state == VCPU_INIT )
565
- return ;
566
+ if (vcpu -> state != VCPU_INIT ) {
567
+ vcpu -> state = VCPU_INIT ;
566
568
567
- vcpu -> state = VCPU_INIT ;
569
+ vcpu -> launched = false;
570
+ vcpu -> running = 0U ;
571
+ vcpu -> arch .nr_sipi = 0U ;
568
572
569
- vcpu -> launched = false;
570
- vcpu -> running = 0 ;
571
- vcpu -> arch .nr_sipi = 0 ;
573
+ vcpu -> arch .exception_info .exception = VECTOR_INVALID ;
574
+ vcpu -> arch .cur_context = NORMAL_WORLD ;
575
+ vcpu -> arch .irq_window_enabled = false;
576
+ vcpu -> arch .inject_event_pending = false;
577
+ (void )memset ((void * )vcpu -> arch .vmcs , 0U , PAGE_SIZE );
572
578
573
- vcpu -> arch .exception_info .exception = VECTOR_INVALID ;
574
- vcpu -> arch .cur_context = NORMAL_WORLD ;
575
- vcpu -> arch .irq_window_enabled = false;
576
- vcpu -> arch .inject_event_pending = false;
577
- (void )memset ((void * )vcpu -> arch .vmcs , 0U , PAGE_SIZE );
578
-
579
- for (i = 0 ; i < NR_WORLD ; i ++ ) {
580
- (void )memset ((void * )(& vcpu -> arch .contexts [i ]), 0U ,
581
- sizeof (struct run_context ));
582
- }
583
- vcpu -> arch .cur_context = NORMAL_WORLD ;
579
+ for (i = 0 ; i < NR_WORLD ; i ++ ) {
580
+ (void )memset ((void * )(& vcpu -> arch .contexts [i ]), 0U ,
581
+ sizeof (struct run_context ));
582
+ }
583
+ vcpu -> arch .cur_context = NORMAL_WORLD ;
584
584
585
- vlapic = vcpu_vlapic (vcpu );
586
- vlapic_reset (vlapic );
585
+ vlapic = vcpu_vlapic (vcpu );
586
+ vlapic_reset (vlapic );
587
587
588
- reset_vcpu_regs (vcpu );
588
+ reset_vcpu_regs (vcpu );
589
+ }
589
590
}
590
591
591
592
void pause_vcpu (struct acrn_vcpu * vcpu , enum vcpu_state new_state )
@@ -699,35 +700,33 @@ static uint64_t build_stack_frame(struct acrn_vcpu *vcpu)
699
700
/* help function for vcpu create */
700
701
int32_t prepare_vcpu (struct acrn_vm * vm , uint16_t pcpu_id )
701
702
{
702
- int32_t ret = 0 ;
703
+ int32_t ret ;
703
704
struct acrn_vcpu * vcpu = NULL ;
704
705
char thread_name [16 ];
705
706
uint64_t orig_val , final_val ;
706
707
struct acrn_vm_config * conf ;
707
708
708
709
ret = create_vcpu (pcpu_id , vm , & vcpu );
709
- if (ret != 0 ) {
710
- return ret ;
711
- }
712
-
713
- set_pcpu_used (pcpu_id );
710
+ if (ret == 0 ) {
711
+ set_pcpu_used (pcpu_id );
712
+
713
+ /* Update CLOS for this CPU */
714
+ if (cat_cap_info .enabled ) {
715
+ conf = get_vm_config (vm -> vm_id );
716
+ orig_val = msr_read (MSR_IA32_PQR_ASSOC );
717
+ final_val = (orig_val & 0xffffffffUL ) | (((uint64_t )conf -> clos ) << 32UL );
718
+ msr_write_pcpu (MSR_IA32_PQR_ASSOC , final_val , pcpu_id );
719
+ }
714
720
715
- /* Update CLOS for this CPU */
716
- if (cat_cap_info .enabled ) {
717
- conf = get_vm_config (vm -> vm_id );
718
- orig_val = msr_read (MSR_IA32_PQR_ASSOC );
719
- final_val = (orig_val & 0xffffffffUL ) | (((uint64_t )conf -> clos ) << 32UL );
720
- msr_write_pcpu (MSR_IA32_PQR_ASSOC , final_val , pcpu_id );
721
+ INIT_LIST_HEAD (& vcpu -> sched_obj .run_list );
722
+ snprintf (thread_name , 16U , "vm%hu:vcpu%hu" , vm -> vm_id , vcpu -> vcpu_id );
723
+ (void )strncpy_s (vcpu -> sched_obj .name , 16U , thread_name , 16U );
724
+ vcpu -> sched_obj .thread = vcpu_thread ;
725
+ vcpu -> sched_obj .host_sp = build_stack_frame (vcpu );
726
+ vcpu -> sched_obj .prepare_switch_out = context_switch_out ;
727
+ vcpu -> sched_obj .prepare_switch_in = context_switch_in ;
721
728
}
722
729
723
- INIT_LIST_HEAD (& vcpu -> sched_obj .run_list );
724
- snprintf (thread_name , 16U , "vm%hu:vcpu%hu" , vm -> vm_id , vcpu -> vcpu_id );
725
- (void )strncpy_s (vcpu -> sched_obj .name , 16U , thread_name , 16U );
726
- vcpu -> sched_obj .thread = vcpu_thread ;
727
- vcpu -> sched_obj .host_sp = build_stack_frame (vcpu );
728
- vcpu -> sched_obj .prepare_switch_out = context_switch_out ;
729
- vcpu -> sched_obj .prepare_switch_in = context_switch_in ;
730
-
731
730
return ret ;
732
731
}
733
732
0 commit comments