1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* Copyright (c) 2012-2013 Wladimir J. van der Laan
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "viv_hook.h"
#include "elf_hook.h"
#include "flightrecorder.h"
/* hooking / logging functionality for Vivante GL driver
*/
#include "gc_abi.h"
#include "gc_hal_base.h"
#include "gc_hal.h"
#include "gc_hal_driver.h"
#ifdef GCABI_HAS_STATE_DELTAS
#include "gc_hal_kernel_buffer.h"
#else /* V2 */
#include "gc_hal_user_context.h"
#endif
#include "gc_hal_types.h"
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <pthread.h>
flightrec_t _fdr;
static int _galcore_handle = 0;
/* keep track of mapped video memory (not mapped through mmap) */
#define MAX_MAPPINGS 128
typedef struct
{
void *node;
void *logical;
size_t bytes;
} mapping_t;
static mapping_t mappings[MAX_MAPPINGS];
int my_open(const char* path, int flags, ...)
{
int ret=0;
if (flags & O_CREAT)
{
int mode=0;
va_list args;
va_start(args, flags);
mode = (mode_t) va_arg(args, int);
va_end(args);
ret = open(path, flags, mode);
} else {
ret = open(path, flags);
}
if(ret >= 0 && (!strcmp(path, "/dev/gal3d") || !strcmp(path, "/dev/galcore") || !strcmp(path, "/dev/graphics/galcore")))
{
_galcore_handle = ret;
printf("opened galcore: %i\n", ret);
}
return ret;
}
void *my_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
if(_fdr != NULL && fd == _galcore_handle)
{
flightrec_event_t evctx = fdr_new_event(_fdr, "MMAP_BEFORE");
fdr_event_add_parameter(evctx, "addr",(size_t)addr);
fdr_event_add_parameter(evctx, "length",(size_t)length);
fdr_event_add_parameter(evctx, "prot",(size_t)prot);
fdr_event_add_parameter(evctx, "flags",(size_t)flags);
fdr_event_add_parameter(evctx, "fd",(size_t)fd);
fdr_event_add_parameter(evctx, "offset",(size_t)offset);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_log_event(_fdr, evctx);
}
void *ret = mmap(addr, length, prot, flags, fd, offset);
if(_fdr != NULL && fd == _galcore_handle)
{
printf("new mapping %p %d\n", ret, (int)length);
flightrec_event_t evctx = fdr_new_event(_fdr, "MMAP_AFTER");
fdr_event_add_parameter(evctx, "addr",(size_t)addr);
fdr_event_add_parameter(evctx, "length",(size_t)length);
fdr_event_add_parameter(evctx, "prot",(size_t)prot);
fdr_event_add_parameter(evctx, "flags",(size_t)flags);
fdr_event_add_parameter(evctx, "fd",(size_t)fd);
fdr_event_add_parameter(evctx, "offset",(size_t)offset);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_event_add_parameter(evctx, "ret", (size_t)ret);
if(ret)
{
/* monitor new mmapped range */
/* Only small ranges, tracking the 128MB range that vivante maps is waaay
too slow (and in the current way things are done, needs all memory that
the device has to detect changes) */
/*
fdr_add_monitored_range(_fdr, ret, length);
*/
}
fdr_log_event(_fdr, evctx);
}
return ret;
}
int my_munmap(void *addr, size_t length)
{
if(_fdr != NULL)
{
flightrec_event_t evctx = fdr_new_event(_fdr, "MUNMAP_BEFORE");
fdr_event_add_parameter(evctx, "addr",(size_t)addr);
fdr_event_add_parameter(evctx, "length",(size_t)length);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_log_event(_fdr, evctx);
}
printf("removed mapping %p %d\n", addr, (int)length);
//fdr_remove_monitored_range(_fdr, addr, length);
int ret = munmap(addr, length);
if(_fdr != NULL)
{
flightrec_event_t evctx = fdr_new_event(_fdr, "MUNMAP_AFTER");
fdr_event_add_parameter(evctx, "addr",(size_t)addr);
fdr_event_add_parameter(evctx, "length",(size_t)length);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_event_add_parameter(evctx, "ret", (size_t)ret);
fdr_log_event(_fdr, evctx);
}
return ret;
}
/* Log contents of HAL_INTERFACE (input) child pointers.
* Assumes that the parent structure id was already added. */
static void log_interface_in(flightrec_event_t evctx, gcsHAL_INTERFACE *id)
{
switch(id->command)
{
case gcvHAL_COMMIT:
fdr_event_add_oneshot_range(evctx, id->u.Commit.commandBuffer, sizeof(struct _gcoCMDBUF));
//fdr_event_add_oneshot_range(evctx, id->u.Commit.commandBuffer->logical, id->u.Commit.commandBuffer->offset);
#ifndef GCABI_HAS_STATE_DELTAS
fdr_event_add_oneshot_range(evctx, id->u.Commit.contextBuffer, sizeof(struct _gcoCONTEXT));
if(id->u.Commit.contextBuffer->map) /* state map */
fdr_event_add_oneshot_range(evctx, id->u.Commit.contextBuffer->map, id->u.Commit.contextBuffer->stateCount*4);
if(id->u.Commit.contextBuffer->buffer) /* context command temp buffer */
fdr_event_add_oneshot_range(evctx, id->u.Commit.contextBuffer->buffer, id->u.Commit.contextBuffer->bufferSize);
#endif
break;
case gcvHAL_EVENT_COMMIT: { /* log entire event chain */
struct _gcsQUEUE *queue = id->u.Event.queue;
while(queue != NULL)
{
fdr_event_add_oneshot_range(evctx, queue, sizeof(struct _gcsQUEUE));
log_interface_in(evctx, &queue->iface);
queue = queue->next;
}
}
case gcvHAL_FREE_VIDEO_MEMORY:
for(int idx=0; idx<MAX_MAPPINGS; ++idx)
{
if(mappings[idx].node == id->u.FreeVideoMemory.node)
{
mappings[idx].node = 0;
mappings[idx].bytes = 0;
mappings[idx].logical = 0;
break;
}
}
break;
case gcvHAL_UNLOCK_VIDEO_MEMORY:
for(int idx=0; idx<MAX_MAPPINGS; ++idx)
{
if(mappings[idx].node == id->u.UnlockVideoMemory.node)
{
printf("remove_range %p %08x\n", mappings[idx].logical, mappings[idx].bytes);
fdr_remove_monitored_range(_fdr, mappings[idx].logical, mappings[idx].bytes);
mappings[idx].logical = 0;
break;
}
}
break;
case gcvHAL_FREE_CONTIGUOUS_MEMORY:
fdr_remove_monitored_range(_fdr, id->u.FreeContiguousMemory.logical, id->u.FreeContiguousMemory.bytes);
break;
default:
break;
}
}
/* Log contents of HAL_INTERFACE (output) child pointers.
* Assumes that the parent structure id was already added. */
static void log_interface_out(flightrec_event_t evctx, gcsHAL_INTERFACE *id)
{
switch(id->command)
{
case gcvHAL_ALLOCATE_LINEAR_VIDEO_MEMORY:
printf("vidalloc %p %08x\n", id->u.AllocateLinearVideoMemory.node, id->u.AllocateLinearVideoMemory.bytes);
for(int idx=0; idx<MAX_MAPPINGS; ++idx)
{
if(mappings[idx].node == NULL)
{
mappings[idx].node = id->u.AllocateLinearVideoMemory.node;
mappings[idx].bytes = id->u.AllocateLinearVideoMemory.bytes;
mappings[idx].logical = NULL;
break;
}
}
break;
case gcvHAL_LOCK_VIDEO_MEMORY:
for(int idx=0; idx<MAX_MAPPINGS; ++idx)
{
if(mappings[idx].node == id->u.LockVideoMemory.node)
{
mappings[idx].logical = id->u.LockVideoMemory.memory;
printf("add_range %p %08x\n", mappings[idx].logical, mappings[idx].bytes);
fdr_add_monitored_range(_fdr, mappings[idx].logical, mappings[idx].bytes);
break;
}
}
break;
case gcvHAL_ALLOCATE_CONTIGUOUS_MEMORY:
fdr_add_monitored_range(_fdr, id->u.AllocateContiguousMemory.logical, id->u.AllocateContiguousMemory.bytes);
break;
default:
break;
}
}
int my_ioctl(int d, int request, void *ptr_)
{
vivante_ioctl_data_t *ptr = (vivante_ioctl_data_t*) ptr_;
#if 0 /* UGH, this handle gets passed in some other way instead of open() for i.mx6 blobster */
if(d != _galcore_handle)
{
printf("unhandled ioctl %08x on fd %i\n", request, d);
return -1;
}
#endif
int ret=0;
if(_fdr != NULL)
{
if(request == IOCTL_GCHAL_INTERFACE)
{
flightrec_event_t evctx = fdr_new_event(_fdr, "IOCTL_BEFORE");
fdr_event_add_parameter(evctx, "d", (size_t)d);
fdr_event_add_parameter(evctx, "request",(size_t)request);
fdr_event_add_parameter(evctx, "ptr",(size_t)ptr);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_event_add_oneshot_range(evctx, ptr, sizeof(vivante_ioctl_data_t));
fdr_event_add_oneshot_range(evctx, ptr->in_buf, ptr->in_buf_size);
log_interface_in(evctx, ptr->in_buf);
fdr_log_event(_fdr, evctx);
} else {
printf("unhandled ioctl %08x on fd %i\n", request, d);
return -1;
}
}
ret = ioctl(d, request, ptr);
if(_fdr != NULL)
{
if(request == IOCTL_GCHAL_INTERFACE)
{
flightrec_event_t evctx = fdr_new_event(_fdr, "IOCTL_AFTER");
fdr_event_add_parameter(evctx, "d", (size_t)d);
fdr_event_add_parameter(evctx, "request",(size_t)request);
fdr_event_add_parameter(evctx, "ptr",(size_t)ptr);
fdr_event_add_parameter(evctx, "thread", (size_t)pthread_self());
fdr_event_add_parameter(evctx, "ret", (size_t)ret);
fdr_event_add_oneshot_range(evctx, ptr, sizeof(vivante_ioctl_data_t));
fdr_event_add_oneshot_range(evctx, ptr->out_buf, ptr->out_buf_size);
log_interface_out(evctx, ptr->out_buf);
fdr_log_event(_fdr, evctx);
}
}
return ret;
}
void hook_start_logging(const char *filename)
{
if(_fdr == NULL)
{
printf("viv_hook: logging to %s\n", filename);
_fdr = fdr_open(filename);
}
}
void the_hook(const char *filename)
{
char *mali_path = NULL; // path to libMali.so
void *mali_base = NULL;
char *gal_names[] = {"libGAL.so", "libGAL-fb.so", 0};
hook_start_logging(filename);
int i=0;
while (gal_names[i] != 0)
{
if(parse_maps(gal_names[i], &mali_path, &mali_base) == 0)
break;
i++;
}
if (gal_names[i] == 0)
{
fprintf(stderr, "Could not find libGAL library in process map\n");
return;
}
printf("Hooking %s at %p\n", mali_path, mali_base);
if(elf_hook(mali_path, mali_base, "open", &my_open) == NULL)
{
fprintf(stderr, "Hooking open failed\n");
return;
}
if(elf_hook(mali_path, mali_base, "ioctl", &my_ioctl) == NULL)
{
fprintf(stderr, "Hooking ioctl failed\n");
return;
}
if(elf_hook(mali_path, mali_base, "mmap", &my_mmap) == NULL)
{
fprintf(stderr, "Hooking mmap failed\n");
return;
}
if(elf_hook(mali_path, mali_base, "munmap", &my_munmap) == NULL)
{
fprintf(stderr, "Hooking munmap failed\n");
return;
}
printf("Hook succeeded!\n");
}
void close_hook()
{
fdr_close(_fdr);
_fdr = 0;
}
|