1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2019, Linaro Limited 4 * Copyright (c) 2020-2023, Arm Limited 5 * Copyright 2022-2023 NXP 6 */ 7 8 #ifndef __LDELF_H 9 #define __LDELF_H 10 11 #ifndef __ASSEMBLER__ 12 #include <types_ext.h> 13 #include <tee_api_types.h> 14 #include <user_ta_header.h> 15 16 /* Size of stack for TEE Core to allocate */ 17 #define LDELF_STACK_SIZE (4096 * 2) 18 19 /* 20 * struct ldelf_arg - argument for ldelf 21 * @uuid: [in] UUID of TA to load 22 * @is_32bit: [out] 1 if a 32bit TA or 0 if a 64bit TA 23 * @flags: [out] Flags field of TA header 24 * @entry_func: [out] TA entry function 25 * @stack_ptr: [out] TA stack pointer 26 * @dump_entry: [out] Dump TA mappings and stack trace 27 * @ftrace_entry: [out] Dump TA mappings and ftrace buffer 28 * @fbuf: [out] ftrace buffer pointer 29 * @dl_entry: [out] Dynamic linking interface (for libdl) 30 */ 31 struct ldelf_arg { 32 TEE_UUID uuid; 33 uint32_t is_32bit; 34 uint32_t flags; 35 uint64_t entry_func; 36 uint64_t load_addr; 37 uint64_t stack_ptr; 38 uint64_t dump_entry; 39 uint64_t ftrace_entry; 40 uint64_t dl_entry; 41 struct ftrace_buf *fbuf; 42 }; 43 44 #define DUMP_MAP_READ BIT(0) 45 #define DUMP_MAP_WRITE BIT(1) 46 #define DUMP_MAP_EXEC BIT(2) 47 #define DUMP_MAP_SECURE BIT(3) 48 #define DUMP_MAP_EPHEM BIT(4) 49 #define DUMP_MAP_LDELF BIT(7) 50 51 /* 52 * struct dump_entry_arg - argument for ldelf_arg::dump_entry() 53 */ 54 struct dump_entry_arg { 55 union { 56 struct { 57 uint32_t regs[16]; 58 } arm32; 59 struct { 60 uint64_t fp; 61 uint64_t sp; 62 uint64_t pc; 63 } arm64; 64 struct { 65 unsigned long fp; 66 unsigned long sp; 67 unsigned long pc; 68 } rv; 69 }; 70 bool is_32bit; 71 size_t num_maps; 72 struct dump_map { 73 vaddr_t va; 74 paddr_t pa; 75 size_t sz; 76 uint32_t flags; 77 } maps[]; 78 }; 79 80 /* 81 * struct dl_entry_arg - argument for ldelf_arg::dl_entry() 82 */ 83 struct dl_entry_arg { 84 uint32_t cmd; 85 TEE_Result ret; 86 union { 87 struct { 88 TEE_UUID uuid; /* in */ 89 uint32_t flags; /* in */ 90 } dlopen; 91 struct { 92 TEE_UUID uuid; /* in */ 93 vaddr_t val; /* out */ 94 char symbol[]; /* in */ 95 } dlsym; 96 }; 97 }; 98 99 /* 100 * Values for dl_entry_arg::cmd 101 */ 102 #define LDELF_DL_ENTRY_DLOPEN 0 103 #define LDELF_DL_ENTRY_DLSYM 1 104 105 /* 106 * Values for dl_entry_arg::dlopen::flags 107 */ 108 #define RTLD_NOW 2 109 #define RTLD_GLOBAL 0x100 110 #define RTLD_NODELETE 0x1000 111 112 #define LDELF_MAP_FLAG_SHAREABLE BIT32(0) 113 #define LDELF_MAP_FLAG_WRITEABLE BIT32(1) 114 #define LDELF_MAP_FLAG_EXECUTABLE BIT32(2) 115 #define LDELF_MAP_FLAG_BTI BIT32(3) 116 117 #endif /*!__ASSEMBLER__*/ 118 119 #define LDELF_RETURN 0 120 #define LDELF_LOG 1 121 #define LDELF_PANIC 2 122 #define LDELF_MAP_ZI 3 123 #define LDELF_UNMAP 4 124 #define LDELF_OPEN_BIN 5 125 #define LDELF_CLOSE_BIN 6 126 #define LDELF_MAP_BIN 7 127 #define LDELF_CP_FROM_BIN 8 128 #define LDELF_SET_PROT 9 129 #define LDELF_REMAP 10 130 #define LDELF_GEN_RND_NUM 11 131 132 #define LDELF_SCN_MAX 11 133 134 /* 135 * ldelf is loaded into memory by TEE Core. BSS is initialized and a 136 * stack is allocated and supplied in SP register. A struct ldelf_arg 137 * is placed in the stack and a pointer to the struct is provided in 138 * r0/x0 on ARM and in a0/x10 on RISC-V. 139 * 140 * ldelf relocates itself to the address where it is loaded before the main 141 * C routine is called. 142 * 143 * In the main C routine the TA is loaded using the PTA System interface. 144 */ 145 146 #endif /*__LDELF_H*/ 147