-
Notifications
You must be signed in to change notification settings - Fork 43
/
msgqueue_common.c
244 lines (189 loc) · 5.3 KB
/
msgqueue_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#include <assert.h>
#include <hyptypes.h>
#include <string.h>
#include <hypcontainers.h>
#include <scheduler.h>
#include <spinlock.h>
#include <vic.h>
#include <virq.h>
#include "event_handlers.h"
#include "msgqueue_common.h"
#include "useraccess.h"
bool_result_t
msgqueue_send_msg(msgqueue_t *msgqueue, size_t size, kernel_or_gvaddr_t msg,
bool push, bool from_kernel)
{
bool_result_t ret;
ret.r = true;
ret.e = OK;
assert(msgqueue != NULL);
spinlock_acquire(&msgqueue->lock);
if (msgqueue->count == msgqueue->queue_depth) {
ret.e = ERROR_MSGQUEUE_FULL;
ret.r = false;
goto out;
}
// Enqueue message at the tail of the queue
void *hyp_va =
(void *)(msgqueue->buf + msgqueue->tail + sizeof(size_t));
if (from_kernel) {
if (size > msgqueue->max_msg_size) {
ret.e = ERROR_ARGUMENT_SIZE;
ret.r = false;
goto out;
}
(void)memcpy(hyp_va, (void *)msg.kernel_addr, size);
} else {
size_result_t ret_val = useraccess_copy_from_guest_va(
hyp_va, msgqueue->max_msg_size, msg.guest_addr, size);
if (ret_val.e != OK) {
goto out;
}
}
(void)memcpy(msgqueue->buf + msgqueue->tail, (uint8_t *)&size,
sizeof(size_t));
msgqueue->count++;
// Update tail value
msgqueue->tail += (count_t)(msgqueue->max_msg_size + sizeof(size_t));
if (msgqueue->tail == msgqueue->queue_size) {
msgqueue->tail = 0U;
}
// If buffer was previously below the not empty threshold, we must
// wake up the receiver side by asserting the receiver virq source.
if (push || (msgqueue->count == msgqueue->notempty_thd)) {
(void)virq_assert(&msgqueue->rcv_source, false);
}
if (msgqueue->count == msgqueue->queue_depth) {
ret.r = false;
}
out:
spinlock_release(&msgqueue->lock);
return ret;
}
receive_info_result_t
msgqueue_receive_msg(msgqueue_t *msgqueue, kernel_or_gvaddr_t buffer,
size_t max_size, bool to_kernel)
{
receive_info_result_t ret = { 0 };
size_t size = 0U;
ret.e = OK;
ret.r.size = 0U;
ret.r.notempty = true;
assert(msgqueue != NULL);
assert(msgqueue->buf != NULL);
spinlock_acquire(&msgqueue->lock);
if (msgqueue->count == 0U) {
ret.e = ERROR_MSGQUEUE_EMPTY;
ret.r.notempty = false;
goto out;
}
(void)memcpy((uint8_t *)&size, msgqueue->buf + msgqueue->head,
sizeof(size_t));
// Dequeue message from the head of the queue
void *hyp_va =
(void *)(msgqueue->buf + msgqueue->head + sizeof(size_t));
if (to_kernel) {
if (size > max_size) {
ret.e = ERROR_ARGUMENT_SIZE;
ret.r.notempty = false;
goto out;
}
(void)memcpy((void *)buffer.kernel_addr, hyp_va, size);
} else {
size_result_t ret_val = useraccess_copy_to_guest_va(
buffer.guest_addr, max_size, hyp_va, size, false);
if (ret_val.e != OK) {
goto out;
}
}
ret.r.size = size;
msgqueue->count--;
// Update head value
msgqueue->head += (count_t)(msgqueue->max_msg_size + sizeof(size_t));
assert(msgqueue->head <= msgqueue->queue_size);
if (msgqueue->head == msgqueue->queue_size) {
msgqueue->head = 0U;
}
// If buffer was previously above the not full threshold, we must let
// the sender side know that it can send more messages.
if (msgqueue->count == msgqueue->notfull_thd) {
// We wake up the sender side by asserting the sender virq
// source.
(void)virq_assert(&msgqueue->send_source, false);
}
if (msgqueue->count == 0U) {
ret.r.notempty = false;
}
out:
spinlock_release(&msgqueue->lock);
return ret;
}
void
msgqueue_flush_queue(msgqueue_t *msgqueue)
{
assert(msgqueue != NULL);
assert(msgqueue->buf != NULL);
spinlock_acquire(&msgqueue->lock);
// If there is a pending bound interrupt, it will be de-asserted
if (msgqueue->count != 0U) {
(void)virq_assert(&msgqueue->send_source, false);
(void)virq_clear(&msgqueue->rcv_source);
}
(void)memset_s(msgqueue->buf, msgqueue->queue_size, 0,
msgqueue->queue_size);
msgqueue->count = 0U;
msgqueue->head = 0U;
msgqueue->tail = 0U;
spinlock_release(&msgqueue->lock);
}
error_t
msgqueue_bind(msgqueue_t *msgqueue, vic_t *vic, virq_t virq,
virq_source_t *source, virq_trigger_t trigger)
{
assert(msgqueue != NULL);
assert(vic != NULL);
assert(source != NULL);
error_t ret = vic_bind_shared(source, vic, virq, trigger);
return ret;
}
void
msgqueue_unbind(virq_source_t *source)
{
assert(source != NULL);
vic_unbind_sync(source);
}
bool
msgqueue_rx_handle_virq_check_pending(virq_source_t *source, bool reasserted)
{
bool ret;
assert(source != NULL);
msgqueue_t *msgqueue = msgqueue_container_of_rcv_source(source);
if (reasserted) {
// Previous VIRQ wasn't delivered yet. If we return false in
// this case, we can't be sure that we won't race with a
// msgqueue_send_msg() on another CPU.
ret = true;
} else {
ret = (msgqueue->count >= msgqueue->notempty_thd);
}
return ret;
}
bool
msgqueue_tx_handle_virq_check_pending(virq_source_t *source, bool reasserted)
{
bool ret;
assert(source != NULL);
msgqueue_t *msgqueue = msgqueue_container_of_send_source(source);
if (reasserted) {
// Previous VIRQ wasn't delivered yet. If we return false in
// this case, we can't be sure that we won't race with a
// msgqueue_receive_msg() on another CPU.
ret = true;
} else {
ret = (msgqueue->count <= msgqueue->notfull_thd);
}
return ret;
}