hv:Replace dynamic memory with static for vm

-- Replace dynamic memory allocation with static memory
-- Remove the parameter check if the vm is NULL

Tracked-On: #861
Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Mingqiang Chi 2018-09-26 11:24:38 +08:00 committed by wenlingz
parent ff3f9bd1e6
commit b8e59e1638
7 changed files with 40 additions and 60 deletions

View File

@ -32,6 +32,10 @@ config RELEASE
bool "Release build"
default n
config MAX_VM_NUM
int "Maximum number of VM"
default 4
config NR_IOAPICS
int "Maximum number of IOAPICs supported"
default 1

View File

@ -705,7 +705,8 @@ END:
return 0;
}
/* except vm0, Device Model should call this function to pre-hold ptdev intx
/* @pre vm != NULL
* except vm0, Device Model should call this function to pre-hold ptdev intx
* entries:
* - the entry is identified by phys_pin:
* one entry vs. one phys_pin
@ -717,7 +718,7 @@ int ptdev_add_intx_remapping(struct vm *vm, uint8_t virt_pin, uint8_t phys_pin,
{
struct ptdev_remapping_info *entry;
if (vm == NULL || (!pic_pin && virt_pin >= vioapic_pincount(vm))
if ((!pic_pin && virt_pin >= vioapic_pincount(vm))
|| (pic_pin && virt_pin >= vpic_pincount())) {
pr_err("ptdev_add_intx_remapping fails!\n");
return -EINVAL;
@ -728,13 +729,11 @@ int ptdev_add_intx_remapping(struct vm *vm, uint8_t virt_pin, uint8_t phys_pin,
return (entry != NULL) ? 0 : -ENODEV;
}
/*
* @pre vm != NULL
*/
void ptdev_remove_intx_remapping(struct vm *vm, uint8_t virt_pin, bool pic_pin)
{
if (vm == NULL) {
pr_err("ptdev_remove_intr_remapping fails!\n");
return;
}
remove_intx_remapping(vm, virt_pin, pic_pin);
}
@ -759,16 +758,14 @@ int ptdev_add_msix_remapping(struct vm *vm, uint16_t virt_bdf,
return 0;
}
/*
* @pre vm != NULL
*/
void ptdev_remove_msix_remapping(struct vm *vm, uint16_t virt_bdf,
uint32_t vector_count)
{
uint32_t i;
if (vm == NULL) {
pr_err("ptdev_remove_msix_remapping fails!\n");
return;
}
for (i = 0U; i < vector_count; i++) {
remove_msix_remapping(vm, virt_bdf, i);
}

View File

@ -230,7 +230,10 @@ void set_vcpu_regs(struct vcpu *vcpu, struct acrn_vcpu_regs *vcpu_regs)
}
/***********************************************************************
* vcpu_id/pcpu_id mapping table:
*
* @pre vm != NULL && rtn_vcpu_handle != NULL
*
* vcpu_id/pcpu_id mapping table:
*
* if
* VM0_CPUS[2] = {0, 2} , VM1_CPUS[2] = {3, 1};
@ -245,8 +248,6 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
{
struct vcpu *vcpu;
ASSERT(vm != NULL, "");
ASSERT(rtn_vcpu_handle != NULL, "");
pr_info("Creating VCPU working on PCPU%hu", pcpu_id);

View File

@ -23,6 +23,8 @@ spinlock_t vm_list_lock = {
.tail = 0U
};
static struct vm vm_array[CONFIG_MAX_VM_NUM] __aligned(CPU_PAGE_SIZE);
#ifndef CONFIG_PARTITION_MODE
/* used for vmid allocation. And this means the max vm number is 64 */
static uint64_t vmid_bitmap;
@ -89,23 +91,29 @@ struct vm *get_vm_from_vmid(uint16_t vm_id)
return NULL;
}
/**
* @pre vm_desc != NULL && rtn_vm != NULL
*/
int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
{
struct vm *vm;
int status;
uint16_t vm_id;
if ((vm_desc == NULL) || (rtn_vm == NULL)) {
pr_err("%s, invalid paramater\n", __func__);
return -EINVAL;
#ifdef CONFIG_PARTITION_MODE
vm_id = vm_desc->vm_id;
#else
vm_id = alloc_vm_id();
#endif
if (vm_id >= CONFIG_MAX_VM_NUM) {
pr_err("%s, vm id is invalid!\n", __func__);
return -ENODEV;
}
/* Allocate memory for virtual machine */
vm = calloc(1U, sizeof(struct vm));
if (vm == NULL) {
pr_err("%s, vm allocation failed\n", __func__);
return -ENOMEM;
}
vm = &vm_array[vm_id];
(void)memset((void *)vm, 0U, sizeof(struct vm));
vm->vm_id = vm_id;
/*
* Map Virtual Machine to its VM Description
*/
@ -117,7 +125,6 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
if (vm->hw.num_vcpus == 0U) {
vm->hw.num_vcpus = phys_cpu_num;
}
vm->hw.vcpu_array =
calloc(1U, sizeof(struct vcpu *) * vm->hw.num_vcpus);
if (vm->hw.vcpu_array == NULL) {
@ -126,17 +133,6 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
goto err;
}
#ifdef CONFIG_PARTITION_MODE
vm->vm_id = vm_desc->vm_id;
#else
vm->vm_id = alloc_vm_id();
if (vm->vm_id == INVALID_VM_ID) {
pr_err("%s, no more VMs can be supported\n", __func__);
status = -ENODEV;
goto err;
}
#endif
atomic_store16(&vm->hw.created_vcpus, 0U);
/* gpa_lowtop are used for system start up */
@ -250,21 +246,18 @@ err:
if (vm->hw.vcpu_array != NULL) {
free(vm->hw.vcpu_array);
}
free(vm);
return status;
}
/*
* @pre vm != NULL
*/
int shutdown_vm(struct vm *vm)
{
int status = 0;
uint16_t i;
struct vcpu *vcpu = NULL;
if (vm == NULL) {
return -EINVAL;
}
pause_vm(vm);
/* Only allow shutdown paused vm */
@ -310,13 +303,8 @@ int shutdown_vm(struct vm *vm)
#ifdef CONFIG_PARTITION_MODE
vpci_cleanup(vm);
#endif
free(vm->hw.vcpu_array);
/* TODO: De-Configure HV-SW */
/* Deallocate VM */
free(vm);
/* Return status to caller */
return status;
}

View File

@ -70,11 +70,6 @@ static void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig,
struct vm *vm0 = get_vm_from_vmid(0U);
uint16_t i;
if (vm0 == NULL) {
pr_err("Parse vm0 context failed.");
return;
}
if ((vm->sworld_control.flag.supported == 0UL)
|| (vm->arch_vm.sworld_eptp != NULL)) {
pr_err("Sworld is not supported or Sworld eptp is not NULL");
@ -154,11 +149,6 @@ void destroy_secure_world(struct vm *vm, bool need_clr_mem)
uint64_t gpa_uos = vm->sworld_control.sworld_memory.base_gpa_in_uos;
uint64_t size = vm->sworld_control.sworld_memory.length;
if (vm0 == NULL) {
pr_err("Parse vm0 context failed.");
return;
}
if (vm->arch_vm.sworld_eptp == NULL) {
pr_err("sworld eptp is NULL, it's not created");
return;

View File

@ -19,7 +19,6 @@ static void fire_vhm_interrupt(void)
struct vcpu *vcpu;
vm0 = get_vm_from_vmid(0U);
ASSERT(vm0 != NULL, "VM Pointer is NULL");
vcpu = vcpu_from_vid(vm0, 0U);
ASSERT(vcpu != NULL, "vcpu_from_vid failed");

View File

@ -115,6 +115,9 @@ int load_guest(struct vm *vm, struct vcpu *vcpu)
return ret;
}
/*
* @pre vm != NULL
*/
int general_sw_loader(struct vm *vm, struct vcpu *vcpu)
{
int32_t ret = 0;
@ -125,8 +128,6 @@ int general_sw_loader(struct vm *vm, struct vcpu *vcpu)
struct sw_linux *sw_linux = &(vm->sw.linux_info);
struct sw_kernel_info *sw_kernel = &(vm->sw.kernel_info);
ASSERT(vm != NULL, "Incorrect argument");
pr_dbg("Loading guest to run-time location");
/* ACRN in partiton mode boots all VMs without devicemodel */