blob: e19a1153a71f421e7d601a39acdfc9e0730d8e80 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
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 Walbran475c1452020-02-07 13:22:22 +000017#include "hf/spci_memory.h"
18
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/api.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010020#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/dlog.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000022#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023#include "hf/spci_internal.h"
24#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000025#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026
Andrew Walbran475c1452020-02-07 13:22:22 +000027struct spci_mem_transitions {
28 uint32_t orig_from_mode;
29 uint32_t orig_to_mode;
30 uint32_t from_mode;
31 uint32_t to_mode;
32};
33
34/* TODO: Add device attributes: GRE, cacheability, shareability. */
35static inline uint32_t spci_memory_attrs_to_mode(uint16_t memory_attributes)
36{
37 uint32_t mode = 0;
38
39 switch (spci_get_memory_access_attr(memory_attributes)) {
40 case SPCI_MEMORY_RO_NX:
41 mode = MM_MODE_R;
42 break;
43 case SPCI_MEMORY_RO_X:
44 mode = MM_MODE_R | MM_MODE_X;
45 break;
46 case SPCI_MEMORY_RW_NX:
47 mode = MM_MODE_R | MM_MODE_W;
48 break;
49 case SPCI_MEMORY_RW_X:
50 mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
51 break;
52 }
53
54 return mode;
55}
56
Jose Marinho75509b42019-04-09 09:34:59 +010057/**
Jose Marinho75509b42019-04-09 09:34:59 +010058 * Obtain the next mode to apply to the two VMs.
59 *
Jose Marinho7fbbf2e2019-08-05 13:19:58 +010060 * Returns true iff a state transition was found.
Jose Marinho75509b42019-04-09 09:34:59 +010061 */
62static bool spci_msg_get_next_state(
63 const struct spci_mem_transitions *transitions,
64 uint32_t transition_count, uint32_t memory_to_attributes,
Andrew Walbran1281ed42019-10-22 17:23:40 +010065 uint32_t orig_from_mode, uint32_t orig_to_mode, uint32_t *from_mode,
66 uint32_t *to_mode)
Jose Marinho75509b42019-04-09 09:34:59 +010067{
68 const uint32_t state_mask =
69 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
70 const uint32_t orig_from_state = orig_from_mode & state_mask;
71
72 for (uint32_t index = 0; index < transition_count; index++) {
73 uint32_t table_orig_from_mode =
74 transitions[index].orig_from_mode;
75 uint32_t table_orig_to_mode = transitions[index].orig_to_mode;
76
77 if (((orig_from_state) == table_orig_from_mode) &&
78 ((orig_to_mode & state_mask) == table_orig_to_mode)) {
79 *to_mode = transitions[index].to_mode |
80 memory_to_attributes;
Jose Marinho713f13a2019-05-21 11:54:16 +010081
82 *from_mode = transitions[index].from_mode |
83 (~state_mask & orig_from_mode);
Jose Marinho75509b42019-04-09 09:34:59 +010084
85 return true;
86 }
87 }
88 return false;
89}
90
91/**
92 * Verify that all pages have the same mode, that the starting mode
93 * constitutes a valid state and obtain the next mode to apply
94 * to the two VMs.
95 *
96 * Returns:
97 * The error code false indicates that:
98 * 1) a state transition was not found;
99 * 2) the pages being shared do not have the same mode within the <to>
Andrew Walbran475c1452020-02-07 13:22:22 +0000100 * or <from> VMs;
Jose Marinho75509b42019-04-09 09:34:59 +0100101 * 3) The beginning and end IPAs are not page aligned;
102 * 4) The requested share type was not handled.
103 * Success is indicated by true.
Jose Marinho75509b42019-04-09 09:34:59 +0100104 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000105static bool spci_msg_check_transition(
106 struct vm *to, struct vm *from, uint32_t share_func,
107 uint32_t *orig_from_mode,
108 struct spci_memory_region_constituent *constituents,
109 uint32_t constituent_count, uint32_t memory_to_attributes,
110 uint32_t *from_mode, uint32_t *to_mode)
Jose Marinho75509b42019-04-09 09:34:59 +0100111{
Andrew Walbran1281ed42019-10-22 17:23:40 +0100112 uint32_t orig_to_mode;
Jose Marinho75509b42019-04-09 09:34:59 +0100113 const struct spci_mem_transitions *mem_transition_table;
114 uint32_t transition_table_size;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100115 uint32_t i;
Jose Marinho75509b42019-04-09 09:34:59 +0100116
117 /*
118 * TODO: Transition table does not currently consider the multiple
119 * shared case.
120 */
121 static const struct spci_mem_transitions donate_transitions[] = {
122 {
123 /* 1) {O-EA, !O-NA} -> {!O-NA, O-EA} */
124 .orig_from_mode = 0,
125 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED,
126 .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED,
127 .to_mode = 0,
128 },
129 {
Jose Marinho75509b42019-04-09 09:34:59 +0100130 /*
131 * Duplicate of 1) in order to cater for an alternative
132 * representation of !O-NA:
133 * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED)
134 * are both alternate representations of !O-NA.
135 */
136 /* 4) {O-EA, !O-NA} -> {!O-NA, O-EA} */
137 .orig_from_mode = 0,
138 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
139 MM_MODE_SHARED,
140 .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
141 MM_MODE_SHARED,
142 .to_mode = 0,
143 },
144 };
Jose Marinho56c25732019-05-20 09:48:53 +0100145
Jose Marinho713f13a2019-05-21 11:54:16 +0100146 static const uint32_t size_donate_transitions =
147 ARRAY_SIZE(donate_transitions);
148
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100149 /*
150 * This data structure holds the allowed state transitions for the
151 * "lend" state machine. In this state machine the owner keeps ownership
152 * but loses access to the lent pages.
153 */
154 static const struct spci_mem_transitions lend_transitions[] = {
Jose Marinho56c25732019-05-20 09:48:53 +0100155 {
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100156 /* 1) {O-EA, !O-NA} -> {O-NA, !O-EA} */
157 .orig_from_mode = 0,
158 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
159 MM_MODE_SHARED,
160 .from_mode = MM_MODE_INVALID,
161 .to_mode = MM_MODE_UNOWNED,
Jose Marinho56c25732019-05-20 09:48:53 +0100162 },
163 {
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100164 /*
165 * Duplicate of 1) in order to cater for an alternative
166 * representation of !O-NA:
167 * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED)
168 * are both alternate representations of !O-NA.
169 */
170 /* 2) {O-EA, !O-NA} -> {O-NA, !O-EA} */
171 .orig_from_mode = 0,
172 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED,
173 .from_mode = MM_MODE_INVALID,
174 .to_mode = MM_MODE_UNOWNED,
Jose Marinho56c25732019-05-20 09:48:53 +0100175 },
176 };
177
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100178 static const uint32_t size_lend_transitions =
179 ARRAY_SIZE(lend_transitions);
Jose Marinho56c25732019-05-20 09:48:53 +0100180
Jose Marinho713f13a2019-05-21 11:54:16 +0100181 /*
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100182 * This data structure holds the allowed state transitions for the
183 * "share" state machine. In this state machine the owner keeps the
184 * shared pages mapped on its stage2 table and keeps access as well.
Jose Marinho713f13a2019-05-21 11:54:16 +0100185 */
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100186 static const struct spci_mem_transitions share_transitions[] = {
Jose Marinho713f13a2019-05-21 11:54:16 +0100187 {
188 /* 1) {O-EA, !O-NA} -> {O-SA, !O-SA} */
189 .orig_from_mode = 0,
190 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
191 MM_MODE_SHARED,
192 .from_mode = MM_MODE_SHARED,
193 .to_mode = MM_MODE_UNOWNED | MM_MODE_SHARED,
194 },
195 {
196 /*
197 * Duplicate of 1) in order to cater for an alternative
198 * representation of !O-NA:
199 * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED)
200 * are both alternate representations of !O-NA.
201 */
202 /* 2) {O-EA, !O-NA} -> {O-SA, !O-SA} */
203 .orig_from_mode = 0,
204 .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED,
205 .from_mode = MM_MODE_SHARED,
206 .to_mode = MM_MODE_UNOWNED | MM_MODE_SHARED,
207 },
208 };
209
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100210 static const uint32_t size_share_transitions =
211 ARRAY_SIZE(share_transitions);
212
213 static const struct spci_mem_transitions relinquish_transitions[] = {
214 {
215 /* 1) {!O-EA, O-NA} -> {!O-NA, O-EA} */
216 .orig_from_mode = MM_MODE_UNOWNED,
217 .orig_to_mode = MM_MODE_INVALID,
218 .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
219 MM_MODE_SHARED,
220 .to_mode = 0,
221 },
222 {
223 /* 2) {!O-SA, O-SA} -> {!O-NA, O-EA} */
224 .orig_from_mode = MM_MODE_UNOWNED | MM_MODE_SHARED,
225 .orig_to_mode = MM_MODE_SHARED,
226 .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED |
227 MM_MODE_SHARED,
228 .to_mode = 0,
229 },
230 };
231
232 static const uint32_t size_relinquish_transitions =
233 ARRAY_SIZE(relinquish_transitions);
Jose Marinho75509b42019-04-09 09:34:59 +0100234
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000235 if (constituent_count == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100236 /*
237 * Fail if there are no constituents. Otherwise
238 * spci_msg_get_next_state would get an unitialised
239 * *orig_from_mode and orig_to_mode.
240 */
Jose Marinho75509b42019-04-09 09:34:59 +0100241 return false;
242 }
243
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000244 for (i = 0; i < constituent_count; ++i) {
Andrew Walbrand88ee922020-01-15 18:13:21 +0000245 ipaddr_t begin =
246 ipa_init(spci_memory_region_constituent_get_address(
247 &constituents[i]));
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100248 size_t size = constituents[i].page_count * PAGE_SIZE;
249 ipaddr_t end = ipa_add(begin, size);
250 uint32_t current_from_mode;
251 uint32_t current_to_mode;
252
253 /* Fail if addresses are not page-aligned. */
254 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
255 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
256 return false;
257 }
258
259 /*
260 * Ensure that this constituent memory range is all mapped with
261 * the same mode.
262 */
263 if (!mm_vm_get_mode(&from->ptable, begin, end,
264 &current_from_mode) ||
265 !mm_vm_get_mode(&to->ptable, begin, end,
266 &current_to_mode)) {
267 return false;
268 }
269
270 /*
271 * Ensure that all constituents are mapped with the same mode.
272 */
273 if (i == 0) {
274 *orig_from_mode = current_from_mode;
275 orig_to_mode = current_to_mode;
276 } else if (current_from_mode != *orig_from_mode ||
277 current_to_mode != orig_to_mode) {
278 return false;
279 }
Jose Marinho75509b42019-04-09 09:34:59 +0100280 }
281
Andrew Scullb5f49e02019-10-02 13:20:47 +0100282 /* Ensure the address range is normal memory and not a device. */
283 if (*orig_from_mode & MM_MODE_D) {
284 return false;
285 }
286
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000287 switch (share_func) {
288 case SPCI_MEM_DONATE_32:
Jose Marinho75509b42019-04-09 09:34:59 +0100289 mem_transition_table = donate_transitions;
290 transition_table_size = size_donate_transitions;
291 break;
292
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000293 case SPCI_MEM_LEND_32:
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100294 mem_transition_table = lend_transitions;
295 transition_table_size = size_lend_transitions;
296 break;
297
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000298 case SPCI_MEM_SHARE_32:
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100299 mem_transition_table = share_transitions;
300 transition_table_size = size_share_transitions;
301 break;
302
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000303 case HF_SPCI_MEM_RELINQUISH:
Jose Marinho56c25732019-05-20 09:48:53 +0100304 mem_transition_table = relinquish_transitions;
305 transition_table_size = size_relinquish_transitions;
306 break;
307
Jose Marinho75509b42019-04-09 09:34:59 +0100308 default:
309 return false;
310 }
311
312 return spci_msg_get_next_state(mem_transition_table,
313 transition_table_size,
314 memory_to_attributes, *orig_from_mode,
315 orig_to_mode, from_mode, to_mode);
316}
Jose Marinho09b1db82019-08-08 09:16:59 +0100317
318/**
319 * Updates a VM's page table such that the given set of physical address ranges
320 * are mapped in the address space at the corresponding address ranges, in the
321 * mode provided.
322 *
323 * If commit is false, the page tables will be allocated from the mpool but no
324 * mappings will actually be updated. This function must always be called first
325 * with commit false to check that it will succeed before calling with commit
326 * true, to avoid leaving the page table in a half-updated state. To make a
327 * series of changes atomically you can call them all with commit false before
328 * calling them all with commit true.
329 *
330 * mm_vm_defrag should always be called after a series of page table updates,
331 * whether they succeed or fail.
332 *
333 * Returns true on success, or false if the update failed and no changes were
334 * made to memory mappings.
335 */
336static bool spci_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000337 struct vm_locked vm_locked,
338 struct spci_memory_region_constituent *constituents,
339 uint32_t constituent_count, int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100340{
Jose Marinho09b1db82019-08-08 09:16:59 +0100341 /* Iterate over the memory region constituents. */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000342 for (uint32_t index = 0; index < constituent_count; index++) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100343 size_t size = constituents[index].page_count * PAGE_SIZE;
Andrew Walbrand88ee922020-01-15 18:13:21 +0000344 paddr_t pa_begin = pa_from_ipa(
345 ipa_init(spci_memory_region_constituent_get_address(
346 &constituents[index])));
Jose Marinho09b1db82019-08-08 09:16:59 +0100347 paddr_t pa_end = pa_add(pa_begin, size);
348
349 if (commit) {
Andrew Scull3c257452019-11-26 13:32:50 +0000350 vm_identity_commit(vm_locked, pa_begin, pa_end, mode,
351 ppool, NULL);
352 } else if (!vm_identity_prepare(vm_locked, pa_begin, pa_end,
353 mode, ppool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100354 return false;
355 }
356 }
357
358 return true;
359}
360
361/**
362 * Clears a region of physical memory by overwriting it with zeros. The data is
363 * flushed from the cache so the memory has been cleared across the system.
364 */
365static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
366{
367 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000368 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100369 * global mapping of the whole range. Such an approach will limit
370 * the changes to stage-1 tables and will allow only local
371 * invalidation.
372 */
373 bool ret;
374 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
375 void *ptr =
376 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
377 size_t size = pa_difference(begin, end);
378
379 if (!ptr) {
380 /* TODO: partial defrag of failed range. */
381 /* Recover any memory consumed in failed mapping. */
382 mm_defrag(stage1_locked, ppool);
383 goto fail;
384 }
385
386 memset_s(ptr, size, 0, size);
387 arch_mm_flush_dcache(ptr, size);
388 mm_unmap(stage1_locked, begin, end, ppool);
389
390 ret = true;
391 goto out;
392
393fail:
394 ret = false;
395
396out:
397 mm_unlock_stage1(&stage1_locked);
398
399 return ret;
400}
401
402/**
403 * Clears a region of physical memory by overwriting it with zeros. The data is
404 * flushed from the cache so the memory has been cleared across the system.
405 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000406static bool spci_clear_memory_constituents(
407 struct spci_memory_region_constituent *constituents,
Andrew Walbran475c1452020-02-07 13:22:22 +0000408 uint32_t constituent_count, struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100409{
410 struct mpool local_page_pool;
Jose Marinho09b1db82019-08-08 09:16:59 +0100411 struct mm_stage1_locked stage1_locked;
412 bool ret = false;
413
414 /*
415 * Create a local pool so any freed memory can't be used by another
416 * thread. This is to ensure each constituent that is mapped can be
417 * unmapped again afterwards.
418 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000419 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100420
421 /* Iterate over the memory region constituents. */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000422 for (uint32_t i = 0; i < constituent_count; ++i) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100423 size_t size = constituents[i].page_count * PAGE_SIZE;
Andrew Walbrand88ee922020-01-15 18:13:21 +0000424 paddr_t begin = pa_from_ipa(
425 ipa_init(spci_memory_region_constituent_get_address(
426 &constituents[i])));
Jose Marinho09b1db82019-08-08 09:16:59 +0100427 paddr_t end = pa_add(begin, size);
428
429 if (!clear_memory(begin, end, &local_page_pool)) {
430 /*
431 * api_clear_memory will defrag on failure, so no need
432 * to do it here.
433 */
434 goto out;
435 }
436 }
437
438 /*
439 * Need to defrag after clearing, as it may have added extra mappings to
440 * the stage 1 page table.
441 */
442 stage1_locked = mm_lock_stage1();
443 mm_defrag(stage1_locked, &local_page_pool);
444 mm_unlock_stage1(&stage1_locked);
445
446 ret = true;
447
448out:
449 mpool_fini(&local_page_pool);
450 return ret;
451}
452
453/**
454 * Shares memory from the calling VM with another. The memory can be shared in
455 * different modes.
456 *
457 * This function requires the calling context to hold the <to> and <from> locks.
458 *
459 * Returns:
460 * In case of error one of the following values is returned:
461 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
462 * erroneous;
463 * 2) SPCI_NO_MEMORY - Hafnium did not have sufficient memory to complete
464 * the request.
465 * Success is indicated by SPCI_SUCCESS.
466 */
467static struct spci_value spci_share_memory(
468 struct vm_locked to_locked, struct vm_locked from_locked,
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000469 struct spci_memory_region_constituent *constituents,
470 uint32_t constituent_count, uint32_t memory_to_attributes,
Andrew Walbran475c1452020-02-07 13:22:22 +0000471 uint32_t share_func, struct mpool *page_pool, bool clear)
Jose Marinho09b1db82019-08-08 09:16:59 +0100472{
473 struct vm *to = to_locked.vm;
474 struct vm *from = from_locked.vm;
475 uint32_t orig_from_mode;
476 uint32_t from_mode;
477 uint32_t to_mode;
478 struct mpool local_page_pool;
479 struct spci_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100480
481 /*
Andrew Walbrand88ee922020-01-15 18:13:21 +0000482 * Make sure constituents are properly aligned to a 32-bit boundary. If
483 * not we would get alignment faults trying to read (32-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100484 */
Andrew Walbrand88ee922020-01-15 18:13:21 +0000485 if (!is_aligned(constituents, 4)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100486 return spci_error(SPCI_INVALID_PARAMETERS);
487 }
488
489 /* Disallow reflexive shares as this suggests an error in the VM. */
490 if (to == from) {
491 return spci_error(SPCI_INVALID_PARAMETERS);
492 }
493
494 /*
495 * Check if the state transition is lawful for both VMs involved
496 * in the memory exchange, ensure that all constituents of a memory
497 * region being shared are at the same state.
498 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000499 if (!spci_msg_check_transition(to, from, share_func, &orig_from_mode,
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000500 constituents, constituent_count,
501 memory_to_attributes, &from_mode,
502 &to_mode)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100503 return spci_error(SPCI_INVALID_PARAMETERS);
504 }
505
506 /*
507 * Create a local pool so any freed memory can't be used by another
508 * thread. This is to ensure the original mapping can be restored if the
509 * clear fails.
510 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000511 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100512
513 /*
514 * First reserve all required memory for the new page table entries in
515 * both sender and recipient page tables without committing, to make
516 * sure the entire operation will succeed without exhausting the page
517 * pool.
518 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000519 if (!spci_region_group_identity_map(from_locked, constituents,
520 constituent_count, from_mode,
Andrew Walbran475c1452020-02-07 13:22:22 +0000521 page_pool, false) ||
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000522 !spci_region_group_identity_map(to_locked, constituents,
523 constituent_count, to_mode,
Andrew Walbran475c1452020-02-07 13:22:22 +0000524 page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100525 /* TODO: partial defrag of failed range. */
526 ret = spci_error(SPCI_NO_MEMORY);
527 goto out;
528 }
529
530 /*
531 * First update the mapping for the sender so there is no overlap with
532 * the recipient. This won't allocate because the transaction was
533 * already prepared above, but may free pages in the case that a whole
534 * block is being unmapped that was previously partially mapped.
535 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000536 CHECK(spci_region_group_identity_map(from_locked, constituents,
537 constituent_count, from_mode,
538 &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100539
540 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000541 if (clear && !spci_clear_memory_constituents(
Andrew Walbran475c1452020-02-07 13:22:22 +0000542 constituents, constituent_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100543 /*
544 * On failure, roll back by returning memory to the sender. This
545 * may allocate pages which were previously freed into
546 * `local_page_pool` by the call above, but will never allocate
547 * more pages than that so can never fail.
548 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000549 CHECK(spci_region_group_identity_map(
550 from_locked, constituents, constituent_count,
551 orig_from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100552
553 ret = spci_error(SPCI_NO_MEMORY);
554 goto out;
555 }
556
557 /*
558 * Complete the transfer by mapping the memory into the recipient. This
559 * won't allocate because the transaction was already prepared above, so
560 * it doesn't need to use the `local_page_pool`.
561 */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000562 CHECK(spci_region_group_identity_map(to_locked, constituents,
563 constituent_count, to_mode,
Andrew Walbran475c1452020-02-07 13:22:22 +0000564 page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100565
566 ret = (struct spci_value){.func = SPCI_SUCCESS_32};
567
568out:
569 mpool_fini(&local_page_pool);
570
571 /*
572 * Tidy up the page tables by reclaiming failed mappings (if there was
573 * an error) or merging entries into blocks where possible (on success).
574 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000575 mm_vm_defrag(&to->ptable, page_pool);
576 mm_vm_defrag(&from->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100577
578 return ret;
579}
580
581/**
Andrew Walbran475c1452020-02-07 13:22:22 +0000582 * Validates a call to donate, lend or share memory and then updates the stage-2
583 * page tables. Specifically, check if the message length and number of memory
584 * region constituents match, and if the transition is valid for the type of
585 * memory sending operation.
586 *
587 * Assumes that the caller has already found and locked both VMs and ensured
588 * that the destination RX buffer is available, and copied the memory region
589 * descriptor from the sender's TX buffer to a trusted internal buffer.
Jose Marinho09b1db82019-08-08 09:16:59 +0100590 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000591struct spci_value spci_memory_send(struct vm_locked to_locked,
592 struct vm_locked from_locked,
593 struct spci_memory_region *memory_region,
594 uint32_t memory_share_size,
595 uint32_t share_func, struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100596{
597 uint32_t memory_to_attributes;
598 uint32_t attributes_size;
599 uint32_t constituents_size;
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000600 struct spci_memory_region_constituent *constituents;
601 uint32_t constituent_count = memory_region->constituent_count;
Jose Marinho09b1db82019-08-08 09:16:59 +0100602
603 /*
604 * Ensure the number of constituents are within the memory
605 * bounds.
606 */
607 attributes_size = sizeof(struct spci_memory_region_attributes) *
608 memory_region->attribute_count;
609 constituents_size = sizeof(struct spci_memory_region_constituent) *
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000610 constituent_count;
Jose Marinho09b1db82019-08-08 09:16:59 +0100611 if (memory_region->constituent_offset <
612 sizeof(struct spci_memory_region) + attributes_size ||
613 memory_share_size !=
614 memory_region->constituent_offset + constituents_size) {
615 return spci_error(SPCI_INVALID_PARAMETERS);
616 }
617
Andrew Walbrane28f4a22019-12-24 15:45:36 +0000618 /* The sender must match the message sender. */
619 if (memory_region->sender != from_locked.vm->id) {
620 return spci_error(SPCI_INVALID_PARAMETERS);
621 }
622
Jose Marinho09b1db82019-08-08 09:16:59 +0100623 /* We only support a single recipient. */
624 if (memory_region->attribute_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +0000625 return spci_error(SPCI_NOT_SUPPORTED);
Jose Marinho09b1db82019-08-08 09:16:59 +0100626 }
627
Andrew Walbrancfe61642019-12-02 15:34:06 +0000628 /* The recipient must match the message recipient. */
629 if (memory_region->attributes[0].receiver != to_locked.vm->id) {
630 return spci_error(SPCI_INVALID_PARAMETERS);
631 }
632
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000633 switch (share_func) {
634 case SPCI_MEM_DONATE_32:
635 case SPCI_MEM_LEND_32:
636 case SPCI_MEM_SHARE_32:
Jose Marinho09b1db82019-08-08 09:16:59 +0100637 memory_to_attributes = spci_memory_attrs_to_mode(
638 memory_region->attributes[0].memory_attributes);
639 break;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000640 case HF_SPCI_MEM_RELINQUISH:
Jose Marinho09b1db82019-08-08 09:16:59 +0100641 memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
642 break;
643 default:
Andrew Walbran17eebf92020-02-05 16:35:49 +0000644 dlog_error("Invalid memory sharing message.\n");
Jose Marinho09b1db82019-08-08 09:16:59 +0100645 return spci_error(SPCI_INVALID_PARAMETERS);
646 }
647
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000648 constituents = spci_memory_region_get_constituents(memory_region);
649 return spci_share_memory(
650 to_locked, from_locked, constituents, constituent_count,
Andrew Walbran475c1452020-02-07 13:22:22 +0000651 memory_to_attributes, share_func, page_pool,
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000652 memory_region->flags & SPCI_MEMORY_REGION_FLAG_CLEAR);
Jose Marinho09b1db82019-08-08 09:16:59 +0100653}