Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 59254)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk	(revision 59258)
@@ -521,4 +521,6 @@
 	bs3-cmn-TestSendStrCmd.asm \
 	bs3-cmn-TestIsVmmDevTestingPresent.asm \
+	bs3-cmn-Trap32Init.c \
+	bs3-cmn-Trap32SetGate.c \
        ../../../Runtime/common/asm/ASMBitFirstClear.asm \
        ../../../Runtime/common/asm/ASMBitFirstSet.asm \
@@ -557,4 +559,6 @@
 	bs3-wc32-U8RS.asm \
 	bs3-wc32-U8LS.asm \
+       bs3-c32-Trap32Generic.asm \
+       bs3-c32-Trap32ResumeFrame.asm
 
 # The 64-bit BS3Kit library.
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm	(revision 59258)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32Generic.asm	(revision 59258)
@@ -0,0 +1,323 @@
+; $Id$
+;; @file
+; BS3Kit - Trap, 32-bit assembly handlers.
+;
+
+;
+; 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"
+
+%ifndef TMPL_32BIT
+ %error "32-bit only template"
+%endif
+
+BS3_BEGIN_DATA32
+;; Easy to access flat address of Bs3Idt32GenericEntries.
+BS3_GLOBAL_DATA g_Bs3Idt32GenericEntriesFlatAddr, 4
+        dd Bs3Idt32GenericEntries
+
+;; Pointer C trap handlers.
+BS3_GLOBAL_DATA g_apfnBs3Trap32Handlers, 1024
+        resd 256
+
+
+TMPL_BEGIN_TEXT
+BS3_EXTERN_CMN Bs3Trap32DefaultHandler
+BS3_EXTERN_CMN Bs3Trap32ResumeFrame
+
+
+;;
+; Generic entry points for IDT handlers, 8 byte spacing.
+;
+BS3_PROC_BEGIN Bs3Idt32GenericEntries
+%macro Bs3Idt32GenericEntry 1
+        db      06ah, i                 ; push imm8 - note that this is a signextended value.
+        jmp     %1
+        ALIGNCODE(8)
+%assign i i+1
+%endmacro
+
+%assign i 0                             ; start counter.
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 0
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 2
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 3
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 4
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 5
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 6
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 7
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; 8
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 9
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; a
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; b
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; c
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; d
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; e
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; f  (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 10
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; 11
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 12
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 13
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 14
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 15 (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 16 (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 17 (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 18 (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 19 (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1a (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1b (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1c (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1d (reserved)
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapErrCode ; 1e
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt   ; 1f (reserved)
+%rep 224
+        Bs3Idt32GenericEntry bs3Idt32GenericTrapOrInt
+%endrep
+BS3_PROC_END  Bs3Idt32GenericEntries
+
+
+
+
+;;
+; Trap or interrupt (no error code).
+;
+BS3_PROC_BEGIN bs3Idt32GenericTrapOrInt
+        pushfd
+        cli
+        cld
+
+        sub     esp, BS3TRAPFRAME_size
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], eax
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], ebp
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], edx
+        lea     ebp, [esp + BS3TRAPFRAME_size + 4] ; iret - 4 (i.e. ebp frame chain location)
+
+        mov     edx, [esp + BS3TRAPFRAME_size]
+        mov     [esp + BS3TRAPFRAME.fHandlerRfl], edx
+
+        movzx   edx, byte [esp + BS3TRAPFRAME_size + 4]
+        mov     [esp + BS3TRAPFRAME.bXcpt], edx
+
+        xor     edx, edx
+        mov     [esp + BS3TRAPFRAME.uErrCd], edx
+        mov     [esp + BS3TRAPFRAME.uErrCd + 4], edx
+        jmp     bs3Idt32GenericCommon
+BS3_PROC_END   bs3Idt32GenericTrapOrInt
+
+
+;;
+; Trap with error code.
+;
+BS3_PROC_BEGIN bs3Idt32GenericTrapErrCode
+        pushfd
+        cli
+        cld
+
+        sub     esp, BS3TRAPFRAME_size
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], eax
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], ebp
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], edx
+        lea     ebp, [esp + BS3TRAPFRAME_size + 8] ; iret - 4 (i.e. ebp frame chain location)
+
+        mov     edx, [esp + BS3TRAPFRAME_size]
+        mov     [esp + BS3TRAPFRAME.fHandlerRfl], edx
+
+        movzx   edx, byte [esp + BS3TRAPFRAME_size + 4]
+        mov     [esp + BS3TRAPFRAME.bXcpt], edx
+
+        mov     edx, [esp + BS3TRAPFRAME_size + 8]
+;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
+        mov     [esp + BS3TRAPFRAME.uErrCd], edx
+        xor     edx, edx
+        mov     [esp + BS3TRAPFRAME.uErrCd + 4], edx
+        jmp     bs3Idt32GenericCommon
+BS3_PROC_END   bs3Idt32GenericTrapErrCode
+
+
+;;
+; Common context saving code and dispatching.
+;
+; @param    esp     Pointer to the trap frame.  The following members have been
+;                   filled in by the previous code:
+;                       - bXcpt
+;                       - uErrCd
+;                       - fHandlerRFL
+;                       - Ctx.eax (except upper dword)
+;                       - Ctx.edx (except upper dword)
+;                       - Ctx.ebp (except upper dword)
+;
+; @param    ebp     Pointer to the dword before the iret frame, i.e. where ebp
+;                   would be saved if this was a normal call.
+; @param    edx     Zero (0).
+;
+BS3_PROC_BEGIN bs3Idt32GenericCommon
+        ;
+        ; Fake EBP frame.
+        ;
+        mov     eax, [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
+        mov     [ebp], eax
+
+        ;
+        ; Save the remaining GPRs and segment registers.
+        ;
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx], ecx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx], ebx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], edi
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi], esi
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], ds
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.es], es
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], fs
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], gs
+
+        ;
+        ; Load 32-bit data selector for the DPL we're executing at into DS and ES.
+        ; Save the handler SS and CS values first.
+        ;
+        mov     ax, cs
+        mov     [esp + BS3TRAPFRAME.uHandlerCs], ax
+        mov     ax, ss
+        mov     [esp + BS3TRAPFRAME.uHandlerSs], ax
+        and     ax, 3
+        mov     cx, ax
+        shl     ax, BS3_SEL_RING_SHIFT
+        or      ax, cx
+        and     ax, BS3_SEL_R0_DS32
+        mov     ds, ax
+        mov     es, ax
+
+        ;
+        ; Copy iret info.
+        ;
+        mov     ecx, [ebp + 4]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rip], ecx
+        mov     ecx, [ebp + 12]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags], ecx
+        mov     cx, [ebp + 8]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cs], cx
+        test    dword [ebp + 12], X86_EFL_VM
+        jnz     .iret_frame_v8086
+        mov     ax, ss
+        and     ax, 3
+        and     cx, 3
+        cmp     ax, ax
+        je      .iret_frame_same_cpl
+
+        mov     ecx, [ebp + 16]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
+        mov     cx, [ebp + 20]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
+        lea     eax, [ebp + 24]
+        mov     [esp + BS3TRAPFRAME.uHandlerRsp], eax
+        jmp     .iret_frame_done
+
+.iret_frame_same_cpl:
+        lea     ecx, [ebp + 12]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
+        mov     cx, ss
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
+        lea     eax, [ebp + 16]
+        mov     [esp + BS3TRAPFRAME.uHandlerRsp], eax
+        jmp     .iret_frame_done
+
+.iret_frame_v8086:
+        lea     ecx, [ebp + 12]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
+        mov     cx, [ebp + 20]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
+        mov     cx, [ebp + 24]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.es], cx
+        mov     cx, [ebp + 28]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], cx
+        mov     cx, [ebp + 32]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], cx
+        mov     cx, [ebp + 36]
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], cx
+        lea     eax, [ebp + 40]
+        mov     [esp + BS3TRAPFRAME.uHandlerRsp], eax
+        jmp     .iret_frame_done
+
+.iret_frame_done:
+        ;
+        ; Control registers.
+        ;
+        mov     eax, cr0
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0], eax
+        mov     eax, cr2
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2], eax
+        mov     eax, cr3
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3], eax
+        mov     eax, cr4
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4], eax
+
+        ;
+        ; Set context bit width and clear all upper dwords and unused register members.
+        ;
+        mov     dword [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cBits], 32
+        xor     edx, edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rax    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r8     + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r9     + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r10    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r11    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r12    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r13    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r14    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.r15    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.rip    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3    + 4], edx
+        mov     [esp + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4    + 4], edx
+
+        ;
+        ; Dispatch it to C code.
+        ;
+        movzx   ebx, byte [esp + BS3TRAPFRAME.bXcpt]
+        mov     eax, [BS3_DATA_NM(g_apfnBs3Trap32Handlers) + ebx * 4]
+        or      eax, eax
+        jnz     .call_handler
+        mov     eax, Bs3Trap32DefaultHandler
+.call_handler:
+        mov     edi, esp
+        push    edi
+        call    eax
+
+        ;
+        ; Resume execution using trap frame.
+        ;
+        push    0
+        push    edi
+        call    Bs3Trap32ResumeFrame
+.panic:
+        int3
+        hlt
+        jmp     .panic
+BS3_PROC_END   bs3Idt32GenericCommon
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32ResumeFrame.asm
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32ResumeFrame.asm	(revision 59258)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-c32-Trap32ResumeFrame.asm	(revision 59258)
@@ -0,0 +1,167 @@
+; $Id$
+;; @file
+; BS3Kit - Trap, 32-bit resume.
+;
+
+;
+; 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"
+
+%ifndef TMPL_32BIT
+ %error "32-bit only template, at the moment"
+%endif
+
+
+;;
+; @cproto  BS3_DECL(void) Bs3Trap32ResumeFrame(BS3TRAPFRAME BS3_FAR *pTrapFrame, uint16_t fFlags)
+BS3_PROC_BEGIN_CMN Bs3Trap32ResumeFrame
+        push    xBP
+        mov     xBP, xSP
+
+        mov     ebx, [xBP + 8]          ; pTrapFrame
+
+        ;
+        ; Restore control registers if they've changed.
+        ;
+        test    word [xBP + 12], BS3TRAPRESUME_F_SKIP_CRX
+        jnz     .skip_control_regs
+
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4]
+        mov     edx, cr4
+        cmp     eax, edx
+        je      .skip_cr4
+        mov     cr4, eax
+.skip_cr4:
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3]
+        mov     edx, cr3
+        cmp     eax, edx
+        je      .skip_cr3
+        mov     cr3, eax
+.skip_cr3:
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2]
+        mov     edx, cr2
+        cmp     eax, edx
+        je      .skip_cr2
+        mov     cr2, eax
+.skip_cr2:
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0]
+        mov     edx, cr0
+        cmp     eax, edx
+        je      .skip_cr0
+        mov     cr0, eax
+.skip_cr0:
+.skip_control_regs:
+
+        ;
+        ; Branch into iret frame specific code.
+        ;
+        test    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags], X86_EFL_VM
+        jnz     .iret_v8086
+        mov     ax, ss
+        mov     dx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss]
+        and     ax, 3
+        and     dx, 3
+        cmp     ax, dx
+        cli
+        jne     .iret_other_cpl
+
+        ;
+        ; IRET to same CPL.  ASSUMES that we can stash an IRET frame and EBP at ESP.
+        ;
+        mov     ebp, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp]
+        sub     ebp, 16                 ; IRET + EBP
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags]
+        mov     [ebp + 12], eax
+        movzx   eax, word [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cs]
+        mov     [ebp + 8], eax
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rip]
+        mov     [ebp + 4], eax
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
+        mov     [ebp], eax
+
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax]
+        mov     ecx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx]
+        mov     edx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx]
+        mov     esi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi]
+        mov     edi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi]
+
+        mov     gs, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.gs]
+        mov     fs, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.fs]
+        mov     es, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.es]
+        mov     ss, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ds]
+        mov     ebx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx]
+        pop     ds
+        leave
+        iretd
+
+        ;
+        ; IRET to other CPL.
+        ;
+.iret_other_cpl:
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cs]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rip]
+
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax]
+        mov     ecx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx]
+        mov     edx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx]
+        mov     esi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi]
+        mov     edi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi]
+
+        mov     gs, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.gs]
+        mov     fs, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.fs]
+        mov     es, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.es]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ds]
+        mov     ebx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx]
+        pop     ds
+        pop     ebp
+        iretd
+
+        ;
+        ; IRET to virtual 8086 mode.
+        ;
+.iret_v8086:
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.gs]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.fs]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ds]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.es]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.ss]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.cs]
+        push    dword [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rip]
+
+        mov     eax, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rax]
+        mov     ecx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx]
+        mov     edx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx]
+        mov     esi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi]
+        mov     edi, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi]
+        mov     ebp, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
+        mov     ebx, [ebx + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx]
+        iretd
+
+BS3_PROC_END_CMN   Bs3Trap32ResumeFrame
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32Init.c
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32Init.c	(revision 59258)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32Init.c	(revision 59258)
@@ -0,0 +1,42 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Bs3Trap32Init
+ */
+
+/*
+ * Copyright (C) 2007-2016 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+/*********************************************************************************************************************************
+*   Header Files                                                                                                                 *
+*********************************************************************************************************************************/
+#include "bs3kit-template-header.h"
+#include <iprt/asm.h>
+
+
+BS3_DECL(void) Bs3Trap32Init(void)
+{
+    unsigned iIdt = 256;
+    while (iIdt-- > 0)
+        Bs3Trap32SetGate(iIdt, X86_SEL_TYPE_SYS_386_INT_GATE, 0 /*bDpl*/,
+                         BS3_SEL_R0_CS32, BS3_DATA_NM(g_Bs3Idt32GenericEntriesFlatAddr) + iIdt * 8, 0 /*cParams*/);
+    /** @todo Init TSS for double faults and stuff. */
+}
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32SetGate.c
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32SetGate.c	(revision 59258)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-Trap32SetGate.c	(revision 59258)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ * BS3Kit - Bs3Trap32SetGate
+ */
+
+/*
+ * Copyright (C) 2007-2015 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+/*********************************************************************************************************************************
+*   Header Files                                                                                                                 *
+*********************************************************************************************************************************/
+#include "bs3kit-template-header.h"
+#include <iprt/asm.h>
+
+
+BS3_DECL(void) Bs3Trap32SetGate(uint8_t iIdt, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint32_t off, uint8_t cParams)
+{
+    X86DESC BS3_FAR *pIdte = &BS3_DATA_NM(Bs3Idt32)[iIdt];
+
+    BS3_ASSERT(bDpl <= 3);
+    BS3_ASSERT(bType <= 7);
+    BS3_ASSERT(cParams <= 15);
+    pIdte->Gate.u16OffsetLow    = (uint16_t)off;
+    pIdte->Gate.u16OffsetHigh   = (uint16_t)(off >> 16);
+    pIdte->Gate.u16Sel          = uSel;
+    pIdte->Gate.u4ParmCount     = cParams;
+    pIdte->Gate.u4Type          = bType;
+    pIdte->Gate.u2Dpl           = bDpl;
+    pIdte->Gate.u4Reserved      = 0;
+    pIdte->Gate.u1DescType      = 0; /* system */
+    pIdte->Gate.u1Present       = 1;
+}
+
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 59254)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h	(revision 59258)
@@ -1559,4 +1559,137 @@
 #define Bs3KbdWrite BS3_CMN_NM(Bs3KbdWrite) /**< Selects #Bs3KbdWrite_c16, #Bs3KbdWrite_c32 or #Bs3KbdWrite_c64. */
 
