Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 19 | #include <stdatomic.h> |
| 20 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 21 | #include "hf/arch/types.h" |
| 22 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/cpu.h" |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 24 | #include "hf/list.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 25 | #include "hf/mm.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 26 | #include "hf/mpool.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 27 | |
| 28 | #include "vmapi/hf/spci.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 29 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 30 | #define MAX_SMCS 32 |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 31 | #define LOG_BUFFER_SIZE 256 |
| 32 | |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 33 | /** |
| 34 | * The state of an RX buffer. |
| 35 | * |
| 36 | * EMPTY is the initial state. The follow state transitions are possible: |
| 37 | * * EMPTY → RECEIVED: message sent to the VM. |
| 38 | * * RECEIVED → READ: secondary VM returns from SPCI_MSG_WAIT or |
| 39 | * SPCI_MSG_POLL, or primary VM returns from SPCI_RUN with an SPCI_MSG_SEND |
| 40 | * where the receiver is itself. |
| 41 | * * READ → EMPTY: VM called SPCI_RX_RELEASE. |
| 42 | */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 43 | enum mailbox_state { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 44 | /** There is no message in the mailbox. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 45 | MAILBOX_STATE_EMPTY, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 46 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 47 | /** There is a message in the mailbox that is waiting for a reader. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 48 | MAILBOX_STATE_RECEIVED, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 49 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 50 | /** There is a message in the mailbox that has been read. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 51 | MAILBOX_STATE_READ, |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 54 | struct wait_entry { |
| 55 | /** The VM that is waiting for a mailbox to become writable. */ |
| 56 | struct vm *waiting_vm; |
| 57 | |
| 58 | /** |
| 59 | * Links used to add entry to a VM's waiter_list. This is protected by |
| 60 | * the notifying VM's lock. |
| 61 | */ |
| 62 | struct list_entry wait_links; |
| 63 | |
| 64 | /** |
| 65 | * Links used to add entry to a VM's ready_list. This is protected by |
| 66 | * the waiting VM's lock. |
| 67 | */ |
| 68 | struct list_entry ready_links; |
| 69 | }; |
| 70 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 71 | struct mailbox { |
| 72 | enum mailbox_state state; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 73 | void *recv; |
| 74 | const void *send; |
| 75 | |
| 76 | /** The ID of the VM which sent the message currently in `recv`. */ |
| 77 | spci_vm_id_t recv_sender; |
| 78 | |
| 79 | /** The size of the message currently in `recv`. */ |
| 80 | uint32_t recv_size; |
| 81 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 82 | /** |
| 83 | * The SPCI function ID to use to deliver the message currently in |
| 84 | * `recv`. |
| 85 | */ |
| 86 | uint32_t recv_func; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * List of wait_entry structs representing VMs that want to be notified |
| 90 | * when the mailbox becomes writable. Once the mailbox does become |
| 91 | * writable, the entry is removed from this list and added to the |
| 92 | * waiting VM's ready_list. |
| 93 | */ |
| 94 | struct list_entry waiter_list; |
| 95 | |
| 96 | /** |
| 97 | * List of wait_entry structs representing VMs whose mailboxes became |
| 98 | * writable since the owner of the mailbox registers for notification. |
| 99 | */ |
| 100 | struct list_entry ready_list; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 103 | struct smc_whitelist { |
| 104 | uint32_t smcs[MAX_SMCS]; |
| 105 | uint16_t smc_count; |
| 106 | bool permissive; |
| 107 | }; |
| 108 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 109 | struct vm { |
Andrew Walbran | 9553492 | 2019-06-19 11:32:54 +0100 | [diff] [blame] | 110 | spci_vm_id_t id; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 111 | struct smc_whitelist smc_whitelist; |
| 112 | |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame] | 113 | /** See api.c for the partial ordering on locks. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 114 | struct spinlock lock; |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 115 | spci_vcpu_count_t vcpu_count; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 116 | struct vcpu vcpus[MAX_CPUS]; |
Andrew Scull | 89a7524 | 2018-08-06 17:04:55 +0100 | [diff] [blame] | 117 | struct mm_ptable ptable; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 118 | struct mailbox mailbox; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 119 | char log_buffer[LOG_BUFFER_SIZE]; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 120 | uint16_t log_buffer_length; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 121 | |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 122 | /** |
| 123 | * Wait entries to be used when waiting on other VM mailboxes. See |
| 124 | * comments on `struct wait_entry` for the lock discipline of these. |
| 125 | */ |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 126 | struct wait_entry wait_entries[MAX_VMS]; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 127 | |
| 128 | atomic_bool aborting; |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 129 | |
| 130 | /** Arch-specific VM information. */ |
| 131 | struct arch_vm arch; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /** Encapsulates a VM whose lock is held. */ |
| 135 | struct vm_locked { |
| 136 | struct vm *vm; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 139 | /** Container for two vm_locked structures */ |
| 140 | struct two_vm_locked { |
| 141 | struct vm_locked vm1; |
| 142 | struct vm_locked vm2; |
| 143 | }; |
| 144 | |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 145 | bool vm_init(spci_vcpu_count_t vcpu_count, struct mpool *ppool, |
| 146 | struct vm **new_vm); |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 147 | spci_vm_count_t vm_get_count(void); |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 148 | struct vm *vm_find(spci_vm_id_t id); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 149 | struct vm_locked vm_lock(struct vm *vm); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 150 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 151 | void vm_unlock(struct vm_locked *locked); |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 152 | struct vcpu *vm_get_vcpu(struct vm *vm, spci_vcpu_index_t vcpu_index); |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 153 | struct wait_entry *vm_get_wait_entry(struct vm *vm, spci_vm_id_t for_vm); |
| 154 | spci_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 155 | |
| 156 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 157 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
| 158 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 159 | uint32_t mode, struct mpool *ppool); |
| 160 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 161 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
| 162 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 163 | struct mpool *ppool); |
| 164 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool); |