VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-rm-InitMemory.c@ 62410

Last change on this file since 62410 was 61385, checked in by vboxsync, 9 years ago

bs3-fpustate-1: Extended the tests to force the ring-0 and raw-mode VMM code to do interesting things.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette