aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/syslinux/movebits.c
blob: 7a05f3c1d807801b2a0b890a177a6f2aaafa46d9 (plain)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   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, sublicense, 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 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
 *   NONINFRINGEMENT. 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.
 *
 * ----------------------------------------------------------------------- */

/*
 * movebits.c
 *
 * Utility function to take a list of memory areas to shuffle and
 * convert it to a set of shuffle operations.
 *
 * Note: a lot of the functions in this file deal with "parent pointers",
 * which are pointers to a pointer to a list, or part of the list.
 * They can be pointers to a variable holding the list root pointer,
 * or pointers to a next field of a previous entry.
 */

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <inttypes.h>
#include <setjmp.h>
#include <minmax.h>
#include <stdbool.h>

#include <syslinux/movebits.h>
#include <dprintf.h>

static jmp_buf new_movelist_bail;

static struct syslinux_movelist *new_movelist(addr_t dst, addr_t src,
					      addr_t len)
{
    struct syslinux_movelist *ml = malloc(sizeof(struct syslinux_movelist));

    if (!ml)
	longjmp(new_movelist_bail, 1);

    ml->dst = dst;
    ml->src = src;
    ml->len = len;
    ml->next = NULL;

    return ml;
}

static struct syslinux_movelist *dup_movelist(struct syslinux_movelist *src)
{
    struct syslinux_movelist *dst = NULL, **dstp = &dst, *ml;

    while (src) {
	ml = new_movelist(src->dst, src->src, src->len);
	*dstp = ml;
	dstp = &ml->next;
	src = src->next;
    }

    return dst;
}

static void
add_freelist(struct syslinux_memmap **mmap, addr_t start,
	     addr_t len, enum syslinux_memmap_types type)
{
    if (syslinux_add_memmap(mmap, start, len, type))
	longjmp(new_movelist_bail, 1);
}

/*
 * Take a chunk, entirely confined in **parentptr, and split it off so that
 * it has its own structure.
 */
static struct syslinux_movelist **split_movelist(addr_t start, addr_t len,
						 struct syslinux_movelist
						 **parentptr)
{
    struct syslinux_movelist *m, *ml = *parentptr;

    assert(start >= ml->src);
    assert(start < ml->src + ml->len);

    /* Split off the beginning */
    if (start > ml->src) {
	addr_t l = start - ml->src;

	m = new_movelist(ml->dst + l, start, ml->len - l);
	m->next = ml->next;
	ml->len = l;
	ml->next = m;

	parentptr = &ml->next;
	ml = m;			/* Continue processing the new node */
    }

    /* Split off the end */
    if (ml->len > len) {
	addr_t l = ml->len - len;

	m = new_movelist(ml->dst + len, ml->src + len, l);
	m->next = ml->next;
	ml->len = len;
	ml->next = m;
    }

    return parentptr;
}

static void delete_movelist(struct syslinux_movelist **parentptr)
{
    struct syslinux_movelist *o = *parentptr;
    *parentptr = o->next;
    free(o);
}

static void free_movelist(struct syslinux_movelist **parentptr)
{
    while (*parentptr)
	delete_movelist(parentptr);
}

/*
 * Scan the freelist looking for a particular chunk of memory
 */
static const struct syslinux_memmap *is_free_zone(const struct syslinux_memmap
						  *list, addr_t start,
						  addr_t len)
{
    dprintf("f: 0x%08x bytes at 0x%08x\n", len, start);

    addr_t last, llast;

    last = start + len - 1;

    while (list->type != SMT_END) {
	llast = list->next->start - 1;
	if (list->start <= start) {
	    if (llast >= last) {
		/* Chunk has a single, well-defined type */
		if (list->type == SMT_FREE) {
		    dprintf("F: 0x%08x bytes at 0x%08x\n",
			    list->next->start, list->start);
		    return list;	/* It's free */
		}
		return NULL;	/* Not free */
	    } else if (llast >= start) {
		return NULL;	/* Crosses region boundary */
	    }
	}
	list = list->next;
    }

    return NULL;		/* Internal error? */
}

