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 Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/cpu.h" |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 22 | #include "hf/list.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/mm.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 24 | #include "hf/mpool.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 25 | #include "hf/spci.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 26 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 27 | enum mailbox_state { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 28 | /** There is no message in the mailbox. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 29 | MAILBOX_STATE_EMPTY, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 30 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 31 | /** 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] | 32 | MAILBOX_STATE_RECEIVED, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 33 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 34 | /** There is a message in the mailbox that has been read. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 35 | MAILBOX_STATE_READ, |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 38 | struct wait_entry { |
| 39 | /** The VM that is waiting for a mailbox to become writable. */ |
| 40 | struct vm *waiting_vm; |
| 41 | |
| 42 | /** |
| 43 | * Links used to add entry to a VM's waiter_list. This is protected by |
| 44 | * the notifying VM's lock. |
| 45 | */ |
| 46 | struct list_entry wait_links; |
| 47 | |
| 48 | /** |
| 49 | * Links used to add entry to a VM's ready_list. This is protected by |
| 50 | * the waiting VM's lock. |
| 51 | */ |
| 52 | struct list_entry ready_links; |
| 53 | }; |
| 54 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 55 | struct mailbox { |
| 56 | enum mailbox_state state; |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 57 | struct spci_message *recv; |
| 58 | const struct spci_message *send; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * List of wait_entry structs representing VMs that want to be notified |
| 62 | * when the mailbox becomes writable. Once the mailbox does become |
| 63 | * writable, the entry is removed from this list and added to the |
| 64 | * waiting VM's ready_list. |
| 65 | */ |
| 66 | struct list_entry waiter_list; |
| 67 | |
| 68 | /** |
| 69 | * List of wait_entry structs representing VMs whose mailboxes became |
| 70 | * writable since the owner of the mailbox registers for notification. |
| 71 | */ |
| 72 | struct list_entry ready_list; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 75 | struct vm { |
Andrew Scull | 8c3a63a | 2018-09-20 13:38:34 +0100 | [diff] [blame] | 76 | uint32_t id; |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame] | 77 | /** See api.c for the partial ordering on locks. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 78 | struct spinlock lock; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 79 | uint32_t vcpu_count; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 80 | struct vcpu vcpus[MAX_CPUS]; |
Andrew Scull | 89a7524 | 2018-08-06 17:04:55 +0100 | [diff] [blame] | 81 | struct mm_ptable ptable; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 82 | struct mailbox mailbox; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 83 | |
| 84 | /** Wait entries to be used when waiting on other VM mailboxes. */ |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 85 | struct wait_entry wait_entries[MAX_VMS]; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 86 | |
| 87 | atomic_bool aborting; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /** Encapsulates a VM whose lock is held. */ |
| 91 | struct vm_locked { |
| 92 | struct vm *vm; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 95 | bool vm_init(uint32_t vcpu_count, struct mpool *ppool, struct vm **new_vm); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 96 | uint32_t vm_get_count(void); |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame^] | 97 | struct vm *vm_find(spci_vm_id_t id); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 98 | struct vm_locked vm_lock(struct vm *vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 99 | void vm_unlock(struct vm_locked *locked); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 100 | struct vcpu *vm_get_vcpu(struct vm *vm, uint32_t vcpu_index); |