mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-07-12 06:38:22 +00:00
This patch will move the VM configuration check to pre-build stage, a test program will do the check for pre-defined VM configuration data before making hypervisor binary. If test failed, the make process will be aborted. So once the hypervisor binary is built successfully or start to run, it means the VM configuration has been sanitized. The patch did not add any new VM configuration check function, it just port the original sanitize_vm_config() function from cpu.c to static_checks.c with below change: 1. remove runtime rdt detection for clos check; 2. replace pr_err() from logmsg.h with printf() from stdio.h; 3. replace runtime call get_pcpu_nums() in ALL_CPUS_MASK macro with static defined MAX_PCPU_NUM; The BOARD/SCENARIO parameter check and configuration folder check is also moved to prebuild Makefile. Tracked-On: #5077 Signed-off-by: Victor Sun <victor.sun@intel.com> Reviewed-by: Jason Chen CJ <jason.cj.chen@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
201 lines
5.6 KiB
C
201 lines
5.6 KiB
C
/*
|
|
* Copyright (C) 2020 Intel Corporation. All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include <bits.h>
|
|
#include <page.h>
|
|
#include <vm_config.h>
|
|
#include <rdt.h>
|
|
#include <vuart.h>
|
|
#include <vpci.h>
|
|
#include <hv_prebuild.h>
|
|
|
|
static uint8_t rtvm_uuid1[16] = POST_RTVM_UUID1;
|
|
static uint8_t safety_vm_uuid1[16] = SAFETY_VM_UUID1;
|
|
|
|
/* sanity check for below structs is not needed, so use a empty struct instead */
|
|
struct acrn_vm_pci_dev_config sos_pci_devs[CONFIG_MAX_PCI_DEV_NUM];
|
|
const struct pci_vdev_ops vhostbridge_ops;
|
|
|
|
#define PLATFORM_CPUS_MASK ((1UL << MAX_PCPU_NUM) - 1UL)
|
|
|
|
/**
|
|
* return true if the input uuid is for RTVM
|
|
*
|
|
* @pre vmid < CONFIG_MAX_VM_NUM
|
|
*/
|
|
static bool is_safety_vm_uuid(const uint8_t *uuid)
|
|
{
|
|
/* TODO: Extend to check more safety VM uuid if we have more than one safety VM. */
|
|
return uuid_is_equal(uuid, safety_vm_uuid1);
|
|
}
|
|
|
|
/**
|
|
* return true if the input uuid is for RTVM
|
|
*
|
|
* @pre vmid < CONFIG_MAX_VM_NUM
|
|
*/
|
|
static bool is_rtvm_uuid(const uint8_t *uuid)
|
|
{
|
|
/* TODO: Extend to check more rtvm uuid if we have more than one RTVM. */
|
|
return uuid_is_equal(uuid, rtvm_uuid1);
|
|
}
|
|
|
|
/**
|
|
* return true if no UUID collision is found in vm configs array start from vm_configs[vm_id]
|
|
*
|
|
* @pre vm_id < CONFIG_MAX_VM_NUM
|
|
*/
|
|
static bool check_vm_uuid_collision(uint16_t vm_id)
|
|
{
|
|
uint16_t i;
|
|
bool ret = true;
|
|
struct acrn_vm_config *start_config = get_vm_config(vm_id);
|
|
struct acrn_vm_config *following_config;
|
|
|
|
for (i = vm_id + 1U; i < CONFIG_MAX_VM_NUM; i++) {
|
|
following_config = get_vm_config(i);
|
|
if (uuid_is_equal(&start_config->uuid[0], &following_config->uuid[0])) {
|
|
ret = false;
|
|
break;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static bool check_vm_clos_config(uint16_t vm_id)
|
|
{
|
|
uint16_t i;
|
|
uint16_t platform_clos_num = MAX_PLATFORM_CLOS_NUM;
|
|
bool ret = true;
|
|
struct acrn_vm_config *vm_config = get_vm_config(vm_id);
|
|
uint16_t vcpu_num = bitmap_weight(vm_config->cpu_affinity);
|
|
|
|
for (i = 0U; i < vcpu_num; i++) {
|
|
if (((platform_clos_num != 0U) && (vm_config->clos[i] == platform_clos_num))
|
|
|| (vm_config->clos[i] > platform_clos_num)) {
|
|
printf("vm%u: vcpu%u clos(%u) exceed the max clos(%u).",
|
|
vm_id, i, vm_config->clos[i], platform_clos_num);
|
|
ret = false;
|
|
break;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @pre vm_config != NULL
|
|
*/
|
|
bool sanitize_vm_config(void)
|
|
{
|
|
bool ret = true;
|
|
uint16_t vm_id, vuart_idx;
|
|
uint64_t pre_launch_pcpu_bitmap = 0UL;
|
|
struct acrn_vm_config *vm_config;
|
|
|
|
/* All physical CPUs except ocuppied by Pre-launched VMs are all
|
|
* belong to SOS_VM. i.e. The cpu_affinity of a SOS_VM is decided
|
|
* by cpu_affinity status in PRE_LAUNCHED_VMs.
|
|
* We need to setup a rule, that the vm_configs[] array should follow
|
|
* the order of PRE_LAUNCHED_VM first, and then SOS_VM.
|
|
*/
|
|
for (vm_id = 0U; vm_id < CONFIG_MAX_VM_NUM; vm_id++) {
|
|
vm_config = get_vm_config(vm_id);
|
|
|
|
if ((vm_config->cpu_affinity & ~PLATFORM_CPUS_MASK) != 0UL) {
|
|
printf("%s: vm%u assigns invalid PCPU (0x%llx)", __func__, vm_id, vm_config->cpu_affinity);
|
|
ret = false;
|
|
}
|
|
|
|
switch (vm_config->load_order) {
|
|
case PRE_LAUNCHED_VM:
|
|
if (vm_config->cpu_affinity == 0UL) {
|
|
printf("%s: Pre-launch VM has no pcpus!\n", __func__);
|
|
ret = false;
|
|
/* GUEST_FLAG_RT must be set if we have GUEST_FLAG_LAPIC_PASSTHROUGH set in guest_flags */
|
|
} else if (((vm_config->guest_flags & GUEST_FLAG_LAPIC_PASSTHROUGH) != 0U)
|
|
&& ((vm_config->guest_flags & GUEST_FLAG_RT) == 0U)) {
|
|
ret = false;
|
|
} else if (vm_config->epc.size != 0UL) {
|
|
ret = false;
|
|
} else if (is_safety_vm_uuid(vm_config->uuid) && (vm_config->severity != (uint8_t)SEVERITY_SAFETY_VM)) {
|
|
ret = false;
|
|
} else {
|
|
pre_launch_pcpu_bitmap |= vm_config->cpu_affinity;
|
|
}
|
|
break;
|
|
case SOS_VM:
|
|
/* Deduct pcpus of PRE_LAUNCHED_VMs */
|
|
vm_config->cpu_affinity = PLATFORM_CPUS_MASK ^ pre_launch_pcpu_bitmap;
|
|
if ((vm_config->cpu_affinity == 0UL) || (vm_config->severity != (uint8_t)SEVERITY_SOS) ||
|
|
((vm_config->guest_flags & GUEST_FLAG_LAPIC_PASSTHROUGH) != 0U)) {
|
|
ret = false;
|
|
}
|
|
break;
|
|
case POST_LAUNCHED_VM:
|
|
if ((vm_config->cpu_affinity == 0UL) ||
|
|
((vm_config->cpu_affinity & pre_launch_pcpu_bitmap) != 0UL)) {
|
|
printf("%s: Post-launch VM has no pcpus or share pcpu with Pre-launch VM!\n", __func__);
|
|
ret = false;
|
|
}
|
|
|
|
if ((vm_config->severity == (uint8_t)SEVERITY_SAFETY_VM) ||
|
|
(vm_config->severity == (uint8_t)SEVERITY_SOS)) {
|
|
ret = false;
|
|
}
|
|
|
|
/* VM with RTVM uuid must have RTVM severity */
|
|
if (is_rtvm_uuid(vm_config->uuid) && (vm_config->severity != (uint8_t)SEVERITY_RTVM)) {
|
|
ret = false;
|
|
}
|
|
|
|
/* VM WITHOUT RTVM uuid must NOT have RTVM severity */
|
|
if (!is_rtvm_uuid(vm_config->uuid) && (vm_config->severity == (uint8_t)SEVERITY_RTVM)) {
|
|
ret = false;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
/* Nothing to do for a unknown VM, break directly. */
|
|
break;
|
|
}
|
|
|
|
if (ret) {
|
|
ret = check_vm_clos_config(vm_id);
|
|
}
|
|
|
|
if (ret &&
|
|
(((vm_config->epc.size | vm_config->epc.base) & ~PAGE_MASK) != 0UL)) {
|
|
ret = false;
|
|
}
|
|
|
|
if (ret) {
|
|
/* make sure no identical UUID in following VM configurations */
|
|
ret = check_vm_uuid_collision(vm_id);
|
|
}
|
|
|
|
if (ret) {
|
|
/* vuart[1+] are used for VM communications */
|
|
for (vuart_idx = 1U; vuart_idx < MAX_VUART_NUM_PER_VM; vuart_idx++) {
|
|
const struct vuart_config *vu_config = &vm_config->vuart[vuart_idx];
|
|
|
|
if (!(vu_config->type == VUART_LEGACY_PIO) &&
|
|
(vu_config->addr.port_base == INVALID_COM_BASE)) {
|
|
if ((vu_config->t_vuart.vm_id >= CONFIG_MAX_VM_NUM) ||
|
|
(vu_config->t_vuart.vm_id == vm_id)) {
|
|
printf("%s invalid vuart configuration for VM %d\n", __func__, vm_id);
|
|
ret = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!ret) {
|
|
break;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|