blob: 02b60ff97314ea57ed2d3c7d46ad92cf4a37716b [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/api.h"
20#include "hf/cpu.h"
21#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010022
Andrew Scull19503262018-09-20 14:48:39 +010023#include "vmapi/hf/call.h"
24
25static struct vm vms[MAX_VMS];
26static uint32_t vm_count;
27
28bool vm_init(uint32_t vcpu_count, struct vm **new_vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010030 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010031 struct vm *vm;
32
33 if (vm_count >= MAX_VMS) {
34 return false;
35 }
36
37 vm = &vms[vm_count];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010038
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010039 memset(vm, 0, sizeof(*vm));
40
Andrew Scull19503262018-09-20 14:48:39 +010041 vm->id = vm_count;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010042 vm->vcpu_count = vcpu_count;
Andrew Scullaa039b32018-10-04 15:02:26 +010043 vm->mailbox.state = mailbox_state_empty;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010044
45 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010046 for (i = 0; i < vcpu_count; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010047 vcpu_init(&vm->vcpus[i], vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010048 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010049
Andrew Scull19503262018-09-20 14:48:39 +010050 ++vm_count;
51 *new_vm = vm;
52
Andrew Scull8c3a63a2018-09-20 13:38:34 +010053 return mm_ptable_init(&vm->ptable, 0);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054}
55
Andrew Scull19503262018-09-20 14:48:39 +010056uint32_t vm_get_count(void)
57{
58 return vm_count;
59}
60
61struct vm *vm_get(uint32_t id)
62{
63 /* Ensure the VM is initialized. */
64 if (id >= vm_count) {
65 return NULL;
66 }
67
68 return &vms[id];
69}
70
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010071/* TODO: Shall we use index or id here? */
Andrew Scull37402872018-10-24 14:23:06 +010072void vm_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073{
Andrew Scullf3d45592018-09-20 14:30:22 +010074 struct vcpu *vcpu = &vm->vcpus[index];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010075 if (index < vm->vcpu_count) {
Andrew Scull89a75242018-08-06 17:04:55 +010076 arch_regs_init(&vcpu->regs, entry, arg);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010077 vcpu_on(vcpu);
78 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010079}
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010080
81void vm_set_current(struct vm *vm)
82{
Andrew Scull19503262018-09-20 14:48:39 +010083 arch_cpu_update(vm->id == HF_PRIMARY_VM_ID);
Andrew Scull8c3a63a2018-09-20 13:38:34 +010084 arch_mm_set_vm(vm->id, vm->ptable.table);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010085}