Index: /trunk/src/VBox/ValidationKit/bootsectors/Config.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/Config.kmk	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/Config.kmk	(revision 59863)
@@ -343,5 +343,5 @@
 TEMPLATE_VBoxBS3KitImg_CXXFLAGS     = \
 	-nt=BS3TEXT16 -nd=BS3DATA16 -nc=BS3CODE16 -ecw -q -0 -wx -zl -zu -mc $(BS3_OW_DBG_OPT) -d1 -s -oa -ob -of -oi -ol -or -os
-TEMPLATE_VBoxBS3KitImg_INCS         = $(VBOX_PATH_BS3KIT_SRC)
+TEMPLATE_VBoxBS3KitImg_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
 TEMPLATE_VBoxBS3KitImg_LDTOOL       = OPENWATCOM-WL
 # 	option quiet, nofarcalls\
@@ -451,5 +451,5 @@
 TEMPLATE_VBoxBS3KitImg32_CXXFLAGS     = \
 	-nt=BS3TEXT32 -nd=BS3DATA32 -nc=BS3CODE32 -ecc -q -wx -zl -mf $(BS3_OW_DBG_OPT) -d1 -s -oa -ob -of -oi -ol -or -os
-TEMPLATE_VBoxBS3KitImg32_INCS         = $(VBOX_PATH_BS3KIT_SRC)
+TEMPLATE_VBoxBS3KitImg32_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
 TEMPLATE_VBoxBS3KitImg32_LDTOOL       = VBoxBsUnusedLd
 
@@ -465,5 +465,5 @@
 TEMPLATE_VBoxBS3KitImg64_DEFS.debug   = BS3_STRICT
 TEMPLATE_VBoxBS3KitImg64_ARTOOL       = OPENWATCOM
-TEMPLATE_VBoxBS3KitImg64_INCS         = $(VBOX_PATH_BS3KIT_SRC)
+TEMPLATE_VBoxBS3KitImg64_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
 if1of ($(KBUILD_HOST), win)
  ifndef TOOL_VCC100AMD64 # For win.x86 builds.
@@ -494,5 +494,5 @@
 TEMPLATE_VBoxBS3KitBS_ASFLAGS         = -f bin --mapfile
 TEMPLATE_VBoxBS3KitBS_ASDEFS          = ASM_FORMAT_BIN RT_NOINC_SEGMENTS ARCH_BITS=16 __YASM__
-TEMPLATE_VBoxBS3KitBS_INCS            = $(VBOX_PATH_BS3KIT_SRC)
+TEMPLATE_VBoxBS3KitBS_INCS            = $(VBOX_PATH_BS3KIT_SRC) .
 TEMPLATE_VBoxBS3KitBS_LDTOOL          = VBoxBsCpLd
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk	(revision 59863)
@@ -229,5 +229,7 @@
 bs3-cpu-basic-2_TEMPLATE = VBoxBS3KitImg
 bs3-cpu-basic-2_SOURCES = \
-	bs3-cpu-basic-2.asm
+	bs3kit/bs3-first-rm.asm \
+	bs3-cpu-basic-2-c.c
+#	bs3-cpu-basic-2.asm
 endif
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-c.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-c.c	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-c.c	(revision 59863)
@@ -0,0 +1,43 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - bs3-cpu-basic-2, 16-bit C code.
+ */
+
+/*
+ * Copyright (C) 2007-2016 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.h>
+
+
+
+BS3_DECL(void) Main_rm()
+{
+    Bs3InitAll_rm();
+    Bs3TestInit("bs3-cpu-basic-2");
+
+    Bs3TestTerm();
+
+}
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac	(revision 59863)
@@ -0,0 +1,10 @@
+%include "bs3kit-template-header.mac"   ; setup environment
+
+
+
+
+
+
+
+%include "bs3kit-template-footer.mac"   ; reset environment
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.asm	(revision 59863)
@@ -63,4 +63,6 @@
         ;
 
+
+
         ;
         ; Done.
@@ -72,3 +74,10 @@
 
 
+;
+; Instantiate the test template code.
+;
+%include "bs3kit-template-footer.mac"  ; reset the initial environemnt.
 
+
+BS3_INSTANTIATE_TEMPLATE_WITH_WEIRD_ONES "bs3-cpu-basic-2-template.mac"
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 59863)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2006-2015 Oracle Corporation
+# Copyright (C) 2006-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -100,5 +100,7 @@
        bs3-cmn-TestData.c \
        bs3-cmn-TestInit.c \
-	bs3-cmn-TestSendStrCmd.asm \
+       bs3-cmn-TestTerm.c \
+	bs3-cmn-TestSendCmdWithStr.asm \
+	bs3-cmn-TestSendCmdWithU32.asm \
 	bs3-cmn-TestIsVmmDevTestingPresent.asm \
 	bs3-cmn-Trap16SetGate.c \
@@ -136,4 +138,5 @@
 	bs3-cmn-hexdigits.c \
 	bs3-cmn-CpuDetectData.c \
+	bs3-cmn-PerCpuData.c \
 	bs3-cmn-ConvertRMStackToP16UsingCxReturnToAx.asm \
 	bs3-wc16-U8DR.asm \
@@ -204,5 +207,5 @@
 bs3kit-rm_TEMPLATE = VBoxBS3KitImg
 bs3kit-rm_INSTTYPE = none
-bs3kit-rm_DEFS     = TMPL_RM
+bs3kit-rm_DEFS     = TMPL_MODE=BS3_MODE_RM
 bs3kit-rm_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
 	bs3-first-rm.asm \
@@ -210,72 +213,135 @@
 
 
-# The 16-bit protected mode BS3Kit library.
+# The 16-bit BS3Kit library for 16-bit protected kernel+tss.
 LIBRARIES += bs3kit-pe16
 bs3kit-pe16_TEMPLATE = VBoxBS3KitImg
 bs3kit-pe16_INSTTYPE = none
-bs3kit-pe16_DEFS     = TMPL_PE16
+bs3kit-pe16_DEFS     = TMPL_MODE=BS3_MODE_PE16
 bs3kit-pe16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
 	bs3-first-pe16.asm \
 #	bs3-mode-CpuDetect.asm
 
-# The virtual 8086 under protected mode BS3Kit library.
+# The 32-bit BS3Kit library for 16-bit protected kernel+tss.
+LIBRARIES += bs3kit-pe16_32
+bs3kit-pe16_32_TEMPLATE = VBoxBS3KitImg32
+bs3kit-pe16_32_INSTTYPE = none
+bs3kit-pe16_32_DEFS     = TMPL_MODE=BS3_MODE_PE16_32
+bs3kit-pe16_32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v86 BS3Kit library for 16-bit protected kernel+tss.
+LIBRARIES += bs3kit-pe16_v86
+bs3kit-pe16_v86_TEMPLATE = VBoxBS3KitImg
+bs3kit-pe16_v86_INSTTYPE = none
+bs3kit-pe16_v86_DEFS     = TMPL_MODE=BS3_MODE_PE16_V86
+bs3kit-pe16_v86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The 32-bit BS3Kit library for 32-bit protected kernel+tss.
+LIBRARIES += bs3kit-pe32
+bs3kit-pe32_TEMPLATE = VBoxBS3KitImg32
+bs3kit-pe32_INSTTYPE = none
+bs3kit-pe32_DEFS     = TMPL_MODE=BS3_MODE_PE32
+bs3kit-pe32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The 16-bit BS3Kit library for 32-bit protected kernel+tss.
+LIBRARIES += bs3kit-pe32_16
+bs3kit-pe32_16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pe32_16_INSTTYPE = none
+bs3kit-pe32_16_DEFS     = TMPL_MODE=BS3_MODE_PE32_16
+bs3kit-pe32_16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v8086 BS3Kit library for 32-bit protected kernel+tss.
 LIBRARIES += bs3kit-pev86
 bs3kit-pev86_TEMPLATE = VBoxBS3KitImg
 bs3kit-pev86_INSTTYPE = none
-bs3kit-pev86_DEFS     = TMPL_PEV86
-bs3kit-pev86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
-
-# The 32-bit protected mode BS3Kit library.
-LIBRARIES += bs3kit-pe32
-bs3kit-pe32_TEMPLATE = VBoxBS3KitImg32
-bs3kit-pe32_INSTTYPE = none
-bs3kit-pe32_DEFS     = TMPL_PE32
-bs3kit-pe32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
-
-
-# The 16-bit paged protected mode BS3Kit library.
+bs3kit-pev86_DEFS     = TMPL_MODE=BS3_MODE_PEV86
+bs3kit-pev86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+
+# The 16-bit BS3Kit library for 16-bit paged protected kernel+tss.
 LIBRARIES += bs3kit-pp16
 bs3kit-pp16_TEMPLATE = VBoxBS3KitImg
 bs3kit-pp16_INSTTYPE = none
-bs3kit-pp16_DEFS     = TMPL_PP16
+bs3kit-pp16_DEFS     = TMPL_MODE=BS3_MODE_PP16
 bs3kit-pp16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
 	bs3-mode-CpuDetect.asm
 
-# The 32-bit paged protected mode BS3Kit library.
+# The 32-bit BS3Kit library for 16-bit paged protected kernel+tss.
+LIBRARIES += bs3kit-pp16_32
+bs3kit-pp16_32_TEMPLATE = VBoxBS3KitImg32
+bs3kit-pp16_32_INSTTYPE = none
+bs3kit-pp16_32_DEFS     = TMPL_MODE=BS3_MODE_PP16_32
+bs3kit-pp16_32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v8086 BS3Kit library for 16-bit paged protected kernel+tss.
+LIBRARIES += bs3kit-pp16_v86
+bs3kit-pp16_v86_TEMPLATE = VBoxBS3KitImg
+bs3kit-pp16_v86_INSTTYPE = none
+bs3kit-pp16_v86_DEFS     = TMPL_MODE=BS3_MODE_PP16_V86
+bs3kit-pp16_v86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The 32-bit BS3Kit library for 32-bit paged protected kernel+tss.
 LIBRARIES += bs3kit-pp32
 bs3kit-pp32_TEMPLATE = VBoxBS3KitImg32
 bs3kit-pp32_INSTTYPE = none
-bs3kit-pp32_DEFS     = TMPL_PP32
+bs3kit-pp32_DEFS     = TMPL_MODE=BS3_MODE_PP32
 bs3kit-pp32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
 
-# The virtual 8086 under protected mode BS3Kit library.
+# The 16-bit BS3Kit library for 32-bit paged protected kernel+tss.
+LIBRARIES += bs3kit-pp32_16
+bs3kit-pp32_16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pp32_16_INSTTYPE = none
+bs3kit-pp32_16_DEFS     = TMPL_MODE=BS3_MODE_PP32_16
+bs3kit-pp32_16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v8086 BS3Kit library for 32-bit paged protected kernel+tss.
 LIBRARIES += bs3kit-ppv86
 bs3kit-ppv86_TEMPLATE = VBoxBS3KitImg
 bs3kit-ppv86_INSTTYPE = none
-bs3kit-ppv86_DEFS     = TMPL_PPV86
-bs3kit-ppv86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
-
-
-# The 16-bit paged protected mode BS3Kit library.
+bs3kit-ppv86_DEFS     = TMPL_MODE=BS3_MODE_PPV86
+bs3kit-ppv86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+
+# The 16-bit BS3Kit library for 16-bit PAE paged protected kernel+tss.
 LIBRARIES += bs3kit-pae16
 bs3kit-pae16_TEMPLATE = VBoxBS3KitImg
 bs3kit-pae16_INSTTYPE = none
-bs3kit-pae16_DEFS     = TMPL_PAE16
+bs3kit-pae16_DEFS     = TMPL_MODE=BS3_MODE_PAE16
 bs3kit-pae16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
 	bs3-mode-CpuDetect.asm
 
-# The 32-bit paged protected mode BS3Kit library.
+# The 16-bit BS3Kit library for 16-bit PAE paged protected kernel+tss.
+LIBRARIES += bs3kit-pae16_32
+bs3kit-pae16_32_TEMPLATE = VBoxBS3KitImg32
+bs3kit-pae16_32_INSTTYPE = none
+bs3kit-pae16_32_DEFS     = TMPL_MODE=BS3_MODE_PAE16_32
+bs3kit-pae16_32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v8086 BS3Kit library for 16-bit PAE paged protected kernel+tss.
+LIBRARIES += bs3kit-pae16_v86
+bs3kit-pae16_v86_TEMPLATE = VBoxBS3KitImg
+bs3kit-pae16_v86_INSTTYPE = none
+bs3kit-pae16_v86_DEFS     = TMPL_MODE=BS3_MODE_PAE16_V86
+bs3kit-pae16_v86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The 32-bit BS3Kit library for 32-bit PAE paged protected kernel+tss.
 LIBRARIES += bs3kit-pae32
 bs3kit-pae32_TEMPLATE = VBoxBS3KitImg32
 bs3kit-pae32_INSTTYPE = none
-bs3kit-pae32_DEFS     = TMPL_PAE32
+bs3kit-pae32_DEFS     = TMPL_MODE=BS3_MODE_PAE32
 bs3kit-pae32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
 
-# The virtual 8086 under protected mode BS3Kit library.
+# The 16-bit BS3Kit library for 32-bit PAE paged protected kernel+tss.
+LIBRARIES += bs3kit-pae32_16
+bs3kit-pae32_16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pae32_16_INSTTYPE = none
+bs3kit-pae32_16_DEFS     = TMPL_MODE=BS3_MODE_PAE32_16
+bs3kit-pae32_16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The v8086 BS3Kit library for 32-bit PAE paged protected kernel+tss.
 LIBRARIES += bs3kit-paev86
 bs3kit-paev86_TEMPLATE = VBoxBS3KitImg
 bs3kit-paev86_INSTTYPE = none
-bs3kit-paev86_DEFS     = TMPL_PAEV86
-bs3kit-paev86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+bs3kit-paev86_DEFS     = TMPL_MODE=BS3_MODE_PAEV86
+bs3kit-paev86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
 
 
@@ -284,6 +350,6 @@
 bs3kit-lm16_TEMPLATE = VBoxBS3KitImg
 bs3kit-lm16_INSTTYPE = none
-bs3kit-lm16_DEFS     = TMPL_LM16
-bs3kit-lm16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+bs3kit-lm16_DEFS     = TMPL_MODE=BS3_MODE_LM16
+bs3kit-lm16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
 
 # The 32-bit long mode BS3Kit library.
@@ -291,6 +357,6 @@
 bs3kit-lm32_TEMPLATE = VBoxBS3KitImg32
 bs3kit-lm32_INSTTYPE = none
-bs3kit-lm32_DEFS     = TMPL_LM32
-bs3kit-lm32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+bs3kit-lm32_DEFS     = TMPL_MODE=BS3_MODE_LM32
+bs3kit-lm32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
 
 # The 64-bit long mode BS3Kit library.
@@ -298,6 +364,6 @@
 bs3kit-lm64-asm_TEMPLATE = VBoxBS3KitImg64
 bs3kit-lm64-asm_INSTTYPE = none
-bs3kit-lm64-asm_DEFS     = TMPL_LM64
-bs3kit-lm64-asm_SOURCES  = $(filter %.asm,$(VBOX_BS3KIT_MODE_SOURCES)) \
+bs3kit-lm64-asm_DEFS     = TMPL_MODE=BS3_MODE_LM64
+bs3kit-lm64-asm_SOURCES  = $(filter %.asm,$(VBOX_BS3KIT_MODE_SOURCES))
 
 # The 64-bit long mode BS3Kit library.
@@ -305,6 +371,6 @@
 bs3kit-lm64-c_TEMPLATE = VBoxBS3KitImg64
 bs3kit-lm64-c_INSTTYPE = none
-bs3kit-lm64-c_DEFS     = TMPL_LM64
-bs3kit-lm64-c_SOURCES  = $(filter-out %.asm, $(VBOX_BS3KIT_MODE_SOURCES)) \
+bs3kit-lm64-c_DEFS     = TMPL_MODE=BS3_MODE_LM64
+bs3kit-lm64-c_SOURCES  = $(filter-out %.asm, $(VBOX_BS3KIT_MODE_SOURCES))
 
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PerCpuData.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PerCpuData.c	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PerCpuData.c	(revision 59863)
@@ -0,0 +1,46 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Per CPU Data.
+ *
+ * @remarks Not quite sure how to do per-cpu data yet, but this is stuff
+ *          that eventually needs to be per CPU.
+ */
+
+/*
+ * Copyright (C) 2007-2016 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-test.h"
+
+
+/*********************************************************************************************************************************
+*   Global Variables                                                                                                             *
+*********************************************************************************************************************************/
+#if ARCH_BITS == 16
+
+uint8_t BS3_DATA_NM(g_bBs3CurrentMode) = BS3_MODE_INVALID;
+
+#endif /* ARCH_BITS == 16 */
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 59863)
@@ -26,4 +26,10 @@
 
 %include "bs3kit-template-header.mac"
