Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 58812)
@@ -367,5 +367,20 @@
 	$(PATH_OBJ)/bs3kit-common-32/bs3kit-common-32.lib \
 	$(PATH_OBJ)/bs3kit-common-64-asm/bs3kit-common-64-asm.lib \
-	$(PATH_OBJ)/bs3kit-common-64-c/bs3kit-common-64-c.lib
+	$(PATH_OBJ)/bs3kit-common-64-c/bs3kit-common-64-c.lib \
+       \
+	$(PATH_OBJ)/bs3kit-rm/bs3kit-rm.lib \
+	$(PATH_OBJ)/bs3kit-pe16/bs3kit-pe16.lib \
+	$(PATH_OBJ)/bs3kit-pe32/bs3kit-pe32.lib \
+	$(PATH_OBJ)/bs3kit-pev86/bs3kit-pev86.lib \
+	$(PATH_OBJ)/bs3kit-pp16/bs3kit-pp16.lib \
+	$(PATH_OBJ)/bs3kit-pp32/bs3kit-pp32.lib \
+	$(PATH_OBJ)/bs3kit-ppv86/bs3kit-ppv86.lib \
+	$(PATH_OBJ)/bs3kit-pae16/bs3kit-pae16.lib \
+	$(PATH_OBJ)/bs3kit-pae32/bs3kit-pae32.lib \
+	$(PATH_OBJ)/bs3kit-paev86/bs3kit-paev86.lib \
+	$(PATH_OBJ)/bs3kit-lm16/bs3kit-lm16.lib \
+	$(PATH_OBJ)/bs3kit-lm32/bs3kit-lm32.lib \
+	$(PATH_OBJ)/bs3kit-lm64-asm/bs3kit-lm64-asm.lib \
+	$(PATH_OBJ)/bs3kit-lm64-c/bs3kit-lm64-c.lib
 
 # BS3Kit template for 32-bit code.
@@ -441,5 +456,7 @@
 bs3-bootsector_SOURCES  = bs3-bootsector.asm
 
+#
 # Common sources to be compiled into _p16, _p32 and _p64 versions.
+#
 VBOX_BS3KIT_COMMON_SOURCES = \
 	bs3-cmn-Shutdown.asm \
@@ -473,4 +490,11 @@
        bs3-cmn-SlabListAllocEx.c \
        bs3-cmn-SlabListFree.c \
+       bs3-cmn-SwitchToRing0.asm \
+       bs3-cmn-SwitchToRing1.asm \
+       bs3-cmn-SwitchToRing2.asm \
+       bs3-cmn-SwitchToRing3.asm \
+       bs3-cmn-SwitchTo16Bit.asm \
+       bs3-cmn-SwitchTo32Bit.asm \
+       bs3-cmn-SwitchTo64Bit.asm \
        bs3-cmn-TestData.c \
        bs3-cmn-TestInit.c \
@@ -482,5 +506,5 @@
        ../../../Runtime/common/asm/ASMBitNextSet.asm
 
-# The BS3Kit library.
+# The 16-bit BS3Kit library.
 LIBRARIES += bs3kit-common-16
 bs3kit-common-16_TEMPLATE = VBoxBS3KitImg
@@ -526,4 +550,107 @@
 bs3kit-common-64-c_SOURCES  = $(filter-out %.asm,$(VBOX_BS3KIT_COMMON_SOURCES))
 
+
+#
+# Common sources to be compiled for each CPU mode.
+#
+VBOX_BS3KIT_MODE_SOURCES = \
+	bs3-mode-SwitchToRM.asm \
+	bs3-mode-SwitchToPE16.asm \
+	bs3-mode-SwitchToPE32.asm \
+	bs3-mode-SwitchToPP16.asm \
+	bs3-mode-SwitchToPP32.asm \
+	bs3-mode-EnteredMode.asm \
+
+# The 16-bit real mode BS3Kit library.
+LIBRARIES += bs3kit-rm
+bs3kit-rm_TEMPLATE = VBoxBS3KitImg
+bs3kit-rm_DEFS     = TMPL_RM
+bs3kit-rm_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+	bs3-first-rm.asm
+
+
+# The 16-bit protected mode BS3Kit library.
+LIBRARIES += bs3kit-pe16
+bs3kit-pe16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pe16_DEFS     = TMPL_PE16
+bs3kit-pe16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+	bs3-first-pe16.asm
+
+# The virtual 8086 under protected mode BS3Kit library.
+LIBRARIES += bs3kit-pev86
+bs3kit-pev86_TEMPLATE = VBoxBS3KitImg
+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 = VBoxBS3KitImg
+bs3kit-pe32_DEFS     = TMPL_PE32
+bs3kit-pe32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+
+# The 16-bit paged protected mode BS3Kit library.
+LIBRARIES += bs3kit-pp16
+bs3kit-pp16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pp16_DEFS     = TMPL_PP16
+bs3kit-pp16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+
+# The 32-bit paged protected mode BS3Kit library.
+LIBRARIES += bs3kit-pp32
+bs3kit-pp32_TEMPLATE = VBoxBS3KitImg
+bs3kit-pp32_DEFS     = TMPL_PP32
+bs3kit-pp32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The virtual 8086 under protected mode BS3Kit library.
+LIBRARIES += bs3kit-ppv86
+bs3kit-ppv86_TEMPLATE = VBoxBS3KitImg
+bs3kit-ppv86_DEFS     = TMPL_PPV86
+bs3kit-ppv86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+
+
+# The 16-bit paged protected mode BS3Kit library.
+LIBRARIES += bs3kit-pae16
+bs3kit-pae16_TEMPLATE = VBoxBS3KitImg
+bs3kit-pae16_DEFS     = TMPL_PAE16
+bs3kit-pae16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The 32-bit paged protected mode BS3Kit library.
+LIBRARIES += bs3kit-pae32
+bs3kit-pae32_TEMPLATE = VBoxBS3KitImg
+bs3kit-pae32_DEFS     = TMPL_PAE32
+bs3kit-pae32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES)
+
+# The virtual 8086 under protected mode BS3Kit library.
+LIBRARIES += bs3kit-paev86
+bs3kit-paev86_TEMPLATE = VBoxBS3KitImg
+bs3kit-paev86_DEFS     = TMPL_PAEV86
+bs3kit-paev86_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+
+
+# The 16-bit long mode BS3Kit library.
+LIBRARIES += bs3kit-lm16
+bs3kit-lm16_TEMPLATE = VBoxBS3KitImg
+bs3kit-lm16_DEFS     = TMPL_LM16
+bs3kit-lm16_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+
+# The 32-bit long mode BS3Kit library.
+LIBRARIES += bs3kit-lm32
+bs3kit-lm32_TEMPLATE = VBoxBS3KitImg
+bs3kit-lm32_DEFS     = TMPL_LM32
+bs3kit-lm32_SOURCES  = $(VBOX_BS3KIT_MODE_SOURCES) \
+
+# The 64-bit long mode BS3Kit library.
+LIBRARIES += bs3kit-lm64-asm
+bs3kit-lm64-asm_TEMPLATE = VBoxBS3KitImg
+bs3kit-lm64-asm_DEFS     = TMPL_LM64
+bs3kit-lm64-asm_SOURCES  = $(filter %.asm,$(VBOX_BS3KIT_MODE_SOURCES)) \
+
+# The 64-bit long mode BS3Kit library.
+LIBRARIES += bs3kit-lm64-c
+bs3kit-lm64-c_TEMPLATE = VBoxBS3KitImg
+bs3kit-lm64-c_DEFS     = TMPL_LM64
+bs3kit-lm64-c_SOURCES  = $(filter-out %.asm, $(VBOX_BS3KIT_MODE_SOURCES)) \
+
+
 #
 # shutdown example.
