• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014-2019 Advanced Micro Devices, Inc.
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #ifndef AC_RTLD_H
8 #define AC_RTLD_H
9 
10 #include "compiler/shader_enums.h"
11 #include "util/u_dynarray.h"
12 #include "amd_family.h"
13 
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 struct ac_rtld_part;
23 struct ac_shader_config;
24 struct radeon_info;
25 
26 struct ac_rtld_symbol {
27    const char *name;
28    uint32_t size;
29    uint32_t align;
30    uint64_t offset;   /* filled in by ac_rtld_open */
31    unsigned part_idx; /* shader part in which this symbol appears */
32 };
33 
34 struct ac_rtld_options {
35    /* Loader will insert an s_sethalt 1 instruction as the
36     * first instruction. */
37    bool halt_at_entry : 1;
38 };
39 
40 /* Lightweight wrapper around underlying ELF objects. */
41 struct ac_rtld_binary {
42    struct ac_rtld_options options;
43    enum amd_gfx_level gfx_level;
44    unsigned wave_size;
45 
46    /* Required buffer sizes, currently read/executable only. */
47    uint64_t rx_size;
48 
49    /* Size of executable code, for reporting purposes. */
50    uint64_t exec_size;
51 
52    uint64_t rx_end_markers;
53 
54    unsigned num_parts;
55    struct ac_rtld_part *parts;
56 
57    struct util_dynarray lds_symbols;
58    uint32_t lds_size;
59 };
60 
61 /**
62  * Callback function type used during upload to resolve external symbols that
63  * are not defined in any of the ELF binaries available to the linker.
64  *
65  * \param cb_data caller-defined data
66  * \param symbol NUL-terminated symbol name
67  * \param value to be filled in by the callback
68  * \return whether the symbol was found successfully
69  */
70 typedef bool (*ac_rtld_get_external_symbol_cb)(enum amd_gfx_level gfx_level, void *cb_data,
71                                                const char *symbol, uint64_t *value);
72 
73 /**
74  * Lifetimes of \ref info, in-memory ELF objects, and the names of
75  * \ref shared_lds_symbols must extend until \ref ac_rtld_close is called on
76  * the opened binary.
77  */
78 struct ac_rtld_open_info {
79    const struct radeon_info *info;
80    struct ac_rtld_options options;
81    gl_shader_stage shader_type;
82    unsigned wave_size;
83 
84    unsigned num_parts;
85    const char *const *elf_ptrs; /* in-memory ELF objects of each part */
86    const size_t *elf_sizes;     /* sizes of corresponding in-memory ELF objects in bytes */
87 
88    /* Shared LDS symbols are layouted such that they are accessible from
89     * all shader parts. Non-shared (private) LDS symbols of one part may
90     * overlap private LDS symbols of another shader part.
91     */
92    unsigned num_shared_lds_symbols;
93    const struct ac_rtld_symbol *shared_lds_symbols;
94 };
95 
96 bool ac_rtld_open(struct ac_rtld_binary *binary, struct ac_rtld_open_info i);
97 
98 void ac_rtld_close(struct ac_rtld_binary *binary);
99 
100 bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name, const char **data,
101                                  size_t *nbytes);
102 
103 bool ac_rtld_read_config(const struct radeon_info *info, struct ac_rtld_binary *binary,
104                          struct ac_shader_config *config);
105 
106 struct ac_rtld_upload_info {
107    struct ac_rtld_binary *binary;
108 
109    /** GPU mapping of the read/executable buffer. */
110    uint64_t rx_va;
111 
112    /** CPU mapping of the read/executable buffer */
113    char *rx_ptr;
114 
115    /** Optional callback function that will be queried for symbols not
116     * defined in any of the binary's parts. */
117    ac_rtld_get_external_symbol_cb get_external_symbol;
118 
119    /** Caller-defined data that will be passed to callback functions. */
120    void *cb_data;
121 };
122 
123 int ac_rtld_upload(struct ac_rtld_upload_info *u);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* AC_RTLD_H */
130