Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 58777)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 58785)
@@ -396,5 +396,6 @@
 bs3kit-common-16_ASDEFS   = RT_ASMDEFS_INC_FIRST_FILE
 bs3kit-common-16_SOURCES  = $(VBOX_BS3KIT_COMMON_SOURCES) \
-	bs3-system-data.asm
+	bs3-system-data.asm \
+	bs3-rm-InitMemory.c
 
 # The 32-bit BS3Kit library.
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-memory.h
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-memory.h	(revision 58785)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-memory.h	(revision 58785)
@@ -0,0 +1,63 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Internal Memory Structures, Variables and Functions.
+ */
+
+/*
+ * Copyright (C) 2007-2015 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+#ifndef ___bs3_cmn_memory_h
+#define ___bs3_cmn_memory_h
+
+#include "bs3kit.h"
+
+RT_C_DECLS_BEGIN;
+
+
+typedef union BS3SLABCLTLOW
+{
+    BS3SLABCLT  Core;
+    uint32_t    au32Alloc[(sizeof(BS3SLABCLT) + (0xA0000 / _4K / 8) ) / 4];
+} BS3SLABCLTLOW;
+extern BS3SLABCLTLOW            BS3_DATA_NM(g_LowMemory);
+
+
+typedef union BS3SLABCTLUPPERTILED
+{
+    BS3SLABCLT  Core;
+    uint32_t    au32Alloc[(sizeof(BS3SLABCLT) + ((BS3_SEL_TILED_AREA_SIZE - _1M) / _4K / 8) ) / 4];
+} BS3SLABCTLUPPERTILED;
+extern BS3SLABCTLUPPERTILED     BS3_DATA_NM(g_UpperTiledMemory);
+
+
+/** The number of chunk sizes used by the slab list arrays
+ * (g_aBs3LowSlabLists, g_aBs3UpperTiledSlabLists, more?). */
+#define BS3_MEM_SLAB_LIST_COUNT 6
+
+extern uint16_t const           BS3_DATA_NM(g_acbBs3SlabLists)[BS3_MEM_SLAB_LIST_COUNT];
+extern BS3SLABHEAD              BS3_DATA_NM(g_aBs3LowSlabLists)[BS3_MEM_SLAB_LIST_COUNT];
+extern BS3SLABHEAD              BS3_DATA_NM(g_aBs3UpperTiledSlabLists)[BS3_MEM_SLAB_LIST_COUNT];
+
+
+RT_C_DECLS_END;
+
+#endif
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 58777)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 58785)
@@ -44,4 +44,7 @@
 section BS3TEXT16_END   align=2 CLASS=BS3CODE16 PUBLIC USE16
 %endif
+
+BS3_GLOBAL_DATA Bs3Text16_Size, 2
+    dw  BS3_DATA_NM(Bs3Text16_EndOfSegment) wrt BS3TEXT16
 BS3_GLOBAL_DATA Bs3Text16_EndOfSegment, 0
 
@@ -65,4 +68,5 @@
 section BS3DATA16_END   align=2   CLASS=FAR_DATA PUBLIC USE16
 %endif
+
 BS3_GLOBAL_DATA Bs3Data16_EndOfSegment, 0
 
@@ -81,4 +85,6 @@
 section BS3TEXT32_END   align=1 CLASS=BS3CODE32 PUBLIC USE32 FLAT
 %endif
+BS3_GLOBAL_DATA Bs3Data16_Size, 4
+    dd  BS3_DATA_NM(Bs3Data16_EndOfSegment) wrt BS3DATA16
 BS3_GLOBAL_DATA Bs3Text32_EndOfSegment, 0
 
@@ -115,4 +121,11 @@
 section BS3DATA64_END   align=16   CLASS=DATA PUBLIC USE32 FLAT
 %endif
+
+ALIGNDATA(16)
+    db      10,13,'eye-catcher: sizes  ',10,13
+BS3_GLOBAL_DATA Bs3Data16Thru64Text32And64_TotalSize, 4
+    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt BS3DATA16
+BS3_GLOBAL_DATA Bs3TotalImageSize, 4
+    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt BS3TEXT16
 BS3_GLOBAL_DATA Bs3Data64_EndOfSegment, 0
 
@@ -140,12 +153,4 @@
     db      10,13,'eye-catcher: BS3TEXT16',10,13
 .after_eye_catcher:
