hv:cleanup vmid related code

Remove structure vm_attr
Wrap two APIs alloc_vm_id and free_vm_id

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-08-03 15:43:25 +08:00 committed by lijinxia
parent 2299926a88
commit 7345677bbb
17 changed files with 73 additions and 65 deletions

View File

@ -329,10 +329,10 @@ add_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint16_t phys_bdf,
" not able to add into vm%d with vbdf%x",
entry->phys_bdf,
entry->ptdev_intr_info.msi.msix_entry_index,
entry->vm->attr.id,
entry->virt_bdf, vm->attr.id, virt_bdf);
entry->vm->vm_id,
entry->virt_bdf, vm->vm_id, virt_bdf);
ASSERT(false, "msix entry pbdf%x idx%d already in vm%d",
phys_bdf, msix_entry_index, entry->vm->attr.id);
phys_bdf, msix_entry_index, entry->vm->vm_id);
spinlock_release(&ptdev_lock);
return &invalid_entry;
@ -345,7 +345,7 @@ add_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint16_t phys_bdf,
dev_dbg(ACRN_DBG_IRQ,
"VM%d MSIX add vector mapping vbdf%x:pbdf%x idx=%d",
entry->vm->attr.id, virt_bdf, phys_bdf, msix_entry_index);
entry->vm->vm_id, virt_bdf, phys_bdf, msix_entry_index);
return entry;
}
@ -369,7 +369,7 @@ remove_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint32_t msix_entry_inde
dev_dbg(ACRN_DBG_IRQ,
"VM%d MSIX remove vector mapping vbdf-pbdf:0x%x-0x%x idx=%d",
entry->vm->attr.id,
entry->vm->vm_id,
entry->virt_bdf, entry->phys_bdf, msix_entry_index);
release_entry(entry);
@ -413,11 +413,11 @@ add_intx_remapping(struct vm *vm, uint8_t virt_pin,
pr_err("INTX pin%d already in vm%d with vpin%d,"
" not able to add into vm%d with vpin%d",
entry->ptdev_intr_info.intx.phys_pin,
entry->vm->attr.id,
entry->vm->vm_id,
entry->ptdev_intr_info.intx.virt_pin,
vm->attr.id, virt_pin);
vm->vm_id, virt_pin);
ASSERT(false, "intx entry pin%d already vm%d",
phys_pin, entry->vm->attr.id);
phys_pin, entry->vm->vm_id);
spinlock_release(&ptdev_lock);
return &invalid_entry;
@ -431,7 +431,7 @@ add_intx_remapping(struct vm *vm, uint8_t virt_pin,
dev_dbg(ACRN_DBG_IRQ,
"VM%d INTX add pin mapping vpin%d:ppin%d",
entry->vm->attr.id, virt_pin, phys_pin);
entry->vm->vm_id, virt_pin, phys_pin);
return entry;
}
@ -466,7 +466,7 @@ static void remove_intx_remapping(struct vm *vm, uint8_t virt_pin, bool pic_pin)
"vPIC" : "vIOAPIC",
entry->ptdev_intr_info.intx.phys_pin, phys_irq);
dev_dbg(ACRN_DBG_IRQ, "from vm%d vpin=%d\n",
entry->vm->attr.id,
entry->vm->vm_id,
entry->ptdev_intr_info.intx.virt_pin);
}
@ -677,7 +677,7 @@ int ptdev_msix_remap(struct vm *vm, uint16_t virt_bdf,
entry->ptdev_intr_info.msi.msix_entry_index,
entry->ptdev_intr_info.msi.virt_vector,
entry->ptdev_intr_info.msi.phys_vector,
entry->vm->attr.id);
entry->vm->vm_id);
END:
return 0;
}
@ -831,7 +831,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
(entry->ptdev_intr_info.intx.vpin_src != 0)?
"vIOPIC" : "vPIC",
info->virt_pin,
entry->vm->attr.id);
entry->vm->vm_id);
intx->vpin_src = info->vpin_src;
intx->virt_pin = info->virt_pin;
}
@ -848,7 +848,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
"IOAPIC pin=%hhu pirq=%u deassigned ",
phys_pin, phys_irq);
dev_dbg(ACRN_DBG_IRQ, "from vm%d vIOAPIC vpin=%d",
entry->vm->attr.id,
entry->vm->vm_id,
intx->virt_pin);
goto END;
} else {
@ -869,7 +869,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
dev_dbg(ACRN_DBG_IRQ,
"IOAPIC pin=%hhu pirq=%u assigned to vm%d %s vpin=%d",
phys_pin, phys_irq, entry->vm->attr.id,
phys_pin, phys_irq, entry->vm->vm_id,
intx->vpin_src == PTDEV_VPIN_PIC ?
"vPIC" : "vIOAPIC",
intx->virt_pin);
@ -1037,7 +1037,7 @@ void get_ptdev_info(char *str_arg, int str_max)
&bdf, &vbdf);
len = snprintf(str, size,
"\r\n%d\t%s\t%d\t0x%X\t0x%X",
entry->vm->attr.id, type,
entry->vm->vm_id, type,
irq, vector, dest);
size -= len;
str += len;

