summaryrefslogtreecommitdiff
path: root/laden/loader.c
blob: 5cec081fb92b0200aac5138e7304e3aea4e8d925 (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
/* loader.c - Load ELF files.
   Copyright (C) 2003, 2007 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <l4/kip.h>

#include "laden.h"
#include "loader.h"
#include "output.h"
#include "shutdown.h"

#include "elf.h"

l4_memory_desc_t memory_map[MEMORY_MAP_MAX];

l4_word_t memory_map_size;


/* Return the number of memory descriptors.  */
l4_word_t
loader_get_num_memory_desc (void)
{
  return memory_map_size;
}


/* Return the NRth memory descriptor.  The first memory descriptor is
   indexed by 0.  */
l4_memory_desc_t *
loader_get_memory_desc (l4_word_t nr)
{
  return &memory_map[nr];
}

/* Verify that the memory region START to END (exclusive) is valid.  */
static void
mem_check (const char *name, l4_word_t start, l4_word_t end)
{
  l4_memory_desc_t *memdesc;
  int nr;
  int fits = 0;
  int conflicts = 0;

  if (!loader_get_num_memory_desc ())
    return;

  /* FIXME: This implementation does not account for conventional
     memory overriding non-conventional memory in the descriptor
     list.  */
  for (nr = 0; nr < loader_get_num_memory_desc (); nr++)
    {
      memdesc = loader_get_memory_desc (nr);

      if (memdesc->type == L4_MEMDESC_CONVENTIONAL)
	{
	  /* Check if the region fits into conventional memory.  */
	  if (start >= l4_memory_desc_low (memdesc)
	      && start <= l4_memory_desc_high (memdesc)
	      && end >= l4_memory_desc_low (memdesc)
	      && end <= l4_memory_desc_high (memdesc))
	    {
	      debug (1, "Memory 0x%x-0x%x fits in conventional memory map %d "
		     "(0x%x-0x%x)\n",
		     start, end, nr,
		     l4_memory_desc_low (memdesc),
		     l4_memory_desc_high (memdesc));
	      fits = 1;
	    }
	}
      else
	{
	  /* Check if the region overlaps with non-conventional
	     memory.  */
	  if ((start >= l4_memory_desc_low (memdesc)
	       && start <= l4_memory_desc_high (memdesc))
	      || (end >= l4_memory_desc_low (memdesc)
		  && end <= l4_memory_desc_high (memdesc))
	      || (start < l4_memory_desc_low (memdesc)
		  && end > l4_memory_desc_high (memdesc)))
	    {
	      debug (1, "Memory 0x%x-0x%x conflicts with non-conventional "
		     "memory map %d (0x%x-0x%x)\n",
		     start, end, nr,
		     l4_memory_desc_low (memdesc),
		     l4_memory_desc_high (memdesc));
	      fits = 0;
	      conflicts = 1 + nr;
	    }
	}
    }

  if (!fits)
    {
      if (conflicts)
	{
	  memdesc = loader_get_memory_desc (conflicts - 1);
	  panic ("%s (0x%" L4_PRIxWORD " - 0x%" L4_PRIxWORD ") conflicts "
		 "with memory of type %i/%i (0x%" L4_PRIxWORD " - 0x%"
		 L4_PRIxWORD ")", name, start, end + 1,
		 memdesc->type, memdesc->subtype,
		 l4_memory_desc_low (memdesc), l4_memory_desc_high (memdesc));
	}
      else
	panic ("%s (0x%" L4_PRIxWORD " - 0x%" L4_PRIxWORD ") does not fit "
	       "into memory", name, start, end + 1);
    }
}


/* We use a table of memory regions to check for overlap.  */

#define MAX_REGIONS 16

static struct
{
  const char *name;
  l4_word_t start;
  l4_word_t end;
  /* Function pointer to relocate this region in case of conflict.  */  
  relocate_region rr;
  void *cookie;

  /* If this descriptor is valid.  */
  int used;

  /* What type of descriptor this is (L4_MEMDESC_RESERVED,
     L4_MEMDESC_BOOTLOADER, etc.).  If -1, added to the KIP's memory
     descriptor table.  */
  int desc_type;
} used_regions[MAX_REGIONS];

static int nr_regions;


void
loader_add_region (const char *name, l4_word_t start, l4_word_t end,
		   relocate_region rr, void *cookie,
		   int desc_type)
{
  debug (1, "Protected Region: %s (0x%x - 0x%x)\n", name, start, end);

  if (start >= end)
    panic ("Region %s has a start address following the end address", name);

  /* Make sure that the requested region corresponds to real
     memory.  */
  mem_check (name, start, end);

  /* Allocate a region descriptor.  */
  int region = nr_regions;
  if (region == MAX_REGIONS)
    {
      for (region = 0; region < MAX_REGIONS; region ++)
	if (! used_regions[region].used)
	  break;

      if (region == MAX_REGIONS)
	panic ("Out of memory region descriptors, region %s doesn't fit",
	       name);
    }
  else
    nr_regions ++;

  used_regions[region].name = name;
  used_regions[region].start = start;
  used_regions[region].end = end;
  used_regions[region].rr = rr;
  used_regions[region].cookie = cookie;
  used_regions[region].desc_type = desc_type;
  used_regions[region].used = 1;

  /* Check if the region overlaps with any established regions.  */
  int i;
  for (i = 0; i < nr_regions; i++)
    if (i != region
	&& used_regions[i].used
	&& ((start >= used_regions[i].start && start < used_regions[i].end)
	    || (end >= used_regions[i].start && end <= used_regions[i].end)
	    || (start < used_regions[i].start && end > used_regions[i].start)))
      /* Region REGION conflict with region I.  Try to relocate region
	 I.  */
      {
	l4_word_t mstart = used_regions[i].start;
	l4_word_t mend = used_regions[i].end;
	l4_word_t msize = mend - mstart + 1;

	if (! used_regions[i].rr)
	  /* Region I is not relocatable.  */
	  panic ("%s (0x%x - 0x%x) conflicts with %s (0x%x - 0x%x)",
		 used_regions[region].name, start, end,
		 used_regions[i].name, mstart, mend);

	debug (1, "%s (0x%x - 0x%x) conflicts with %s (0x%x - 0x%x);"
	       " moving latter\n",
	       used_regions[region].name, start, end,
	       used_regions[i].name, mstart, mend);

	int j;
	for (j = 0; j < loader_get_num_memory_desc (); j ++)
	  {
	    l4_memory_desc_t *memdesc = loader_get_memory_desc (j);
	    l4_word_t mem_low = l4_memory_desc_low (memdesc);
	    l4_word_t mem_high = l4_memory_desc_high (memdesc);

	    debug (1, "consider memory_map: %d, %d, 0x%x-0x%x\n",
		   j, memory_map[j].type, mem_low, mem_high);

	    if (memory_map[j].type == L4_MEMDESC_CONVENTIONAL
		&& mem_high - mem_low >= msize)
	      /* This memory map is useable and large enough.  See if
		 there is a non-conflicting, contiguous space of SIZE
		 bytes.  */
	      {
		debug (1, "Considering memory (0x%x-0x%x)\n",
		       mem_low, mem_high);

		/* Start with the lowest address.  */
		l4_word_t ns = mem_low;

		/* See if any regions overlap with (NS, NS + SIZE].  If
		   so, advance NS just beyond them.  */
		int k;
	      restart:
		for (k = 0;
		     k < nr_regions && ns + msize - 1 <= mem_high;
		     k ++)
		  if (used_regions[k].used && k != i
		      && ((ns >= used_regions[k].start
			   && ns <= used_regions[k].end)
			  || (ns + msize - 1 >= used_regions[k].start
			      && ns + msize - 1 <= used_regions[k].end)
			  || (ns < used_regions[k].start
			      && ns + msize - 1 >= used_regions[k].start)))
		    /* There is overlap, take the end of the conflicting
		       region as our new start and restart.  */
		    {
		      debug (1, "Can't use 0x%x, %s(0x%x-0x%x) in way\n",
			     ns, used_regions[k].name,
			     used_regions[k].start, used_regions[k].end);
		      /* Try the page aligned after the end of this
			 region.  */
		      ns = (used_regions[k].end + 1 + 0xfff) & ~0xfff;
		      debug (1, "Trying address 0x%x\n", ns);
		      goto restart;
		    }

		if (k == nr_regions && ns + msize - 1 <= mem_high)
		  /* NS is a good new start!  */
		  {
		    debug (1, "Moving %s(%d) from 0x%x-0x%x to 0x%x-0x%x\n",
			   used_regions[i].name, i,
			   used_regions[i].start, used_regions[i].end,
			   ns, ns + msize - 1);

		    memmove ((void *) ns, (void *) used_regions[i].start,
			     msize);
		    used_regions[i].rr (used_regions[i].name,
					used_regions[i].start,
					used_regions[i].end,
					ns, used_regions[i].cookie);
		    used_regions[i].start = ns;
		    used_regions[i].end = ns + msize - 1;
		    break;
		  }
	      }
	  }

	if (j == memory_map_size)
	  /* Couldn't find a free region!  */
	  panic ("%s (0x%x - 0x%x) conflicts with %s (0x%x - 0x%x)"
		 " but nowhere to move the latter",
		 used_regions[region].name,
		 used_regions[region].start, used_regions[region].end,
		 used_regions[i].name,
		 used_regions[i].start, used_regions[i].end);
      }
}


/* Remove the region with the name NAME from the table.  */
void
loader_remove_region (const char *name)
{
  int i;
  int found = 0;

  for (i = 0; i < nr_regions; i++)
    if (!strcmp (used_regions[i].name, name))
      {
	found = 1;
	used_regions[i].used = 0;
      }

  if (! found)
    panic ("Assertion failure: Could not find region %s for removal", name);
}

void
loader_regions_reserve (void)
{
#ifdef _L4_V2
  int i;
  for (i = 0; i < nr_regions; i++)
    if (used_regions[i].used && used_regions[i].desc_type != -1)
      {
	/* Round down.  */
	l4_word_t start = used_regions[i].start & ~0x3ff;
	/* Round up.  */
	l4_word_t end = ((used_regions[i].end + 0x3ff - 1) & ~0x3ff) - 1;

	debug (1, "Reserving memory 0x%x-0x%x (%s)\n",
	       start, end, used_regions[i].name);

	add_memory_map (start, end, used_regions[i].desc_type, 0);
      }
#endif
}

/* Get the memory range to which the ELF image from START to END
   (exclusive) will be loaded.  NAME is used for panic messages.  */
void
loader_elf_dest (const char *name, l4_word_t start, l4_word_t end,
		 l4_word_t *new_start_p, l4_word_t *new_end_p)
{
  l4_word_t new_start = -1;
  l4_word_t new_end = 0;
  int i;

  Elf32_Ehdr *elf = (Elf32_Ehdr *) start;

  if (elf->e_ident[EI_MAG0] != ELFMAG0
      || elf->e_ident[EI_MAG1] != ELFMAG1
      || elf->e_ident[EI_MAG2] != ELFMAG2
      || elf->e_ident[EI_MAG3] != ELFMAG3)
    panic ("%s is not an ELF file", name);

  if (elf->e_type != ET_EXEC)
    panic ("%s is not an executable file", name);

  if (!elf->e_phoff)
    panic ("%s has no valid program header offset", name);

  /* FIXME: Some architectures support both word sizes.  */
  if (!((elf->e_ident[EI_CLASS] == ELFCLASS32
	 && L4_WORDSIZE == 32)
	|| (elf->e_ident[EI_CLASS] == ELFCLASS64
	    && L4_WORDSIZE == 64)))
    panic ("%s has invalid word size", name);
  if (!((elf->e_ident[EI_DATA] == ELFDATA2LSB
	 && L4_BYTE_ORDER == L4_LITTLE_ENDIAN)
	|| (elf->e_ident[EI_DATA] == ELFDATA2MSB
	    && L4_BYTE_ORDER == L4_BIG_ENDIAN)))
    panic ("%s has invalid byte order", name);

#if i386
# define elf_machine EM_386
#elif PPC
# define elf_machine EM_PPC
#else
# error Not ported to this architecture!
#endif

  if (elf->e_machine != elf_machine)
    panic ("%s is not for this architecture", name);

  for (i = 0; i < elf->e_phnum; i++)
    {
      Elf32_Phdr *ph = (Elf32_Phdr *) (start + elf->e_phoff
				       + i * elf->e_phentsize);
      if (ph->p_type == PT_LOAD)
	{
	  if (ph->p_paddr < new_start)
	    new_start = ph->p_paddr;
	  if (ph->p_memsz + ph->p_paddr > new_end)
	    new_end = ph->p_memsz + ph->p_paddr;
	}
    }

  if (new_start_p)
    *new_start_p = new_start;
  if (new_end_p)
    *new_end_p = new_end;
}


/* Load the ELF image from START to END (exclusive) into memory under
   the name NAME (also used as the name for the region of the
   resulting ELF program).  Return the lowest and highest address used
   by the program in NEW_START_P and NEW_END_P, and the entry point in
   ENTRY.  */
void
loader_elf_load (const char *name, l4_word_t start, l4_word_t end,
		 l4_word_t *new_start_p, l4_word_t *new_end_p,
		 l4_word_t *entry, int desc_type)
{
  l4_word_t new_start = -1;
  l4_word_t new_end = 0;
  int i;

  Elf32_Ehdr *elf = (Elf32_Ehdr *) start;

  if (elf->e_ident[EI_MAG0] != ELFMAG0
      || elf->e_ident[EI_MAG1] != ELFMAG1
      || elf->e_ident[EI_MAG2] != ELFMAG2
      || elf->e_ident[EI_MAG3] != ELFMAG3)
    panic ("%s is not an ELF file", name);

  if (elf->e_type != ET_EXEC)
    panic ("%s is not an executable file", name);

  if (!elf->e_phoff)
    panic ("%s has no valid program header offset", name);

  /* FIXME: Some architectures support both word sizes.  */
  if (!((elf->e_ident[EI_CLASS] == ELFCLASS32
	 && L4_WORDSIZE == 32)
	|| (elf->e_ident[EI_CLASS] == ELFCLASS64
	    && L4_WORDSIZE == 64)))
    panic ("%s has invalid word size", name);
  if (!((elf->e_ident[EI_DATA] == ELFDATA2LSB
	 && L4_BYTE_ORDER == L4_LITTLE_ENDIAN)
	|| (elf->e_ident[EI_DATA] == ELFDATA2MSB
	    && L4_BYTE_ORDER == L4_BIG_ENDIAN)))
    panic ("%s has invalid byte order", name);

#if i386
# define elf_machine EM_386
#elif PPC
# define elf_machine EM_PPC
#else
# error Not ported to this architecture!
#endif

  if (elf->e_machine != elf_machine)
    panic ("%s is not for this architecture", name);

  /* Determine the bounds of the extracted executable.  */
  l4_word_t seg_start = 0;
  l4_word_t seg_end = 0;
  for (i = 0; i < elf->e_phnum; i++)
    {
      Elf32_Phdr *ph = (Elf32_Phdr *) (start + elf->e_phoff
				       + i * elf->e_phentsize);
      if (ph->p_type == PT_LOAD)
	{
	  /* FIXME: Add this as a bootloader specific memory type to L4's
	     memdesc list instead.  */
	  /* Add the region.  */
	  if (seg_end == 0)
	    seg_start = ph->p_paddr;
	  else if (! (seg_end < ph->p_paddr
		      && ph->p_paddr <= seg_end + 0x1000))
	    /* Don't coalesce segments with more than a 4k separation.  */
	    {
	      /* Protect last segment(s), rounding up the end.  */
	      seg_end = ((seg_end + 1 + 0x3ff) & ~0x3ff) - 1;
	      loader_add_region (name, seg_start, seg_end, NULL, NULL,
				 desc_type);

	      /* Note start of new segment.  */
	      seg_start = ph->p_paddr;
	    }
	  seg_end = ph->p_paddr + ph->p_memsz - 1;

	  if (ph->p_paddr < new_start)
	    new_start = ph->p_paddr;
	  if (ph->p_memsz + ph->p_paddr > new_end)
	    new_end = ph->p_memsz + ph->p_paddr;
	}
    }

  if (seg_start != seg_end)
    loader_add_region (name, seg_start, seg_end, NULL, NULL, desc_type);

  /* Now load it.  */
  for (i = 0; i < elf->e_phnum; i++)
    {
      Elf32_Phdr *ph = (Elf32_Phdr *) (start + elf->e_phoff
				       + i * elf->e_phentsize);
      if (ph->p_type == PT_LOAD)
	{
	  memcpy ((char *) ph->p_paddr, (char *) start + ph->p_offset,
		  ph->p_filesz);
	  /* Initialize the rest.  */
	  if (ph->p_memsz > ph->p_filesz)
	    memset ((char *) ph->p_paddr + ph->p_filesz, 0,
		    ph->p_memsz - ph->p_filesz);
	}
    }

  if (new_start_p)
    *new_start_p = new_start;
  if (new_end_p)
    *new_end_p = new_end;
  if (entry)
    *entry = elf->e_entry;
}