radeonsi: add s_sethalt to shaders for debugging
[mesa.git] / src / amd / common / ac_rtld.h
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 <stdbool.h>
28 #include <stdint.h>
29 #include <stddef.h>
30
31 #include "util/u_dynarray.h"
32
33 struct ac_rtld_part;
34 struct ac_shader_config;
35 struct radeon_info;
36
37 struct ac_rtld_symbol {
38 const char *name;
39 uint32_t size;
40 uint32_t align;
41 uint64_t offset; /* filled in by ac_rtld_open */
42 unsigned part_idx; /* shader part in which this symbol appears */
43 };
44
45 struct ac_rtld_options {
46 /* Loader will insert an s_sethalt 1 instruction as the
47 * first instruction. */
48 bool halt_at_entry:1;
49 };
50
51 /* Lightweight wrapper around underlying ELF objects. */
52 struct ac_rtld_binary {
53 struct ac_rtld_options options;
54
55 /* Required buffer sizes, currently read/executable only. */
56 uint64_t rx_size;
57
58 uint64_t rx_end_markers;
59
60 unsigned num_parts;
61 struct ac_rtld_part *parts;
62
63 struct util_dynarray lds_symbols;
64 uint32_t lds_size;
65 };
66
67 /**
68 * Callback function type used during upload to resolve external symbols that
69 * are not defined in any of the ELF binaries available to the linker.
70 *
71 * \param cb_data caller-defined data
72 * \param symbol NUL-terminated symbol name
73 * \param value to be filled in by the callback
74 * \return whether the symbol was found successfully
75 */
76 typedef bool (*ac_rtld_get_external_symbol_cb)(
77 void *cb_data, const char *symbol, uint64_t *value);
78
79 /**
80 * Lifetimes of \ref info, in-memory ELF objects, and the names of
81 * \ref shared_lds_symbols must extend until \ref ac_rtld_close is called on
82 * the opened binary.
83 */
84 struct ac_rtld_open_info {
85 const struct radeon_info *info;
86 struct ac_rtld_options options;
87
88 unsigned num_parts;
89 const char * const *elf_ptrs; /* in-memory ELF objects of each part */
90 const size_t *elf_sizes; /* sizes of corresponding in-memory ELF objects in bytes */
91
92 /* Shared LDS symbols are layouted such that they are accessible from
93 * all shader parts. Non-shared (private) LDS symbols of one part may
94 * overlap private LDS symbols of another shader part.
95 */
96 unsigned num_shared_lds_symbols;
97 const struct ac_rtld_symbol *shared_lds_symbols;
98 };
99
100 bool ac_rtld_open(struct ac_rtld_binary *binary,
101 struct ac_rtld_open_info i);
102
103 void ac_rtld_close(struct ac_rtld_binary *binary);
104
105 bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name,
106 const char **data, size_t *nbytes);
107
108 bool ac_rtld_read_config(struct ac_rtld_binary *binary,
109 struct ac_shader_config *config);
110
111 struct ac_rtld_upload_info {
112 struct ac_rtld_binary *binary;
113
114 /** GPU mapping of the read/executable buffer. */
115 uint64_t rx_va;
116
117 /** CPU mapping of the read/executable buffer */
118 char *rx_ptr;
119
120 /** Optional callback function that will be queried for symbols not
121 * defined in any of the binary's parts. */
122 ac_rtld_get_external_symbol_cb get_external_symbol;
123
124 /** Caller-defined data that will be passed to callback functions. */
125 void *cb_data;
126 };
127
128 bool ac_rtld_upload(struct ac_rtld_upload_info *u);
129
130 #endif /* AC_RTLD_H */