1 // Support for building memory maps suitable for int 15 e820 calls.
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "config.h" // BUILD_MAX_E820
8 #include "e820map.h" // struct e820entry
9 #include "output.h" // dprintf
10 #include "string.h" // memmove
13 /****************************************************************
15 ****************************************************************/
17 // Info on e820 map location and size.
18 struct e820entry e820_list[BUILD_MAX_E820] VARFSEG;
19 int e820_count VARFSEG;
21 // Remove an entry from the e820_list.
26 memmove(&e820_list[i], &e820_list[i+1]
27 , sizeof(e820_list[0]) * (e820_count - i));
30 // Insert an entry in the e820_list at the given position.
32 insert_e820(int i, u64 start, u64 size, u32 type)
34 if (e820_count >= BUILD_MAX_E820) {
39 memmove(&e820_list[i+1], &e820_list[i]
40 , sizeof(e820_list[0]) * (e820_count - i));
42 struct e820entry *e = &e820_list[i];
49 e820_type_name(u32 type)
52 case E820_RAM: return "RAM";
53 case E820_RESERVED: return "RESERVED";
54 case E820_ACPI: return "ACPI";
55 case E820_NVS: return "NVS";
56 case E820_UNUSABLE: return "UNUSABLE";
57 default: return "UNKNOWN";
61 // Show the current e820_list.
65 dprintf(1, "e820 map has %d items:\n", e820_count);
67 for (i=0; i<e820_count; i++) {
68 struct e820entry *e = &e820_list[i];
69 u64 e_end = e->start + e->size;
70 dprintf(1, " %d: %016llx - %016llx = %d %s\n", i
71 , e->start, e_end, e->type, e820_type_name(e->type));
75 #define E820_HOLE ((u32)-1) // Used internally to remove entries
77 // Add a new entry to the list. This scans for overlaps and keeps the
80 e820_add(u64 start, u64 size, u32 type)
82 dprintf(8, "Add to e820 map: %08llx %08llx %d\n", start, size, type);
85 // Huh? Nothing to do.
88 // Find position of new item (splitting existing item if needed).
89 u64 end = start + size;
91 for (i=0; i<e820_count; i++) {
92 struct e820entry *e = &e820_list[i];
93 u64 e_end = e->start + e->size;
96 // Found position - check if an existing item needs to be split.
97 if (start > e->start) {
98 if (type == e->type) {
99 // Same type - merge them.
100 size += start - e->start;
103 // Split existing item.
104 e->size = start - e->start;
107 insert_e820(i, end, e_end - end, e->type);
112 // Remove/adjust existing items that are overlapping.
113 while (i<e820_count) {
114 struct e820entry *e = &e820_list[i];
116 // No overlap - done.
118 u64 e_end = e->start + e->size;
120 // Existing item completely overlapped - remove it.
124 // Not completely overlapped - adjust its start.
126 e->size = e_end - end;
127 if (type == e->type) {
128 // Same type - merge them.
135 if (type != E820_HOLE)
136 insert_e820(i, start, size, type);
140 // Remove any definitions in a memory range (make a memory hole).
142 e820_remove(u64 start, u64 size)
144 e820_add(start, size, E820_HOLE);
147 // Report on final memory locations.