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