Index: /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/Makefile.kmk	(revision 60657)
@@ -247,5 +247,6 @@
 	$$(bs3-cpu-basic-2_0_OUTDIR)/bs3kit/bs3-cmn-instantiate-x0.o16 \
 	$$(bs3-cpu-basic-2_0_OUTDIR)/bs3kit/bs3-cmn-instantiate.o32 \
-	$$(bs3-cpu-basic-2_0_OUTDIR)/bs3kit/bs3-cmn-instantiate.o64
+	$$(bs3-cpu-basic-2_0_OUTDIR)/bs3kit/bs3-cmn-instantiate.o64 \
+	$$(bs3-cpu-basic-2_0_OUTDIR)/bs3-cpu-basic-2-asm.o16
 endif
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.c	(revision 60657)
@@ -1310,10 +1310,20 @@
     BS3TRAPFRAME        TrapCtx;
     BS3REGCTX           Ctx;
+    BS3REGCTX           CtxUdExpected;
     BS3REGCTX           TmpCtx;
-    uint8_t             abBuf[16];
+    uint8_t const       cbBuf = 8*2;         /* test buffer area  */
+    uint8_t             abBuf[8*2 + 8 + 8];  /* test buffer w/ misalignment test space and some extra guard. */
     uint8_t BS3_FAR    *pbBuf = abBuf;
+    uint8_t const       cbIdtr = BS3_MODE_IS_64BIT_CODE(g_bTestMode) ? 2+8 : BS3_MODE_IS_32BIT_CODE(g_bTestMode) ? 2+4
+                        : (g_uBs3CpuDetected & BS3CPU_TYPE_MASK) == BS3CPU_80286 ? 2+3 : 2+4;
+    uint8_t             bFiller;
+    unsigned            off;
+    //unsigned            i, j;
+
+    g_usBs3TestStep = 0;
 
     /* make sure they're allocated  */
     Bs3MemZero(&Ctx, sizeof(Ctx));
+    Bs3MemZero(&CtxUdExpected, sizeof(CtxUdExpected));
     Bs3MemZero(&TmpCtx, sizeof(TmpCtx));
     Bs3MemZero(&TrapCtx, sizeof(TrapCtx));
@@ -1322,6 +1332,5 @@
     /* Create a context, give this routine some more stack space, point the context
        at our SIDT [xBX] + UD2 combo, and point DS:xBX at abBuf. */
-    Bs3RegCtxSave(&Ctx);
-    Ctx.rsp.u -= 0x80;
+    Bs3RegCtxSaveEx(&Ctx, g_bTestMode, 256 /*cbExtraStack*/);
     Ctx.rip.u  = (uintptr_t)BS3_FP_OFF(&bs3CpuBasic2_sidt_bx_ud2);
 # if TMPL_BITS == 32
@@ -1333,11 +1342,149 @@
 # endif
 
-    /*
-     * Check that it works at all.
-     */
-    Bs3MemZero(&abBuf, sizeof(abBuf));
+    /* For successful SIDT attempts, we'll stop at the UD2. */
+    Bs3MemCpy(&CtxUdExpected, &Ctx, sizeof(Ctx));
+    CtxUdExpected.rip.u += 3;
+
+    /*
+     * Check that it works at all and that only bytes we expect gets written to.
+     */
+    /* First with zero buffer. */
+    Bs3MemZero(abBuf, sizeof(abBuf));
+    if (!ASMMemIsAllU8(abBuf, sizeof(abBuf), 0))
+        Bs3TestFailedF("ASMMemIsAllU8 or Bs3MemZero is busted: abBuf=%.*Rhxs\n", sizeof(abBuf), pbBuf);
+    if (!ASMMemIsZero(abBuf, sizeof(abBuf)))
+        Bs3TestFailedF("ASMMemIsZero or Bs3MemZero is busted: abBuf=%.*Rhxs\n", sizeof(abBuf), pbBuf);
     Bs3TrapSetJmpAndRestore(&Ctx, &TrapCtx);
