CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
| 1 | /* ORACLE: retained default-OFF C writer; production packing is om/cubin/cubin_elf.om. Allocating C ABI shell remains. */ |
| 2 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 3 | /* omega_cubin: explicitly selected sm_86/sm_121 cubin container writer. See omega_cubin.h for the ABI |
| 4 | * facts and for what this file deliberately does not do. No device is opened, |
| 5 | * no address is produced, no launch is performed. |
| 6 | * |
| 7 | * The hosted build packs through om/cubin/cubin_elf.om, translated by the pinned omega-bridge into portable |
| 8 | * src/cudapp_om_cubin_elf.c (generated by om/units.sh; never edited by hand). |
| 9 | * This file keeps pointer checks, input/output buffers and refusal messages. |
| 10 | * The C writer is a default-OFF oracle selected by CUDAPP_MIGRATION_PENDING_C_CORE |
| 11 | * or CUDAPP_MIGRATION_PENDING_C_NO_POSIX. NO_POSIX calls the seeded caller-stack |
| 12 | * native unit, while allocation and diagnostics remain in this ABI shell. */ |
| 13 | #include "omega_cubin.h" |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | const char *omega_cubin_rc_name(int rc) { |
| 20 | switch (rc) { |
| 21 | case OMEGA_CUBIN_OK: return "OK"; |
| 22 | case OMEGA_CUBIN_E_ARG: return "ARG"; |
| 23 | case OMEGA_CUBIN_E_TEXT: return "TEXT"; |
| 24 | case OMEGA_CUBIN_E_PARAMS: return "PARAMS"; |
| 25 | case OMEGA_CUBIN_E_CBANK: return "CBANK"; |
| 26 | case OMEGA_CUBIN_E_GPR: return "GPR"; |
| 27 | case OMEGA_CUBIN_E_SHARED: return "SHARED"; |
| 28 | case OMEGA_CUBIN_E_MEM: return "MEM"; |
| 29 | case OMEGA_CUBIN_E_TARGET: return "TARGET"; |
| 30 | default: return "UNKNOWN"; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void omega_cubin_free(uint8_t *p) { free(p); } |
| 35 | |
| 36 | #if defined(CUDAPP_MIGRATION_PENDING_C_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_CORE) |
| 37 | #define CUBIN_C_PRIMARY 1 |
| 38 | #endif |
| 39 | #if defined(CUBIN_C_PRIMARY) && defined(CUDAPP_MIGRATION_PENDING_CUBIN_DIFF) |
| 40 | #error "CUDAPP_MIGRATION_PENDING_CUBIN_DIFF needs the .om packer, not the C oracle" |
| 41 | #endif |
| 42 | #if defined(CUBIN_C_PRIMARY) || defined(CUDAPP_MIGRATION_PENDING_CUBIN_DIFF) |
| 43 | #define ELF_HDR 64u |
| 44 | #define SH_HDR 64u |
| 45 | #define PHDR_ENT 56u |
| 46 | #define PHDR_COUNT 5u |
| 47 | #define SYM_ENT 24u |
| 48 | |
| 49 | typedef struct { |
| 50 | const char *name; |
| 51 | uint32_t type; |
| 52 | uint64_t flags; |
| 53 | const uint8_t *data; |
| 54 | size_t size; |
| 55 | uint32_t link; |
| 56 | uint32_t info; |
| 57 | uint64_t align; |
| 58 | uint64_t entsize; |
| 59 | } Sec; |
| 60 | |
| 61 | static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 62 | static void put32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } |
| 63 | static void put64(uint8_t *p, uint64_t v) { put32(p, (uint32_t)v); put32(p + 4, (uint32_t)(v >> 32)); } |
| 64 | static size_t align_up(size_t n, size_t a) { return a ? (n + a - 1) / a * a : n; } |
| 65 | |
| 66 | /* One Elf64_Phdr, laid out like the ptxas reference (PT_LOAD for text, |
| 67 | * constant and shared). REVIEW C-22 assumed the loader needs them. The 200 an |
| 68 | * image carrying them returned on GB10 (window 20261024T044814Z-4dc098) was the |
| 69 | * missing .note.nv.tkinfo: the G2 load matrix (evidence/MAX-INTEROP/G2, H6 ran, |
| 70 | * H7 refused) isolates it. Whether the headers are necessary is unproven. */ |
| 71 | static void put_phdr(uint8_t *p, uint32_t type, uint32_t flags, uint64_t off, |
| 72 | uint64_t filesz, uint64_t memsz) { |
| 73 | put32(p + 0, type); |
| 74 | put32(p + 4, flags); |
| 75 | put64(p + 8, off); |
| 76 | put64(p + 16, 0); /* p_vaddr */ |
| 77 | put64(p + 24, 0); /* p_paddr */ |
| 78 | put64(p + 32, filesz); |
| 79 | put64(p + 40, memsz); |
| 80 | put64(p + 48, 8); /* p_align */ |
| 81 | } |
| 82 | |
| 83 | static int name_ok(const char *s) { |
| 84 | size_t n; |
| 85 | if (!s) return 0; |
| 86 | n = strlen(s); |
| 87 | if (n == 0 || n > OMEGA_CUBIN_MAX_NAME) return 0; |
| 88 | if (!((s[0] >= 'A' && s[0] <= 'Z') || (s[0] >= 'a' && s[0] <= 'z') || s[0] == '_')) return 0; |
| 89 | for (size_t i = 0; i < n; i++) { |
| 90 | char c = s[i]; |
| 91 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')) return 0; |
| 92 | } |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | /* Append one .nv.info attribute. Format 4 has a 4-byte payload, format 3 a |
| 97 | * 2-byte payload, format 2 no payload. These are the encodings of the |
| 98 | * reference container; attr is stored as (id << 8) | format. */ |
| 99 | static size_t put_attr4(uint8_t *p, uint16_t id, const void *data, size_t n) { |
| 100 | put16(p, (uint16_t)((id << 8) | 4)); |
| 101 | put16(p + 2, (uint16_t)n); |
| 102 | memcpy(p + 4, data, n); |
| 103 | return 4 + n; |
| 104 | } |
| 105 | static size_t put_attr3(uint8_t *p, uint16_t id, uint16_t v) { |
| 106 | put16(p, (uint16_t)((id << 8) | 3)); |
| 107 | put16(p + 2, v); |
| 108 | return 4; |
| 109 | } |
| 110 | static size_t put_attr2(uint8_t *p, uint16_t id) { |
| 111 | put16(p, (uint16_t)((id << 8) | 2)); |
| 112 | put16(p + 2, 0); |
| 113 | return 4; |
| 114 | } |
| 115 | |
| 116 | static size_t build_param_info(uint8_t *p, const OmegaCubinParam *params, size_t nparams, uint32_t pbase) { |
| 117 | size_t o = 0; |
| 118 | uint32_t end = pbase; |
| 119 | for (size_t i = 0; i < nparams; i++) { |
| 120 | /* ptxas ABI: ordinal in bits 0..15, offset relative to the parameter |
| 121 | * base in bits 16..31. An absolute offset here makes every loader bind |
| 122 | * the wrong argument (REVIEW C-01). */ |
| 123 | uint32_t w2 = (uint32_t)i | ((params[i].offset - pbase) << 16); |
| 124 | uint32_t type = params[i].is_pointer ? 0x0021f500u : 0x0011f000u; |
| 125 | uint8_t d[12]; |
| 126 | put32(d, 0); |
| 127 | put32(d + 4, w2); |
| 128 | put32(d + 8, type); |
| 129 | o += put_attr4(p + o, 0x17, d, sizeof d); |
| 130 | if (params[i].offset + params[i].size > end) end = params[i].offset + params[i].size; |
| 131 | } |
| 132 | /* ptxas uses the end of the last parameter, not the sum of sizes: gaps |
| 133 | * between parameters are part of the constant bank (REVIEW C-04). */ |
| 134 | o += put_attr3(p + o, 0x19, (uint16_t)(end - pbase)); |
| 135 | { |
| 136 | uint8_t d[8]; |
| 137 | put32(d, 3); /* symbol index of .nv.constant0.<fn> in this writer */ |
| 138 | put32(d + 4, pbase | ((end - pbase) << 16)); |
| 139 | o += put_attr4(p + o, 0x0a, d, sizeof d); |
| 140 | } |
| 141 | return o; |
| 142 | } |
| 143 | |
| 144 | static int c_cubin_pack(uint32_t target_sm, const char *fn_name, |
| 145 | const uint8_t *text, size_t text_len, |
| 146 | const OmegaCubinParam *params, size_t nparams, |
| 147 | uint32_t cbank_size, uint32_t gpr_count, |
| 148 | uint32_t shared_reserved, |
| 149 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 150 | const uint32_t pbase = target_sm == OMEGA_CUBIN_SM_86 ? OMEGA_CUBIN_PARAM_BASE_SM86 : OMEGA_CUBIN_PARAM_BASE; |
| 151 | if (err && err_cap) err[0] = 0; |
| 152 | if (!out || !out_len) return OMEGA_CUBIN_E_ARG; |
| 153 | *out = NULL; |
| 154 | *out_len = 0; |
| 155 | if (!fn_name || !text || !name_ok(fn_name)) { |
| 156 | if (err) snprintf(err, err_cap, "invalid function name or text"); |
| 157 | return OMEGA_CUBIN_E_ARG; |
| 158 | } |
| 159 | if (text_len == 0 || text_len % 16u != 0 || text_len > (1u << 24)) { |
| 160 | if (err) snprintf(err, err_cap, "text length %zu is not a nonzero multiple of 16", text_len); |
| 161 | return OMEGA_CUBIN_E_TEXT; |
| 162 | } |
| 163 | if (nparams > OMEGA_CUBIN_MAX_PARAMS) { |
| 164 | if (err) snprintf(err, err_cap, "too many parameters: %zu", nparams); |
| 165 | return OMEGA_CUBIN_E_PARAMS; |
| 166 | } |
| 167 | if (gpr_count > 255u) { |
| 168 | if (err) snprintf(err, err_cap, "gpr count %u exceeds 255", gpr_count); |
| 169 | return OMEGA_CUBIN_E_GPR; |
| 170 | } |
| 171 | if (shared_reserved % 4u) { |
| 172 | if (err) snprintf(err, err_cap, "reserved shared bytes %u not a multiple of 4", shared_reserved); |
| 173 | return OMEGA_CUBIN_E_SHARED; |
| 174 | } |
| 175 | { |
| 176 | uint64_t end = pbase; |
| 177 | for (size_t i = 0; i < nparams; i++) { |
| 178 | const OmegaCubinParam *q = ¶ms[i]; |
| 179 | if (q->offset < end) { |
| 180 | if (err) snprintf(err, err_cap, "parameter %zu offset 0x%x overlaps or is unsorted", i, q->offset); |
| 181 | return OMEGA_CUBIN_E_PARAMS; |
| 182 | } |
| 183 | if (q->size != 8u && q->size != 4u && q->size != 2u && q->size != 1u) { |
| 184 | if (err) snprintf(err, err_cap, "parameter %zu has unsupported size %u", i, q->size); |
| 185 | return OMEGA_CUBIN_E_PARAMS; |
| 186 | } |
| 187 | if (q->is_pointer && q->size != 8u) { |
| 188 | if (err) snprintf(err, err_cap, "parameter %zu is a %u-byte pointer", i, q->size); |
| 189 | return OMEGA_CUBIN_E_PARAMS; |
| 190 | } |
| 191 | if ((q->size == 8u && (q->offset & 7u)) || (q->size == 4u && (q->offset & 3u)) || (q->size == 2u && (q->offset & 1u))) { |
| 192 | if (err) snprintf(err, err_cap, "parameter %zu offset 0x%x is not naturally aligned", i, q->offset); |
| 193 | return OMEGA_CUBIN_E_PARAMS; |
| 194 | } |
| 195 | end = (uint64_t)q->offset + q->size; |
| 196 | } |
| 197 | if (end > cbank_size) { |
| 198 | if (err) snprintf(err, err_cap, "parameter area ends at 0x%llx beyond cbank size 0x%x", |
| 199 | (unsigned long long)end, cbank_size); |
| 200 | return OMEGA_CUBIN_E_CBANK; |
| 201 | } |
| 202 | if (cbank_size > OMEGA_CUBIN_CBANK_MAX || cbank_size < pbase) { |
| 203 | if (err) snprintf(err, err_cap, "cbank size 0x%x outside 0x%x..0x%x", |
| 204 | cbank_size, pbase, OMEGA_CUBIN_CBANK_MAX); |
| 205 | return OMEGA_CUBIN_E_CBANK; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* Section payloads that depend on the descriptor. */ |
| 210 | char text_name[OMEGA_CUBIN_MAX_NAME + 16], cbank_name[OMEGA_CUBIN_MAX_NAME + 24], info_name[OMEGA_CUBIN_MAX_NAME + 16]; |
| 211 | snprintf(text_name, sizeof text_name, ".text.%s", fn_name); |
| 212 | snprintf(cbank_name, sizeof cbank_name, ".nv.constant0.%s", fn_name); |
| 213 | snprintf(info_name, sizeof info_name, ".nv.info.%s", fn_name); |
| 214 | |
| 215 | uint8_t ginfo[36]; |
| 216 | { |
| 217 | size_t o = 0; |
| 218 | uint8_t d[8]; |
| 219 | put32(d, 2); put32(d + 4, gpr_count); /* first function symbol, register count */ |
| 220 | o += put_attr4(ginfo + o, 0x2f, d, 8); |
| 221 | put32(d, 2); put32(d + 4, 0); |
| 222 | o += put_attr4(ginfo + o, 0x11, d, 8); |
| 223 | o += put_attr4(ginfo + o, 0x12, d, 8); |
| 224 | } |
| 225 | uint8_t compat[36] = { |
| 226 | 0x02,0x09,0x00,0x00, 0x02,0x02,0x02,0x00, 0x02,0x05,0x05,0x00, |
| 227 | 0x03,0x07,0x01,0x01, 0x02,0x03,0x00,0x00, 0x02,0x06,0x01,0x00, |
| 228 | 0x04,0x0b,0x08,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00 |
| 229 | }; |
| 230 | uint8_t note[32]; |
| 231 | { |
| 232 | put32(note, 12); put32(note + 4, 8); put32(note + 8, 0x3e8); |
| 233 | memcpy(note + 12, "NVIDIA Corp\0", 12); |
| 234 | put16(note + 24, 2); put16(note + 26, (uint16_t)target_sm); |
| 235 | put32(note + 28, 130); |
| 236 | } |
| 237 | /* .note.nv.tkinfo: the sm_121 driver refuses an image without it |
| 238 | * (cuModuleLoadData -> CUDA_ERROR_INVALID_IMAGE, CUDApp#68; chip windows |
| 239 | * 20261030T235211Z-0352f1 and 20261030T235858Z-10fa8c). Same note header as |
| 240 | * .note.nv.cuinfo with type 2000; the descriptor is version 2, 0, then the |
| 241 | * offsets of the tool, version, build and option strings in a zero-padded |
| 242 | * string blob. The strings are this writer's own and name sm_121, so the note |
| 243 | * is written for sm_121 only; sm_86 images stay as they were (no tkinfo). */ |
| 244 | const int with_tk = target_sm == OMEGA_CUBIN_SM_121; |
| 245 | static const char tk_str[72] = "\0cudapp\0CUDApp omega_cubin sm_121\0Build omega_cubin\0-arch sm_121"; |
| 246 | uint8_t tk[120]; |
| 247 | { |
| 248 | put32(tk, 12); put32(tk + 4, 96); put32(tk + 8, 2000); |
| 249 | memcpy(tk + 12, "NVIDIA Corp\0", 12); |
| 250 | put32(tk + 24, 2); put32(tk + 28, 0); |
| 251 | put32(tk + 32, 1); put32(tk + 36, 8); put32(tk + 40, 34); put32(tk + 44, 52); |
| 252 | memcpy(tk + 48, tk_str, sizeof tk_str); |
| 253 | } |
| 254 | uint8_t *info = malloc(4096), *text_copy = malloc(text_len), *cbank = calloc(1, cbank_size); |
| 255 | if (!info || !text_copy || !cbank) { |
| 256 | free(info); free(text_copy); free(cbank); |
| 257 | return OMEGA_CUBIN_E_MEM; |
| 258 | } |
| 259 | memcpy(text_copy, text, text_len); |
| 260 | size_t io = 0; |
| 261 | { |
| 262 | uint8_t d[4]; |
| 263 | put32(d, 130); |
| 264 | io += put_attr4(info + io, 0x37, d, 4); |
| 265 | } |
| 266 | io += build_param_info(info + io, params, nparams, pbase); |
| 267 | io += put_attr3(info + io, 0x50, 0); |
| 268 | io += put_attr3(info + io, 0x1b, (uint16_t)gpr_count); |
| 269 | io += put_attr3(info + io, 0x5f, 0x0101); |
| 270 | io += put_attr2(info + io, 0x4a); |
| 271 | /* EIATTR_EXIT_INSTR_OFFSETS omitted: the writer does not decode SASS, and |
| 272 | * a fabricated first-instruction offset is worse than no entry (REVIEW C-06). */ |
| 273 | io += put_attr4(info + io, 0x36, (uint8_t[4]){0,0,0,0}, 4); |
| 274 | size_t info_len = io; |
| 275 | |
| 276 | /* Names. */ |
| 277 | const char *snames[] = { ".shstrtab", ".strtab", ".symtab", text_name, cbank_name, |
| 278 | ".nv.info", ".nv.compat", info_name, ".nv.shared.reserved.0", ".note.nv.cuinfo", |
| 279 | ".note.nv.tkinfo" }; |
| 280 | uint8_t shstr[512], strtab[128]; |
| 281 | size_t shstr_len = 1, strtab_len = 1; |
| 282 | uint32_t shname[11], strname = 0; |
| 283 | shstr[0] = 0; strtab[0] = 0; |
| 284 | for (int i = 0; i < 10 + with_tk; i++) { |
| 285 | size_t n = strlen(snames[i]); |
| 286 | shname[i] = (uint32_t)shstr_len; |
| 287 | memcpy(shstr + shstr_len, snames[i], n + 1); |
| 288 | shstr_len += n + 1; |
| 289 | } |
| 290 | strname = (uint32_t)strtab_len; |
| 291 | memcpy(strtab + strtab_len, fn_name, strlen(fn_name) + 1); |
| 292 | strtab_len += strlen(fn_name) + 1; |
| 293 | |
| 294 | uint8_t symtab[4 * SYM_ENT]; |
| 295 | memset(symtab, 0, sizeof symtab); |
| 296 | put32(symtab + 1 * SYM_ENT + 0, 0); /* st_name */ |
| 297 | symtab[1 * SYM_ENT + 4] = 0x03; /* STT_SECTION, LOCAL */ |
| 298 | put16(symtab + 1 * SYM_ENT + 6, 4); /* .text section index */ |
| 299 | put32(symtab + 2 * SYM_ENT + 0, strname); |
| 300 | symtab[2 * SYM_ENT + 4] = 0x12; /* GLOBAL FUNC */ |
| 301 | symtab[2 * SYM_ENT + 5] = 0x10; /* STO_CUDA_ENTRY */ |
| 302 | put16(symtab + 2 * SYM_ENT + 6, 4); |
| 303 | put64(symtab + 2 * SYM_ENT + 8, 0); |
| 304 | put64(symtab + 2 * SYM_ENT + 16, text_len); |
| 305 | put32(symtab + 3 * SYM_ENT + 0, 0); |
| 306 | symtab[3 * SYM_ENT + 4] = 0x03; |
| 307 | put16(symtab + 3 * SYM_ENT + 6, 5); /* .nv.constant0 section index */ |
| 308 | |
| 309 | Sec secs[12]; |
| 310 | memset(secs, 0, sizeof secs); |
| 311 | int s = 1; |
| 312 | secs[s++] = (Sec){ ".shstrtab", 3, 0, shstr, shstr_len, 0, 0, 1, 0 }; |
| 313 | secs[s++] = (Sec){ ".strtab", 3, 0, strtab, strtab_len, 0, 0, 1, 0 }; |
| 314 | secs[s++] = (Sec){ ".symtab", 2, 0, symtab, sizeof symtab, 2, 2, 8, SYM_ENT }; |
| 315 | secs[s++] = (Sec){ text_name, 1, 0x6, text_copy, text_len, 3, 2, 128, 0 }; |
| 316 | secs[s++] = (Sec){ cbank_name, 1, 0x42, cbank, cbank_size, 3, 4, 16, 0 }; |
| 317 | secs[s++] = (Sec){ ".nv.info", 0x70000000u, 0, ginfo, sizeof ginfo, 3, 0, 4, 0 }; |
| 318 | secs[s++] = (Sec){ ".nv.compat", 0x70000086u, 0, compat, sizeof compat, 0, 0, 4, 0 }; |
| 319 | secs[s++] = (Sec){ info_name, 0x70000000u, 0x40, info, info_len, 3, 4, 4, 0 }; |
| 320 | secs[s++] = (Sec){ ".nv.shared.reserved.0", 8, 0x3, NULL, shared_reserved, 0, 0, 1, 0 }; |
| 321 | secs[s++] = (Sec){ ".note.nv.cuinfo", 7, 0x1000040u, note, sizeof note, with_tk ? 11u : 0u, 2, 4, 0 }; /* link: tkinfo */ |
| 322 | if (with_tk) secs[s++] = (Sec){ ".note.nv.tkinfo", 7, 0x2000000u, tk, sizeof tk, 0, 0, 4, 0 }; |
| 323 | size_t nsec = (size_t)s; |
| 324 | |
| 325 | /* Layout: ELF header, section payloads (aligned), section header table. */ |
| 326 | size_t off = ELF_HDR; |
| 327 | uint64_t sec_off[12]; |
| 328 | memset(sec_off, 0, sizeof sec_off); |
| 329 | for (size_t i = 1; i < nsec; i++) { |
| 330 | off = align_up(off, (size_t)secs[i].align); |
| 331 | sec_off[i] = off; |
| 332 | off += secs[i].size; |
| 333 | } |
| 334 | off = align_up(off, 8); |
| 335 | size_t shoff = off; |
| 336 | size_t phoff = shoff + SH_HDR * nsec; |
| 337 | size_t total = phoff + PHDR_ENT * PHDR_COUNT; |
| 338 | uint8_t *buf = calloc(1, total); |
| 339 | if (!buf) { |
| 340 | free(info); free(text_copy); free(cbank); |
| 341 | return OMEGA_CUBIN_E_MEM; |
| 342 | } |
| 343 | /* Segment ranges: text (index 4), constant bank (5), shared reserved (9). */ |
| 344 | uint64_t text_off = sec_off[4], text_sz = secs[4].size; |
| 345 | uint64_t const_off = sec_off[5], const_sz = secs[5].size; |
| 346 | uint64_t shared_off = sec_off[9], shared_sz = secs[9].size; |
| 347 | /* ELF header. */ |
| 348 | memcpy(buf, "\177ELF", 4); |
| 349 | buf[4] = 2; buf[5] = 1; buf[6] = 1; |
| 350 | buf[7] = target_sm == OMEGA_CUBIN_SM_86 ? 0x33 : 0x41; /* ELFOSABI_CUDA / NV */ |
| 351 | buf[8] = target_sm == OMEGA_CUBIN_SM_86 ? 7 : OMEGA_CUBIN_ABI_VERSION; |
| 352 | put16(buf + 16, 2); /* ET_EXEC */ |
| 353 | put16(buf + 18, 190); /* EM_CUDA */ |
| 354 | put32(buf + 20, target_sm == OMEGA_CUBIN_SM_86 ? 130 : 1); |
| 355 | put64(buf + 24, 0); /* e_entry */ |
| 356 | put64(buf + 32, phoff); /* e_phoff */ |
| 357 | put64(buf + 40, shoff); |
| 358 | put32(buf + 48, target_sm == OMEGA_CUBIN_SM_86 ? OMEGA_CUBIN_EFLAGS_SM86 : OMEGA_CUBIN_EFLAGS_SM121); |
| 359 | put16(buf + 52, ELF_HDR); |
| 360 | put16(buf + 54, PHDR_ENT); |
| 361 | put16(buf + 56, PHDR_COUNT); /* e_phnum */ |
| 362 | put16(buf + 58, SH_HDR); |
| 363 | put16(buf + 60, (uint16_t)nsec); |
| 364 | put16(buf + 62, 1); /* .shstrtab */ |
| 365 | for (size_t i = 1; i < nsec; i++) { |
| 366 | if (secs[i].type != 8 && secs[i].size) memcpy(buf + sec_off[i], secs[i].data, secs[i].size); |
| 367 | } |
| 368 | /* Null section header then each section header. */ |
| 369 | for (size_t i = 1; i < nsec; i++) { |
| 370 | uint8_t *p = buf + shoff + i * SH_HDR; |
| 371 | put32(p + 0, shname[i - 1]); |
| 372 | put32(p + 4, secs[i].type); |
| 373 | put64(p + 8, secs[i].flags); |
| 374 | put64(p + 16, 0); |
| 375 | put64(p + 24, sec_off[i]); |
| 376 | put64(p + 32, secs[i].size); |
| 377 | put32(p + 40, secs[i].link); |
| 378 | put32(p + 44, secs[i].info); |
| 379 | put64(p + 48, secs[i].align); |
| 380 | put64(p + 56, secs[i].entsize); |
| 381 | } |
| 382 | /* Program header table (after the section header table, like the reference). |
| 383 | * 0: PHDR (the table itself), 1: LOAD R (the table), 2: LOAD R|E (text), |
| 384 | * 3: LOAD R|W (shared, zero file size), 4: LOAD R (constant bank). */ |
| 385 | { |
| 386 | uint8_t *ph = buf + phoff; |
| 387 | put_phdr(ph + 0 * PHDR_ENT, 6, 4, phoff, PHDR_ENT * PHDR_COUNT, PHDR_ENT * PHDR_COUNT); |
| 388 | put_phdr(ph + 1 * PHDR_ENT, 1, 4, phoff, PHDR_ENT * PHDR_COUNT, PHDR_ENT * PHDR_COUNT); |
| 389 | put_phdr(ph + 2 * PHDR_ENT, 1, 5, text_off, text_sz, text_sz); |
| 390 | put_phdr(ph + 3 * PHDR_ENT, 1, 6, shared_off, 0, shared_sz); |
| 391 | put_phdr(ph + 4 * PHDR_ENT, 1, 4, const_off, const_sz, const_sz); |
| 392 | } |
| 393 | free(info); |
| 394 | free(text_copy); |
| 395 | free(cbank); |
| 396 | *out = buf; |
| 397 | *out_len = total; |
| 398 | return OMEGA_CUBIN_OK; |
| 399 | } |
| 400 | #endif /* C packer, migration-pending */ |
| 401 | |
| 402 | #ifdef CUBIN_C_PRIMARY |
| 403 | static int cubin_pack_impl(uint32_t target_sm, const char *fn_name, |
| 404 | const uint8_t *text, size_t text_len, |
| 405 | const OmegaCubinParam *params, size_t nparams, |
| 406 | uint32_t cbank_size, uint32_t gpr_count, |
| 407 | uint32_t shared_reserved, |
| 408 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 409 | return c_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 410 | out, out_len, err, err_cap); |
| 411 | } |
| 412 | #else |
| 413 | /* Only the cubin diagnostic/admission roots are needed here; both have checked |
| 414 | * import-free bindings. Do not open the unrelated full core freestanding gate. */ |
| 415 | #include "cudapp_native.h" |
| 416 | #ifdef OMEGA_GPU_NO_POSIX |
| 417 | #include "omega_native.h" |
| 418 | #endif |
| 419 | /* The array lengths of om/cubin/cubin_elf.om's entry, which documents the layouts. */ |
| 420 | #define CUBIN_OM_META 104u /* 8 + 3 * OMEGA_CUBIN_MAX_PARAMS */ |
| 421 | #define CUBIN_OM_TEXT 16777216u /* the text limit, 1 << 24 bytes */ |
| 422 | #define CUBIN_OM_WORDS 16777216u /* output view: 134217728 bytes as little-endian ulongs */ |
| 423 | _Static_assert(OMEGA_CUBIN_MAX_PARAMS == 32u && OMEGA_CUBIN_MAX_NAME == 64u && OMEGA_CUBIN_PARAM_BASE == 0x380u && |
| 424 | OMEGA_CUBIN_CBANK_MAX == 1024u && OMEGA_CUBIN_SM_121 == 121u && OMEGA_CUBIN_ABI_VERSION == 8u && |
| 425 | OMEGA_CUBIN_EFLAGS_SM121 == 0x6007902u, "cubin_elf.om constants"); |
| 426 | _Static_assert(OMEGA_CUBIN_E_ARG == 1 && OMEGA_CUBIN_E_TEXT == 2 && OMEGA_CUBIN_E_PARAMS == 3 && |
| 427 | OMEGA_CUBIN_E_CBANK == 4 && OMEGA_CUBIN_E_GPR == 5 && OMEGA_CUBIN_E_SHARED == 6 && |
| 428 | OMEGA_CUBIN_E_MEM == 7, "cubin_elf.om refusal numbers"); |
| 429 | _Static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, "the image is read out of the unit's words in host order"); |
| 430 | |
| 431 | /* The C writer's message for a refusal of the unit; d is its detail (cubin_elf.om header). */ |
| 432 | static void om_cubin_refusal(uint32_t target_sm, int rc, const uint64_t *d, size_t text_len, const OmegaCubinParam *params, |
| 433 | size_t nparams, uint32_t cbank_size, uint32_t gpr_count, uint32_t shared_reserved, |
| 434 | char *err, size_t err_cap) { |
| 435 | if (!err || !err_cap) return; |
| 436 | uint64_t m[16] = {(uint64_t)rc, target_sm, 0, 0, 0, text_len, nparams, |
| 437 | cbank_size, gpr_count, shared_reserved, 0, 0, err_cap}; |
| 438 | if (d) { m[2] = d[0]; m[3] = d[1]; m[4] = d[2]; } |
| 439 | if (rc == OMEGA_CUBIN_E_PARAMS && params && d && d[1] < nparams) { |
| 440 | m[10] = params[d[1]].offset; |
| 441 | m[11] = params[d[1]].size; |
| 442 | } |
| 443 | char text[256]; |
| 444 | uint64_t n; |
| 445 | if (omega_cubmsg(&n, m, 16, text, sizeof text) != OMEGA_OK) abort(); |
| 446 | memcpy(err, text, (size_t)n + 1); |
| 447 | } |
| 448 | |
| 449 | static int om_cubin_pack(uint32_t target_sm, const char *fn_name, |
| 450 | const uint8_t *text, size_t text_len, |
| 451 | const OmegaCubinParam *params, size_t nparams, |
| 452 | uint32_t cbank_size, uint32_t gpr_count, |
| 453 | uint32_t shared_reserved, |
| 454 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 455 | if (err && err_cap) err[0] = 0; |
| 456 | if (!out || !out_len) return OMEGA_CUBIN_E_ARG; |
| 457 | *out = NULL; |
| 458 | *out_len = 0; |
| 459 | if (!fn_name || !text) { |
| 460 | om_cubin_refusal(target_sm, OMEGA_CUBIN_E_ARG, NULL, text_len, params, nparams, |
| 461 | cbank_size, gpr_count, shared_reserved, err, err_cap); |
| 462 | return OMEGA_CUBIN_E_ARG; |
| 463 | } |
| 464 | uint64_t m[CUBIN_OM_META] = { 0 }; |
| 465 | uint8_t nm[OMEGA_CUBIN_MAX_NAME] = { 0 }; |
| 466 | size_t n = strlen(fn_name); |
| 467 | memcpy(nm, fn_name, n < sizeof nm ? n : sizeof nm); |
| 468 | m[0] = n; |
| 469 | m[1] = text_len; |
| 470 | m[2] = nparams; |
| 471 | m[3] = cbank_size; |
| 472 | m[4] = gpr_count; |
| 473 | m[5] = shared_reserved; |
| 474 | m[6] = target_sm; |
| 475 | if (nparams <= OMEGA_CUBIN_MAX_PARAMS) |
| 476 | for (size_t i = 0; i < nparams; i++) { |
| 477 | m[8 + 3 * i] = params[i].offset; |
| 478 | m[9 + 3 * i] = params[i].size; |
| 479 | m[10 + 3 * i] = params[i].is_pointer; |
| 480 | } |
| 481 | uint8_t *tx = calloc(1, CUBIN_OM_TEXT); |
| 482 | uint64_t *o = calloc(CUBIN_OM_WORDS, sizeof *o); |
| 483 | if (!tx || !o) { |
| 484 | free(tx); free(o); |
| 485 | return OMEGA_CUBIN_E_MEM; |
| 486 | } |
| 487 | if (text_len <= CUBIN_OM_TEXT) memcpy(tx, text, text_len); |
| 488 | uint64_t r = 0; |
| 489 | #ifdef OMEGA_GPU_NO_POSIX |
| 490 | uint32_t st = omega2_cubin_elf(&r, m, CUBIN_OM_META, nm, sizeof nm, tx, CUBIN_OM_TEXT, o, CUBIN_OM_WORDS); |
| 491 | #else |
| 492 | uint32_t st = omega_cubin_elf(&r, m, CUBIN_OM_META, nm, sizeof nm, tx, CUBIN_OM_TEXT, o, CUBIN_OM_WORDS); |
| 493 | #endif |
| 494 | free(tx); |
| 495 | int rc = OMEGA_CUBIN_OK; |
| 496 | if (st != OMEGA_OK) { |
| 497 | uint64_t detail[3] = {st, 0, 0}; |
| 498 | om_cubin_refusal(target_sm, 8, detail, text_len, params, nparams, |
| 499 | cbank_size, gpr_count, shared_reserved, err, err_cap); |
| 500 | rc = OMEGA_CUBIN_E_MEM; |
| 501 | } else if (r <= OMEGA_CUBIN_E_MEM) { |
| 502 | rc = (int)r; |
| 503 | om_cubin_refusal(target_sm, rc, o, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, err, err_cap); |
| 504 | } else { |
| 505 | uint8_t *buf = malloc((size_t)r); |
| 506 | if (!buf) { |
| 507 | rc = OMEGA_CUBIN_E_MEM; |
| 508 | } else { |
| 509 | memcpy(buf, o, (size_t)r); |
| 510 | *out = buf; |
| 511 | *out_len = (size_t)r; |
| 512 | } |
| 513 | } |
| 514 | free(o); |
| 515 | return rc; |
| 516 | } |
| 517 | |
| 518 | #ifndef CUDAPP_MIGRATION_PENDING_CUBIN_DIFF |
| 519 | static int cubin_pack_impl(uint32_t target_sm, const char *fn_name, |
| 520 | const uint8_t *text, size_t text_len, |
| 521 | const OmegaCubinParam *params, size_t nparams, |
| 522 | uint32_t cbank_size, uint32_t gpr_count, |
| 523 | uint32_t shared_reserved, |
| 524 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 525 | return om_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 526 | out, out_len, err, err_cap); |
| 527 | } |
| 528 | #else |
| 529 | /* One line per agreement, appended; any disagreement aborts the process after naming it. */ |
| 530 | static void cubin_diff_note(int rc, size_t len, int agree) { |
| 531 | const char *path = getenv("CUDAPP_CUBIN_DIFF_LOG"); |
| 532 | if (path && path[0]) { |
| 533 | FILE *f = fopen(path, "a"); |
| 534 | if (f) { |
| 535 | fprintf(f, "cubin rc=%d bytes=%zu %s\n", rc, len, agree ? "SAME" : "DIFFER"); |
| 536 | fclose(f); |
| 537 | } |
| 538 | } |
| 539 | if (!agree) { |
| 540 | fprintf(stderr, "CUBIN DIFF: .om and C packers disagree (rc %d, %zu bytes)\n", rc, len); |
| 541 | abort(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static int cubin_pack_impl(uint32_t target_sm, const char *fn_name, |
| 546 | const uint8_t *text, size_t text_len, |
| 547 | const OmegaCubinParam *params, size_t nparams, |
| 548 | uint32_t cbank_size, uint32_t gpr_count, |
| 549 | uint32_t shared_reserved, |
| 550 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 551 | if (!out || !out_len) { |
| 552 | int a = om_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 553 | out, out_len, err, err_cap); |
| 554 | int b = c_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 555 | out, out_len, err, err_cap); |
| 556 | cubin_diff_note(a, 0, a == b); |
| 557 | return a; |
| 558 | } |
| 559 | char e1[256], e2[256]; |
| 560 | uint8_t *b1 = NULL, *b2 = NULL; |
| 561 | size_t n1 = 0, n2 = 0; |
| 562 | int r1 = om_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 563 | &b1, &n1, e1, sizeof e1); |
| 564 | #ifdef CUDAPP_CUBIN_DIFF_SELFTEST |
| 565 | if (r1 == OMEGA_CUBIN_OK && n1) b1[n1 / 2] ^= 1; /* red check of the comparison itself: one flipped bit must abort */ |
| 566 | #endif |
| 567 | int r2 = c_cubin_pack(target_sm, fn_name, text, text_len, params, nparams, cbank_size, gpr_count, shared_reserved, |
| 568 | &b2, &n2, e2, sizeof e2); |
| 569 | cubin_diff_note(r1, n1, r1 == r2 && n1 == n2 && (n1 == 0 || memcmp(b1, b2, n1) == 0) && strcmp(e1, e2) == 0); |
| 570 | free(b2); |
| 571 | if (err && err_cap) snprintf(err, err_cap, "%s", e1); |
| 572 | *out = b1; |
| 573 | *out_len = n1; |
| 574 | return r1; |
| 575 | } |
| 576 | #endif |
| 577 | #endif |
| 578 | |
| 579 | #ifndef CUBIN_C_PRIMARY |
| 580 | /* Target and null-parameter admission. Pointer presence is a flag; the message is Omega. */ |
| 581 | static int cubin_admit(uint32_t target_sm, size_t nparams, int present, char *err, size_t err_cap) { |
| 582 | uint64_t rc = 0; |
| 583 | char msg[64]; |
| 584 | if (omega_cubadmit(&rc, target_sm, (uint64_t)nparams, present ? 1u : 0u, msg, sizeof msg) != OMEGA_OK) |
| 585 | abort(); |
| 586 | if (rc && err && err_cap) { |
| 587 | size_t n = 0; |
| 588 | while (n + 1 < err_cap && msg[n]) { err[n] = msg[n]; n++; } |
| 589 | err[n] = 0; |
| 590 | } |
| 591 | return (int)rc; |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | int omega_cubin_pack_target(uint32_t target_sm, const char *fn_name, |
| 596 | const uint8_t *text, size_t text_len, |
| 597 | const OmegaCubinParam *params, size_t nparams, |
| 598 | uint32_t cbank_size, uint32_t gpr_count, uint32_t shared_reserved, |
| 599 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 600 | if (err && err_cap) err[0] = 0; |
| 601 | if (out) *out = NULL; |
| 602 | if (out_len) *out_len = 0; |
| 603 | #ifndef CUBIN_C_PRIMARY |
| 604 | { |
| 605 | int admit = cubin_admit(target_sm, nparams, params != NULL, err, err_cap); |
| 606 | if (admit) return admit; |
| 607 | } |
| 608 | #else |
| 609 | if (target_sm != OMEGA_CUBIN_SM_86 && target_sm != OMEGA_CUBIN_SM_121) { |
| 610 | if (err && err_cap) snprintf(err, err_cap, "unsupported cubin target sm_%u", target_sm); |
| 611 | return OMEGA_CUBIN_E_TARGET; |
| 612 | } |
| 613 | if (nparams && !params) { |
| 614 | if (err && err_cap) snprintf(err, err_cap, "null parameter array"); |
| 615 | return OMEGA_CUBIN_E_ARG; |
| 616 | } |
| 617 | #endif |
| 618 | return cubin_pack_impl(target_sm, fn_name, text, text_len, params, nparams, |
| 619 | cbank_size, gpr_count, shared_reserved, out, out_len, err, err_cap); |
| 620 | } |
| 621 | |
| 622 | int omega_cubin_pack(const char *fn_name, const uint8_t *text, size_t text_len, |
| 623 | const OmegaCubinParam *params, size_t nparams, |
| 624 | uint32_t cbank_size, uint32_t gpr_count, uint32_t shared_reserved, |
| 625 | uint8_t **out, size_t *out_len, char *err, size_t err_cap) { |
| 626 | return omega_cubin_pack_target(OMEGA_CUBIN_SM_121, fn_name, text, text_len, |
| 627 | params, nparams, cbank_size, gpr_count, shared_reserved, |
| 628 | out, out_len, err, err_cap); |
| 629 | } |