Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 Alyssa Rosenzweig |
| 3 | * Copyright (C) 2017-2018 Lyude Paul |
Alyssa Rosenzweig | d4575c3 | 2019-06-25 13:30:17 -0700 | [diff] [blame] | 4 | * Copyright (C) 2019 Collabora, Ltd. |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 31 | #include <sys/mman.h> |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 32 | |
| 33 | #include "decode.h" |
| 34 | #include "util/macros.h" |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 35 | #include "util/u_debug.h" |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 36 | #include "util/u_dynarray.h" |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 37 | #include "util/hash_table.h" |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 38 | |
| 39 | /* Memory handling */ |
| 40 | |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 41 | static struct hash_table_u64 *mmap_table; |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 42 | |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 43 | static struct util_dynarray ro_mappings; |
| 44 | |
| 45 | static struct pandecode_mapped_memory * |
| 46 | pandecode_find_mapped_gpu_mem_containing_rw(uint64_t addr) |
| 47 | { |
| 48 | return _mesa_hash_table_u64_search(mmap_table, addr & ~(4096 - 1)); |
| 49 | } |
| 50 | |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 51 | struct pandecode_mapped_memory * |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 52 | pandecode_find_mapped_gpu_mem_containing(uint64_t addr) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 53 | { |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 54 | struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing_rw(addr); |
| 55 | |
| 56 | if (mem && mem->addr && !mem->ro) { |
| 57 | mprotect(mem->addr, mem->length, PROT_READ); |
| 58 | mem->ro = true; |
| 59 | util_dynarray_append(&ro_mappings, struct pandecode_mapped_memory *, mem); |
| 60 | } |
| 61 | |
| 62 | return mem; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | pandecode_map_read_write(void) |
| 67 | { |
| 68 | util_dynarray_foreach(&ro_mappings, struct pandecode_mapped_memory *, mem) { |
| 69 | (*mem)->ro = false; |
| 70 | mprotect((*mem)->addr, (*mem)->length, PROT_READ | PROT_WRITE); |
| 71 | } |
| 72 | util_dynarray_clear(&ro_mappings); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 75 | static void |
| 76 | pandecode_add_name(struct pandecode_mapped_memory *mem, uint64_t gpu_va, const char *name) |
| 77 | { |
| 78 | if (!name) { |
| 79 | /* If we don't have a name, assign one */ |
| 80 | |
| 81 | snprintf(mem->name, ARRAY_SIZE(mem->name) - 1, |
| 82 | "memory_%" PRIx64, gpu_va); |
| 83 | } else { |
| 84 | assert((strlen(name) + 1) < ARRAY_SIZE(mem->name)); |
| 85 | memcpy(mem->name, name, strlen(name) + 1); |
| 86 | } |
| 87 | } |
| 88 | |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 89 | void |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 90 | pandecode_inject_mmap(uint64_t gpu_va, void *cpu, unsigned sz, const char *name) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 91 | { |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 92 | /* First, search if we already mapped this and are just updating an address */ |
| 93 | |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 94 | struct pandecode_mapped_memory *existing = |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 95 | pandecode_find_mapped_gpu_mem_containing_rw(gpu_va); |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 96 | |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 97 | if (existing && existing->gpu_va == gpu_va) { |
| 98 | existing->length = sz; |
| 99 | existing->addr = cpu; |
| 100 | pandecode_add_name(existing, gpu_va, name); |
| 101 | return; |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Otherwise, add a fresh mapping */ |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 105 | struct pandecode_mapped_memory *mapped_mem = NULL; |
| 106 | |
Alyssa Rosenzweig | 7c351a6 | 2020-09-24 19:13:43 -0400 | [diff] [blame^] | 107 | mapped_mem = calloc(1, sizeof(*mapped_mem)); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 108 | mapped_mem->gpu_va = gpu_va; |
| 109 | mapped_mem->length = sz; |
| 110 | mapped_mem->addr = cpu; |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 111 | pandecode_add_name(mapped_mem, gpu_va, name); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 112 | |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 113 | /* Add it to the table */ |
| 114 | assert((gpu_va & 4095) == 0); |
| 115 | |
| 116 | for (unsigned i = 0; i < sz; i += 4096) |
| 117 | _mesa_hash_table_u64_insert(mmap_table, gpu_va + i, mapped_mem); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | char * |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 121 | pointer_as_memory_reference(uint64_t ptr) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 122 | { |
| 123 | struct pandecode_mapped_memory *mapped; |
| 124 | char *out = malloc(128); |
| 125 | |
| 126 | /* Try to find the corresponding mapped zone */ |
| 127 | |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 128 | mapped = pandecode_find_mapped_gpu_mem_containing_rw(ptr); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 129 | |
| 130 | if (mapped) { |
| 131 | snprintf(out, 128, "%s + %d", mapped->name, (int) (ptr - mapped->gpu_va)); |
| 132 | return out; |
| 133 | } |
| 134 | |
| 135 | /* Just use the raw address if other options are exhausted */ |
| 136 | |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 137 | snprintf(out, 128, "0x%" PRIx64, ptr); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 138 | return out; |
| 139 | |
| 140 | } |
| 141 | |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame] | 142 | static int pandecode_dump_frame_count = 0; |
| 143 | |
Icecream95 | 01d60d3 | 2020-07-16 16:12:13 +1200 | [diff] [blame] | 144 | static bool force_stderr = false; |
| 145 | |
| 146 | void |
| 147 | pandecode_dump_file_open(void) |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 148 | { |
| 149 | if (pandecode_dump_stream) |
| 150 | return; |
| 151 | |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame] | 152 | /* This does a getenv every frame, so it is possible to use |
| 153 | * setenv to change the base at runtime. |
| 154 | */ |
| 155 | const char *dump_file_base = debug_get_option("PANDECODE_DUMP_FILE", "pandecode.dump"); |
Tomeu Vizoso | c409428 | 2020-04-30 10:48:02 +0200 | [diff] [blame] | 156 | if (force_stderr || !strcmp(dump_file_base, "stderr")) |
| 157 | pandecode_dump_stream = stderr; |
| 158 | else { |
| 159 | char buffer[1024]; |
| 160 | snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base, pandecode_dump_frame_count); |
| 161 | printf("pandecode: dump command stream to file %s\n", buffer); |
| 162 | pandecode_dump_stream = fopen(buffer, "w"); |
| 163 | if (!pandecode_dump_stream) |
| 164 | fprintf(stderr, |
| 165 | "pandecode: failed to open command stream log file %s\n", |
| 166 | buffer); |
| 167 | } |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void |
| 171 | pandecode_dump_file_close(void) |
| 172 | { |
Tomeu Vizoso | c409428 | 2020-04-30 10:48:02 +0200 | [diff] [blame] | 173 | if (pandecode_dump_stream && pandecode_dump_stream != stderr) { |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 174 | fclose(pandecode_dump_stream); |
| 175 | pandecode_dump_stream = NULL; |
| 176 | } |
| 177 | } |
| 178 | |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 179 | void |
Alyssa Rosenzweig | 070bc88 | 2020-02-18 08:05:11 -0500 | [diff] [blame] | 180 | pandecode_initialize(bool to_stderr) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 181 | { |
Icecream95 | 01d60d3 | 2020-07-16 16:12:13 +1200 | [diff] [blame] | 182 | force_stderr = to_stderr; |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 183 | mmap_table = _mesa_hash_table_u64_create(NULL); |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 184 | util_dynarray_init(&ro_mappings, NULL); |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame] | 188 | pandecode_next_frame(void) |
| 189 | { |
| 190 | pandecode_dump_file_close(); |
| 191 | pandecode_dump_frame_count++; |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 195 | pandecode_close(void) |
| 196 | { |
Alyssa Rosenzweig | deb78ee | 2020-05-15 12:57:38 -0400 | [diff] [blame] | 197 | _mesa_hash_table_u64_destroy(mmap_table, NULL); |
Icecream95 | ef67218 | 2020-06-22 22:49:53 +1200 | [diff] [blame] | 198 | util_dynarray_fini(&ro_mappings); |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 199 | pandecode_dump_file_close(); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 200 | } |