Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <buffer.h> |
| 7 | #include <granule.h> |
| 8 | #include <realm.h> |
AlexeiFedorov | 5b186ad | 2023-04-26 14:43:18 +0100 | [diff] [blame] | 9 | #include <rsi-handler.h> |
AlexeiFedorov | ee2fc82 | 2023-10-31 14:54:39 +0000 | [diff] [blame] | 10 | #include <rsi-host-call.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 11 | #include <smc-rsi.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | |
| 14 | /* |
| 15 | * If the RIPAS of the target IPA is empty then return value is RSI_ERROR_INPUT. |
| 16 | * |
| 17 | * If the RTT walk fails then: |
| 18 | * - @rsi_walk_result.abort is true and @rsi_walk_result.rtt_level is the |
| 19 | * last level reached by the walk. |
| 20 | * - Return value is RSI_SUCCESS. |
| 21 | * |
| 22 | * If the RTT walk succeeds then: |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 23 | * - If @rec_exit is not NULL and @rec_enter is NULL, then copy host call |
Alexei Fedorov | 6a15c9e | 2022-11-16 16:48:12 +0000 | [diff] [blame] | 24 | * arguments from host call data structure (in Realm memory) to @rec_exit. |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 25 | * - If @rec_exit is NULL and @rec_enter is not NULL, then copy host call |
Alexei Fedorov | 6a15c9e | 2022-11-16 16:48:12 +0000 | [diff] [blame] | 26 | * results to host call data structure (in Realm memory). |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 27 | * - Return value is RSI_SUCCESS. |
| 28 | */ |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 29 | static void do_host_call(struct rec *rec, struct rmi_rec_exit *rec_exit, |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 30 | struct rmi_rec_enter *rec_enter, |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 31 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 32 | { |
| 33 | enum s2_walk_status walk_status; |
| 34 | struct s2_walk_result walk_result; |
| 35 | unsigned long ipa = rec->regs[1]; |
| 36 | unsigned long page_ipa; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 37 | struct granule *gr; |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 38 | uintptr_t data; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 39 | struct rsi_host_call *host_call; |
| 40 | unsigned int i; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 41 | |
| 42 | assert(addr_in_rec_par(rec, ipa)); |
Alexei Fedorov | 6a15c9e | 2022-11-16 16:48:12 +0000 | [diff] [blame] | 43 | |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 44 | /* Only 'rec_enter' or 'rec_exit' should be set */ |
| 45 | assert((rec_enter != NULL) != (rec_exit != NULL)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 47 | page_ipa = ipa & GRANULE_MASK; |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 48 | walk_status = realm_ipa_to_pa(rec, page_ipa, &walk_result); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 49 | |
| 50 | switch (walk_status) { |
| 51 | case WALK_SUCCESS: |
| 52 | break; |
| 53 | case WALK_FAIL: |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 54 | if (walk_result.ripas_val == RIPAS_EMPTY) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 55 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 56 | } else { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 57 | res->action = STAGE_2_TRANSLATION_FAULT; |
| 58 | res->rtt_level = walk_result.rtt_level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 59 | } |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 60 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 61 | case WALK_INVALID_PARAMS: |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 62 | default: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 63 | assert(false); |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | /* Map Realm data granule to RMM address space */ |
| 68 | gr = find_granule(walk_result.pa); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 69 | data = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL); |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 70 | assert(data != 0UL); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 71 | |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 72 | host_call = (struct rsi_host_call *)(data + ipa - page_ipa); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 73 | |
| 74 | if (rec_exit != NULL) { |
| 75 | /* Copy host call arguments to REC exit data structure */ |
| 76 | rec_exit->imm = host_call->imm; |
| 77 | for (i = 0U; i < RSI_HOST_CALL_NR_GPRS; i++) { |
| 78 | rec_exit->gprs[i] = host_call->gprs[i]; |
| 79 | } |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 80 | |
| 81 | /* Record that a host call is pending */ |
| 82 | rec->host_call = true; |
| 83 | |
| 84 | /* |
| 85 | * Notify the Host. |
| 86 | * Leave REC registers unchanged, |
| 87 | * these will be read and updated by complete_rsi_host_call. |
| 88 | */ |
| 89 | res->action = EXIT_TO_HOST; |
| 90 | rec_exit->exit_reason = RMI_EXIT_HOST_CALL; |
Alexei Fedorov | 6a15c9e | 2022-11-16 16:48:12 +0000 | [diff] [blame] | 91 | } else { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 92 | /* Copy host call results to host call data structure */ |
| 93 | for (i = 0U; i < RSI_HOST_CALL_NR_GPRS; i++) { |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 94 | host_call->gprs[i] = rec_enter->gprs[i]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 95 | } |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 96 | |
| 97 | rec->regs[0] = RSI_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Unmap Realm data granule */ |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 101 | buffer_unmap((void *)data); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | |
| 103 | /* Unlock last level RTT */ |
| 104 | granule_unlock(walk_result.llt); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 105 | } |
| 106 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 107 | void handle_rsi_host_call(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 108 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 109 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 110 | unsigned long ipa = rec->regs[1]; |
| 111 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 112 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 113 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 114 | if (!ALIGNED(ipa, sizeof(struct rsi_host_call))) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 115 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 116 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if ((ipa / GRANULE_SIZE) != |
| 120 | ((ipa + sizeof(struct rsi_host_call) - 1UL) / GRANULE_SIZE)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 121 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 122 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (!addr_in_rec_par(rec, ipa)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 126 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 127 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 128 | } |
| 129 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 130 | do_host_call(rec, rec_exit, NULL, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | struct rsi_walk_result complete_rsi_host_call(struct rec *rec, |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 134 | struct rmi_rec_enter *rec_enter) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 135 | { |
AlexeiFedorov | 6a4314e | 2023-10-20 15:40:14 +0100 | [diff] [blame] | 136 | struct rsi_result res = { (enum rsi_action)0U }; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 137 | struct rsi_walk_result walk_res = { false, 0UL }; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * Do the necessary to walk the S2 RTTs and copy args from NS Host |
| 141 | * to the host call data structure. But it is possible for the |
| 142 | * RIPAS of the IPA to be EMPTY and hence this call can return |
| 143 | * RSI_ERROR_INPUT. In this case, we return RSI_SUCCESS to Realm |
| 144 | * and Realm may take an abort on accessing the IPA (depending on |
| 145 | * the RIPAS of IPA at that time). This is a situation which can be |
| 146 | * controlled from Realm and Realm should avoid this. |
| 147 | */ |
AlexeiFedorov | 52912a6 | 2023-07-24 12:28:47 +0100 | [diff] [blame] | 148 | do_host_call(rec, NULL, rec_enter, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 149 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 150 | if (res.action == STAGE_2_TRANSLATION_FAULT) { |
| 151 | walk_res.abort = true; |
| 152 | walk_res.rtt_level = res.rtt_level; |
| 153 | } |
| 154 | |
| 155 | return walk_res; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 156 | } |