blob: 8d5727e5a353b3d944e3a8e5ac915e4babac31b2 [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 Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull9726c252019-01-23 13:44:19 +000019#include <stdatomic.h>
20
Andrew Walbran1f32e722019-06-07 17:57:26 +010021#include "hf/arch/types.h"
22
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/cpu.h"
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000024#include "hf/list.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/mm.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000026#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010027
28#include "vmapi/hf/spci.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029
Andrew Scullae9962e2019-10-03 16:51:16 +010030#define MAX_SMCS 32
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010031#define LOG_BUFFER_SIZE 256
32
Andrew Scullaa039b32018-10-04 15:02:26 +010033enum mailbox_state {
Andrew Walbranc3910f72018-11-27 14:24:36 +000034 /** There is no message in the mailbox. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010035 MAILBOX_STATE_EMPTY,
Andrew Scullaa039b32018-10-04 15:02:26 +010036
Andrew Walbranc3910f72018-11-27 14:24:36 +000037 /** There is a message in the mailbox that is waiting for a reader. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010038 MAILBOX_STATE_RECEIVED,
Andrew Scullaa039b32018-10-04 15:02:26 +010039
Andrew Walbranc3910f72018-11-27 14:24:36 +000040 /** There is a message in the mailbox that has been read. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010041 MAILBOX_STATE_READ,
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010042};
43
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000044struct wait_entry {
45 /** The VM that is waiting for a mailbox to become writable. */
46 struct vm *waiting_vm;
47
48 /**
49 * Links used to add entry to a VM's waiter_list. This is protected by
50 * the notifying VM's lock.
51 */
52 struct list_entry wait_links;
53
54 /**
55 * Links used to add entry to a VM's ready_list. This is protected by
56 * the waiting VM's lock.
57 */
58 struct list_entry ready_links;
59};
60
Andrew Scullaa039b32018-10-04 15:02:26 +010061struct mailbox {
62 enum mailbox_state state;
Andrew Walbran70bc8622019-10-07 14:15:58 +010063 void *recv;
64 const void *send;
65
66 /** The ID of the VM which sent the message currently in `recv`. */
67 spci_vm_id_t recv_sender;
68
69 /** The size of the message currently in `recv`. */
70 uint32_t recv_size;
71
72 /** The attributes of the message currently in `recv`. */
73 uint32_t recv_attributes;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000074
75 /**
76 * List of wait_entry structs representing VMs that want to be notified
77 * when the mailbox becomes writable. Once the mailbox does become
78 * writable, the entry is removed from this list and added to the
79 * waiting VM's ready_list.
80 */
81 struct list_entry waiter_list;
82
83 /**
84 * List of wait_entry structs representing VMs whose mailboxes became
85 * writable since the owner of the mailbox registers for notification.
86 */
87 struct list_entry ready_list;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010088};
89
Andrew Scullae9962e2019-10-03 16:51:16 +010090struct smc_whitelist {
91 uint32_t smcs[MAX_SMCS];
92 uint16_t smc_count;
93 bool permissive;
94};
95
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096struct vm {
Andrew Walbran95534922019-06-19 11:32:54 +010097 spci_vm_id_t id;
Andrew Scullae9962e2019-10-03 16:51:16 +010098 struct smc_whitelist smc_whitelist;
99
Andrew Walbran0d7a0682018-12-06 16:48:47 +0000100 /** See api.c for the partial ordering on locks. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100101 struct spinlock lock;
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100102 spci_vcpu_count_t vcpu_count;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100103 struct vcpu vcpus[MAX_CPUS];
Andrew Scull89a75242018-08-06 17:04:55 +0100104 struct mm_ptable ptable;
Andrew Scullaa039b32018-10-04 15:02:26 +0100105 struct mailbox mailbox;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100106 char log_buffer[LOG_BUFFER_SIZE];
Andrew Scullae9962e2019-10-03 16:51:16 +0100107 uint16_t log_buffer_length;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000108
Andrew Walbranf76f5752019-12-03 18:33:08 +0000109 /**
110 * Wait entries to be used when waiting on other VM mailboxes. See
111 * comments on `struct wait_entry` for the lock discipline of these.
112 */
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000113 struct wait_entry wait_entries[MAX_VMS];
Andrew Scull9726c252019-01-23 13:44:19 +0000114
115 atomic_bool aborting;
Andrew Walbran1f32e722019-06-07 17:57:26 +0100116
117 /** Arch-specific VM information. */
118 struct arch_vm arch;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000119};
120
121/** Encapsulates a VM whose lock is held. */
122struct vm_locked {
123 struct vm *vm;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100124};
125
Jose Marinho75509b42019-04-09 09:34:59 +0100126/** Container for two vm_locked structures */
127struct two_vm_locked {
128 struct vm_locked vm1;
129 struct vm_locked vm2;
130};
131
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100132bool vm_init(spci_vcpu_count_t vcpu_count, struct mpool *ppool,
133 struct vm **new_vm);
Andrew Walbran52d99672019-06-25 15:51:11 +0100134spci_vm_count_t vm_get_count(void);
Andrew Walbran42347a92019-05-09 13:59:03 +0100135struct vm *vm_find(spci_vm_id_t id);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100136struct vm_locked vm_lock(struct vm *vm);
Jose Marinho75509b42019-04-09 09:34:59 +0100137struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000138void vm_unlock(struct vm_locked *locked);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100139struct vcpu *vm_get_vcpu(struct vm *vm, spci_vcpu_index_t vcpu_index);
Andrew Walbranaad8f982019-12-04 10:56:39 +0000140struct wait_entry *vm_get_wait_entry(struct vm *vm, spci_vm_id_t for_vm);
141spci_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry);
Andrew Scull3c257452019-11-26 13:32:50 +0000142
143bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
144 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa);
145bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
146 uint32_t mode, struct mpool *ppool);
147void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
148 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa);
149bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
150 struct mpool *ppool);
151bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool);