blob: d03aba8cdef009788aca4c42cc651a09d1f138b9 [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
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01007bool vm_init(struct vm *vm, uint32_t id, uint32_t vcpu_count)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01008{
Wedson Almeida Filho87009642018-07-02 10:20:07 +01009 uint32_t i;
10
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010011 memset(vm, 0, sizeof(*vm));
12
Andrew Scull8c3a63a2018-09-20 13:38:34 +010013 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010014 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010015 vm->rpc.state = rpc_state_idle;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010016
17 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010018 for (i = 0; i < vcpu_count; i++) {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010019 vcpu_init(vm->vcpus + i, vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010020 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010021
Andrew Scull8c3a63a2018-09-20 13:38:34 +010022 return mm_ptable_init(&vm->ptable, 0);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010023}
24
25/* TODO: Shall we use index or id here? */
Andrew Scull89a75242018-08-06 17:04:55 +010026void vm_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, size_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027{
28 struct vcpu *vcpu = vm->vcpus + index;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010029 if (index < vm->vcpu_count) {
Andrew Scull89a75242018-08-06 17:04:55 +010030 arch_regs_init(&vcpu->regs, entry, arg);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010031 vcpu_on(vcpu);
32 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010033}
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010034
35void vm_set_current(struct vm *vm)
36{
Wedson Almeida Filho52bb3f92018-07-30 15:52:38 +010037 arch_cpu_update(vm == &primary_vm);
Andrew Scull8c3a63a2018-09-20 13:38:34 +010038 arch_mm_set_vm(vm->id, vm->ptable.table);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010039}