View File

@ -129,7 +129,7 @@ uint64_t local_gpa2hpa(struct vm *vm, uint64_t gpa, uint32_t *size)
pr_dbg("GPA2HPA: 0x%llx->0x%llx", gpa, hpa);
} else {
pr_err("VM %d GPA2HPA: failed for gpa 0x%llx",
vm->attr.boot_idx, gpa);
vm->vm_id, gpa);
}
if (size != NULL) {
@ -153,7 +153,7 @@ uint64_t hpa2gpa(struct vm *vm, uint64_t hpa)
hpa, &pg_size, PTT_EPT);
if (pgentry == NULL) {
pr_err("VM %d hpa2gpa: failed for hpa 0x%llx",
vm->attr.boot_idx, hpa);
vm->vm_id, hpa);
ASSERT(false, "hpa2gpa not found");
}
return ((*pgentry & (~(pg_size - 1UL)))

View File

@ -32,7 +32,7 @@ struct page_walk_info {
inline bool
is_vm0(struct vm *vm)
{
return (vm->attr.boot_idx & 0x7FU) == 0U;
return (vm->vm_id) == 0U;
}
inline struct vcpu *vcpu_from_vid(struct vm *vm, uint16_t vcpu_id)

View File

@ -210,7 +210,7 @@ register_gas_io_handler(struct vm *vm, struct acpi_generic_address *gas)
&pm1ab_io_read, &pm1ab_io_write);
pr_dbg("Enable PM1A trap for VM %d, port 0x%x, size %d\n",
vm->attr.id, gas_io.base, gas_io.len);
vm->vm_id, gas_io.base, gas_io.len);
}
void register_pm1ab_handler(struct vm *vm)

View File

