1 /* 2 * Copyright 2014-2019 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #ifndef AC_RTLD_H 25 #define AC_RTLD_H 26 27 #include "compiler/shader_enums.h" 28 #include "util/u_dynarray.h" 29 #include "amd_family.h" 30 31 #include <stdbool.h> 32 #include <stddef.h> 33 #include <stdint.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 struct ac_rtld_part; 40 struct ac_shader_config; 41 struct radeon_info; 42 43 struct ac_rtld_symbol { 44 const char *name; 45 uint32_t size; 46 uint32_t align; 47 uint64_t offset; /* filled in by ac_rtld_open */ 48 unsigned part_idx; /* shader part in which this symbol appears */ 49 }; 50 51 struct ac_rtld_options { 52 /* Loader will insert an s_sethalt 1 instruction as the 53 * first instruction. */ 54 bool halt_at_entry : 1; 55 }; 56 57 /* Lightweight wrapper around underlying ELF objects. */ 58 struct ac_rtld_binary { 59 struct ac_rtld_options options; 60 enum amd_gfx_level gfx_level; 61 unsigned wave_size; 62 63 /* Required buffer sizes, currently read/executable only. */ 64 uint64_t rx_size; 65 66 /* Size of executable code, for reporting purposes. */ 67 uint64_t exec_size; 68 69 uint64_t rx_end_markers; 70 71 unsigned num_parts; 72 struct ac_rtld_part *parts; 73 74 struct util_dynarray lds_symbols; 75 uint32_t lds_size; 76 }; 77 78 /** 79 * Callback function type used during upload to resolve external symbols that 80 * are not defined in any of the ELF binaries available to the linker. 81 * 82 * \param cb_data caller-defined data 83 * \param symbol NUL-terminated symbol name 84 * \param value to be filled in by the callback 85 * \return whether the symbol was found successfully 86 */ 87 typedef bool (*ac_rtld_get_external_symbol_cb)(enum amd_gfx_level gfx_level, void *cb_data, 88 const char *symbol, uint64_t *value); 89 90 /** 91 * Lifetimes of \ref info, in-memory ELF objects, and the names of 92 * \ref shared_lds_symbols must extend until \ref ac_rtld_close is called on 93 * the opened binary. 94 */ 95 struct ac_rtld_open_info { 96 const struct radeon_info *info; 97 struct ac_rtld_options options; 98 gl_shader_stage shader_type; 99 unsigned wave_size; 100 101 unsigned num_parts; 102 const char *const *elf_ptrs; /* in-memory ELF objects of each part */ 103 const size_t *elf_sizes; /* sizes of corresponding in-memory ELF objects in bytes */ 104 105 /* Shared LDS symbols are layouted such that they are accessible from 106 * all shader parts. Non-shared (private) LDS symbols of one part may 107 * overlap private LDS symbols of another shader part. 108 */ 109 unsigned num_shared_lds_symbols; 110 const struct ac_rtld_symbol *shared_lds_symbols; 111 }; 112 113 bool ac_rtld_open(struct ac_rtld_binary *binary, struct ac_rtld_open_info i); 114 115 void ac_rtld_close(struct ac_rtld_binary *binary); 116 117 bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name, const char **data, 118 size_t *nbytes); 119 120 bool ac_rtld_read_config(const struct radeon_info *info, struct ac_rtld_binary *binary, 121 struct ac_shader_config *config); 122 123 struct ac_rtld_upload_info { 124 struct ac_rtld_binary *binary; 125 126 /** GPU mapping of the read/executable buffer. */ 127 uint64_t rx_va; 128 129 /** CPU mapping of the read/executable buffer */ 130 char *rx_ptr; 131 132 /** Optional callback function that will be queried for symbols not 133 * defined in any of the binary's parts. */ 134 ac_rtld_get_external_symbol_cb get_external_symbol; 135 136 /** Caller-defined data that will be passed to callback functions. */ 137 void *cb_data; 138 }; 139 140 int ac_rtld_upload(struct ac_rtld_upload_info *u); 141 142 #ifdef __cplusplus 143 } 144 #endif 145 146 #endif /* AC_RTLD_H */ 147