@@ -531,7 +658,10 @@
 MISCBINS += bs3-shutdown
 bs3-shutdown_TEMPLATE = VBoxBS3KitImg
-bs3-shutdown_SOURCES = bs3-first-rm.asm bs3-shutdown.c
+bs3-shutdown_SOURCES = \
+	bs3-first-pe16.asm \
+	bs3-shutdown.c
 
 
 include $(FILE_KBUILD_SUB_FOOTER)
 
+.PRECIOUS: E:/vbox/svn/trunk/out/win.amd64/debug/obj/bs3-shutdown/bs3-shutdown.map
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Printf.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Printf.c	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Printf.c	(revision 58812)
@@ -29,5 +29,5 @@
 
 
-BS3_DECL(size_t) bs3PrintFmtOutput(char ch, void BS3_FAR *pvUser)
+static BS3_DECL_CALLBACK(size_t) bs3PrintFmtOutput(char ch, void BS3_FAR *pvUser)
 {
     if (ch != '\0')
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrPrintf.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrPrintf.c	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrPrintf.c	(revision 58812)
@@ -47,5 +47,5 @@
 
 
-BS3_DECL(size_t) bs3StrPrintfFmtOutput(char ch, void BS3_FAR *pvUser)
+static BS3_DECL_CALLBACK(size_t) bs3StrPrintfFmtOutput(char ch, void BS3_FAR *pvUser)
 {
     PBS3STRPRINTFSTATE pState = (PBS3STRPRINTFSTATE)pvUser;
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 58812)
@@ -0,0 +1,62 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchTo16Bit
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchTo16Bit(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchTo16Bit
+%if TMPL_BITS == 16
+        ret
+%else
+        push    xAX                     ; reserve space for far return
+        push    xAX
+
+        ; Far return, offset part. bits 63/31:16 must be zero.
+        movzx   eax, word [xSP + xCB * 3] ; returning to 16-bit, so bit 16 and out must be zero.
+        mov     [xSP + xCB * 2], xAX
+
+        mov     ax, cs
+        and     ax, 3
+        shl     ax, BS3_SEL_RING_SHIFT  ; ring addend.
+        add     ax, BS3_SEL_R0_CS16
+        mov     [esp + xCB * 3], xAX
+
+        ; Load 16-bit segment registers.
+        add     ax, BS3_SEL_R0_SS16 - BS3_SEL_R0_CS16
+        mov     ss, ax
+
+        add     ax, BS3_SEL_R0_DS16 - BS3_SEL_R0_SS16
+        mov     ds, ax
+        mov     es, ax
+
+        ; Restore and return.
+        pop     xAX
+        retf
+%endif
+BS3_PROC_END_CMN   Bs3SwitchTo16Bit
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo32Bit.asm	(revision 58812)
@@ -0,0 +1,83 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchTo32Bit
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchTo32Bit(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchTo32Bit
+%if TMPL_BITS == 32
+        ret
+%else
+ %if TMPL_BITS == 16
+        sub     sp, 6
+        push    bp
+        mov     bp, sp
+ %else
+        push    xPRE [xSP]                ; duplicate the return address
+ %endif
+        push    sAX
+
+ %if TMPL_BITS == 16
+        ; Convert the 16-bit near return into a 32-bit far return
+        movzx   eax, word [bp + 8]
+        add     eax, BS3_ADDR_BS3TEXT16
+        mov     [bp + 2], eax
+ %endif
+
+        ; Calc ring addend.
+        mov     ax, cs
+        and     ax, 3
+        shl     ax, BS3_SEL_RING_SHIFT
+
+        ; Set return segment.
+        add     ax, BS3_SEL_R0_CS32
+ %if TMPL_BITS == 16
+        mov     [bp + 6], eax
+ %else
+        mov     [xSP + xCB*2], eax
+ %endif
+
+        ; Load 32-bit segment registers.
+        add     ax, BS3_SEL_R0_SS32 - BS3_SEL_R0_CS32
+        mov     ss, ax
+
+        add     ax, BS3_SEL_R0_DS32 - BS3_SEL_R0_SS32
+        mov     ds, ax
+        mov     es, ax
+
+        ; Restore and return.
+        pop     sAX
+ %if TMPL_BITS == 16
+        leave
+        o32 retf
+ %else
+        retf
+ %endif
+%endif
+BS3_PROC_END_CMN   Bs3SwitchTo32Bit
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo64Bit.asm	(revision 58812)
@@ -0,0 +1,85 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchTo64Bit
+;
+
+;
+; 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"
+
+%ifndef TMPL_CMN_LM
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchTo64Bit(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchTo64Bit
+ %if TMPL_BITS == 64
+        ret
+ %else
+  %if TMPL_BITS == 16
+        sub     sp, 6
+        push    bp
+        mov     bp, sp
+  %else
+        push    xPRE [xSP]                ; duplicate the return address
+  %endif
+        push    sAX
+
+  %if TMPL_BITS == 16
+        ; Convert the 16-bit near return into a 32-bit far return
+        movzx   eax, word [bp + 8]
+        add     eax, BS3_ADDR_BS3TEXT16
+        mov     [bp + 2], eax
+  %endif
+
+        ; Calc ring addend.
+        mov     ax, cs
+        and     ax, 3
+        shl     ax, BS3_SEL_RING_SHIFT
+
+        ; Set return segment.
+        add     ax, BS3_SEL_R0_CS64
+  %if TMPL_BITS == 16
+        mov     [bp + 6], eax
+  %else
+        mov     [xSP + xCB*2], eax
+  %endif
+
+        ; Load 64-bit segment registers (SS64==DS64).
+        add     ax, BS3_SEL_R0_DS64 - BS3_SEL_R0_CS64
+        mov     ss, ax
+        mov     ds, ax
+        mov     es, ax
+
+        ; Restore and return.
+        pop     sAX
+ %if TMPL_BITS == 16
+        leave
+        o32 retf
+ %else
+        retf
+ %endif
+%endif
+BS3_PROC_END_CMN   Bs3SwitchTo64Bit
+
+%endif
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm	(revision 58812)
@@ -0,0 +1,51 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToRing0
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchToRing0(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchToRing0
+        push    sAX
+
+%if TMPL_BITS == 16
+        smsw    ax
+        test    ax, X86_CR0_PE
+        jz      .return                 ; real mode.
+%endif
+        mov     ax, cs
+        test    ax, 3
+        jz      .return
+
+        mov     eax, BS3_SYSCALL_TO_RING0
+        int     BS3_TRAP_SYSCALL
+
+.return:
+        pop     sAX
+        ret
+BS3_PROC_END_CMN   Bs3SwitchToRing0
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm	(revision 58812)
@@ -0,0 +1,47 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToRing1
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchToRing1(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchToRing1
+        push    sAX
+
+        mov     ax, cs
+        and     ax, 3
+        cmp     ax, 1
+        je      .return
+
+        mov     eax, BS3_SYSCALL_TO_RING1
+        int     BS3_TRAP_SYSCALL
+
+.return
+        pop     sAX
+        ret
+BS3_PROC_END_CMN   Bs3SwitchToRing1
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm	(revision 58812)
@@ -0,0 +1,47 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToRing2
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchToRing2(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchToRing2
+        push    sAX
+
+        mov     ax, cs
+        and     ax, 3
+        cmp     ax, 2
+        je      .return
+
+        mov     eax, BS3_SYSCALL_TO_RING2
+        int     BS3_TRAP_SYSCALL
+
+.return:
+        pop     sAX
+        ret
+BS3_PROC_END_CMN   Bs3SwitchToRing2
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm	(revision 58812)
@@ -0,0 +1,47 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToRing3
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_DECL(void) Bs3SwitchToRing3(void);
+;
+BS3_PROC_BEGIN_CMN Bs3SwitchToRing3
+        push    sAX
+
+        mov     ax, cs
+        and     ax, 3
+        cmp     ax, 3
+        je      .return
+
+        mov     eax, BS3_SYSCALL_TO_RING3
+        int     BS3_TRAP_SYSCALL
+
+.return:
+        pop     sAX
+        ret
+BS3_PROC_END_CMN   Bs3SwitchToRing3
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-common.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-common.mac	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-common.mac	(revision 58812)
@@ -0,0 +1,168 @@
+; $Id$
+;; @file
+; BS3Kit - First Object, common stuff.
+;
+
+;
+; 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.mac"
+
+
+;
+;
+; Define all the segments and their grouping, just to get that right once at
+; the start of everything.
+;
+;
+
+;
+; 16-bit text
+;
+BS3_BEGIN_TEXT16
+BS3_GLOBAL_DATA Bs3Text16_StartOfSegment, 0
+
+; Entry point with eye-catcher.
+GLOBALNAME start
+    jmp     .after_eye_catcher
+    db      10,13,'eye-catcher: BS3TEXT16',10,13
+.after_eye_catcher:
+
+%ifdef ASM_FORMAT_ELF
+section BS3TEXT16_END   align=2 progbits alloc exec nowrite
+%else
+section BS3TEXT16_END   align=2 CLASS=BS3CODE16 PUBLIC USE16
+%endif
+
+BS3_GLOBAL_DATA Bs3Text16_EndOfSegment, 0
+
+%ifndef ASM_FORMAT_ELF
+GROUP CGROUP16 BS3TEXT16 BS3TEXT16_END
+%endif
+
+
+;
+; 16-bit data
+;
+BS3_BEGIN_DATA16
+BS3_GLOBAL_DATA Bs3Data16_StartOfSegment, 0
+    db      10,13,'eye-catcher: BS3DATA16',10,13
+
+ALIGNDATA(16)
+BS3_GLOBAL_DATA Bs3Data16_Size, 4
+    dd  BS3_DATA_NM(Bs3Data16_EndOfSegment) wrt BS3DATA16_GROUP
+BS3_GLOBAL_DATA Bs3Data16Thru64Text32And64_TotalSize, 4
+    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt BS3DATA16_GROUP
+BS3_GLOBAL_DATA Bs3TotalImageSize, 4
+    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt CGROUP16
+BS3_GLOBAL_DATA Bs3Text16_Size, 2
+    dd  BS3_DATA_NM(Bs3Text16_EndOfSegment) wrt CGROUP16
+
+%ifdef ASM_FORMAT_ELF
+section BS3DATA16CONST  align=2   progbits alloc noexec write
+section BS3DATA16CONST2 align=2   progbits alloc noexec write
+section BS3DATA16_DATA  align=2   progbits alloc noexec write
+section BS3DATA16_END   align=2   progbits alloc noexec write
+%else
+section BS3DATA16CONST  align=2   CLASS=FAR_DATA PUBLIC USE16
+section BS3DATA16CONST2 align=2   CLASS=FAR_DATA PUBLIC USE16
+section BS3DATA16_DATA  align=2   CLASS=FAR_DATA PUBLIC USE16
+section BS3DATA16_END   align=2   CLASS=FAR_DATA PUBLIC USE16
+%endif
+
+BS3_GLOBAL_DATA Bs3Data16_EndOfSegment, 0
+
+%ifndef ASM_FORMAT_ELF
+GROUP BS3DATA16_GROUP BS3DATA16 BS3DATA16CONST BS3DATA16CONST2 BS3DATA16_END
+%endif
+
+
+;
+; 32-bit text
+;
+BS3_BEGIN_TEXT32
+BS3_GLOBAL_DATA Bs3Text32_StartOfSegment, 0
+    db      10,13,'eye-catcher: BS3TEXT32',10,13
+
+%ifdef ASM_FORMAT_ELF
+section BS3TEXT32_END   align=1 progbits alloc exec nowrite
+%else
+section BS3TEXT32_END   align=1 CLASS=BS3CODE32 PUBLIC USE32 FLAT
+%endif
+
+BS3_GLOBAL_DATA Bs3Text32_EndOfSegment, 0
+
+
+;
+; 64-bit text
+;
+BS3_BEGIN_TEXT64
+BS3_GLOBAL_DATA Bs3Text64_StartOfSegment, 0
+    db      10,13,'eye-catcher: BS3TEXT64',10,13
+
+%ifdef ASM_FORMAT_ELF
+section BS3TEXT64_END   align=1 progbits alloc exec nowrite
+%else
+section BS3TEXT64_END   align=1 CLASS=CODE PUBLIC USE32 FLAT
+%endif
+BS3_GLOBAL_DATA Bs3Text64_EndOfSegment, 0
+
+
+;
+; 32-bit data
+;
+BS3_BEGIN_DATA32
+BS3_GLOBAL_DATA Bs3Data32_StartOfSegment, 0
+    db      10,13,'eye-catcher: BS3DATA32',10,13
+%ifdef ASM_FORMAT_ELF
+section BS3DATA32_END   align=16   progbits alloc noexec write
+%else
+section BS3DATA32_END   align=16   CLASS=FAR_DATA PUBLIC USE32 FLAT
+%endif
+BS3_GLOBAL_DATA Bs3Data32_EndOfSegment, 0
+
+;
+; 64-bit data
+;
+BS3_BEGIN_DATA64
+BS3_GLOBAL_DATA Bs3Data64_StartOfSegment, 0
+    db      10,13,'eye-catcher: BS3DATA64',10,13
+%ifdef ASM_FORMAT_ELF
+section BS3DATA64_END   align=16   progbits alloc noexec write
+%else
+section BS3DATA64_END   align=16   CLASS=DATA PUBLIC USE32 FLAT
+%endif
+BS3_GLOBAL_DATA Bs3Data64_EndOfSegment, 0
+
+
+;
+; 16-bit accessible system data.
+; No need to do anything here.
+;
+BS3_BEGIN_SYSTEM16
+
+
+;
+; Switch back to the 16-bit code segment.
+;
+BS3_BEGIN_TEXT16
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-pe16.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-pe16.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-pe16.asm	(revision 58812)
@@ -0,0 +1,79 @@
+; $Id$
+;; @file
+; BS3Kit - First Object, calling real-mode main().
+;
+
+;
+; Copyright (C) 2007-2015 Oracle Corporation
+;
+; This file is part of VirtualBox Open Source Edition (OSE), as
+; available from http://www.virtualbox.org. This file is free software;
+; you can redistribute it and/or modify it under the terms of the GNU
+; General Public License (GPL) as published by the Free Software
+; Foundation, in version 2 as it comes in the "COPYING" file of the
+; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+;
+; The contents of this file may alternatively be used under the terms
+; of the Common Development and Distribution License Version 1.0
+; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+; VirtualBox OSE distribution, in which case the provisions of the
+; CDDL are applicable instead of those of the GPL.
+;
+; You may elect to license modified versions of this file under the
+; terms and conditions of either the GPL or the CDDL or both.
+;
+
+
+;*********************************************************************************************************************************
+;*  Header Files                                                                                                                 *
+;*********************************************************************************************************************************
+%include "bs3kit.mac"
+
+;
+; Segment defs, grouping and related variables.
+; Defines the entry point 'start' as well, leaving us in BS3TEXT16.
+;
+%include "bs3-first-common.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
+BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt16
+
+BS3_BEGIN_TEXT16
+EXTERN Main_pe16
+EXTERN Bs3SwitchToPE16_rm
+EXTERN Bs3SwitchToRM_pe16
+EXTERN Bs3SwitchToPE32_rm
+EXTERN Bs3SwitchToRM_pe32
+EXTERN Bs3InitMemory_rm
+BS3_EXTERN_CMN Bs3Shutdown
+
+extern _Bs3Printf_c32
+
+BS3_BEGIN_TEXT16
+    ;
+    ; We need to enter 16-bit protected mode before we can call Main_pe16.
+    ;
+    call    NAME(Bs3InitMemory_rm)      ; Initialize the memory (must be done from real mode).
+    call    NAME(Bs3SwitchToPE16_rm)
+    call    NAME(Bs3SwitchToRM_pe16)
+
+    call    NAME(Bs3SwitchToPE32_rm)
+    BS3_SET_BITS 32
+    call    NAME(Bs3SwitchToRM_pe32)
+    BS3_SET_BITS 16
+
+.halt: hlt
+jmp .halt
+
+
+    ;
+    ; Call main, if it returns shutdown the system.
+    ;
+    call    NAME(Main_pe16)
+    call    Bs3Shutdown
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-first-rm.asm	(revision 58812)
@@ -29,133 +29,21 @@
 %include "bs3kit.mac"
 
-
 ;
-; Define all the segments and their grouping, just to get that right once at
-; the start of everything.
+; Segment defs, grouping and related variables.
+; Defines the entry point 'start' as well, leaving us in BS3TEXT16.
 ;
-
-; 16-bit text
-BS3_BEGIN_TEXT16
-BS3_GLOBAL_DATA Bs3Text16_StartOfSegment, 0
-
-%ifdef ASM_FORMAT_ELF
-section BS3TEXT16_END   align=2 progbits alloc exec nowrite
-%else
-section BS3TEXT16_END   align=2 CLASS=BS3CODE16 PUBLIC USE16
-%endif
-
-BS3_GLOBAL_DATA Bs3Text16_EndOfSegment, 0
-
-%ifndef ASM_FORMAT_ELF
-GROUP CGROUP16 BS3TEXT16 BS3TEXT16_END
-%endif
-
-; 16-bit data
-BS3_BEGIN_DATA16
-BS3_GLOBAL_DATA Bs3Data16_StartOfSegment, 0
-    db      10,13,'eye-catcher: BS3DATA16',10,13
-
-ALIGNDATA(16)
-BS3_GLOBAL_DATA Bs3Data16_Size, 4
-    dd  BS3_DATA_NM(Bs3Data16_EndOfSegment) wrt BS3DATA16_GROUP
-BS3_GLOBAL_DATA Bs3Data16Thru64Text32And64_TotalSize, 4
-    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt BS3DATA16_GROUP
-BS3_GLOBAL_DATA Bs3TotalImageSize, 4
-    dd  BS3_DATA_NM(Bs3Data64_EndOfSegment) wrt CGROUP16
-BS3_GLOBAL_DATA Bs3Text16_Size, 2
-    dd  BS3_DATA_NM(Bs3Text16_EndOfSegment) wrt CGROUP16
-
-%ifdef ASM_FORMAT_ELF
-section BS3DATA16CONST  align=2   progbits alloc noexec write
-section BS3DATA16CONST2 align=2   progbits alloc noexec write
-section BS3DATA16_DATA  align=2   progbits alloc noexec write
-section BS3DATA16_END   align=2   progbits alloc noexec write
-%else
-section BS3DATA16CONST  align=2   CLASS=FAR_DATA PUBLIC USE16
-section BS3DATA16CONST2 align=2   CLASS=FAR_DATA PUBLIC USE16
-section BS3DATA16_DATA  align=2   CLASS=FAR_DATA PUBLIC USE16
-section BS3DATA16_END   align=2   CLASS=FAR_DATA PUBLIC USE16
-%endif
-
-BS3_GLOBAL_DATA Bs3Data16_EndOfSegment, 0
-
-%ifndef ASM_FORMAT_ELF
-GROUP BS3DATA16_GROUP BS3DATA16 BS3DATA16CONST BS3DATA16CONST2 BS3DATA16_END
-%endif
-
-; 32-bit text
-BS3_BEGIN_TEXT32
-BS3_GLOBAL_DATA Bs3Text32_StartOfSegment, 0
-    db      10,13,'eye-catcher: BS3TEXT32',10,13
-
-%ifdef ASM_FORMAT_ELF
-section BS3TEXT32_END   align=1 progbits alloc exec nowrite
-%else
-section BS3TEXT32_END   align=1 CLASS=BS3CODE32 PUBLIC USE32 FLAT
-%endif
-
-BS3_GLOBAL_DATA Bs3Text32_EndOfSegment, 0
-
-; 64-bit text
-BS3_BEGIN_TEXT64
-BS3_GLOBAL_DATA Bs3Text64_StartOfSegment, 0
-    db      10,13,'eye-catcher: BS3TEXT64',10,13
-
-%ifdef ASM_FORMAT_ELF
-section BS3TEXT64_END   align=1 progbits alloc exec nowrite
-%else
-section BS3TEXT64_END   align=1 CLASS=CODE PUBLIC USE32 FLAT
-%endif
-BS3_GLOBAL_DATA Bs3Text64_EndOfSegment, 0
-
-; 32-bit data
-BS3_BEGIN_DATA32
-BS3_GLOBAL_DATA Bs3Data32_StartOfSegment, 0
-    db      10,13,'eye-catcher: BS3DATA32',10,13
-%ifdef ASM_FORMAT_ELF
-section BS3DATA32_END   align=16   progbits alloc noexec write
-%else
-section BS3DATA32_END   align=16   CLASS=FAR_DATA PUBLIC USE32 FLAT
-%endif
-BS3_GLOBAL_DATA Bs3Data32_EndOfSegment, 0
-
-; 64-bit data
-BS3_BEGIN_DATA64
-BS3_GLOBAL_DATA Bs3Data64_StartOfSegment, 0
-    db      10,13,'eye-catcher: BS3DATA64',10,13
-%ifdef ASM_FORMAT_ELF
-section BS3DATA64_END   align=16   progbits alloc noexec write
-%else
-section BS3DATA64_END   align=16   CLASS=DATA PUBLIC USE32 FLAT
-%endif
-BS3_GLOBAL_DATA Bs3Data64_EndOfSegment, 0
-
-BS3_BEGIN_SYSTEM16
+%include "bs3-first-common.mac"
 
 
+EXTERN Main_rm
+BS3_EXTERN_CMN Bs3Shutdown
 
-;*********************************************************************************************************************************
-;*  External Symbols                                                                                                             *
-;*********************************************************************************************************************************
-BS3_BEGIN_TEXT16
-extern BS3_CMN_NM(Bs3Shutdown)
-extern NAME(Main_rm)
-
-BS3_BEGIN_SYSTEM16
-extern BS3_DATA_NM(Bs3Gdt)
-extern BS3_DATA_NM(Bs3Lgdt_Gdt)
-
-;
-; Nothing to init here, just call main and shutdown if it returns.
-;
-BS3_BEGIN_TEXT16
-GLOBALNAME start
-    jmp     .after_eye_catcher
-    db      10,13,'eye-catcher: BS3TEXT16',10,13
-.after_eye_catcher:
+    ;
+    ; 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    BS3_CMN_NM(Bs3Shutdown)
+    call    Bs3Shutdown
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-EnteredMode.asm	(revision 58812)
@@ -0,0 +1,136 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3EnteredMode
+;
+
+;
+; 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"
+
+
+;;
+; @cproto   BS3_DECL(void) Bs3EnteredMode(void);
+;
+; @uses     Nothing.
+;
+; @remarks  ASSUMES we're in ring-0 when not in some kind of real mode.
+;
+BS3_PROC_BEGIN_MODE Bs3EnteredMode
+        push    xAX
+
+        ;
+        ; Load selectors, IDTs and stuff as appropriate.
+        ;
+%ifdef TMPL_CMN_R86
+BS3_BEGIN_DATA16
+TMPL_BEGIN_TEXT
+        xor     ax, ax
+        mov     ss, ax
+        mov     ax, BS3DATA16
+        mov     ds, ax
+        mov     es, ax
+
+%elif TMPL_BITS == 16
+BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt16
+BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16
+BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16DoubleFault
+TMPL_BEGIN_TEXT
+        jmp     BS3_SEL_R0_CS16:.reloaded_cs
+.reloaded_cs:
+
+        mov     ax, BS3_SEL_R0_SS16
+        mov     ss, ax
+
+        mov     ax, BS3_SEL_SYSTEM16
+        mov     ds, ax
+        lidt    [Bs3Lidt_Idt16]
+
+        mov     ax, X86DESCGENERIC_BIT_OFF_TYPE + 1
+        btr     [Bs3Gdte_Tss16DoubleFault], ax  ; mark it not busy
+        btr     [Bs3Gdte_Tss16], ax             ; mark it not busy
+        mov     ax, BS3_SEL_TSS16
+        ltr     ax
+
+        mov     ax, BS3_SEL_LDT
+        lldt    ax
+
+        mov     ax, BS3_SEL_R0_DS16
+        mov     ds, ax
+        mov     es, ax
+
+%elif TMPL_BITS == 32
+BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt32
+BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32
+BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32DoubleFault
+TMPL_BEGIN_TEXT
+        mov     ax, BS3_SEL_R0_SS32
+        mov     ss, ax
+
+        mov     ax, BS3_SEL_SYSTEM16
+        mov     ds, ax
+        lidt    [Bs3Lidt_Idt32]
+
+        mov     ax, X86DESCGENERIC_BIT_OFF_TYPE + 1
+        btr     [Bs3Gdte_Tss32DoubleFault], ax  ; mark it not busy
+        btr     [Bs3Gdte_Tss32], ax             ; mark it not busy
+        mov     ax, BS3_SEL_TSS32
+        ltr     ax
+
+        mov     ax, BS3_SEL_LDT
+        lldt    ax
+
+        mov     ax, BS3_SEL_R0_DS32
+        mov     ds, ax
+        mov     es, ax
+
+
+%elif TMPL_BITS == 64
+BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt64
+BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss64
+TMPL_BEGIN_TEXT
+        mov     ax, BS3_SEL_R0_DS64
+        mov     ss, ax
+
+        mov     ax, BS3_SEL_SYSTEM16
+        mov     ds, ax
+        lidt    [Bs3Lidt_Idt64]
+
+        mov     eax, X86DESCGENERIC_BIT_OFF_TYPE + 1
+        btr     [Bs3Gdte_Tss64], eax ; mark it not busy
+        mov     ax, BS3_SEL_TSS64
+        ltr     ax
+
+        mov     ax, BS3_SEL_LDT
+        lldt    ax
+
+        mov     ax, BS3_SEL_R0_DS64
+        mov     ds, ax
+        mov     es, ax
+
+%else
+ %error "TMPL_BITS"
+%endif
+
+        pop     xAX
+        ret
+BS3_PROC_END_MODE   Bs3EnteredMode
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE16.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE16.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE16.asm	(revision 58812)
@@ -0,0 +1,98 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToPE16
+;
+
+;
+; 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"
+
+
+;;
+; Switch to 16-bit unpaged protected mode from any other mode.
+;
+; @cproto   BS3_DECL(void) Bs3SwitchToPE16(void);
+;
+; @uses     Nothing (except high 32-bit register parts).
+;
+; @remarks  Obviously returns to 16-bit mode, even if the caller was
+;           in 32-bit or 64-bit mode.
+;
+BS3_PROC_BEGIN_MODE Bs3SwitchToPE16
+%ifdef TMPL_PE16
+        ret
+
+%else
+        ;
+        ; Switch to 16-bit mode and prepare for returning in 16-bit mode.
+        ;
+ %if TMPL_BITS != 16
+        shl     xPRE [xSP + xCB], TMPL_BITS - 16    ; Adjust the return address.
+        add     xSP, xCB - 2
+
+        ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
+        jmp     .sixteen_bit_segment
+BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.sixteen_bit_segment:
+ %endif
+
+        ;
+        ; Switch to real mode.
+        ;
+        extern  TMPL_NM(Bs3SwitchToRM)
+        call    TMPL_NM(Bs3SwitchToRM)
+        BS3_SET_BITS 16
+
+        push    ax
+        pushfd
+        cli
+
+        ;
+        ; Load the GDT and enable PE16.
+        ;
+BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
+BS3_BEGIN_TEXT16
+        mov     ax, BS3SYSTEM16
+        mov     ds, ax
+        lgdt    [Bs3Lgdt_Gdt]
+
+        smsw    ax
+        or      ax, X86_CR0_PE
+        lmsw    ax
+
+        ;
+        ; Call rountine for doing mode specific setups.
+        ;
+        extern  NAME(Bs3EnteredMode_pe16)
+        call    NAME(Bs3EnteredMode_pe16)
+
+        popfd
+        pop     ax
+        ret
+
+ %if TMPL_BITS != 16
+TMPL_BEGIN_TEXT
+ %endif
+%endif
+BS3_PROC_END_MODE   Bs3SwitchToPE16
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE32.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE32.asm	(revision 58812)
@@ -0,0 +1,115 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToPE32
+;
+
+;
+; 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"
+
+
+;;
+; Switch to 32-bit unpaged protected mode from any other mode.
+;
+; @cproto   BS3_DECL(void) Bs3SwitchToPE32(void);
+;
+; @uses     Nothing (except high 32-bit register parts), upper part of ESP is
+;           cleared if caller is in 16-bit mode.
+;
+; @remarks  Obviously returns to 32-bit mode, even if the caller was
+;           in 16-bit or 64-bit mode.
+;
+BS3_PROC_BEGIN_MODE Bs3SwitchToPE32
+%ifdef TMPL_PE32
+        ret
+
+%else
+        ;
+        ; Switch to real mode.
+        ;
+ %if TMPL_BITS != 32
+  %if TMPL_BITS > 32
+        shl     xPRE [xSP + xCB], 32    ; Adjust the return address from 64-bit to 32-bit.
+        add     rsp, xCB - 4
+  %else
+        push    word 0                  ; Reserve space to expand the return address.
+  %endif
+        ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
+        jmp     .sixteen_bit_segment
+BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.sixteen_bit_segment:
+ %endif
+
+        ;
+        ; Switch to real mode.
+        ;
+        extern  TMPL_NM(Bs3SwitchToRM)
+        call    TMPL_NM(Bs3SwitchToRM)
+        BS3_SET_BITS 16
+
+        push    eax
+        pushfd
+        cli
+
+        ;
+        ; Load the GDT and enable PE32.
+        ;
+BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
+BS3_BEGIN_TEXT16
+        mov     ax, BS3SYSTEM16
+        mov     ds, ax
+        lgdt    [Bs3Lgdt_Gdt]
+
+        mov     eax, cr0
+        or      eax, X86_CR0_PE
+        mov     cr0, eax
+        jmp     BS3_SEL_R0_CS32:dword .thirty_two_bit wrt FLAT
+extern TODO_CONVINCE_WATCOM_TO_DO_FLAT_RELOCS
+BS3_BEGIN_TEXT32
+.thirty_two_bit:
+
+        ;
+        ; Call rountine for doing mode specific setups.
+        ;
+        extern  NAME(Bs3EnteredMode_pe32)
+        call    NAME(Bs3EnteredMode_pe32)
+
+        ;
+        ; Restore eax and flags (IF).
+        ;
+ %if TMPL_BITS < 32
+        and     esp, 0ffffh             ; Make sure the high word is zero.
+        movzx   eax, word [esp + 8 + 2] ; Load return address.
+        add     eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
+        mov     [esp + 8], eax          ; Store it in the place right for 32-bit returns.
+ %endif
+        pop     esp
+        popfd
+        ret
+
+ %if TMPL_BITS != 32
+TMPL_BEGIN_TEXT
+ %endif
+%endif
+BS3_PROC_END_MODE   Bs3SwitchToPE32
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP16.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP16.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP16.asm	(revision 58812)
@@ -0,0 +1,118 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToPP16
+;
+
+;
+; 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"
+
+
+;;
+; Switch to 16-bit paged protected mode from any other mode.
+;
+; @cproto   BS3_DECL(void) Bs3SwitchToPP16(void);
+;
+; @uses     Nothing (except high 32-bit register parts).
+;
+; @remarks  Obviously returns to 16-bit mode, even if the caller was
+;           in 32-bit or 64-bit mode.
+;
+BS3_PROC_BEGIN_MODE Bs3SwitchToPP16
+%ifdef TMPL_PE16
+        ret
+
+%else
+        ;
+        ; Switch to 16-bit mode and prepare for returning in 16-bit mode.
+        ;
+ %if TMPL_BITS != 16
+        shl     xPRE [xSP + xCB], TMPL_BITS - 16    ; Adjust the return address.
+        add     xSP, xCB - 2
+
+        ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
+        jmp     .sixteen_bit_segment
+BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.sixteen_bit_segment:
+ %endif
+
+        ;
+        ; Switch to real mode.
+        ;
+        extern  TMPL_NM(Bs3SwitchToRM)
+        call    TMPL_NM(Bs3SwitchToRM)
+        BS3_SET_BITS 16
+
+        push    eax
+        pushfd
+
+        ;
+        ; Make sure PAE is really off.
+        ;
+        mov     eax, cr4
+        test    eax, X86_CR4_PAE
+        jz      .cr4_is_fine
+        and     eax, ~X86_CR4_PAE
+        mov     cr4, eax
+.cr4_is_fine:
+
+        ;
+        ; Get the page directory (returned in eax).  Will lazy init page tables.
+        ;
+        extern NAME(Bs3PagingGetRootForPP32_rm)
+        call   NAME(Bs3PagingGetRootForPP32_rm)
+
+        cli
+        mov     cr3, eax
+
+        ;
+        ; Load the GDT and enable PP16.
+        ;
+BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
+BS3_BEGIN_TEXT16
+        mov     ax, BS3SYSTEM16
+        mov     ds, ax
+        lgdt    [Bs3Lgdt_Gdt]
+
+        mov     eax, cr0
+        or      eax, X86_CR0_PE | X86_CR0_PG
+        mov     cr0, eax
+        jmp     BS3_SEL_R0_CS16:.reload_cs_and_stuff
+.reload_cs_and_stuff:
+
+        ;
+        ; Call rountine for doing mode specific setups.
+        ;
+        extern  NAME(Bs3EnteredMode_pp16)
+        call    NAME(Bs3EnteredMode_pp16)
+
+        popfd
+        pop     eax
+        ret
+
+ %if TMPL_BITS != 16
+TMPL_BEGIN_TEXT
+ %endif
+%endif
+BS3_PROC_END_MODE   Bs3SwitchToPP16
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP32.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP32.asm	(revision 58812)
@@ -0,0 +1,132 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToPP32
+;
+
+;
+; 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"
+
+
+;;
+; Switch to 32-bit paged protected mode from any other mode.
+;
+; @cproto   BS3_DECL(void) Bs3SwitchToPE32(void);
+;
+; @uses     Nothing (except high 32-bit register parts), upper part of ESP is
+;           cleared if caller is in 16-bit mode.
+;
+; @remarks  Obviously returns to 32-bit mode, even if the caller was
+;           in 16-bit or 64-bit mode.
+;
+BS3_PROC_BEGIN_MODE Bs3SwitchToPP32
+%ifdef TMPL_PE32
+        ret
+
+%else
+        ;
+        ; Switch to real mode.
+        ;
+ %if TMPL_BITS != 32
+  %if TMPL_BITS > 32
+        shl     xPRE [xSP + xCB], 32    ; Adjust the return address from 64-bit to 32-bit.
+        add     rsp, xCB - 4
+  %else
+        push    word 0                  ; Reserve space to expand the return address.
+  %endif
+        ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
+        jmp     .sixteen_bit_segment
+BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.sixteen_bit_segment:
+ %endif
+
+        ;
+        ; Switch to real mode.
+        ;
+        extern  TMPL_NM(Bs3SwitchToRM)
+        call    TMPL_NM(Bs3SwitchToRM)
+        BS3_SET_BITS 16
+
+        push    eax
+        pushfd
+
+        ;
+        ; Make sure PAE is really off.
+        ;
+        mov     eax, cr4
+        test    eax, X86_CR4_PAE
+        jz      .cr4_is_fine
+        and     eax, ~X86_CR4_PAE
+        mov     cr4, eax
+.cr4_is_fine:
+
+        ;
+        ; Get the page directory (returned in eax).  Will lazy init page tables.
+        ;
+        extern NAME(Bs3PagingGetRootForPP32_rm)
+        call   NAME(Bs3PagingGetRootForPP32_rm)
+
+        cli
+        mov     cr3, eax
+
+        ;
+        ; Load the GDT and enable PE32.
+        ;
+BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
+BS3_BEGIN_TEXT16
+        mov     ax, BS3SYSTEM16
+        mov     ds, ax
+        lgdt    [Bs3Lgdt_Gdt]
+
+        mov     eax, cr0
+        or      eax, X86_CR0_PE | X86_CR0_PG
+        mov     cr0, eax
+        jmp     BS3_SEL_R0_CS32:dword .thirty_two_bit wrt FLAT
+BS3_BEGIN_TEXT32
+.thirty_two_bit:
+
+        ;
+        ; Call rountine for doing mode specific setups.
+        ;
+        extern  NAME(Bs3EnteredMode_pe32)
+        call    NAME(Bs3EnteredMode_pe32)
+
+        ;
+        ; Restore eax and flags (IF).
+        ;
+ %if TMPL_BITS < 32
+        and     esp, 0ffffh             ; Make sure the high word is zero.
+        movzx   eax, word [esp + 8 + 2] ; Load return address.
+        add     eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
+        mov     [esp + 8], eax          ; Store it in the place right for 32-bit returns.
+ %endif
+        pop     esp
+        popfd
+        ret
+
+ %if TMPL_BITS != 32
+TMPL_BEGIN_TEXT
+ %endif
+%endif
+BS3_PROC_END_MODE   Bs3SwitchToPP32
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 58812)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 58812)
@@ -0,0 +1,96 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SwitchToRM
+;
+
+;
+; 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"
+
+
+;;
+; Switch to real mode from any other mode.
+;
+; @cproto   BS3_DECL(void) Bs3SwitchToRealMode(void);
+;
+; @uses     Nothing (except high 32-bit register parts).
+;
+; @remarks  Obviously returns to 16-bit mode, even if the caller was
+;           in 32-bit or 64-bit mode.
+;
+BS3_PROC_BEGIN_MODE Bs3SwitchToRM
+%ifdef TMPL_RM
+        ret
+
+%else
+        ;
+        ; Get to 16-bit ring-0.
+        ;
+        extern  BS3_CMN_NM(Bs3SwitchToRing0)
+        call    BS3_CMN_NM(Bs3SwitchToRing0)
+
+ %if TMPL_BITS != 16
+        ; Adjust the return address.
+        push    xAX
+        mov     ax, [xSP + xCB]
+        mov     [xSP + xCB + xCB - 2], ax
+        pop     xAX
+
+        ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
+        jmp     .sixteen_bit_segment
+BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.sixteen_bit_segment:
+
+        extern  BS3_CMN_NM(Bs3SwitchTo16Bit)
+        call    BS3_CMN_NM(Bs3SwitchTo16Bit)
+        BS3_SET_BITS 16
+ %endif
+
+        ;
+        ; Exit to real mode.
+        ;
+%ifdef TMPL_LM
+ extern TODO_FIX_LM_MODE_RETURN
+%endif
+        push    eax
+        mov     eax, cr0
+        and     eax, X86_CR0_NO_PE_NO_PG
+        mov     cr0, eax
+        jmp     BS3TEXT16:.reload_cs
+.reload_cs:
+        pop     eax
+
+        ;
+        ; Call rountine for doing mode specific setups.
+        ;
+        extern  NAME(Bs3EnteredMode_rm)
+        call    NAME(Bs3EnteredMode_rm)
+
+        ret
+
+ %if TMPL_BITS != 16
+TMPL_BEGIN_TEXT
+ %endif
+%endif
+BS3_PROC_END_MODE   Bs3SwitchToRM
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-shutdown.c	(revision 58812)
@@ -9,5 +9,5 @@
 
 /* Just a sample. */
-BS3_DECL(void) Main_rm(void)
+BS3_DECL(void) Main_pe16(void)
 {
     void BS3_FAR *pvTmp1;
@@ -15,5 +15,4 @@
     void BS3_FAR *pvTmp3;
     void BS3_FAR *pvTmp4;
-    Bs3InitMemory_rm();
 
     Bs3TestInit("bs3-shutdown");
@@ -23,5 +22,5 @@
 Bs3PrintStr("\r\n");
 
-Bs3Printf("Bs3Printf: RX32=%#'RX32 string='%s' d=%d p=%p\n", UINT32_C(0xfdb97531), "my string", 42, Main_rm);
+Bs3Printf("Bs3Printf: RX32=%#'RX32 string='%s' d=%d p=%p\n", UINT32_C(0xfdb97531), "my string", 42, Main_pe16);
 
 pvTmp2 = Bs3MemAlloc(BS3MEMKIND_REAL, _4K);
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 58812)
@@ -502,6 +502,6 @@
   %error "Both 'TMPL_PAEV86' and 'TMPL_PAE16' are defined."
  %endif
- %ifdef TMPL_PAEV86
-  %error "Both 'TMPL_PAEV86' and 'TMPL_PAEV86' are defined."
+ %ifdef TMPL_PAE32
+  %error "Both 'TMPL_PAEV86' and 'TMPL_PAE32' are defined."
  %endif
  %ifdef TMPL_LM16
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 58812)
@@ -131,4 +131,6 @@
 
 #define BS3_SEL_VMMDEV_MMIO16       0x00f8 /**< Selector for accessing the VMMDev MMIO segment at 0100000h from 16-bit code. */
+
+#define BS3_SEL_RING_SHIFT          8      /**< For the formula: BS3_SEL_R0_XXX + ((cs & 3) << BS3_SEL_RING_SHIFT) */
 
 #define BS3_SEL_R0_FIRST            0x0100 /**< The first selector in the ring-0 block. */
@@ -336,4 +338,16 @@
 #else
 # define BS3_DECL(a_Type)   DECLIMPORT(a_Type) BS3_NEAR_CODE BS3_CALL
+#endif
+
+/** @def BS3_DECL_CALLBACK
+ * Declares a BS3Kit callback function (typically static).
+ *
+ * Until we outgrow BS3TEXT16, we use all near functions in 16-bit.
+ *
+ * @param a_Type        The return type. */
+#ifdef IN_BS3KIT
+# define BS3_DECL_CALLBACK(a_Type)   a_Type BS3_NEAR_CODE BS3_CALL
+#else
+# define BS3_DECL_CALLBACK(a_Type)   a_Type BS3_NEAR_CODE BS3_CALL
 #endif
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 58811)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 58812)
@@ -160,5 +160,5 @@
         section BS3TEXT32 align=1 progbits alloc exec nowrite
   %else
-        section BS3TEXT32 align=1 CLASS=BS3CODE32 PUBLIC USE32
+        section BS3TEXT32 align=1 CLASS=BS3CODE32 PUBLIC USE32 FLAT
   %endif
  %else
@@ -174,5 +174,5 @@
         section BS3DATA32 align=16 progbits alloc noexec write
   %else
-        section BS3DATA32 align=16 CLASS=FAR_DATA PUBLIC USE32
+        section BS3DATA32 align=16 CLASS=FAR_DATA PUBLIC USE32 FLAT
   %endif
  %else
@@ -188,5 +188,5 @@
         section BS3TEXT64 align=1 progbits alloc exec nowrite
   %else
-        section BS3TEXT64 align=1 CLASS=CODE PUBLIC USE32       ; class=CODE here because of 64-bit cl and/or wlink.exe
+        section BS3TEXT64 align=1 CLASS=CODE PUBLIC USE32 FLAT  ; class=CODE here because of 64-bit cl and/or wlink.exe
   %endif
  %else
@@ -202,5 +202,5 @@
         section BS3DATA64 align=16 progbits alloc noexec write
   %else
-        section BS3DATA64 align=16 CLASS=DATA PUBLIC USE32      ; class=DATA here because of 64-bit cl and/or wlink.exe
+        section BS3DATA64 align=16 CLASS=DATA PUBLIC USE32 FLAT  ; class=DATA here because of 64-bit cl and/or wlink.exe
   %endif
  %else
@@ -312,4 +312,20 @@
 
 ;;
+; Extern macro which mangles a BS3SYSTEM16 symbol correctly, redefining the
+; unmangled name to the mangled one for ease of use.
+;
+; @param    %1  The unmangled common name.
+;
+; @remarks  Will change to the SYSTEM16 segment, use must switch back afterwards!
+;
+%macro BS3_EXTERN_SYSTEM16 1
+ BS3_BEGIN_SYSTEM16
+ extern _ %+ %1
+ %undef  %1
+ %define %1  _ %+ %1
+%endmacro
+
+
+;;
 ; Global name with ELF attributes and size.
 ;
@@ -373,4 +389,5 @@
 %endmacro
 
+
 ;; Convenience macro for defining common procedures.
 %macro BS3_PROC_BEGIN_CMN 1
@@ -382,4 +399,16 @@
  BS3_PROC_END       BS3_CMN_NM(%1)
 %endmacro
+
+
+;; Convenience macro for defining mode specific procedures.
+%macro BS3_PROC_BEGIN_MODE 1
+ BS3_PROC_BEGIN     TMPL_NM(%1)
+%endmacro
+
+;; Convenience macro for defining mode specific procedures.
+%macro BS3_PROC_END_MODE 1
+ BS3_PROC_END       TMPL_NM(%1)
+%endmacro
+
 
 ;;
@@ -595,4 +624,12 @@
 ;; Print char (cl).
 %define BS3_SYSCALL_PRINT_CHR   0ca110001h
+;; Switch to ring-0.
+%define BS3_SYSCALL_TO_RING0    0ca110002h
+;; Switch to ring-1.
+%define BS3_SYSCALL_TO_RING1    0ca110003h
+;; Switch to ring-2.
+%define BS3_SYSCALL_TO_RING2    0ca110004h
+;; Switch to ring-3.
+%define BS3_SYSCALL_TO_RING3    0ca110005h
 ;; @}
 
@@ -624,4 +661,6 @@
 %define BS3_SEL_TSS64               0040h ;;< The 64-bit TSS selector.
 %define BS3_SEL_VMMDEV_MMIO16       00f8h ;;< Selector for accessing the VMMDev MMIO segment at 0100000h from 16-bit code.
+
+%define BS3_SEL_RING_SHIFT          8     ;;< For the formula: BS3_SEL_R0_XXX + ((cs & 3) << BS3_SEL_RING_SHIFT)
 
 %define BS3_SEL_R0_FIRST            0100h ;;< The first selector in the ring-0 block.
