Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 1 | // Support for building memory maps suitable for int 15 e820 calls. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 6 | |
| 7 | #include "memmap.h" // struct e820entry |
| 8 | #include "util.h" // dprintf.h |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "biosvar.h" // SET_EBDA |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 10 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 11 | // Remove an entry from the e820_list. |
| 12 | static void |
| 13 | remove_e820(int i) |
| 14 | { |
| 15 | e820_count--; |
| 16 | memmove(&e820_list[i], &e820_list[i+1] |
| 17 | , sizeof(e820_list[0]) * (e820_count - i)); |
| 18 | } |
| 19 | |
| 20 | // Insert an entry in the e820_list at the given position. |
| 21 | static void |
| 22 | insert_e820(int i, u64 start, u64 size, u32 type) |
| 23 | { |
Kevin O'Connor | d995b3d | 2008-11-08 13:05:27 -0500 | [diff] [blame] | 24 | if (e820_count >= CONFIG_MAX_E820) { |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 25 | dprintf(1, "Overflowed e820 list!\n"); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | memmove(&e820_list[i+1], &e820_list[i] |
| 30 | , sizeof(e820_list[0]) * (e820_count - i)); |
| 31 | e820_count++; |
| 32 | struct e820entry *e = &e820_list[i]; |
| 33 | e->start = start; |
| 34 | e->size = size; |
| 35 | e->type = type; |
| 36 | } |
| 37 | |
| 38 | // Show the current e820_list. |
| 39 | static void |
| 40 | dump_map() |
| 41 | { |
| 42 | dprintf(1, "e820 map has %d items:\n", e820_count); |
| 43 | int i; |
| 44 | for (i=0; i<e820_count; i++) { |
| 45 | struct e820entry *e = &e820_list[i]; |
| 46 | u64 e_end = e->start + e->size; |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame^] | 47 | dprintf(1, " %d: %08x%08x - %08x%08x = %d\n", i |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 48 | , (u32)(e->start >> 32), (u32)e->start |
| 49 | , (u32)(e_end >> 32), (u32)e_end |
| 50 | , e->type); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Add a new entry to the list. This scans for overlaps and keeps the |
| 55 | // list sorted. |
| 56 | void |
| 57 | add_e820(u64 start, u64 size, u32 type) |
| 58 | { |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame^] | 59 | dprintf(8, "Add to e820 map: %08x %08x %d\n", (u32)start, (u32)size, type); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 60 | |
| 61 | if (! size) |
| 62 | // Huh? Nothing to do. |
| 63 | return; |
| 64 | |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 65 | // Find position of new item (splitting existing item if needed). |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 66 | u64 end = start + size; |
| 67 | int i; |
| 68 | for (i=0; i<e820_count; i++) { |
| 69 | struct e820entry *e = &e820_list[i]; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 70 | u64 e_end = e->start + e->size; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 71 | if (start > e_end) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 72 | continue; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 73 | // Found position - check if an existing item needs to be split. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 74 | if (start > e->start) { |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 75 | if (type == e->type) { |
| 76 | // Same type - merge them. |
| 77 | size += start - e->start; |
| 78 | start = e->start; |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 79 | } else { |
| 80 | // Split existing item. |
| 81 | e->size = start - e->start; |
| 82 | i++; |
| 83 | if (e_end > end) |
| 84 | insert_e820(i, end, e_end - end, e->type); |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 85 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 86 | } |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 87 | break; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 88 | } |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 89 | // Remove/adjust existing items that are overlapping. |
| 90 | while (i<e820_count) { |
| 91 | struct e820entry *e = &e820_list[i]; |
| 92 | if (end < e->start) |
| 93 | // No overlap - done. |
| 94 | break; |
| 95 | u64 e_end = e->start + e->size; |
| 96 | if (end >= e_end) { |
| 97 | // Existing item completely overlapped - remove it. |
| 98 | remove_e820(i); |
| 99 | continue; |
| 100 | } |
| 101 | // Not completely overlapped - adjust its start. |
| 102 | e->start = end; |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 103 | e->size = e_end - end; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 104 | if (type == e->type) { |
| 105 | // Same type - merge them. |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 106 | size += e->size; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 107 | remove_e820(i); |
| 108 | } |
| 109 | break; |
| 110 | } |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 111 | // Insert new item. |
| 112 | if (type != E820_HOLE) |
| 113 | insert_e820(i, start, size, type); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 114 | //dump_map(); |
| 115 | } |
| 116 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 117 | // Prep for memmap stuff - init bios table locations. |
| 118 | void |
| 119 | memmap_setup() |
| 120 | { |
Kevin O'Connor | 0763381 | 2009-02-17 22:36:49 -0500 | [diff] [blame] | 121 | memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE); |
Kevin O'Connor | df2c19a | 2009-01-17 20:07:09 -0500 | [diff] [blame] | 122 | bios_table_cur_addr = (u32)BiosTableSpace; |
| 123 | bios_table_end_addr = bios_table_cur_addr + CONFIG_MAX_BIOSTABLE; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 124 | dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n", |
| 125 | bios_table_cur_addr, bios_table_end_addr); |
Kevin O'Connor | d995b3d | 2008-11-08 13:05:27 -0500 | [diff] [blame] | 126 | |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 127 | e820_count = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Kevin O'Connor | 9649a96 | 2008-12-10 20:53:35 -0500 | [diff] [blame] | 130 | // Report on final memory locations. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 131 | void |
| 132 | memmap_finalize() |
| 133 | { |
| 134 | dump_map(); |
| 135 | |
Kevin O'Connor | 15b3165 | 2008-08-29 21:21:09 -0400 | [diff] [blame] | 136 | dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n" |
| 137 | , bios_table_cur_addr |
Kevin O'Connor | df2c19a | 2009-01-17 20:07:09 -0500 | [diff] [blame] | 138 | , (100 * (bios_table_cur_addr - (u32)&BiosTableSpace) |
| 139 | / CONFIG_MAX_BIOSTABLE)); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 140 | if (bios_table_cur_addr > bios_table_end_addr) |
Kevin O'Connor | e07e18e | 2009-02-08 17:07:29 -0500 | [diff] [blame] | 141 | panic("bios_table_end_addr overflow!\n"); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 142 | } |