-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfxboot_malloc.c
523 lines (402 loc) · 13.6 KB
/
gfxboot_malloc.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include <gfxboot/gfxboot.h>
static int gfx_malloc_check_basic(void);
static int gfx_malloc_check_xref(void);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// malloc functions
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_malloc_init()
{
malloc_head_t *head = &gfxboot_data->vm.mem;
if(!head->ptr || head->size < 0x1000) return 1;
head->first_chunk = head->ptr;
head->first_free = head->ptr;
*(malloc_chunk_t *) head->first_chunk = (malloc_chunk_t) { .prev = head->size, .next = head->size, .id = 0 };
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_malloc_dump(dump_style_t style)
{
malloc_head_t *head = &gfxboot_data->vm.mem;
malloc_chunk_t *chunk;
unsigned idx;
void *mem;
void *mem_start = head->first_chunk;
void *mem_end = mem_start + head->size;
gfxboot_log("=== memory dump ===\n");
if(!mem_start) {
gfxboot_log(" no memory\n");
return;
}
if(!style.no_check) gfx_malloc_check(mc_basic + mc_xref);
for(idx = 0, mem = mem_start; mem >= mem_start && mem < mem_end; mem += chunk->next, idx++) {
if(style.max && idx >= style.max) break;
chunk = (malloc_chunk_t *) mem;
gfxboot_log("%4u%c ", idx, mem == head->first_free ? '*' : ':');
if(gfxboot_data->vm.debug.show_pointer) {
gfxboot_log("%p", chunk->data);
}
else {
gfxboot_log("0x%08x", (int) ((void *) (chunk->data) - mem_start));
}
gfxboot_log("[%8d]", (int) (chunk->next - sizeof (malloc_chunk_t)));
if(style.dump) gfxboot_log(" [%8d/%8d]", chunk->prev, chunk->next);
if(chunk->id && (style.inspect || style.dump)) {
gfxboot_log(" ");
gfx_obj_dump(chunk->id, (dump_style_t) { .inspect = 1, .no_nl = 1 });
}
gfxboot_log("\n");
if(chunk->next <= sizeof (malloc_chunk_t)) break;
}
if(!style.max && mem != mem_end) {
gfxboot_log(" -- malloc chain corrupt --\n");
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void *gfx_malloc(uint32_t size, obj_id_t id)
{
malloc_head_t *head = &gfxboot_data->vm.mem;
void *mem_start = head->first_chunk;
void *mem_end = mem_start + head->size;
malloc_chunk_t *chunk;
// can never be 0
if(id == 0) return 0;
// out of memory
if(size > gfxboot_data->vm.mem.size) return 0;
// size 0: return a valid pointer that we won't try to free
if(size == 0) return mem_end;
size += sizeof (malloc_chunk_t); // include header size
size = (size + 3) & ~3U; // align to 4 byte
unsigned first_free_seen = 0;
for(void *mem = head->first_free; mem >= mem_start && mem < mem_end; mem += chunk->next) {
chunk = (malloc_chunk_t *) mem;
if(chunk->id == 0) {
if(!first_free_seen) head->first_free = mem;
first_free_seen++;
if(chunk->next >= size) {
chunk->id = id;
gfx_memset(mem + sizeof (malloc_chunk_t), 0, size - sizeof (malloc_chunk_t));
// big enough to split off an empty chunk
if(chunk->next > size + sizeof (malloc_chunk_t)) {
void *mem_next = mem + chunk->next;
malloc_chunk_t *chunk_next = mem_next == mem_end ? mem_start : mem_next;
uint32_t n = chunk->next - size;
chunk->next = size;
chunk = (malloc_chunk_t *) (mem + size);
if(first_free_seen == 1) head->first_free = mem + size;
chunk->id = 0;
chunk->next = n;
chunk->prev = size;
chunk_next->prev = n;
}
return mem + sizeof (malloc_chunk_t);
}
}
}
// out of memory
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_free(void *ptr)
{
malloc_head_t *head = &gfxboot_data->vm.mem;
void *mem_start = head->first_chunk;
void *mem_end = mem_start + head->size;
if(!ptr || ptr < mem_start + sizeof (malloc_chunk_t) || ptr >= mem_end) return;
// point to chunk header
void *mem = ptr - sizeof (malloc_chunk_t);
malloc_chunk_t *chunk = mem;
uint32_t next = chunk->next;
void *mem_next = mem != mem_end ? mem + next : mem_start;
malloc_chunk_t *chunk_next = mem_next;
if(mem != mem_start) {
uint32_t prev = chunk->prev;
void *mem_prev = mem - prev;
malloc_chunk_t *chunk_prev = mem_prev;
if(mem_prev < mem_start || mem_prev >= mem || chunk_prev->next != prev) goto error;
// join with preceeding free chunk
if(chunk_prev->id == 0) {
chunk_prev->next += next;
mem = mem_prev;
chunk = mem;
next = chunk->next;
chunk_next->prev = next;
}
}
if(mem_next <= mem || mem_next > mem_end || chunk_next->prev != next) goto error;
if(mem_next != mem_end) {
// join with following free chunk
if(chunk_next->id == 0) {
chunk->next += chunk_next->next;
mem_next = mem + chunk->next != mem_end ? mem + chunk->next : mem_start;
chunk_next = mem_next;
chunk_next->prev = chunk->next;
}
}
chunk->id = 0;
if(mem < head->first_free) head->first_free = mem;
if(gfxboot_data->vm.debug.trace.memcheck && gfx_malloc_check(mc_basic)) {
gfxboot_log("-- error in gfx_free\n");
gfx_malloc_dump((dump_style_t) { .dump = 1, .no_check = 1 });
}
return;
error:
gfxboot_log("-- free(0x%x) failed\n", (int) (ptr - mem_start));
GFX_ERROR(err_memory_corruption);
if(gfxboot_data->vm.debug.trace.memcheck) {
gfx_malloc_dump((dump_style_t) { .dump = 1, .no_check = 1 });
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_malloc_check_basic()
{
malloc_head_t *head = &gfxboot_data->vm.mem;
void *mem, *mem_start, *mem_prev, *mem_next, *mem_end;
malloc_chunk_t *chunk, *chunk_start, *chunk_prev, *chunk_next;
unsigned idx;
mem_start = head->first_chunk;
mem_end = mem_start + head->size;
chunk_start = mem_start;
if(!mem_start) return 0;
unsigned free_chunk = 0, first_free_ok = 0;
for(idx = 0, mem = mem_prev = mem_start; mem >= mem_start && mem < mem_end; mem_prev = mem, mem = mem_next, idx++) {
chunk = (malloc_chunk_t *) mem;
chunk_prev = (malloc_chunk_t *) mem_prev;
if(mem == head->first_free && !free_chunk) first_free_ok = 1;
if(chunk->id == 0) free_chunk = 1;
mem_next = mem + chunk->next;
if(chunk->next < sizeof (malloc_chunk_t) || mem_next > mem_end || mem_next <= mem) break;
chunk_next = (malloc_chunk_t *) mem_next;
if(chunk != chunk_prev && chunk->prev != chunk_prev->next) break;
if(mem_next != mem_end && chunk->next != chunk_next->prev) break;
if(chunk->id && gfxboot_data->vm.olist.ptr) {
obj_t *obj_ptr = gfx_obj_ptr(chunk->id);
if(!obj_ptr) goto error;
if(!obj_ptr->flags.data_is_ptr) goto error;
if(obj_ptr->flags.nofree) continue;
void *data_start = obj_ptr->data.ptr;
void *data_end = data_start + obj_ptr->data.size;
if(
data_start < mem + sizeof (malloc_chunk_t) ||
data_start > mem_next ||
data_end < mem + sizeof (malloc_chunk_t) ||
data_end > mem_next
) {
gfxboot_log("-- object #%u not inside malloc chunk\n", OBJ_ID2IDX(chunk->id));
goto error;
}
}
}
if(!first_free_ok) {
gfxboot_log("-- first free ptr wrong\n");
goto error;
}
if(mem != mem_end) goto error;
if(chunk->next != chunk_start->prev) goto error;
return 0;
error:
gfxboot_log("-- malloc chain corrupt (entry %d)\n", idx);
GFX_ERROR(err_memory_corruption);
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_malloc_check_xref()
{
malloc_head_t *head = &gfxboot_data->vm.mem;
olist_t *obj_list = gfxboot_data->vm.olist.ptr;
int err = 0;
unsigned obj_idx;
if(!obj_list) return 0;
void *mem_end = head->first_chunk + head->size;
for(obj_idx = 0; obj_idx < obj_list->max; obj_idx++) {
obj_t *obj_ptr = obj_list->ptr + obj_idx;
if(obj_ptr->base_type == OTYPE_NONE) continue;
if(!obj_ptr->flags.data_is_ptr) continue;
if(obj_ptr->flags.nofree) {
obj_id_t ref_id = obj_ptr->data.ref_id;
if(ref_id) {
obj_t *ptr = gfx_obj_ptr(ref_id);
if(!ptr || !ptr->flags.has_ref) {
gfxboot_log("-- object #%u does not know it is referenced by #%u\n", OBJ_ID2IDX(ref_id), obj_idx);
err = 1;
}
}
continue;
}
// special case: size 0 objects point to end of malloc area
if(obj_ptr->data.size == 0 && obj_ptr->data.ptr == mem_end) continue;
malloc_chunk_t *chunk = gfx_malloc_find_chunk(obj_ptr->data.ptr);
if(!chunk) {
gfxboot_log("-- object #%u is missing its malloc chunk\n", obj_idx);
err = 1;
continue;
}
void *chunk_ptr = chunk;
void *d_end = obj_ptr->data.ptr + obj_ptr->data.size;
if(
!chunk->id ||
d_end < chunk_ptr + sizeof (malloc_chunk_t) ||
d_end > chunk_ptr + chunk->next
) {
gfxboot_log("-- object #%u not inside its malloc chunk\n", obj_idx);
err = 1;
continue;
}
obj_id_t id = obj_ptr->data.ref_id;
if(!id) id = OBJ_ID(obj_idx, obj_ptr->gen);
if(chunk->id != id) {
gfxboot_log(
"-- malloc chunk id mismatch (malloc id #%u, obj id #%u)\n",
OBJ_ID2IDX(chunk->id), OBJ_ID2IDX(id)
);
err = 1;
}
}
if(err) {
gfxboot_log("-- object store corrupt\n");
GFX_ERROR(err_memory_corruption);
}
return err;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_malloc_check(malloc_check_t what)
{
int result = 0;
if((what & mc_basic)) result |= gfx_malloc_check_basic();
if((what & mc_xref)) result |= gfx_malloc_check_xref();
return result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
malloc_chunk_t *gfx_malloc_find_chunk(void *ptr)
{
malloc_head_t *head = &gfxboot_data->vm.mem;
void *mem_start, *mem_next, *mem_end;
malloc_chunk_t *chunk;
mem_start = head->first_chunk;
mem_end = mem_start + head->size;
if(ptr < mem_start || ptr >= mem_end) return 0;
for(void *mem = mem_start; mem < mem_end; mem = mem_next) {
chunk = (malloc_chunk_t *) mem;
mem_next = mem + chunk->next;
if(ptr >= mem && ptr < mem_next) {
return (unsigned) (ptr - mem) < sizeof (malloc_chunk_t) ? 0 : mem;
}
}
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_defrag(unsigned max)
{
malloc_head_t *head = &gfxboot_data->vm.mem;
void *mem_start = head->first_chunk;
void *mem_end = head->first_chunk + head->size;
void *mem_defrag_start = 0;
void *mem_defrag_end = 0;
malloc_chunk_t *chunk, *chunk_prev, *chunk_next;
obj_id_t ref_id = 0;
int err = 0;
unsigned cnt = 0;
uint32_t diff = 0;
int olist_adjusted = 0;
for(void *mem = head->first_free; mem >= mem_start && mem < mem_end; mem += chunk->next) {
chunk = (malloc_chunk_t *) mem;
if(mem_defrag_start) cnt++;
if(chunk->id == 0) {
if(mem_defrag_start) {
mem_defrag_end = mem;
break;
}
else {
mem_defrag_start = mem;
diff = chunk->next;
}
continue;
}
if(!mem_defrag_start) continue;
obj_t *ptr = gfx_obj_ptr(chunk->id);
if(!ptr) {
err = 1;
break;
}
// is olist?
if(chunk->id == OBJ_ID(0, 1)) olist_adjusted = 1;
if(ptr->flags.data_is_ptr && !ptr->flags.nofree) {
ptr->data.ptr -= diff;
}
if(ptr->flags.has_ref) {
if(ref_id) {
mem_defrag_end = mem;
break;
}
ref_id = chunk->id;
}
if(cnt >= max) {
mem_defrag_end = mem;
break;
}
}
// if olist was adjusted, adjust also global pointer
if(olist_adjusted) {
gfxboot_data->vm.olist.ptr -= diff;
}
if(!err && cnt && mem_defrag_start) {
if(!mem_defrag_end) mem_defrag_end = mem_end;
gfxboot_log(
"-- defrag: %u chunks, diff %u, 0x%x - 0x%x, ref_id #%u\n",
cnt,
diff,
(unsigned) (mem_defrag_start - mem_start),
(unsigned) (mem_defrag_end - mem_start),
OBJ_ID2IDX(ref_id)
);
chunk = mem_defrag_start;
chunk_next = mem_defrag_start + diff;
// adjust chunks at beginning
chunk_prev = (mem_defrag_start != mem_start ? mem_defrag_start : mem_end) - chunk->prev;
chunk_next->prev = chunk_prev->next;
// remember last chunk size
malloc_chunk_t *chunk_end = mem_defrag_end != mem_end ? mem_defrag_end : mem_start;
uint32_t prev = chunk_end->prev;
mem_defrag_end -= diff;
// adjust pointer to first free block
head->first_free = mem_defrag_end;
// copy
gfx_memcpy(mem_defrag_start, mem_defrag_start + diff, (unsigned) (mem_defrag_end - mem_defrag_start));
// adjust chunks at end
chunk = mem_defrag_end;
chunk->id = 0;
chunk->next = diff;
chunk->prev = prev;
chunk_end->prev = diff;
void *mem_next = mem_defrag_end + diff;
chunk_next = mem_next;
// join free end blocks
if(mem_next != mem_end && chunk_next->id == 0) {
chunk->next += chunk_next->next;
mem_next += chunk_next->next;
if(mem_next == mem_end) mem_next = mem_start;
chunk_next = mem_next;
chunk_next->prev = chunk->next;
}
}
// adjust everything pointing to ref_id
if(ref_id) {
olist_t *ol = gfxboot_data->vm.olist.ptr;
for(unsigned u = 0; u < ol->max; u++) {
obj_t *ptr = ol->ptr + u;
if(
ptr->base_type != OTYPE_NONE &&
ptr->flags.data_is_ptr &&
ptr->data.ref_id == ref_id
) {
ptr->data.ptr -= diff;
}
}
}
if(gfxboot_data->vm.debug.trace.memcheck && gfx_malloc_check(mc_basic + mc_xref)) {
GFX_ERROR(err_memory_corruption);
gfxboot_log("-- error in gfx_defrag\n");
gfx_malloc_dump((dump_style_t) { .dump = 1, .no_check = 1 });
}
}