@ -75,7 +75,7 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
per_cpu(vcpu, pcpu_id) = vcpu;
pr_info("PCPU%d is working as VM%d VCPU%d, Role: %s",
vcpu->pcpu_id, vcpu->vm->attr.id, vcpu->vcpu_id,
vcpu->pcpu_id, vcpu->vm->vm_id, vcpu->vcpu_id,
is_vcpu_bsp(vcpu) ? "PRIMARY" : "SECONDARY");
#ifdef CONFIG_START_VM0_BSP_64BIT
@ -84,7 +84,7 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
/* Set up temporary guest page tables */
vm->arch_vm.guest_init_pml4 = create_guest_initial_paging(vm);
pr_info("VM %d VCPU %hu CR3: 0x%016llx ",
vm->attr.id, vcpu->vcpu_id,
vm->vm_id, vcpu->vcpu_id,
vm->arch_vm.guest_init_pml4);
}
#endif
@ -156,7 +156,7 @@ int start_vcpu(struct vcpu *vcpu)
/* If this VCPU is not already launched, launch it */
if (!vcpu->launched) {
pr_info("VM %d Starting VCPU %hu",
vcpu->vm->attr.id, vcpu->vcpu_id);
vcpu->vm->vm_id, vcpu->vcpu_id);
if (vcpu->arch_vcpu.vpid)
exec_vmwrite16(VMX_VPID, vcpu->arch_vcpu.vpid);
@ -185,7 +185,7 @@ int start_vcpu(struct vcpu *vcpu)
if (status == 0) {
if (is_vcpu_bsp(vcpu)) {
pr_info("VM %d VCPU %hu successfully launched",
vcpu->vm->attr.id, vcpu->vcpu_id);
vcpu->vm->vm_id, vcpu->vcpu_id);
}
}
} else {

View File

@ -105,7 +105,7 @@ vm_lapic_from_vcpu_id(struct vm *vm, uint16_t vcpu_id)
struct vcpu *vcpu;
vcpu = vcpu_from_vid(vm, vcpu_id);
ASSERT(vcpu != NULL, "vm%d, vcpu%hu", vm->attr.id, vcpu_id);
ASSERT(vcpu != NULL, "vm%d, vcpu%hu", vm->vm_id, vcpu_id);
return vcpu->arch_vcpu.vlapic;
}
@ -116,7 +116,7 @@ vm_lapic_from_pcpuid(struct vm *vm, uint16_t pcpu_id)
struct vcpu *vcpu;
vcpu = vcpu_from_pid(vm, pcpu_id);
ASSERT(vcpu != NULL, "vm%d, pcpu%hu", vm->attr.id, pcpu_id);
ASSERT(vcpu != NULL, "vm%d, pcpu%hu", vm->vm_id, pcpu_id);
return vcpu->arch_vcpu.vlapic;
}
@ -1173,7 +1173,7 @@ vlapic_icrlo_write_handler(struct acrn_vlapic *vlapic)
target_vcpu->arch_vcpu.sipi_vector = vec;
pr_err("Start Secondary VCPU%hu for VM[%d]...",
target_vcpu->vcpu_id,
target_vcpu->vm->attr.id);
target_vcpu->vm->vm_id);
schedule_vcpu(target_vcpu);
} else {
pr_err("Unhandled icrlo write with mode %u\n", mode);

View File

@ -25,6 +25,25 @@ spinlock_t vm_list_lock = {
/* used for vmid allocation. And this means the max vm number is 64 */
static uint64_t vmid_bitmap;
static inline uint16_t alloc_vm_id(void)
{
uint16_t id = ffz64(vmid_bitmap);
while (id < (size_t)(sizeof(vmid_bitmap) * 8U)) {
if (!bitmap_test_and_set_lock(id, &vmid_bitmap)) {
return id;
}
id = ffz64(vmid_bitmap);
}
return INVALID_VM_ID;
}
static inline void free_vm_id(struct vm *vm)
{
bitmap_clear_lock(vm->vm_id, &vmid_bitmap);
}
static void init_vm(struct vm_description *vm_desc,
struct vm *vm_handle)
{
@ -54,7 +73,7 @@ struct vm *get_vm_from_vmid(uint16_t vm_id)
spinlock_obtain(&vm_list_lock);
list_for_each(pos, &vm_list) {
vm = list_entry(pos, struct vm, list);
if (vm->attr.id == vm_id) {
if (vm->vm_id == vm_id) {
spinlock_release(&vm_list_lock);
return vm;
}
@ -66,7 +85,6 @@ struct vm *get_vm_from_vmid(uint16_t vm_id)
int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
{
uint16_t id;
struct vm *vm;
int status;
@ -102,21 +120,13 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
goto err;
}
for (id = 0U; id < (size_t)(sizeof(vmid_bitmap) * 8U); id++) {
if (!bitmap_test_and_set_lock(id, &vmid_bitmap)) {
break;
}
}
if (id >= (size_t)(sizeof(vmid_bitmap) * 8U)) {
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 = -EINVAL;
status = -ENODEV;
goto err;
}
vm->attr.id = id;
vm->attr.boot_idx = id;
atomic_store16(&vm->hw.created_vcpus, 0U);
/* gpa_lowtop are used for system start up */
@ -273,7 +283,8 @@ int shutdown_vm(struct vm *vm)
destroy_iommu_domain(vm->iommu);
}
bitmap_clear_lock(vm->attr.id, &vmid_bitmap);
/* Free vm id */
free_vm_id(vm);
if (vm->vpic != NULL) {
vpic_cleanup(vm);
@ -300,7 +311,7 @@ int start_vm(struct vm *vm)
/* Only start BSP (vid = 0) and let BSP start other APs */
vcpu = vcpu_from_vid(vm, 0U);
ASSERT(vcpu != NULL, "vm%d, vcpu0", vm->attr.id);
ASSERT(vcpu != NULL, "vm%d, vcpu0", vm->vm_id);
schedule_vcpu(vcpu);
return 0;

View File

@ -181,7 +181,7 @@ int vmcall_vmexit_handler(struct vcpu *vcpu)
out:
cur_context->guest_cpu_regs.regs.rax = (uint64_t)ret;
TRACE_2L(TRACE_VMEXIT_VMCALL, vm->attr.id, hypcall_id);
TRACE_2L(TRACE_VMEXIT_VMCALL, vm->vm_id, hypcall_id);
return 0;
}

View File

@ -234,7 +234,7 @@ static void vpic_notify_intr(struct acrn_vpic *vpic)
if (vpic->vm->wire_mode == VPIC_WIRE_INTR) {
struct vcpu *vcpu = vcpu_from_vid(vpic->vm, 0U);
ASSERT(vcpu != NULL, "vm%d, vcpu0", vpic->vm->attr.id);
ASSERT(vcpu != NULL, "vm%d, vcpu0", vpic->vm->vm_id);
vcpu_inject_extint(vcpu);
} else {
vlapic_set_local_intr(vpic->vm, BROADCAST_CPU_ID, APIC_LVT_LINT0);

View File

@ -117,7 +117,7 @@ void init_mtrr(struct vcpu *vcpu)
}
pr_dbg("vm%d vcpu%hu fixed-range MTRR[%u]: %16llx",
vcpu->vm->attr.id, vcpu->vcpu_id, i,
vcpu->vm->vm_id, vcpu->vcpu_id, i,
vcpu->mtrr.fixed_range[i].value);
}
}

View File

@ -25,7 +25,7 @@ uint64_t gpa2hpa_for_trusty(struct vm *vm, uint64_t gpa)
pr_dbg("GPA2HPA: 0x%llx->0x%llx", gpa, hpa);
} else {
pr_err("VM %d GPA2HPA: failed for gpa 0x%llx",
vm->attr.boot_idx, gpa);
vm->vm_id, gpa);
}
return hpa;

