1 | /* $Id: bs3-rm-InitMemory.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3InitMemory
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define BS3_USE_RM_TEXT_SEG 1
|
---|
32 | #include "bs3kit-template-header.h"
|
---|
33 | #include "bs3-cmn-memory.h"
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <VBox/VMMDevTesting.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 |
|
---|
42 | typedef struct INT15E820ENTRY
|
---|
43 | {
|
---|
44 | uint64_t uBaseAddr;
|
---|
45 | uint64_t cbRange;
|
---|
46 | /** Memory type this entry describes, see INT15E820_TYPE_XXX. */
|
---|
47 | uint32_t uType;
|
---|
48 | uint32_t fAcpi3;
|
---|
49 | } INT15E820ENTRY;
|
---|
50 | AssertCompileSize(INT15E820ENTRY,24);
|
---|
51 |
|
---|
52 |
|
---|
53 | /** @name INT15E820_TYPE_XXX - Memory types returned by int 15h function 0xe820.
|
---|
54 | * @{ */
|
---|
55 | #define INT15E820_TYPE_USABLE 1 /**< Usable RAM. */
|
---|
56 | #define INT15E820_TYPE_RESERVED 2 /**< Reserved by the system, unusable. */
|
---|
57 | #define INT15E820_TYPE_ACPI_RECLAIMABLE 3 /**< ACPI reclaimable memory, whatever that means. */
|
---|
58 | #define INT15E820_TYPE_ACPI_NVS 4 /**< ACPI non-volatile storage? */
|
---|
59 | #define INT15E820_TYPE_BAD 5 /**< Bad memory, unusable. */
|
---|
60 | /** @} */
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Performs a int 15h function 0xe820 call.
|
---|
65 | *
|
---|
66 | * @returns Continuation value on success, 0 on failure.
|
---|
67 | * (Because of the way the API works, EBX should never be zero when
|
---|
68 | * data is returned.)
|
---|
69 | * @param pEntry The return buffer.
|
---|
70 | * @param cbEntry The size of the buffer (min 20 bytes).
|
---|
71 | * @param uContinuationValue Zero the first time, the return value from the
|
---|
72 | * previous call after that.
|
---|
73 | */
|
---|
74 | BS3_DECL(uint32_t) Bs3BiosInt15hE820(INT15E820ENTRY BS3_FAR *pEntry, size_t cbEntry, uint32_t uContinuationValue);
|
---|
75 | #pragma aux Bs3BiosInt15hE820 = \
|
---|
76 | ".386" \
|
---|
77 | "shl ebx, 10h" \
|
---|
78 | "mov bx, ax" /* ebx = continutation */ \
|
---|
79 | "movzx ecx, cx" \
|
---|
80 | "movzx edi, di" \
|
---|
81 | "mov edx, 0534d4150h" /*SMAP*/ \
|
---|
82 | "mov eax, 0xe820" \
|
---|
83 | "int 15h" \
|
---|
84 | "jc failed" \
|
---|
85 | "cmp eax, 0534d4150h" \
|
---|
86 | "jne failed" \
|
---|
87 | "cmp cx, 20" \
|
---|
88 | "jb failed" \
|
---|
89 | "mov ax, bx" \
|
---|
90 | "shr ebx, 10h" /* ax:bx = continuation */ \
|
---|
91 | "jmp done" \
|
---|
92 | "failed:" \
|
---|
93 | "xor ax, ax" \
|
---|
94 | "xor bx, bx" \
|
---|
95 | "done:" \
|
---|
96 | parm [es di] [cx] [ax bx] \
|
---|
97 | value [ax bx] \
|
---|
98 | modify exact [ax bx cx dx di es];
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Performs a int 15h function 0x88 call.
|
---|
102 | *
|
---|
103 | * @returns UINT32_MAX on failure, number of KBs above 1MB otherwise.
|
---|
104 | */
|
---|
105 | BS3_DECL(uint32_t) Bs3BiosInt15h88(void);
|
---|
106 | #pragma aux Bs3BiosInt15h88 = \
|
---|
107 | ".286" \
|
---|
108 | "clc" \
|
---|
109 | "mov ax, 08800h" \
|
---|
110 | "int 15h" \
|
---|
111 | "jc failed" \
|
---|
112 | "xor dx, dx" \
|
---|
113 | "jmp done" \
|
---|
114 | "failed:" \
|
---|
115 | "xor ax, ax" \
|
---|
116 | "dec ax" \
|
---|
117 | "mov dx, ax" \
|
---|
118 | "done:" \
|
---|
119 | value [ax dx] \
|
---|
120 | modify exact [ax bx cx dx es];
|
---|
121 |
|
---|
122 |
|
---|
123 | /*********************************************************************************************************************************
|
---|
124 | * Global Variables *
|
---|
125 | *********************************************************************************************************************************/
|
---|
126 | /** Slab control structure for the 4K management of low memory (< 1MB). */
|
---|
127 | BS3SLABCTLLOW g_Bs3Mem4KLow;
|
---|
128 | /** Slab control structure for the 4K management of tiled upper memory,
|
---|
129 | * between 1 MB and 16MB. */
|
---|
130 | BS3SLABCTLUPPERTILED g_Bs3Mem4KUpperTiled;
|
---|
131 |
|
---|
132 |
|
---|
133 | /** Translates a power of two request size to an slab list index. */
|
---|
134 | uint8_t const g_aiBs3SlabListsByPowerOfTwo[12] =
|
---|
135 | {
|
---|
136 | /* 2^0 = 1 */ 0,
|
---|
137 | /* 2^1 = 2 */ 0,
|
---|
138 | /* 2^2 = 4 */ 0,
|
---|
139 | /* 2^3 = 8 */ 0,
|
---|
140 | /* 2^4 = 16 */ 0,
|
---|
141 | /* 2^5 = 32 */ 1,
|
---|
142 | /* 2^6 = 64 */ 2,
|
---|
143 | /* 2^7 = 128 */ 3,
|
---|
144 | /* 2^8 = 256 */ 4,
|
---|
145 | /* 2^9 = 512 */ 5,
|
---|
146 | /* 2^10 = 1024 */ -1
|
---|
147 | /* 2^11 = 2048 */ -1
|
---|
148 | };
|
---|
149 |
|
---|
150 | /** The slab list chunk sizes. */
|
---|
151 | uint16_t const g_acbBs3SlabLists[BS3_MEM_SLAB_LIST_COUNT] =
|
---|
152 | {
|
---|
153 | 16,
|
---|
154 | 32,
|
---|
155 | 64,
|
---|
156 | 128,
|
---|
157 | 256,
|
---|
158 | 512,
|
---|
159 | };
|
---|
160 |
|
---|
161 | /** Low memory slab lists, sizes given by g_acbBs3SlabLists. */
|
---|
162 | BS3SLABHEAD g_aBs3LowSlabLists[BS3_MEM_SLAB_LIST_COUNT];
|
---|
163 | /** Upper tiled memory slab lists, sizes given by g_acbBs3SlabLists. */
|
---|
164 | BS3SLABHEAD g_aBs3UpperTiledSlabLists[BS3_MEM_SLAB_LIST_COUNT];
|
---|
165 |
|
---|
166 | /** Slab control structure sizes for the slab lists.
|
---|
167 | * This is to help the allocator when growing a list. */
|
---|
168 | uint16_t const g_cbBs3SlabCtlSizesforLists[BS3_MEM_SLAB_LIST_COUNT] =
|
---|
169 | {
|
---|
170 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 16 / 8 /*=32*/), 16),
|
---|
171 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 32 / 8 /*=16*/), 32),
|
---|
172 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 64 / 8 /*=8*/), 64),
|
---|
173 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 128 / 8 /*=4*/), 128),
|
---|
174 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 256 / 8 /*=2*/), 256),
|
---|
175 | RT_ALIGN(sizeof(BS3SLABCTL) - 4 + (4096 / 512 / 8 /*=1*/), 512),
|
---|
176 | };
|
---|
177 |
|
---|
178 |
|
---|
179 | /** The last RAM address below 4GB (approximately). */
|
---|
180 | uint32_t g_uBs3EndOfRamBelow4G = 0;
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Adds a range of memory to the tiled slabs.
|
---|
186 | *
|
---|
187 | * @param uRange Start of range.
|
---|
188 | * @param cbRange Size of range.
|
---|
189 | */
|
---|
190 | static void bs3InitMemoryAddRange32(uint32_t uRange, uint32_t cbRange)
|
---|
191 | {
|
---|
192 | uint32_t uRangeEnd = uRange + cbRange;
|
---|
193 | if (uRangeEnd < uRange)
|
---|
194 | uRangeEnd = UINT32_MAX;
|
---|
195 |
|
---|
196 | /* Raise the end-of-ram-below-4GB marker? */
|
---|
197 | if (uRangeEnd > g_uBs3EndOfRamBelow4G)
|
---|
198 | g_uBs3EndOfRamBelow4G = uRangeEnd;
|
---|
199 |
|
---|
200 | /* Applicable to tiled memory? */
|
---|
201 | if ( uRange < BS3_SEL_TILED_AREA_SIZE
|
---|
202 | && ( uRange >= _1M
|
---|
203 | || uRangeEnd >= _1M))
|
---|
204 | {
|
---|
205 | uint16_t cPages;
|
---|
206 |
|
---|
207 | /* Adjust the start of the range such that it's at or above 1MB and page aligned. */
|
---|
208 | if (uRange < _1M)
|
---|
209 | {
|
---|
210 | cbRange -= _1M - uRange;
|
---|
211 | uRange = _1M;
|
---|
212 | }
|
---|
213 | else if (uRange & (_4K - 1U))
|
---|
214 | {
|
---|
215 | cbRange -= uRange & (_4K - 1U);
|
---|
216 | uRange = RT_ALIGN_32(uRange, _4K);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /* Adjust the end/size of the range such that it's page aligned and not beyond the tiled area. */
|
---|
220 | if (uRangeEnd > BS3_SEL_TILED_AREA_SIZE)
|
---|
221 | {
|
---|
222 | cbRange -= uRangeEnd - BS3_SEL_TILED_AREA_SIZE;
|
---|
223 | uRangeEnd = BS3_SEL_TILED_AREA_SIZE;
|
---|
224 | }
|
---|
225 | else if (uRangeEnd & (_4K - 1U))
|
---|
226 | {
|
---|
227 | cbRange -= uRangeEnd & (_4K - 1U);
|
---|
228 | uRangeEnd &= ~(uint32_t)(_4K - 1U);
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* If there is still something, enable it.
|
---|
232 | (We're a bit paranoid here don't trust the BIOS to only report a page once.) */
|
---|
233 | cPages = cbRange >> 12; /*div 4K*/
|
---|
234 | if (cPages)
|
---|
235 | {
|
---|
236 | unsigned i;
|
---|
237 | uRange -= _1M;
|
---|
238 | i = uRange >> 12; /*div _4K*/
|
---|
239 | while (cPages-- > 0)
|
---|
240 | {
|
---|
241 | uint16_t uLineToLong = ASMBitTestAndClear(g_Bs3Mem4KUpperTiled.Core.bmAllocated, i);
|
---|
242 | g_Bs3Mem4KUpperTiled.Core.cFreeChunks += uLineToLong;
|
---|
243 | i++;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | BS3_DECL(void) BS3_FAR_CODE Bs3InitMemory_rm_far(void)
|
---|
251 | {
|
---|
252 | uint16_t i;
|
---|
253 | uint16_t cPages;
|
---|
254 | uint32_t u32;
|
---|
255 | INT15E820ENTRY Entry;
|
---|
256 | uint32_t BS3_FAR *pu32Mmio;
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Enable the A20 gate.
|
---|
260 | */
|
---|
261 | Bs3A20Enable();
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * Low memory (4K chunks).
|
---|
265 | * - 0x00000 to 0x004ff - Interrupt Vector table, BIOS data area.
|
---|
266 | * - 0x01000 to 0x0ffff - Stacks.
|
---|
267 | * - 0x10000 to 0x1yyyy - BS3TEXT16
|
---|
268 | * - 0x20000 to 0x26fff - BS3SYSTEM16
|
---|
269 | * - 0x29000 to 0xzzzzz - BS3DATA16, BS3TEXT32, BS3TEXT64, BS3DATA32, BS3DATA64 (in that order).
|
---|
270 | * - 0xzzzzZ to 0x9fdff - Free conventional memory.
|
---|
271 | * - 0x9fc00 to 0x9ffff - Extended BIOS data area (exact start may vary).
|
---|
272 | * - 0xa0000 to 0xbffff - VGA MMIO
|
---|
273 | * - 0xc0000 to 0xc7fff - VGA BIOS
|
---|
274 | * - 0xc8000 to 0xeffff - ROMs, tables, unusable.
|
---|
275 | * - 0xf0000 to 0xfffff - PC BIOS.
|
---|
276 | */
|
---|
277 | Bs3SlabInit(&g_Bs3Mem4KLow.Core, sizeof(g_Bs3Mem4KLow), 0 /*uFlatSlabPtr*/, 0xA0000 /* 640 KB*/, _4K);
|
---|
278 |
|
---|
279 | /* Mark the stacks and whole image as allocated. */
|
---|
280 | cPages = (Bs3TotalImageSize + _4K - 1U) >> 12;
|
---|
281 | ASMBitSetRange(g_Bs3Mem4KLow.Core.bmAllocated, 0, 0x10 + cPages);
|
---|
282 |
|
---|
283 | /* Mark any unused pages between BS3TEXT16 and BS3SYSTEM16 as free. */
|
---|
284 | cPages = (Bs3Text16_Size + (uint32_t)_4K - 1U) >> 12;
|
---|
285 | ASMBitClearRange(g_Bs3Mem4KLow.Core.bmAllocated, 0x10U + cPages, 0x20U);
|
---|
286 |
|
---|
287 | /* In case the system has less than 640KB of memory, check the BDA variable for it. */
|
---|
288 | cPages = *(uint16_t BS3_FAR *)BS3_FP_MAKE(0x0000, 0x0413); /* KB of low memory */
|
---|
289 | if (cPages < 640)
|
---|
290 | {
|
---|
291 | cPages = 640 - cPages;
|
---|
292 | cPages = RT_ALIGN(cPages, 4);
|
---|
293 | cPages >>= 2;
|
---|
294 | ASMBitSetRange(g_Bs3Mem4KLow.Core.bmAllocated, 0xA0 - cPages, 0xA0);
|
---|
295 | }
|
---|
296 | else
|
---|
297 | ASMBitSet(g_Bs3Mem4KLow.Core.bmAllocated, 0x9F);
|
---|
298 |
|
---|
299 | /* Recalc free pages. */
|
---|
300 | cPages = 0;
|
---|
301 | i = g_Bs3Mem4KLow.Core.cChunks;
|
---|
302 | while (i-- > 0)
|
---|
303 | cPages += !ASMBitTest(g_Bs3Mem4KLow.Core.bmAllocated, i);
|
---|
304 | g_Bs3Mem4KLow.Core.cFreeChunks = cPages;
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * First 16 MB of memory above 1MB. We start out by marking it all allocated.
|
---|
308 | */
|
---|
309 | Bs3SlabInit(&g_Bs3Mem4KUpperTiled.Core, sizeof(g_Bs3Mem4KUpperTiled), _1M, BS3_SEL_TILED_AREA_SIZE - _1M, _4K);
|
---|
310 |
|
---|
311 | ASMBitSetRange(g_Bs3Mem4KUpperTiled.Core.bmAllocated, 0, g_Bs3Mem4KUpperTiled.Core.cChunks);
|
---|
312 | g_Bs3Mem4KUpperTiled.Core.cFreeChunks = 0;
|
---|
313 |
|
---|
314 | /* Ask the BIOS about where there's memory, and make pages in between 1MB
|
---|
315 | and BS3_SEL_TILED_AREA_SIZE present. This means we're only interested
|
---|
316 | in entries describing usable memory, ASSUMING of course no overlaps. */
|
---|
317 | if ( (g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80386
|
---|
318 | && Bs3BiosInt15hE820(&Entry, sizeof(Entry), 0) != 0)
|
---|
319 | {
|
---|
320 | uint32_t uCont = 0;
|
---|
321 | i = 0;
|
---|
322 | while ( (uCont = Bs3BiosInt15hE820(&Entry, sizeof(Entry), uCont)) != 0
|
---|
323 | && i++ < 2048)
|
---|
324 | if (Entry.uType == INT15E820_TYPE_USABLE)
|
---|
325 | if (!(Entry.uBaseAddr >> 32))
|
---|
326 | /* Convert from 64-bit to 32-bit value and record it. */
|
---|
327 | bs3InitMemoryAddRange32((uint32_t)Entry.uBaseAddr,
|
---|
328 | (Entry.cbRange >> 32) ? UINT32_C(0xfffff000) : (uint32_t)Entry.cbRange);
|
---|
329 | }
|
---|
330 | /* Try the 286+ API for getting memory above 1MB and (usually) below 16MB. */
|
---|
331 | else if ( (g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80386
|
---|
332 | && (u32 = Bs3BiosInt15h88()) != UINT32_MAX
|
---|
333 | && u32 > 0)
|
---|
334 | bs3InitMemoryAddRange32(_1M, u32 * _1K);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Check if we've got the VMMDev MMIO testing memory mapped above 1MB.
|
---|
338 | */
|
---|
339 | pu32Mmio = (uint32_t BS3_FAR *)BS3_FP_MAKE(VMMDEV_TESTING_MMIO_RM_SEL,
|
---|
340 | VMMDEV_TESTING_MMIO_RM_OFF2(VMMDEV_TESTING_MMIO_OFF_NOP));
|
---|
341 | if (*pu32Mmio == VMMDEV_TESTING_NOP_RET)
|
---|
342 | {
|
---|
343 | Bs3Printf("Memory: Found VMMDev MMIO testing region\n");
|
---|
344 | if (!ASMBitTestAndSet(g_Bs3Mem4KUpperTiled.Core.bmAllocated, 1))
|
---|
345 | g_Bs3Mem4KUpperTiled.Core.cFreeChunks--;
|
---|
346 |
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Initialize the slab lists.
|
---|
351 | */
|
---|
352 | for (i = 0; i < BS3_MEM_SLAB_LIST_COUNT; i++)
|
---|
353 | {
|
---|
354 | Bs3SlabListInit(&g_aBs3LowSlabLists[i], g_acbBs3SlabLists[i]);
|
---|
355 | Bs3SlabListInit(&g_aBs3UpperTiledSlabLists[i], g_acbBs3SlabLists[i]);
|
---|
356 | }
|
---|
357 |
|
---|
358 | #if 0
|
---|
359 | /*
|
---|
360 | * For debugging.
|
---|
361 | */
|
---|
362 | Bs3Printf("Memory-low: %u/%u chunks bmAllocated[]=", g_Bs3Mem4KLow.Core.cFreeChunks, g_Bs3Mem4KLow.Core.cChunks);
|
---|
363 | for (i = 0; i < 20; i++)
|
---|
364 | Bs3Printf("%02x ", g_Bs3Mem4KLow.Core.bmAllocated[i]);
|
---|
365 | Bs3Printf("\n");
|
---|
366 | Bs3Printf("Memory-upt: %u/%u chunks bmAllocated[]=", g_Bs3Mem4KUpperTiled.Core.cFreeChunks, g_Bs3Mem4KUpperTiled.Core.cChunks);
|
---|
367 | for (i = 0; i < 32; i++)
|
---|
368 | Bs3Printf("%02x ", g_Bs3Mem4KUpperTiled.Core.bmAllocated[i]);
|
---|
369 | Bs3Printf("...\n");
|
---|
370 | #endif
|
---|
371 | }
|
---|
372 |
|
---|