+
+%if TMPL_BITS != 16
+BS3_EXTERN_DATA16 g_bBs3CurrentMode
+TMPL_BEGIN_TEXT
+%endif
+
 
 ;;
@@ -62,4 +68,8 @@
         mov     es, ax
 
+        ; Update globals.
+        and     byte [g_bBs3CurrentMode], ~BS3_MODE_CODE_MASK
+        or      byte [g_bBs3CurrentMode], BS3_MODE_CODE_16
+
         popfd
  %if TMPL_BITS == 64
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm	(revision 59863)
@@ -26,4 +26,11 @@
 
 %include "bs3kit-template-header.mac"
+
+
+%if TMPL_BITS != 32
+BS3_EXTERN_DATA16 g_bBs3CurrentMode
+TMPL_BEGIN_TEXT
+%endif
+
 
 ;;
@@ -72,4 +79,8 @@
         mov     es, ax
 
+        ; Update globals.
+        and     byte [g_bBs3CurrentMode], ~BS3_MODE_CODE_MASK
+        or      byte [g_bBs3CurrentMode], BS3_MODE_CODE_32
+
  %if TMPL_BITS == 16
         ; Adjust the return address.
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm	(revision 59863)
@@ -27,5 +27,9 @@
 %include "bs3kit-template-header.mac"
 
-%ifndef TMPL_CMN_LM
+%if TMPL_BITS != 64
+BS3_EXTERN_DATA16 g_bBs3CurrentMode
+TMPL_BEGIN_TEXT
+%endif
+
 
 ;;
@@ -35,13 +39,13 @@
 ;
 BS3_PROC_BEGIN_CMN Bs3SwitchTo64Bit
- %if TMPL_BITS == 64
+%if TMPL_BITS == 64
         ret
+%else
+ %if TMPL_BITS == 16
+        sub     sp, 6                   ; Space for extended return value (corrected in 64-bit mode).
  %else
-  %if TMPL_BITS == 16
-        sub     sp, 6                   ; Space for extended return value (corrected in 64-bit mode).
-  %else
         push    xPRE [xSP]              ; Duplicate the return address.
         and     dword [xSP + xCB], 0    ; Clear the high dword or it.
-  %endif
+ %endif
         push    dword 0
         push    sAX
@@ -58,11 +62,11 @@
         ; setup far return.
         push    sAX
- %if TMPL_BITS == 16
+%if TMPL_BITS == 16
         push    dword .sixty_four_bit
         o32 retf
- %else
+%else
         push    .sixty_four_bit
         retf
- %endif
+%endif
 
 BS3_SET_BITS 64
@@ -75,16 +79,18 @@
         mov     es, ax
 
-  %if TMPL_BITS == 16
+        ; Update globals.
+        and     byte [BS3_WRT_RIP(g_bBs3CurrentMode)], ~BS3_MODE_CODE_MASK
+        or      byte [BS3_WRT_RIP(g_bBs3CurrentMode)], BS3_MODE_CODE_64
+
+ %if TMPL_BITS == 16
         movzx   eax, word [rsp + 8*2+6]
         add     eax, BS3_ADDR_BS3TEXT16
         mov     [rsp + 8*2], rax
-  %endif
+ %endif
 
         popfq
         pop     rax
         ret
- %endif
+%endif
 BS3_PROC_END_CMN   Bs3SwitchTo64Bit
 
-%endif
-
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestData.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestData.c	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestData.c	(revision 59863)
@@ -42,8 +42,8 @@
 
 /** The number of tests that have failed. */
-uint16_t    g_uscBs3TestErrors = 0;
+uint16_t    g_cusBs3TestErrors = 0;
 
 /** The start error count of the current subtest. */
-uint16_t    g_uscBs3SubTestAtErrors = 0;
+uint16_t    g_cusBs3SubTestAtErrors = 0;
 
 /** Whether we've reported the sub-test result or not. */
@@ -51,8 +51,8 @@
 
 /** The number of sub tests. */
-uint16_t    g_uscBs3SubTests = 0;
+uint16_t    g_cusBs3SubTests = 0;
 
 /** The number of sub tests that failed. */
-uint16_t    g_uscBs3SubTestsFailed = 0;
+uint16_t    g_cusBs3SubTestsFailed = 0;
 
 /** VMMDEV_TESTING_UNIT_XXX -> string */
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestInit.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestInit.c	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestInit.c	(revision 59863)
@@ -43,9 +43,9 @@
     BS3_CMN_NM(g_pszBs3Test)    = pszTest;
     BS3_CMN_NM(g_pszBs3SubTest) = NULL;
-    g_uscBs3TestErrors          = 0;
-    g_uscBs3SubTestAtErrors     = 0;
+    g_cusBs3TestErrors          = 0;
+    g_cusBs3SubTestAtErrors     = 0;
     g_fbBs3SubTestReported      = true;
-    g_uscBs3SubTests            = 0;
-    g_uscBs3SubTestsFailed      = 0;
+    g_cusBs3SubTests            = 0;
+    g_cusBs3SubTestsFailed      = 0;
     g_fbBs3VMMDevTesting        = bs3TestIsVmmDevTestingPresent();
 
@@ -59,5 +59,5 @@
      * Report it to the VMMDev.
      */
-    bs3TestSendStrCmd(VMMDEV_TESTING_CMD_INIT, pszTest);
+    bs3TestSendCmdWithStr(VMMDEV_TESTING_CMD_INIT, pszTest);
 }
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm	(revision 59863)
@@ -0,0 +1,75 @@
+; $Id$
+;; @file
+; BS3Kit - bs3TestSendStrCmd.
+;
+
+;
+; 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.
+;
+
+%include "bs3kit-template-header.mac"
+%include "VBox/VMMDevTesting.mac"
+
+BS3_EXTERN_DATA16 g_fbBs3VMMDevTesting
+TMPL_BEGIN_TEXT
+
+;;
+; @cproto   BS3_DECL(void) bs3TestSendCmdWithStr_c16(uint32_t uCmd, const char BS3_FAR *pszString);
+;
+BS3_PROC_BEGIN_CMN bs3TestSendCmdWithStr
+        BS3_CALL_CONV_PROLOG 2
+        push    xBP
+        mov     xBP, xSP
+        push    xAX
+        push    xDX
+        push    xSI
+        BS3_ONLY_16BIT_STMT push ds
+
+        cmp     byte [g_fbBs3VMMDevTesting], 0
+        je      .no_vmmdev
+
+        ; The command (uCmd).
+        mov     dx, VMMDEV_TESTING_IOPORT_CMD
+        mov     eax, [xBP + xCB*2]
+        out     dx, eax
+
+        ; The string.
+        mov     dx, VMMDEV_TESTING_IOPORT_DATA
+%ifdef TMPL_16BIT
+        lds     si, [xBP + xCB*2 + sCB]
+%else
+        mov     xSI, [xBP + xCB*2 + sCB]
+%endif
+.next_char:
+        lodsb
+        out     dx, al
+        test    al, al
+        jnz     .next_char
+
+.no_vmmdev:
+        BS3_ONLY_16BIT_STMT pop ds
+        pop     xSI
+        pop     xDX
+        pop     xAX
+        leave
+        BS3_CALL_CONV_EPILOG 2
+        ret
+BS3_PROC_END_CMN   bs3TestSendCmdWithStr
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithU32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithU32.asm	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithU32.asm	(revision 59863)
@@ -0,0 +1,65 @@
+; $Id$
+;; @file
+; BS3Kit - bs3TestSendCmdWithU32.
+;
+
+;
+; Copyright (C) 2007-2016 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.
+;
+
+%include "bs3kit-template-header.mac"
+%include "VBox/VMMDevTesting.mac"
+
+BS3_EXTERN_DATA16 g_fbBs3VMMDevTesting
+TMPL_BEGIN_TEXT
+
+;;
+; @cproto   BS3_DECL(void) bs3TestSendCmdWithU32_c16(uint32_t uCmd, uint32_t uValue);
+;
+BS3_PROC_BEGIN_CMN bs3TestSendCmdWithU32
+        BS3_CALL_CONV_PROLOG 2
+        push    xBP
+        mov     xBP, xSP
+        push    xAX
+        push    xDX
+        push    xSI
+
+        cmp     byte [g_fbBs3VMMDevTesting], 0
+        je      .no_vmmdev
+
+        ; The command (uCmd).
+        mov     dx, VMMDEV_TESTING_IOPORT_CMD
+        mov     eax, [xBP + xCB*2]
+        out     dx, eax
+
+        ; The value (uValue).
+        mov     dx, VMMDEV_TESTING_IOPORT_DATA
+        mov     eax, [xBP + xCB*2 + sCB]
+        out     dx, eax
+
+.no_vmmdev:
+        pop     xSI
+        pop     xDX
+        pop     xAX
+        leave
+        BS3_CALL_CONV_EPILOG 2
+        ret
+BS3_PROC_END_CMN   bs3TestSendCmdWithU32
+
Index: unk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendStrCmd.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendStrCmd.asm	(revision 59862)
+++ 	(revision )
@@ -1,75 +1,0 @@
-; $Id$
-;; @file
-; BS3Kit - bs3TestSendStrCmd.
-;
-
-;
-; 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.
-;
-
-%include "bs3kit-template-header.mac"
-%include "VBox/VMMDevTesting.mac"
-
-BS3_EXTERN_DATA16 g_fbBs3VMMDevTesting
-TMPL_BEGIN_TEXT
-
-;;
-; @cproto   BS3_DECL(void) bs3TestSendStrCmd_c16(uint32_t uCmd, const char BS3_FAR *pszString);
-;
-BS3_PROC_BEGIN_CMN bs3TestSendStrCmd
-        BS3_CALL_CONV_PROLOG 2
-        push    xBP
-        mov     xBP, xSP
-        push    xAX
-        push    xDX
-        push    xSI
-        BS3_ONLY_16BIT_STMT push ds
-
-        cmp     byte [g_fbBs3VMMDevTesting], 0
-        je      .no_vmmdev
-
-        ; The command (uCmd).
-        mov     dx, VMMDEV_TESTING_IOPORT_CMD
-        mov     eax, [xBP + xCB]
-        out     dx, eax
-
-        ; The string.
-        mov     dx, VMMDEV_TESTING_IOPORT_DATA
-%ifdef TMPL_16BIT
-        lds     si, [xBP + xCB + sCB]
-%else
-        mov     xSI, [xBP + xCB + sCB]
-%endif
-.next_char:
-        lodsb
-        out     dx, al
-        test    al, al
-        jnz     .next_char
-
-.no_vmmdev:
-        BS3_ONLY_16BIT_STMT pop ds
-        pop     xSI
-        pop     xDX
-        pop     xAX
-        leave
-        BS3_CALL_CONV_EPILOG 2
-        ret
-BS3_PROC_END_CMN   bs3TestSendStrCmd
-
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestTerm.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestTerm.c	(revision 59863)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestTerm.c	(revision 59863)
@@ -0,0 +1,66 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Bs3TestTerm
+ */
+
+/*
+ * Copyright (C) 2007-2016 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.
+ */
+
+
+#include "bs3kit-template-header.h"
+#include "bs3-cmn-test.h"
+
+
+/**
+ * Equivalent to RTTestSummaryAndDestroy.
+ */
+BS3_DECL(void) Bs3TestTerm(void)
+{
+    /*
+     * Close current sub-test.
+     */
+    if (BS3_CMN_NM(g_pszBs3SubTest))
+    {
+
+    }
+
+
+    /*
+     * Report summary.
+     */
+    if (BS3_CMN_NM(g_pszBs3Test))
+    {
+        Bs3PrintStr(BS3_CMN_NM(g_pszBs3Test));
+        if (g_cusBs3TestErrors == 0)
+            Bs3Printf(": SUCCESS (%u tests)\n", g_cusBs3SubTests);
+        else
+            Bs3Printf(": FAILURE - %u (%u of %u tests)\n", g_cusBs3TestErrors, g_cusBs3SubTestsFailed, g_cusBs3SubTests);
+    }
+
+    /*
+     * Tell VMMDev.
+     */
+    bs3TestSendCmdWithU32(VMMDEV_TESTING_CMD_TERM, g_cusBs3TestErrors);
+    for (;;) { }
+
+    BS3_CMN_NM(g_pszBs3Test) = NULL;
+}
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-test.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-test.h	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-test.h	(revision 59863)
@@ -36,8 +36,8 @@
 
 /** The number of tests that have failed. */
-extern uint16_t             g_uscBs3TestErrors;
+extern uint16_t             g_cusBs3TestErrors;
 
 /** The start error count of the current subtest. */
-extern uint16_t             g_uscBs3SubTestAtErrors;
+extern uint16_t             g_cusBs3SubTestAtErrors;
 
 /**  Whether we've reported the sub-test result or not. */
@@ -45,8 +45,8 @@
 
 /** The number of sub tests. */
-extern uint16_t             g_uscBs3SubTests;
+extern uint16_t             g_cusBs3SubTests;
 
 /** The number of sub tests that failed. */
-extern uint16_t             g_uscBs3SubTestsFailed;
+extern uint16_t             g_cusBs3SubTestsFailed;
 
 /** VMMDEV_TESTING_UNIT_XXX -> string */
@@ -69,11 +69,25 @@
  * do nothing.
  *