-    g_usBs3TestStep = 0;
-    //bs3CpuBasic2_CompareIntCtx1(&TrapCtx, &Ctx, 0x80 /*bXcpt*/);
+    bs3CpuBasic2_CompareUdCtx(&TrapCtx, &CtxUdExpected);
+    if (!ASMMemIsZero(&abBuf[cbIdtr], cbBuf - cbIdtr))
+        Bs3TestFailedF("Unexpected buffer bytes set (#1): cbIdtr=%u abBuf=%.*Rhxs\n", cbIdtr, cbBuf, pbBuf);
+    g_usBs3TestStep++;
+
+    /* Again with buffer filled a byte not occuring in the previous result. */
+    bFiller = 0x55;
+    while (Bs3MemChr(abBuf, bFiller, cbBuf) != NULL)
+        bFiller++;
+    Bs3MemSet(abBuf, bFiller, sizeof(abBuf));
+    if (!ASMMemIsAllU8(abBuf, sizeof(abBuf), bFiller))
+        Bs3TestFailedF("ASMMemIsAllU8 or Bs3MemSet is busted: bFiller=%#x abBuf=%.*Rhxs\n", bFiller, sizeof(abBuf), pbBuf);
+
+    Bs3TrapSetJmpAndRestore(&Ctx, &TrapCtx);
+    bs3CpuBasic2_CompareUdCtx(&TrapCtx, &CtxUdExpected);
+    if (!ASMMemIsAllU8(&abBuf[cbIdtr], cbBuf - cbIdtr, bFiller))
+        Bs3TestFailedF("Unexpected buffer bytes set (#2): cbIdtr=%u bFiller=%#x abBuf=%.*Rhxs\n", cbIdtr, bFiller, cbBuf, pbBuf);
+    if (Bs3MemChr(abBuf, bFiller, cbIdtr) != NULL)
+        Bs3TestFailedF("Not all bytes touched: cbIdtr=%u bFiller=%#x abBuf=%.*Rhxs\n", cbIdtr, bFiller, cbBuf, pbBuf);
+    g_usBs3TestStep++;
+
+    /*
+     * Slide the buffer along 8 bytes to cover misalignment.
+     */
+    for (off = 0; off < 8; off++)
+    {
+        pbBuf = &abBuf[off];
+        CtxUdExpected.rbx.u = Ctx.rbx.u = BS3_FP_OFF(pbBuf);
+
+        /* First with zero buffer. */
+        Bs3MemZero(abBuf, sizeof(abBuf));
+        Bs3TrapSetJmpAndRestore(&Ctx, &TrapCtx);
+        bs3CpuBasic2_CompareUdCtx(&TrapCtx, &CtxUdExpected);
+        if (off > 0 && !ASMMemIsZero(abBuf, off))
+            Bs3TestFailedF("Unexpected buffer bytes set before (#3): cbIdtr=%u off=%u abBuf=%.*Rhxs\n",
+                           cbIdtr, off, off + cbBuf, abBuf);
+        if (!ASMMemIsZero(&abBuf[off + cbIdtr], sizeof(abBuf) - cbIdtr - off))
+            Bs3TestFailedF("Unexpected buffer bytes set after (#3): cbIdtr=%u off=%u abBuf=%.*Rhxs\n",
+                           cbIdtr, off, off + cbBuf, abBuf);
+        g_usBs3TestStep++;
+
+        /* Again with buffer filled a byte not occuring in the previous result. */
+        Bs3MemSet(abBuf, bFiller, sizeof(abBuf));
+        Bs3TrapSetJmpAndRestore(&Ctx, &TrapCtx);
+        bs3CpuBasic2_CompareUdCtx(&TrapCtx, &CtxUdExpected);
+        if (off > 0 && !ASMMemIsAllU8(abBuf, off, bFiller))
+            Bs3TestFailedF("Unexpected buffer bytes set before (#4): cbIdtr=%u off=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                           cbIdtr, off, bFiller, off + cbBuf, abBuf);
+        if (!ASMMemIsAllU8(&abBuf[off + cbIdtr], sizeof(abBuf) - cbIdtr - off, bFiller))
+            Bs3TestFailedF("Unexpected buffer bytes set after (#4): cbIdtr=%u off=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                           cbIdtr, off, bFiller, off + cbBuf, abBuf);
+        if (Bs3MemChr(&abBuf[off], bFiller, cbIdtr) != NULL)
+            Bs3TestFailedF("Not all bytes touched (#4): cbIdtr=%u off=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                           cbIdtr, off, bFiller, off + cbBuf, abBuf);
+        g_usBs3TestStep++;
+
+    }
+    pbBuf = abBuf;
+    CtxUdExpected.rbx.u = Ctx.rbx.u = BS3_FP_OFF(pbBuf);
+
+    /*
+     * Play with the selector limit if the target mode supports limit checking
+     * We use BS3_SEL_TEST_PAGE_00 for this
+     */
+    if (   !BS3_MODE_IS_RM_OR_V86(g_bTestMode)
+        && !BS3_MODE_IS_64BIT_CODE(g_bTestMode))
+    {
+        uint16_t cbLimit;
+        uint16_t const uSavedDs = Ctx.ds;
+        uint32_t uFlatBuf = Bs3SelPtrToFlat(pbBuf);
+        Bs3GdteTestPage00 = Bs3Gdte_DATA16;
+        Bs3GdteTestPage00.Gen.u16BaseLow  = (uint16_t)uFlatBuf;
+        Bs3GdteTestPage00.Gen.u8BaseHigh1 = (uint8_t)(uFlatBuf >> 16);
+        Bs3GdteTestPage00.Gen.u8BaseHigh2 = (uint8_t)(uFlatBuf >> 24);
+
+        CtxUdExpected.ds = Ctx.ds = BS3_SEL_TEST_PAGE_00;
+        for (off = 0; off < 8; off++)
+        {
+            CtxUdExpected.rbx.u = Ctx.rbx.u = off;
+            for (cbLimit = 0; cbLimit < cbIdtr*2; cbLimit++)
+            {
+                Bs3GdteTestPage00.Gen.u16LimitLow = cbLimit;
+                Bs3MemSet(abBuf, bFiller, sizeof(abBuf));
+                Bs3TrapSetJmpAndRestore(&Ctx, &TrapCtx);
+                if (off + cbIdtr <= cbLimit + 1)
+                {
+                    bs3CpuBasic2_CompareUdCtx(&TrapCtx, &CtxUdExpected);
+                    if (Bs3MemChr(&abBuf[off], bFiller, cbIdtr) != NULL)
+                        Bs3TestFailedF("Not all bytes touched (#5): cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                       cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+                }
+                else
+                {
+                    bs3CpuBasic2_CompareGpCtx(&TrapCtx, &Ctx, 0);
+                    if (off + 2 <= cbLimit + 1)
+                    {
+                        if (Bs3MemChr(&abBuf[off], bFiller, 2) != NULL)
+                            Bs3TestFailedF("Limit bytes not touched (#6): cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                           cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+                        if (!ASMMemIsAllU8(&abBuf[off + 2], cbIdtr - 2, bFiller))
+                            Bs3TestFailedF("Base bytes touched on #GP (#6): cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                           cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+                    }
+                    else if (!ASMMemIsAllU8(abBuf, sizeof(abBuf), bFiller))
+                        Bs3TestFailedF("Bytes touched on #GP: cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                       cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+                }
+
+                if (off > 0 && !ASMMemIsAllU8(abBuf, off, bFiller))
+                    Bs3TestFailedF("Leading bytes touched (#7): cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                   cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+                if (!ASMMemIsAllU8(&abBuf[off + cbIdtr], sizeof(abBuf) - off - cbIdtr, bFiller))
+                    Bs3TestFailedF("Trailing bytes touched (#7): cbIdtr=%u off=%u cbLimit=%u bFiller=%#x abBuf=%.*Rhxs\n",
+                                   cbIdtr, off, cbLimit, bFiller, off + cbBuf, abBuf);
+
+                g_usBs3TestStep++;
+            }
+        }
+
+        CtxUdExpected.ds = Ctx.ds = uSavedDs;
+        CtxUdExpected.rbx.u = Ctx.rbx.u = BS3_FP_OFF(pbBuf);
+    }
+
+    /*
+     * Play with the paging.
+     */
+    if (BS3_MODE_IS_PAGED(g_bTestMode))
+    {
+
+
+    }
 
 }
@@ -1591,4 +1738,6 @@
 BS3_DECL_FAR(uint8_t) TMPL_NM(bs3CpuBasic2_sidt)(uint8_t bMode)
 {
+//if (bMode == BS3_MODE_PE16_V86)
+{
     g_pszTestMode = TMPL_NM(g_szBs3ModeName);
     g_bTestMode   = bMode;
@@ -1606,4 +1755,6 @@
      */
     Bs3TrapInit();
+}
+
     return 0;
 }
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2-template.mac	(revision 60657)
@@ -96,4 +96,5 @@
 .again: ud2
         jmp     .again
+AssertCompile(.again - BS3_CMN_NM(bs3CpuBasic2_sidt_bx_ud2) == 3)
 BS3_PROC_END_CMN   bs3CpuBasic2_sidt_bx_ud2
 
@@ -103,4 +104,5 @@
 .again: ud2
         jmp     .again
+AssertCompile(.again - BS3_CMN_NM(bs3CpuBasic2_lidt_bx_ud2) == 3)
 BS3_PROC_END_CMN   bs3CpuBasic2_lidt_bx_ud2
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3-cpu-basic-2.c	(revision 60657)
@@ -49,8 +49,8 @@
 {
     //BS3TESTMODEENTRY_MODE("tss / gate / esp", bs3CpuBasic2_TssGateEsp),
-    BS3TESTMODEENTRY_MODE("raise xcpt #1", bs3CpuBasic2_RaiseXcpt1),
+//    BS3TESTMODEENTRY_MODE("raise xcpt #1", bs3CpuBasic2_RaiseXcpt1),
     //BS3TESTMODEENTRY_CMN("iret", bs3CpuBasic2_iret),
-    BS3TESTMODEENTRY_MODE("iret", bs3CpuBasic2_iret),
-//    BS3TESTMODEENTRY_MODE("sidt", bs3CpuBasic2_sidt),
+//    BS3TESTMODEENTRY_MODE("iret", bs3CpuBasic2_iret),
+    BS3TESTMODEENTRY_MODE("sidt", bs3CpuBasic2_sidt),
 };
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 60657)
@@ -71,7 +71,9 @@
        bs3-cmn-StrNLen.c \
        bs3-cmn-StrCpy.c \
+       bs3-cmn-MemChr.asm \
        bs3-cmn-MemCpy.c \
        bs3-cmn-MemPCpy.c \
        bs3-cmn-MemMove.c \
+       bs3-cmn-MemSet.asm \
        bs3-cmn-MemZero.asm \
        bs3-cmn-MemAlloc.c \
@@ -88,5 +90,7 @@
        bs3-cmn-RegCtxPrint.c \
        bs3-cmn-RegCtxSave.asm \
+       bs3-cmn-RegCtxSaveEx.asm \
        bs3-cmn-SelFar32ToFlat32.c \
+       bs3-cmn-SelFar32ToFlat32NoClobber.asm \
        bs3-cmn-SelProtFar32ToFlat32.c \
        bs3-cmn-SelProtModeCodeToRealMode.asm \
@@ -94,4 +98,10 @@
        bs3-cmn-SelFlatCodeToRealMode.asm \
        bs3-cmn-SelFlatCodeToProtFar16.asm \
+       bs3-cmn-SelRealModeDataToProtFar16.asm \
+       bs3-cmn-SelProtFar16DataToRealMode.asm \
+       bs3-cmn-SelRealModeDataToFlat.asm \
+       bs3-cmn-SelProtFar16DataToFlat.asm \
+       bs3-cmn-SelFlatDataToProtFar16.asm \
+       bs3-cmn-SelFlatDataToRealMode.asm \
        bs3-cmn-SlabInit.c \
        bs3-cmn-SlabAlloc.c \
@@ -451,5 +461,5 @@
 # Rule for regenerating bs3kit-mangling-functions-undef.h.
 #
-bs3kit-mangling-code-undef.h: $(PATH_SUB_CURRENT)/bs3kit-mangling-code-define.h
+bs3kit-mangling-code-undef.h: $(PATH_SUB_CURRENT)/bs3kit-mangling-code-define.h $(MAKEFILE)
 	$(SED) \
 		-e 's/#\( *\)define \([a-zA-Z_][a-zA-Z0-9_]*\) .*$(DOLLAR)/#\1undef \2/' \
@@ -464,5 +474,6 @@
 		$(PATH_SUB_CURRENT)/bs3kit.h \
 		$(PATH_SUB_CURRENT)/bs3-cmn-paging.h \
-		$(PATH_SUB_CURRENT)/bs3-cmn-test.h
+		$(PATH_SUB_CURRENT)/bs3-cmn-test.h \
+		$(MAKEFILE)
 	$(APPEND) -tn "$(dir $<)$@" \
 		 '/* $(DOLLAR)Id: $(DOLLAR) */' \
@@ -496,5 +507,5 @@
 		-e 's/^ *BS3_CMN_PROTO_NOSB([^,]*, *\([a-zA-Z_][a-zA-Z0-9_]*\) *,.*$(DOLLAR)/#define \1 BS3_CMN_MANGLER(\1)/p' \
 		-e 's/^ *BS3_CMN_PROTO_FARSTUB([^,]*,[^,]*, *\([a-zA-Z_][a-zA-Z0-9_]*\) *,.*$(DOLLAR)/#define \1 BS3_CMN_MANGLER(\1)/p' \
-		$< | sort >> "$(dir $<)bs3kit-mangling-code-define.h"
+		$(filter %.h,$^) | sort >> "$(dir $<)bs3kit-mangling-code-define.h"
 	$(APPEND) -n "$(dir $<)$@" '#ifndef BS3_CMN_ONLY'
 	$(SED) -n \
@@ -502,5 +513,5 @@
 		-e 's/^ *BS3_MODE_PROTO_NOSB([^,]*, *\([a-zA-Z_][a-zA-Z0-9_]*\) *,.*$(DOLLAR)/# define \1 BS3_MODE_MANGLER(\1)/p' \
 		-e 's/^ *BS3_MODE_PROTO_FARSTUB([^,]*,[^,]*, *\([a-zA-Z_][a-zA-Z0-9_]*\) *,.*$(DOLLAR)/# define \1 BS3_MODE_MANGLER(\1)/p' \
-		$< | sort >> "$(dir $<)bs3kit-mangling-code-define.h"
+		$(filter %.h,$^) | sort >> "$(dir $<)bs3kit-mangling-code-define.h"
 	$(APPEND) -n "$(dir $<)$@" '#endif /* !BS3_CMN_ONLY */'
 
@@ -512,5 +523,6 @@
 		$(PATH_SUB_CURRENT)/bs3-cmn-memory.h \
 		$(PATH_SUB_CURRENT)/bs3-cmn-paging.h \
-		$(PATH_SUB_CURRENT)/bs3-cmn-test.h
+		$(PATH_SUB_CURRENT)/bs3-cmn-test.h \
+		$(MAKEFILE)
 	$(APPEND) -tn "$(dir $<)$@" \
 		 '# $(DOLLAR)Id: $(DOLLAR)' \
@@ -542,8 +554,8 @@
 	$(SED) -n \
 		-e '/^ *BS3_CMN_PROTO_STUB/p' \
-		-e '/^ *BS3_CMD_PROTO_FARSTUB/p' \
+		-e '/^ *BS3_CMN_PROTO_FARSTUB/p' \
 		-e '/^ *BS3_MODE_PROTO_STUB/p' \
 		-e '/^ *BS3_MODE_PROTO_FARSTUB/p' \
-		$< \
+		$(filter %.h,$^) \
 	| sort \
 	| $(SED) -n \
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-Trap16Generic.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-Trap16Generic.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-Trap16Generic.asm	(revision 60657)
@@ -432,6 +432,6 @@
 
 .skip_crX_because_cpl_not_0:
-        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
-        jmp     .set_flags
+        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], \
+                BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4 | BS3REG_CTX_F_NO_CR0_IS_MSW
 
 CPU 286
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-TrapRmV86Generic.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-TrapRmV86Generic.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c16-TrapRmV86Generic.asm	(revision 60657)
@@ -54,7 +54,8 @@
 BS3_PROC_BEGIN Bs3TrapRmV86GenericEntries
 %macro Bs3TrapRmV86GenericEntryNoErr 1
-        push    byte 0                  ; 2 byte: fake error code
-        db      06ah, i                 ; 2 byte: push imm8 - note that this is a signextended value.
-        jmp     %1                      ; 3 byte
+        push    ax                      ; 1 byte:  Reserve space for fake error cd.     (BP(+2) + 4)
+        push    ax                      ; 1 byte:  Save AX                              (BP(+2) + 2)
+        mov     ax, i | 00000h          ; 2 bytes: AL = trap/interrupt number; AH=indicate no error code
+        jmp     %1                      ; 3 bytes: Jump to handler code
         ALIGNCODE(8)
 %assign i i+1
@@ -62,6 +63,7 @@
 
 %macro Bs3TrapRmV86GenericEntryErrCd 1
-        db      06ah, i                 ; 2 byte: push imm8 - note that this is a signextended value.
-        jmp     %1                      ; 3 byte
+        push    ax                      ; 1 byte:  Save AX                              (BP(+2) + 2)
+        mov     ax, i | 0ff00h          ; 2 bytes: AL = trap/interrupt number; AH=indicate have error code.
+        jmp     %1                      ; 3 bytes: Jump to handler code
         ALIGNCODE(8)
 %assign i i+1
@@ -111,6 +113,4 @@
 ; Trap or interrupt with error code, faked if necessary.
 ;
-; Note! This code is going to "misbehave" if the high word of ESP is not cleared.
-;
 BS3_PROC_BEGIN _bs3TrapRmV86GenericTrapOrInt
 BS3_PROC_BEGIN bs3TrapRmV86GenericTrapOrInt
@@ -137,10 +137,9 @@
         movzx   ebx, sp
 
-        mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], eax
-        mov     edx, [bp - 12h]         ; This isn't quite right for wrap arounds, but close enough for now
+
+        mov     edx, [bp - 12h]
         mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], edx     ; high bits
         mov     [ss:bx + BS3TRAPFRAME.uHandlerRsp], edx             ; high bits
         mov     dx, [bp - 0eh]
-        ;mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], dx - share this here
         mov     [ss:bx + BS3TRAPFRAME.uHandlerSs], dx
         mov     edx, [bp - 0ch]
@@ -153,11 +152,16 @@
         mov     edx, [bp]
         mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], edx
-
-        mov     dl, [bp + 4]
-        mov     [ss:bx + BS3TRAPFRAME.bXcpt], dl
-
+        mov     edx, eax                                            ; high bits
+        mov     dx, [bp + 4]
+        mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], edx
+
+        mov     [ss:bx + BS3TRAPFRAME.bXcpt], al
+
+        test    ah, 0ffh
+        jz      .no_error_code
+;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
         mov     dx, [bp + 6]
-;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
         mov     [ss:bx + BS3TRAPFRAME.uErrCd], dx
+.no_error_code:
 
         add     bp, 6                   ; adjust so it points to the word before the iret frame.
@@ -173,11 +177,11 @@
         push    bp
         mov     bp, sp
-        push    bx
-        pushf
-        push    ax
+        push    bx                      ; BP - 2
+        pushf                           ; BP - 4
         cld
 
         ; Reserve space for the the register and trap frame.
         mov     bx, (BS3TRAPFRAME_size + 7) / 8
+        push    ax
         xor     ax, ax
 .more_zeroed_space:
@@ -189,7 +193,6 @@
         jnz     .more_zeroed_space
         mov     bx, sp
-
-        mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], ax
-        ;mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], ss - share this here
+        pop     ax
+
         mov     [ss:bx + BS3TRAPFRAME.uHandlerSs], ss
         mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], dx
@@ -201,10 +204,15 @@
         mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], dx
 
-        mov     dl, [bp + 2]
-        mov     [ss:bx + BS3TRAPFRAME.bXcpt], dl
-
+        mov     dx, [bp + 2]
+        mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], dx
+
+        mov     [ss:bx + BS3TRAPFRAME.bXcpt], al
+
+        test    ah, 0ffh
+        jz      .no_error_code
+;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
         mov     dx, [bp + 4]
-;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
         mov     [ss:bx + BS3TRAPFRAME.uErrCd], dx
+.no_error_code:
 
         add     bp, 4                   ; adjust so it points to the word before the iret frame.
@@ -217,5 +225,5 @@
 ; Common context saving code and dispatching.
 ;
-; @param    bx      Pointer to the trap frame, zero filled.  The following members
+; @param    ss:bx   Pointer to the trap frame, zero filled.  The following members
 ;                   have been filled in by the previous code:
 ;                       - bXcpt
@@ -295,14 +303,22 @@
         mov     byte [ss:bx + BS3TRAPFRAME.cbIretFrame], 3*2
 
+        ; The VM flag and CPL.
+        test    al, BS3_MODE_CODE_V86
+        jz      .dont_set_vm
+        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags + 2], X86_EFL_VM >> 16
+        mov     byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.bCpl], 3
+.dont_set_vm:
+
+
         ;
         ; Control registers.
         ;
+        ; Since we're in real or v8086 here, we cannot save TR and LDTR.
+        ; But get MSW (CR0) first since that's always accessible and we
+        ; need it even on a 386 to check whether we're in v8086 mode or not.
+        ;
         cmp     byte [BS3_DATA16_WRT(g_uBs3CpuDetected)], BS3CPU_80286
