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 GPLv3 license.
7 #include "memmap.h" // struct e820entry
8 #include "util.h" // dprintf.h
9 #include "biosvar.h" // SET_EBDA
11 // Temporary storage used during map building.
12 static struct e820entry e820_list[64];
13 static int e820_count;
15 // Remove an entry from the e820_list.
20 memmove(&e820_list[i], &e820_list[i+1]
21 , sizeof(e820_list[0]) * (e820_count - i));
24 // Insert an entry in the e820_list at the given position.
26 insert_e820(int i, u64 start, u64 size, u32 type)
28 if (e820_count >= ARRAY_SIZE(e820_list)) {
29 dprintf(1, "Overflowed e820 list!\n");
33 memmove(&e820_list[i+1], &e820_list[i]
34 , sizeof(e820_list[0]) * (e820_count - i));
36 struct e820entry *e = &e820_list[i];
42 // Show the current e820_list.
46 dprintf(1, "e820 map has %d items:\n", e820_count);
48 for (i=0; i<e820_count; i++) {
49 struct e820entry *e = &e820_list[i];
50 u64 e_end = e->start + e->size;
51 dprintf(1, " %d: %x%x - %x%x = %d\n", i
52 , (u32)(e->start >> 32), (u32)e->start
53 , (u32)(e_end >> 32), (u32)e_end
58 // Add a new entry to the list. This scans for overlaps and keeps the
61 add_e820(u64 start, u64 size, u32 type)
63 dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type);
66 // Huh? Nothing to do.
69 u64 end = start + size;
71 for (i=0; i<e820_count; i++) {
72 struct e820entry *e = &e820_list[i];
74 // Simple insertion point.
76 u64 e_end = e->start + e->size;
80 // New item overlaps (or borders) an existing one.
81 if (start > e->start) {
82 e->size = start - e->start;
85 // Need to split existing item
86 insert_e820(i, end, e_end - end, e->type);
87 if (type == e->type) {
88 // Same type - merge them.
89 size += start - e->start;
95 if (type != E820_HOLE) {
96 insert_e820(i, start, size, type);
99 // Remove all existing items that are completely overlapped.
100 while (i<e820_count) {
103 // No overlap - done.
105 e_end = e->start + e->size;
107 // Existing item completely overlapped - remove it.
111 // Not completely overlapped - adjust its start.
113 e->size = e_end - e->start;
114 if (type == e->type) {
115 // Same type - merge them.
116 (e-1)->size += e->size;
125 insert_e820(i, start, size, type);
129 u32 bios_table_cur_addr, bios_table_end_addr;
131 // Prep for memmap stuff - init bios table locations.
135 // Symbols defined in romlayout.S
136 extern char freespace2_start, freespace2_end;
138 bios_table_cur_addr = (u32)&freespace2_start;
139 bios_table_end_addr = (u32)&freespace2_end;
140 dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
141 bios_table_cur_addr, bios_table_end_addr);
144 // Copy the temporary e820 map info to its permanent location.
150 u32 msize = e820_count * sizeof(e820_list[0]);
151 if (bios_table_cur_addr + msize > bios_table_end_addr) {
152 dprintf(1, "No room for e820 map!\n");
155 memcpy((void*)bios_table_cur_addr, e820_list, msize);
156 SET_EBDA(e820_loc, bios_table_cur_addr);
157 SET_EBDA(e820_count, e820_count);
158 bios_table_cur_addr += msize;
160 dprintf(1, "bios_table_cur_addr: 0x%08x\n", bios_table_cur_addr);
161 if (bios_table_cur_addr > bios_table_end_addr)
162 BX_PANIC("bios_table_end_addr overflow!\n");