aboutsummaryrefslogtreecommitdiffstats
path: root/arch/i386/kernel/e820.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/i386/kernel/e820.c')
-rw-r--r--arch/i386/kernel/e820.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/arch/i386/kernel/e820.c b/arch/i386/kernel/e820.c
index cce706049488..0db95760b073 100644
--- a/arch/i386/kernel/e820.c
+++ b/arch/i386/kernel/e820.c
@@ -19,6 +19,14 @@ EXPORT_SYMBOL(efi_enabled);
19#endif 19#endif
20 20
21struct e820map e820; 21struct e820map e820;
22struct change_member {
23 struct e820entry *pbios; /* pointer to original bios entry */
24 unsigned long long addr; /* address for this change point */
25};
26static struct change_member change_point_list[2*E820MAX] __initdata;
27static struct change_member *change_point[2*E820MAX] __initdata;
28static struct e820entry *overlap_list[E820MAX] __initdata;
29static struct e820entry new_bios[E820MAX] __initdata;
22struct resource data_resource = { 30struct resource data_resource = {
23 .name = "Kernel data", 31 .name = "Kernel data",
24 .start = 0, 32 .start = 0,
@@ -287,3 +295,247 @@ static int __init request_standard_resources(void)
287} 295}
288 296
289subsys_initcall(request_standard_resources); 297subsys_initcall(request_standard_resources);
298
299void __init add_memory_region(unsigned long long start,
300 unsigned long long size, int type)
301{
302 int x;
303
304 if (!efi_enabled) {
305 x = e820.nr_map;
306
307 if (x == E820MAX) {
308 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
309 return;
310 }
311
312 e820.map[x].addr = start;
313 e820.map[x].size = size;
314 e820.map[x].type = type;
315 e820.nr_map++;
316 }
317} /* add_memory_region */
318
319/*
320 * Sanitize the BIOS e820 map.
321 *
322 * Some e820 responses include overlapping entries. The following
323 * replaces the original e820 map with a new one, removing overlaps.
324 *
325 */
326int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
327{
328 struct change_member *change_tmp;
329 unsigned long current_type, last_type;
330 unsigned long long last_addr;
331 int chgidx, still_changing;
332 int overlap_entries;
333 int new_bios_entry;
334 int old_nr, new_nr, chg_nr;
335 int i;
336
337 /*
338 Visually we're performing the following (1,2,3,4 = memory types)...
339
340 Sample memory map (w/overlaps):
341 ____22__________________
342 ______________________4_
343 ____1111________________
344 _44_____________________
345 11111111________________
346 ____________________33__
347 ___________44___________
348 __________33333_________
349 ______________22________
350 ___________________2222_
351 _________111111111______
352 _____________________11_
353 _________________4______
354
355 Sanitized equivalent (no overlap):
356 1_______________________
357 _44_____________________
358 ___1____________________
359 ____22__________________
360 ______11________________
361 _________1______________
362 __________3_____________
363 ___________44___________
364 _____________33_________
365 _______________2________
366 ________________1_______
367 _________________4______
368 ___________________2____
369 ____________________33__
370 ______________________4_
371 */
372 printk("sanitize start\n");
373 /* if there's only one memory region, don't bother */
374 if (*pnr_map < 2) {
375 printk("sanitize bail 0\n");
376 return -1;
377 }
378
379 old_nr = *pnr_map;
380
381 /* bail out if we find any unreasonable addresses in bios map */
382 for (i=0; i<old_nr; i++)
383 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
384 printk("sanitize bail 1\n");
385 return -1;
386 }
387
388 /* create pointers for initial change-point information (for sorting) */
389 for (i=0; i < 2*old_nr; i++)
390 change_point[i] = &change_point_list[i];
391
392 /* record all known change-points (starting and ending addresses),
393 omitting those that are for empty memory regions */
394 chgidx = 0;
395 for (i=0; i < old_nr; i++) {
396 if (biosmap[i].size != 0) {
397 change_point[chgidx]->addr = biosmap[i].addr;
398 change_point[chgidx++]->pbios = &biosmap[i];
399 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
400 change_point[chgidx++]->pbios = &biosmap[i];
401 }
402 }
403 chg_nr = chgidx; /* true number of change-points */
404
405 /* sort change-point list by memory addresses (low -> high) */
406 still_changing = 1;
407 while (still_changing) {
408 still_changing = 0;
409 for (i=1; i < chg_nr; i++) {
410 /* if <current_addr> > <last_addr>, swap */
411 /* or, if current=<start_addr> & last=<end_addr>, swap */
412 if ((change_point[i]->addr < change_point[i-1]->addr) ||
413 ((change_point[i]->addr == change_point[i-1]->addr) &&
414 (change_point[i]->addr == change_point[i]->pbios->addr) &&
415 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
416 )
417 {
418 change_tmp = change_point[i];
419 change_point[i] = change_point[i-1];
420 change_point[i-1] = change_tmp;
421 still_changing=1;
422 }
423 }
424 }
425
426 /* create a new bios memory map, removing overlaps */
427 overlap_entries=0; /* number of entries in the overlap table */
428 new_bios_entry=0; /* index for creating new bios map entries */
429 last_type = 0; /* start with undefined memory type */
430 last_addr = 0; /* start with 0 as last starting address */
431 /* loop through change-points, determining affect on the new bios map */
432 for (chgidx=0; chgidx < chg_nr; chgidx++)
433 {
434 /* keep track of all overlapping bios entries */
435 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
436 {
437 /* add map entry to overlap list (> 1 entry implies an overlap) */
438 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
439 }
440 else
441 {
442 /* remove entry from list (order independent, so swap with last) */
443 for (i=0; i<overlap_entries; i++)
444 {
445 if (overlap_list[i] == change_point[chgidx]->pbios)
446 overlap_list[i] = overlap_list[overlap_entries-1];
447 }
448 overlap_entries--;
449 }
450 /* if there are overlapping entries, decide which "type" to use */
451 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
452 current_type = 0;
453 for (i=0; i<overlap_entries; i++)
454 if (overlap_list[i]->type > current_type)
455 current_type = overlap_list[i]->type;
456 /* continue building up new bios map based on this information */
457 if (current_type != last_type) {
458 if (last_type != 0) {
459 new_bios[new_bios_entry].size =
460 change_point[chgidx]->addr - last_addr;
461 /* move forward only if the new size was non-zero */
462 if (new_bios[new_bios_entry].size != 0)
463 if (++new_bios_entry >= E820MAX)
464 break; /* no more space left for new bios entries */
465 }
466 if (current_type != 0) {
467 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
468 new_bios[new_bios_entry].type = current_type;
469 last_addr=change_point[chgidx]->addr;
470 }
471 last_type = current_type;
472 }
473 }
474 new_nr = new_bios_entry; /* retain count for new bios entries */
475
476 /* copy new bios mapping into original location */
477 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
478 *pnr_map = new_nr;
479
480 printk("sanitize end\n");
481 return 0;
482}
483
484/*
485 * Copy the BIOS e820 map into a safe place.
486 *
487 * Sanity-check it while we're at it..
488 *
489 * If we're lucky and live on a modern system, the setup code
490 * will have given us a memory map that we can use to properly
491 * set up memory. If we aren't, we'll fake a memory map.
492 *
493 * We check to see that the memory map contains at least 2 elements
494 * before we'll use it, because the detection code in setup.S may
495 * not be perfect and most every PC known to man has two memory
496 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
497 * thinkpad 560x, for example, does not cooperate with the memory
498 * detection code.)
499 */
500int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
501{
502 /* Only one memory region (or negative)? Ignore it */
503 if (nr_map < 2)
504 return -1;
505
506 do {
507 unsigned long long start = biosmap->addr;
508 unsigned long long size = biosmap->size;
509 unsigned long long end = start + size;
510 unsigned long type = biosmap->type;
511 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
512
513 /* Overflow in 64 bits? Ignore the memory map. */
514 if (start > end)
515 return -1;
516
517 /*
518 * Some BIOSes claim RAM in the 640k - 1M region.
519 * Not right. Fix it up.
520 */
521 if (type == E820_RAM) {
522 printk("copy_e820_map() type is E820_RAM\n");
523 if (start < 0x100000ULL && end > 0xA0000ULL) {
524 printk("copy_e820_map() lies in range...\n");
525 if (start < 0xA0000ULL) {
526 printk("copy_e820_map() start < 0xA0000ULL\n");
527 add_memory_region(start, 0xA0000ULL-start, type);
528 }
529 if (end <= 0x100000ULL) {
530 printk("copy_e820_map() end <= 0x100000ULL\n");
531 continue;
532 }
533 start = 0x100000ULL;
534 size = end - start;
535 }
536 }
537 add_memory_region(start, size, type);
538 } while (biosmap++,--nr_map);
539 return 0;
540}
541