Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | From b417c1124af9d4569ba8871dfd1e43e626dddf12 Mon Sep 17 00:00:00 2001 |
| 2 | From: Satish Kumar <satish.kumar01@arm.com> |
| 3 | Date: Sun, 12 Dec 2021 10:57:17 +0000 |
| 4 | Subject: [PATCH] Use address instead of pointers |
| 5 | |
| 6 | Since secure enclave is 32bit and we 64bit there is an issue |
| 7 | in the protocol communication design that force us to handle |
| 8 | on our side the manipulation of address and pointers to make |
| 9 | this work. |
| 10 | |
| 11 | Signed-off-by: Rui Miguel Silva <rui.silva@arm.com> |
| 12 | Signed-off-by: Satish Kumar <satish.kumar01@arm.com> |
| 13 | |
| 14 | Upstream-Status: Pending [Not submitted to upstream yet] |
| 15 | Signed-off-by: Satish Kumar <satish.kumar01@arm.com> |
| 16 | |
| 17 | |
| 18 | --- |
| 19 | .../service/common/include/psa/client.h | 15 ++++++++++++++ |
| 20 | .../service/common/psa_ipc/service_psa_ipc.c | 20 ++++++++++++------- |
| 21 | .../secure_storage_ipc/secure_storage_ipc.c | 20 +++++++++---------- |
| 22 | 3 files changed, 38 insertions(+), 17 deletions(-) |
| 23 | |
| 24 | diff --git a/components/service/common/include/psa/client.h b/components/service/common/include/psa/client.h |
| 25 | index 69ccf14f..12dcd68f 100644 |
| 26 | --- a/components/service/common/include/psa/client.h |
| 27 | +++ b/components/service/common/include/psa/client.h |
| 28 | @@ -81,6 +81,21 @@ struct __attribute__ ((__packed__)) psa_outvec { |
| 29 | uint32_t len; /*!< the size in bytes */ |
| 30 | }; |
| 31 | |
| 32 | +static void *psa_u32_to_ptr(uint32_t addr) |
| 33 | +{ |
| 34 | + return (void *)(uintptr_t)addr; |
| 35 | +} |
| 36 | + |
| 37 | +static uint32_t psa_ptr_to_u32(void *ptr) |
| 38 | +{ |
| 39 | + return (uintptr_t)ptr; |
| 40 | +} |
| 41 | + |
| 42 | +static uint32_t psa_ptr_const_to_u32(const void *ptr) |
| 43 | +{ |
| 44 | + return (uintptr_t)ptr; |
| 45 | +} |
| 46 | + |
| 47 | /*************************** PSA Client API **********************************/ |
| 48 | |
| 49 | /** |
| 50 | diff --git a/components/service/common/psa_ipc/service_psa_ipc.c b/components/service/common/psa_ipc/service_psa_ipc.c |
| 51 | index 5e5815db..435c6c0a 100644 |
| 52 | --- a/components/service/common/psa_ipc/service_psa_ipc.c |
| 53 | +++ b/components/service/common/psa_ipc/service_psa_ipc.c |
| 54 | @@ -62,6 +62,11 @@ static size_t psa_call_out_vec_len(const struct psa_outvec *out_vec, size_t out_ |
| 55 | return resp_len; |
| 56 | } |
| 57 | |
| 58 | +static uint32_t psa_virt_to_phys_u32(struct rpc_caller *caller, void *va) |
| 59 | +{ |
| 60 | + return (uintptr_t)rpc_caller_virt_to_phys(caller, va); |
| 61 | +} |
| 62 | + |
| 63 | psa_handle_t psa_connect(struct rpc_caller *caller, uint32_t sid, |
| 64 | uint32_t version) |
| 65 | { |
| 66 | @@ -147,20 +152,20 @@ psa_status_t psa_call(struct rpc_caller *caller, psa_handle_t psa_handle, |
| 67 | req_msg->params.psa_call_params.handle = psa_handle; |
| 68 | req_msg->params.psa_call_params.type = type; |
| 69 | req_msg->params.psa_call_params.in_len = in_len; |
| 70 | - req_msg->params.psa_call_params.in_vec = rpc_caller_virt_to_phys(caller, in_vec_param); |
| 71 | + req_msg->params.psa_call_params.in_vec = psa_virt_to_phys_u32(caller, in_vec_param); |
| 72 | req_msg->params.psa_call_params.out_len = out_len; |
| 73 | - req_msg->params.psa_call_params.out_vec = rpc_caller_virt_to_phys(caller, out_vec_param); |
| 74 | + req_msg->params.psa_call_params.out_vec = psa_virt_to_phys_u32(caller, out_vec_param); |
| 75 | |
| 76 | for (i = 0; i < in_len; i++) { |
| 77 | - in_vec_param[i].base = rpc_caller_virt_to_phys(caller, payload); |
| 78 | + in_vec_param[i].base = psa_virt_to_phys_u32(caller, payload); |
| 79 | in_vec_param[i].len = in_vec[i].len; |
| 80 | |
| 81 | - memcpy(payload, in_vec[i].base, in_vec[i].len); |
| 82 | + memcpy(payload, psa_u32_to_ptr(in_vec[i].base), in_vec[i].len); |
| 83 | payload += in_vec[i].len; |
| 84 | } |
| 85 | |
| 86 | for (i = 0; i < out_len; i++) { |
| 87 | - out_vec_param[i].base = NULL; |
| 88 | + out_vec_param[i].base = 0; |
| 89 | out_vec_param[i].len = out_vec[i].len; |
| 90 | } |
| 91 | |
| 92 | @@ -182,11 +187,12 @@ psa_status_t psa_call(struct rpc_caller *caller, psa_handle_t psa_handle, |
| 93 | goto caller_end; |
| 94 | |
| 95 | out_vec_param = (struct psa_outvec *)rpc_caller_phys_to_virt(caller, |
| 96 | - resp_msg->params.out_vec); |
| 97 | + psa_u32_to_ptr(resp_msg->params.out_vec)); |
| 98 | |
| 99 | for (i = 0; i < resp_msg->params.out_len; i++) { |
| 100 | out_vec[i].len = out_vec_param[i].len; |
| 101 | - memcpy(out_vec[i].base, rpc_caller_phys_to_virt(caller, out_vec_param[i].base), |
| 102 | + memcpy(psa_u32_to_ptr(out_vec[i].base), |
| 103 | + rpc_caller_phys_to_virt(caller, psa_u32_to_ptr(out_vec_param[i].base)), |
| 104 | out_vec[i].len); |
| 105 | } |
| 106 | |
| 107 | diff --git a/components/service/secure_storage/backend/secure_storage_ipc/secure_storage_ipc.c b/components/service/secure_storage/backend/secure_storage_ipc/secure_storage_ipc.c |
| 108 | index a1f369db..bda442a6 100644 |
| 109 | --- a/components/service/secure_storage/backend/secure_storage_ipc/secure_storage_ipc.c |
| 110 | +++ b/components/service/secure_storage/backend/secure_storage_ipc/secure_storage_ipc.c |
| 111 | @@ -22,9 +22,9 @@ static psa_status_t secure_storage_ipc_set(void *context, uint32_t client_id, |
| 112 | psa_handle_t psa_handle; |
| 113 | psa_status_t psa_status; |
| 114 | struct psa_invec in_vec[] = { |
| 115 | - { .base = &uid, .len = sizeof(uid) }, |
| 116 | - { .base = p_data, .len = data_length }, |
| 117 | - { .base = &create_flags, .len = sizeof(create_flags) }, |
| 118 | + { .base = psa_ptr_to_u32(&uid), .len = sizeof(uid) }, |
| 119 | + { .base = psa_ptr_const_to_u32(p_data), .len = data_length }, |
| 120 | + { .base = psa_ptr_to_u32(&create_flags), .len = sizeof(create_flags) }, |
| 121 | }; |
| 122 | |
| 123 | (void)client_id; |
| 124 | @@ -53,11 +53,11 @@ static psa_status_t secure_storage_ipc_get(void *context, |
| 125 | psa_status_t psa_status; |
| 126 | uint32_t offset = (uint32_t)data_offset; |
| 127 | struct psa_invec in_vec[] = { |
| 128 | - { .base = &uid, .len = sizeof(uid) }, |
| 129 | - { .base = &offset, .len = sizeof(offset) }, |
| 130 | + { .base = psa_ptr_to_u32(&uid), .len = sizeof(uid) }, |
| 131 | + { .base = psa_ptr_to_u32(&offset), .len = sizeof(offset) }, |
| 132 | }; |
| 133 | struct psa_outvec out_vec[] = { |
| 134 | - { .base = p_data, .len = data_size }, |
| 135 | + { .base = psa_ptr_to_u32(p_data), .len = data_size }, |
| 136 | }; |
| 137 | |
| 138 | if (!p_data_length) { |
| 139 | @@ -84,10 +84,10 @@ static psa_status_t secure_storage_ipc_get_info(void *context, |
| 140 | psa_handle_t psa_handle; |
| 141 | psa_status_t psa_status; |
| 142 | struct psa_invec in_vec[] = { |
| 143 | - { .base = &uid, .len = sizeof(uid) }, |
| 144 | + { .base = psa_ptr_to_u32(&uid), .len = sizeof(uid) }, |
| 145 | }; |
| 146 | struct psa_outvec out_vec[] = { |
| 147 | - { .base = p_info, .len = sizeof(*p_info) }, |
| 148 | + { .base = psa_ptr_to_u32(p_info), .len = sizeof(*p_info) }, |
| 149 | }; |
| 150 | |
| 151 | (void)client_id; |
| 152 | @@ -110,7 +110,7 @@ static psa_status_t secure_storage_ipc_remove(void *context, |
| 153 | psa_handle_t psa_handle; |
| 154 | psa_status_t psa_status; |
| 155 | struct psa_invec in_vec[] = { |
| 156 | - { .base = &uid, .len = sizeof(uid) }, |
| 157 | + { .base = psa_ptr_to_u32(&uid), .len = sizeof(uid) }, |
| 158 | }; |
| 159 | |
| 160 | (void)client_id; |
| 161 | @@ -164,7 +164,7 @@ static uint32_t secure_storage_get_support(void *context, uint32_t client_id) |
| 162 | psa_status_t psa_status; |
| 163 | uint32_t support_flags; |
| 164 | struct psa_outvec out_vec[] = { |
| 165 | - { .base = &support_flags, .len = sizeof(support_flags) }, |
| 166 | + { .base = psa_ptr_to_u32(&support_flags), .len = sizeof(support_flags) }, |
| 167 | }; |
| 168 | |
| 169 | (void)client_id; |