-        jb      .skip_control_regsiters_because_80186_or_older
-
-        ; The 286 ones.
+        jb      .skip_control_registers_because_80186_or_older
 CPU 286
-        str     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.tr]
-        sldt    [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.ldtr]
         smsw    ax
         mov     [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0], ax
@@ -333,9 +349,10 @@
 
 CPU 8086
-.skip_control_regsiters_because_80186_or_older:
+.skip_control_registers_because_80186_or_older:
 .skip_crX_because_v8086:
-        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
+        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], \
+                BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4
 .set_flags:                             ; The double fault code joins us here.
-        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_AMD64
+        or      byte [ss:bx + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_AMD64 | BS3REG_CTX_F_NO_TR_LDTR
 
         ;
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm	(revision 60657)
@@ -370,6 +370,7 @@
 
 .skip_crX_because_cpl_not_0:
-        or      byte [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
-
+        or      byte [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], \
+                BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4
+        smsw    [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0]
 .set_flags:
         or      byte [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_AMD64
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c64-Trap64Generic.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c64-Trap64Generic.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c64-Trap64Generic.asm	(revision 60657)
@@ -296,5 +296,7 @@
 
 .skip_crX_because_cpl_not_0:
-        or      byte [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
+        or      byte [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], \
+                BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4
+        smsw    [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0]
 
         ;
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Disable.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Disable.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Disable.asm	(revision 60657)
@@ -40,8 +40,8 @@
 BS3_PROC_BEGIN_CMN Bs3A20Disable, BS3_PBC_HYBRID_0_ARGS
         ; Must call both because they may be ORed together on real HW.
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
         call    BS3_CMN_NM(Bs3A20DisableViaKbd)
         call    BS3_CMN_NM(Bs3A20DisableViaPortA)
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
         BS3_HYBRID_RET
 BS3_PROC_END_CMN   Bs3A20Disable
@@ -80,5 +80,5 @@
         pushf
         cli
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
 
         call    Bs3KbdWait
@@ -97,5 +97,5 @@
         call    Bs3KbdWait
 
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
         popf
         pop     xAX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Enable.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Enable.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-A20Enable.asm	(revision 60657)
@@ -41,5 +41,5 @@
         push    xBP
         mov     xBP, xSP
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
 
         call    BS3_CMN_NM(Bs3A20EnableViaPortA)
@@ -63,5 +63,5 @@
         pushf
         cli
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
 
         call    Bs3KbdWait
@@ -80,5 +80,5 @@
         call    Bs3KbdWait
 
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
         popf
         pop     xAX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-KbdWrite.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-KbdWrite.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-KbdWrite.asm	(revision 60657)
@@ -49,5 +49,5 @@
         mov     xBP, xSP
         push    xAX
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
 
         mov     al, [xBP + xCB*2]
@@ -59,5 +59,5 @@
         call    Bs3KbdWait
 
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
         pop     xAX
         pop     xBP
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemChr.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemChr.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemChr.asm	(revision 60657)
@@ -0,0 +1,78 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3MemChr.
+;
+
+;
+; 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"
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(void BS3_FAR *, Bs3MemChr,(void BS3_FAR const *pvHaystack, uint8_t bNeedle, size_t cbHaystack));
+;
+BS3_PROC_BEGIN_CMN Bs3MemChr, BS3_PBC_HYBRID
+        push    xBP
+        mov     xBP, xSP
+        push    xDI
+TONLY16 push    es
+
+%if TMPL_BITS == 64
+
+        mov     rdi, rcx                ; rdi = pvHaystack
+        mov     rcx, r8                 ; rcx = cbHaystack
+        mov     al, dl                  ; bNeedle
+        mov     rcx, r8
+
+%elif TMPL_BITS == 16
+        mov     di, [bp + 2 + cbCurRetAddr]     ; pvHaystack.off
+        mov     es, [bp + 2 + cbCurRetAddr + 2] ; pvHaystack.sel
+        mov     al, [bp + 2 + cbCurRetAddr + 4] ; bNeedle
+        mov     cx, [bp + 2 + cbCurRetAddr + 6] ; cbHaystack
+
+%elif TMPL_BITS == 32
+        mov     edi, [ebp + 8]                          ; pvHaystack
+        mov     al, byte [ebp + 4 + cbCurRetAddr + 4]   ; bNeedle
+        mov     ecx, [ebp + 4 + cbCurRetAddr + 8]       ; cbHaystack
+%else
+ %error "TMPL_BITS!"
+%endif
+
+        cld
+        repne scasb
+        je      .found
+
+        xor     xAX, xAX
+TONLY16 xor     dx, dx
+
+.return:
+TONLY16 pop     es
+        pop     xDI
+        pop     xBP
+        BS3_HYBRID_RET
+
+.found:
+        lea     xAX, [xDI - 1]
+TONLY16 mov     dx, es
+        jmp     .return
+
+BS3_PROC_END_CMN   Bs3MemChr
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemSet.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemSet.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemSet.asm	(revision 60657)
@@ -0,0 +1,92 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3MemSet.
+;
+
+;
+; 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_CMN_PROTO_NOSB(void, Bs3MemSet,(void BS3_FAR *pvDst, uint8_t bFiller, size_t cbDst));
+;
+BS3_PROC_BEGIN_CMN Bs3MemSet, BS3_PBC_HYBRID
+        push    xBP
+        mov     xBP, xSP
+        push    xDI
+%ifdef RT_ARCH_AMD64
+
+        mov     rdi, rcx                ; rdi = pvDst
+        mov     rcx, r8                 ; rcx = cbDst
+        movzx   edx, dl                 ; bFiller
+        mov     rax, 0101010101010101h
+        mul     rdx
+        mov     rcx, r8
+        shr     rcx, 3                  ; calc qword count.
+        cld
+        rep stosq
+
+        mov     rcx, r8                 ; cbDst
+        and     rcx, 7                  ; calc trailing byte count.
+        rep stosb
+
+%elif ARCH_BITS == 16
+        push    es
+
+        mov     di, [bp + 2 + cbCurRetAddr]     ; pvDst.off
+        mov     es, [bp + 2 + cbCurRetAddr + 2] ; pvDst.sel
+        mov     al, [bp + 2 + cbCurRetAddr + 4] ; bFiller
+        mov     ah, al
+        mov     cx, [bp + 2 + cbCurRetAddr + 6] ; cbDst
+        shr     cx, 1                           ; calc dword count.
+        rep stosw
+
+        mov     cx, [bp + 2 + cbCurRetAddr + 6] ; cbDst
+        and     cx, 1                           ; calc tailing byte count.
+        rep stosb
+
+        pop     es
+
+%elif ARCH_BITS == 32
+        mov     edi, [ebp + 8]                          ; pvDst
+        mov     al, byte [ebp + 4 + cbCurRetAddr + 4]   ; bFiller
+        mov     ah, al
+        mov     dx, ax
+        shl     eax, 16
+        mov     ax, dx                                  ; eax = RT_MAKE_U32_FROM_U8(bFiller, bFiller, bFiller, bFiller)
+        mov     ecx, [ebp + 4 + cbCurRetAddr + 8]       ; cbDst
+        shr     cx, 2                                   ; calc dword count.
+        rep stosd
+
+        mov     ecx, [ebp + 4 + cbCurRetAddr + 8]       ; cbDst
+        and     ecx, 3                                  ; calc tailing byte count.
+        rep stosb
+
+%else
+ %error "Unknown bitness."
+%endif
+
+        pop     xDI
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3MemSet
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintU32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintU32.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintU32.asm	(revision 60657)
@@ -43,5 +43,5 @@
         push    sCX
         push    sBX
-        BS3_ONLY_16BIT_STMT push ds
+BONLY16 push    ds
 
         mov     eax, [xBP + xCB + cbCurRetAddr]
@@ -49,6 +49,6 @@
         ; Allocate a stack buffer and terminate it. ds:bx points ot the end.
         sub     xSP, 30h
-        BS3_ONLY_16BIT_STMT mov bx, ss
-        BS3_ONLY_16BIT_STMT mov ds, bx
+BONLY16 mov     bx, ss
+BONLY16 mov     ds, bx
         mov     xBX, xSP
         add     xBX, 2fh
@@ -66,11 +66,11 @@
 
         ; Print the string.
-        BS3_ONLY_64BIT_STMT add     rsp, 18h
-        BS3_ONLY_16BIT_STMT push    ss
+BONLY64 add     rsp, 18h
+BONLY16 push    ss
         push    xBX
         BS3_CALL Bs3PrintStr, 1
 
         add     xSP, 30h + BS3_IF_16_32_64BIT(2, 0, 18h) + xCB
-        BS3_ONLY_16BIT_STMT pop ds
+BONLY16 pop     ds
         pop     sBX
         pop     sCX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintX32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintX32.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PrintX32.asm	(revision 60657)
@@ -43,5 +43,5 @@
         push    sCX
         push    sBX
-        BS3_ONLY_16BIT_STMT push ds
+BONLY16 push    ds
 
         mov     eax, [xBP + xCB + cbCurRetAddr]
@@ -49,6 +49,6 @@
         ; Allocate a stack buffer and terminate it. ds:bx points ot the end.
         sub     xSP, 30h
-        BS3_ONLY_16BIT_STMT mov bx, ss
-        BS3_ONLY_16BIT_STMT mov ds, bx
+BONLY16 mov     bx, ss
+BONLY16 mov     ds, bx
         mov     xBX, xSP
         add     xBX, 2fh
@@ -70,11 +70,11 @@
 
         ; Print the string.
-        BS3_ONLY_64BIT_STMT add     rsp, 18h
-        BS3_ONLY_16BIT_STMT push    ss
+BONLY64 add     rsp, 18h
+BONLY16 push    ss
         push    xBX
         BS3_CALL Bs3PrintStr, 1
 
         add     xSP, 30h + BS3_IF_16_32_64BIT(2, 0, 18h) + xCB
-        BS3_ONLY_16BIT_STMT pop ds
+BONLY16 pop     ds
         pop     sBX
         pop     sCX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxRestore.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxRestore.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxRestore.asm	(revision 60657)
@@ -69,7 +69,15 @@
         ; CS or SS values).
         ;
+%if TMPL_BITS == 16
+        cmp     byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_RM
+        je      .in_ring0
+        test    byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_CODE_V86
+        jnz     .do_syscall_restore_ctx
+%endif
         mov     ax, ss
         test    al, 3
         jz      .in_ring0
+
+.do_syscall_restore_ctx:
 %if TMPL_BITS == 16
         mov     si, [bp + xCB + cbCurRetAddr]
@@ -84,5 +92,4 @@
 %endif
         call    Bs3Syscall
-.in_ring0:
 
         ;
@@ -90,4 +97,5 @@
         ; and g_uBs3CpuDetected), DS:xBX with pRegCtx and fFlags into xCX.
         ;
+.in_ring0:
 %if TMPL_BITS == 16
         mov     ax, BS3_SEL_DATA16
@@ -236,5 +244,5 @@
         test    cl, BS3TRAPRESUME_F_SKIP_CRX
         jnz     .skip_control_regs
-        test    byte [xBX + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
+        test    byte [xBX + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3
         jnz     .skip_control_regs
 
@@ -355,5 +363,6 @@
         ; 32-bit/16-bit is more complicated as we have three different iret frames.
         ;
-        cmp     byte [BS3_ONLY_16BIT(es:) BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_RM
+        mov     al, [BS3_ONLY_16BIT(es:) BS3_DATA16_WRT(g_bBs3CurrentMode)]
+        cmp     al, BS3_MODE_RM
         je      .iretd_same_cpl_rm
 
@@ -387,9 +396,9 @@
         mov     ecx, [xBX + BS3REGCTX.rcx]
         mov     esi, [xBX + BS3REGCTX.rsi]
-%if TMPL_BITS == 16 ; if SS is 16-bit, we will not be able to restore the high word.
+ %if TMPL_BITS == 16 ; if SS is 16-bit, we will not be able to restore the high word.
         mov     edi, [xBX + BS3REGCTX.rsp]
         mov     di, sp
         mov     esp, edi
-%endif
+ %endif
         mov     edi, [xBX + BS3REGCTX.rdi]
         mov     ebx, [xBX + BS3REGCTX.rbx]
@@ -405,6 +414,8 @@
         ; Use STOSD/ES:EDI to create the frame.
         mov     es,  [xBX + BS3REGCTX.ss]
-        movzx   esp, word [xBX + BS3REGCTX.rsp]
-        jmp     .using_16_bit_stack_pointer
+        mov     esi, [xBX + BS3REGCTX.rsp]
+        sub     esi, 5*4
+        movzx   edi, si
+        jmp     .es_edi_is_pointing_to_return_frame_location
 
 .iretd_same_cpl:
@@ -420,7 +431,6 @@
         jnz     .using_32_bit_stack_pointer
 .using_16_bit_stack_pointer:
+        mov     esi, edi                ; save rsp for later.
         movzx   edi, di
-        mov     esi, [xBX + BS3REGCTX.rsp]
-        mov     si, di                  ; save rsp for later.
         jmp     .es_edi_is_pointing_to_return_frame_location
 .using_32_bit_stack_pointer:
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSave.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSave.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSave.asm	(revision 60657)
@@ -44,5 +44,5 @@
 ;
 BS3_PROC_BEGIN_CMN Bs3RegCtxSave, BS3_PBC_HYBRID_SAFE
-        TMPL_ONLY_16BIT_STMT CPU 8086
+TONLY16 CPU 8086
         BS3_CALL_CONV_PROLOG 1
         push    xBP
@@ -52,6 +52,6 @@
         push    xCX                     ; xBP - xCB*3: save incoming xCX
         push    xDI                     ; xBP - xCB*4: save incoming xDI
-        BS3_ONLY_16BIT_STMT push    es  ; xBP - xCB*5
-        BS3_ONLY_16BIT_STMT push    ds  ; xBP - xCB*6
+BONLY16 push    es                      ; xBP - xCB*5
+BONLY16 push    ds                      ; xBP - xCB*6
 
         ;
@@ -153,4 +153,12 @@
         pop     dword [xDI + BS3REGCTX.rflags]
 %endif
+%if TMPL_BITS != 64
+        ; The VM flag is never on the stack, so derive it from the bMode we saved above.
+        test    byte [xDI + BS3REGCTX.bMode], BS3_MODE_CODE_V86
+        jz      .not_v8086
+        or      byte [xDI + BS3REGCTX.rflags + 2], X86_EFL_VM >> 16
+        mov     byte [xDI + BS3REGCTX.bCpl], 3
+.not_v8086:
+%endif
 
         ; 386 segment registers.
@@ -186,17 +194,25 @@
 
 .common_full_no_control_regs:
-        or      byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
+        or      byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4
+        smsw    [xDI + BS3REGCTX.cr0]
 
         ; 80286 control registers.
 .common_80286:
-        TMPL_ONLY_16BIT_STMT CPU 286
+TONLY16 CPU 286
+%if TMPL_BITS != 64
         cmp     cl, BS3_MODE_RM
-        je      .common_ancient
+        je      .no_str_sldt
+        test    cl, BS3_MODE_CODE_V86
+        jnz     .no_str_sldt
+%endif
         str     [xDI + BS3REGCTX.tr]
         sldt    [xDI + BS3REGCTX.ldtr]
 
+.no_str_sldt:
+        or      byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_TR_LDTR
+
         ; Common stuff - stuff on the stack, 286 segment registers.
 .common_ancient:
-        TMPL_ONLY_16BIT_STMT CPU 8086
+TONLY16 CPU 8086
         mov     xAX, [xBP - xCB*1]
         mov     [xDI + BS3REGCTX.rflags], xAX
@@ -232,6 +248,6 @@
         ;
 .return:
-        BS3_ONLY_16BIT_STMT pop     ds
-        BS3_ONLY_16BIT_STMT pop     es
+BONLY16 pop     ds
+BONLY16 pop     es
         pop     xDI
         pop     xCX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSaveEx.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSaveEx.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSaveEx.asm	(revision 60657)
@@ -0,0 +1,336 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3RegCtxSaveEx.
+;
+
+;
+; 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"
+
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_EXTERN_DATA16   g_bBs3CurrentMode
+
+TMPL_BEGIN_TEXT
+BS3_EXTERN_CMN      Bs3Panic
+BS3_EXTERN_CMN      Bs3RegCtxSave
+BS3_EXTERN_CMN      Bs3SwitchTo16Bit
+%if TMPL_BITS != 64
+BS3_EXTERN_CMN      Bs3SwitchTo16BitV86
+%endif
+%if TMPL_BITS != 32
+BS3_EXTERN_CMN      Bs3SwitchTo32Bit
+%endif
+%if TMPL_BITS != 64
+BS3_EXTERN_CMN      Bs3SwitchTo64Bit
+%endif
+%if TMPL_BITS == 16
+BS3_EXTERN_CMN      Bs3SelRealModeDataToProtFar16
+BS3_EXTERN_CMN      Bs3SelProtFar16DataToRealMode
+BS3_EXTERN_CMN      Bs3SelRealModeDataToFlat
+BS3_EXTERN_CMN      Bs3SelProtFar16DataToFlat
+%else
+BS3_EXTERN_CMN      Bs3SelFlatDataToProtFar16
+%endif
+%if TMPL_BITS == 32
+BS3_EXTERN_CMN      Bs3SelFlatDataToRealMode
+%endif
+
+BS3_BEGIN_TEXT16
+%if TMPL_BITS != 16
+extern              _Bs3RegCtxSave_c16
+extern              _Bs3SwitchTo%[TMPL_BITS]Bit_c16
+%endif
+
+BS3_BEGIN_TEXT32
+%if TMPL_BITS != 32
+extern              _Bs3RegCtxSave_c32
+extern              _Bs3SwitchTo%[TMPL_BITS]Bit_c32
+%endif
+%if TMPL_BITS == 16
+extern              _Bs3SwitchTo16BitV86_c32
+%endif
+
+BS3_BEGIN_TEXT64
+%if TMPL_BITS != 64
+extern              _Bs3RegCtxSave_c64
+%endif
+
+TMPL_BEGIN_TEXT
+
+
+
+;;
+; Saves the current register context.
+;
+; @param        pRegCtx
+; @param        bBitMode     (8)
+; @param        cbExtraStack (16)
+; @uses         xAX, xDX, xCX
+;
+BS3_PROC_BEGIN_CMN Bs3RegCtxSaveEx, BS3_PBC_NEAR        ; Far stub generated by the makefile/bs3kit.h.
+TONLY16 CPU 8086
+        BS3_CALL_CONV_PROLOG 3
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; Get the CPU bitcount part of the current mode.
+        ;
+        mov     dl, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
+        and     dl, BS3_MODE_CODE_MASK
+%if TMPL_BITS == 16
+        push    dx                          ; xBP - xCB*1: save space for previous CPU mode (16-bit)
+%endif
+
+        ;
+        ; Reserve extra stack space.  Make sure we've got 20h here in case we
+        ; are saving a 64-bit context.
+        ;
+        mov     ax, [xBP + xCB + cbCurRetAddr + sCB + xCB]
+%ifdef BS3_STRICT
+        cmp     ax, 4096
+        jb      .extra_stack_ok
+        call    Bs3Panic
+.extra_stack_ok:
+%endif
+        cmp     ax, 20h
+        jae     .at_least_20h_extra_stack
+        add     ax, 20h
+.at_least_20h_extra_stack:
+        sub     sp, ax
+
+        ;
+        ; Are we just saving the mode we're already in?
+        ;
+        mov     al, [xBP + xCB + cbCurRetAddr + sCB]
+        and     al, BS3_MODE_CODE_MASK
+        cmp     dl, al
+        jne     .not_the_same_mode
+
+%if TMPL_BITS == 16
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        push    word [xBP + xCB + cbCurRetAddr]
+%elif TMPL_BITS == 32
+        push    dword [xBP + xCB + cbCurRetAddr]
+%endif
+        call    Bs3RegCtxSave               ; 64-bit: rcx is untouched thus far.
+
+
+        ;
+        ; Return - no need to pop xAX and xDX as the last two
+        ;          operations preserves all registers.
+        ;
+.return:
+        mov     xSP, xBP
+        pop     xBP
+        BS3_CALL_CONV_EPILOG 3
+        BS3_HYBRID_RET
+
+
+        ;
+        ; Turns out we have to do switch to a different bitcount before saving.
+        ;
+.not_the_same_mode:
+        cmp     al, BS3_MODE_CODE_16
+        je      .code_16
+
+TONLY16 CPU 386
+%if TMPL_BITS != 32
+        cmp     al, BS3_MODE_CODE_32
+        je      .code_32
+%endif
+%if TMPL_BITS != 64
+        cmp     al, BS3_MODE_CODE_V86
+        je      .code_v86
+        cmp     al, BS3_MODE_CODE_64
+        jne     .bad_input_mode
+        jmp     .code_64
+%endif
+
+        ; Bad input (al=input, dl=current).
+.bad_input_mode:
+        call    Bs3Panic
+
+
+        ;
+        ; Save a 16-bit context.
+        ;
+        ; Convert pRegCtx to 16:16 protected mode and make sure we're in the
+        ; 16-bit code segment.
+        ;
+.code_16:
+%if TMPL_BITS == 16
+ %ifdef BS3_STRICT
+        cmp     dl, BS3_MODE_CODE_V86
+        jne     .bad_input_mode
+ %endif
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        push    word [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelRealModeDataToProtFar16
+        add     sp, 4h
+        push    dx                          ; Parameter #0 for _Bs3RegCtxSave_c16
+        push    ax
+%else
+ %if TMPL_BITS == 32
+        push    dword [xBP + xCB + cbCurRetAddr]
+ %endif
+        call    Bs3SelFlatDataToProtFar16   ; 64-bit: BS3_CALL not needed, ecx not touched thus far.
+        mov     [xSP], eax                  ; Parameter #0 for _Bs3RegCtxSave_c16
+        jmp     .code_16_safe_segment
+        BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.code_16_safe_segment:
+%endif
+        call    Bs3SwitchTo16Bit
+        BS3_SET_BITS 16
+
+        call    _Bs3RegCtxSave_c16
+
+%if TMPL_BITS == 16
+        call    _Bs3SwitchTo16BitV86_c16
+%else
+        call    _Bs3SwitchTo%[TMPL_BITS]Bit_c16
+%endif
+        BS3_SET_BITS TMPL_BITS
+        jmp     .return
+        TMPL_BEGIN_TEXT
+
+TONLY16 CPU 386
+
+
+%if TMPL_BITS != 64
+        ;
+        ; Save a v8086 context.
+        ;
+.code_v86:
+ %if TMPL_BITS == 16
+  %ifdef BS3_STRICT
+        cmp     dl, BS3_MODE_CODE_16
+        jne     .bad_input_mode
+  %endif
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        push    word [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelProtFar16DataToRealMode
+        add     sp, 4h
+        push    dx                          ; Parameter #0 for _Bs3RegCtxSave_c16
+        push    ax
+ %else
+        push    dword [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelFlatDataToRealMode
+        mov     [xSP], eax                  ; Parameter #0 for _Bs3RegCtxSave_c16
+        jmp     .code_v86_safe_segment
+        BS3_BEGIN_TEXT16
+        BS3_SET_BITS TMPL_BITS
+.code_v86_safe_segment:
+ %endif
+        call    Bs3SwitchTo16BitV86
+        BS3_SET_BITS 16
+
+        call    _Bs3RegCtxSave_c16
+
+        call    _Bs3SwitchTo%[TMPL_BITS]Bit_c16
+        BS3_SET_BITS TMPL_BITS
+        jmp     .return
+TMPL_BEGIN_TEXT
+%endif
+
+
+%if TMPL_BITS != 32
+        ;
+        ; Save a 32-bit context.
+        ;
+.code_32:
+ %if TMPL_BITS == 16
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        push    word [xBP + xCB + cbCurRetAddr]
+        test    dl, BS3_MODE_CODE_V86
+        jnz     .code_32_from_v86
+        call    Bs3SelProtFar16DataToFlat
+        jmp     .code_32_flat_ptr
+.code_32_from_v86:
+        call    Bs3SelRealModeDataToFlat
+.code_32_flat_ptr:
+        add     sp, 4h
+        push    dx                          ; Parameter #0 for _Bs3RegCtxSave_c32
+        push    ax
+ %else
+        mov     [rsp], ecx                  ; Parameter #0 for _Bs3RegCtxSave_c16
+ %endif
+        call    Bs3SwitchTo32Bit
+        BS3_SET_BITS 32
+
+        call    _Bs3RegCtxSave_c32
+
+ %if TMPL_BITS == 16
+        cmp     byte [xBP - xCB*1], BS3_MODE_CODE_16
+        jne     .code_32_back_to_v86
+        call    _Bs3SwitchTo16Bit_c32
+        BS3_SET_BITS TMPL_BITS
+        jmp     .return
+.code_32_back_to_v86:
+        BS3_SET_BITS 32
+        call    _Bs3SwitchTo16BitV86_c32
+ %else
+        call    _Bs3SwitchTo64Bit_c32
+ %endif
+        BS3_SET_BITS TMPL_BITS
+        jmp     .return
+%endif
+
+
+%if TMPL_BITS != 64
+        ;
+        ; Save a 64-bit context.
+        ;
+        CPU     x86-64
+.code_64:
+ %if TMPL_BITS == 16
+  %ifdef BS3_STRICT
+        cmp     dl, BS3_MODE_CODE_16
+        jne     .bad_input_mode
+  %endif
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        push    word [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelProtFar16DataToFlat
+        add     sp, 4h
+        mov     cx, dx                      ; Parameter #0 for _Bs3RegCtxSave_c64
+        shl     ecx, 16
+        mov     cx, ax
+ %else
+        mov     ecx, [xBP + xCB + cbCurRetAddr] ; Parameter #0 for _Bs3RegCtxSave_c64
+ %endif
+        call    Bs3SwitchTo64Bit            ; (preserves all 32-bit GPRs)
+        BS3_SET_BITS 64
+
+        call    _Bs3RegCtxSave_c64          ; No BS3_CALL as rcx is already ready.
+
+        call    _Bs3SwitchTo16Bit_c32
+        BS3_SET_BITS 16
+        jmp     .return
+%endif
+BS3_PROC_END_CMN   Bs3RegCtxSaveEx
+
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFar32ToFlat32NoClobber.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFar32ToFlat32NoClobber.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFar32ToFlat32NoClobber.asm	(revision 60657)
@@ -0,0 +1,104 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelFar32ToFlat32NoClobber.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_EXTERN_CMN Bs3SelFar32ToFlat32
+TMPL_BEGIN_TEXT
+
+
+;;
+; Wrapper around the Bs3SelFar32ToFlat32 C function that doesn't
+; clobber any registers nor require 20h stack scratch area (64-bit).
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch space required in 64-bit mode.
+;
+BS3_PROC_BEGIN_CMN Bs3SelFar32ToFlat32NoClobber, BS3_PBC_NEAR      ; Far stub generated by the makefile.
+        push    xBP
+        mov     xBP, xSP
+
+%if TMPL_BITS == 16
+        push    bx
+        push    cx
+        push    es
+
+        push    word [xBP + xCB + cbCurRetAddr + 4] ; uSel
+        push    word [xBP + xCB + cbCurRetAddr + 2] ; high off
+        push    word [xBP + xCB + cbCurRetAddr]     ; low off
+        call    Bs3SelFar32ToFlat32
+        add     sp, 6
+
+        pop     es
+        pop     cx
+        pop     bx
+%else
+        push    xDX
+        push    xCX
+ %if TMPL_BITS == 32
+        push    es
+        push    fs
+        push    gs
+
+        push    dword [xBP + xCB + cbCurRetAddr + 4] ; uSel
+        push    dword [xBP + xCB + cbCurRetAddr]     ; off
+        call    Bs3SelFar32ToFlat32
+        add     esp, 8
+
+        pop     gs
+        pop     fs
+        pop     es
+ %else
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        sub     rsp, 20h
+
+        call    Bs3SelFar32ToFlat32     ; Just pass ECX and DX along as-is.
+
+        add     rsp, 20h
+        pop     r11
+        pop     r10
+        pop     r9
+        pop     r8
+ %endif
+        pop     xCX
+        pop     xDX
+%endif
+
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3SelFar32ToFlat32NoClobber
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToProtFar16.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToProtFar16.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToProtFar16.asm	(revision 60657)
@@ -0,0 +1,101 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelFlatDataToProtFar16.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+%ifdef BS3_STRICT
+BS3_EXTERN_CMN Bs3Panic
+%endif
+
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatDataToProtFar16,(uint32_t uFlatAddr));
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelFlatDataToProtFar16, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; Just translate the address to tiled.
+        ;
+%if TMPL_BITS == 16
+        ; Convert upper 16-bit to a tiled selector.
+        mov     ax, cx                  ; save cx
+        mov     dx, [xBP + xCB + cbCurRetAddr + 2]
+ %ifdef BS3_STRICT
+        cmp     dx, BS3_SEL_TILED_AREA_SIZE >> 16
+        jb      .address_ok
+        call    Bs3Panic
+.address_ok:
+ %endif
+        mov     cl, X86_SEL_SHIFT
+        shl     dx, cl
+        add     dx, BS3_SEL_TILED
+        mov     cx, ax                  ; restore cx
+
+        ; Load segment offset and return.
+        mov     ax, [xBP + xCB + cbCurRetAddr]
+
+%else
+        ; Convert upper 16-bit to tiled selector.
+ %if TMPL_BITS == 32
+        mov     eax, [xBP + xCB + cbCurRetAddr]
+ %else
+        mov     rax, rcx
+ %endif
+ %ifdef BS3_STRICT
+        cmp     xAX, BS3_SEL_TILED_AREA_SIZE
+        jb      .address_ok
+        call    Bs3Panic
+.address_ok:
+ %endif
+        ror     eax, 16
+        shl     ax, X86_SEL_SHIFT
+        add     ax, BS3_SEL_TILED
+        rol     eax, 16
+%endif
+
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3SelFlatDataToProtFar16
+
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToRealMode.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToRealMode.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelFlatDataToRealMode.asm	(revision 60657)
@@ -0,0 +1,94 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelFlatDataToRealMode
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+%ifdef BS3_STRICT
+BS3_EXTERN_CMN Bs3Panic
+%endif
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatDataToRealMode,(uint32_t uFlatAddr));
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelFlatDataToRealMode, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; Take the simplest approach possible (64KB tiled).
+        ;
+%if TMPL_BITS == 16
+        mov     ax, cx                  ; save cx
+        mov     dx, [xBP + xCB + cbCurRetAddr + 2]
+ %ifdef BS3_STRICT
+        cmp     dx, _1M >> 16
+        jb      .address_ok
+        call    Bs3Panic
+.address_ok:
+ %endif
+        mov     cl, 12
+        shl     dx, cl
+        mov     ax, cx                  ; restore cx
+
+        mov     ax, [xBP + xCB + cbCurRetAddr]
+
+%else
+ %if TMPL_BITS == 32
+        mov     eax, [xBP + xCB + cbCurRetAddr]
+ %else
+        mov     rax, rcx
+ %endif
+ %ifdef BS3_STRICT
+        cmp     xAX, _1M
+        jb      .address_ok
+        call    Bs3Panic
+.address_ok:
+ %endif
+        ror     eax, 16
+        shl     ax, 12
+        rol     eax, 16
+%endif
+
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3SelFlatDataToRealMode
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToFlat.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToFlat.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToFlat.asm	(revision 60657)
@@ -0,0 +1,90 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelProtFar16DataToFlat.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_EXTERN_CMN Bs3SelFar32ToFlat32NoClobber
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelProtFar16DataToFlat,(uint32_t uFar1616));
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelProtFar16DataToFlat, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; Just call Bs3SelFar32ToFlat32NoClobber to do the job.
+        ;
+%if TMPL_BITS == 16
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        xor     ax, ax
+        push    ax
+        push    word [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelFar32ToFlat32NoClobber
+        add     sp, 6
+%else
+ %if TMPL_BITS == 32
+        movzx   eax, word [xBP + xCB + cbCurRetAddr + 2]
+        push    eax
+        movzx   eax, word [xBP + xCB + cbCurRetAddr]
+        push    eax
+        call    Bs3SelFar32ToFlat32NoClobber
+        add     esp, 8
+ %else
+        push    xDX
+        push    xCX
+
+        mov     edx, ecx                ; arg #2: selector
+        shr     edx, 16
+        movzx   ecx, cx                 ; arg #1: offset
+        call    Bs3SelFar32ToFlat32NoClobber
+
+        pop     xDX
+        pop     xCX
+ %endif
+%endif
+
+.return:
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3SelProtFar16DataToFlat
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToRealMode.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToRealMode.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelProtFar16DataToRealMode.asm	(revision 60657)
@@ -0,0 +1,147 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelProtFar16DataToRealMode.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_BEGIN_DATA16                        ; For real mode segment value.
+BS3_BEGIN_SYSTEM16                      ; Ditto.
+TMPL_BEGIN_TEXT
+BS3_EXTERN_CMN Bs3SelFar32ToFlat32NoClobber
+
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelProtFar16DataToRealMode,(uint32_t uFar1616));
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelProtFar16DataToRealMode, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; See if it's our default 16-bit ring-0 data, stack or system data segment.
+        ;
+%if TMPL_BITS == 16
+        mov     ax, [xBP + xCB + cbCurRetAddr + 2]
+%elif TMPL_BITS == 32
+        movzx   eax, word [xBP + xCB + cbCurRetAddr + 2]
+%else
+        mov     eax, ecx
+        shr     eax, 16
+%endif
+        cmp     ax, BS3_SEL_R0_SS16
+        jne     .not_stack
+        mov     ax, 0
+
+.quick_return:
+%if TMPL_BITS == 16
+        mov     dx, ax
+        mov     ax, [xBP + xCB + cbCurRetAddr]
+%elif TMPL_BITS == 32
+        shl     eax, 16
+        mov     ax, word [xBP + xCB + cbCurRetAddr]
+%else
+        shl     eax, 16
+        mov     ax, cx
+%endif
+
+.return:
+        pop     xBP
+        BS3_HYBRID_RET
+
+.not_stack:
+        cmp     ax, BS3_SEL_R0_DS16
+        jne     .not_dgroup
+        mov     ax, BS3KIT_GRPNM_DATA16
+        jmp     .quick_return
+
+.not_dgroup:
+        cmp     ax, BS3_SEL_SYSTEM16
+        jne     .not_system16
+        mov     ax, BS3SYSTEM16
+        jmp     .quick_return
+
+        ;
+        ; Call worker function to convert it to flat and the do tiled
+        ; calculation from that.
+        ;
+.not_system16:
+%if TMPL_BITS == 16
+        push    word [xBP + xCB + cbCurRetAddr + 2]
+        xor     ax, ax
+        push    ax
+        push    word [xBP + xCB + cbCurRetAddr]
+        call    Bs3SelFar32ToFlat32NoClobber
+        add     sp, 6
+
+        ; Convert upper 16-bit of the flat address to a tiled selector.
+        push    cx
+        mov     cl, X86_SEL_SHIFT
+        shl     dx, cl
+        add     dx, BS3_SEL_TILED
+        pop     cx
+%else
+ %if TMPL_BITS == 32
+        push    eax
+        movzx   eax, word [xBP + xCB + cbCurRetAddr]
+        push    eax
+        call    Bs3SelFar32ToFlat32NoClobber
+        add     esp, 8
+ %else
+        push    xDX
+        push    xCX
+
+        mov     edx, eax                ; arg #2: selector
+        movzx   ecx, cx                 ; arg #1: offset
+        call    Bs3SelFar32ToFlat32NoClobber
+
+        pop     xDX
+        pop     xCX
+ %endif
+
+        ; Convert upper 16-bit to tiled selector.
+        rol     eax, 16
+        shl     ax, X86_SEL_SHIFT
+        add     ax, BS3_SEL_TILED
+        ror     eax, 16
+%endif
+        jmp     .return
+BS3_PROC_END_CMN   Bs3SelProtFar16DataToRealMode
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToFlat.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToFlat.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToFlat.asm	(revision 60657)
@@ -0,0 +1,89 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelRealModeDataToFlat.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelRealModeDataToFlat,(uint32_t uFar1616));
+;
+; @uses     Only return registers (ax:dx, eax, eax);
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelRealModeDataToFlat, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ; Calc flat address.
+%if TMPL_BITS == 16
+        push    cx
+        mov     dx, [xBP + xCB + cbCurRetAddr + 2]
+        mov     ax, dx
+        mov     cl, 12
+        shr     dx, cl
+        mov     cl, 4
+        shl     ax, cl
+        add     ax, [xBP + xCB + cbCurRetAddr]
+        adc     dx, 0
+        pop     cx
+
+%elif TMPL_BITS == 32
+        movzx   eax, word [xBP + xCB + cbCurRetAddr + 2]
+        shl     eax, 4
+        add     ax, [xBP + xCB + cbCurRetAddr]
+        jnc     .return
+        add     eax, 10000h
+
+%elif TMPL_BITS == 64
+        mov     eax, ecx
+        shr     eax, 16
+        shl     eax, 4
+        add     ax, cx
+        jnc     .return
+        add     eax, 10000h
+
+%else
+ %error "TMPL_BITS!"
+%endif
+
+.return:
+        pop     xBP
+        BS3_HYBRID_RET
+BS3_PROC_END_CMN   Bs3SelRealModeDataToFlat
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToProtFar16.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToProtFar16.asm	(revision 60657)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SelRealModeDataToProtFar16.asm	(revision 60657)
@@ -0,0 +1,141 @@
+; $Id$
+;; @file
+; BS3Kit - Bs3SelRealModeDataToProtFar16.
+;
+
+;
+; 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.mac"
+
+
+;*********************************************************************************************************************************
+;*  External Symbols                                                                                                             *
+;*********************************************************************************************************************************
+BS3_BEGIN_DATA16                        ; For real mode segment value.
+BS3_BEGIN_SYSTEM16                      ; Ditto.
+
+TMPL_BEGIN_TEXT
+%if TMPL_BITS == 16
+CPU 8086
+%endif
+
+
+;;
+; @cproto   BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelRealModeDataToProtFar16,(uint32_t uFar1616));
+;
+; @uses     Only return registers (ax:dx, eax, eax)
+; @remarks  No 20h scratch area requirements.
+;
+BS3_PROC_BEGIN_CMN Bs3SelRealModeDataToProtFar16, BS3_PBC_NEAR      ; Far stub generated by the makefile/bs3kit.h.
+        push    xBP
+        mov     xBP, xSP
+
+        ;
+        ; See if it's our default 16-bit data, stack or system data segment.
+        ;
+%if TMPL_BITS == 16
+        mov     ax, [xBP + xCB + cbCurRetAddr + 2]
+%elif TMPL_BITS == 32
+        movzx   eax, word [xBP + xCB + cbCurRetAddr + 2]
+%else
+        mov     eax, ecx
+        shr     eax, 16
+%endif
+        cmp     ax, 0
+        jnz     .not_stack
+        mov     ax, BS3_SEL_R0_SS16
+
+.quick_return:
+%if TMPL_BITS == 16
+        mov     dx, ax
+        mov     ax, [xBP + xCB + cbCurRetAddr]
+%elif TMPL_BITS == 32
+        shl     eax, 16
+        mov     ax, word [xBP + xCB + cbCurRetAddr]
+%else
+        shl     eax, 16
+        mov     ax, cx
+%endif
+
+.return:
+        pop     xBP
+        BS3_HYBRID_RET
+
+.not_stack:
+        cmp     ax, BS3KIT_GRPNM_DATA16
+        jne     .not_dgroup
+        mov     ax, BS3_SEL_R0_DS16
+        jmp     .quick_return
+
+.not_dgroup:
+        cmp     ax, BS3SYSTEM16
+        jne     .not_system16
+        mov     ax, BS3_SEL_SYSTEM16
+        jmp     .quick_return
+
+        ;
+        ; Compute flat address and translate it to tiled.
+        ;
+.not_system16:
+%if TMPL_BITS == 16
+        push    cx
+
+        ; Calc flat address.
+        mov     dx, ax
+        mov     cl, 12
+        shr     dx, cl
+        mov     cl, 4
+        shl     ax, cl
+        add     ax, [xBP + xCB + cbCurRetAddr]
+        adc     dx, 0
+
+        ; Convert upper 16-bit to tiled selector.
+        mov     cl, X86_SEL_SHIFT
+        shl     dx, cl
+        add     dx, BS3_SEL_TILED
+
+        pop     cx
+%else
+        ; Calc flat address.
+        shl     eax, 4
+ %if TMPL_BITS == 32
+        add     ax, [xBP + xCB + cbCurRetAddr]
+ %else
+        add     ax, cx
+ %endif
+        jnc     .no_carry
+        add     eax, 10000h
+.no_carry:
+
+        ; Convert upper 16-bit to tiled selector.
+        rol     eax, 16
+        shl     ax, X86_SEL_SHIFT
+        add     ax, BS3_SEL_TILED
+        ror     eax, 16
+%endif
+        jmp     .return
+BS3_PROC_END_CMN   Bs3SelRealModeDataToProtFar16
+
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrFormatV.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrFormatV.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrFormatV.c	(revision 60657)
@@ -436,4 +436,5 @@
             }
             State.fFlags |= STR_F_WIDTH;
+            ch = *pszFormat++;
         }
 
@@ -442,20 +443,25 @@
          */
         State.cchPrecision = 0;
-        if (RT_C_IS_DIGIT(ch))
-        {
-            do
-            {
-                State.cchPrecision *= 10;
-                State.cchPrecision  = ch - '0';
+        if (ch == '.')
+        {
+            ch = *pszFormat++;
+            if (RT_C_IS_DIGIT(ch))
+            {
+                do
+                {
+                    State.cchPrecision *= 10;
+                    State.cchPrecision  = ch - '0';
+                    ch = *pszFormat++;
+                } while (RT_C_IS_DIGIT(ch));
+                State.fFlags |= STR_F_PRECISION;
+            }
+            else if (ch == '*')
+            {
+                State.cchPrecision = va_arg(va, int);
+                if (State.cchPrecision < 0)
+                    State.cchPrecision = 0;
+                State.fFlags |= STR_F_PRECISION;
                 ch = *pszFormat++;
-            } while (RT_C_IS_DIGIT(ch));
-            State.fFlags |= STR_F_PRECISION;
-        }
-        else if (ch == '*')
-        {
-            State.cchPrecision = va_arg(va, int);
-            if (State.cchPrecision < 0)
-                State.cchPrecision = 0;
-            State.fFlags |= STR_F_PRECISION;
+            }
         }
 
@@ -651,4 +657,28 @@
                         State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
                         State.uBase   = 16;
+                        break;
+                    case 'h':
+                        ch = *pszFormat++;
+                        if (ch == 'x')
+                        {
+                            /* Hex dumping. */
+                            uint8_t const BS3_FAR *pbHex = va_arg(va, uint8_t const BS3_FAR *);
+                            if (State.cchPrecision < 0)
+                                State.cchPrecision = 16;
+                            ch = *pszFormat++;
+                            if (ch == 's' || ch == 'd')
+                            {
+                                /* %Rhxd is currently implemented as %Rhxs. */
+                                while (State.cchPrecision-- > 0)
+                                {
+                                    uint8_t b = *pbHex++;
+                                    State.pfnOutput(g_achBs3HexDigits[b >> 4], State.pvUser);
+                                    State.pfnOutput(g_achBs3HexDigits[b & 0x0f], State.pvUser);
+                                    if (State.cchPrecision)
+                                        State.pfnOutput(' ', State.pvUser);
+                                }
+                            }
+                        }
+                        State.uBase   = 0;
                         break;
                     default:
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16Bit.asm	(revision 60657)
@@ -103,9 +103,9 @@
 
         popfd
-        TMPL_ONLY_64BIT_STMT pop     ebx
+TONLY64 pop     ebx
         pop     ebx
-        TMPL_ONLY_64BIT_STMT pop     eax
+TONLY64 pop     eax
         pop     eax
-        TMPL_ONLY_64BIT_STMT add     sp, 4
+TONLY64 add     sp, 4
         ret     (TMPL_BITS - 16) / 8    ; Return and pop 2 or 6 bytes of "parameters" (unused return value)
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16BitV86.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16BitV86.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchTo16BitV86.asm	(revision 60657)
@@ -42,5 +42,5 @@
 BS3_PROC_BEGIN_CMN Bs3SwitchTo16BitV86, BS3_PBC_NEAR
         ; Construct basic v8086 return frame.
-        BS3_ONLY_16BIT_STMT movzx   esp, sp
+BONLY16 movzx   esp, sp
         push    dword 0                                 ; +0x20: GS
         push    dword 0                                 ; +0x1c: FS
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing0.asm	(revision 60657)
@@ -55,5 +55,5 @@
 %else
         push    0
-        TMPL_ONLY_16BIT_STMT push cs
+TONLY16 push    cs
         call    Bs3SwitchToRingX
         add     xSP, xCB
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing1.asm	(revision 60657)
@@ -55,5 +55,5 @@
 %else
         push    1
-        TMPL_ONLY_16BIT_STMT push cs
+TONLY16 push    cs
         call    Bs3SwitchToRingX
         add     xSP, xCB
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing2.asm	(revision 60657)
@@ -55,5 +55,5 @@
 %else
         push    2
-        TMPL_ONLY_16BIT_STMT push cs
+TONLY16 push    cs
         call    Bs3SwitchToRingX
         add     xSP, xCB
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-SwitchToRing3.asm	(revision 60657)
@@ -55,5 +55,5 @@
 %else
         push    3
-        TMPL_ONLY_16BIT_STMT push cs
+TONLY16 push    cs
         call    Bs3SwitchToRingX
         add     xSP, xCB
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestCheckRegCtxEx.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestCheckRegCtxEx.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestCheckRegCtxEx.c	(revision 60657)
@@ -36,5 +36,6 @@
                                         uint32_t fExtraEfl, const char *pszMode, uint16_t idTestStep))
 {
-    uint16_t cErrorsBefore = Bs3TestSubErrorCount();
+    uint16_t const cErrorsBefore = Bs3TestSubErrorCount();
+    uint8_t  const fbFlags       = pActualCtx->fbFlags | pExpectedCtx->fbFlags;
 
 #define CHECK_MEMBER(a_szName, a_szFmt, a_Actual, a_Expected) \
@@ -52,6 +53,5 @@
     CHECK_MEMBER("rsi",     "%08RX64",  pActualCtx->rsi.u,    pExpectedCtx->rsi.u);
     CHECK_MEMBER("rdi",     "%08RX64",  pActualCtx->rdi.u,    pExpectedCtx->rdi.u);
-    if (   !(pActualCtx->fbFlags & BS3REG_CTX_F_NO_AMD64)
-        && !(pExpectedCtx->fbFlags & BS3REG_CTX_F_NO_AMD64) )
+    if (!(fbFlags & BS3REG_CTX_F_NO_AMD64))
     {
         CHECK_MEMBER("r8",      "%08RX64",  pActualCtx->r8.u,     pExpectedCtx->r8.u);
@@ -71,15 +71,24 @@
     CHECK_MEMBER("fs",      "%04RX16",  pActualCtx->fs,       pExpectedCtx->fs);
     CHECK_MEMBER("gs",      "%04RX16",  pActualCtx->gs,       pExpectedCtx->gs);
-    CHECK_MEMBER("tr",      "%04RX16",  pActualCtx->tr,       pExpectedCtx->tr);
-    CHECK_MEMBER("ldtr",    "%04RX16",  pActualCtx->ldtr,     pExpectedCtx->ldtr);
+
+    if (!(fbFlags & BS3REG_CTX_F_NO_TR_LDTR))
+    {
+        CHECK_MEMBER("tr",      "%04RX16",  pActualCtx->tr,       pExpectedCtx->tr);
+        CHECK_MEMBER("ldtr",    "%04RX16",  pActualCtx->ldtr,     pExpectedCtx->ldtr);
+    }
     CHECK_MEMBER("bMode",   "%#04x",    pActualCtx->bMode,    pExpectedCtx->bMode);
     CHECK_MEMBER("bCpl",    "%u",       pActualCtx->bCpl,     pExpectedCtx->bCpl);
-    if (!(pActualCtx->fbFlags & BS3REG_CTX_F_NO_CR))
+
+    if (!(fbFlags & BS3REG_CTX_F_NO_CR0_IS_MSW))
+        CHECK_MEMBER("cr0", "%08RX64",  pActualCtx->cr0.u,    pExpectedCtx->cr0.u);
+    else
+        CHECK_MEMBER("msw", "%08RX16",  pActualCtx->cr0.u16,  pExpectedCtx->cr0.u16);
+    if (!(fbFlags & BS3REG_CTX_F_NO_CR2_CR3))
     {
-        CHECK_MEMBER("cr0", "%08RX64",  pActualCtx->cr0.u,    pExpectedCtx->cr0.u);
         CHECK_MEMBER("cr2", "%08RX64",  pActualCtx->cr2.u,    pExpectedCtx->cr2.u);
         CHECK_MEMBER("cr3", "%08RX64",  pActualCtx->cr3.u,    pExpectedCtx->cr3.u);
+    }
+    if (!(fbFlags & BS3REG_CTX_F_NO_CR4))
         CHECK_MEMBER("cr4", "%08RX64",  pActualCtx->cr4.u,    pExpectedCtx->cr4.u);
-    }
 #undef CHECK_MEMBER
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TestSendCmdWithStr.asm	(revision 60657)
@@ -41,5 +41,5 @@
         push    xDX
         push    xSI
-        BS3_ONLY_16BIT_STMT push ds
+BONLY16 push    ds
 
         cmp     byte [BS3_DATA16_WRT(g_fbBs3VMMDevTesting)], 0
@@ -70,5 +70,5 @@
 
 .no_vmmdev:
-        BS3_ONLY_16BIT_STMT pop ds
+BONLY16 pop     ds
         pop     xSI
         pop     xDX
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapDefaultHandler.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapDefaultHandler.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapDefaultHandler.c	(revision 60657)
@@ -29,4 +29,8 @@
 *********************************************************************************************************************************/
 #include "bs3kit-template-header.h"
+#if TMPL_BITS != 64
+# include <VBox/VMMDevTesting.h>
+# include <iprt/asm-amd64-x86.h>
+#endif
 
 
@@ -148,4 +152,30 @@
             else if (bOpCode == 0xfb)
                 pTrapFrame->Ctx.rflags.u16 |= X86_EFL_IF;
+            /* OUT: byte I/O to VMMDev. */
+            else if (   bOpCode == 0xee
+                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
+                ASMOutU8(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u8);
+            /* OUT: [d]word I/O to VMMDev. */
+            else if (   bOpCode == 0xef
+                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
+            {
+                if (cBitsOpcode != 32)
+                    ASMOutU16(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u16);
+                else
+                    ASMOutU32(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u32);
+            }
+            /* IN: byte I/O to VMMDev. */
+            else if (   bOpCode == 0xec
+                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
+                pTrapFrame->Ctx.rax.u8 = ASMInU8(pTrapFrame->Ctx.rdx.u16);
+            /* IN: [d]word I/O to VMMDev. */
+            else if (   bOpCode == 0xed
+                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
+            {
+                if (cBitsOpcode != 32)
+                    pTrapFrame->Ctx.rax.u16 = ASMInU16(pTrapFrame->Ctx.rdx.u16);
+                else
+                    pTrapFrame->Ctx.rax.u32 = ASMInU32(pTrapFrame->Ctx.rdx.u32);
+            }
             /* Unexpected. */
             else
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapPrintFrame.c
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapPrintFrame.c	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapPrintFrame.c	(revision 60657)
@@ -35,9 +35,11 @@
 {
 #if 1
-    Bs3TestPrintf("Trap %#04x errcd=%#06RX64 at %04x:%016RX64 - test step %d (%#x)\n",
+    Bs3TestPrintf("Trap %#04x errcd=%#06RX64 at %04x:%016RX64 (by %04x/%04x) - test step %d (%#x)\n",
                   pTrapFrame->bXcpt,
                   pTrapFrame->uErrCd,
                   pTrapFrame->Ctx.cs,
                   pTrapFrame->Ctx.rip.u64,
+                  pTrapFrame->uHandlerCs,
+                  pTrapFrame->uHandlerSs,
                   g_usBs3TestStep, g_usBs3TestStep);
     Bs3RegCtxPrint(&pTrapFrame->Ctx);
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapSetJmp.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapSetJmp.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapSetJmp.asm	(revision 60657)
@@ -54,10 +54,10 @@
         mov     xBP, xSP
         push    xBX
-        BS3_ONLY_64BIT_STMT sub     xSP, 20h
+BONLY64 sub     xSP, 20h
 
         ;
         ; Save the current register context.
         ;
-        BS3_ONLY_16BIT_STMT push    ds
+BONLY16 push    ds
         BS3_LEA_MOV_WRT_RIP(xAX, BS3_DATA16_WRT(g_Bs3TrapSetJmpCtx))
         push    xAX
@@ -125,5 +125,5 @@
         ;
         mov     xAX, 1
-        BS3_ONLY_64BIT_STMT add     xSP, 20h
+BONLY64 add     xSP, 20h
         pop     xBX
         pop     xBP
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForLM64.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForLM64.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForLM64.asm	(revision 60657)
@@ -71,5 +71,5 @@
         push    xBP
         mov     xBP, xSP
-        BS3_ONLY_16BIT_STMT push    es
+BONLY16 push    es
         push    sDX
         push    sCX
@@ -102,7 +102,7 @@
         ; Not a problematic addressing mode.
         ;
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
         BS3_CALL Bs3PagingInitRootForLM, 0
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
 %endif
 
@@ -121,5 +121,5 @@
         pop     sCX
         pop     sDX
-        BS3_ONLY_16BIT_STMT pop     es
+BONLY16 pop     es
         leave
 %ifdef BS3_STRICT
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPAE32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPAE32.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPAE32.asm	(revision 60657)
@@ -60,5 +60,5 @@
         push    xBP
         mov     xBP, xSP
-        BS3_ONLY_16BIT_STMT push    es
+BONLY16 push    es
         push    sDX
         push    sCX
@@ -92,7 +92,7 @@
         ; Not a problematic addressing mode.
         ;
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
         BS3_CALL Bs3PagingInitRootForPAE, 0
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
 %endif
 
@@ -111,5 +111,5 @@
         pop     sCX
         pop     sDX
-        BS3_ONLY_16BIT_STMT pop     es
+BONLY16 pop     es
         leave
         ret
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPP32.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPP32.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-PagingGetRootForPP32.asm	(revision 60657)
@@ -71,5 +71,5 @@
         push    xBP
         mov     xBP, xSP
-        BS3_ONLY_16BIT_STMT push    es
+BONLY16 push    es
         push    sDX
         push    sCX
@@ -103,7 +103,7 @@
         ; Not a problematic addressing mode.
         ;
-        BS3_ONLY_64BIT_STMT sub     rsp, 20h
+BONLY64 sub     rsp, 20h
         BS3_CALL Bs3PagingInitRootForPP, 0
-        BS3_ONLY_64BIT_STMT add     rsp, 20h
+BONLY64 add     rsp, 20h
 %endif
 
@@ -122,5 +122,5 @@
         pop     sCX
         pop     sDX
-        BS3_ONLY_16BIT_STMT pop     es
+BONLY16 pop     es
         leave
 %ifdef BS3_STRICT
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToRM.asm	(revision 60657)
@@ -181,5 +181,4 @@
         and     ax, ~X86_CR0_PE
         mov     cr0, eax
-hlt
         jmp     BS3_SEL_TEXT16:.resume
  %endif
@@ -323,14 +322,14 @@
  %if BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
         pop     ecx
-        TMPL_ONLY_64BIT_STMT pop eax
+TONLY64 pop     eax
         pop     edx
-        TMPL_ONLY_64BIT_STMT pop eax
+TONLY64 pop     eax
  %endif
         popfd
-        TMPL_ONLY_64BIT_STMT pop eax
+TONLY64 pop     eax
         pop     ebx
-        TMPL_ONLY_64BIT_STMT pop eax
+TONLY64 pop     eax
         pop     eax
-        TMPL_ONLY_64BIT_STMT add sp, 4
+TONLY64 add     sp, 4
         retn    (TMPL_BITS - 16) / 8
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-TrapSystemCallHandler.asm
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-TrapSystemCallHandler.asm	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-TrapSystemCallHandler.asm	(revision 60657)
@@ -319,9 +319,9 @@
 
         ; Convert the register context from whatever it is to ring-0.
-        BS3_ONLY_64BIT_STMT sub     rsp, 10h
+BONLY64 sub     rsp, 10h
         mov     ax, VAR_CALLER_AX
         sub     ax, BS3_SYSCALL_TO_RING0
         push    xAX
-        BS3_ONLY_16BIT_STMT push    ss
+BONLY16 push    ss
         push    xBX
         BS3_CALL Bs3RegCtxConvertToRingX, 2
@@ -330,6 +330,6 @@
         ; Restore the register context (does not return).
         pop     xBX                     ; restore saved pointer.
-        BS3_ONLY_64BIT_STMT sub     rsp, 18h
-        BS3_ONLY_16BIT_STMT push    ss
+BONLY64 sub     rsp, 18h
+BONLY16 push    ss
         push    xBX
         BS3_CALL Bs3RegCtxRestore, 1
@@ -342,8 +342,8 @@
 .restore_ctx:
         call    .convert_ptr_arg_to_cx_xSI
-        BS3_ONLY_64BIT_STMT sub     rsp, 10h
+BONLY64 sub     rsp, 10h
         mov     xDX, VAR_CALLER_DX
         push    xDX
-        BS3_ONLY_16BIT_STMT push    cx
+BONLY16 push    cx
         push    xSI
         BS3_CALL Bs3RegCtxRestore, 2
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-autostubs.kmk
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-autostubs.kmk	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-autostubs.kmk	(revision 60657)
@@ -25,4 +25,12 @@
 #
 
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelFlatDataToProtFar16,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelFlatDataToRealMode,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelProtFar16DataToFlat,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelProtFar16DataToRealMode,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelRealModeDataToFlat,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelRealModeDataToProtFar16,4)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3SelFar32ToFlat32NoClobber,6)
+$(call BS3KIT_FN_GEN_CMN_FARSTUB,bs3kit-common-16,Bs3RegCtxSaveEx,8)
 $(call BS3KIT_FN_GEN_CMN_NEARSTUB,bs3kit-common-16,Bs3TestCheckRegCtxEx)
 $(call BS3KIT_FN_GEN_CMN_NEARSTUB,bs3kit-common-16,Bs3StrCpy)
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-define.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-define.h	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-define.h	(revision 60657)
@@ -36,8 +36,10 @@
 #define Bs3MemAlloc BS3_CMN_MANGLER(Bs3MemAlloc)
 #define Bs3MemAllocZ BS3_CMN_MANGLER(Bs3MemAllocZ)
+#define Bs3MemChr BS3_CMN_MANGLER(Bs3MemChr)
 #define Bs3MemCpy BS3_CMN_MANGLER(Bs3MemCpy)
 #define Bs3MemFree BS3_CMN_MANGLER(Bs3MemFree)
 #define Bs3MemMove BS3_CMN_MANGLER(Bs3MemMove)
 #define Bs3MemPCpy BS3_CMN_MANGLER(Bs3MemPCpy)
+#define Bs3MemSet BS3_CMN_MANGLER(Bs3MemSet)
 #define Bs3MemZero BS3_CMN_MANGLER(Bs3MemZero)
 #define Bs3PagingInitRootForLM BS3_CMN_MANGLER(Bs3PagingInitRootForLM)
@@ -59,10 +61,18 @@
 #define Bs3RegCtxRestore BS3_CMN_MANGLER(Bs3RegCtxRestore)
 #define Bs3RegCtxSave BS3_CMN_MANGLER(Bs3RegCtxSave)
+#define Bs3RegCtxSaveEx BS3_CMN_MANGLER(Bs3RegCtxSaveEx)
 #define Bs3SelFar32ToFlat32 BS3_CMN_MANGLER(Bs3SelFar32ToFlat32)
+#define Bs3SelFar32ToFlat32NoClobber BS3_CMN_MANGLER(Bs3SelFar32ToFlat32NoClobber)
 #define Bs3SelFlatCodeToProtFar16 BS3_CMN_MANGLER(Bs3SelFlatCodeToProtFar16)
 #define Bs3SelFlatCodeToRealMode BS3_CMN_MANGLER(Bs3SelFlatCodeToRealMode)
+#define Bs3SelFlatDataToProtFar16 BS3_CMN_MANGLER(Bs3SelFlatDataToProtFar16)
+#define Bs3SelFlatDataToRealMode BS3_CMN_MANGLER(Bs3SelFlatDataToRealMode)
+#define Bs3SelProtFar16DataToFlat BS3_CMN_MANGLER(Bs3SelProtFar16DataToFlat)
+#define Bs3SelProtFar16DataToRealMode BS3_CMN_MANGLER(Bs3SelProtFar16DataToRealMode)
 #define Bs3SelProtFar32ToFlat32 BS3_CMN_MANGLER(Bs3SelProtFar32ToFlat32)
 #define Bs3SelProtModeCodeToRealMode BS3_CMN_MANGLER(Bs3SelProtModeCodeToRealMode)
 #define Bs3SelRealModeCodeToProtMode BS3_CMN_MANGLER(Bs3SelRealModeCodeToProtMode)
+#define Bs3SelRealModeDataToFlat BS3_CMN_MANGLER(Bs3SelRealModeDataToFlat)
+#define Bs3SelRealModeDataToProtFar16 BS3_CMN_MANGLER(Bs3SelRealModeDataToProtFar16)
 #define Bs3Shutdown BS3_CMN_MANGLER(Bs3Shutdown)
 #define Bs3SlabAlloc BS3_CMN_MANGLER(Bs3SlabAlloc)
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-undef.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-undef.h	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-mangling-code-undef.h	(revision 60657)
@@ -36,8 +36,10 @@
 #undef Bs3MemAlloc
 #undef Bs3MemAllocZ
+#undef Bs3MemChr
 #undef Bs3MemCpy
 #undef Bs3MemFree
 #undef Bs3MemMove
 #undef Bs3MemPCpy
+#undef Bs3MemSet
 #undef Bs3MemZero
 #undef Bs3PagingInitRootForLM
@@ -59,10 +61,18 @@
 #undef Bs3RegCtxRestore
 #undef Bs3RegCtxSave
+#undef Bs3RegCtxSaveEx
 #undef Bs3SelFar32ToFlat32
+#undef Bs3SelFar32ToFlat32NoClobber
 #undef Bs3SelFlatCodeToProtFar16
 #undef Bs3SelFlatCodeToRealMode
+#undef Bs3SelFlatDataToProtFar16
+#undef Bs3SelFlatDataToRealMode
+#undef Bs3SelProtFar16DataToFlat
+#undef Bs3SelProtFar16DataToRealMode
 #undef Bs3SelProtFar32ToFlat32
 #undef Bs3SelProtModeCodeToRealMode
 #undef Bs3SelRealModeCodeToProtMode
+#undef Bs3SelRealModeDataToFlat
+#undef Bs3SelRealModeDataToProtFar16
 #undef Bs3Shutdown
 #undef Bs3SlabAlloc
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-footer.mac	(revision 60657)
@@ -124,6 +124,6 @@
 %undef sSI
 
-%unmacro TMPL_ONLY_16BIT_STMT 1+
-%unmacro TMPL_ONLY_32BIT_STMT 1+
-%unmacro TMPL_ONLY_64BIT_STMT 1+
+%unmacro TONLY16 1+
+%unmacro TONLY32 1+
+%unmacro TONLY64 1+
 
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit-template-header.mac	(revision 60657)
@@ -438,34 +438,37 @@
 %endif
 
-;; @def TMPL_ONLY_16BIT_STMT
-; Version of BS3_ONLY_16BIT_STMT that follows the code template.
+;; @def TONLY16
+; Version of BONLY16 that follows the code template.
+; Like BONLY16 this normally goes in column 1.
 %if TMPL_BITS == 16
- %macro TMPL_ONLY_16BIT_STMT 1+
+ %macro TONLY16 1+
         %1
  %endmacro
 %else
- %macro TMPL_ONLY_16BIT_STMT 1+
- %endmacro
-%endif
-
-;; @def TMPL_ONLY_32BIT_STMT
-; Version of BS3_ONLY_32BIT_STMT that follows the code template.
+ %macro TONLY16 1+
+ %endmacro
+%endif
+
+;; @def TONLY32
+; Version of BONLY32 that follows the code template.
+; Like BONLY32 this normally goes in column 1.
 %if TMPL_BITS == 32
- %macro TMPL_ONLY_32BIT_STMT 1+
+ %macro TONLY32 1+
         %1
  %endmacro
 %else
- %macro TMPL_ONLY_32BIT_STMT 1+
- %endmacro
-%endif
-
-;; @def TMPL_ONLY_64BIT_STMT
-; Version of BS3_ONLY_64BIT_STMT that follows the code template.
+ %macro TONLY32 1+
+ %endmacro
+%endif
+
+;; @def TONLY64
+; Version of BONLY64 that follows the code template.
+; Like BONLY64 this normally goes in column 1.
 %if TMPL_BITS == 64
- %macro TMPL_ONLY_64BIT_STMT 1+
+ %macro TONLY64 1+
         %1
  %endmacro
 %else
- %macro TMPL_ONLY_64BIT_STMT 1+
+ %macro TONLY64 1+
  %endmacro
 %endif
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 60657)
@@ -911,7 +911,8 @@
 /** The BS3DATA16/BS3KIT_GRPNM_DATA16 GDT entry. */
 extern X86DESC BS3_FAR_DATA Bs3Gdte_DATA16;
+
 /** Free GDTes, part \#4. */
-
 extern X86DESC BS3_FAR_DATA Bs3GdteFreePart4[211];
+
 extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage08; /**< GDT entry 8 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_08 */
 extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage07; /**< GDT entry 7 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_07 */
@@ -1623,4 +1624,22 @@
 BS3_CMN_PROTO_NOSB(void, Bs3MemZero,(void BS3_FAR *pvDst, size_t cbDst));
 
+/**
+ * CRT style memset.
+ *
+ * @param   pvDst           The buffer to be fill.
+ * @param   bFiller         The filler byte.
+ * @param   cbDst           The number of bytes to fill.
+ */
+BS3_CMN_PROTO_NOSB(void, Bs3MemSet,(void BS3_FAR *pvDst, uint8_t bFiller, size_t cbDst));
+
+/**
+ * CRT style memchr.
+ *
+ * @param   pvHaystack      The memory to scan for @a bNeedle.
+ * @param   bNeedle         The byte to search for.
+ * @param   cbHaystack      The amount of memory to search.
+ */
+BS3_CMN_PROTO_NOSB(void BS3_FAR *, Bs3MemChr,(void const BS3_FAR *pvHaystack, uint8_t bNeedle, size_t cbHaystack));
+
 
 BS3_CMN_PROTO_STUB(void, Bs3UInt64Div,(RTUINT64U uDividend, RTUINT64U uDivisor, RTUINT64U BS3_FAR *paQuotientReminder));
@@ -1647,8 +1666,21 @@
 
 /**
+ * Wrapper around Bs3SelFar32ToFlat32 that makes it easier to use in tight
+ * assembly spots.
+ *
+ * @returns 32-bit flat address.
+ * @param   off             The segment offset.
+ * @param   uSel            The current mode segment selector.
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(6, uint32_t, Bs3SelFar32ToFlat32NoClobber,(uint32_t off, uint16_t uSel));
+
+/**
  * Converts a real mode code segment to a protected mode code segment selector.
  *
  * @returns protected mode segment selector.
  * @param   uRealSeg        Real mode code segment.
+ * @remarks All register are preserved, except return and parameter.
  */
 BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelRealModeCodeToProtMode,(uint16_t uRealSeg));
@@ -1659,4 +1691,5 @@
  * @returns protected mode segment selector.
  * @param   uProtSel        Real mode code segment.
+ * @remarks All register are preserved, except return and parameter.
  */
 BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelProtModeCodeToRealMode,(uint16_t uProtSel));
@@ -1665,6 +1698,7 @@
  * Converts a flat code address to a real mode segment and offset.
  *
- * @returns Far real mode address (high 16-bit is segment, low is offset)
+ * @returns Far real mode address (high 16-bit is segment, low is offset).
  * @param   uFlatAddr       Flat code address.
+ * @remarks All register are preserved, except return and parameter.
  */
 BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToRealMode,(uint32_t uFlatAddr));
@@ -1676,6 +1710,72 @@
  *          low is segment offset).
  * @param   uFlatAddr       Flat code address.
+ * @remarks All register are preserved, except return and parameter.
  */
 BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToProtFar16,(uint32_t uFlatAddr));
