1 // Support for building memory maps suitable for int 15 e820 calls.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "memmap.h" // struct e820entry
8 #include "util.h" // dprintf.h
9 #include "biosvar.h" // SET_EBDA
11 // Remove an entry from the e820_list.
16 memmove(&e820_list[i], &e820_list[i+1]
17 , sizeof(e820_list[0]) * (e820_count - i));
20 // Insert an entry in the e820_list at the given position.
22 insert_e820(int i, u64 start, u64 size, u32 type)
24 if (e820_count >= CONFIG_MAX_E820) {
25 dprintf(1, "Overflowed e820 list!\n");
29 memmove(&e820_list[i+1], &e820_list[i]
30 , sizeof(e820_list[0]) * (e820_count - i));
32 struct e820entry *e = &e820_list[i];
38 // Show the current e820_list.
42 dprintf(1, "e820 map has %d items:\n", e820_count);
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
54 // Add a new entry to the list. This scans for overlaps and keeps the
57 add_e820(u64 start, u64 size, u32 type)
59 dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type);
62 // Huh? Nothing to do.
65 // Find position of new item (splitting existing item if needed).
66 u64 end = start + size;
68 for (i=0; i<e820_count; i++) {
69 struct e820entry *e = &e820_list[i];
70 u64 e_end = e->start + e->size;
73 // Found position - check if an existing item needs to be split.
74 if (start > e->start) {
75 if (type == e->type) {
76 // Same type - merge them.
77 size += start - e->start;
80 // Split existing item.
81 e->size = start - e->start;
84 insert_e820(i, end, e_end - end, e->type);
89 // Remove/adjust existing items that are overlapping.
90 while (i<e820_count) {
91 struct e820entry *e = &e820_list[i];
95 u64 e_end = e->start + e->size;
97 // Existing item completely overlapped - remove it.
101 // Not completely overlapped - adjust its start.
103 e->size = e_end - end;
104 if (type == e->type) {
105 // Same type - merge them.
112 if (type != E820_HOLE)
113 insert_e820(i, start, size, type);
117 // Prep for memmap stuff - init bios table locations.
121 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
122 bios_table_cur_addr = (u32)BiosTableSpace;
123 bios_table_end_addr = bios_table_cur_addr + CONFIG_MAX_BIOSTABLE;
124 dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
125 bios_table_cur_addr, bios_table_end_addr);
130 // Report on final memory locations.
136 dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n"
137 , bios_table_cur_addr
138 , (100 * (bios_table_cur_addr - (u32)&BiosTableSpace)
139 / CONFIG_MAX_BIOSTABLE));
140 if (bios_table_cur_addr > bios_table_end_addr)
141 panic("bios_table_end_addr overflow!\n");