Skip to content

Commit e0c329e

Browse files
lifeixlijinxia
authored andcommitted
hv: create vm failed don't panic system
Just return error number to the caller. Signed-off-by: Li, Fei1 <fei1.li@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
1 parent 6c8fc0a commit e0c329e

File tree

2 files changed

+98
-74
lines changed

2 files changed

+98
-74
lines changed

hypervisor/arch/x86/guest/vm.c

Lines changed: 93 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -90,99 +90,119 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
9090
{
9191
unsigned int id;
9292
struct vm *vm;
93-
int status = 0;
93+
int status;
9494

95-
if ((vm_desc == NULL) || (rtn_vm == NULL))
96-
status = -EINVAL;
97-
98-
if (status == 0) {
99-
/* Allocate memory for virtual machine */
100-
vm = calloc(1, sizeof(struct vm));
101-
ASSERT(vm != NULL, "vm allocation failed");
102-
103-
/*
104-
* Map Virtual Machine to its VM Description
105-
*/
106-
init_vm(vm_desc, vm);
95+
if ((vm_desc == NULL) || (rtn_vm == NULL)) {
96+
pr_err("%s, invalid paramater\n", __func__);
97+
return -EINVAL;
98+
}
10799

100+
/* Allocate memory for virtual machine */
101+
vm = calloc(1, sizeof(struct vm));
102+
if (vm == NULL) {
103+
pr_err("%s, vm allocation failed\n", __func__);
104+
return -ENOMEM;
105+
}
108106

109-
/* Init mmio list */
110-
INIT_LIST_HEAD(&vm->mmio_list);
107+
/*
108+
* Map Virtual Machine to its VM Description
109+
*/
110+
init_vm(vm_desc, vm);
111111

112-
if (vm->hw.num_vcpus == 0)
113-
vm->hw.num_vcpus = phy_cpu_num;
114112

115-
vm->hw.vcpu_array =
116-
calloc(1, sizeof(struct vcpu *) * vm->hw.num_vcpus);
117-
ASSERT(vm->hw.vcpu_array != NULL,
118-
"vcpu_array allocation failed");
113+
/* Init mmio list */
114+
INIT_LIST_HEAD(&vm->mmio_list);
119115

120-
for (id = 0; id < sizeof(long) * 8; id++)
121-
if (bitmap_test_and_set(id, &vmid_bitmap) == 0)
122-
break;
123-
vm->attr.id = vm->attr.boot_idx = id;
124-
snprintf(&vm->attr.name[0], MAX_VM_NAME_LEN, "vm_%d",
125-
vm->attr.id);
116+
if (vm->hw.num_vcpus == 0)
117+
vm->hw.num_vcpus = phy_cpu_num;
126118

127-
atomic_store(&vm->hw.created_vcpus, 0);
119+
vm->hw.vcpu_array =
120+
calloc(1, sizeof(struct vcpu *) * vm->hw.num_vcpus);
121+
if (vm->hw.vcpu_array == NULL) {
122+
pr_err("%s, vcpu_array allocation failed\n", __func__);
123+
status = -ENOMEM;
124+
goto err1;
125+
}
128126

129-
/* gpa_lowtop are used for system start up */
130-
vm->hw.gpa_lowtop = 0;
131-
/* Only for SOS: Configure VM software information */
132-
/* For UOS: This VM software information is configure in DM */
133-
if (is_vm0(vm)) {
134-
prepare_vm0_memmap_and_e820(vm);
127+
for (id = 0; id < sizeof(long) * 8; id++)
128+
if (bitmap_test_and_set(id, &vmid_bitmap) == 0)
129+
break;
130+
vm->attr.id = vm->attr.boot_idx = id;
131+
snprintf(&vm->attr.name[0], MAX_VM_NAME_LEN, "vm_%d",
132+
vm->attr.id);
133+
134+
atomic_store(&vm->hw.created_vcpus, 0);
135+
136+
/* gpa_lowtop are used for system start up */
137+
vm->hw.gpa_lowtop = 0;
138+
/* Only for SOS: Configure VM software information */
139+
/* For UOS: This VM software information is configure in DM */
140+
if (is_vm0(vm)) {
141+
status = prepare_vm0_memmap_and_e820(vm);
142+
if (status != 0)
143+
goto err2;
135144
#ifndef CONFIG_EFI_STUB
136-
status = init_vm0_boot_info(vm);
145+
status = init_vm0_boot_info(vm);
146+
if (status != 0)
147+
goto err2;
137148
#endif
138-
} else {
139-
/* populate UOS vm fields according to vm_desc */
140-
vm->sworld_control.sworld_enabled =
141-
vm_desc->sworld_enabled;
142-
memcpy_s(&vm->GUID[0], sizeof(vm->GUID),
143-
&vm_desc->GUID[0],
144-
sizeof(vm_desc->GUID));
145-
}
149+
} else {
150+
/* populate UOS vm fields according to vm_desc */
151+
vm->sworld_control.sworld_enabled =
152+
vm_desc->sworld_enabled;
153+
memcpy_s(&vm->GUID[0], sizeof(vm->GUID),
154+
&vm_desc->GUID[0],
155+
sizeof(vm_desc->GUID));
156+
}
146157

147-
INIT_LIST_HEAD(&vm->list);
148-
spinlock_obtain(&vm_list_lock);
149-
list_add(&vm->list, &vm_list);
150-
spinlock_release(&vm_list_lock);
158+
INIT_LIST_HEAD(&vm->list);
159+
spinlock_obtain(&vm_list_lock);
160+
list_add(&vm->list, &vm_list);
161+
spinlock_release(&vm_list_lock);
151162

152-
/* Ensure VM software information obtained */
153-
if (status == 0) {
163+
/* Set up IO bit-mask such that VM exit occurs on
164+
* selected IO ranges
165+
*/
166+
setup_io_bitmap(vm);
154167

155-
/* Set up IO bit-mask such that VM exit occurs on
156-
* selected IO ranges
157-
*/
158-
setup_io_bitmap(vm);
168+
vm_setup_cpu_state(vm);
159169

160-
vm_setup_cpu_state(vm);
170+
/* Create virtual uart */
171+
if (is_vm0(vm))
172+
vm->vuart = vuart_init(vm);
161173

162-
/* Create virtual uart */
163-
if (is_vm0(vm))
164-
vm->vuart = vuart_init(vm);
174+
vm->vpic = vpic_init(vm);
165175

166-
vm->vpic = vpic_init(vm);
176+
/* vpic wire_mode default is INTR */
177+
vm->vpic_wire_mode = VPIC_WIRE_INTR;
167178

168-
/* vpic wire_mode default is INTR */
169-
vm->vpic_wire_mode = VPIC_WIRE_INTR;
179+
/* Allocate full emulated vIOAPIC instance */
180+
vm->arch_vm.virt_ioapic = vioapic_init(vm);
181+
if (vm->arch_vm.virt_ioapic == NULL) {
182+
status = -ENODEV;
183+
goto err3;
184+
}
170185

171-
/* Allocate full emulated vIOAPIC instance */
172-
vm->arch_vm.virt_ioapic = vioapic_init(vm);
186+
/* Populate return VM handle */
187+
*rtn_vm = vm;
188+
vm->sw.io_shared_page = NULL;
173189

174-
/* Populate return VM handle */
175-
*rtn_vm = vm;
176-
vm->sw.io_shared_page = NULL;
190+
status = set_vcpuid_entries(vm);
191+
if (status != 0)
192+
goto err4;
177193

178-
status = set_vcpuid_entries(vm);
179-
if (status)
180-
vm->state = VM_CREATED;
181-
}
194+
vm->state = VM_CREATED;
182195

183-
}
196+
return 0;
184197

185-
/* Return status to caller */
198+
err4:
199+
vioapic_cleanup(vm->arch_vm.virt_ioapic);
200+
err3:
201+
vpic_cleanup(vm);
202+
err2:
203+
free(vm->hw.vcpu_array);
204+
err1:
205+
free(vm);
186206
return status;
187207
}
188208

@@ -300,7 +320,8 @@ int prepare_vm0(void)
300320
struct vm_description *vm_desc = &vm0_desc;
301321

302322
ret = create_vm(vm_desc, &vm);
303-
ASSERT(ret == 0, "VM creation failed!");
323+
if (ret != 0)
324+
return ret;
304325

305326
/* Allocate all cpus to vm0 at the beginning */
306327
for (i = 0; i < phy_cpu_num; i++)

hypervisor/common/hv_main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ int hv_main(int cpu_id)
159159
/* X2APIC mode is disabled by default. */
160160
x2apic_enabled = false;
161161

162-
if (is_vm0_bsp(cpu_id))
163-
prepare_vm0();
162+
if (is_vm0_bsp(cpu_id)) {
163+
ret = prepare_vm0();
164+
if (ret != 0)
165+
return ret;
166+
}
164167

165168
default_idle();
166169

0 commit comments

Comments
 (0)