blob: b2ebf16d41baf3e8a59326b7333a41dca84818ba [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 */
J-Alves460d36c2023-10-12 17:02:15 +010014#pragma once
15
J-Alves66652252022-07-06 09:49:51 +010016#define MAX_MEM_SHARES 100
17
J-Alvesd15905d2023-02-20 11:52:37 +000018#include <stdbool.h>
19#include <stdint.h>
20
21#include "hf/check.h"
22#include "hf/ffa_memory.h"
Karl Meakin84710f32023-10-12 15:14:49 +010023#include "hf/ffa_v1_0.h"
J-Alvesd15905d2023-02-20 11:52:37 +000024#include "hf/mpool.h"
25#include "hf/vm.h"
26
27#include "vmapi/hf/ffa.h"
28
J-Alves66652252022-07-06 09:49:51 +010029/**
30 * The maximum number of fragments into which a memory sharing message may be
31 * broken.
32 */
33#define MAX_FRAGMENTS 20
34
35static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
36 "struct ffa_memory_region_constituent must be a multiple of 16 "
37 "bytes long.");
38static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
39 "struct ffa_composite_memory_region must be a multiple of 16 "
40 "bytes long.");
41static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
42 "struct ffa_memory_region_attributes must be 4 bytes long.");
43static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
44 "struct ffa_memory_access must be a multiple of 16 bytes long.");
45static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
46 "struct ffa_memory_region must be a multiple of 16 bytes long.");
47static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
48 "struct ffa_mem_relinquish must be a multiple of 16 "
49 "bytes long.");
Karl Meakin95f0e602024-03-07 17:23:26 +000050static_assert(sizeof((struct ffa_memory_region){0}.receiver_count) == 4,
Demi Marie Obenourd4677412023-02-03 20:35:12 -050051 "struct ffa_memory_region::receiver_count must be 4 bytes long");
J-Alves66652252022-07-06 09:49:51 +010052
Karl Meakin0fd67292024-02-06 17:33:05 +000053static_assert(sizeof(struct ffa_features_rxtx_map_params) == 4,
54 "struct ffa_features_rxtx_map_params must be 4 "
55 "bytes long");
56
Karl Meakin84710f32023-10-12 15:14:49 +010057static_assert(sizeof(ffa_memory_access_permissions_t) == 1,
58 "ffa_memory_access_permissions_t must be 1 byte wide");
59static_assert(sizeof(ffa_memory_attributes_t) == 2,
60 "ffa_memory_attributes_t must be 2 bytes wide");
61static_assert(sizeof(ffa_memory_attributes_v1_0) == 1,
62 "ffa_memory_attributes_v1_0 must be 1 byte wide");
63
J-Alves66652252022-07-06 09:49:51 +010064struct ffa_memory_share_state {
65 /**
66 * The memory region being shared, or NULL if this share state is
67 * unallocated.
68 */
69 struct ffa_memory_region *memory_region;
70
71 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
72
73 /** The number of constituents in each fragment. */
74 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
75
76 /**
77 * The number of valid elements in the `fragments` and
78 * `fragment_constituent_counts` arrays.
79 */
80 uint32_t fragment_count;
81
82 /**
83 * The FF-A function used for sharing the memory. Must be one of
84 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
85 * share state is allocated, or 0.
86 */
87 uint32_t share_func;
88
89 /**
90 * The sender's original mode before invoking the FF-A function for
91 * sharing the memory.
92 * This is used to reset the original configuration when sender invokes
93 * FFA_MEM_RECLAIM_32.
94 */
95 uint32_t sender_orig_mode;
96
97 /**
98 * True if all the fragments of this sharing request have been sent and
99 * Hafnium has updated the sender page table accordingly.
100 */
101 bool sending_complete;
102
103 /**
104 * How many fragments of the memory region each recipient has retrieved
105 * so far. The order of this array matches the order of the endpoint
106 * memory access descriptors in the memory region descriptor. Any
107 * entries beyond the receiver_count will always be 0.
108 */
109 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
110
111 /**
112 * Field for the SPMC to keep track of how many fragments of the memory
113 * region the hypervisor has managed to retrieve, using a
114 * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0
115 * specification.
116 */
117 uint32_t hypervisor_fragment_count;
J-Alves460d36c2023-10-12 17:02:15 +0100118
119 /*
120 * Record whether memory has been protected through the platform
121 * specific means. Used for linking map action on memory send to memory
122 * action on reclaim. I.e. if as a result of memory lend/donate the
123 * memory has been protected, this will be used to reset memory's state,
124 * by unprotecting on reclaim when the sender reestablishes its
125 * ownership and exclusive access.
126 */
127 bool memory_protected;
J-Alves66652252022-07-06 09:49:51 +0100128};
129
130/**
J-Alves0a83dc22023-05-05 09:50:37 +0100131 * Actions that can be taken by function `ffa_region_group_identity_map`.
132 */
133enum ffa_map_action {
134 MAP_ACTION_CHECK,
135 MAP_ACTION_CHECK_PROTECT,
136 MAP_ACTION_COMMIT,
137 MAP_ACTION_COMMIT_UNPROTECT,
138 MAP_ACTION_MAX,
139};
140
141/**
J-Alves66652252022-07-06 09:49:51 +0100142 * Encapsulates the set of share states while the `share_states_lock` is held.
143 */
144struct share_states_locked {
145 struct ffa_memory_share_state *share_states;
146};
147
Karl Meakin52cdfe72023-06-30 14:49:10 +0100148struct ffa_memory_share_state *allocate_share_state(
149 struct share_states_locked share_states, uint32_t share_func,
150 struct ffa_memory_region *memory_region, uint32_t fragment_length,
151 ffa_memory_handle_t handle);
J-Alves66652252022-07-06 09:49:51 +0100152struct share_states_locked share_states_lock(void);
153void share_states_unlock(struct share_states_locked *share_states);
Karl Meakin4a2854a2023-06-30 16:26:52 +0100154struct ffa_memory_share_state *get_share_state(
155 struct share_states_locked share_states, ffa_memory_handle_t handle);
J-Alvesfdd29272022-07-19 13:16:31 +0100156void share_state_free(struct share_states_locked share_states,
157 struct ffa_memory_share_state *share_state,
158 struct mpool *page_pool);
159uint32_t share_state_next_fragment_offset(
160 struct share_states_locked share_states,
161 struct ffa_memory_share_state *share_state);
162/** Checks whether the given share state has been fully sent. */
163bool share_state_sending_complete(struct share_states_locked share_states,
164 struct ffa_memory_share_state *share_state);
J-Alves66652252022-07-06 09:49:51 +0100165void dump_share_states(void);
166
J-Alves66652252022-07-06 09:49:51 +0100167struct ffa_value ffa_memory_send_validate(
168 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
169 uint32_t memory_share_length, uint32_t fragment_length,
170 uint32_t share_func);
J-Alvesfdd29272022-07-19 13:16:31 +0100171struct ffa_value ffa_memory_send_complete(
172 struct vm_locked from_locked, struct share_states_locked share_states,
173 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
174 uint32_t *orig_from_mode_ret);
175struct ffa_value ffa_memory_send_continue_validate(
176 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +0100177 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
J-Alvesfdd29272022-07-19 13:16:31 +0100178 struct mpool *page_pool);
J-Alvesfc19b372022-07-06 12:17:35 +0100179struct ffa_value ffa_retrieve_check_transition(
180 struct vm_locked to, uint32_t share_func,
181 struct ffa_memory_region_constituent **fragments,
182 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100183 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
184 enum ffa_map_action *map_action);
185
J-Alvesb5084cf2022-07-06 14:20:12 +0100186struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +0100187 struct vm_locked to_locked,
J-Alvesb5084cf2022-07-06 14:20:12 +0100188 struct ffa_memory_region_constituent **fragments,
189 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +0100190 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +0100191 struct mpool *page_pool, uint32_t *response_mode,
192 bool memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +0000193struct ffa_value ffa_region_group_identity_map(
J-Alvesfdd29272022-07-19 13:16:31 +0100194 struct vm_locked vm_locked,
195 struct ffa_memory_region_constituent **fragments,
196 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +0000197 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
198 bool *memory_protected);
J-Alvesfdd29272022-07-19 13:16:31 +0100199bool memory_region_receivers_from_other_world(
200 struct ffa_memory_region *memory_region);