diff --git a/hypervisor/arch/x86/guest/vm.c b/hypervisor/arch/x86/guest/vm.c index 29b7cd9fd..fb37ebea2 100644 --- a/hypervisor/arch/x86/guest/vm.c +++ b/hypervisor/arch/x86/guest/vm.c @@ -11,24 +11,11 @@ /* Local variables */ -/* VMs list */ -struct list_head vm_list = { - .next = &vm_list, - .prev = &vm_list, -}; - -/* Lock for VMs list */ -spinlock_t vm_list_lock = { - .head = 0U, - .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; +/* used for vmid allocation. And this means the max vm number is 64 */ static inline uint16_t alloc_vm_id(void) { uint16_t id = ffz64(vmid_bitmap); @@ -47,7 +34,11 @@ static inline void free_vm_id(struct vm *vm) { bitmap_clear_lock(vm->vm_id, &vmid_bitmap); } -#endif + +static inline bool is_vm_valid(uint16_t vm_id) +{ + return bitmap_test(vm_id, &vmid_bitmap); +} static void init_vm(struct vm_description *vm_desc, struct vm *vm_handle) @@ -75,20 +66,11 @@ static void init_vm(struct vm_description *vm_desc, */ struct vm *get_vm_from_vmid(uint16_t vm_id) { - struct vm *vm = NULL; - struct list_head *pos; - - spinlock_obtain(&vm_list_lock); - list_for_each(pos, &vm_list) { - vm = list_entry(pos, struct vm, list); - if (vm->vm_id == vm_id) { - spinlock_release(&vm_list_lock); - return vm; - } + if (is_vm_valid(vm_id)) { + return &vm_array[vm_id]; + } else { + return NULL; } - spinlock_release(&vm_list_lock); - - return NULL; } /** @@ -102,6 +84,7 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm) #ifdef CONFIG_PARTITION_MODE vm_id = vm_desc->vm_id; + bitmap_set_lock(vm_id, &vmid_bitmap); #else vm_id = alloc_vm_id(); #endif @@ -176,11 +159,6 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm) #endif } - INIT_LIST_HEAD(&vm->list); - spinlock_obtain(&vm_list_lock); - list_add(&vm->list, &vm_list); - spinlock_release(&vm_list_lock); - INIT_LIST_HEAD(&vm->softirq_dev_entry_list); spinlock_init(&vm->softirq_dev_lock); vm->intr_inject_delay_delta = 0UL; @@ -270,10 +248,6 @@ int shutdown_vm(struct vm *vm) destroy_vcpu(vcpu); } - spinlock_obtain(&vm_list_lock); - list_del_init(&vm->list); - spinlock_release(&vm_list_lock); - ptdev_release_all_entries(vm); /* cleanup vioapic */ diff --git a/hypervisor/debug/shell.c b/hypervisor/debug/shell.c index 334dcb322..55b6cd591 100644 --- a/hypervisor/debug/shell.c +++ b/hypervisor/debug/shell.c @@ -525,17 +525,18 @@ static int shell_cmd_help(__unused int argc, __unused char **argv) static int shell_list_vm(__unused int argc, __unused char **argv) { char temp_str[MAX_STR_SIZE]; - struct list_head *pos; struct vm *vm; + uint16_t idx; + char state[32]; shell_puts("\r\nVM NAME VM ID VM STATE" "\r\n======= ===== ========\r\n"); - spinlock_obtain(&vm_list_lock); - list_for_each(pos, &vm_list) { - char state[32]; - - vm = list_entry(pos, struct vm, list); + for (idx = 0; idx < CONFIG_MAX_VM_NUM; idx++) { + vm = get_vm_from_vmid(idx); + if (vm == NULL) { + continue; + } switch (vm->state) { case VM_CREATED: (void)strcpy_s(state, 32, "Created"); @@ -559,7 +560,6 @@ static int shell_list_vm(__unused int argc, __unused char **argv) /* Output information for this task */ shell_puts(temp_str); } - spinlock_release(&vm_list_lock); return 0; } @@ -567,19 +567,20 @@ static int shell_list_vm(__unused int argc, __unused char **argv) static int shell_list_vcpu(__unused int argc, __unused char **argv) { char temp_str[MAX_STR_SIZE]; - struct list_head *pos; struct vm *vm; struct vcpu *vcpu; + char state[32]; + uint16_t i; + uint16_t idx; shell_puts("\r\nVM ID PCPU ID VCPU ID VCPU ROLE VCPU STATE" "\r\n===== ======= ======= ========= ==========\r\n"); - spinlock_obtain(&vm_list_lock); - list_for_each(pos, &vm_list) { - char state[32]; - uint16_t i; - - vm = list_entry(pos, struct vm, list); + for (idx = 0; idx < CONFIG_MAX_VM_NUM; idx++) { + vm = get_vm_from_vmid(idx); + if (vm == NULL) { + continue; + } foreach_vcpu(i, vm, vcpu) { switch (vcpu->state) { case VCPU_INIT: @@ -612,7 +613,6 @@ static int shell_list_vcpu(__unused int argc, __unused char **argv) shell_puts(temp_str); } } - spinlock_release(&vm_list_lock); return 0; } diff --git a/hypervisor/include/arch/x86/guest/vm.h b/hypervisor/include/arch/x86/guest/vm.h index 8e689fb92..b7e52eb20 100644 --- a/hypervisor/include/arch/x86/guest/vm.h +++ b/hypervisor/include/arch/x86/guest/vm.h @@ -139,7 +139,6 @@ struct vm { struct acrn_vuart vuart; /* Virtual UART */ enum vpic_wire_mode wire_mode; struct iommu_domain *iommu; /* iommu domain of this VM */ - struct list_head list; /* list of VM */ spinlock_t spinlock; /* Spin-lock used to protect VM modifications */ struct list_head mmio_list; /* list for mmio. This list is not updated @@ -279,9 +278,6 @@ const struct vm_description_array *get_vm_desc_base(void); struct vm *get_vm_from_vmid(uint16_t vm_id); -extern struct list_head vm_list; -extern spinlock_t vm_list_lock; - #ifdef CONFIG_PARTITION_MODE struct vm_description_array { int num_vm_desc;