blob: c24700c6547f0a07dd6ba701ed0829d3eb99e990 [file] [log] [blame]
Andrew Scull6d2db332018-10-10 15:28:17 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull6d2db332018-10-10 15:28:17 +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
17#pragma once
18
19#include "hf/types.h"
20
21enum hf_vcpu_run_code {
Andrew Walbran3d720052018-12-17 18:37:48 +000022 /**
Andrew Scull33fecd32019-01-08 14:48:27 +000023 * The vCPU has been preempted but still has work to do. If the
24 * scheduling quantum has not expired, the scheduler MUST call
25 * `hf_vcpu_run` on the vCPU to allow it to continue.
26 */
Andrew Scull9726c252019-01-23 13:44:19 +000027 HF_VCPU_RUN_PREEMPTED = 0,
Andrew Scull33fecd32019-01-08 14:48:27 +000028
29 /**
30 * The vCPU has voluntarily yielded the CPU. The scheduler SHOULD take a
31 * scheduling decision to give cycles to those that need them but MUST
32 * call `hf_vcpu_run` on the vCPU at a later point.
Andrew Walbran3d720052018-12-17 18:37:48 +000033 */
Andrew Scull9726c252019-01-23 13:44:19 +000034 HF_VCPU_RUN_YIELD = 1,
Andrew Walbran3d720052018-12-17 18:37:48 +000035
36 /**
Andrew Scull33fecd32019-01-08 14:48:27 +000037 * The vCPU is blocked waiting for an interrupt. The scheduler MUST take
38 * it off the run queue and not call `hf_vcpu_run` on the vCPU until it
Andrew Scullb06d1752019-02-04 10:15:48 +000039 * has injected an interrupt, received `HF_VCPU_RUN_WAKE_UP` for it
40 * from another vCPU or the timeout provided in
41 * `hf_vcpu_run_return.sleep` is not `HF_SLEEP_INDEFINITE` and the
42 * specified duration has expired.
Andrew Walbran3d720052018-12-17 18:37:48 +000043 */
Andrew Scull9726c252019-01-23 13:44:19 +000044 HF_VCPU_RUN_WAIT_FOR_INTERRUPT = 2,
Andrew Walbran3d720052018-12-17 18:37:48 +000045
46 /**
Andrew Scullb06d1752019-02-04 10:15:48 +000047 * The vCPU is blocked waiting for a message. The scheduler MUST take it
48 * off the run queue and not call `hf_vcpu_run` on the vCPU until it has
49 * injected an interrupt, sent it a message, or received
50 * `HF_VCPU_RUN_WAKE_UP` for it from another vCPU from another vCPU or
51 * the timeout provided in `hf_vcpu_run_return.sleep` is not
52 * `HF_SLEEP_INDEFINITE` and the specified duration has expired.
Andrew Walbran3d720052018-12-17 18:37:48 +000053 */
Andrew Scullb06d1752019-02-04 10:15:48 +000054 HF_VCPU_RUN_WAIT_FOR_MESSAGE = 3,
Andrew Walbran3d720052018-12-17 18:37:48 +000055
56 /**
Andrew Scullb06d1752019-02-04 10:15:48 +000057 * Hafnium would like `hf_vcpu_run` to be called on another vCPU,
58 * specified by `hf_vcpu_run_return.wake_up`. The scheduler MUST either
59 * wake the vCPU in question up if it is blocked, or preempt and re-run
60 * it if it is already running somewhere. This gives Hafnium a chance to
61 * update any CPU state which might have changed.
Andrew Walbran3d720052018-12-17 18:37:48 +000062 */
Andrew Scullb06d1752019-02-04 10:15:48 +000063 HF_VCPU_RUN_WAKE_UP = 4,
Andrew Walbran3d720052018-12-17 18:37:48 +000064
65 /**
Andrew Scullb06d1752019-02-04 10:15:48 +000066 * A message has been sent by the vCPU. The scheduler MUST run a vCPU
67 * from the recipient VM and priority SHOULD be given to those vCPUs
68 * that are waiting for a message.
Andrew Walbran3d720052018-12-17 18:37:48 +000069 */
Andrew Scullb06d1752019-02-04 10:15:48 +000070 HF_VCPU_RUN_MESSAGE = 5,
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000071
72 /**
73 * The vCPU has made the mailbox writable and there are pending waiters.
74 * The scheduler MUST call hf_mailbox_waiter_get() repeatedly and notify
75 * all waiters by injecting an HF_MAILBOX_WRITABLE_INTID interrupt.
76 */
Andrew Scull9726c252019-01-23 13:44:19 +000077 HF_VCPU_RUN_NOTIFY_WAITERS = 6,
78
79 /**
80 * The vCPU has aborted triggering the whole VM to abort. The scheduler
81 * MUST treat this as `HF_VCPU_RUN_WAIT_FOR_INTERRUPT` for this vCPU and
82 * `HF_VCPU_RUN_WAKE_UP` for all the other vCPUs of the VM.
83 */
84 HF_VCPU_RUN_ABORTED = 7,
Andrew Scull6d2db332018-10-10 15:28:17 +010085};
86
87struct hf_vcpu_run_return {
88 enum hf_vcpu_run_code code;
89 union {
90 struct {
91 uint32_t vm_id;
92 uint16_t vcpu;
93 } wake_up;
94 struct {
Andrew Scullb06d1752019-02-04 10:15:48 +000095 uint16_t vm_id;
Andrew Scull6d2db332018-10-10 15:28:17 +010096 uint32_t size;
97 } message;
98 struct {
99 uint64_t ns;
100 } sleep;
101 };
102};
103
104struct hf_mailbox_receive_return {
105 uint32_t vm_id;
106 uint32_t size;
107};
108
Andrew Scull6386f252018-12-06 13:29:10 +0000109enum hf_share {
110 /**
111 * Relinquish ownership and access to the memory and pass them to the
112 * recipient.
113 */
114 HF_MEMORY_GIVE,
115
116 /**
117 * Retain ownership of the memory but relinquish access to the
118 * recipient.
119 */
120 HF_MEMORY_LEND,
121
122 /**
123 * Retain ownership and access but additionally allow access to the
124 * recipient.
125 */
126 HF_MEMORY_SHARE,
127};
128
Andrew Scull6d2db332018-10-10 15:28:17 +0100129/**
130 * Encode an hf_vcpu_run_return struct in the 64-bit packing ABI.
131 */
132static inline uint64_t hf_vcpu_run_return_encode(struct hf_vcpu_run_return res)
133{
134 uint64_t ret = res.code & 0xff;
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000135
Andrew Scull6d2db332018-10-10 15:28:17 +0100136 switch (res.code) {
137 case HF_VCPU_RUN_WAKE_UP:
138 ret |= (uint64_t)res.wake_up.vm_id << 32;
139 ret |= (uint64_t)res.wake_up.vcpu << 16;
140 break;
141 case HF_VCPU_RUN_MESSAGE:
142 ret |= (uint64_t)res.message.size << 32;
Andrew Scullb06d1752019-02-04 10:15:48 +0000143 ret |= (uint64_t)res.message.vm_id << 16;
Andrew Scull6d2db332018-10-10 15:28:17 +0100144 break;
Andrew Scullb06d1752019-02-04 10:15:48 +0000145 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
146 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100147 ret |= res.sleep.ns << 8;
148 break;
149 default:
150 break;
151 }
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000152
Andrew Scull6d2db332018-10-10 15:28:17 +0100153 return ret;
154}
155
156/**
157 * Decode an hf_vcpu_run_return struct from the 64-bit packing ABI.
158 */
159static inline struct hf_vcpu_run_return hf_vcpu_run_return_decode(uint64_t res)
160{
Wedson Almeida Filhobdcd8362018-12-15 03:26:21 +0000161 struct hf_vcpu_run_return ret = {
162 .code = (enum hf_vcpu_run_code)(res & 0xff),
163 };
Andrew Scull6d2db332018-10-10 15:28:17 +0100164
165 /* Some codes include more data. */
166 switch (ret.code) {
167 case HF_VCPU_RUN_WAKE_UP:
168 ret.wake_up.vm_id = res >> 32;
169 ret.wake_up.vcpu = (res >> 16) & 0xffff;
170 break;
171 case HF_VCPU_RUN_MESSAGE:
172 ret.message.size = res >> 32;
Andrew Scullb06d1752019-02-04 10:15:48 +0000173 ret.message.vm_id = (res >> 16) & 0xffff;
Andrew Scull6d2db332018-10-10 15:28:17 +0100174 break;
Andrew Scullb06d1752019-02-04 10:15:48 +0000175 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
176 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
Andrew Scull6d2db332018-10-10 15:28:17 +0100177 ret.sleep.ns = res >> 8;
178 break;
179 default:
180 break;
181 }
182
183 return ret;
184}
185
186/**
187 * Encode an hf_mailbox_receive_return struct in the 64-bit packing ABI.
188 */
189static inline uint64_t hf_mailbox_receive_return_encode(
190 struct hf_mailbox_receive_return res)
191{
192 return res.vm_id | ((uint64_t)res.size << 32);
193}
194
195/**
196 * Decode an hf_mailbox_receive_return struct from the 64-bit packing ABI.
197 */
198static inline struct hf_mailbox_receive_return hf_mailbox_receive_return_decode(
199 uint64_t res)
200{
201 return (struct hf_mailbox_receive_return){
202 .vm_id = (uint32_t)(res & 0xffffffff),
203 .size = (uint32_t)(res >> 32),
204 };
205}