View File

@ -181,7 +181,7 @@ int32_t hcall_create_vm(struct vm *vm, uint64_t param)
cv.vmid = ACRN_INVALID_VMID;
ret = -1;
} else {
cv.vmid = target_vm->attr.id;
cv.vmid = target_vm->vm_id;
ret = 0;
}
@ -393,7 +393,7 @@ int32_t hcall_notify_ioreq_finish(uint16_t vmid, uint16_t vcpu_id)
vcpu = vcpu_from_vid(target_vm, vcpu_id);
if (vcpu == NULL) {
pr_err("%s, failed to get VCPU %d context from VM %d\n",
__func__, vcpu_id, target_vm->attr.id);
__func__, vcpu_id, target_vm->vm_id);
return -EINVAL;
}
@ -410,13 +410,13 @@ static int32_t local_set_vm_memory_region(struct vm *vm,
if ((region->size & (CPU_PAGE_SIZE - 1UL)) != 0UL) {
pr_err("%s: [vm%d] map size 0x%x is not page aligned",
__func__, target_vm->attr.id, region->size);
__func__, target_vm->vm_id, region->size);
return -EINVAL;
}
hpa = gpa2hpa(vm, region->vm0_gpa);
dev_dbg(ACRN_DBG_HYCALL, "[vm%d] gpa=0x%x hpa=0x%x size=0x%x",
target_vm->attr.id, region->gpa, hpa, region->size);
target_vm->vm_id, region->gpa, hpa, region->size);
base_paddr = get_hv_image_base();
if (((hpa <= base_paddr) &&
@ -540,7 +540,7 @@ static int32_t write_protect_page(struct vm *vm, struct wp_data *wp)
hpa = gpa2hpa(vm, wp->gpa);
dev_dbg(ACRN_DBG_HYCALL, "[vm%d] gpa=0x%x hpa=0x%x",
vm->attr.id, wp->gpa, hpa);
vm->vm_id, wp->gpa, hpa);
base_paddr = get_hv_image_base();
if (((hpa <= base_paddr) && (hpa + CPU_PAGE_SIZE > base_paddr)) ||
@ -665,7 +665,7 @@ int32_t hcall_assign_ptdev(struct vm *vm, uint16_t vmid, uint64_t param)
if (copy_from_gpa(vm, &bdf, param, sizeof(bdf)) != 0) {
pr_err("%s: Unable copy param from vm %d\n",
__func__, vm->attr.id);
__func__, vm->vm_id);
return -EIO;
}
@ -673,7 +673,7 @@ int32_t hcall_assign_ptdev(struct vm *vm, uint16_t vmid, uint64_t param)
if (target_vm->iommu == NULL) {
if (target_vm->arch_vm.nworld_eptp == NULL) {
pr_err("%s, EPT of VM not set!\n",
__func__, target_vm->attr.id);
__func__, target_vm->vm_id);
return -EPERM;
}
/* TODO: how to get vm's address width? */

View File

@ -188,7 +188,7 @@ void get_req_info(char *str_arg, int str_max)
state, dir, &addr, &val);
len = snprintf(str, size,
"\r\n%d\t%d\t%d\t%s\t%s\t%s",
vm->attr.id, i, client_id, type,
vm->vm_id, i, client_id, type,
state, dir);
size -= len;
str += len;

