blob: b3b7492f8bea8d901f23cf6750e1007b4cc70f05 [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 Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/cpu.h"
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000022#include "hf/list.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/mm.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000024#include "hf/mpool.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010025
Andrew Scullaa039b32018-10-04 15:02:26 +010026enum mailbox_state {
Andrew Walbranc3910f72018-11-27 14:24:36 +000027 /** There is no message in the mailbox. */
Andrew Scullaa039b32018-10-04 15:02:26 +010028 mailbox_state_empty,
29
Andrew Walbranc3910f72018-11-27 14:24:36 +000030 /** There is a message in the mailbox that is waiting for a reader. */
Andrew Scullaa039b32018-10-04 15:02:26 +010031 mailbox_state_received,
32
Andrew Walbranc3910f72018-11-27 14:24:36 +000033 /** There is a message in the mailbox that has been read. */
Andrew Scullaa039b32018-10-04 15:02:26 +010034 mailbox_state_read,
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010035};
36
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000037struct wait_entry {
38 /** The VM that is waiting for a mailbox to become writable. */
39 struct vm *waiting_vm;
40
41 /**
42 * Links used to add entry to a VM's waiter_list. This is protected by
43 * the notifying VM's lock.
44 */
45 struct list_entry wait_links;
46
47 /**
48 * Links used to add entry to a VM's ready_list. This is protected by
49 * the waiting VM's lock.
50 */
51 struct list_entry ready_links;
52};
53
Andrew Scullaa039b32018-10-04 15:02:26 +010054struct mailbox {
55 enum mailbox_state state;
56 uint32_t recv_from_id;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010057 int16_t recv_bytes;
58 void *recv;
59 const void *send;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000060
61 /**
62 * List of wait_entry structs representing VMs that want to be notified
63 * when the mailbox becomes writable. Once the mailbox does become
64 * writable, the entry is removed from this list and added to the
65 * waiting VM's ready_list.
66 */
67 struct list_entry waiter_list;
68
69 /**
70 * List of wait_entry structs representing VMs whose mailboxes became
71 * writable since the owner of the mailbox registers for notification.
72 */
73 struct list_entry ready_list;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074};
75
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010076struct vm {
Andrew Scull8c3a63a2018-09-20 13:38:34 +010077 uint32_t id;
Andrew Walbran0d7a0682018-12-06 16:48:47 +000078 /** See api.c for the partial ordering on locks. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010079 struct spinlock lock;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010080 uint32_t vcpu_count;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010081 struct vcpu vcpus[MAX_CPUS];
Andrew Scull89a75242018-08-06 17:04:55 +010082 struct mm_ptable ptable;
Andrew Scullaa039b32018-10-04 15:02:26 +010083 struct mailbox mailbox;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000084
85 /** Wait entries to be used when waiting on other VM mailboxes. */
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000086 struct wait_entry wait_entries[MAX_VMS];
Andrew Scull9726c252019-01-23 13:44:19 +000087
88 atomic_bool aborting;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000089};
90
91/** Encapsulates a VM whose lock is held. */
92struct vm_locked {
93 struct vm *vm;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010094};
95
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000096bool vm_init(uint32_t vcpu_count, struct mpool *ppool, struct vm **new_vm);
Andrew Scull19503262018-09-20 14:48:39 +010097uint32_t vm_get_count(void);
98struct vm *vm_get(uint32_t id);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000099void vm_secondary_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry,
100 uintreg_t arg);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000101void vm_lock(struct vm *vm, struct vm_locked *locked);
102void vm_unlock(struct vm_locked *locked);