/*
 * Scan the freelist looking for the smallest chunk of memory which
 * can fit X bytes; returns the length of the block on success.
 */
static addr_t free_area(const struct syslinux_memmap *mmap,
			addr_t len, addr_t * start)
{
    const struct syslinux_memmap *best = NULL;
    const struct syslinux_memmap *s;
    addr_t slen, best_len = -1;

    for (s = mmap; s->type != SMT_END; s = s->next) {
	if (s->type != SMT_FREE)
	    continue;
	slen = s->next->start - s->start;
	if (slen >= len) {
	    if (!best || best_len > slen) {
		best = s;
		best_len = slen;
	    }
	}
    }

    if (best) {
	*start = best->start;
	return best_len;
    } else {
	return 0;
    }
}

/*
 * Remove a chunk from the freelist
 */
static void
allocate_from(struct syslinux_memmap **mmap, addr_t start, addr_t len)
{
    syslinux_add_memmap(mmap, start, len, SMT_ALLOC);
}

/*
 * Find chunks of a movelist which are one-to-many (one source, multiple
 * destinations.)  Those chunks can get turned into post-shuffle copies,
 * to avoid confusing the shuffler.
 */
static void shuffle_dealias(struct syslinux_movelist **fraglist,
			    struct syslinux_movelist **postcopy)
{
    struct syslinux_movelist *mp, **mpp, *mx, *np;
    addr_t ps, pe, xs, xe, delta;
    bool advance;

    dprintf("Before alias resolution:\n");
    syslinux_dump_movelist(*fraglist);

    *postcopy = NULL;

    /*
     * Note: as written, this is an O(n^2) algorithm; by producing a list
     * sorted by destination address we could reduce it to O(n log n).
     */
    mpp = fraglist;
    while ((mp = *mpp)) {
	dprintf("mp -> (%#x,%#x,%#x)\n", mp->dst, mp->src, mp->len);
	ps = mp->src;
	pe = mp->src + mp->len - 1;
	for (mx = *fraglist; mx != mp; mx = mx->next) {
	    dprintf("mx -> (%#x,%#x,%#x)\n", mx->dst, mx->src, mx->len);
	    /*
	     * If there is any overlap between mx and mp, mp should be
	     * modified and possibly split.
	     */
	    xs = mx->src;
	    xe = mx->src + mx->len - 1;

	    dprintf("?: %#x..%#x (inside %#x..%#x)\n", ps, pe, xs, xe);

	    if (pe <= xs || ps >= xe)
		continue;	/* No overlap */

	    advance = false;
	    *mpp = mp->next;	/* Remove from list */

	    if (pe > xe) {
		delta = pe - xe;
		np = new_movelist(mp->dst + mp->len - delta,
				  mp->src + mp->len - delta, delta);
		mp->len -= delta;
		pe = xe;
		np->next = *mpp;
		*mpp = np;
		advance = true;
	    }
	    if (ps < xs) {
		delta = xs - ps;
		np = new_movelist(mp->dst, ps, delta);
		mp->src += delta;
		ps = mp->src;
		mp->dst += delta;
		mp->len -= delta;
		np->next = *mpp;
		*mpp = np;
		advance = true;
	    }

	    assert(ps >= xs && pe <= xe);

	    dprintf("Overlap: %#x..%#x (inside %#x..%#x)\n", ps, pe, xs, xe);

	    mp->src = mx->dst + (ps - xs);
	    mp->next = *postcopy;
	    *postcopy = mp;

	    mp = *mpp;

	    if (!advance)
		goto restart;
	}

	mpp = &mp->next;
restart:
	;
    }

    dprintf("After alias resolution:\n");
    syslinux_dump_movelist(*fraglist);
    dprintf("Post-shuffle copies:\n");
    syslinux_dump_movelist(*postcopy);
}

/*
 * The code to actually emit moving of a chunk into its final place.
 */
