blob: 51835b5f7da26f03d4208022327058cb6597eb19 [file] [log] [blame]
J-Alves66652252022-07-06 09:49:51 +01001/*
2 * Copyright 2022 The Hafnium Authors.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
7 */
8
9/**
10 * The maximum number of memory sharing handles which may be active at once. A
11 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
12 * or LEND handle is active from when it is sent to when it is reclaimed.
13 */
14#define MAX_MEM_SHARES 100
15
J-Alvesd15905d2023-02-20 11:52:37 +000016#include <stdbool.h>
17#include <stdint.h>
18
19#include "hf/check.h"
20#include "hf/ffa_memory.h"
21#include "hf/mpool.h"
22#include "hf/vm.h"
23
24#include "vmapi/hf/ffa.h"
25
J-Alves66652252022-07-06 09:49:51 +010026/**
27 * The maximum number of fragments into which a memory sharing message may be
28 * broken.
29 */
30#define MAX_FRAGMENTS 20
31
32static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
33 "struct ffa_memory_region_constituent must be a multiple of 16 "
34 "bytes long.");
35static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
36 "struct ffa_composite_memory_region must be a multiple of 16 "
37 "bytes long.");
38static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
39 "struct ffa_memory_region_attributes must be 4 bytes long.");
40static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
41 "struct ffa_memory_access must be a multiple of 16 bytes long.");
42static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
43 "struct ffa_memory_region must be a multiple of 16 bytes long.");
44static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
45 "struct ffa_mem_relinquish must be a multiple of 16 "
46 "bytes long.");
Demi Marie Obenourd4677412023-02-03 20:35:12 -050047static_assert(sizeof(((struct ffa_memory_region){0}).receiver_count == 4),
48 "struct ffa_memory_region::receiver_count must be 4 bytes long");
J-Alves66652252022-07-06 09:49:51 +010049
50struct ffa_memory_share_state {
51 /**
52 * The memory region being shared, or NULL if this share state is
53 * unallocated.
54 */
55 struct ffa_memory_region *memory_region;
56
57 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
58
59 /** The number of constituents in each fragment. */
60 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
61
62 /**
63 * The number of valid elements in the `fragments` and
64 * `fragment_constituent_counts` arrays.
65 */
66 uint32_t fragment_count;
67
68 /**
69 * The FF-A function used for sharing the memory. Must be one of
70 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
71 * share state is allocated, or 0.
72 */
73 uint32_t share_func;
74
75 /**
76 * The sender's original mode before invoking the FF-A function for
77 * sharing the memory.
78 * This is used to reset the original configuration when sender invokes
79 * FFA_MEM_RECLAIM_32.
80 */
81 uint32_t sender_orig_mode;
82
83 /**
84 * True if all the fragments of this sharing request have been sent and
85 * Hafnium has updated the sender page table accordingly.
86 */
87 bool sending_complete;
88
89 /**
90 * How many fragments of the memory region each recipient has retrieved
91 * so far. The order of this array matches the order of the endpoint
92 * memory access descriptors in the memory region descriptor. Any
93 * entries beyond the receiver_count will always be 0.
94 */
95 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
96
97 /**
98 * Field for the SPMC to keep track of how many fragments of the memory
99 * region the hypervisor has managed to retrieve, using a
100 * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0
101 * specification.
102 */
103 uint32_t hypervisor_fragment_count;
104};
105
106/**
J-Alves0a83dc22023-05-05 09:50:37 +0100107 * Actions that can be taken by function `ffa_region_group_identity_map`.
108 */
109enum ffa_map_action {
110 MAP_ACTION_CHECK,
111 MAP_ACTION_CHECK_PROTECT,
112 MAP_ACTION_COMMIT,
113 MAP_ACTION_COMMIT_UNPROTECT,
114 MAP_ACTION_MAX,
115};
116
117/**
J-Alves66652252022-07-06 09:49:51 +0100118 * Encapsulates the set of share states while the `share_states_lock` is held.
119 */
120struct share_states_locked {
121 struct ffa_memory_share_state *share_states;
122};
123
Karl Meakin52cdfe72023-06-30 14:49:10 +0100124struct ffa_memory_share_state *allocate_share_state(
125 struct share_states_locked share_states, uint32_t share_func,
126 struct ffa_memory_region *memory_region, uint32_t fragment_length,
127 ffa_memory_handle_t handle);
J-Alves66652252022-07-06 09:49:51 +0100128struct share_states_locked share_states_lock(void);
129void share_states_unlock(struct share_states_locked *share_states);
Karl Meakin4a2854a2023-06-30 16:26:52 +0100130struct ffa_memory_share_state *get_share_state(
131 struct share_states_locked share_states, ffa_memory_handle_t handle);
J-Alvesfdd29272022-07-19 13:16:31 +0100132void share_state_free(struct share_states_locked share_states,
133 struct ffa_memory_share_state *share_state,
134 struct mpool *page_pool);
135uint32_t share_state_next_fragment_offset(
136 struct share_states_locked share_states,
137 struct ffa_memory_share_state *share_state);
138/** Checks whether the given share state has been fully sent. */
139bool share_state_sending_complete(struct share_states_locked share_states,
140 struct ffa_memory_share_state *share_state);
J-Alves66652252022-07-06 09:49:51 +0100141void dump_share_states(void);
142
J-Alves66652252022-07-06 09:49:51 +0100143struct ffa_value ffa_memory_send_validate(
144 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
145 uint32_t memory_share_length, uint32_t fragment_length,
146 uint32_t share_func);
147struct ffa_value ffa_send_check_update(
148 struct vm_locked from_locked,
149 struct ffa_memory_region_constituent **fragments,
150 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000151 uint32_t total_page_count, uint32_t share_func,
152 struct ffa_memory_access *receivers, uint32_t receivers_count,
153 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret);
J-Alvesfdd29272022-07-19 13:16:31 +0100154struct ffa_value ffa_memory_send_complete(
155 struct vm_locked from_locked, struct share_states_locked share_states,
156 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
157 uint32_t *orig_from_mode_ret);
158struct ffa_value ffa_memory_send_continue_validate(
159 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +0100160 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
J-Alvesfdd29272022-07-19 13:16:31 +0100161 struct mpool *page_pool);
J-Alvesfc19b372022-07-06 12:17:35 +0100162struct ffa_value ffa_retrieve_check_transition(
163 struct vm_locked to, uint32_t share_func,
164 struct ffa_memory_region_constituent **fragments,
165 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
166 uint32_t memory_to_attributes, uint32_t *to_mode);
J-Alvesb5084cf2022-07-06 14:20:12 +0100167struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +0100168 struct vm_locked to_locked,
J-Alvesb5084cf2022-07-06 14:20:12 +0100169 struct ffa_memory_region_constituent **fragments,
170 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +0100171 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alvesb5084cf2022-07-06 14:20:12 +0100172 struct mpool *page_pool);
J-Alvescf6253e2024-01-03 13:48:48 +0000173struct ffa_value ffa_region_group_identity_map(
J-Alvesfdd29272022-07-19 13:16:31 +0100174 struct vm_locked vm_locked,
175 struct ffa_memory_region_constituent **fragments,
176 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +0000177 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
178 bool *memory_protected);
J-Alvesfdd29272022-07-19 13:16:31 +0100179bool memory_region_receivers_from_other_world(
180 struct ffa_memory_region *memory_region);