1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* HVM e820 support.
*
* Leendert van Doorn, leendert@watson.ibm.com
* Copyright (c) 2005, International Business Machines Corporation.
* Copyright (c) 2006, Keir Fraser, XenSource Inc.
* Copyright (c) 2011, Citrix Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "util.h"
struct e820map memory_map;
void memory_map_setup(void)
{
unsigned int nr_entries = E820MAX, i;
int rc;
uint64_t alloc_addr = RESERVED_MEMORY_DYNAMIC_START;
uint64_t alloc_size = RESERVED_MEMORY_DYNAMIC_END - alloc_addr;
rc = get_mem_mapping_layout(memory_map.map, &nr_entries);
if ( rc || !nr_entries )
{
printf("Get guest memory maps[%d] failed. (%d)\n", nr_entries, rc);
BUG();
}
memory_map.nr_map = nr_entries;
for ( i = 0; i < nr_entries; i++ )
{
if ( memory_map.map[i].type == E820_RESERVED &&
check_overlap(alloc_addr, alloc_size,
memory_map.map[i].addr, memory_map.map[i].size) )
{
printf("Fail to setup memory map due to conflict");
printf(" on dynamic reserved memory range.\n");
BUG();
}
}
}
/*
* Sometimes hvmloader may have relocated RAM so low_mem_pgend/high_mem_end
* would be changed over there. But memory_map[] just records the
* original low/high memory, so we need to sync these entries once
* hvmloader modifies low/high memory.
*/
void adjust_memory_map(void)
{
uint32_t low_mem_end = hvm_info->low_mem_pgend << PAGE_SHIFT;
uint64_t high_mem_end = (uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT;
unsigned int i;
for ( i = 0; i < memory_map.nr_map; i++ )
{
uint64_t map_start = memory_map.map[i].addr;
uint64_t map_size = memory_map.map[i].size;
uint64_t map_end = map_start + map_size;
/* If we need to adjust lowmem. */
if ( memory_map.map[i].type == E820_RAM &&
low_mem_end > map_start && low_mem_end < map_end )
{
memory_map.map[i].size = low_mem_end - map_start;
continue;
}
/* Modify the existing highmem region if it exists. */
if ( memory_map.map[i].type == E820_RAM &&
high_mem_end && map_start == GB(4) )
{
if ( high_mem_end != map_end )
memory_map.map[i].size = high_mem_end - map_start;
high_mem_end = 0;
continue;
}
}
/* If there was no highmem region, just create one. */
if ( high_mem_end )
{
memory_map.map[i].addr = GB(4);
memory_map.map[i].size =
((uint64_t)hvm_info->high_mem_pgend << PAGE_SHIFT) -
memory_map.map[i].addr;
memory_map.map[i].type = E820_RAM;
memory_map.nr_map++;
}
}
void dump_e820_table(struct e820entry *e820, unsigned int nr)
{
uint64_t last_end = 0, start, end;
int i;
printf("E820 table:\n");
for ( i = 0; i < nr; i++ )
{
start = e820[i].addr;
end = e820[i].addr + e820[i].size;
if ( start < last_end )
printf(" OVERLAP!!\n");
else if ( start > last_end )
printf(" HOLE: %08x:%08x - %08x:%08x\n",
(uint32_t)(last_end >> 32), (uint32_t)last_end,
(uint32_t)(start >> 32), (uint32_t)start);
printf(" [%02d]: %08x:%08x - %08x:%08x: ", i,
(uint32_t)(start >> 32), (uint32_t)start,
(uint32_t)(end >> 32), (uint32_t)end);
switch ( e820[i].type )
{
case E820_RAM:
printf("RAM\n");
break;
case E820_RESERVED:
printf("RESERVED\n");
break;
case E820_ACPI:
printf("ACPI\n");
break;
case E820_NVS:
printf("NVS\n");
break;
default:
printf("UNKNOWN (%08x)\n", e820[i].type);
break;
}
last_end = end;
}
}
/* Create an E820 table based on memory parameters provided in hvm_info. */
int build_e820_table(struct e820entry *e820,
unsigned int lowmem_reserved_base,
unsigned int bios_image_base)
{
unsigned int nr = 0, i, j;
uint32_t low_mem_end = hvm_info->low_mem_pgend << PAGE_SHIFT;
unsigned long acpi_mem_end = acpi_enabled ?
ACPI_MEMORY_DYNAMIC_START + (acpi_pages_allocated() << PAGE_SHIFT) :
RESERVED_MEMBASE;
if ( !lowmem_reserved_base )
lowmem_reserved_base = 0xA0000;
/* Lowmem must be at least 512K to keep Windows happy) */
ASSERT ( lowmem_reserved_base > 512<<10 );
ASSERT ( bios_image_base < 0x100000 );
/*
* 0x0-lowmem_reserved_base: Ordinary RAM.
*/
e820[nr].addr = 0x00000;
e820[nr].size = lowmem_reserved_base;
e820[nr].type = E820_RAM;
nr++;
/* lowmem_reserved_base-0xA0000: reserved by BIOS implementation. */
if ( lowmem_reserved_base < 0xA0000 )
{
/* Reserved for internal use. */
e820[nr].addr = lowmem_reserved_base;
e820[nr].size = 0xA0000-lowmem_reserved_base;
e820[nr].type = E820_RESERVED;
nr++;
}
/*
* Following regions are standard regions of the PC memory map.
* They are not covered by e820 regions. OSes will not use as RAM.
* 0xA0000-0xC0000: VGA memory-mapped I/O. Not covered by E820.
* 0xC0000-0xE0000: 16-bit devices, expansion ROMs (inc. vgabios).
* TODO: free pages which turn out to be unused.
*/
/*
* BIOS region.
*/
e820[nr].addr = bios_image_base;
e820[nr].size = 0x100000-bios_image_base;
e820[nr].type = E820_RESERVED;
nr++;
/*
* Mark populated reserved memory that contains ACPI tables as ACPI NVS.
* That should help the guest to treat it correctly later: e.g. pass to
* the next kernel on kexec.
*
* Using NVS type instead of a regular one helps to prevent potential
* space reuse by an ACPI unaware / buggy bootloader, option ROM, etc.
* before an ACPI OS takes control. This is possible due to the fact that
* ACPI NVS memory is explicitly described as non-reclaimable in ACPI spec.
*/
if ( acpi_enabled )
{
e820[nr].addr = RESERVED_MEMBASE;
e820[nr].size = acpi_mem_end - RESERVED_MEMBASE;
e820[nr].type = E820_NVS;
nr++;
}
/*
* Explicitly reserve space for special pages.
* This space starts right after ACPI region (to avoid creating a hole that
* might be accidentally occupied by MMIO) and extends to cover various
* fixed hardware mappings (e.g., LAPIC, IOAPIC, default SVGA framebuffer).
*
* If igd_opregion_pgbase we need to split the RESERVED region in two.
*/
if ( igd_opregion_pgbase )
{
uint32_t igd_opregion_base = igd_opregion_pgbase << PAGE_SHIFT;
e820[nr].addr = acpi_mem_end;
e820[nr].size = igd_opregion_base - acpi_mem_end;
e820[nr].type = E820_RESERVED;
nr++;
e820[nr].addr = igd_opregion_base;
e820[nr].size = IGD_OPREGION_PAGES * PAGE_SIZE;
e820[nr].type = E820_NVS;
nr++;
e820[nr].addr = igd_opregion_base + IGD_OPREGION_PAGES * PAGE_SIZE;
e820[nr].size = (uint32_t)-e820[nr].addr;
e820[nr].type = E820_RESERVED;
nr++;
}
else
{
e820[nr].addr = acpi_mem_end;
e820[nr].size = (uint32_t)-e820[nr].addr;
e820[nr].type = E820_RESERVED;
nr++;
}
/* Low RAM goes here. Reserve space for special pages. */
BUG_ON(low_mem_end < MB(2));
/*
* Construct E820 table according to recorded memory map.
*
* The memory map created by toolstack may include,
*
* #1. Low memory region
*
* Low RAM starts at least from 1M to make sure all standard regions
* of the PC memory map, like BIOS, VGA memory-mapped I/O and vgabios,
* have enough space.
*
* #2. Reserved regions if they exist
*
* #3. High memory region if it exists
*
* Note we just have one low memory entry and one high mmeory entry if
* exists.
*/
for ( i = 0; i < memory_map.nr_map; i++ )
{
e820[nr] = memory_map.map[i];
nr++;
}
/* Finally we need to sort all e820 entries. */
for ( j = 0; j < nr - 1; j++ )
{
for ( i = j + 1; i < nr; i++ )
{
if ( e820[j].addr > e820[i].addr )
{
struct e820entry tmp = e820[j];
e820[j] = e820[i];
e820[i] = tmp;
}
}
}
return nr;
}
/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|