- * @param   uCmd       The command.
- * @param   pszString  The string.
+ * @param   uCmd        The command.
+ * @param   pszString   The string.
  */
-BS3_DECL(void) bs3TestSendStrCmd_c16(uint32_t uCmd, const char BS3_FAR *pszString);
-BS3_DECL(void) bs3TestSendStrCmd_c32(uint32_t uCmd, const char BS3_FAR *pszString); /**< @copydoc bs3TestSendStrCmd_c16 */
-BS3_DECL(void) bs3TestSendStrCmd_c64(uint32_t uCmd, const char BS3_FAR *pszString); /**< @copydoc bs3TestSendStrCmd_c16 */
-#define bs3TestSendStrCmd BS3_CMN_NM(bs3TestSendStrCmd) /**< Selects #bs3TestSendStrCmd_c16, #bs3TestSendStrCmd_c32 or #bs3TestSendStrCmd_c64. */
+BS3_DECL(void) bs3TestSendCmdWithStr_c16(uint32_t uCmd, const char BS3_FAR *pszString);
+BS3_DECL(void) bs3TestSendCmdWithStr_c32(uint32_t uCmd, const char BS3_FAR *pszString); /**< @copydoc bs3TestSendCmdWithStr_c16 */
+BS3_DECL(void) bs3TestSendCmdWithStr_c64(uint32_t uCmd, const char BS3_FAR *pszString); /**< @copydoc bs3TestSendCmdWithStr_c16 */
+#define bs3TestSendCmdWithStr BS3_CMN_NM(bs3TestSendCmdWithStr) /**< Selects #bs3TestSendCmdWithStr_c16, #bs3TestSendCmdWithStr_c32 or #bs3TestSendCmdWithStr_c64. */
+
+/**
+ * Sends a command to VMMDev followed by a 32-bit unsigned integer value.
+ *
+ * If the VMMDev is not present or is not being used, this function will
+ * do nothing.
+ *
+ * @param   uCmd        The command.
+ * @param   uValue      The value.
+ */
+BS3_DECL(void) bs3TestSendCmdWithU32_c16(uint32_t uCmd, uint32_t uValue);
+BS3_DECL(void) bs3TestSendCmdWithU32_c32(uint32_t uCmd, uint32_t uValue); /**< @copydoc bs3TestSendCmdWithU32_c16 */
+BS3_DECL(void) bs3TestSendCmdWithU32_c64(uint32_t uCmd, uint32_t uValue); /**< @copydoc bs3TestSendCmdWithU32_c16 */
+#define bs3TestSendCmdWithU32 BS3_CMN_NM(bs3TestSendCmdWithU32) /**< Selects #bs3TestSendCmdWithU32_c16, #bs3TestSendCmdWithU32_c32 or #bs3TestSendCmdWithU32_c64. */
 
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 59863)
@@ -38,12 +38,15 @@
 EXTERN Main_rm
 BS3_EXTERN_CMN Bs3Shutdown
+        push    word 0                  ; zero return address.
+        push    word 0                  ; zero caller BP
+        mov     bp, sp
 
-    ;
-    ; Nothing to init here, just call main and shutdown if it returns.
-    ;
-    mov     ax, BS3DATA16
-    mov     es, ax
-    mov     ds, ax
-    call    NAME(Main_rm)
-    call    Bs3Shutdown
+        ;
+        ; Nothing to init here, just call main and shutdown if it returns.
+        ;
+        mov     ax, BS3DATA16
+        mov     es, ax
+        mov     ds, ax
+        call    NAME(Main_rm)
+        call    Bs3Shutdown
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm	(revision 59863)
@@ -27,4 +27,6 @@
 %include "bs3kit-template-header.mac"
 
+BS3_EXTERN_DATA16 g_bBs3CurrentMode
+TMPL_BEGIN_TEXT
 
 ;;
@@ -39,5 +41,5 @@
         mov     xBP, xSP
         push    xAX
-%if TMPL_BITS == 64
+%if BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
         push    rcx
         push    rdx
@@ -49,19 +51,19 @@
         ; Load stack selector (not always necessary) and sometimes CS too.
         ;
-%ifdef TMPL_RM
+%if BS3_MODE_IS_RM_SYS(TMPL_MODE)
         xor     ax, ax
-%elifdef TMPL_CMN_V86
+%elif BS3_MODE_IS_V86(TMPL_MODE)
         extern  v86_versions_of_Bs3EnteredMode_should_not_be_dragged_into_the_link
         call    v86_versions_of_Bs3EnteredMode_should_not_be_dragged_into_the_link
-%elif TMPL_BITS == 16
+%elif BS3_MODE_IS_16BIT_CODE(TMPL_MODE)
         jmp     BS3_SEL_R0_CS16:.reloaded_cs
 .reloaded_cs:
         mov     ax, BS3_SEL_R0_SS16
-%elif TMPL_BITS == 32
+%elif BS3_MODE_IS_32BIT_CODE(TMPL_MODE)
         mov     ax, BS3_SEL_R0_SS32
-%elif TMPL_BITS == 64
+%elif BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
         mov     ax, BS3_SEL_R0_DS64
 %else
- %error "TMPL_BITS"
+ %error "TMPL_MODE"
 %endif
         mov     ss, ax
@@ -70,5 +72,5 @@
         ; Load selector appropriate for accessing BS3SYSTEM16 data.
         ;
-%if TMPL_BITS == 16
+%if BS3_MODE_IS_16BIT_CODE(TMPL_MODE)
         mov     ax, BS3_SEL_SYSTEM16
 %else
@@ -81,19 +83,28 @@
         ; Always 64-bit in long mode, otherwise according to TMPL_BITS.
         ;
-%ifdef TMPL_CMN_R86
+%if BS3_MODE_IS_RM_SYS(TMPL_MODE)
         BS3_EXTERN_SYSTEM16 Bs3Lidt_Ivt
         TMPL_BEGIN_TEXT
         lidt    [Bs3Lidt_Ivt]
-%elifdef TMPL_CMN_LM
+
+%elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
+        BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt16
+        TMPL_BEGIN_TEXT
+        lidt    [Bs3Lidt_Idt16 TMPL_WRT_SYSTEM16_OR_FLAT]
+
+%elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
+        BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt32
+        TMPL_BEGIN_TEXT
+        lidt    [Bs3Lidt_Idt32 TMPL_WRT_SYSTEM16_OR_FLAT]
+
+%elif BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
         BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt64
         TMPL_BEGIN_TEXT
         lidt    [Bs3Lidt_Idt64 TMPL_WRT_SYSTEM16_OR_FLAT]
 %else
-        BS3_EXTERN_SYSTEM16 RT_CONCAT(Bs3Lidt_Idt,TMPL_BITS)
-        TMPL_BEGIN_TEXT
-        lidt    [RT_CONCAT(Bs3Lidt_Idt,TMPL_BITS) TMPL_WRT_SYSTEM16_OR_FLAT]
-%endif
-
-%ifndef TMPL_CMN_R86
+ %error "TMPL_MODE"
+%endif
+
+%if !BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
         ;
         ; Load the appropriate task selector.
@@ -101,5 +112,5 @@
         ;
         mov     ax, X86DESCGENERIC_BIT_OFF_TYPE + 1 ; For clearing the busy bit in the TSS descriptor type.
- %ifdef TMPL_CMN_LM
+ %if BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
         BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss64
         TMPL_BEGIN_TEXT
@@ -107,5 +118,5 @@
         mov     ax, BS3_SEL_TSS64
 
- %elif TMPL_BITS == 16
+ %elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
         BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16
         BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16DoubleFault
@@ -115,5 +126,5 @@
         mov     ax, BS3_SEL_TSS16
 
- %elif TMPL_BITS == 32
+ %elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
         BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32
         BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32DoubleFault
@@ -128,10 +139,10 @@
         mov     ax, BS3_SEL_TSS32
  %else
- %error "TMPL_BITS"
+  %error "TMPL_BITS"
  %endif
         ltr     ax
 %endif ; !TMPL_CMN_R86
 
-%ifndef TMPL_CMN_R86
+%if !BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
         ;
         ; Load the LDT.
@@ -153,16 +164,50 @@
 
         ;
+        ; Set global indicating CPU mode.
+        ;
+        mov     byte [BS3_WRT_RIP(g_bBs3CurrentMode)], TMPL_MODE
+
+        ;
         ; Install system call handler.
         ; Always 64-bit in long mode, otherwise according to TMPL_BITS.
         ;
-%ifdef TMPL_CMN_RM
-        mov     word [ss: BS3_TRAP_SYSCALL*4], TMPL_NM(Bs3TrapSystemCallHandler) wrt BS3TEXT16
+%if BS3_MODE_IS_RM_SYS(TMPL_MODE)
+        extern         _Bs3TrapSystemCallHandler_rm
+        mov     word [ss: BS3_TRAP_SYSCALL*4], _Bs3TrapSystemCallHandler_rm wrt BS3TEXT16
         mov     word [ss: BS3_TRAP_SYSCALL*4 + 2], BS3TEXT16
-%elifdef TMPL_CMN_LM
+
+%elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
+        BS3_EXTERN_CMN Bs3Trap16SetGate
+        extern         TMPL_NM(Bs3TrapSystemCallHandler)
+        BS3_BEGIN_TEXT16
+        TMPL_BEGIN_TEXT
+        push    0                       ; cParams
+        push    TMPL_NM(Bs3TrapSystemCallHandler) wrt BS3TEXT16
+        push    BS3_SEL_R0_CS16
+        push    3                       ; DPL
+        push    X86_SEL_TYPE_SYS_286_INT_GATE
+        push    BS3_TRAP_SYSCALL
+        BS3_CALL Bs3Trap16SetGate,6
+        add     xSP, xCB * 6
+
+%elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
+        BS3_EXTERN_CMN Bs3Trap32SetGate
+        extern         TMPL_NM(Bs3TrapSystemCallHandler)
+        TMPL_BEGIN_TEXT
+        push    0                       ; cParams
+        push    dword TMPL_NM(Bs3TrapSystemCallHandler) wrt FLAT
+        push    BS3_SEL_R0_CS32
+        push    3                       ; DPL
+        push    X86_SEL_TYPE_SYS_386_INT_GATE
+        push    BS3_TRAP_SYSCALL
+        BS3_CALL Bs3Trap32SetGate,6
+        add     xSP, xCB * 6
+
+%elif BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
         BS3_EXTERN_CMN Bs3Trap64SetGate
         extern         Bs3TrapSystemCallHandler_lm64
         TMPL_BEGIN_TEXT
         push    0                       ; bIst
- %ifdef TMPL_64BIT
+ %if BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
         push    Bs3TrapSystemCallHandler_lm64 wrt FLAT
  %else
@@ -176,30 +221,4 @@
         BS3_CALL Bs3Trap64SetGate,6
         add     xSP, xCB * 5 + 8
-
-%elif TMPL_BITS == 16
-        BS3_EXTERN_CMN Bs3Trap16SetGate
-        extern         TMPL_NM(Bs3TrapSystemCallHandler)
-        TMPL_BEGIN_TEXT
-        push    0                       ; cParams
-        push    TMPL_NM(Bs3TrapSystemCallHandler) wrt BS3TEXT16
-        push    BS3_SEL_R0_CS16
-        push    3                       ; DPL
-        push    X86_SEL_TYPE_SYS_286_INT_GATE
-        push    BS3_TRAP_SYSCALL
-        BS3_CALL Bs3Trap16SetGate,6
-        add     xSP, xCB * 6
-
-%elif TMPL_BITS == 32
-        BS3_EXTERN_CMN Bs3Trap32SetGate
-        extern         TMPL_NM(Bs3TrapSystemCallHandler)
-        TMPL_BEGIN_TEXT
-        push    0                       ; cParams
-        push    TMPL_NM(Bs3TrapSystemCallHandler) wrt FLAT
-        push    BS3_SEL_R0_CS32
-        push    3                       ; DPL
-        push    X86_SEL_TYPE_SYS_386_INT_GATE
-        push    BS3_TRAP_SYSCALL
-        BS3_CALL Bs3Trap32SetGate,6
-        add     xSP, xCB * 6
 %else
  %error "TMPL_BITS"
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 59863)
@@ -34,5 +34,5 @@
 ; Switch to real mode from any other mode.
 ;
-; @cproto   BS3_DECL(void) Bs3SwitchToRealMode(void);
+; @cproto   BS3_DECL(void) Bs3SwitchToRM(void);
 ;
 ; @uses     Nothing (except high 32-bit register parts).
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.h	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.h	(revision 59863)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2007-2015 Oracle Corporation
+ * Copyright (C) 2007-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -32,11 +32,20 @@
 #undef TMPL_RM
 #undef TMPL_PE16
+#undef TMPL_PE16_32
+#undef TMPL_PE16_V86
 #undef TMPL_PE32
+#undef TMPL_PE32_16
 #undef TMPL_PEV86
 #undef TMPL_PP16
+#undef TMPL_PP16_32
+#undef TMPL_PP16_V86
 #undef TMPL_PP32
+#undef TMPL_PP32_16
 #undef TMPL_PPV86
 #undef TMPL_PAE16
+#undef TMPL_PAE16_32
+#undef TMPL_PAE16_V86
 #undef TMPL_PAE32
+#undef TMPL_PAE32_16
 #undef TMPL_PAEV86
 #undef TMPL_LM16
@@ -45,14 +54,19 @@
 
 #undef TMPL_CMN_PE
+#undef TMPL_SYS_PE16
+#undef TMPL_SYS_PE32
 #undef TMPL_CMN_PP
+#undef TMPL_SYS_PP16
+#undef TMPL_SYS_PP32
 #undef TMPL_CMN_PAE
+#undef TMPL_SYS_PAE16
+#undef TMPL_SYS_PAE32
 #undef TMPL_CMN_LM
 #undef TMPL_CMN_V86
+#undef TMPL_CMN_R86
 #undef TMPL_CMN_PAGING
+#undef TMPL_CMN_WEIRD
+#undef TMPL_CMN_WEIRD_V86
 
-#undef TMPL_CMN_P16
-#undef TMPL_CMN_P32
-#undef TMPL_CMN_P64
-#undef TMPL_CMN_R16
 #undef TMPL_CMN_R86
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac	(revision 59863)
@@ -42,11 +42,20 @@
 %undef TMPL_RM
 %undef TMPL_PE16
+%undef TMPL_PE16_32
+%undef TMPL_PE16_V86
 %undef TMPL_PE32
+%undef TMPL_PE32_16
 %undef TMPL_PEV86
 %undef TMPL_PP16
+%undef TMPL_PP16_32
+%undef TMPL_PP16_V86
 %undef TMPL_PP32
+%undef TMPL_PP32_16
 %undef TMPL_PPV86
 %undef TMPL_PAE16
+%undef TMPL_PAE16_32
+%undef TMPL_PAE16_V86
 %undef TMPL_PAE32
+%undef TMPL_PAE32_16
 %undef TMPL_PAEV86
 %undef TMPL_LM16
@@ -55,14 +64,19 @@
 
 %undef TMPL_CMN_PE
+%undef TMPL_SYS_PE16
+%undef TMPL_SYS_PE32
 %undef TMPL_CMN_PP