static void
move_chunk(struct syslinux_movelist ***moves,
	   struct syslinux_memmap **mmap,
	   struct syslinux_movelist **fp, addr_t copylen)
{
    addr_t copydst, copysrc;
    addr_t freebase, freelen;
    addr_t needlen;
    int reverse;
    struct syslinux_movelist *f = *fp, *mv;

    if (f->src < f->dst && (f->dst - f->src) < f->len) {
	/* "Shift up" type overlap */
	needlen = f->dst - f->src;
	reverse = 1;
    } else if (f->src > f->dst && (f->src - f->dst) < f->len) {
	/* "Shift down" type overlap */
	needlen = f->src - f->dst;
	reverse = 0;
    } else {
	needlen = f->len;
	reverse = 0;
    }

    copydst = f->dst;
    copysrc = f->src;

    dprintf("Q: copylen = 0x%08x, needlen = 0x%08x\n", copylen, needlen);

    if (copylen < needlen) {
	if (reverse) {
	    copydst += (f->len - copylen);
	    copysrc += (f->len - copylen);
	}

	dprintf("X: 0x%08x bytes at 0x%08x -> 0x%08x\n",
		copylen, copysrc, copydst);

	/* Didn't get all we wanted, so we have to split the chunk */
	fp = split_movelist(copysrc, copylen, fp);	/* Is this right? */
	f = *fp;
    }

    mv = new_movelist(f->dst, f->src, f->len);
    dprintf("A: 0x%08x bytes at 0x%08x -> 0x%08x\n", mv->len, mv->src, mv->dst);
    **moves = mv;
    *moves = &mv->next;

    /* Figure out what memory we just freed up */
    if (f->dst > f->src) {
	freebase = f->src;
	freelen = min(f->len, f->dst - f->src);
    } else if (f->src >= f->dst + f->len) {
	freebase = f->src;
	freelen = f->len;
    } else {
	freelen = f->src - f->dst;
	freebase = f->dst + f->len;
    }

    dprintf("F: 0x%08x bytes at 0x%08x\n", freelen, freebase);

    add_freelist(mmap, freebase, freelen, SMT_FREE);

    delete_movelist(fp);
}

/*
 * moves is computed from "frags" and "freemem".  "space" lists
 * free memory areas at our disposal, and is (src, cnt) only.
 */
int
syslinux_compute_movelist(struct syslinux_movelist **moves,
			  struct syslinux_movelist *ifrags,
			  struct syslinux_memmap *memmap)
{
    struct syslinux_memmap *mmap = NULL;
    const struct syslinux_memmap *mm, *ep;
    struct syslinux_movelist *frags = NULL;
    struct syslinux_movelist *postcopy = NULL;
    struct syslinux_movelist *mv;
    struct syslinux_movelist *f, **fp;
    struct syslinux_movelist *o, **op;
    addr_t needbase, needlen, copysrc, copydst, copylen;
    addr_t avail;
    addr_t fstart, flen;
    addr_t cbyte;
    addr_t ep_len;
    int rv = -1;
    int reverse;

    dprintf("entering syslinux_compute_movelist()...\n");

    if (setjmp(new_movelist_bail)) {
nomem:
	dprintf("Out of working memory!\n");
	goto bail;
    }

    *moves = NULL;

    /* Create our memory map.  Anything that is SMT_FREE or SMT_ZERO is
       fair game, but mark anything used by source material as SMT_ALLOC. */
    mmap = syslinux_init_memmap();
    if (!mmap)
	goto nomem;

    frags = dup_movelist(ifrags);

    /* Process one-to-many conditions */
    shuffle_dealias(&frags, &postcopy);

    for (mm = memmap; mm->type != SMT_END; mm = mm->next)
	add_freelist(&mmap, mm->start, mm->next->start - mm->start,
		     mm->type == SMT_ZERO ? SMT_FREE : mm->type);

    for (f = frags; f; f = f->next)
	add_freelist(&mmap, f->src, f->len, SMT_ALLOC);

