forked from prplfoundation/prpl-hypervisor
/
vm.c
317 lines (253 loc) · 7.89 KB
/
vm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (c) 2016, prpl Foundation
Permission to use, copy, modify, and/or distribute this software for any purpose with or without
fee is hereby granted, provided that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
This code was written by Carlos Moratelli at Embedded System Group (GSE) at PUCRS/Brazil.
*/
#include <vm.h>
#include <hal.h>
#include <scheduler.h>
#include <libc.h>
#include <vcpu.h>
#include <config.h>
#include <malloc.h>
#include <tlb.h>
#include <types.h>
#include <globals.h>
uint32_t vmconf[][VMCONF_NUMCOLUNS] = VMCONF;
uint32_t vmconf_rt[][VMCONF_NUMCOLUNS] = VMCONF_RT;
uint32_t idleconf[][VMCONF_NUMCOLUNS] = {0, 0, 0, IDLEVCPU, 0, (uint32_t)idlevcpu};
void delete_vcpu(vcpu_t *vcpu) {
ll_node_t *a;
a = (ll_node_t*)((unsigned int)vcpu - sizeof(ll_node_t));
ll_remove(a);
free(a);
}
vm_t *get_vm(unsigned int id) {
ll_node_t *a;
for(a = virtualmachines.head ; a ; a = a->next)
if(((vm_t*)a->ptr)->id == id)
break;
return a ? a->ptr : NULL;
}
void delete_vm(vm_t *dom) {
ll_node_t *nd;
//free((void*)dom->base_addr);
nd = (ll_node_t*)((unsigned int)dom - sizeof(ll_node_t));
ll_remove(nd);
free(nd);
// flush_tlb();
}
/* Cria mapeapmento das VM com base em dados carregados na memoria pelo loader. */
void inMemoryLoad(){
static uint32_t *confaddr = (uint32_t*)0x80300000;
/* Carrega configura a partir do endereço 0x80300000 */
while(*confaddr){
HF_Template[0][0] = *confaddr;
HF_Template[1][0] = TLBIndex;
HF_Template[1][1] = *confaddr/0x1000;
HF_Template[1][2] = *confaddr/0x1000+0x10;
HF_Template[2][0] = TLBIndex;
switch ( *(confaddr+1)){
case HELLFIRE:
HF_Template[0][3] = HELLFIRE;
break;
case BAREOS:
HF_Template[0][3] = BAREOS;
break;
case BAREOS_RT:
HF_Template[0][3] = BAREOS_RT;
break;
default:
Warning("Undentified VM 0x%x.", *(confaddr+1));
}
create_vm(HF_Template);
confaddr += 2;
TLBIndex++;
}
}
/** Initialize the RT VMs */
initializeRTMachines(void){
unsigned int i;
Info("Initializing RT Virtual Machines");
if(vmconf_rt[0][0] != 0){
/* Create initialize the VM structure */
for(i=0; vmconf_rt[i][0]; i+=vmconf_rt[i][2]+1){
create_vm(&vmconf_rt[i]);
}
}else{
//inMemoryLoad();
}
}
/** Initialize the VMs */
void initializeMachines(void) {
unsigned int i;
Info("Initializing Virtual Machines");
/* Pode carregar a tabela de VMs a partir do arquivo config.h ou de configuração carregada na memória a partir do endereço 0x80000500 */
if(vmconf[0][0] != 0){
/* Create initialize the VM structure */
for(i=0; vmconf[i][0]; i+=vmconf[i][2]+1){
create_vm(&vmconf[i]);
}
}else{
// inMemoryLoad();
}
//create_vm(idleconf);
}
/** Create and initialize the structure of the VM abstraction */
vm_t *create_vm(uint32_t vm[][VMCONF_NUMCOLUNS]) {
static uint32_t vm_id = 1; /* vm_id is the guestid */
static uint32_t tlbindex = 0; /* unique tlb entry */
vm_t *ret;
ll_node_t* nd;
uint32_t i;
vcpu_t *vcpu;
/* number of fix tlb entries */
uint32_t ntlbent = vm[0][2];
if(!(nd = (ll_node_t*) calloc(1, sizeof(ll_node_t) + sizeof(vm_t))))
return NULL;
ret = (vm_t*)((unsigned int)nd + sizeof(ll_node_t));
//Memory map
ret->base_addr = vm[0][0];
ret->size = vm[0][1];
ret->id = vm_id++;
ret->os_type = vm[0][3];
ret->ntlbent = ntlbent;
ret->init = 1;
ret->tlbentries = NULL;
#ifdef STATICTLB
/* allocate a tlb entry to the VM */
if(ret->os_type != IDLEVCPU){
ret->tlbentries = (struct tlbentry *)calloc(1, sizeof(struct tlbentry)*(ntlbent));
memset(ret->tlbentries, 0, sizeof(struct tlbentry)*ntlbent);
}else{
ret->tlbentries = NULL;
ret->id = 0;
}
/* fill the tlb entries to the VM */
for(i=0; i<ntlbent; i++, tlbindex++){
ret->tlbentries[i].guestid = vm[i+1][0];
ret->tlbentries[i].index = tlbindex;
ret->tlbentries[i].entrylo0 = vm[i+1][1];
if(ret->tlbentries[i].entrylo0)
ret->tlbentries[i].lo0flags = ENTRYLO_V | ENTRYLO_D;
ret->tlbentries[i].entrylo1 = vm[i+1][2];
if(ret->tlbentries[i].entrylo1)
ret->tlbentries[i].lo1flags = ENTRYLO_V | ENTRYLO_D;
ret->tlbentries[i].pagemask = vm[i+1][3];
ret->tlbentries[i].entryhi = vm[i+1][4];
ret->tlbentries[i].coherency = vm[i+1][5];
ret->tlbentries[i].onhardware = 0;
}
#else
if(ret->os_type != IDLEVCPU){
ret->vmmap = (memVMMap_t *)calloc(1, sizeof(memVMMap_t)*(ntlbent));
memset(ret->vmmap, 0, sizeof(memVMMap_t)*ntlbent);
}else{
ret->vmmap = NULL;
ret->id = 0;
}
/* fill the tlb entries to the VM */
for(i=0; i<ntlbent; i++, tlbindex++){
ret->vmmap[i].phyGuestBase = vm[i+1][1];
ret->vmmap[i].vGuestBase = vm[i+1][4];
ret->vmmap[i].size = vm[i+1][2];
ret->vmmap[i].coherency = vm[i+1][5];
}
#endif
/* Set the VM entry Point and scheduler*/
switch(ret->os_type){
case BAREOS:
vcpu = create_vcpu(ret, vm[0][5], 0 ,0, vm[0][4], BAREOS);
addVcpu_bestEffortList(vcpu);
ll_append(&virtualmachines, nd);
break;
case GENERIC:
vcpu = create_vcpu(ret, 0x80000000, 0, 0, vm[0][4], GENERIC);
addVcpu_bestEffortList(vcpu);
ll_append(&virtualmachines, nd);
break;
case HELLFIRE:
vcpu = create_vcpu(ret, vm[0][5], 0, 0, vm[0][4], HELLFIRE);
addVcpu_bestEffortList(vcpu);
ll_append(&virtualmachines, nd);
break;
case BAREOS_RT:
vcpu = create_vcpu(ret, 0x801000f4, 0, 0, vm[0][4], BAREOS_RT);
addVcpu_servicesInitList(vcpu);
ll_append(&virtualmachines_rt, nd);
break;
case LINUX:
vcpu = create_vcpu(ret, vm[0][5], 0, 0, vm[0][4], LINUX);
addVcpu_bestEffortList(vcpu);
ll_append(&virtualmachines, nd);
break;
case IDLEVCPU:
vcpu = create_vcpu(ret, vm[0][5], 0, 0, vm[0][4], IDLEVCPU);
idle_vcpu = vcpu;
ll_append(&virtualmachines, nd);
break;
default:
Warning("OS type 0x%x supported!\n", ret->os_type);
break;
}
nd->ptr = ret;
return ret;
}
void machine_init_vm(vm_t *d) {
}
vcpu_t *create_vcpu(vm_t *vm, unsigned int entry_point, unsigned int arg, char* stack_pointer, uint32_t pip, uint32_t ostype){
static uint32_t vcpu_id=1;
static uint32_t shadow_gpr_to_assign = 0;
uint32_t num_shadow_gprs;
vcpu_t *ret;
ll_node_t *nd;
Info("Creating VCPUs");
if(!(nd = (ll_node_t *) calloc(1, sizeof(vcpu_t)+sizeof(ll_node_t))))
return NULL;
ret = (vcpu_t*)((unsigned int)nd + sizeof(ll_node_t));
memset(ret, 0, sizeof(vcpu_t));
//Set vcpu id and gprshadowset
//ret->gprshadowset = vcpu_id;
num_shadow_gprs = hal_lr_srsclt();
num_shadow_gprs = (num_shadow_gprs & SRSCTL_HSS) >> SRSCTL_HSS_SHIFT;
if (ostype == IDLEVCPU){
ret->gprshadowset = num_shadow_gprs;
}else
//Highest shadown gpr is used to
if(shadow_gpr_to_assign==num_shadow_gprs){
ret->gprshadowset=shadow_gpr_to_assign-1;
}else{
ret->gprshadowset = shadow_gpr_to_assign;
shadow_gpr_to_assign++;
}
ret->pip = pip;
if (ostype == IDLEVCPU){
ret->id = 0;
}else{
ret->id = vcpu_id;
vcpu_id++;
}
//Not initialized
ret->init=1;
/* initilize the VCPU cp0 registers with the guest cp0 status */
contextSave(ret);
/* Initialize compare and count registers. */
ret->cp0_registers[9][0] = 0;
ret->cp0_registers[11][0] = 0;
ret->pc = entry_point;
ret->sp = (uint32_t)stack_pointer;
ret->arg = arg;
//Vm pointer
ret->vm = vm;
//Adding to local list of vm's vcpus
nd->ptr = ret;
ll_append(&(vm->vcpus), nd);
return ret;
}