blob: 095868215a111a84c8f9d18055dfeddf7978c87c [file] [log] [blame]
Andrew Scull18c78fc2018-08-20 12:57:41 +01001#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01002
Andrew Scull18c78fc2018-08-20 12:57:41 +01003#include "hf/api.h"
4#include "hf/cpu.h"
5#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01006
Andrew Scull19503262018-09-20 14:48:39 +01007#include "vmapi/hf/call.h"
8
9static struct vm vms[MAX_VMS];
10static uint32_t vm_count;
11
12bool vm_init(uint32_t vcpu_count, struct vm **new_vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010013{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010014 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010015 struct vm *vm;
16
17 if (vm_count >= MAX_VMS) {
18 return false;
19 }
20
21 vm = &vms[vm_count];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010022
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010023 memset(vm, 0, sizeof(*vm));
24
Andrew Scull19503262018-09-20 14:48:39 +010025 vm->id = vm_count;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010026 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010027 vm->rpc.state = rpc_state_idle;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010028
29 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010030 for (i = 0; i < vcpu_count; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010031 vcpu_init(&vm->vcpus[i], vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010032 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033
Andrew Scull19503262018-09-20 14:48:39 +010034 ++vm_count;
35 *new_vm = vm;
36
Andrew Scull8c3a63a2018-09-20 13:38:34 +010037 return mm_ptable_init(&vm->ptable, 0);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038}
39
Andrew Scull19503262018-09-20 14:48:39 +010040uint32_t vm_get_count(void)
41{
42 return vm_count;
43}
44
45struct vm *vm_get(uint32_t id)
46{
47 /* Ensure the VM is initialized. */
48 if (id >= vm_count) {
49 return NULL;
50 }
51
52 return &vms[id];
53}
54
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010055/* TODO: Shall we use index or id here? */
Andrew Scull89a75242018-08-06 17:04:55 +010056void vm_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, size_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010057{
Andrew Scullf3d45592018-09-20 14:30:22 +010058 struct vcpu *vcpu = &vm->vcpus[index];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010059 if (index < vm->vcpu_count) {
Andrew Scull89a75242018-08-06 17:04:55 +010060 arch_regs_init(&vcpu->regs, entry, arg);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010061 vcpu_on(vcpu);
62 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063}
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010064
65void vm_set_current(struct vm *vm)
66{
Andrew Scull19503262018-09-20 14:48:39 +010067 arch_cpu_update(vm->id == HF_PRIMARY_VM_ID);
Andrew Scull8c3a63a2018-09-20 13:38:34 +010068 arch_mm_set_vm(vm->id, vm->ptable.table);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010069}