blob: fd477cd2d4bcda7b8828ff7b91056a8848076908 [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"
Andrew Scull877ae4b2019-07-02 12:52:33 +010020#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/cpu.h"
Andrew Walbranb037d5b2019-06-25 17:19:41 +010022#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024
Andrew Scull19503262018-09-20 14:48:39 +010025#include "vmapi/hf/call.h"
26
27static struct vm vms[MAX_VMS];
Andrew Walbran52d99672019-06-25 15:51:11 +010028static spci_vm_count_t vm_count;
Andrew Scull19503262018-09-20 14:48:39 +010029
Andrew Walbranc6d23c42019-06-26 13:30:42 +010030bool vm_init(spci_vcpu_count_t vcpu_count, struct mpool *ppool,
31 struct vm **new_vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010032{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010033 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010034 struct vm *vm;
35
36 if (vm_count >= MAX_VMS) {
37 return false;
38 }
39
40 vm = &vms[vm_count];
Wedson Almeida Filho87009642018-07-02 10:20:07 +010041
Andrew Scull2b5fbad2019-04-05 13:55:56 +010042 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010043
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000044 list_init(&vm->mailbox.waiter_list);
45 list_init(&vm->mailbox.ready_list);
46 sl_init(&vm->lock);
47
Andrew Scull19503262018-09-20 14:48:39 +010048 vm->id = vm_count;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010049 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010050 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000051 atomic_init(&vm->aborting, false);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010052
Andrew Scullda3df7f2019-01-05 17:49:27 +000053 if (!mm_vm_init(&vm->ptable, ppool)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +000054 return false;
55 }
56
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000057 /* Initialise waiter entries. */
58 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000059 vm->wait_entries[i].waiting_vm = vm;
60 list_init(&vm->wait_entries[i].wait_links);
61 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000062 }
63
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064 /* Do basic initialization of vcpus. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010065 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010066 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010067 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010068
Andrew Scull19503262018-09-20 14:48:39 +010069 ++vm_count;
70 *new_vm = vm;
71
Wedson Almeida Filho03306112018-11-26 00:08:03 +000072 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073}
74
Andrew Walbran52d99672019-06-25 15:51:11 +010075spci_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +010076{
77 return vm_count;
78}
79
Andrew Walbran42347a92019-05-09 13:59:03 +010080struct vm *vm_find(spci_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +010081{
82 /* Ensure the VM is initialized. */
83 if (id >= vm_count) {
84 return NULL;
85 }
86
87 return &vms[id];
88}
89
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000090/**
91 * Locks the given VM and updates `locked` to hold the newly locked vm.
92 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +010093struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000094{
Andrew Walbran7e932bd2019-04-29 16:47:06 +010095 struct vm_locked locked = {
96 .vm = vm,
97 };
98
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000099 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100100
101 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000102}
103
104/**
Jose Marinho75509b42019-04-09 09:34:59 +0100105 * Locks two VMs ensuring that the locking order is according to the locks'
106 * addresses.
107 */
108struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
109{
110 struct two_vm_locked dual_lock;
111
112 sl_lock_both(&vm1->lock, &vm2->lock);
113 dual_lock.vm1.vm = vm1;
114 dual_lock.vm2.vm = vm2;
115
116 return dual_lock;
117}
118
119/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000120 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
121 * the fact that the VM is no longer locked.
122 */
123void vm_unlock(struct vm_locked *locked)
124{
125 sl_unlock(&locked->vm->lock);
126 locked->vm = NULL;
127}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100128
129/**
130 * Get the vCPU with the given index from the given VM.
131 * This assumes the index is valid, i.e. less than vm->vcpu_count.
132 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100133struct vcpu *vm_get_vcpu(struct vm *vm, spci_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100134{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100135 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100136 return &vm->vcpus[vcpu_index];
137}