+
+/**
+ * Converts a flat data address to a real mode segment and offset.
+ *
+ * @returns Far real mode address (high 16-bit is segment, low is offset)
+ * @param   uFlatAddr       Flat code address.
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToRealMode,(uint32_t uFlatAddr));
+
+/**
+ * Converts a flat data address to a real mode segment and offset.
+ *
+ * @returns Far 16-bit protected mode address (high 16-bit is segment selector,
+ *          low is segment offset).
+ * @param   uFlatAddr       Flat code address.
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToProtFar16,(uint32_t uFlatAddr));
+
+/**
+ * Converts a far 16:16 data address to a real mode segment and offset.
+ *
+ * @returns Far real mode address (high 16-bit is segment, low is offset)
+ * @param   uFar1616        Far 16-bit protected mode address (high 16-bit is
+ *                          segment selector, low is segment offset).
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToRealMode,(uint32_t uFar1616));
+
+/**
+ * Converts a far 16:16 real mode address to a 16-bit protected mode address.
+ *
+ * @returns Far real mode address (high 16-bit is segment, low is offset)
+ * @param   uFar1616        Far real mode address (high 16-bit is segment, low
+ *                          is offset).
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToProtFar16,(uint32_t uFar1616));
+
+/**
+ * Converts a far 16:16 data address to a flat 32-bit address.
+ *
+ * @returns 32-bit flat address.
+ * @param   uFar1616        Far 16-bit protected mode address (high 16-bit is
+ *                          segment selector, low is segment offset).
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToFlat,(uint32_t uFar1616));
+
+/**
+ * Converts a far 16:16 real mode address to a flat address.
+ *
+ * @returns 32-bit flat address.
+ * @param   uFar1616        Far real mode address (high 16-bit is segment, low
+ *                          is offset).
+ * @remarks All register are preserved, except return.
+ * @remarks No 20h scratch space required in 64-bit mode.
+ */
+BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToFlat,(uint32_t uFar1616));
 
 /**
@@ -2094,11 +2194,14 @@
 /** @name BS3REG_CTX_F_XXX - BS3REGCTX::fbFlags masks.
  * @{ */