-mov     ax, BS3SYSTEM16
-mov     ds, ax
-lgdt    [BS3_DATA_NM(Bs3Lgdt_Gdt)]
-mov ax, X86_CR0_PE
-lmsw ax
-cli
-hlt
-
     mov     ax, BS3DATA16
     mov     es, ax
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-rm-InitMemory.c
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-rm-InitMemory.c	(revision 58785)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-rm-InitMemory.c	(revision 58785)
@@ -0,0 +1,257 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Bs3InitMemory
+ */
+
+/*
+ * Copyright (C) 2007-2015 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+/*********************************************************************************************************************************
+*   Header Files                                                                                                                 *
+*********************************************************************************************************************************/
+#include "bs3kit-template-header.h"
+#include "bs3-cmn-memory.h"
+#include <iprt/asm.h>
+
+
+/*********************************************************************************************************************************
+*   Structures and Typedefs                                                                                                      *
+*********************************************************************************************************************************/
+
+typedef struct INT15E820ENTRY
+{
+    uint64_t    uBaseAddr;
+    uint64_t    cbRange;
+    /** Memory type this entry describes, see INT15E820_TYPE_XXX. */
+    uint32_t    uType;
+    uint32_t    fAcpi3;
+} INT15E820ENTRY;
+AssertCompileSize(INT15E820ENTRY,24);
+
+
+/** @name INT15E820_TYPE_XXX - Memory types returned by int 15h function 0xe820.
+ * @{ */
+#define INT15E820_TYPE_USABLE               1 /**< Usable RAM. */
+#define INT15E820_TYPE_RESERVED             2 /**< Reserved by the system, unusable. */
+#define INT15E820_TYPE_ACPI_RECLAIMABLE     3 /**< ACPI reclaimable memory, whatever that means. */
+#define INT15E820_TYPE_ACPI_NVS             4 /**< ACPI non-volatile storage? */
+#define INT15E820_TYPE_BAD                  5 /**< Bad memory, unusable. */
+/** @} */
+
+
+/**
+ * Performs a int 15h function 0xe820 call.
+ *
+ * @returns Continuation value on success, 0 on failure.
+ *          (Because of the way the API works, EBX should never be zero when
+ *          data is returned.)
+ * @param   pEntry              The return buffer.
+ * @param   cbEntry             The size of the buffer (min 20 bytes).
+ * @param   uContinuationValue  Zero the first time, the return value from the
+ *                              previous call after that.
+ */
+BS3_DECL(uint32_t) Bs3BiosInt15hE820(INT15E820ENTRY BS3_FAR *pEntry, size_t cbEntry, uint32_t uContinuationValue);
+#pragma aux Bs3BiosInt15hE820 = \
+    "shl    ebx, 10h" \
+    "mov    bx, ax" /* ebx = continutation */ \
+    "movzx  ecx, cx" \
+    "movzx  edi, di" \
+    "mov    edx, 0534d4150h" /*SMAP*/ \
+    "mov    eax, 0xe820" \
+    "int    15h" \
+    "jc     failed" \
+    "cmp    eax, 0534d4150h" \
+    "jne    failed" \
+    "cmp    cx, 20" \
+    "jb     failed" \
+    "mov    ax, bx" \
+    "shr    ebx, 10h" /* ax:bx = continuation */ \
+    "jmp    done" \
+    "failed:" \
+    "xor    ax, ax" \
+    "xor    bx, bx" \
+    "done:" \
+    parm [es di] [cx] [ax bx] \
+    value [ax bx] \
+    modify exact [ax bx cx dx di es];
+
+
+/*********************************************************************************************************************************
+*   Global Variables                                                                                                             *
+*********************************************************************************************************************************/
+/** Slab control structure for the 4K management of low memory (< 1MB). */
+BS3SLABCLTLOW           BS3_DATA_NM(g_Bs3Mem4KLow);
+/** Slab control structure for the 4K management of tiled upper memory,
+ *  between 1 MB and 16MB. */
+BS3SLABCTLUPPERTILED    BS3_DATA_NM(g_Bs3Mem4KUpperTiled);
+
+/** The slab list chunk sizes. */
+uint16_t const          BS3_DATA_NM(g_acbBs3SlabLists)[BS3_MEM_SLAB_LIST_COUNT] =
+{
+    16,
+    32,
+    64,
+    128,
+    256,
+    512,
+};
+/** Low memory slab lists, sizes given by g_acbBs3SlabLists. */
+BS3SLABHEAD             BS3_DATA_NM(g_aBs3LowSlabLists)[BS3_MEM_SLAB_LIST_COUNT];
+/** Upper tiled memory slab lists, sizes given by g_acbBs3SlabLists. */
+BS3SLABHEAD             BS3_DATA_NM(g_aBs3UpperTiledSlabLists)[BS3_MEM_SLAB_LIST_COUNT];
+
+
+
+BS3_DECL(void) Bs3InitMemory_rm(void)
+{
+    uint16_t        i;
+    uint16_t        cPages;
+    INT15E820ENTRY  Entry;
+
+    /*
+     * Low memory (4K chunks).
+     *      - 0x00000 to 0x004ff - Interrupt Vector table, BIOS data area.
+     *      - 0x01000 to 0x0ffff - Stacks.
+     *      - 0x10000 to 0x1yyyy - BS3TEXT16
+     *      - 0x20000 to 0x26fff - BS3SYSTEM16
+     *      - 0x27000 to 0xzzzzz - BS3DATA16, BS3TEXT32, BS3TEXT64, BS3DATA32, BS3DATA64 (in that order).
+     *      - 0xzzzzZ to 0x9fdff - Free conventional memory.
+     *      - 0x9fc00 to 0x9ffff - Extended BIOS data area (exact start may vary).
+     *      - 0xa0000 to 0xbffff - VGA MMIO
+     *      - 0xc0000 to 0xc7fff - VGA BIOS
+     *      - 0xc8000 to 0xeffff - ROMs, tables, unusable.
+     *      - 0xf0000 to 0xfffff - PC BIOS.
+     */
+    Bs3SlabInit(&g_Bs3Mem4KLow.Core, sizeof(g_Bs3Mem4KLow), 0 /*uFlatSlabPtr*/, 0xA0000 /* 640 KB*/, _4K);
+
+    /* Mark the stacks and whole image as allocated. */
+    cPages  = (BS3_DATA_NM(Bs3TotalImageSize) + _4K - 1U) >> 12;
+    ASMBitSetRange(g_Bs3Mem4KLow.Core.bmAllocated, 0, 0x10 + cPages);
+
+    /* Mark any unused pages between BS3TEXT16 and BS3SYSTEM16 as free. */
+    cPages = (BS3_DATA_NM(Bs3Text16_Size) + _4K - 1U) >> 12;
+    ASMBitClearRange(g_Bs3Mem4KLow.Core.bmAllocated, 0x10U + cPages, 0x20U);
+
+    /* In case the system has less than 640KB of memory, check the BDA variable for it. */
+    cPages = *(uint16_t BS3_FAR *)BS3_FP_MAKE(0x0000, 0x0413); /* KB of low memory */
+    if (cPages < 640)
+    {
+        cPages = 640 - cPages;
+        cPages >>= 2;
+        ASMBitSetRange(g_Bs3Mem4KLow.Core.bmAllocated, 0xA0 - cPages, 0xA0);
+    }
+    else
+        ASMBitSet(g_Bs3Mem4KLow.Core.bmAllocated, 0x9F);
+
+    /* Recalc free pages. */
+    cPages = 0;
+    i = g_Bs3Mem4KLow.Core.cChunks;
+    while (i-- > 0)
+        cPages += ASMBitTest(g_Bs3Mem4KLow.Core.bmAllocated, i);
+    g_Bs3Mem4KLow.Core.cFreeChunks = cPages;
+
+
+    /*
+     * First 16 MB of memory above 1MB.  We start out by marking it all allocated.
+     */
+    Bs3SlabInit(&g_Bs3Mem4KUpperTiled.Core, sizeof(g_Bs3Mem4KUpperTiled), _1M, BS3_SEL_TILED_AREA_SIZE - _1M, _4K);
+
+    ASMBitSetRange(g_Bs3Mem4KUpperTiled.Core.bmAllocated, 0, g_Bs3Mem4KUpperTiled.Core.cChunks);
+    g_Bs3Mem4KUpperTiled.Core.cFreeChunks = 0;
+
+    /* Ask the BIOS about where there's memory, and make pages in between 1MB
+       and BS3_SEL_TILED_AREA_SIZE present.  This means we're only interested
+       in entries describing usable memory, ASSUMING of course no overlaps. */
+    if (Bs3BiosInt15hE820(&Entry, sizeof(Entry), 0) != 0)
+    {
+        uint32_t uCont = 0;
+        i = 0;
+        while (   (uCont = Bs3BiosInt15hE820(&Entry, sizeof(Entry), uCont)) != 0
+               && i++ < 2048)
+        {
+            if (Entry.uType == INT15E820_TYPE_USABLE)
+            {
+                if (Entry.uBaseAddr < BS3_SEL_TILED_AREA_SIZE)
+                {
+                    /* Entry concerning tiled memory. Convert from 64-bit to 32-bit
+                       values and check whether it's concerning anything at or above 1MB  */
+                    uint32_t uRange    = (uint32_t)Entry.uBaseAddr;
+                    uint32_t cbRange   = Entry.cbRange >= BS3_SEL_TILED_AREA_SIZE
+                                       ? BS3_SEL_TILED_AREA_SIZE : (uint32_t)Entry.cbRange;
+                    uint32_t uRangeEnd = uRange + cbRange;
+                    AssertCompile(BS3_SEL_TILED_AREA_SIZE <= _512M /* the range of 16-bit cPages. */ );
+                    if (   uRange >= _1M
+                        || uRangeEnd > _1M)
+                    {
+                        /* Adjust the start of the range such that it's at or above 1MB and page aligned.  */
+                        if (uRange < _1M)
+                        {
+                            cbRange -= _1M - uRange;
+                            uRange   = _1M;
+                        }
+                        else if (uRange & (_4K - 1U))
+                        {
+                            cbRange -= uRange & (_4K - 1U);
+                            uRange   = RT_ALIGN_32(uRange, _4K);
+                        }
+
+                        /* Adjust the end/size of the range such that it's page aligned and not beyond the tiled area. */
+                        if (uRangeEnd > BS3_SEL_TILED_AREA_SIZE)
+                        {
+                            cbRange  -= uRangeEnd - BS3_SEL_TILED_AREA_SIZE;
+                            uRangeEnd = BS3_SEL_TILED_AREA_SIZE;
+                        }
+                        else if (uRangeEnd & (_4K - 1U))
+                        {
+                            cbRange   -= uRangeEnd & (_4K - 1U);
+                            uRangeEnd &= ~(uint32_t)(_4K - 1U);
+                        }
+
+                        /* If there is still something, enable it.
+                           (We're a bit paranoid here don't trust the BIOS to only report a page once.)  */
+                        cPages = cbRange >> 12; /*div 4K*/
+                        if (cPages)
+                        {
+                            uRange -= _1M;
+                            i = uRange >> 12; /*div _4K*/
+                            while (cPages-- > 0)
+                            {
+                                g_Bs3Mem4KUpperTiled.Core.cFreeChunks += !ASMBitTestAndSet(g_Bs3Mem4KUpperTiled.Core.bmAllocated,
+                                                                                            i);
+                                i++;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /*
+     * Initialize the slab lists.
+     */
+    for (i = 0; i < BS3_MEM_SLAB_LIST_COUNT; i++)
+    {
+        Bs3SlabListInit(&g_aBs3LowSlabLists[i], g_acbBs3SlabLists[i]);
+        Bs3SlabListInit(&g_aBs3UpperTiledSlabLists[i], g_acbBs3SlabLists[i]);
+    }
+}
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c	(revision 58777)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c	(revision 58785)
@@ -11,6 +11,9 @@
 BS3_DECL(void) Main_rm(void)
 {
+    Bs3InitMemory_rm();
+
     Bs3TestInit("bs3-shutdown");
 
+Bs3Panic();
     Bs3Shutdown();
     return;
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 58777)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 58785)
@@ -596,4 +596,50 @@
 
 
+/** @name Segment start and end markers, sizes.
+ * @{ */
+/** Start of the BS3TEXT16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text16_StartOfSegment);
+/** End of the BS3TEXT16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text16_EndOfSegment);
+/** The size of the BS3TEXT16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text16_Size);
+
+/** Start of the BS3SYSTEM16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3System16_StartOfSegment);
+/** End of the BS3SYSTEM16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3System16_EndOfSegment);
+
+/** Start of the BS3DATA16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data16_StartOfSegment);
+/** End of the BS3DATA16 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data16_EndOfSegment);
+
+/** Start of the BS3TEXT32 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text32_StartOfSegment);
+/** Start of the BS3TEXT32 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text32_EndOfSegment);
+
+/** Start of the BS3DATA32 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data32_StartOfSegment);
+/** Start of the BS3DATA32 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data32_EndOfSegment);
+
+/** Start of the BS3TEXT64 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text64_StartOfSegment);
+/** Start of the BS3TEXT64 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Text64_EndOfSegment);
+
+/** Start of the BS3DATA64 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data64_StartOfSegment);
+/** Start of the BS3DATA64 segment.   */
+extern uint8_t BS3_DATA_NM(Bs3Data64_EndOfSegment);
+
+/** The size of the Data16, Text32, Text64, Data32 and Data64 blob. */
+extern uint8_t BS3_DATA_NM(Bs3Data16Thru64Text32And64_TotalSize);
+/** The total image size (from Text16 thu Data64). */
+extern uint8_t BS3_DATA_NM(Bs3TotalImageSize);
+/** @} */
+
+
 #ifdef __WATCOMC__
 /**
@@ -1234,4 +1280,9 @@
 
 
+/**
+ * Initializes the REAL and TILED memory pools.
+ */
+BS3_DECL(void) Bs3InitMemory_rm(void);
+
 
 /** @defgroup grp_bs3kit_mode   Mode Specific Functions and Data
