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 | // |
| 5 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 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; |
| 47 | dprintf(1, " %d: %x%x - %x%x = %d\n", i |
| 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 | { |
| 59 | dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type); |
| 60 | |
| 61 | if (! size) |
| 62 | // Huh? Nothing to do. |
| 63 | return; |
| 64 | |
| 65 | u64 end = start + size; |
| 66 | int i; |
| 67 | for (i=0; i<e820_count; i++) { |
| 68 | struct e820entry *e = &e820_list[i]; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 69 | if (end < e->start) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 70 | // Simple insertion point. |
| 71 | break; |
| 72 | u64 e_end = e->start + e->size; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 73 | if (start > e_end) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 74 | // No overlap. |
| 75 | continue; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 76 | // New item overlaps (or borders) an existing one. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 77 | if (start > e->start) { |
| 78 | e->size = start - e->start; |
| 79 | i++; |
| 80 | if (end < e_end) |
| 81 | // Need to split existing item |
| 82 | insert_e820(i, end, e_end - end, e->type); |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 83 | if (type == e->type) { |
| 84 | // Same type - merge them. |
| 85 | size += start - e->start; |
| 86 | start = e->start; |
| 87 | i--; |
| 88 | remove_e820(i); |
| 89 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 90 | } |
Kevin O'Connor | b8d7a47 | 2008-06-21 11:43:32 -0400 | [diff] [blame] | 91 | if (type != E820_HOLE) { |
| 92 | insert_e820(i, start, size, type); |
| 93 | i++; |
| 94 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 95 | // Remove all existing items that are completely overlapped. |
| 96 | while (i<e820_count) { |
| 97 | e = &e820_list[i]; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 98 | if (end < e->start) |
| 99 | // No overlap - done. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 100 | break; |
| 101 | e_end = e->start + e->size; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 102 | if (end >= e_end) { |
| 103 | // Existing item completely overlapped - remove it. |
| 104 | remove_e820(i); |
| 105 | continue; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 106 | } |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 107 | // Not completely overlapped - adjust its start. |
| 108 | e->start = end; |
| 109 | e->size = e_end - e->start; |
| 110 | if (type == e->type) { |
| 111 | // Same type - merge them. |
| 112 | (e-1)->size += e->size; |
| 113 | remove_e820(i); |
| 114 | } |
| 115 | break; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 116 | } |
| 117 | //dump_map(); |
| 118 | return; |
| 119 | } |
| 120 | // Just insert item. |
| 121 | insert_e820(i, start, size, type); |
| 122 | //dump_map(); |
| 123 | } |
| 124 | |
Kevin O'Connor | 15b3165 | 2008-08-29 21:21:09 -0400 | [diff] [blame] | 125 | // Symbols defined in romlayout.S |
| 126 | extern char freespace2_start, freespace2_end; |
| 127 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 128 | u32 bios_table_cur_addr, bios_table_end_addr; |
| 129 | |
| 130 | // Prep for memmap stuff - init bios table locations. |
| 131 | void |
| 132 | memmap_setup() |
| 133 | { |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 134 | bios_table_cur_addr = (u32)&freespace2_start; |
| 135 | bios_table_end_addr = (u32)&freespace2_end; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 136 | dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n", |
| 137 | bios_table_cur_addr, bios_table_end_addr); |
Kevin O'Connor | d995b3d | 2008-11-08 13:05:27 -0500 | [diff] [blame] | 138 | |
| 139 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 4); |
| 140 | u32 msize = CONFIG_MAX_E820 * sizeof(e820_list[0]); |
| 141 | if (bios_table_cur_addr + msize > bios_table_end_addr) { |
| 142 | dprintf(1, "No room for e820 map!\n"); |
| 143 | return; |
| 144 | } |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 145 | e820_count = 0; |
Kevin O'Connor | d995b3d | 2008-11-08 13:05:27 -0500 | [diff] [blame] | 146 | e820_list = (void*)bios_table_cur_addr; |
| 147 | bios_table_cur_addr += msize; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Copy the temporary e820 map info to its permanent location. |
| 151 | void |
| 152 | memmap_finalize() |
| 153 | { |
| 154 | dump_map(); |
| 155 | |
Kevin O'Connor | 15b3165 | 2008-08-29 21:21:09 -0400 | [diff] [blame] | 156 | dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n" |
| 157 | , bios_table_cur_addr |
| 158 | , (100 * (bios_table_cur_addr - (u32)&freespace2_start) |
| 159 | / ((u32)&freespace2_end - (u32)&freespace2_start))); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 160 | if (bios_table_cur_addr > bios_table_end_addr) |
| 161 | BX_PANIC("bios_table_end_addr overflow!\n"); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 162 | } |