VirtualBox

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

Last change on this file since 60462 was 60321, checked in by vboxsync, 9 years ago

bs3kit: 386 fixes.

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

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