@@ -90,99 +90,119 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
90
90
{
91
91
unsigned int id ;
92
92
struct vm * vm ;
93
- int status = 0 ;
93
+ int status ;
94
94
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
+ }
107
99
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
+ }
108
106
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 );
111
111
112
- if (vm -> hw .num_vcpus == 0 )
113
- vm -> hw .num_vcpus = phy_cpu_num ;
114
112
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 );
119
115
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 ;
126
118
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
+ }
128
126
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 ;
135
144
#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 ;
137
148
#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
+ }
146
157
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 );
151
162
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 );
154
167
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 );
159
169
160
- vm_setup_cpu_state (vm );
170
+ /* Create virtual uart */
171
+ if (is_vm0 (vm ))
172
+ vm -> vuart = vuart_init (vm );
161
173
162
- /* Create virtual uart */
163
- if (is_vm0 (vm ))
164
- vm -> vuart = vuart_init (vm );
174
+ vm -> vpic = vpic_init (vm );
165
175
166
- vm -> vpic = vpic_init (vm );
176
+ /* vpic wire_mode default is INTR */
177
+ vm -> vpic_wire_mode = VPIC_WIRE_INTR ;
167
178
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
+ }
170
185
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 ;
173
189
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 ;
177
193
178
- status = set_vcpuid_entries (vm );
179
- if (status )
180
- vm -> state = VM_CREATED ;
181
- }
194
+ vm -> state = VM_CREATED ;
182
195
183
- }
196
+ return 0 ;
184
197
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 );
186
206
return status ;
187
207
}
188
208
@@ -300,7 +320,8 @@ int prepare_vm0(void)
300
320
struct vm_description * vm_desc = & vm0_desc ;
301
321
302
322
ret = create_vm (vm_desc , & vm );
303
- ASSERT (ret == 0 , "VM creation failed!" );
323
+ if (ret != 0 )
324
+ return ret ;
304
325
305
326
/* Allocate all cpus to vm0 at the beginning */
306
327
for (i = 0 ; i < phy_cpu_num ; i ++ )
0 commit comments