blob: 532d5a255d497d9989ad1c649518239c9a1c9eb7 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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"
Andrew Scull8d9e1212019-04-05 13:52:55 +010021#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
Andrew Scull2b5fbad2019-04-05 13:55:56 +010039 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010040
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 Sculld6ee1102019-04-05 22:12:42 +010047 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000048 atomic_init(&vm->aborting, false);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010049
Andrew Scullda3df7f2019-01-05 17:49:27 +000050 if (!mm_vm_init(&vm->ptable, ppool)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +000051 return false;
52 }
53
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000054 /* Initialise waiter entries. */
55 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000056 vm->wait_entries[i].waiting_vm = vm;
57 list_init(&vm->wait_entries[i].wait_links);
58 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000059 }
60
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010061 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010062 for (i = 0; i < vcpu_count; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010063 vcpu_init(&vm->vcpus[i], vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010064 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010065
Andrew Scull19503262018-09-20 14:48:39 +010066 ++vm_count;
67 *new_vm = vm;
68
Wedson Almeida Filho03306112018-11-26 00:08:03 +000069 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010070}
71
Andrew Scull19503262018-09-20 14:48:39 +010072uint32_t vm_get_count(void)
73{
74 return vm_count;
75}
76
77struct vm *vm_get(uint32_t id)
78{
79 /* Ensure the VM is initialized. */
80 if (id >= vm_count) {
81 return NULL;
82 }
83
84 return &vms[id];
85}
86
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000087/**
88 * Locks the given VM and updates `locked` to hold the newly locked vm.
89 */
90void vm_lock(struct vm *vm, struct vm_locked *locked)
91{
92 sl_lock(&vm->lock);
93 locked->vm = vm;
94}
95
96/**
97 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
98 * the fact that the VM is no longer locked.
99 */
100void vm_unlock(struct vm_locked *locked)
101{
102 sl_unlock(&locked->vm->lock);
103 locked->vm = NULL;
104}
105
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000106/**
107 * Starts a vCPU of a secondary VM.
108 *
109 * TODO: Shall we use index or id here?
110 */
111void vm_secondary_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry,
112 uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100113{
Andrew Scullf3d45592018-09-20 14:30:22 +0100114 struct vcpu *vcpu = &vm->vcpus[index];
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000115
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000116 if (index >= vm->vcpu_count) {
117 return;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100118 }
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000119
120 /*
121 * Set vCPU registers to a clean state ready for boot. As this is a
122 * secondary which can migrate between pCPUs, the ID of the vCPU is
123 * defined as the index and does not match the ID of the pCPU it is
124 * running on.
125 */
126 arch_regs_reset(&vcpu->regs, vm->id == HF_PRIMARY_VM_ID, vm->id, index,
127 vm->ptable.root);
128 vcpu_on(vcpu, entry, arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100129}