+%undef TMPL_SYS_PP16
+%undef TMPL_SYS_PP32
 %undef TMPL_CMN_PAE
+%undef TMPL_SYS_PAE16
+%undef TMPL_SYS_PAE32
 %undef TMPL_CMN_LM
 %undef TMPL_CMN_V86
+%undef TMPL_CMN_R86
 %undef TMPL_CMN_PAGING
+%undef TMPL_CMN_WEIRD
+%undef TMPL_CMN_WEIRD_V86
 
-%undef TMPL_CMN_P16
-%undef TMPL_CMN_P32
-%undef TMPL_CMN_P64
-%undef TMPL_CMN_R16
 %undef TMPL_CMN_R86
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.h	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.h	(revision 59863)
@@ -54,28 +54,49 @@
  *
  * @{ */
-# define TMPL_RM     /**< real mode. */
-# define TMPL_PE16   /**< 16-bit protected mode, unpaged. */
-# define TMPL_PE32   /**< 32-bit protected mode, unpaged. */
-# define TMPL_PEV86  /**< virtual 8086 mode under protected mode, unpaged. */
-# define TMPL_PP16   /**< 16-bit protected mode, paged. */
-# define TMPL_PP32   /**< 32-bit protected mode, paged. */
-# define TMPL_PPV86  /**< virtual 8086 mode under protected mode, paged. */
-# define TMPL_PAE16  /**< 16-bit protected mode with PAE (paged). */
-# define TMPL_PAE32  /**< 16-bit protected mode with PAE (paged). */
-# define TMPL_PAEV86 /**< virtual 8086 mode under protected mode with PAE (paged). */
-# define TMPL_LM16   /**< 16-bit long mode (paged). */
-# define TMPL_LM32   /**< 32-bit long mode (paged). */
-# define TMPL_LM64   /**< 64-bit long mode (paged). */
+# define TMPL_RM            /**< real mode. */
+
+# define TMPL_PE16          /**< 16-bit protected mode kernel+tss, running 16-bit code, unpaged. */
+# define TMPL_PE16_32       /**< 16-bit protected mode kernel+tss, running 32-bit code, unpaged. */
+# define TMPL_PE16_V86      /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
+# define TMPL_PE32          /**< 32-bit protected mode kernel+tss, running 32-bit code, unpaged. */
+# define TMPL_PE32_16       /**< 32-bit protected mode kernel+tss, running 16-bit code, unpaged. */
+# define TMPL_PEV86         /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
+
+# define TMPL_PP16          /**< 16-bit protected mode kernel+tss, running 16-bit code, paged. */
+# define TMPL_PP16_32       /**< 16-bit protected mode kernel+tss, running 32-bit code, paged. */
+# define TMPL_PP16_V86      /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
+# define TMPL_PP32          /**< 32-bit protected mode kernel+tss, running 32-bit code, paged. */
+# define TMPL_PP32_16       /**< 32-bit protected mode kernel+tss, running 16-bit code, paged. */
+# define TMPL_PPV86         /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
+
+# define TMPL_PAE16         /**< 16-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
+# define TMPL_PAE16_32      /**< 16-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
+# define TMPL_PAE16_V86     /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, PAE paging. */
+# define TMPL_PAE32         /**< 32-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
+# define TMPL_PAE32_16      /**< 32-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
+# define TMPL_PAEV86        /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, PAE paging. */
+
+# define TMPL_LM16          /**< 16-bit long mode (paged), kernel+tss always 64-bit. */
+# define TMPL_LM32          /**< 32-bit long mode (paged), kernel+tss always 64-bit. */
+# define TMPL_LM64          /**< 64-bit long mode (paged), kernel+tss always 64-bit. */
 /** @} */
 
 /** @name Derived Indicators
  * @{ */
-# define TMPL_CMN_PE    /**< TMPL_PE16  | TMPL_PE32  | TMPL_PEV86  */
-# define TMPL_CMN_PP    /**< TMPL_PP16  | TMPL_PP32  | TMPL_PPV86  */
-# define TMPL_CMN_PAE   /**< TMPL_PAE16 | TMPL_PAE32 | TMPL_PAEV86 */
-# define TMPL_CMN_LM    /**< TMPL_LM16  | TMPL_LM32  | TMPL_LM64   */
-# define TMPL_CMN_V86   /**< TMPL_PEV86 | TMPL_PPV86 | TMPL_PAEV86 */
-# define TMPL_CMN_R86   /**< TMPL_CMN_V86 | TMPL_RM                */
-# define TMPL_CMN_PAGING /**< TMPL_CMN_PP | TMPL_CMN_PAE | TMPL_CMN_LM */
+# define TMPL_CMN_PE        /**< TMPL_PE16  | TMPL_PE16_32  | TMPL_PE16_V86  | TMPL_PE32  | TMPL_PE32_16  | TMPL_PEV86 */
+# define TMPL_SYS_PE16      /**< TMPL_PE16  | TMPL_PE16_32  | TMPL_PE16_V86 */
+# define TMPL_SYS_PE32      /**< TMPL_PE32  | TMPL_PE32_16  | TMPL_PEV86 */
+# define TMPL_CMN_PP        /**< TMPL_PP16  | TMPL_PP16_32  | TMPL_PP16_V86  | TMPL_PP32  | TMPL_PP32_16  | TMPL_PPV86 */
+# define TMPL_SYS_PP16      /**< TMPL_PP16  | TMPL_PP16_32  | TMPL_PP16_V86 */
+# define TMPL_SYS_PP32      /**< TMPL_PP32  | TMPL_PP32_16  | TMPL_PPV86 */
+# define TMPL_CMN_PAE       /**< TMPL_PAE16 | TMPL_PAE16_32 | TMPL_PAE16_V86 | TMPL_PAE32 | TMPL_PAE32_16 | TMPL_PAEV86 */
+# define TMPL_SYS_PAE16     /**< TMPL_PAE16 | TMPL_PAE16_32 | TMPL_PAE16_V86 */
+# define TMPL_SYS_PAE32     /**< TMPL_PAE32 | TMPL_PAE32_16 | TMPL_PAEV86 */
+# define TMPL_CMN_LM        /**< TMPL_LM16  | TMPL_LM32  | TMPL_LM64 */
+# define TMPL_CMN_V86       /**< TMPL_PEV86 | TMPL_PE16_V86 | TMPL_PPV86 | TMPL_PP16_V86 | TMPL_PAEV86 | TMPL_PAE16_V86 */
+# define TMPL_CMN_R86       /**< TMPL_CMN_V86 | TMPL_RM */
+# define TMPL_CMN_PAGING    /**< TMPL_CMN_PP | TMPL_CMN_PAE | TMPL_CMN_LM */
+# define TMPL_CMN_WEIRD     /**< TMPL_PE16_32 | TMPL_PE32_16 | TMPL_PP16_32 | TMPL_PP32_16 | TMPL_PAE16_32 | TMPL_PAE32_16 | TMPL_CMN_WEIRD_V86 */
+# define TMPL_CMN_WEIRD_V86 /**< TMPL_PE16_V86 | TMPL_PP16_V86 | TMPL_PAE16_V86 */
 /** @} */
 
@@ -119,652 +140,330 @@
 #undef BS3_CMN_NM
 
-#ifdef TMPL_RM
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_RM' and 'TMPL_PE16' are defined."
+
+/*
+ * Convert TMPL_XXX to TMPL_MODE.
+ */
+#ifndef TMPL_MODE
+# ifdef TMPL_RM
+#  define TMPL_MODE         BS3_MODE_RM
+# elif defined(TMPL_PE16)
+#  define TMPL_MODE         BS3_MODE_PE16
+# elif defined(TMPL_PE16_32)
+#  define TMPL_MODE         BS3_MODE_PE16_32
+# elif defined(TMPL_PE16_V86)
+#  define TMPL_MODE         BS3_MODE_PE16_V86
+# elif defined(TMPL_PE32)
+#  define TMPL_MODE         BS3_MODE_PE32
+# elif defined(TMPL_PE32_16)
+#  define TMPL_MODE         BS3_MODE_PE32_16
+# elif defined(TMPL_PEV86)
+#  define TMPL_MODE         BS3_MODE_PEV86
+# elif defined(TMPL_PP16)
+#  define TMPL_MODE         BS3_MODE_PP16
+# elif defined(TMPL_PP16_32)
+#  define TMPL_MODE         BS3_MODE_PP16_32
+# elif defined(TMPL_PP16_V86)
+#  define TMPL_MODE         BS3_MODE_PP16_V86
+# elif defined(TMPL_PP32)
+#  define TMPL_MODE         BS3_MODE_PP32
+# elif defined(TMPL_PP32_16)
+#  define TMPL_MODE         BS3_MODE_PP32_16
+# elif defined(TMPL_PPV86)
+#  define TMPL_MODE         BS3_MODE_PPV86
+# elif defined(TMPL_PAE16)
+#  define TMPL_MODE         BS3_MODE_PAE16
+# elif defined(TMPL_PAE16_32)
+#  define TMPL_MODE         BS3_MODE_PAE16_32
+# elif defined(TMPL_PAE16_V86)
+#  define TMPL_MODE         BS3_MODE_PAE16_V86
+# elif defined(TMPL_PAE32)
+#  define TMPL_MODE         BS3_MODE_PAE32
+# elif defined(TMPL_PAE32_16)
+#  define TMPL_MODE         BS3_MODE_PAE32_16
+# elif defined(TMPL_PAEV86)
+#  define TMPL_MODE         BS3_MODE_PAEV86
+# elif defined(TMPL_LM16)
+#  define TMPL_MODE         BS3_MODE_LM16
+# elif defined(TMPL_LM32)
+#  define TMPL_MODE         BS3_MODE_LM32
+# elif defined(TMPL_LM64)
+#  define TMPL_MODE         BS3_MODE_LM64
+# else
+#  error "Unable to to figure out the template mode."
 # endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_RM' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_RM' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_RM' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_RM' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_RM' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_RM' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_RM' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_RM' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_RM' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_RM' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_RM' and 'TMPL_LM64' are defined."
-# endif
+#endif
+
+
+/*
+ * Check the code bitness and set derived defines.
+ */
+#if (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_16
 # if ARCH_BITS != 16
-#  error "TMPL_RM requires ARCH_BITS to be 16."
+#  error "BS3_MODE_CODE_16 requires ARCH_BITS to be 16."
 # endif
 # define TMPL_16BIT
 # define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_rm)
+# define TMPL_UNDERSCORE        _
 # define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "real mode"
+
+#elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_32
+# if ARCH_BITS != 32
+#  error "BS3_MODE_CODE_32 requires ARCH_BITS to be 32."
+# endif
+# define TMPL_32BIT
+# define TMPL_BITS              32
+# define TMPL_UNDERSCORE        _
+# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c32)
+
+#elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_V86
+# if ARCH_BITS != 16
+#  error "BS3_MODE_CODE_V86 requires ARCH_BITS to be 16."
+# endif
+# define TMPL_16BIT
+# define TMPL_BITS              16
+# define TMPL_UNDERSCORE        _
+# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
+# define TMPL_CMN_R86
+# define TMPL_CMN_V86
+
+#elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_64
+# if ARCH_BITS != 64
+#  error "BS3_MODE_CODE_64 requires ARCH_BITS to be 64."
+# endif
+# define TMPL_64BIT
+# define TMPL_BITS              64
+# define TMPL_UNDERSCORE
+# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c64)
+
+#else
+# error "Invalid TMPL_MODE value!"
+#endif
+
+
+/*
+ * Check the system specific mask and set derived values.
+ */
+#if (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_RM
 # define TMPL_HAVE_BIOS
 # define TMPL_CMN_R86
-#endif
-
-#ifdef TMPL_PE16
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PE16' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PE16' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_RM' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PE16' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PE16' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PE16' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PE16' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PE16' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PE32' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PE16' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PE16' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PE16' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PE16 requires ARCH_BITS to be 16."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE16
+# define TMPL_SYS_PE16
 # define TMPL_CMN_PE
-# define TMPL_CMN_P16
-# define TMPL_16BIT
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pe16)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "16-bit unpaged protected mode"
-#endif
-
-#ifdef TMPL_PE32
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PE32' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PE32' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PE32' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PE32' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PE32' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PE32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PE32' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PE32' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAE86
-#  error "Both 'TMPL_PE32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PE32' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PE32' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PE32' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 32
-#  error "TMPL_PE32 requires ARCH_BITS to be 32."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE32
+# define TMPL_SYS_PE32
 # define TMPL_CMN_PE
-# define TMPL_CMN_P32
-# define TMPL_32BIT
-# define TMPL_BITS              32
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pe32)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c32)
-# define TMPL_MODE_STR          "32-bit unpaged protected mode"
-#endif
-
-#ifdef TMPL_PEV86
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PEV86' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PEV86' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PEV86' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PEV86' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PEV86' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PEV86' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PEV86' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PEV86' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAE86
-#  error "Both 'TMPL_PEV86' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PEV86' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PEV86' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PEV86' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PEV86 requires ARCH_BITS to be 16."
-# endif
-# define TMPL_CMN_PE
-# define TMPL_CMN_V86
-# define TMPL_CMN_R86
-# define TMPL_16BIT
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pev86)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "v8086 unpaged protected mode"
-#endif
-
-#ifdef TMPL_PP16
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PP16' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PP16' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PP16' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PP16' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PP16' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PP32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PP16' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PP16' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PP16' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PP16' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PP16' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PP16' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PP16 requires ARCH_BITS to be 16."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP16
+# define TMPL_SYS_PP16
 # define TMPL_CMN_PP
 # define TMPL_CMN_PAGING
-# define TMPL_CMN_P16
-# define TMPL_16BIT
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pp16)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "16-bit paged protected mode"
-#endif
-
-#ifdef TMPL_PP32
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PP32' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PP32' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PP32' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PP32' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PP32' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PP32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PP32' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PP32' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PP32' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PP32' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PP32' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PP32' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 32
-#  error "TMPL_PP32 requires ARCH_BITS to be 32."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP32
+# define TMPL_SYS_PP32
 # define TMPL_CMN_PP
 # define TMPL_CMN_PAGING
-# define TMPL_CMN_P32
-# define TMPL_32BIT
-# define TMPL_BITS              32
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pp32)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c32)
-# define TMPL_MODE_STR          "32-bit paged protected mode"
-#endif
-
-#ifdef TMPL_PPV86
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PPV86' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PPV86' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PPV86' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PPV86' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PPV86' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PPV86' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PPV86' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PPV86' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PPV86' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PPV86' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PPV86' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PPV86' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PPV86 requires ARCH_BITS to be 16."
-# endif
-# define TMPL_CMN_PP
-# define TMPL_CMN_PAGING
-# define TMPL_CMN_V86
-# define TMPL_CMN_R86
-# define TMPL_16BIT
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_ppv86)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c86)
-# define TMPL_MODE_STR          "v8086 paged protected mode"
-#endif
-
-#ifdef TMPL_PAE16
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PAE16' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PAE16' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PAE16' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PAE16' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PAE16' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PAE16' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PAE16' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_PAE16' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PAE16' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PAE16' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PAE16' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PAE16' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PAE16 requires ARCH_BITS to be 16."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE16
+# define TMPL_SYS_PAE16
 # define TMPL_CMN_PAE
 # define TMPL_CMN_PAGING
