Skip to content

Commit 66e0023

Browse files
jsun26intelEddie Dong
authored andcommitted
HV: sanitize vm config
Add a API to sanitize vm_configs[] array, in this API, we will do sanity check for VM configurations and refill some fields in vm_configs[]. If sanity check is failed, the HV would reject to boot. About pcpu_bitmap of SOS_VM: We should not hard code pcpu_bitmap for SOS_VM, this is not convenient for unconfigured boards. The pcpu_bitmap follows a very simple rule: All physical CPUs except ocuppied by Pre-launched VMs are all belong to SOS_VM. In this way, the pcpu_bitmap of a SOS_VM is decided by pcpu_bitmap status in PRE_LAUNCHED_VMs. To get the correct pcpu_bitmap of SOS_VM, We need to setup another rule, that the vm_configs[] array should follow the order of PRE_LAUNCHED_VM - SOS_VM strictly. With this patch enabled, the pcpu_bitmap field of sos vm config is not needed to configure; Tracked-On: #2291 Signed-off-by: Victor Sun <victor.sun@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 285b64f commit 66e0023

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

hypervisor/arch/x86/configs/sharing_config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ struct acrn_vm_config vm_configs[CONFIG_MAX_VM_NUM] __aligned(PAGE_SIZE) = {
1111
{
1212
.type = SOS_VM,
1313
.name = SOS_VM_CONFIG_NAME,
14-
.pcpu_bitmap = SOS_VM_CONFIG_PCPU_BITMAP,
1514
.guest_flags = SOS_VM_CONFIG_GUEST_FLAGS,
1615
.memory = {
1716
.start_hpa = 0x0UL,

hypervisor/arch/x86/cpu.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ void init_cpu_post(uint16_t pcpu_id)
171171
panic("hardware not support!");
172172
}
173173

174+
if (sanitize_vm_config() != 0) {
175+
panic("VM Configuration Error!");
176+
}
177+
174178
/* Warn for security feature not ready */
175179
if (!check_cpu_security_cap()) {
176180
pr_fatal("SECURITY WARNING!!!!!!");

hypervisor/arch/x86/guest/vm.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,57 @@ void prepare_vm(uint16_t vm_id, struct acrn_vm_config *vm_config)
438438
}
439439
}
440440

441+
/**
442+
* @pre vm_config != NULL
443+
*/
444+
int32_t sanitize_vm_config(void)
445+
{
446+
int32_t ret = 0;
447+
uint16_t vm_id;
448+
uint64_t sos_pcpu_bitmap, pre_launch_pcpu_bitmap;
449+
struct acrn_vm_config *vm_config;
450+
451+
sos_pcpu_bitmap = (uint64_t)((((uint64_t)1U) << get_pcpu_nums()) - 1U);
452+
pre_launch_pcpu_bitmap = 0U;
453+
/* All physical CPUs except ocuppied by Pre-launched VMs are all
454+
* belong to SOS_VM. i.e. The pcpu_bitmap of a SOS_VM is decided
455+
* by pcpu_bitmap status in PRE_LAUNCHED_VMs.
456+
* We need to setup a rule, that the vm_configs[] array should follow
457+
* the order of PRE_LAUNCHED_VM first, and then SOS_VM.
458+
*/
459+
for (vm_id = 0U; vm_id < CONFIG_MAX_VM_NUM; vm_id++) {
460+
vm_config = get_vm_config(vm_id);
461+
switch (vm_config->type) {
462+
case PRE_LAUNCHED_VM:
463+
if (vm_config->pcpu_bitmap == 0U) {
464+
ret = -EINVAL;
465+
} else {
466+
pre_launch_pcpu_bitmap |= vm_config->pcpu_bitmap;
467+
}
468+
break;
469+
case SOS_VM:
470+
/* Deduct pcpus of PRE_LAUNCHED_VMs */
471+
sos_pcpu_bitmap ^= pre_launch_pcpu_bitmap;
472+
if ((sos_pcpu_bitmap == 0U) || ((vm_config->guest_flags & LAPIC_PASSTHROUGH) != 0U)) {
473+
ret = -EINVAL;
474+
} else {
475+
vm_config->pcpu_bitmap = sos_pcpu_bitmap;
476+
}
477+
break;
478+
case NORMAL_VM:
479+
ret = -EINVAL;
480+
break;
481+
default:
482+
/* Nothing to do for a UNDEFINED_VM, break directly. */
483+
break;
484+
}
485+
if (ret != 0) {
486+
break;
487+
}
488+
}
489+
return ret;
490+
}
491+
441492
/**
442493
* @pre vm_config != NULL
443494
*/

hypervisor/include/arch/x86/guest/vm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ int32_t reset_vm(struct acrn_vm *vm);
295295
int32_t create_vm(uint16_t vm_id, struct acrn_vm_config *vm_config, struct acrn_vm **rtn_vm);
296296
void prepare_vm(uint16_t vm_id, struct acrn_vm_config *vm_config);
297297
void launch_vms(uint16_t pcpu_id);
298+
int32_t sanitize_vm_config(void);
298299

299300
extern struct acrn_vm_config vm_configs[CONFIG_MAX_VM_NUM];
300301

0 commit comments

Comments
 (0)