    /* As long as there are unprocessed fragments in the chain... */
    while ((fp = &frags, f = *fp)) {

	dprintf("Current free list:\n");
	syslinux_dump_memmap(mmap);
	dprintf("Current frag list:\n");
	syslinux_dump_movelist(frags);

	/* Scan for fragments which can be discarded without action. */
	if (f->src == f->dst) {
	    delete_movelist(fp);
	    continue;
	}
	op = &f->next;
	while ((o = *op)) {
	    if (o->src == o->dst)
		delete_movelist(op);
	    else
		op = &o->next;
	}

	/* Scan for fragments which can be immediately moved
	   to their final destination, if so handle them now */
	for (op = fp; (o = *op); op = &o->next) {
	    if (o->src < o->dst && (o->dst - o->src) < o->len) {
		/* "Shift up" type overlap */
		needlen = o->dst - o->src;
		needbase = o->dst + (o->len - needlen);
		reverse = 1;
		cbyte = o->dst + o->len - 1;
	    } else if (o->src > o->dst && (o->src - o->dst) < o->len) {
		/* "Shift down" type overlap */
		needlen = o->src - o->dst;
		needbase = o->dst;
		reverse = 0;
		cbyte = o->dst;	/* "Critical byte" */
	    } else {
		needlen = o->len;
		needbase = o->dst;
		reverse = 0;
		cbyte = o->dst;	/* "Critical byte" */
	    }

	    if (is_free_zone(mmap, needbase, needlen)) {
		fp = op, f = o;
		dprintf("!: 0x%08x bytes at 0x%08x -> 0x%08x\n",
			f->len, f->src, f->dst);
		copysrc = f->src;
		copylen = needlen;
		allocate_from(&mmap, needbase, copylen);
		goto move_chunk;
	    }
	}

	/* Ok, bother.  Need to do real work at least with one chunk. */

	dprintf("@: 0x%08x bytes at 0x%08x -> 0x%08x\n",
		f->len, f->src, f->dst);

	/* See if we can move this chunk into place by claiming
	   the destination, or in the case of partial overlap, the
	   missing portion. */

	if (f->src < f->dst && (f->dst - f->src) < f->len) {
	    /* "Shift up" type overlap */
	    needlen = f->dst - f->src;
	    needbase = f->dst + (f->len - needlen);
	    reverse = 1;
	    cbyte = f->dst + f->len - 1;
	} else if (f->src > f->dst && (f->src - f->dst) < f->len) {
	    /* "Shift down" type overlap */
	    needlen = f->src - f->dst;
	    needbase = f->dst;
	    reverse = 0;
	    cbyte = f->dst;	/* "Critical byte" */
	} else {
	    needlen = f->len;
	    needbase = f->dst;
	    reverse = 0;
	    cbyte = f->dst;
	}

	dprintf("need: base = 0x%08x, len = 0x%08x, "
		"reverse = %d, cbyte = 0x%08x\n",
		needbase, needlen, reverse, cbyte);

	ep = is_free_zone(mmap, cbyte, 1);
	if (ep) {
	    ep_len = ep->next->start - ep->start;
	    if (reverse)
		avail = needbase + needlen - ep->start;
	    else
		avail = ep_len - (needbase - ep->start);
	} else {
	    avail = 0;
	}

	if (avail) {
	    /* We can move at least part of this chunk into place without
	       further ado */
	    dprintf("space: start 0x%08x, len 0x%08x, free 0x%08x\n",
		    ep->start, ep_len, avail);
	    copylen = min(needlen, avail);

	    if (reverse)
		allocate_from(&mmap, needbase + needlen - copylen, copylen);
	    else
		allocate_from(&mmap, needbase, copylen);

	    goto move_chunk;
	}

	/* At this point, we need to evict something out of our space.
	   Find the object occupying the critical byte of our target space,
	   and move it out (the whole object if we can, otherwise a subset.)
	   Then move a chunk of ourselves into place. */
	for (op = &f->next, o = *op; o; op = &o->next, o = *op) {

	    dprintf("O: 0x%08x bytes at 0x%08x -> 0x%08x\n",
		    o->len, o->src, o->dst);

	    if (!(o->src <= cbyte && o->src + o->len > cbyte))
		continue;	/* Not what we're looking for... */

	    /* Find somewhere to put it... */

	    if (is_free_zone(mmap, o->dst, o->len)) {
		/* Score!  We can move it into place directly... */
		copydst = o->dst;
		copysrc = o->src;
		copylen = o->len;
	    } else if (free_area(mmap, o->len, &fstart)) {
		/* We can move the whole chunk */
		copydst = fstart;
		copysrc = o->src;
		copylen = o->len;
	    } else {
		/* Well, copy as much as we can... */
		if (syslinux_memmap_largest(mmap, SMT_FREE, &fstart, &flen)) {
		    dprintf("No free memory at all!\n");
		    goto bail;	/* Stuck! */
		}

		/* Make sure we include the critical byte */
		copydst = fstart;
		if (reverse) {
		    copysrc = max(o->src, cbyte + 1 - flen);
		    copylen = cbyte + 1 - copysrc;
		} else {
		    copysrc = cbyte;
		    copylen = min(flen, o->len - (cbyte - o->src));
		}
	    }
	    allocate_from(&mmap, copydst, copylen);

	    if (copylen < o->len) {
		op = split_movelist(copysrc, copylen, op);
		o = *op;
	    }

	    mv = new_movelist(copydst, copysrc, copylen);
	    dprintf("C: 0x%08x bytes at 0x%08x -> 0x%08x\n",
		    mv->len, mv->src, mv->dst);
	    *moves = mv;
	    moves = &mv->next;

	    o->src = copydst;

	    if (copylen > needlen) {
		/* We don't need all the memory we freed up.  Mark it free. */
		if (copysrc < needbase) {
		    add_freelist(&mmap, copysrc, needbase - copysrc, SMT_FREE);
		    copylen -= (needbase - copysrc);
		}
		if (copylen > needlen) {
		    add_freelist(&mmap, copysrc + needlen, copylen - needlen,
				 SMT_FREE);
		    copylen = needlen;
		}
	    }
	    reverse = 0;
	    goto move_chunk;
	}
	dprintf("Cannot find the chunk containing the critical byte\n");
	goto bail;		/* Stuck! */

move_chunk:
	move_chunk(&moves, &mmap, fp, copylen);
    }