-# define TMPL_16BIT
-# define TMPL_CMN_P16
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pae16)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "16-bit pae protected mode"
-#endif
-
-#ifdef TMPL_PAE32
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PAE32' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PAE32' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PAE32' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PAE32' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PAE32' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PAE32' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PAE32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PAE32' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PAE32' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PAE32' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PAE32' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PAE32' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 32
-#  error "TMPL_PAE32 requires ARCH_BITS to be 32."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE32
+# define TMPL_SYS_PAE32
 # define TMPL_CMN_PAE
 # define TMPL_CMN_PAGING
-# define TMPL_CMN_P32
-# define TMPL_32BIT
-# define TMPL_BITS              32
-# define TMPL_NM(Name)          RT_CONCAT(Name,_pae32)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c32)
-# define TMPL_MODE_STR          "32-bit pae protected mode"
-#endif
-
-#ifdef TMPL_PAEV86
-# ifdef TMPL_RM
-#  error "Both 'TMPL_PAEV86' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_PAEV86' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_PAEV86' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_PAEV86' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_PAEV86' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_PAEV86 requires ARCH_BITS to be 16."
-# endif
-# define TMPL_CMN_PAE
-# define TMPL_CMN_PAGING
-# define TMPL_CMN_V86
-# define TMPL_CMN_R86
-# define TMPL_16BIT
-# define TMPL_BITS              16
-# define TMPL_NM(Name)          RT_CONCAT(Name,_paev86)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c86)
-# define TMPL_MODE_STR          "v8086 pae protected mode"
-#endif
-
-#ifdef TMPL_LM16
-# ifdef TMPL_RM
-#  error "Both 'TMPL_LM16' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_LM16' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_LM16' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_LM16' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_LM16' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_LM16' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_LM16' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_LM16' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_LM16' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_LM16' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_LM16' and 'TMPL_LM32' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_LM16' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 16
-#  error "TMPL_LM16 requires ARCH_BITS to be 16."
-# endif
+
+#elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_LM
+# define TMPL_SYS_LM
 # define TMPL_CMN_LM
 # define TMPL_CMN_PAGING
-# define TMPL_CMN_P16
-# define TMPL_16BIT
-# define TMPL_BITS              16
+
+#else
+# error "Invalid TMPL_MODE value!"
+#endif
+
+
+/*
+ * Mode specific stuff.
+ */
+#if   TMPL_MODE == BS3_MODE_RM
+# define TMPL_RM                1
+# define TMPL_MODE_STR          "real mode"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_rm)
+# define TMPL_MODE_LNAME        rm
+# define TMPL_MODE_UNAME        RM
+
+
+#elif TMPL_MODE == BS3_MODE_PE16
+# define TMPL_PE16              1
+# define TMPL_MODE_STR          "16-bit prot, 16-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pe16)
+# define TMPL_MODE_LNAME        pe16
+# define TMPL_MODE_UNAME        PE16
+
+#elif TMPL_MODE == BS3_MODE_PE16_32
+# define TMPL_PE16_32           1
+# define TMPL_MODE_STR          "16-bit prot, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pe16_32)
+# define TMPL_MODE_LNAME        pe16_32
+# define TMPL_MODE_UNAME        PE16_32
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PE16_V86
+# define TMPL_PE16_V86          1
+# define TMPL_MODE_STR          "16-bit prot, v8086"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pe16_v86)
+# define TMPL_MODE_LNAME        pe16_v86
+# define TMPL_MODE_UNAME        PE16_v86
+# define TMPL_CMN_WEIRD
+# define TMPL_CMN_WEIRD_V86
+
+
+#elif TMPL_MODE == BS3_MODE_PE32
+# define TMPL_PE32              1
+# define TMPL_MODE_STR          "32-bit prot, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pe32)
+# define TMPL_MODE_LNAME        pe32
+# define TMPL_MODE_UNAME        PE32
+
+#elif TMPL_MODE == BS3_MODE_PE32_16
+# define TMPL_PE32_16           1
+# define TMPL_MODE_STR          "32-bit prot, 16-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pe32_16)
+# define TMPL_MODE_LNAME        pe32_16
+# define TMPL_MODE_UNAME        PE32_16
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PEV86
+# define TMPL_PEV86             1
+# define TMPL_MODE_STR          "32-bit prot, v8086"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pev86)
+# define TMPL_MODE_LNAME        pev86
+# define TMPL_MODE_UNAME        PEV86
+
+
+#elif TMPL_MODE == BS3_MODE_PP16
+# define TMPL_PP16              1
+# define TMPL_MODE_STR          "16-bit paged, 16-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pp16)
+# define TMPL_MODE_LNAME        pp16
+# define TMPL_MODE_UNAME        PP16
+
+#elif TMPL_MODE == BS3_MODE_PP16_32
+# define TMPL_PP16_32           1
+# define TMPL_MODE_STR          "16-bit paged, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pp16_32)
+# define TMPL_MODE_LNAME        pp16_32
+# define TMPL_MODE_UNAME        PP16_32
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PP16_V86
+# define TMPL_PP16_V86          1
+# define TMPL_MODE_STR          "16-bit paged, v8086"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pp16_v86)
+# define TMPL_MODE_LNAME        pp16_v86
+# define TMPL_MODE_UNAME        PP16_v86
+# define TMPL_CMN_WEIRD
+# define TMPL_CMN_WEIRD_V86
+
+
+#elif TMPL_MODE == BS3_MODE_PP32
+# define TMPL_PP32              1
+# define TMPL_MODE_STR          "32-bit paged, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pp32)
+# define TMPL_MODE_LNAME        pp32
+# define TMPL_MODE_UNAME        PP32
+
+#elif TMPL_MODE == BS3_MODE_PP32_16
+# define TMPL_PP32_16           1
+# define TMPL_MODE_STR          "32-bit paged, 16-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pp32_16)
+# define TMPL_MODE_LNAME        pp32_16
+# define TMPL_MODE_UNAME        PP32_16
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PPV86
+# define TMPL_PPV86             1
+# define TMPL_MODE_STR          "32-bit paged, v8086"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_ppv86)
+# define TMPL_MODE_LNAME        ppv86
+# define TMPL_MODE_UNAME        PPV86
+
+
+#elif TMPL_MODE == BS3_MODE_PAE16
+# define TMPL_PAE16             1
+# define TMPL_MODE_STR          "16-bit pae, 16-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pae16)
+# define TMPL_MODE_LNAME        pae16
+# define TMPL_MODE_UNAME        PAE16
+
+#elif TMPL_MODE == BS3_MODE_PAE16_32
+# define TMPL_PAE16_32          1
+# define TMPL_MODE_STR          "16-bit pae, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pae16_32)
+# define TMPL_MODE_LNAME        pae16_32
+# define TMPL_MODE_UNAME        PAE16_32
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PAE16_V86
+# define TMPL_PAE16_V86         1
+# define TMPL_MODE_STR          "16-bit pae, v8086"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pae16_v86)
+# define TMPL_MODE_LNAME        pae16_v86
+# define TMPL_MODE_UNAME        PAE16_v86
+# define TMPL_CMN_WEIRD
+# define TMPL_CMN_WEIRD_V86
+
+
+#elif TMPL_MODE == BS3_MODE_PAE32
+# define TMPL_PAE32             1
+# define TMPL_MODE_STR          "32-bit pae, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pae32)
+# define TMPL_MODE_LNAME        pae32
+# define TMPL_MODE_UNAME        PAE32
+
+#elif TMPL_MODE == BS3_MODE_PAE32_16
+# define TMPL_PAE32_16          1
+# define TMPL_MODE_STR          "32-bit pae, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_pae32_16)
+# define TMPL_MODE_LNAME        pae32_16
+# define TMPL_MODE_UNAME        PAE32_16
+# define TMPL_CMN_WEIRD
+
+#elif TMPL_MODE == BS3_MODE_PAEV86
+# define TMPL_PAEV86            1
+# define TMPL_MODE_STR          "32-bit pae, v8086 pae"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_paev86)
+# define TMPL_MODE_LNAME        paev86
+# define TMPL_MODE_UNAME        PAEV86
+
+
+#elif TMPL_MODE == BS3_MODE_LM16
+# define TMPL_LM16              1
+# define TMPL_MODE_STR          "long, 16-bit"
 # define TMPL_NM(Name)          RT_CONCAT(Name,_lm16)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c16)
-# define TMPL_MODE_STR          "16-bit long mode"
+# define TMPL_MODE_LNAME        lm16
+# define TMPL_MODE_UNAME        LM16
+
+#elif TMPL_MODE == BS3_MODE_LM32
+# define TMPL_LM32              1
+# define TMPL_MODE_STR          "long, 32-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_lm32)
+# define TMPL_MODE_LNAME        lm32
+# define TMPL_MODE_UNAME        LM32
+
+#elif TMPL_MODE == BS3_MODE_LM64
+# define TMPL_LM64              1
+# define TMPL_MODE_STR          "long, 64-bit"
+# define TMPL_NM(Name)          RT_CONCAT(Name,_lm64)
+# define TMPL_MODE_LNAME        lm64
+# define TMPL_MODE_UNAME        LM64
+
+#else
+# error "Invalid TMPL_MODE value!!"
 #endif
 
-#ifdef TMPL_LM32
-# ifdef TMPL_RM
-#  error "Both 'TMPL_LM32' and 'TMPL_RM' are defined."
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_LM32' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_LM32' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_LM32' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_LM32' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_LM32' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_LM32' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_LM32' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_LM32' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_LM32' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_LM32' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM64
-#  error "Both 'TMPL_LM32' and 'TMPL_LM64' are defined."
-# endif
-# if ARCH_BITS != 32
-#  error "TMPL_LM32 requires ARCH_BITS to be 32."
-# endif
-# define TMPL_CMN_LM
-# define TMPL_CMN_PAGING
-# define TMPL_CMN_P32
-# define TMPL_32BIT
-# define TMPL_BITS              32
-# define TMPL_NM(Name)          RT_CONCAT(Name,_lm32)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c32)
-# define TMPL_MODE_STR          "32-bit long mode"
-#endif
-
-#ifdef TMPL_LM64
-# ifdef TMPL_RM
-#  error ""Both 'TMPL_LM64' and 'TMPL_RM' are defined.""
-# endif
-# ifdef TMPL_PE16
-#  error "Both 'TMPL_LM64' and 'TMPL_PE16' are defined."
-# endif
-# ifdef TMPL_PE32
-#  error "Both 'TMPL_LM64' and 'TMPL_PE32' are defined."
-# endif
-# ifdef TMPL_PEV86
-#  error "Both 'TMPL_LM64' and 'TMPL_PEV86' are defined."
-# endif
-# ifdef TMPL_PP16
-#  error "Both 'TMPL_LM64' and 'TMPL_PP16' are defined."
-# endif
-# ifdef TMPL_PP32
-#  error "Both 'TMPL_LM64' and 'TMPL_PP32' are defined."
-# endif
-# ifdef TMPL_PPV86
-#  error "Both 'TMPL_LM64' and 'TMPL_PPV86' are defined."
-# endif
-# ifdef TMPL_PAE16
-#  error "Both 'TMPL_LM64' and 'TMPL_PAE16' are defined."
-# endif
-# ifdef TMPL_PAE32
-#  error "Both 'TMPL_LM64' and 'TMPL_PAE32' are defined."
-# endif
-# ifdef TMPL_PAEV86
-#  error "Both 'TMPL_LM64' and 'TMPL_PAEV86' are defined."
-# endif
-# ifdef TMPL_LM16
-#  error "Both 'TMPL_LM64' and 'TMPL_LM16' are defined."
-# endif
-# ifdef TMPL_LM32
-#  error "Both 'TMPL_LM64' and 'TMPL_LM32' are defined."
-# endif
-# if ARCH_BITS != 64
-#  error "TMPL_LM64 requires ARCH_BITS to be 64."
-# endif
-# define TMPL_CMN_LM
-# define TMPL_CMN_PAGING
-# define TMPL_CMN_P64
-# define TMPL_64BIT
-# define TMPL_BITS              64
-# define TMPL_NM(Name)          RT_CONCAT(Name,_lm64)
-# define BS3_CMN_NM(Name)       RT_CONCAT(Name,_c64)
-# define TMPL_MODE_STR          "64-bit long mode"
-#endif
 
 #ifndef TMPL_MODE_STR
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 59863)
@@ -53,668 +53,318 @@
 ;       - TMPL_CMN_PAGING = TMPL_CMN_PP | TMPL_CMN_PAE | TMPL_CMN_LM
 ;
-%ifdef TMPL_RM
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_RM' and 'TMPL_PE16' are defined."
+
+
+;
+; Convert TMPL_XXX to TMPL_MODE.
+;
+%ifndef TMPL_MODE
+ %ifdef TMPL_RM
+  %define TMPL_MODE         BS3_MODE_RM
+ %elifdef TMPL_PE16
+  %define TMPL_MODE         BS3_MODE_PE16
+ %elifdef TMPL_PE16_32
+  %define TMPL_MODE         BS3_MODE_PE16_32
+ %elifdef TMPL_PE16_V86
+  %define TMPL_MODE         BS3_MODE_PE16_V86
+ %elifdef TMPL_PE32
+  %define TMPL_MODE         BS3_MODE_PE32
+ %elifdef TMPL_PE32_16
+  %define TMPL_MODE         BS3_MODE_PE32_16
+ %elifdef TMPL_PEV86
+  %define TMPL_MODE         BS3_MODE_PEV86
+ %elifdef TMPL_PP16
+  %define TMPL_MODE         BS3_MODE_PP16
+ %elifdef TMPL_PP16_32
+  %define TMPL_MODE         BS3_MODE_PP16_32
+ %elifdef TMPL_PP16_V86
+  %define TMPL_MODE         BS3_MODE_PP16_V86
+ %elifdef TMPL_PP32
+  %define TMPL_MODE         BS3_MODE_PP32
+ %elifdef TMPL_PP32_16
+  %define TMPL_MODE         BS3_MODE_PP32_16
+ %elifdef TMPL_PPV86
+  %define TMPL_MODE         BS3_MODE_PPV86
+ %elifdef TMPL_PAE16
+  %define TMPL_MODE         BS3_MODE_PAE16
+ %elifdef TMPL_PAE16_32
+  %define TMPL_MODE         BS3_MODE_PAE16_32
+ %elifdef TMPL_PAE16_V86
+  %define TMPL_MODE         BS3_MODE_PAE16_V86
+ %elifdef TMPL_PAE32
+  %define TMPL_MODE         BS3_MODE_PAE32
+ %elifdef TMPL_PAE32_16
+  %define TMPL_MODE         BS3_MODE_PAE32_16
+ %elifdef TMPL_PAEV86
+  %define TMPL_MODE         BS3_MODE_PAEV86
+ %elifdef TMPL_LM16
+  %define TMPL_MODE         BS3_MODE_LM16
+ %elifdef TMPL_LM32
+  %define TMPL_MODE         BS3_MODE_LM32
+ %elifdef TMPL_LM64
+  %define TMPL_MODE         BS3_MODE_LM64
+ %else
+  %error "Unable to to figure out the template mode."
  %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_RM' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_RM' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_RM' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_RM' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_RM' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_RM' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_RM' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_RM' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_RM' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_RM' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_RM' and 'TMPL_LM64' are defined."
