| 1 |
/* |
| 2 |
* Copyright (C) 2019 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2017-2018 Lyude Paul |
| 4 |
* Copyright (C) 2019 Collabora, Ltd. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/mman.h> |
| 14 |
|
| 15 |
#include "lib/pan_props.h" |
| 16 |
#include "util/log.h" |
| 17 |
#include "util/macros.h" |
| 18 |
#include "util/u_debug.h" |
| 19 |
#include "util/u_hexdump.h" |
| 20 |
#include "util/u_process.h" |
| 21 |
#include "decode.h" |
| 22 |
|
| 23 |
#include "compiler/pan_compiler.h" |
| 24 |
|
| 25 |
/* Used to distiguish dumped files, otherwise we would have to print the ctx |
| 26 |
* pointer, which is annoying for the user since it changes with every run */ |
| 27 |
static int num_ctxs = 0; |
| 28 |
|
| 29 |
#define to_mapped_memory(x) \ |
| 30 |
rb_node_data(struct pandecode_mapped_memory, x, node) |
| 31 |
|
| 32 |
/* |
| 33 |
* Compare a GPU VA to a node, considering a GPU VA to be equal to a node if it |
| 34 |
* is contained in the interval the node represents. This lets us store |
| 35 |
* intervals in our tree. |
| 36 |
*/ |
| 37 |
static int |
| 38 |
pandecode_cmp_key(const struct rb_node *lhs, const void *key) |
| 39 |
{ |
| 40 |
struct pandecode_mapped_memory *mem = to_mapped_memory(lhs); |
| 41 |
uint64_t *gpu_va = (uint64_t *)key; |
| 42 |
|
| 43 |
if (mem->gpu_va <= *gpu_va && *gpu_va < (mem->gpu_va + mem->length)) |
| 44 |
return 0; |
| 45 |
else |
| 46 |
return (mem->gpu_va < *gpu_va) ? -1 : 1; |
| 47 |
} |
| 48 |
|
| 49 |
static int |
| 50 |
pandecode_cmp(const struct rb_node *lhs, const struct rb_node *rhs) |
| 51 |
{ |
| 52 |
return (to_mapped_memory(lhs)->gpu_va < to_mapped_memory(rhs)->gpu_va) ? -1 : 1; |
| 53 |
} |
| 54 |
|
| 55 |
static struct pandecode_mapped_memory * |
| 56 |
pandecode_find_mapped_gpu_mem_containing_rw(struct pandecode_context *ctx, |
| 57 |
uint64_t addr) |
| 58 |
{ |
| 59 |
simple_mtx_assert_locked(&ctx->lock); |
| 60 |
|
| 61 |
struct rb_node *node = |
| 62 |
rb_tree_search(&ctx->mmap_tree, &addr, pandecode_cmp_key); |
| 63 |
|
| 64 |
return to_mapped_memory(node); |
| 65 |
} |
| 66 |
|
| 67 |
struct pandecode_mapped_memory * |
| 68 |
pandecode_find_mapped_gpu_mem_containing(struct pandecode_context *ctx, |
| 69 |
uint64_t addr) |
| 70 |
{ |
| 71 |
simple_mtx_assert_locked(&ctx->lock); |
| 72 |
|
| 73 |
struct pandecode_mapped_memory *mem = |
| 74 |
pandecode_find_mapped_gpu_mem_containing_rw(ctx, addr); |
| 75 |
|
| 76 |
if (mem && mem->addr && !mem->ro) { |
| 77 |
mprotect(mem->addr, mem->length, PROT_READ); |
| 78 |
mem->ro = true; |
| 79 |
util_dynarray_append(&ctx->ro_mappings, mem); |
| 80 |
} |
| 81 |
|
| 82 |
return mem; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* To check for memory safety issues, validates that the given pointer in GPU |
| 87 |
* memory is valid, containing at least sz bytes. This function is a tool to |
| 88 |
* detect GPU-side memory bugs by validating pointers. |
| 89 |
*/ |
| 90 |
void |
| 91 |
pandecode_validate_buffer(struct pandecode_context *ctx, uint64_t addr, |
| 92 |
size_t sz) |
| 93 |
{ |
| 94 |
if (!addr) { |
| 95 |
pandecode_log(ctx, "// XXX: null pointer deref\n"); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
/* Find a BO */ |
| 100 |
|
| 101 |
struct pandecode_mapped_memory *bo = |
| 102 |
pandecode_find_mapped_gpu_mem_containing(ctx, addr); |
| 103 |
|
| 104 |
if (!bo) { |
| 105 |
pandecode_log(ctx, "// XXX: invalid memory dereference\n"); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
/* Bounds check */ |
| 110 |
|
| 111 |
unsigned offset = addr - bo->gpu_va; |
| 112 |
unsigned total = offset + sz; |
| 113 |
|
| 114 |
if (total > bo->length) { |
| 115 |
pandecode_log(ctx, |
| 116 |
"// XXX: buffer overrun. " |
| 117 |
"Chunk of size %zu at offset %d in buffer of size %zu. " |
| 118 |
"Overrun by %zu bytes. \n", |
| 119 |
sz, offset, bo->length, total - bo->length); |
| 120 |
return; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
pandecode_map_read_write(struct pandecode_context *ctx) |
| 126 |
{ |
| 127 |
simple_mtx_assert_locked(&ctx->lock); |
| 128 |
|
| 129 |
util_dynarray_foreach(&ctx->ro_mappings, struct pandecode_mapped_memory *, |
| 130 |
mem) { |
| 131 |
(*mem)->ro = false; |
| 132 |
mprotect((*mem)->addr, (*mem)->length, PROT_READ | PROT_WRITE); |
| 133 |
} |
| 134 |
util_dynarray_clear(&ctx->ro_mappings); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
pandecode_add_name(struct pandecode_context *ctx, |
| 139 |
struct pandecode_mapped_memory *mem, uint64_t gpu_va, |
| 140 |
const char *name) |
| 141 |
{ |
| 142 |
simple_mtx_assert_locked(&ctx->lock); |
| 143 |
|
| 144 |
if (!name) { |
| 145 |
/* If we don't have a name, assign one */ |
| 146 |
|
| 147 |
snprintf(mem->name, sizeof(mem->name) - 1, "memory_%" PRIx64, gpu_va); |
| 148 |
} else { |
| 149 |
assert((strlen(name) + 1) < sizeof(mem->name)); |
| 150 |
memcpy(mem->name, name, strlen(name) + 1); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
pandecode_inject_mmap(struct pandecode_context *ctx, uint64_t gpu_va, void *cpu, |
| 156 |
unsigned sz, const char *name) |
| 157 |
{ |
| 158 |
simple_mtx_lock(&ctx->lock); |
| 159 |
|
| 160 |
/* First, search if we already mapped this and are just updating an address */ |
| 161 |
|
| 162 |
struct pandecode_mapped_memory *existing = |
| 163 |
pandecode_find_mapped_gpu_mem_containing_rw(ctx, gpu_va); |
| 164 |
|
| 165 |
if (existing && existing->gpu_va == gpu_va) { |
| 166 |
existing->length = sz; |
| 167 |
existing->addr = cpu; |
| 168 |
pandecode_add_name(ctx, existing, gpu_va, name); |
| 169 |
} else { |
| 170 |
/* Otherwise, add a fresh mapping */ |
| 171 |
struct pandecode_mapped_memory *mapped_mem = NULL; |
| 172 |
|
| 173 |
mapped_mem = calloc(1, sizeof(*mapped_mem)); |
| 174 |
mapped_mem->gpu_va = gpu_va; |
| 175 |
mapped_mem->length = sz; |
| 176 |
mapped_mem->addr = cpu; |
| 177 |
pandecode_add_name(ctx, mapped_mem, gpu_va, name); |
| 178 |
|
| 179 |
/* Add it to the tree */ |
| 180 |
rb_tree_insert(&ctx->mmap_tree, &mapped_mem->node, pandecode_cmp); |
| 181 |
} |
| 182 |
|
| 183 |
simple_mtx_unlock(&ctx->lock); |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
pandecode_inject_free(struct pandecode_context *ctx, uint64_t gpu_va, |
| 188 |
unsigned sz) |
| 189 |
{ |
| 190 |
simple_mtx_lock(&ctx->lock); |
| 191 |
|
| 192 |
struct pandecode_mapped_memory *mem = |
| 193 |
pandecode_find_mapped_gpu_mem_containing_rw(ctx, gpu_va); |
| 194 |
|
| 195 |
if (mem) { |
| 196 |
assert(mem->gpu_va == gpu_va); |
| 197 |
assert(mem->length == sz); |
| 198 |
|
| 199 |
rb_tree_remove(&ctx->mmap_tree, &mem->node); |
| 200 |
free(mem); |
| 201 |
} |
| 202 |
|
| 203 |
simple_mtx_unlock(&ctx->lock); |
| 204 |
} |
| 205 |
|
| 206 |
char * |
| 207 |
pointer_as_memory_reference(struct pandecode_context *ctx, uint64_t ptr) |
| 208 |
{ |
| 209 |
simple_mtx_assert_locked(&ctx->lock); |
| 210 |
|
| 211 |
struct pandecode_mapped_memory *mapped; |
| 212 |
char *out = malloc(128); |
| 213 |
|
| 214 |
/* Try to find the corresponding mapped zone */ |
| 215 |
|
| 216 |
mapped = pandecode_find_mapped_gpu_mem_containing_rw(ctx, ptr); |
| 217 |
|
| 218 |
if (mapped) { |
| 219 |
snprintf(out, 128, "%s + %d", mapped->name, (int)(ptr - mapped->gpu_va)); |
| 220 |
return out; |
| 221 |
} |
| 222 |
|
| 223 |
/* Just use the raw address if other options are exhausted */ |
| 224 |
|
| 225 |
snprintf(out, 128, "0x%" PRIx64, ptr); |
| 226 |
return out; |
| 227 |
} |
| 228 |
|
| 229 |
void |
| 230 |
pandecode_dump_file_open(struct pandecode_context *ctx) |
| 231 |
{ |
| 232 |
simple_mtx_assert_locked(&ctx->lock); |
| 233 |
|
| 234 |
/* This does a os_get_option every frame, so it is possible to use |
| 235 |
* os_set_option to change the base at runtime. |
| 236 |
*/ |
| 237 |
const char *dump_file_base = |
| 238 |
debug_get_option("PANDECODE_DUMP_FILE", "pandecode.dump"); |
| 239 |
if (!strcmp(dump_file_base, "stderr")) |
| 240 |
ctx->dump_stream = stderr; |
| 241 |
else if (!ctx->dump_stream) { |
| 242 |
char buffer[1024]; |
| 243 |
snprintf(buffer, sizeof(buffer), "%s.%s.ctx-%d.%04d", dump_file_base, |
| 244 |
util_get_process_name(), ctx->id, ctx->dump_frame_count); |
| 245 |
mesa_logd("pandecode: dump command stream to file %s", buffer); |
| 246 |
ctx->dump_stream = fopen(buffer, "w"); |
| 247 |
if (!ctx->dump_stream) { |
| 248 |
mesa_loge("pandecode: failed to open command stream log file %s", |
| 249 |
buffer); |
| 250 |
|
| 251 |
/* Storage access on Android is quite restricted and varies across |
| 252 |
* different processes. Meanwhile, PANDECODE_DUMP_FILE option is |
| 253 |
* global on Android. So we have to fallback to log command stream to |
| 254 |
* stderr (though won't show up in Android logcat) for those unable to |
| 255 |
* create the dump file but involved during app launch animation (e.g. |
| 256 |
* system_server priviledged process). |
| 257 |
*/ |
| 258 |
mesa_loge("pandecode: fall back to log command stream to stderr"); |
| 259 |
ctx->dump_stream = stderr; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
static void |
| 265 |
pandecode_dump_file_close(struct pandecode_context *ctx) |
| 266 |
{ |
| 267 |
simple_mtx_assert_locked(&ctx->lock); |
| 268 |
|
| 269 |
if (ctx->dump_stream && ctx->dump_stream != stderr) { |
| 270 |
if (fclose(ctx->dump_stream)) |
| 271 |
perror("pandecode: dump file"); |
| 272 |
|
| 273 |
ctx->dump_stream = NULL; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
struct pandecode_context * |
| 278 |
pandecode_create_context(bool to_stderr) |
| 279 |
{ |
| 280 |
struct pandecode_context *ctx = calloc(1, sizeof(*ctx)); |
| 281 |
|
| 282 |
/* Not thread safe, but we shouldn't ever hit this, and even if we do, the |
| 283 |
* worst that could happen is having the files dumped with their filenames |
| 284 |
* in a different order. */ |
| 285 |
ctx->id = num_ctxs++; |
| 286 |
|
| 287 |
/* This will be initialized later and can be changed at run time through |
| 288 |
* the PANDECODE_DUMP_FILE environment variable. |
| 289 |
*/ |
| 290 |
ctx->dump_stream = to_stderr ? stderr : NULL; |
| 291 |
|
| 292 |
rb_tree_init(&ctx->mmap_tree); |
| 293 |
ctx->ro_mappings = UTIL_DYNARRAY_INIT; |
| 294 |
|
| 295 |
simple_mtx_t mtx_init = SIMPLE_MTX_INITIALIZER; |
| 296 |
memcpy(&ctx->lock, &mtx_init, sizeof(simple_mtx_t)); |
| 297 |
|
| 298 |
return ctx; |
| 299 |
} |
| 300 |
|
| 301 |
void |
| 302 |
pandecode_next_frame(struct pandecode_context *ctx) |
| 303 |
{ |
| 304 |
simple_mtx_lock(&ctx->lock); |
| 305 |
|
| 306 |
pandecode_dump_file_close(ctx); |
| 307 |
ctx->dump_frame_count++; |
| 308 |
|
| 309 |
simple_mtx_unlock(&ctx->lock); |
| 310 |
} |
| 311 |
|
| 312 |
void |
| 313 |
pandecode_destroy_context(struct pandecode_context *ctx) |
| 314 |
{ |
| 315 |
simple_mtx_lock(&ctx->lock); |
| 316 |
|
| 317 |
rb_tree_foreach_safe(struct pandecode_mapped_memory, it, &ctx->mmap_tree, |
| 318 |
node) { |
| 319 |
rb_tree_remove(&ctx->mmap_tree, &it->node); |
| 320 |
free(it); |
| 321 |
} |
| 322 |
|
| 323 |
util_dynarray_fini(&ctx->ro_mappings); |
| 324 |
pandecode_dump_file_close(ctx); |
| 325 |
|
| 326 |
simple_mtx_unlock(&ctx->lock); |
| 327 |
|
| 328 |
free(ctx); |
| 329 |
} |
| 330 |
|
| 331 |
void |
| 332 |
pandecode_dump_mappings(struct pandecode_context *ctx) |
| 333 |
{ |
| 334 |
simple_mtx_lock(&ctx->lock); |
| 335 |
|
| 336 |
pandecode_dump_file_open(ctx); |
| 337 |
|
| 338 |
rb_tree_foreach(struct pandecode_mapped_memory, it, &ctx->mmap_tree, node) { |
| 339 |
if (!it->addr || !it->length) |
| 340 |
continue; |
| 341 |
|
| 342 |
fprintf(ctx->dump_stream, "Buffer: %s gpu %" PRIx64 "\n\n", it->name, |
| 343 |
it->gpu_va); |
| 344 |
|
| 345 |
u_hexdump(ctx->dump_stream, it->addr, it->length, false); |
| 346 |
fprintf(ctx->dump_stream, "\n"); |
| 347 |
} |
| 348 |
|
| 349 |
fflush(ctx->dump_stream); |
| 350 |
simple_mtx_unlock(&ctx->lock); |
| 351 |
} |
| 352 |
|
| 353 |
void |
| 354 |
pandecode_abort_on_fault(struct pandecode_context *ctx, uint64_t jc_gpu_va, |
| 355 |
uint64_t gpu_id) |
| 356 |
{ |
| 357 |
simple_mtx_lock(&ctx->lock); |
| 358 |
|
| 359 |
switch (pan_arch(gpu_id)) { |
| 360 |
case 4: |
| 361 |
pandecode_abort_on_fault_v4(ctx, jc_gpu_va); |
| 362 |
break; |
| 363 |
case 5: |
| 364 |
pandecode_abort_on_fault_v5(ctx, jc_gpu_va); |
| 365 |
break; |
| 366 |
case 6: |
| 367 |
pandecode_abort_on_fault_v6(ctx, jc_gpu_va); |
| 368 |
break; |
| 369 |
case 7: |
| 370 |
pandecode_abort_on_fault_v7(ctx, jc_gpu_va); |
| 371 |
break; |
| 372 |
case 9: |
| 373 |
pandecode_abort_on_fault_v9(ctx, jc_gpu_va); |
| 374 |
break; |
| 375 |
default: |
| 376 |
UNREACHABLE("Unsupported architecture"); |
| 377 |
} |
| 378 |
|
| 379 |
simple_mtx_unlock(&ctx->lock); |
| 380 |
} |
| 381 |
|
| 382 |
void |
| 383 |
pandecode_jc(struct pandecode_context *ctx, uint64_t jc_gpu_va, uint64_t gpu_id) |
| 384 |
{ |
| 385 |
simple_mtx_lock(&ctx->lock); |
| 386 |
|
| 387 |
switch (pan_arch(gpu_id)) { |
| 388 |
case 4: |
| 389 |
pandecode_jc_v4(ctx, jc_gpu_va, gpu_id); |
| 390 |
break; |
| 391 |
case 5: |
| 392 |
pandecode_jc_v5(ctx, jc_gpu_va, gpu_id); |
| 393 |
break; |
| 394 |
case 6: |
| 395 |
pandecode_jc_v6(ctx, jc_gpu_va, gpu_id); |
| 396 |
break; |
| 397 |
case 7: |
| 398 |
pandecode_jc_v7(ctx, jc_gpu_va, gpu_id); |
| 399 |
break; |
| 400 |
case 9: |
| 401 |
pandecode_jc_v9(ctx, jc_gpu_va, gpu_id); |
| 402 |
break; |
| 403 |
default: |
| 404 |
UNREACHABLE("Unsupported architecture"); |
| 405 |
} |
| 406 |
|
| 407 |
simple_mtx_unlock(&ctx->lock); |
| 408 |
} |
| 409 |
|
| 410 |
void |
| 411 |
pandecode_interpret_cs(struct pandecode_context *ctx, uint64_t queue_gpu_va, |
| 412 |
uint32_t size, uint64_t gpu_id, uint32_t *regs) |
| 413 |
{ |
| 414 |
simple_mtx_lock(&ctx->lock); |
| 415 |
|
| 416 |
switch (pan_arch(gpu_id)) { |
| 417 |
case 10: |
| 418 |
pandecode_interpret_cs_v10(ctx, queue_gpu_va, size, gpu_id, regs); |
| 419 |
break; |
| 420 |
case 12: |
| 421 |
pandecode_interpret_cs_v12(ctx, queue_gpu_va, size, gpu_id, regs); |
| 422 |
break; |
| 423 |
case 13: |
| 424 |
pandecode_interpret_cs_v13(ctx, queue_gpu_va, size, gpu_id, regs); |
| 425 |
break; |
| 426 |
default: |
| 427 |
UNREACHABLE("Unsupported architecture"); |
| 428 |
} |
| 429 |
|
| 430 |
simple_mtx_unlock(&ctx->lock); |
| 431 |
} |
| 432 |
|
| 433 |
void |
| 434 |
pandecode_cs_binary(struct pandecode_context *ctx, uint64_t bin_gpu_va, |
| 435 |
uint32_t size, uint64_t gpu_id) |
| 436 |
{ |
| 437 |
simple_mtx_lock(&ctx->lock); |
| 438 |
|
| 439 |
switch (pan_arch(gpu_id)) { |
| 440 |
case 10: |
| 441 |
pandecode_cs_binary_v10(ctx, bin_gpu_va, size); |
| 442 |
break; |
| 443 |
case 12: |
| 444 |
pandecode_cs_binary_v12(ctx, bin_gpu_va, size); |
| 445 |
break; |
| 446 |
case 13: |
| 447 |
pandecode_cs_binary_v13(ctx, bin_gpu_va, size); |
| 448 |
break; |
| 449 |
default: |
| 450 |
UNREACHABLE("Unsupported architecture"); |
| 451 |
} |
| 452 |
|
| 453 |
simple_mtx_unlock(&ctx->lock); |
| 454 |
} |
| 455 |
|
| 456 |
void |
| 457 |
pandecode_cs_trace(struct pandecode_context *ctx, uint64_t trace_gpu_va, |
| 458 |
uint32_t size, uint64_t gpu_id) |
| 459 |
{ |
| 460 |
simple_mtx_lock(&ctx->lock); |
| 461 |
|
| 462 |
switch (pan_arch(gpu_id)) { |
| 463 |
case 10: |
| 464 |
pandecode_cs_trace_v10(ctx, trace_gpu_va, size, gpu_id); |
| 465 |
break; |
| 466 |
case 12: |
| 467 |
pandecode_cs_trace_v12(ctx, trace_gpu_va, size, gpu_id); |
| 468 |
break; |
| 469 |
case 13: |
| 470 |
pandecode_cs_trace_v13(ctx, trace_gpu_va, size, gpu_id); |
| 471 |
break; |
| 472 |
default: |
| 473 |
UNREACHABLE("Unsupported architecture"); |
| 474 |
} |
| 475 |
|
| 476 |
simple_mtx_unlock(&ctx->lock); |
| 477 |
} |
| 478 |
|
| 479 |
void |
| 480 |
pandecode_shader_disassemble(struct pandecode_context *ctx, uint64_t shader_ptr, |
| 481 |
uint64_t gpu_id) |
| 482 |
{ |
| 483 |
uint8_t *PANDECODE_PTR_VAR(ctx, code, shader_ptr); |
| 484 |
|
| 485 |
/* Compute maximum possible size */ |
| 486 |
struct pandecode_mapped_memory *mem = |
| 487 |
pandecode_find_mapped_gpu_mem_containing(ctx, shader_ptr); |
| 488 |
size_t sz = mem->length - (shader_ptr - mem->gpu_va); |
| 489 |
|
| 490 |
/* Print some boilerplate to clearly denote the assembly (which doesn't |
| 491 |
* obey indentation rules), and actually do the disassembly! */ |
| 492 |
|
| 493 |
pandecode_log_cont(ctx, "\nShader %p (GPU VA %" PRIx64 ") sz %" PRId64 "\n", |
| 494 |
code, shader_ptr, sz); |
| 495 |
|
| 496 |
bool verbose = pan_arch(gpu_id) >= 6 && pan_arch(gpu_id) < 9; |
| 497 |
pan_disassemble(ctx->dump_stream, code, sz, gpu_id, verbose); |
| 498 |
|
| 499 |
pandecode_log_cont(ctx, "\n\n"); |
| 500 |
} |