-/** The context doesn't have valid values for the CRx fields.
- * This is usually because it wasn't created with CPL=0. */
-#define BS3REG_CTX_F_NO_CR              UINT8_C(0x01)
-/** The CPU is too old for CR4, so no CR4 in this context. */
-#define BS3REG_CTX_F_NO_CR4             UINT8_C(0x02)
+/** The CR0 is MSW (only low 16-bit). */
+#define BS3REG_CTX_F_NO_CR0_IS_MSW      UINT8_C(0x01)
+/** No CR2 and CR3 values.  Not in CPL 0 or CPU too old for CR2 & CR3. */
+#define BS3REG_CTX_F_NO_CR2_CR3         UINT8_C(0x02)
+/** No CR4 value. The CPU is too old for CR4. */
+#define BS3REG_CTX_F_NO_CR4             UINT8_C(0x04)
+/** No TR and LDTR values.  Context gathered in real mode or v8086 mode. */
+#define BS3REG_CTX_F_NO_TR_LDTR         UINT8_C(0x08)
 /** The context doesn't have valid values for AMD64 GPR extensions. */
-#define BS3REG_CTX_F_NO_AMD64           UINT8_C(0x04)
+#define BS3REG_CTX_F_NO_AMD64           UINT8_C(0x10)
 /** @} */
 