- %endif
+%endif
+
+;
+; Check the code bitness and set TMPL_XXBITS, TMPL_BITS, BS3_CMN_NM
+;
+%if (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_16
  %define TMPL_16BIT
  %define TMPL_BITS              16
  %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _rm
+ %define TMPL_UNDERSCORE        _
  %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        rm
- %define TMPL_MODE_UNAME        RM
- %define TMPL_MODE_STR          'real mode'
- %define TMPL_HAVE_BIOS
- %define TMPL_CMN_R86
-%endif
-
-%ifdef TMPL_PE16
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PE16' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PE16' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_RM' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PE16' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PE16' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PE16' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PE16' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PE16' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PE32' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PE16' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PE16' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PE16' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PE
- %define TMPL_CMN_P16
- %define TMPL_16BIT
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _pe16
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pe16
- %define TMPL_MODE_UNAME        PE16
- %define TMPL_MODE_STR          '16-bit unpaged protected mode'
-%endif
-
-%ifdef TMPL_PE32
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PE32' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PE32' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PE32' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PE32' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PE32' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PE32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PE32' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PE32' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAE86
-  %error "Both 'TMPL_PE32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PE32' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PE32' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PE32' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PE
- %define TMPL_CMN_P32
+
+%elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_32
  %define TMPL_32BIT
  %define TMPL_BITS              32
  %define TMPL_PTR_DEF           dd
- %define TMPL_NM(Name)          _ %+ Name %+ _pe32
+ %define TMPL_UNDERSCORE        _
  %define BS3_CMN_NM(Name)       _ %+ Name %+ _c32
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pe32
- %define TMPL_MODE_UNAME        PE32
- %define TMPL_MODE_STR          '32-bit unpaged protected mode'
-%endif
-
-%ifdef TMPL_PEV86
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PEV86' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PEV86' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PEV86' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PEV86' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PEV86' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PEV86' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PEV86' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PEV86' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAE86
-  %error "Both 'TMPL_PEV86' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PEV86' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PEV86' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PEV86' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PE
- %define TMPL_CMN_V86
- %define TMPL_CMN_R86
+
+%elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_V86
  %define TMPL_16BIT
  %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _pev86
+ %define TMPL_UNDERSCORE        _
  %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pev86
- %define TMPL_MODE_UNAME        PEV86
- %define TMPL_MODE_STR          'v8086 unpaged protected mode'
-%endif
-
-%ifdef TMPL_PP16
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PP16' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PP16' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PP16' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PP16' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PP16' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PP32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PP16' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PP16' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PP16' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PP16' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PP16' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PP16' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PP
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P16
- %define TMPL_16BIT
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _pp16
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pp16
- %define TMPL_MODE_UNAME        PP16
- %define TMPL_MODE_STR          '16-bit paged protected mode'
-%endif
-
-%ifdef TMPL_PP32
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PP32' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PP32' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PP32' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PP32' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PP32' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PP32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PP32' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PP32' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PP32' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PP32' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PP32' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PP32' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PP
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P32
- %define TMPL_32BIT
- %define TMPL_BITS              32
- %define TMPL_PTR_DEF           dd
- %define TMPL_NM(Name)          _ %+ Name %+ _pp32
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c32
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pp32
- %define TMPL_MODE_UNAME        PP32
- %define TMPL_MODE_STR          '32-bit paged protected mode'
-%endif
-
-%ifdef TMPL_PPV86
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PPV86' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PPV86' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PPV86' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PPV86' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PPV86' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PPV86' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PPV86' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PPV86' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PPV86' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PPV86' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PPV86' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PPV86' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PP
- %define TMPL_CMN_PAGING
+ %define TMPL_CMN_R86
  %define TMPL_CMN_V86
- %define TMPL_CMN_R86
- %define TMPL_16BIT
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _ppv86
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c86
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        ppv86
- %define TMPL_MODE_UNAME        PPV86
- %define TMPL_MODE_STR          'v8086 paged protected mode'
-%endif
-
-%ifdef TMPL_PAE16
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PAE16' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PAE16' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PAE16' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PAE16' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PAE16' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PAE16' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PAE16' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PAE16' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PAE16' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PAE16' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PAE16' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PAE16' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PAE
- %define TMPL_CMN_PAGING
- %define TMPL_16BIT
- %define TMPL_CMN_P16
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _pae16
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pae16
- %define TMPL_MODE_UNAME        PAE16
- %define TMPL_MODE_STR          '16-bit pae protected mode'
-%endif
-
-%ifdef TMPL_PAE32
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PAE32' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PAE32' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PAE32' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PAE32' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PAE32' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PAE32' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PAE32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PAE32' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PAE32' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PAE32' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PAE32' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PAE32' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PAE
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P32
- %define TMPL_32BIT
- %define TMPL_BITS              32
- %define TMPL_PTR_DEF           dd
- %define TMPL_NM(Name)          _ %+ Name %+ _pae32
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c32
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        pae32
- %define TMPL_MODE_UNAME        PAE32
- %define TMPL_MODE_STR          '32-bit pae protected mode'
-%endif
-
-%ifdef TMPL_PAEV86
- %ifdef TMPL_RM
-  %error "Both 'TMPL_PAEV86' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_PAEV86' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_PAEV86' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_PAEV86' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_PAE
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_V86
- %define TMPL_CMN_R86
- %define TMPL_16BIT
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _paev86
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c86
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        paev86
- %define TMPL_MODE_UNAME        PAEV86
- %define TMPL_MODE_STR          'v8086 pae protected mode'
-%endif
-
-%ifdef TMPL_LM16
- %ifdef TMPL_RM
-  %error "Both 'TMPL_LM16' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_LM16' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_LM16' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_LM16' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_LM16' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_LM16' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_LM16' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_LM16' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_LM16' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_LM16' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_LM16' and 'TMPL_LM32' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_LM16' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_LM
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P16
- %define TMPL_16BIT
- %define TMPL_BITS              16
- %define TMPL_PTR_DEF           dw
- %define TMPL_NM(Name)          _ %+ Name %+ _lm16
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c16
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        lm16
- %define TMPL_MODE_UNAME        LM16
- %define TMPL_MODE_STR          '16-bit long mode'
-%endif
-
-%ifdef TMPL_LM32
- %ifdef TMPL_RM
-  %error "Both 'TMPL_LM32' and 'TMPL_RM' are defined."
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_LM32' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_LM32' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_LM32' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_LM32' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_LM32' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_LM32' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_LM32' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_LM32' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_LM32' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_LM32' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM64
-  %error "Both 'TMPL_LM32' and 'TMPL_LM64' are defined."
- %endif
- %define TMPL_CMN_LM
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P32
- %define TMPL_32BIT
- %define TMPL_BITS              32
- %define TMPL_PTR_DEF           dd
- %define TMPL_NM(Name)          _ %+ Name %+ _lm32
- %define BS3_CMN_NM(Name)       _ %+ Name %+ _c32
- %define TMPL_UNDERSCORE        _
- %define TMPL_MODE_LNAME        lm32
- %define TMPL_MODE_UNAME        LM32
- %define TMPL_MODE_STR          '32-bit long mode'
-%endif
-
-%ifdef TMPL_LM64
- %ifdef TMPL_RM
-  %error ""Both 'TMPL_LM64' and 'TMPL_RM' are defined.""
- %endif
- %ifdef TMPL_PE16
-  %error "Both 'TMPL_LM64' and 'TMPL_PE16' are defined."
- %endif
- %ifdef TMPL_PE32
-  %error "Both 'TMPL_LM64' and 'TMPL_PE32' are defined."
- %endif
- %ifdef TMPL_PEV86
-  %error "Both 'TMPL_LM64' and 'TMPL_PEV86' are defined."
- %endif
- %ifdef TMPL_PP16
-  %error "Both 'TMPL_LM64' and 'TMPL_PP16' are defined."
- %endif
- %ifdef TMPL_PP32
-  %error "Both 'TMPL_LM64' and 'TMPL_PP32' are defined."
- %endif
- %ifdef TMPL_PPV86
-  %error "Both 'TMPL_LM64' and 'TMPL_PPV86' are defined."
- %endif
- %ifdef TMPL_PAE16
-  %error "Both 'TMPL_LM64' and 'TMPL_PAE16' are defined."
- %endif
- %ifdef TMPL_PAE32
-  %error "Both 'TMPL_LM64' and 'TMPL_PAE32' are defined."
- %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_LM64' and 'TMPL_PAEV86' are defined."
- %endif
- %ifdef TMPL_LM16
-  %error "Both 'TMPL_LM64' and 'TMPL_LM16' are defined."
- %endif
- %ifdef TMPL_LM32
-  %error "Both 'TMPL_LM64' and 'TMPL_LM32' are defined."
- %endif
- %define TMPL_CMN_LM
- %define TMPL_CMN_PAGING
- %define TMPL_CMN_P64
+
+%elif (TMPL_MODE & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_64
  %define TMPL_64BIT
  %define TMPL_BITS              64
  %define TMPL_PTR_DEF           dq
- %define TMPL_NM(Name)          Name %+ _lm64   ; No underscore (C/C++ compatibility).
- %define BS3_CMN_NM(Name)       Name %+ _c64    ; No underscore (C/C++ compatibility).
  %define TMPL_UNDERSCORE
+ %define BS3_CMN_NM(Name)       Name %+ _c64
+
+%else
+ %error "Invalid TMPL_MODE value!"
+%endif
+
+;
+; Check the system specific mask and set derived values.
+;
+%if (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_RM
+ %define TMPL_HAVE_BIOS
+ %define TMPL_CMN_R86
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE16
+ %define TMPL_SYS_PE16
+ %define TMPL_CMN_PE
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE32
+ %define TMPL_SYS_PE32
+ %define TMPL_CMN_PE
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP16
+ %define TMPL_SYS_PP16
+ %define TMPL_CMN_PP
+ %define TMPL_CMN_PAGING
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP32
+ %define TMPL_SYS_PP32
+ %define TMPL_CMN_PP
+ %define TMPL_CMN_PAGING
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE16
+ %define TMPL_SYS_PAE16
+ %define TMPL_CMN_PAE
+ %define TMPL_CMN_PAGING
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE32
+ %define TMPL_SYS_PAE32
+ %define TMPL_CMN_PAE
+ %define TMPL_CMN_PAGING
+
+%elif (TMPL_MODE & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_LM
+ %define TMPL_SYS_LM
+ %define TMPL_CMN_LM
+ %define TMPL_CMN_PAGING
+
+%else
+ %error "Invalid TMPL_MODE value!"
+%endif
+
+
+;
+; Mode specific stuff.
+;
+%if   TMPL_MODE == BS3_MODE_RM
+ %define TMPL_RM
+ %define TMPL_MODE_STR          "real mode"
+ %define TMPL_NM(Name)          _ %+ Name %+ _rm
+ %define TMPL_MODE_LNAME        rm
+ %define TMPL_MODE_UNAME        RM
+
+
+%elif TMPL_MODE == BS3_MODE_PE16
+ %define TMPL_PE16
+ %define TMPL_MODE_STR          "16-bit prot, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pe16
+ %define TMPL_MODE_LNAME        pe16
+ %define TMPL_MODE_UNAME        PE16
+
+%elif TMPL_MODE == BS3_MODE_PE16_32
+ %define TMPL_PE16_32
+ %define TMPL_MODE_STR          "16-bit prot, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pe16_32
+ %define TMPL_MODE_LNAME        pe16_32
+ %define TMPL_MODE_UNAME        PE16_32
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PE16_V86
+ %define TMPL_PE16_V86
+ %define TMPL_MODE_STR          "16-bit prot, v8086"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pe16_v86
+ %define TMPL_MODE_LNAME        pe16_v86
+ %define TMPL_MODE_UNAME        PE16_v86
+ %define TMPL_CMN_WEIRD
+ %define TMPL_CMN_WEIRD_V86
+
+
+%elif TMPL_MODE == BS3_MODE_PE32
+ %define TMPL_PE32
+ %define TMPL_MODE_STR          "32-bit prot, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pe32
+ %define TMPL_MODE_LNAME        pe32
+ %define TMPL_MODE_UNAME        PE32
+
+%elif TMPL_MODE == BS3_MODE_PE32_16
+ %define TMPL_PE32_16
+ %define TMPL_MODE_STR          "32-bit prot, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pe32_16
+ %define TMPL_MODE_LNAME        pe32_16
+ %define TMPL_MODE_UNAME        PE32_16
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PEV86
+ %define TMPL_PEV86
+ %define TMPL_MODE_STR          "32-bit prot, v8086"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pev86
+ %define TMPL_MODE_LNAME        pev86
+ %define TMPL_MODE_UNAME        PEV86
+
+
+%elif TMPL_MODE == BS3_MODE_PP16
+ %define TMPL_PP16
+ %define TMPL_MODE_STR          "16-bit paged, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pp16
+ %define TMPL_MODE_LNAME        pp16
+ %define TMPL_MODE_UNAME        PP16
+
+%elif TMPL_MODE == BS3_MODE_PP16_32
+ %define TMPL_PP16_32
+ %define TMPL_MODE_STR          "16-bit paged, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pp16_32
+ %define TMPL_MODE_LNAME        pp16_32
+ %define TMPL_MODE_UNAME        PP16_32
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PP16_V86
+ %define TMPL_PP16_V86
+ %define TMPL_MODE_STR          "16-bit paged, v8086"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pp16_v86
+ %define TMPL_MODE_LNAME        pp16_v86
+ %define TMPL_MODE_UNAME        PP16_v86
+ %define TMPL_CMN_WEIRD
+ %define TMPL_CMN_WEIRD_V86
+
+
+%elif TMPL_MODE == BS3_MODE_PP32
+ %define TMPL_PP32
+ %define TMPL_MODE_STR          "32-bit paged, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pp32
+ %define TMPL_MODE_LNAME        pp32
+ %define TMPL_MODE_UNAME        PP32
+
+%elif TMPL_MODE == BS3_MODE_PP32_16
+ %define TMPL_PP32_16
+ %define TMPL_MODE_STR          "32-bit paged, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pp32_16
+ %define TMPL_MODE_LNAME        pp32_16
+ %define TMPL_MODE_UNAME        PP32_16
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PPV86
+ %define TMPL_PPV86
+ %define TMPL_MODE_STR          "32-bit paged, v8086"
+ %define TMPL_NM(Name)          _ %+ Name %+ _ppv86
+ %define TMPL_MODE_LNAME        ppv86
+ %define TMPL_MODE_UNAME        PPV86
+
+
+%elif TMPL_MODE == BS3_MODE_PAE16
+ %define TMPL_PAE16
+ %define TMPL_MODE_STR          "16-bit pae, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pae16
+ %define TMPL_MODE_LNAME        pae16
+ %define TMPL_MODE_UNAME        PAE16
+
+%elif TMPL_MODE == BS3_MODE_PAE16_32
+ %define TMPL_PAE16_32
+ %define TMPL_MODE_STR          "16-bit pae, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pae16_32
+ %define TMPL_MODE_LNAME        pae16_32
+ %define TMPL_MODE_UNAME        PAE16_32
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PAE16_V86
+ %define TMPL_PAE16_V86
+ %define TMPL_MODE_STR          "16-bit pae, v8086"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pae16_v86
+ %define TMPL_MODE_LNAME        pae16_v86
+ %define TMPL_MODE_UNAME        PAE16_v86
+ %define TMPL_CMN_WEIRD
+ %define TMPL_CMN_WEIRD_V86
+
+
+%elif TMPL_MODE == BS3_MODE_PAE32
+ %define TMPL_PAE32
+ %define TMPL_MODE_STR          "32-bit pae, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pae32
+ %define TMPL_MODE_LNAME        pae32
+ %define TMPL_MODE_UNAME        PAE32
+
+%elif TMPL_MODE == BS3_MODE_PAE32_16
+ %define TMPL_PAE32_16
+ %define TMPL_MODE_STR          "32-bit pae, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _pae32_16
+ %define TMPL_MODE_LNAME        pae32_16
+ %define TMPL_MODE_UNAME        PAE32_16
+ %define TMPL_CMN_WEIRD
+
+%elif TMPL_MODE == BS3_MODE_PAEV86
+ %define TMPL_PAEV86
+ %define TMPL_MODE_STR          "32-bit pae, v8086 pae"
+ %define TMPL_NM(Name)          _ %+ Name %+ _paev86
+ %define TMPL_MODE_LNAME        paev86
+ %define TMPL_MODE_UNAME        PAEV86
+
+
+%elif TMPL_MODE == BS3_MODE_LM16
+ %define TMPL_LM16
+ %define TMPL_MODE_STR          "long, 16-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _lm16
+ %define TMPL_MODE_LNAME        lm16
+ %define TMPL_MODE_UNAME        LM16
+
+%elif TMPL_MODE == BS3_MODE_LM32
+ %define TMPL_LM32
+ %define TMPL_MODE_STR          "long, 32-bit"
+ %define TMPL_NM(Name)          _ %+ Name %+ _lm32
+ %define TMPL_MODE_LNAME        lm32
+ %define TMPL_MODE_UNAME        LM32
+
+%elif TMPL_MODE == BS3_MODE_LM64
+ %define TMPL_LM64
+ %define TMPL_MODE_STR          "long, 64-bit"
+ %define TMPL_NM(Name)          Name %+ _lm64
  %define TMPL_MODE_LNAME        lm64
  %define TMPL_MODE_UNAME        LM64
- %define TMPL_MODE_STR          '64-bit long mode'
-%endif
-
-%ifndef TMPL_MODE_STR
- %error "internal error"
+
+%else
+ %error "Invalid TMPL_MODE value!!"
 %endif
 
@@ -739,104 +389,4 @@
 
 
-;
-; Register aliases.
-;
-%ifdef TMPL_64BIT
- %define xCB  8
- %define xDEF dq
- %define xRES resq
- %define xPRE qword
- %define xSP  rsp
- %define xBP  rbp
- %define xAX  rax
- %define xBX  rbx
- %define xCX  rcx
- %define xDX  rdx
- %define xDI  rdi
- %define xSI  rsi
- %define xWrtRIP wrt rip
- %define xPUSHF pushfq
- %define xPOPF  popfq
- %define xRETF  o64 retf
-%else
- %ifdef TMPL_32BIT
-  %define xCB  4
-  %define xDEF dd
-  %define xRES resd
-  %define xPRE dword
-  %define xSP  esp
-  %define xBP  ebp
-  %define xAX  eax
-  %define xBX  ebx
-  %define xCX  ecx
-  %define xDX  edx
-  %define xDI  edi
-  %define xSI  esi
-  %define xWrtRIP
-  %define xPUSHF pushfd
-  %define xPOPF  popfd
-  %define xRETF  retf
- %else
-  %ifndef TMPL_16BIT
-   %error "TMPL_XXBIT is not defined."
-  %endif
-  %define xCB  2
-  %define xDEF dw
-  %define xRES resw
-  %define xPRE word
-  %define xSP  sp
-  %define xBP  bp
-  %define xAX  ax
-  %define xBX  bx
-  %define xCX  cx
-  %define xDX  dx
-  %define xDI  di
-  %define xSI  si
-  %define xWrtRIP
-  %define xPUSHF pushf
-  %define xPOPF  popf
-  %define xRETF  retf
- %endif
-%endif
-
-
-;
-; Register names corresponding to the max size for pop/push <reg>.
-;
-; 16-bit can push both 32-bit and 16-bit registers.  This 's' prefixed variant
-; is used when 16-bit should use the 32-bit register.
-;
-%ifdef TMPL_64BIT
- %define sCB  8
- %define sDEF dq
- %define sRES resq
- %define sPRE qword
- %define sSP  rsp
- %define sBP  rbp
- %define sAX  rax
- %define sBX  rbx
- %define sCX  rcx
- %define sDX  rdx
- %define sDI  rdi
- %define sSI  rsi
- %define sPUSHF pushfq
- %define sPOPF  popfq
-%else
- %define sCB  4
- %define sDEF dd
- %define sRES resd
- %define sPRE dword
- %define sSP  esp
- %define sBP  ebp
- %define sAX  eax
- %define sBX  ebx
- %define sCX  ecx
- %define sDX  edx
- %define sDI  edi
- %define sSI  esi
- %define sPUSHF pushfd
- %define sPOPF  popfd
-%endif
-
 ;; @def TMPL_WRT_FLAT
 ; WRT flat when not in 16-bit modes.
@@ -866,14 +416,4 @@
  %define TMPL_WRT_SYSTEM16_OR_FLAT  wrt FLAT
 %endif
-
-;; @def BS3_EXTERN_TMPL
-; Mangles the given name into a template specific one.  For ease of use, the
-; name is redefined to the mangled one, just like BS3_EXTERN_CMN does.
-; @note Segment does not change.
-%macro BS3_EXTERN_TMPL 1
- extern TMPL_NM(%1)
- %undef  %1
- %define %1 TMPL_NM(%1)
-%endmacro
 
 ;
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 59863)
@@ -54,4 +54,80 @@
 /** @defgroup grp_bs3kit     BS3Kit
  * @{ */
+
+/** @name Execution modes.
+ * @{ */
+#define BS3_MODE_INVALID    UINT8_C(0x00)
+#define BS3_MODE_RM         UINT8_C(0x01)  /**< real mode. */
+#define BS3_MODE_PE16       UINT8_C(0x11)  /**< 16-bit protected mode kernel+tss, running 16-bit code, unpaged. */
+#define BS3_MODE_PE16_32    UINT8_C(0x12)  /**< 16-bit protected mode kernel+tss, running 32-bit code, unpaged. */
+#define BS3_MODE_PE16_V86   UINT8_C(0x13)  /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
+#define BS3_MODE_PE32       UINT8_C(0x22)  /**< 32-bit protected mode kernel+tss, running 32-bit code, unpaged. */
+#define BS3_MODE_PE32_16    UINT8_C(0x21)  /**< 32-bit protected mode kernel+tss, running 16-bit code, unpaged. */
+#define BS3_MODE_PEV86      UINT8_C(0x23)  /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
+#define BS3_MODE_PP16       UINT8_C(0x31)  /**< 16-bit protected mode kernel+tss, running 16-bit code, paged. */
+#define BS3_MODE_PP16_32    UINT8_C(0x32)  /**< 16-bit protected mode kernel+tss, running 32-bit code, paged. */
+#define BS3_MODE_PP16_V86   UINT8_C(0x33)  /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
+#define BS3_MODE_PP32       UINT8_C(0x42)  /**< 32-bit protected mode kernel+tss, running 32-bit code, paged. */
+#define BS3_MODE_PP32_16    UINT8_C(0x41)  /**< 32-bit protected mode kernel+tss, running 16-bit code, paged. */
+#define BS3_MODE_PPV86      UINT8_C(0x43)  /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
+#define BS3_MODE_PAE16      UINT8_C(0x51)  /**< 16-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
+#define BS3_MODE_PAE16_32   UINT8_C(0x52)  /**< 16-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
+#define BS3_MODE_PAE16_V86  UINT8_C(0x53)  /**< 16-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging. */
+#define BS3_MODE_PAE32      UINT8_C(0x62)  /**< 32-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
+#define BS3_MODE_PAE32_16   UINT8_C(0x61)  /**< 32-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
+#define BS3_MODE_PAEV86     UINT8_C(0x63)  /**< 32-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging. */
+#define BS3_MODE_LM16       UINT8_C(0x71)  /**< 16-bit long mode (paged), kernel+tss always 64-bit. */
+#define BS3_MODE_LM32       UINT8_C(0x72)  /**< 32-bit long mode (paged), kernel+tss always 64-bit. */
+#define BS3_MODE_LM64       UINT8_C(0x74)  /**< 64-bit long mode (paged), kernel+tss always 64-bit. */
+
+#define BS3_MODE_CODE_MASK  UINT8_C(0x0f)  /**< Running code mask. */
+#define BS3_MODE_CODE_16    UINT8_C(0x01)  /**< Running 16-bit code. */
+#define BS3_MODE_CODE_32    UINT8_C(0x02)  /**< Running 32-bit code. */
+#define BS3_MODE_CODE_V86   UINT8_C(0x03)  /**< Running 16-bit virtual 8086 code. */
+#define BS3_MODE_CODE_64    UINT8_C(0x04)  /**< Running 64-bit code. */
+
+#define BS3_MODE_SYS_MASK   UINT8_C(0xf0)  /**< kernel+tss mask. */
+#define BS3_MODE_SYS_RM     UINT8_C(0x00)  /**< Real mode kernel+tss. */
+#define BS3_MODE_SYS_PE16   UINT8_C(0x10)  /**< 16-bit protected mode kernel+tss. */
+#define BS3_MODE_SYS_PE32   UINT8_C(0x20)  /**< 32-bit protected mode kernel+tss. */
+#define BS3_MODE_SYS_PP16   UINT8_C(0x30)  /**< 16-bit paged protected mode kernel+tss. */
+#define BS3_MODE_SYS_PP32   UINT8_C(0x40)  /**< 32-bit paged protected mode kernel+tss. */
+#define BS3_MODE_SYS_PAE16  UINT8_C(0x50)  /**< 16-bit PAE paged protected mode kernel+tss. */
+#define BS3_MODE_SYS_PAE32  UINT8_C(0x60)  /**< 32-bit PAE paged protected mode kernel+tss. */
+#define BS3_MODE_SYS_LM     UINT8_C(0x70)  /**< 64-bit (paged) long mode protected mode kernel+tss. */
+
+/** Whether the mode has paging enabled. */
+#define BS3_MODE_IS_PAGED(a_fMode)              ((a_fMode) >= BS3_MODE_PP16)
+
+/** Whether the mode is running v8086 code. */
+#define BS3_MODE_IS_V86(a_fMode)                (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_V86)
+/** Whether the we're executing in real mode or v8086 mode. */
+#define BS3_MODE_IS_RM_OR_V86(a_fMode)          ((a_fMode) == BS3_MODE_RM || BS3_MODE_IS_V86(a_fMode))
+/** Whether the mode is running 16-bit code, except v8086. */
+#define BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode)  (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_16)
+/** Whether the mode is running 16-bit code (includes v8086). */
+#define BS3_MODE_IS_16BIT_CODE(a_fMode)         (BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode) || BS3_MODE_IS_V86(a_fMode))
+/** Whether the mode is running 32-bit code. */
+#define BS3_MODE_IS_32BIT_CODE(a_fMode)         (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_32)
+/** Whether the mode is running 64-bit code. */
+#define BS3_MODE_IS_64BIT_CODE(a_fMode)         (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_64)
+
+/** Whether the system is in real mode. */
+#define BS3_MODE_IS_RM_SYS(a_fMode)             (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_RM)
+/** Whether the system is some 16-bit mode that isn't real mode. */
+#define BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode)    (   ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE16 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP16 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE16)
+/** Whether the system is some 16-bit mode (includes real mode). */
+#define BS3_MODE_IS_16BIT_SYS(a_fMode)          (BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode) || BS3_MODE_IS_RM_SYS(a_fMode))
+/** Whether the system is some 32-bit mode. */
+#define BS3_MODE_IS_32BIT_SYS(a_fMode)          (   ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE32 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP32 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE32)
+/** Whether the system is long mode. */
+#define BS3_MODE_IS_64BIT_SYS(a_fMode)          (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_LM)
+
+/** @todo testcase: How would long-mode handle a 16-bit TSS loaded prior to the switch? (mainly stack switching wise) Hopefully, it will tripple fault, right? */
+/** @} */
 
 
@@ -722,4 +798,8 @@
 
 
+/** The current mode (BS3_MODE_XXX) of CPU \#0. */
+extern uint8_t BS3_FAR_DATA BS3_DATA_NM(g_bBs3CurrentMode);
+
+
 #ifdef __WATCOMC__
 /**
@@ -741,5 +821,9 @@
 # define BS3_IS_PROTECTED_MODE() (true)
 #else
-# define BS3_IS_PROTECTED_MODE() (Bs3AsmSmsw() & 1 /*PE*/)
+# if 1
+#  define BS3_IS_PROTECTED_MODE() (!BS3_MODE_IS_RM_SYS(BS3_DATA_NM(g_bBs3CurrentMode)))
+# else
+#  define BS3_IS_PROTECTED_MODE() (Bs3AsmSmsw() & 1 /*PE*/)
+# endif
 #endif
 
@@ -1235,4 +1319,13 @@
 BS3_DECL(void) Bs3TestInit_c64(const char BS3_FAR *pszTest); /**< @copydoc Bs3TestInit_c16 */
 #define Bs3TestInit BS3_CMN_NM(Bs3TestInit) /**< Selects #Bs3TestInit_c16, #Bs3TestInit_c32 or #Bs3TestInit_c64. */
+
+
+/**
+ * Equivalent to RTTestSummaryAndDestroy.
+ */
+BS3_DECL(void) Bs3TestTerm_c16(void);
+BS3_DECL(void) Bs3TestTerm_c32(void); /**< @copydoc Bs3TestTerm_c16 */
+BS3_DECL(void) Bs3TestTerm_c64(void); /**< @copydoc Bs3TestTerm_c16 */
+#define Bs3TestTerm BS3_CMN_NM(Bs3TestTerm) /**< Selects #Bs3TestTerm_c16, #Bs3TestTerm_c32 or #Bs3TestTerm_c64. */
 
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 59862)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 59863)
@@ -36,6 +36,114 @@
 %undef RT_ASMDEFS_INC_FIRST_FILE
 