View File

@ -137,7 +137,7 @@ int general_sw_loader(struct vm *vm, struct vcpu *vcpu)
/* Set VCPU entry point to kernel entry */
vcpu->entry_addr = sw_kernel->kernel_entry_addr;
pr_info("%s, VM *d VCPU %hu Entry: 0x%016llx ",
__func__, vm->attr.id, vcpu->vcpu_id, vcpu->entry_addr);
__func__, vm->vm_id, vcpu->vcpu_id, vcpu->entry_addr);
}
/* Calculate the host-physical address where the guest will be loaded */
@ -220,7 +220,7 @@ int general_sw_loader(struct vm *vm, struct vcpu *vcpu)
cur_context->guest_cpu_regs.regs.rsi = create_zero_page(vm);
pr_info("%s, RSI pointing to zero page for VM %d at GPA %X",
__func__, vm->attr.id,
__func__, vm->vm_id,
cur_context->guest_cpu_regs.regs.rsi);
} else {

View File

@ -57,7 +57,7 @@ static void dump_guest_reg(struct vcpu *vcpu)
printf("Guest Registers:\r\n");
printf("= VM ID %d ==== vCPU ID %hu === pCPU ID %d ===="
"world %d =============\r\n",
vcpu->vm->attr.id, vcpu->vcpu_id, vcpu->pcpu_id,
vcpu->vm->vm_id, vcpu->vcpu_id, vcpu->pcpu_id,
vcpu->arch_vcpu.cur_context);
printf("= RIP=0x%016llx RSP=0x%016llx "
"RFLAGS=0x%016llx\r\n",

View File

@ -517,8 +517,8 @@ int shell_list_vm(__unused int argc, __unused char **argv)
/* Create output string consisting of VM name and VM id
*/
snprintf(temp_str, MAX_STR_SIZE,
"vm_%-24d %-16d %-8s\r\n", vm->attr.id,
vm->attr.id, state);
"vm_%-24d %-16d %-8s\r\n", vm->vm_id,
vm->vm_id, state);
/* Output information for this task */
shell_puts(temp_str);
@ -562,7 +562,7 @@ int shell_list_vcpu(__unused int argc, __unused char **argv)
*/
snprintf(temp_str, MAX_STR_SIZE,
" %-9d %-10d %-7hu %-12s %-16s\r\n",
vm->attr.id,
vm->vm_id,
vcpu->pcpu_id,
vcpu->vcpu_id,
is_vcpu_bsp(vcpu) ?
@ -627,7 +627,7 @@ int shell_vcpu_dumpreg(int argc, char **argv)
snprintf(temp_str, MAX_STR_SIZE,
"= VM ID %d ==== CPU ID %hu========================\r\n",
vm->attr.id, vcpu->vcpu_id);
vm->vm_id, vcpu->vcpu_id);
shell_puts(temp_str);
snprintf(temp_str, MAX_STR_SIZE, "= RIP=0x%016llx RSP=0x%016llx "
"RFLAGS=0x%016llx\r\n", cur_context->rip,

View File

@ -18,10 +18,7 @@ enum vm_privilege_level {
};
#define MAX_VM_NAME_LEN 16
struct vm_attr {
uint16_t id; /* Virtual machine identifier */
uint16_t boot_idx; /* Index indicating the boot sequence for this VM */
};
#define INVALID_VM_ID 0xffffU
struct vm_hw_info {
uint16_t num_vcpus; /* Number of total virtual cores */
@ -129,7 +126,7 @@ struct vcpuid_entry {
struct acrn_vpic;
struct vm {
struct vm_attr attr; /* Reference to this VM's attributes */
uint16_t vm_id; /* Virtual machine identifier */
struct vm_hw_info hw; /* Reference to this VM's HW information */
struct vm_sw_info sw; /* Reference to SW associated with this VM */
struct vm_pm_info pm; /* Reference to this VM's arch information */