blob: 9bc616f9376235923b67603fd75fa904503bfe34 [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
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000028bool vm_init(uint32_t vcpu_count, struct mpool *ppool, 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
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000041 list_init(&vm->mailbox.waiter_list);
42 list_init(&vm->mailbox.ready_list);
43 sl_init(&vm->lock);
44
Andrew Scull19503262018-09-20 14:48:39 +010045 vm->id = vm_count;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010046 vm->vcpu_count = vcpu_count;
Andrew Scullaa039b32018-10-04 15:02:26 +010047 vm->mailbox.state = mailbox_state_empty;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010048
Andrew Scullda3df7f2019-01-05 17:49:27 +000049 if (!mm_vm_init(&vm->ptable, ppool)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +000050 return false;
51 }
52
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000053 /* Initialise waiter entries. */
54 for (i = 0; i < MAX_VMS; i++) {
55 vm->wentry[i].waiting_vm = vm;
56 list_init(&vm->wentry[i].wait_links);
57 list_init(&vm->wentry[i].ready_links);
58 }
59
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010060 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010061 for (i = 0; i < vcpu_count; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010062 vcpu_init(&vm->vcpus[i], vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010063 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064
Andrew Scull19503262018-09-20 14:48:39 +010065 ++vm_count;
66 *new_vm = vm;
67
Wedson Almeida Filho03306112018-11-26 00:08:03 +000068 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010069}
70
Andrew Scull19503262018-09-20 14:48:39 +010071uint32_t vm_get_count(void)
72{
73 return vm_count;
74}
75
76struct vm *vm_get(uint32_t id)
77{
78 /* Ensure the VM is initialized. */
79 if (id >= vm_count) {
80 return NULL;
81 }
82
83 return &vms[id];
84}
85
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000086/**
87 * Locks the given VM and updates `locked` to hold the newly locked vm.
88 */
89void vm_lock(struct vm *vm, struct vm_locked *locked)
90{
91 sl_lock(&vm->lock);
92 locked->vm = vm;
93}
94
95/**
96 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
97 * the fact that the VM is no longer locked.
98 */
99void vm_unlock(struct vm_locked *locked)
100{
101 sl_unlock(&locked->vm->lock);
102 locked->vm = NULL;
103}
104
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100105/* TODO: Shall we use index or id here? */
Andrew Scull37402872018-10-24 14:23:06 +0100106void vm_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100107{
Andrew Scullf3d45592018-09-20 14:30:22 +0100108 struct vcpu *vcpu = &vm->vcpus[index];
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000109
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100110 if (index < vm->vcpu_count) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000111 arch_regs_set_pc_arg(&vcpu->regs, entry, arg);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100112 vcpu_on(vcpu);
113 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100114}