1 /*
2 * Copyright (C) 2017-2019 Lyude Paul
3 * Copyright (C) 2017-2019 Alyssa Rosenzweig
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #ifndef __PAN_DECODE_H__
27 #define __PAN_DECODE_H__
28
29 #include "genxml/gen_macros.h"
30 #include "util/rb_tree.h"
31
32 #include "wrap.h"
33
34 extern FILE *pandecode_dump_stream;
35
36 void pandecode_dump_file_open(void);
37
38 struct pandecode_mapped_memory {
39 struct rb_node node;
40 size_t length;
41 void *addr;
42 uint64_t gpu_va;
43 bool ro;
44 char name[32];
45 };
46
47 char *pointer_as_memory_reference(uint64_t ptr);
48
49 struct pandecode_mapped_memory *pandecode_find_mapped_gpu_mem_containing(uint64_t addr);
50
51 void pandecode_map_read_write(void);
52
53 void pandecode_dump_mappings(void);
54
55 static inline void *
__pandecode_fetch_gpu_mem(const struct pandecode_mapped_memory * mem,uint64_t gpu_va,size_t size,int line,const char * filename)56 __pandecode_fetch_gpu_mem(const struct pandecode_mapped_memory *mem,
57 uint64_t gpu_va, size_t size,
58 int line, const char *filename)
59 {
60 if (!mem)
61 mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
62
63 if (!mem) {
64 fprintf(stderr, "Access to unknown memory %" PRIx64 " in %s:%d\n",
65 gpu_va, filename, line);
66 assert(0);
67 }
68
69 assert(mem);
70 assert(size + (gpu_va - mem->gpu_va) <= mem->length);
71
72 return mem->addr + gpu_va - mem->gpu_va;
73 }
74
75 #define pandecode_fetch_gpu_mem(mem, gpu_va, size) \
76 __pandecode_fetch_gpu_mem(mem, gpu_va, size, __LINE__, __FILE__)
77
78 /* Returns a validated pointer to mapped GPU memory with the given pointer type,
79 * size automatically determined from the pointer type
80 */
81 #define PANDECODE_PTR(mem, gpu_va, type) \
82 ((type*)(__pandecode_fetch_gpu_mem(mem, gpu_va, sizeof(type), \
83 __LINE__, __FILE__)))
84
85 /* Usage: <variable type> PANDECODE_PTR_VAR(name, mem, gpu_va) */
86 #define PANDECODE_PTR_VAR(name, mem, gpu_va) \
87 name = __pandecode_fetch_gpu_mem(mem, gpu_va, sizeof(*name), \
88 __LINE__, __FILE__)
89
90 #ifdef PAN_ARCH
91 void GENX(pandecode_jc)(mali_ptr jc_gpu_va, unsigned gpu_id);
92 void GENX(pandecode_abort_on_fault)(mali_ptr jc_gpu_va);
93 #endif
94
95 static inline void
pan_hexdump(FILE * fp,const uint8_t * hex,size_t cnt,bool with_strings)96 pan_hexdump(FILE *fp, const uint8_t *hex, size_t cnt, bool with_strings)
97 {
98 for (unsigned i = 0; i < cnt; ++i) {
99 if ((i & 0xF) == 0)
100 fprintf(fp, "%06X ", i);
101
102 uint8_t v = hex[i];
103
104 if (v == 0 && (i & 0xF) == 0) {
105 /* Check if we're starting an aligned run of zeroes */
106 unsigned zero_count = 0;
107
108 for (unsigned j = i; j < cnt; ++j) {
109 if (hex[j] == 0)
110 zero_count++;
111 else
112 break;
113 }
114
115 if (zero_count >= 32) {
116 fprintf(fp, "*\n");
117 i += (zero_count & ~0xF) - 1;
118 continue;
119 }
120 }
121
122 fprintf(fp, "%02X ", hex[i]);
123 if ((i & 0xF) == 0xF && with_strings) {
124 fprintf(fp, " | ");
125 for (unsigned j = i & ~0xF; j <= i; ++j) {
126 uint8_t c = hex[j];
127 fputc((c < 32 || c > 128) ? '.' : c, fp);
128 }
129 }
130
131 if ((i & 0xF) == 0xF)
132 fprintf(fp, "\n");
133 }
134
135 fprintf(fp, "\n");
136 }
137
138 #endif /* __MMAP_TRACE_H__ */
139