+;;
+; Macro for setting register aliases according to the bit count given by %1.
+;
+%macro BS3_SET_REG_ALIASES 1
+ ;
+ ; Register aliases.
+ ;
+ %if %1 == 64
+  %define xCB  8
+  %define xDEF dq
+  %define xRES resq
+  %define xPRE qword
+  %define xSP  rsp
+  %define xBP  rbp
+  %define xAX  rax
+  %define xBX  rbx
+  %define xCX  rcx
+  %define xDX  rdx
+  %define xDI  rdi
+  %define xSI  rsi
+  %define xWrtRIP wrt rip
+  %define xPUSHF pushfq
+  %define xPOPF  popfq
+  %define xRETF  o64 retf
+ %elif %1 == 32
+  %define xCB  4
+  %define xDEF dd
+  %define xRES resd
+  %define xPRE dword
+  %define xSP  esp
+  %define xBP  ebp
+  %define xAX  eax
+  %define xBX  ebx
+  %define xCX  ecx
+  %define xDX  edx
+  %define xDI  edi
+  %define xSI  esi
+  %define xWrtRIP
+  %define xPUSHF pushfd
+  %define xPOPF  popfd
+  %define xRETF  retf
+ %elif %1 == 16
+  %define xCB  2
+  %define xDEF dw
+  %define xRES resw
+  %define xPRE word
+  %define xSP  sp
+  %define xBP  bp
+  %define xAX  ax
+  %define xBX  bx
+  %define xCX  cx
+  %define xDX  dx
+  %define xDI  di
+  %define xSI  si
+  %define xWrtRIP
+  %define xPUSHF pushf
+  %define xPOPF  popf
+  %define xRETF  retf
+ %else
+  %error "Invalid BS3_SET_REG_ALIASES argument:" %1
+ %endif
+
+
+ ;
+ ; Register names corresponding to the max size for pop/push <reg>.
+ ;
+ ; 16-bit can push both 32-bit and 16-bit registers.  This 's' prefixed variant
+ ; is used when 16-bit should use the 32-bit register.
+ ;
+ %if %1 == 64
+  %define sCB  8
+  %define sDEF dq
+  %define sRES resq
+  %define sPRE qword
+  %define sSP  rsp
+  %define sBP  rbp
+  %define sAX  rax
+  %define sBX  rbx
+  %define sCX  rcx
+  %define sDX  rdx
+  %define sDI  rdi
+  %define sSI  rsi
+  %define sPUSHF pushfq
+  %define sPOPF  popfq
+ %else
+  %define sCB  4
+  %define sDEF dd
+  %define sRES resd
+  %define sPRE dword
+  %define sSP  esp
+  %define sBP  ebp
+  %define sAX  eax
+  %define sBX  ebx
+  %define sCX  ecx
+  %define sDX  edx
+  %define sDI  edi
+  %define sSI  esi
+  %define sPUSHF pushfd
+  %define sPOPF  popfd
+ %endif
+%endmacro
+
+; Default to register aliases for ARCH_BITS.
+BS3_SET_REG_ALIASES ARCH_BITS
+
+
 ;; Wrapper around BITS.