+
+/**
+ * BS3 integer register.
+ */
+typedef union BS3REG
+{
+    /** 8-bit unsigned integer. */
+    uint8_t     u8;
+    /** 16-bit unsigned integer. */
+    uint16_t    u16;
+    /** 32-bit unsigned integer. */
+    uint32_t    u32;
+    /** 64-bit unsigned integer. */
+    uint64_t    u64;
+    /** Full unsigned integer. */
+    uint64_t    u;
+    /** High/low byte view. */
+    struct
+    {
+        uint8_t bLo;
+        uint8_t bHi;
+    } b;
+    /** 8-bit view. */
+    uint8_t     au8[8];
+    /** 16-bit view. */
+    uint16_t    au16[4];
+    /** 32-bit view. */
+    uint32_t    au32[2];
+} BS3REG;
+/** Pointer to an integer register. */
+typedef BS3REG BS3_FAR *PBS3REG;
+/** Pointer to a const integer register. */
+typedef BS3REG const BS3_FAR *PCBS3REG;
+
+/**
+ * Register context (without FPU).
+ */
+typedef struct BS3REGCTX
+{
+    BS3REG      rax;
+    BS3REG      rcx;
+    BS3REG      rdx;
+    BS3REG      rbx;
+    BS3REG      rsp;
+    BS3REG      rbp;
+    BS3REG      rsi;
+    BS3REG      rdi;
+    BS3REG      r8;
+    BS3REG      r9;
+    BS3REG      r10;
+    BS3REG      r11;
+    BS3REG      r12;
+    BS3REG      r13;
+    BS3REG      r14;
+    BS3REG      r15;
+    BS3REG      rflags;
+    BS3REG      rip;
+    uint16_t    cs;
+    uint16_t    ds;
+    uint16_t    es;
+    uint16_t    fs;
+    uint16_t    gs;
+    uint16_t    ss;
+    uint8_t     cBits;
+    uint8_t     abPadding[3];
+    BS3REG      cr0;
+    BS3REG      cr2;
+    BS3REG      cr3;
+    BS3REG      cr4;
+} BS3REGCTX;
+/** Pointer to a register context. */
+typedef BS3REGCTX BS3_FAR *PBS3REGCTX;
+/** Pointer to a const register context. */
+typedef BS3REGCTX const BS3_FAR *PCBS3REGCTX;
+
+
+/**
+ * Trap frame.
+ */
+typedef struct BS3TRAPFRAME
+{
+    /** Exception/interrupt number. */
+    uint8_t     bXcpt;
+    /** Explicit alignment. */
+    uint8_t     bAlignment;
+    /** The handler CS. */
+    uint16_t    uHandlerCc;
+    /** The handler SS. */
+    uint16_t    uHandlerSs;
+    /** The handler RSP (top of iret frame). */
+    uint64_t    uHandlerRsp;
+    /** The handler RFLAGS value. */
+    uint64_t    fHandlerRfl;
+    /** The error code (if applicable). */
+    uint64_t    uErrCd;
+    /** The register context. */
+    BS3REGCTX   Ctx;
+} BS3TRAPFRAME;
+/** Pointer to a trap frame. */
+typedef BS3TRAPFRAME BS3_FAR *PBS3TRAPFRAME;
+/** Pointer to a const trap frame.   */
+typedef BS3TRAPFRAME const BS3_FAR *PCBS3TRAPFRAME;
+
+/**
+ * Resumes execution of a 32-bit trap frame.
+ *
+ * @param   pTrapFrame      Trap frame to resume.
+ * @param   fFlags          Flags, BS3TRAPRESUME_F_XXX.
+ */
+BS3_DECL(void) Bs3Trap32ResumeFrame_c32(BS3TRAPFRAME BS3_FAR *pTrapFrame, uint16_t fFlags);
+#define Bs3Trap32ResumeFrame BS3_CMN_NM(Bs3Trap32ResumeFrame) /**< Selects Bs3Trap32ResumeFrame_c16 (not implemented), #Bs3Trap32ResumeFrame_c32 or Bs3Trap32ResumeFrame_c64 (not implemented). */
+
+/** Skip restoring the CRx registers. */
+#define BS3TRAPRESUME_F_SKIP_CRX    UINT16_C(0x0001)
+
+/**
+ * Modifies the 32-bit IDT entry specified by @a iIdt.
+ *
+ * @param   iIdt        The index of the IDT entry to set.
+ * @param   bType       The gate type (X86_SEL_TYPE_SYS_XXX).
+ * @param   bDpl        The DPL.
+ * @param   uSel        The handler selector.
+ * @param   off         The handler offset (if applicable).
+ * @param   cParams     The parameter count (for call gates).
+ */
+BS3_DECL(void) Bs3Trap32SetGate(uint8_t iIdt, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint32_t off, uint8_t cParams);
+
+/** The address of Bs3Idt32GenericEntries.
+ * Bs3Idt32GenericEntries is an array of interrupt/trap/whatever entry
+ * points, 8 bytes each, that will create a register frame and call the generic
+ * C compatible trap handlers. */
+extern uint32_t BS3_DATA_NM(g_Bs3Idt32GenericEntriesFlatAddr);
+
 /** @} */
 