    /* Finally, append the postcopy chain to the end of the moves list */
    for (op = moves; (o = *op); op = &o->next) ;	/* Locate the end of the list */
    *op = postcopy;
    postcopy = NULL;

    rv = 0;
bail:
    if (mmap)
	syslinux_free_memmap(mmap);
    if (frags)
	free_movelist(&frags);
    if (postcopy)
	free_movelist(&postcopy);
    return rv;
}

#ifdef TEST

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *f;
    unsigned long d, s, l;
    struct syslinux_movelist *frags;
    struct syslinux_movelist **fep = &frags;
    struct syslinux_movelist *mv, *moves;
    struct syslinux_memmap *memmap;
    char line[BUFSIZ];

    (void)argc;

    memmap = syslinux_init_memmap();

    f = fopen(argv[1], "r");
    while (fgets(line, sizeof line, f) != NULL) {
	if (sscanf(line, "%lx %lx %lx", &s, &d, &l) == 3) {
	    if (d) {
		if (s == -1UL) {
		    syslinux_add_memmap(&memmap, d, l, SMT_ZERO);
		} else {
		    mv = new_movelist(d, s, l);
		    *fep = mv;
		    fep = &mv->next;
		}
	    } else {
		syslinux_add_memmap(&memmap, s, l, SMT_FREE);
	    }
	}
    }
    fclose(f);

    *fep = NULL;

    dprintf("Input move list:\n");
    syslinux_dump_movelist(frags);
    dprintf("Input free list:\n");
    syslinux_dump_memmap(memmap);

    if (syslinux_compute_movelist(&moves, frags, memmap)) {
	printf("Failed to compute a move sequence\n");
	return 1;
    } else {
	dprintf("Final move list:\n");
	syslinux_dump_movelist(stdout, moves);
	return 0;
    }
}

#endif /* TEST */