-; Updates __BITS__ (built-in variable in nasm, we work it for yasm).
+; Updates __BITS__ (built-in variable in nasm, we work it for yasm) as well
+; a number of convenient macros and register aliases.
+;
 ; @param    %1      The CPU bit count: 16, 32 or 64
 ; @remarks ARCH_BITS is not modified and will remain what it was on the
@@ -81,7 +189,11 @@
  %undef   BS3_WRT_RIP
  %if %1 == 64
-  %define BS3_WRT_RIP               wrt rip
- %else
-  %define BS3_WRT_RIP
+  %ifdef __YASM__
+   %define BS3_WRT_RIP(a_Sym)       a_Sym wrt rip
+  %else
+   %define BS3_WRT_RIP(a_Sym)       rel a_Sym       ; Baka! Why couldn't they do 'wrt rip' like yasm?
+  %endif
+ %else
+  %define BS3_WRT_RIP(a_Sym)        a_Sym
  %endif
 
@@ -106,5 +218,8 @@
   %define BS3_IF_64BIT_OTHERWISE(a_64BitExpr, a_OtherwiseExpr) a_OtherwiseExpr
  %endif
-%endmacro
+
+ BS3_SET_REG_ALIASES %1
+%endmacro
+
 
 ;; Emulate the __BITS__ macro in NASM 2.0+. Follows BS3_SET_BITS.
@@ -123,7 +238,11 @@
 ; other modes. Follows BS3_SET_BITS.
 %if ARCH_BITS == 64
- %define BS3_WRT_RIP                wrt rip
+ %ifdef __YASM__
+  %define BS3_WRT_RIP(a_Sym)        a_Sym wrt rip
+ %else
+  %define BS3_WRT_RIP(a_Sym)        rel a_Sym       ; Baka! Why couldn't they do 'wrt rip' like yasm?
+ %endif
 %else
- %define BS3_WRT_RIP
+ %define BS3_WRT_RIP(a_Sym)         a_Sym
 %endif
 
@@ -363,4 +482,15 @@
 %endmacro
 
+;; @def BS3_EXTERN_TMPL
+; Mangles the given name into a template specific one.  For ease of use, the
+; name is redefined to the mangled one, just like BS3_EXTERN_CMN does.
+; @note Segment does not change.
+%macro BS3_EXTERN_TMPL 1
+ extern TMPL_NM(%1)
+ %undef  %1
+ %define %1 TMPL_NM(%1)
+%endmacro
+
+
 ;;
 ; Mangles a 16-bit and 32-bit accessible data name.
@@ -585,4 +715,189 @@
  %endif
         call        %1
+%endmacro
+
+
+;; @name Execution Modes
+; @{
+%define BS3_MODE_INVALID    000h
+%define BS3_MODE_RM         001h    ;;< real mode.
+%define BS3_MODE_PE16       011h    ;;< 16-bit protected mode kernel+tss, running 16-bit code, unpaged.
+%define BS3_MODE_PE16_32    012h    ;;< 16-bit protected mode kernel+tss, running 32-bit code, unpaged.
+%define BS3_MODE_PE16_V86   013h    ;;< 16-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged.
+%define BS3_MODE_PE32       022h    ;;< 32-bit protected mode kernel+tss, running 32-bit code, unpaged.
+%define BS3_MODE_PE32_16    021h    ;;< 32-bit protected mode kernel+tss, running 16-bit code, unpaged.
+%define BS3_MODE_PEV86      023h    ;;< 32-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged.
+%define BS3_MODE_PP16       031h    ;;< 16-bit protected mode kernel+tss, running 16-bit code, paged.
+%define BS3_MODE_PP16_32    032h    ;;< 16-bit protected mode kernel+tss, running 32-bit code, paged.
+%define BS3_MODE_PP16_V86   033h    ;;< 16-bit protected mode kernel+tss, running virtual 8086 mode code, paged.
+%define BS3_MODE_PP32       042h    ;;< 32-bit protected mode kernel+tss, running 32-bit code, paged.
+%define BS3_MODE_PP32_16    041h    ;;< 32-bit protected mode kernel+tss, running 16-bit code, paged.
+%define BS3_MODE_PPV86      043h    ;;< 32-bit protected mode kernel+tss, running virtual 8086 mode code, paged.
+%define BS3_MODE_PAE16      051h    ;;< 16-bit protected mode kernel+tss, running 16-bit code, PAE paging.
+%define BS3_MODE_PAE16_32   052h    ;;< 16-bit protected mode kernel+tss, running 32-bit code, PAE paging.
+%define BS3_MODE_PAE16_V86  053h    ;;< 16-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging.
+%define BS3_MODE_PAE32      062h    ;;< 32-bit protected mode kernel+tss, running 32-bit code, PAE paging.
+%define BS3_MODE_PAE32_16   061h    ;;< 32-bit protected mode kernel+tss, running 16-bit code, PAE paging.
+%define BS3_MODE_PAEV86     063h    ;;< 32-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging.
+%define BS3_MODE_LM16       071h    ;;< 16-bit long mode (paged), kernel+tss always 64-bit.
+%define BS3_MODE_LM32       072h    ;;< 32-bit long mode (paged), kernel+tss always 64-bit.
+%define BS3_MODE_LM64       074h    ;;< 64-bit long mode (paged), kernel+tss always 64-bit.
+
+%define BS3_MODE_CODE_MASK  00fh    ;;< Running code mask.
+%define BS3_MODE_CODE_16    001h    ;;< Running 16-bit code.
+%define BS3_MODE_CODE_32    002h    ;;< Running 32-bit code.
+%define BS3_MODE_CODE_V86   003h    ;;< Running 16-bit virtual 8086 code.
+%define BS3_MODE_CODE_64    004h    ;;< Running 64-bit code.
+
+%define BS3_MODE_SYS_MASK   0f0h    ;;< kernel+tss mask.
+%define BS3_MODE_SYS_RM     000h    ;;< Real mode kernel+tss.
+%define BS3_MODE_SYS_PE16   010h    ;;< 16-bit protected mode kernel+tss.
+%define BS3_MODE_SYS_PE32   020h    ;;< 32-bit protected mode kernel+tss.
+%define BS3_MODE_SYS_PP16   030h    ;;< 16-bit paged protected mode kernel+tss.
+%define BS3_MODE_SYS_PP32   040h    ;;< 32-bit paged protected mode kernel+tss.
+%define BS3_MODE_SYS_PAE16  050h    ;;< 16-bit PAE paged protected mode kernel+tss.
+%define BS3_MODE_SYS_PAE32  060h    ;;< 32-bit PAE paged protected mode kernel+tss.
+%define BS3_MODE_SYS_LM     070h    ;;< 64-bit (paged) long mode protected mode kernel+tss.
+
+;; Whether the mode has paging enabled.
+%define BS3_MODE_IS_PAGED(a_fMode)              ((a_fMode) >= BS3_MODE_PP16)
+
+;; Whether the mode is running v8086 code.
+%define BS3_MODE_IS_V86(a_fMode)                (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_V86)
+;; Whether the we're executing in real mode or v8086 mode.
+%define BS3_MODE_IS_RM_OR_V86(a_fMode)          ((a_fMode) == BS3_MODE_RM || BS3_MODE_IS_V86(a_fMode))
+;; Whether the mode is running 16-bit code, except v8086.
+%define BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode)  (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_16)
+;; Whether the mode is running 16-bit code (includes v8086).
+%define BS3_MODE_IS_16BIT_CODE(a_fMode)         (BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode) || BS3_MODE_IS_V86(a_fMode))
+;; Whether the mode is running 32-bit code.
+%define BS3_MODE_IS_32BIT_CODE(a_fMode)         (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_32)
+;; Whether the mode is running 64-bit code.
+%define BS3_MODE_IS_64BIT_CODE(a_fMode)         (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_64)
+
+;; Whether the system is in real mode.
+%define BS3_MODE_IS_RM_SYS(a_fMode)             (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_RM)
+;; Whether the system is some 16-bit mode that isn't real mode.
+%define BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode)    (   ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE16 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP16 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE16)
+;; Whether the system is some 16-bit mode (includes real mode).
+%define BS3_MODE_IS_16BIT_SYS(a_fMode)          (BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode) || BS3_MODE_IS_RM_SYS(a_fMode))
+;; Whether the system is some 32-bit mode.
+%define BS3_MODE_IS_32BIT_SYS(a_fMode)          (   ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE32 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP32 \
+                                                 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE32)
+;; Whether the system is long mode.
+%define BS3_MODE_IS_64BIT_SYS(a_fMode)          (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_LM)
+
+;; @}
+
+;;
+; Includes the file %1 with TMPL_MODE set to all possible value.
+; @param 1      Double quoted include file name.
+%macro BS3_INSTANTIATE_TEMPLATE_WITH_WEIRD_ONES 1
+ %define TMPL_MODE BS3_MODE_RM
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PE16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PE16_32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PE16_V86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PE32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PE32_16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PEV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PP16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PP16_32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PP16_V86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PP32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PP32_16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PPV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PAE16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PAE16_32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PAE16_V86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PAE32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PAE32_16
+ %include %1
+ %define TMPL_MODE BS3_MODE_PAEV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_LM16
+ %include %1
+ %define TMPL_MODE BS3_MODE_LM32
+ %include %1
+ %define TMPL_MODE BS3_MODE_LM64
+ %include %1
+%endmacro
+
+
+;;
+; Includes the file %1 with TMPL_MODE set to all but the "weird" value.
+; @param 1      Double quoted include file name.
+%macro BS3_INSTANTIATE_TEMPLATE_ESSENTIALS 1
+ %define TMPL_MODE BS3_MODE_RM
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PE16
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PE32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PEV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PP16
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PP32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PPV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PAE16
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_PAE32
+ %include %1
+ %define TMPL_MODE BS3_MODE_PAEV86
+ %include %1
+
+ %define TMPL_MODE BS3_MODE_LM16
+ %include %1
+ %define TMPL_MODE BS3_MODE_LM32
+ %include %1
+ %define TMPL_MODE BS3_MODE_LM64
+ %include %1
+%endmacro
+
+;;
+; Includes the file %1 with TMPL_MODE set to a 16-bit, a 32-bit and a 64-bit value.
+; @param 1      Double quoted include file name.
+%macro BS3_INSTANTIATE_COMMON_TEMPLATE 1
+ %define TMPL_MODE BS3_MODE_RM
+ %include %1
+ %define TMPL_MODE BS3_MODE_PE32
+ %include %1
+ %define TMPL_MODE BS3_MODE_LM64
+ %include %1
 %endmacro
 
@@ -850,16 +1165,16 @@
 %define BS3_SEL_TILED_AREA_SIZE     001000000h ;;< 16-bit data tiling: Size of addressable area, in bytes. (16 MB)
 
-%define BS3_SEL_FREE_PART1          0e00h ;;< Free selector space - part \#1.
-%define BS3_SEL_FREE_PART1_LAST     0ff8h ;;< Free selector space - part \#1, last entry.
+%define BS3_SEL_FREE_PART1          0e00h ;;< Free selector space - part \%1.
+%define BS3_SEL_FREE_PART1_LAST     0ff8h ;;< Free selector space - part \%1, last entry.
 
 %define BS3_SEL_TEXT16              1000h ;;< The BS3TEXT16 selector.
 
-%define BS3_SEL_FREE_PART2          1008h ;;< Free selector space - part \#2.
-%define BS3_SEL_FREE_PART2_LAST     1ff8h ;;< Free selector space - part \#2, last entry.
+%define BS3_SEL_FREE_PART2          1008h ;;< Free selector space - part \%2.
+%define BS3_SEL_FREE_PART2_LAST     1ff8h ;;< Free selector space - part \%2, last entry.
 
 %define BS3_SEL_SYSTEM16            2000h ;;< The BS3SYSTEM16 selector.
 
-%define BS3_SEL_FREE_PART3          2008h ;;< Free selector space - part \#3.
-%define BS3_SEL_FREE_PART3_LAST     26f8h ;;< Free selector space - part \#3, last entry.
+%define BS3_SEL_FREE_PART3          2008h ;;< Free selector space - part \%3.
+%define BS3_SEL_FREE_PART3_LAST     26f8h ;;< Free selector space - part \%3, last entry.
 
 %define BS3_SEL_DATA16              2700h ;;< The BS3DATA16 selector.