Index: trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac
===================================================================
--- trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 59254)
+++ trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.mac	(revision 59258)
@@ -828,4 +828,57 @@
 %endif
 
-%endif
-
+;
+; BS3 register context (without FPU).
+;
+struc BS3REGCTX
+        .rax            resq 1
+        .rcx            resq 1
+        .rdx            resq 1
+        .rbx            resq 1
+        .rsp            resq 1
+        .rbp            resq 1
+        .rsi            resq 1
+        .rdi            resq 1
+        .r8             resq 1
+        .r9             resq 1
+        .r10            resq 1
+        .r11            resq 1
+        .r12            resq 1
+        .r13            resq 1
+        .r14            resq 1
+        .r15            resq 1
+        .rflags         resq 1
+        .rip            resq 1
+        .cs             resw 1
+        .ds             resw 1
+        .es             resw 1
+        .fs             resw 1
+        .gs             resw 1
+        .ss             resw 1
+        .cBits          resb 1
+        .abPadding      resb 3
+        .cr0            resq 1
+        .cr2            resq 1
+        .cr3            resq 1
+        .cr4            resq 1
+endstruc
+
+;;
+; BS3 Trap Frame.
+;
+struc BS3TRAPFRAME
+        .bXcpt          resb 1
+        .bAlignment     resb 1
+        .uHandlerCs     resw 1
+        .uHandlerSs     resw 1
+        .uHandlerRsp    resq 1
+        .fHandlerRfl    resq 1
+        .uErrCd         resq 1
+        .Ctx            resb BS3REGCTX_size
+endstruc
+
+;; Flag for Bs3TrapXxResumeFrame methods.
+%define BS3TRAPRESUME_F_SKIP_CRX 1
+
+%endif
+
