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 u64 end = start + size;
67 for (i=0; i<e820_count; i++) {
68 struct e820entry *e = &e820_list[i];
70 // Simple insertion point.
72 u64 e_end = e->start + e->size;
76 // New item overlaps (or borders) an existing one.
77 if (start > e->start) {
78 e->size = start - e->start;
81 // Need to split existing item
82 insert_e820(i, end, e_end - end, e->type);
83 if (type == e->type) {
84 // Same type - merge them.
85 size += start - e->start;
91 if (type != E820_HOLE) {
92 insert_e820(i, start, size, type);
95 // Remove all existing items that are completely overlapped.
96 while (i<e820_count) {
101 e_end = e->start + e->size;
103 // Existing item completely overlapped - remove it.
107 // Not completely overlapped - adjust its start.
109 e->size = e_end - e->start;
110 if (type == e->type) {
111 // Same type - merge them.
112 (e-1)->size += e->size;
121 insert_e820(i, start, size, type);
125 // Prep for memmap stuff - init bios table locations.
129 bios_table_cur_addr = (u32)BiosTableSpace;
130 bios_table_end_addr = bios_table_cur_addr + CONFIG_MAX_BIOSTABLE;
131 dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
132 bios_table_cur_addr, bios_table_end_addr);
137 // Report on final memory locations.
143 dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n"
144 , bios_table_cur_addr
145 , (100 * (bios_table_cur_addr - (u32)&BiosTableSpace)
146 / CONFIG_MAX_BIOSTABLE));
147 if (bios_table_cur_addr > bios_table_end_addr)
148 panic("bios_table_end_addr overflow!\n");