Skip to content

Commit c390ab0

Browse files
ZideChen0wenlingz
authored andcommitted
hv: don't overwrite the statically configured vm_configs[] in hypercall
The statically configured vm_configs[].cpu_affinity_bitmap should remain intact during the life cycle of the SOS, otherwise user can't destroy and create the same VM with different CPU affinity. For example: - Initially vm_configs[1].cpu_affinity_bitmap is set to 0xF: pCPU 0/1/2/3. - VM1 is created on pCPU1 and pCPU2 and vm_configs[1].cpu_affinity_bitmap is overwritten as 0x6. - VM1 is destroyed. - Now VM1 can't be launched again on pCPU0 or pCPU3. This patch fixes this by saving the static VM configuration before the create_vm hypercall and restore it when the post-launched VM is shutting down. Tracked-On: #4616 Signed-off-by: Zide Chen <zide.chen@intel.com>
1 parent cbaf3e7 commit c390ab0

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

hypervisor/arch/x86/configs/vm_config.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
static uint8_t rtvm_uuid1[16] = POST_RTVM_UUID1;
1515
static uint8_t safety_vm_uuid1[16] = SAFETY_VM_UUID1;
1616

17+
/*
18+
* To be used for post-launched VMS only.
19+
*
20+
* acrn-dm could modify post-launched VM configuration through command line arguments.
21+
* We make use of this additional array to make sure that the dynamic configuration
22+
* from acrn-dm won't overwrite the static vm_configs[].
23+
*/
24+
static struct acrn_vm_config vm_configs_saved[CONFIG_MAX_VM_NUM];
25+
1726
/*
1827
* @pre vm_id < CONFIG_MAX_VM_NUM
1928
* @post return != NULL
@@ -23,6 +32,20 @@ struct acrn_vm_config *get_vm_config(uint16_t vm_id)
2332
return &vm_configs[vm_id];
2433
}
2534

35+
/*
36+
* @pre vm_id < CONFIG_MAX_VM_NUM
37+
*/
38+
void save_or_restore_vm_config(uint16_t vm_id, bool save)
39+
{
40+
size_t size = sizeof(struct acrn_vm_config);
41+
42+
if (save) {
43+
(void)memcpy_s((void *)&vm_configs_saved[vm_id], size, (void *)&vm_configs[vm_id], size);
44+
} else {
45+
(void)memcpy_s((void *)&vm_configs[vm_id], size, (void *)&vm_configs_saved[vm_id], size);
46+
}
47+
}
48+
2649
static inline bool uuid_is_equal(const uint8_t *uuid1, const uint8_t *uuid2)
2750
{
2851
uint64_t uuid1_h = *(const uint64_t *)uuid1;

hypervisor/arch/x86/guest/vm.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ int32_t shutdown_vm(struct acrn_vm *vm)
585585
uint16_t i;
586586
uint64_t mask;
587587
struct acrn_vcpu *vcpu = NULL;
588-
struct acrn_vm_config *vm_config = NULL;
589588
int32_t ret = 0;
590589

591590
pause_vm(vm);
@@ -603,9 +602,6 @@ int32_t shutdown_vm(struct acrn_vm *vm)
603602
offline_vcpu(vcpu);
604603
}
605604

606-
vm_config = get_vm_config(vm->vm_id);
607-
vm_config->guest_flags &= ~DM_OWNED_GUEST_FLAG_MASK;
608-
609605
if (is_sos_vm(vm)) {
610606
sbuf_reset();
611607
}

hypervisor/common/hypercall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ int32_t hcall_create_vm(struct acrn_vm *vm, uint64_t param)
161161
vm_id = get_vmid_by_uuid(&cv.uuid[0]);
162162
if ((vm_id > vm->vm_id) && (vm_id < CONFIG_MAX_VM_NUM)
163163
&& (is_poweroff_vm(get_vm_from_vmid(vm_id)))) {
164+
165+
/* Save a copy of the static vm configuration */
166+
save_or_restore_vm_config(vm_id, true);
164167
vm_config = get_vm_config(vm_id);
165168

166169
/* Filter out the bits should not set by DM and then assign it to guest_flags */
@@ -220,6 +223,9 @@ int32_t hcall_destroy_vm(uint16_t vmid)
220223
if (!is_poweroff_vm(target_vm) && is_postlaunched_vm(target_vm)) {
221224
/* TODO: check target_vm guest_flags */
222225
ret = shutdown_vm(target_vm);
226+
227+
/* restore with the static vm configuration */
228+
save_or_restore_vm_config(vmid, false);
223229
}
224230

225231
return ret;

hypervisor/include/arch/x86/vm_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ struct acrn_vm_config {
175175
} __aligned(8);
176176

177177
struct acrn_vm_config *get_vm_config(uint16_t vm_id);
178+
void save_or_restore_vm_config(uint16_t vm_id, bool save);
178179
bool vm_has_matched_uuid(uint16_t vmid, const uint8_t *uuid);
179180
bool sanitize_vm_config(void);
180181

0 commit comments

Comments
 (0)