@@ -2109,4 +2212,21 @@
  */
 BS3_CMN_PROTO_NOSB(void, Bs3RegCtxSave,(PCBS3REGCTX pRegCtx));
+
+/**
+ * Switch to the specified CPU bitcount, reserve additional stack and save the
+ * CPU context.
+ *
+ * This is for writing more flexible test drivers that can test more than the
+ * CPU bitcount (16-bit, 32-bit, 64-bit, and virtual 8086) of the driver itself.
+ * For instance a 32-bit driver can do V86 and 16-bit testing, thus saving more
+ * precious and problematic 16-bit code.
+ *
+ * @param   pRegCtx         Where to store the register context.
+ * @param   bBitMode        Bit mode to switch to, BS3_MODE_CODE_XXX.  Only
+ *                          BS3_MODE_CODE_MASK is used, other bits are ignored
+ *                          to make it possible to pass a full mode value.
+ * @param   cbExtraStack    Number of bytes of additional stack to allocate.
+ */
+BS3_CMN_PROTO_FARSTUB(8, void, Bs3RegCtxSaveEx,(PCBS3REGCTX pRegCtx, uint8_t bBitMode, uint16_t cbExtraStack));
 
 /**
@@ -2313,5 +2433,5 @@
 
 /**
- * Sets a trap handler (C/C++/assembly) for the current bitness.
+ * Sets a trap handler (C/C++/assembly) for the current bitcount.
  *
  * When using a 32-bit IDT, only #Bs3TrapSetHandler_c32 will have any effect.
Index: /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac
===================================================================
--- /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 60656)
+++ /trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 60657)
@@ -272,5 +272,6 @@
 ;;
 ; For instruction that should only be emitted in 16-bit mode. Follows BS3_SET_BITS.
-%macro BS3_ONLY_16BIT_STMT 1+
+; BONLY16 normally goes in column 1.
+%macro BONLY16 1+
  %if __BITS__ == 16
         %1
@@ -280,5 +281,6 @@
 ;;
 ; For instruction that should only be emitted in 32-bit mode. Follows BS3_SET_BITS.
-%macro BS3_ONLY_32BIT_STMT 1+
+; BONLY32 normally goes in column 1.
+%macro BONLY32 1+
  %if __BITS__ == 32
         %1
@@ -288,5 +290,6 @@
 ;;
 ; For instruction that should only be emitted in 64-bit mode. Follows BS3_SET_BITS.
-%macro BS3_ONLY_64BIT_STMT 1+
+; BONLY64 normally goes in column 1.
+%macro BONLY64 1+
  %if __BITS__ == 64
         %1
@@ -1405,11 +1408,14 @@
 ;; @name BS3REG_CTX_F_XXX - BS3REGCTX::fbFlags masks.
 ; @{
-;; The context doesn't have valid values for the CRx fields.
-;  This is usually because it wasn't created with CPL=0.
-%define BS3REG_CTX_F_NO_CR              0x01
-;; The CPU is too old for CR4, so no CR4 in this context.
-%define BS3REG_CTX_F_NO_CR4             0x02
+;; The CR0 is MSW (only low 16-bit). */
+%define BS3REG_CTX_F_NO_CR0_IS_MSW      0x01
+;; No CR2 and CR3 values.  Not in CPL 0 or CPU too old for CR2 & CR3.
+%define BS3REG_CTX_F_NO_CR2_CR3         0x02
+;; No CR4 value. The CPU is too old for CR4.
+%define BS3REG_CTX_F_NO_CR4             0x04
+;; No TR and LDTR values.  Context gathered in real mode or v8086 mode.
+%define BS3REG_CTX_F_NO_TR_LDTR         0x08
 ;; The context doesn't have valid values for AMD64 GPR extensions.
-%define BS3REG_CTX_F_NO_AMD64           0x04
+%define BS3REG_CTX_F_NO_AMD64           0x10
 ;; @}
 
