1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2015-2020, 2022 Linaro Limited 5 */ 6 7 #include <initcall.h> 8 #include <kernel/linker.h> 9 #include <kernel/user_access.h> 10 #include <kernel/user_mode_ctx.h> 11 #include <memtag.h> 12 #include <mm/vm.h> 13 #include <string.h> 14 #include <tee_api_types.h> 15 #include <types_ext.h> 16 17 #define BB_ALIGNMENT (sizeof(long) * 2) 18 19 static struct user_mode_ctx *get_current_uctx(void) 20 { 21 struct ts_session *s = ts_get_current_session(); 22 23 if (!is_user_mode_ctx(s->ctx)) { 24 /* 25 * We may be called within a PTA session, which doesn't 26 * have a user_mode_ctx. Here, try to retrieve the 27 * user_mode_ctx associated with the calling session. 28 */ 29 s = TAILQ_NEXT(s, link_tsd); 30 if (!s || !is_user_mode_ctx(s->ctx)) 31 return NULL; 32 } 33 34 return to_user_mode_ctx(s->ctx); 35 } 36 37 static TEE_Result check_access(uint32_t flags, const void *uaddr, size_t len) 38 { 39 struct user_mode_ctx *uctx = get_current_uctx(); 40 41 if (!uctx) 42 return TEE_ERROR_GENERIC; 43 44 return vm_check_access_rights(uctx, flags, (vaddr_t)uaddr, len); 45 } 46 47 TEE_Result copy_from_user(void *kaddr, const void *uaddr, size_t len) 48 { 49 uint32_t flags = TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER; 50 TEE_Result res = TEE_SUCCESS; 51 52 uaddr = memtag_strip_tag_const(uaddr); 53 res = check_access(flags, uaddr, len); 54 if (!res) 55 memcpy(kaddr, uaddr, len); 56 57 return res; 58 } 59 60 TEE_Result copy_to_user(void *uaddr, const void *kaddr, size_t len) 61 { 62 uint32_t flags = TEE_MEMORY_ACCESS_WRITE | TEE_MEMORY_ACCESS_ANY_OWNER; 63 TEE_Result res = TEE_SUCCESS; 64 65 uaddr = memtag_strip_tag(uaddr); 66 res = check_access(flags, uaddr, len); 67 if (!res) 68 memcpy(uaddr, kaddr, len); 69 70 return res; 71 } 72 73 TEE_Result copy_from_user_private(void *kaddr, const void *uaddr, size_t len) 74 { 75 uint32_t flags = TEE_MEMORY_ACCESS_READ; 76 TEE_Result res = TEE_SUCCESS; 77 78 uaddr = memtag_strip_tag_const(uaddr); 79 res = check_access(flags, uaddr, len); 80 if (!res) 81 memcpy(kaddr, uaddr, len); 82 83 return res; 84 } 85 86 TEE_Result copy_to_user_private(void *uaddr, const void *kaddr, size_t len) 87 { 88 uint32_t flags = TEE_MEMORY_ACCESS_WRITE; 89 TEE_Result res = TEE_SUCCESS; 90 91 uaddr = memtag_strip_tag(uaddr); 92 res = check_access(flags, uaddr, len); 93 if (!res) 94 memcpy(uaddr, kaddr, len); 95 96 return res; 97 } 98 99 void *bb_alloc(size_t len) 100 { 101 struct user_mode_ctx *uctx = get_current_uctx(); 102 size_t offs = 0; 103 void *bb = NULL; 104 105 if (uctx && !ADD_OVERFLOW(uctx->bbuf_offs, len, &offs) && 106 offs <= uctx->bbuf_size) { 107 bb = uctx->bbuf + uctx->bbuf_offs; 108 uctx->bbuf_offs = ROUNDUP(offs, BB_ALIGNMENT); 109 } 110 return bb; 111 } 112 113 static void bb_free_helper(struct user_mode_ctx *uctx, vaddr_t bb, size_t len) 114 { 115 vaddr_t bbuf = (vaddr_t)uctx->bbuf; 116 117 if (bb >= bbuf && IS_ALIGNED(bb, BB_ALIGNMENT)) { 118 size_t prev_offs = bb - bbuf; 119 120 if (prev_offs + ROUNDUP(len, BB_ALIGNMENT) == uctx->bbuf_offs) 121 uctx->bbuf_offs = prev_offs; 122 } 123 } 124 125 void bb_free(void *bb, size_t len) 126 { 127 struct user_mode_ctx *uctx = get_current_uctx(); 128 129 if (uctx) 130 bb_free_helper(uctx, (vaddr_t)bb, len); 131 } 132 133 void bb_reset(void) 134 { 135 struct user_mode_ctx *uctx = get_current_uctx(); 136 137 if (uctx) 138 uctx->bbuf_offs = 0; 139 } 140 141 TEE_Result copy_kaddr_to_uref(uint32_t *uref, void *kaddr) 142 { 143 uint32_t ref = kaddr_to_uref(kaddr); 144 145 return copy_to_user_private(uref, &ref, sizeof(ref)); 146 } 147 148 uint32_t kaddr_to_uref(void *kaddr) 149 { 150 if (MEMTAG_IS_ENABLED) { 151 unsigned int uref_tag_shift = 32 - MEMTAG_TAG_WIDTH; 152 vaddr_t uref = memtag_strip_tag_vaddr(kaddr); 153 154 uref -= VCORE_START_VA; 155 assert(uref < (UINT32_MAX >> MEMTAG_TAG_WIDTH)); 156 uref |= memtag_get_tag(kaddr) << uref_tag_shift; 157 return uref; 158 } 159 160 assert(((vaddr_t)kaddr - VCORE_START_VA) < UINT32_MAX); 161 return (vaddr_t)kaddr - VCORE_START_VA; 162 } 163 164 vaddr_t uref_to_vaddr(uint32_t uref) 165 { 166 if (MEMTAG_IS_ENABLED) { 167 vaddr_t u = uref & (UINT32_MAX >> MEMTAG_TAG_WIDTH); 168 unsigned int uref_tag_shift = 32 - MEMTAG_TAG_WIDTH; 169 uint8_t tag = uref >> uref_tag_shift; 170 171 return memtag_insert_tag_vaddr(VCORE_START_VA + u, tag); 172 } 173 174 return VCORE_START_VA + uref; 175 } 176