VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3kit.h@ 107102

最後變更 在這個檔案從107102是 107102,由 vboxsync 提交於 4 月 前

ValidationKit/bootsectors: Implement BS3_ASSERT() to actually do something useful; bugref:9898; bugref:10658; jiraref:VBP-1207

Image size note: this increases image sizes (about +15KB total between
bs3-cpu-instr-{2,3,4}). I've committed changes to make all of the
ValidatonKit/bootsectors images still fit within their constraints.
Some further work may be warranted to either eliminate unneeded asserts;
or wrap them in runtime `if's to shrink and/or coalesce failure strings.

Performance note: completion times for bs3-cpu-instr-{2,3,4} with this
fully disabled vs. are within 2% of each other -- not significant.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 193.2 KB
 
1/* $Id: bs3kit.h 107102 2024-11-22 07:57:01Z vboxsync $ */
2/** @file
3 * BS3Kit - structures, symbols, macros and stuff.
4 */
5
6/*
7 * Copyright (C) 2007-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef BS3KIT_INCLUDED_bs3kit_h
38#define BS3KIT_INCLUDED_bs3kit_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#ifndef DOXYGEN_RUNNING
44# undef IN_RING0
45# define IN_RING0
46#endif
47
48#define RT_NO_STRICT /* Don't drag in IPRT assertion code in inline code we may use (asm.h). */
49#include <iprt/cdefs.h>
50#include <iprt/types.h>
51
52#ifndef DOXYGEN_RUNNING
53# undef IN_RING0
54#endif
55
56/*
57 * Make asm.h and friend compatible with our 64-bit assembly config (ASM_CALL64_MSC).
58 */
59#if defined(__GNUC__) && ARCH_BITS == 64
60# undef DECLASM
61# ifdef __cplusplus
62# define DECLASM(type) extern "C" type BS3_CALL
63# else
64# define DECLASM(type) type BS3_CALL
65# endif
66#endif
67
68
69/*
70 * Work around ms_abi trouble in the gcc camp (gcc bugzilla #50818).
71 * ASSUMES all va_lists are in functions with
72 */
73#if defined(__GNUC__) && ARCH_BITS == 64
74# undef va_list
75# undef va_start
76# undef va_end
77# undef va_copy
78# define va_list __builtin_ms_va_list
79# define va_start(a_Va, a_Arg) __builtin_ms_va_start(a_Va, a_Arg)
80# define va_end(a_Va) __builtin_ms_va_end(a_Va)
81# define va_copy(a_DstVa, a_SrcVa) __builtin_ms_va_copy(a_DstVa, a_SrcVa)
82#endif
83
84
85/** @def BS3_USE_ALT_16BIT_TEXT_SEG
86 * @ingroup grp_bs3kit
87 * Combines the BS3_USE_RM_TEXT_SEG, BS3_USE_X0_TEXT_SEG, and
88 * BS3_USE_X1_TEXT_SEG indicators into a single one.
89 */
90#if defined(BS3_USE_RM_TEXT_SEG) || defined(BS3_USE_X0_TEXT_SEG) || defined(BS3_USE_X1_TEXT_SEG) || defined(DOXYGEN_RUNNING)
91# define BS3_USE_ALT_16BIT_TEXT_SEG
92#else
93# undef BS3_USE_ALT_16BIT_TEXT_SEG
94#endif
95
96/** @def BS3_USE_X0_TEXT_SEG
97 * @ingroup grp_bs3kit
98 * Emit 16-bit code to the BS3X0TEXT16 segment - ignored for 32-bit and 64-bit.
99 *
100 * Calling directly into the BS3X0TEXT16 segment is only possible in real-mode
101 * and v8086 mode. In protected mode the real far pointer have to be converted
102 * to a protected mode pointer that uses BS3_SEL_X0TEXT16_CS, Bs3TestDoModes and
103 * associates does this automatically.
104 */
105#ifdef DOXYGEN_RUNNING
106# define BS3_USE_X0_TEXT_SEG
107#endif
108
109/** @def BS3_USE_X1_TEXT_SEG
110 * @ingroup grp_bs3kit
111 * Emit 16-bit code to the BS3X1TEXT16 segment - ignored for 32-bit and 64-bit.
112 *
113 * Calling directly into the BS3X1TEXT16 segment is only possible in real-mode
114 * and v8086 mode. In protected mode the real far pointer have to be converted
115 * to a protected mode pointer that uses BS3_SEL_X1TEXT16_CS, Bs3TestDoModes and
116 * associates does this automatically.
117 */
118#ifdef DOXYGEN_RUNNING
119# define BS3_USE_X1_TEXT_SEG
120#endif
121
122/** @def BS3_USE_RM_TEXT_SEG
123 * @ingroup grp_bs3kit
124 * Emit 16-bit code to the BS3RMTEXT16 segment - ignored for 32-bit and 64-bit.
125 *
126 * This segment is normally used for real-mode only code, though
127 * BS3_SEL_RMTEXT16_CS can be used to call it from protected mode. Unlike the
128 * BS3X0TEXT16 and BS3X1TEXT16 segments which are empty by default, this segment
129 * is used by common BS3Kit code.
130 */
131#ifdef DOXYGEN_RUNNING
132# define BS3_USE_X0_TEXT_SEG
133#endif
134
135/** @def BS3_MODEL_FAR_CODE
136 * @ingroup grp_bs3kit
137 * Default compiler model indicates far code.
138 */
139#ifdef DOXYGEN_RUNNING
140# define BS3_MODEL_FAR_CODE
141#elif !defined(BS3_MODEL_FAR_CODE) && (defined(__LARGE__) || defined(__MEDIUM__) || defined(__HUGE__)) && ARCH_BITS == 16
142# define BS3_MODEL_FAR_CODE
143#endif
144
145
146/*
147 * We normally don't want the noreturn / aborts attributes as they mess up stack traces.
148 *
149 * Note! pragma aux <fnname> aborts can only be used with functions
150 * implemented in C and functions that does not have parameters.
151 */
152#define BS3_KIT_WITH_NO_RETURN
153#ifndef BS3_KIT_WITH_NO_RETURN
154# undef DECL_NO_RETURN
155# define DECL_NO_RETURN(type) type
156#endif
157
158
159/*
160 * We may want to reuse some IPRT code in the common name space, so we
161 * redefine the RT_MANGLER to work like BS3_CMN_NM. (We cannot use
162 * BS3_CMN_NM yet, as we need to include IPRT headers with function
163 * declarations before we can define it. Thus the duplciate effort.)
164 */
165#if ARCH_BITS == 16
166# undef RTCALL
167# if defined(BS3_USE_ALT_16BIT_TEXT_SEG)
168# define RTCALL __cdecl __far
169# define RT_MANGLER(a_Name) RT_CONCAT(a_Name,_f16)
170# else
171# define RTCALL __cdecl __near
172# define RT_MANGLER(a_Name) RT_CONCAT(a_Name,_c16)
173# endif
174#else
175# define RT_MANGLER(a_Name) RT_CONCAT3(a_Name,_c,ARCH_BITS)
176#endif
177#include <iprt/mangling.h>
178#include <iprt/x86.h>
179#include <iprt/err.h>
180
181/*
182 * Include data symbol mangling (function mangling/mapping must be done
183 * after the protypes).
184 */
185#include "bs3kit-mangling-data.h"
186
187
188
189RT_C_DECLS_BEGIN
190
191/** @defgroup grp_bs3kit BS3Kit - Boot Sector Kit \#3
192 *
193 * The BS3Kit is a framework for bare metal floppy/usb image tests,
194 * see the @ref pg_bs3kit "doc page" for more.
195 *
196 * @{ */
197
198
199/** @name Execution modes.
200 * @{ */
201#define BS3_MODE_INVALID UINT8_C(0x00)
202#define BS3_MODE_RM UINT8_C(0x01) /**< real mode. */
203#define BS3_MODE_PE16 UINT8_C(0x11) /**< 16-bit protected mode kernel+tss, running 16-bit code, unpaged. */
204#define BS3_MODE_PE16_32 UINT8_C(0x12) /**< 16-bit protected mode kernel+tss, running 32-bit code, unpaged. */
205#define BS3_MODE_PE16_V86 UINT8_C(0x18) /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
206#define BS3_MODE_PE32 UINT8_C(0x22) /**< 32-bit protected mode kernel+tss, running 32-bit code, unpaged. */
207#define BS3_MODE_PE32_16 UINT8_C(0x21) /**< 32-bit protected mode kernel+tss, running 16-bit code, unpaged. */
208#define BS3_MODE_PEV86 UINT8_C(0x28) /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, unpaged. */
209#define BS3_MODE_PP16 UINT8_C(0x31) /**< 16-bit protected mode kernel+tss, running 16-bit code, paged. */
210#define BS3_MODE_PP16_32 UINT8_C(0x32) /**< 16-bit protected mode kernel+tss, running 32-bit code, paged. */
211#define BS3_MODE_PP16_V86 UINT8_C(0x38) /**< 16-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
212#define BS3_MODE_PP32 UINT8_C(0x42) /**< 32-bit protected mode kernel+tss, running 32-bit code, paged. */
213#define BS3_MODE_PP32_16 UINT8_C(0x41) /**< 32-bit protected mode kernel+tss, running 16-bit code, paged. */
214#define BS3_MODE_PPV86 UINT8_C(0x48) /**< 32-bit protected mode kernel+tss, running virtual 8086 mode code, paged. */
215#define BS3_MODE_PAE16 UINT8_C(0x51) /**< 16-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
216#define BS3_MODE_PAE16_32 UINT8_C(0x52) /**< 16-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
217#define BS3_MODE_PAE16_V86 UINT8_C(0x58) /**< 16-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging. */
218#define BS3_MODE_PAE32 UINT8_C(0x62) /**< 32-bit protected mode kernel+tss, running 32-bit code, PAE paging. */
219#define BS3_MODE_PAE32_16 UINT8_C(0x61) /**< 32-bit protected mode kernel+tss, running 16-bit code, PAE paging. */
220#define BS3_MODE_PAEV86 UINT8_C(0x68) /**< 32-bit protected mode kernel+tss, running virtual 8086 mode, PAE paging. */
221#define BS3_MODE_LM16 UINT8_C(0x71) /**< 16-bit long mode (paged), kernel+tss always 64-bit. */
222#define BS3_MODE_LM32 UINT8_C(0x72) /**< 32-bit long mode (paged), kernel+tss always 64-bit. */
223#define BS3_MODE_LM64 UINT8_C(0x74) /**< 64-bit long mode (paged), kernel+tss always 64-bit. */
224
225#define BS3_MODE_CODE_MASK UINT8_C(0x0f) /**< Running code mask. */
226#define BS3_MODE_CODE_16 UINT8_C(0x01) /**< Running 16-bit code. */
227#define BS3_MODE_CODE_32 UINT8_C(0x02) /**< Running 32-bit code. */
228#define BS3_MODE_CODE_64 UINT8_C(0x04) /**< Running 64-bit code. */
229#define BS3_MODE_CODE_V86 UINT8_C(0x08) /**< Running 16-bit virtual 8086 code. */
230
231#define BS3_MODE_SYS_MASK UINT8_C(0xf0) /**< kernel+tss mask. */
232#define BS3_MODE_SYS_RM UINT8_C(0x00) /**< Real mode kernel+tss. */
233#define BS3_MODE_SYS_PE16 UINT8_C(0x10) /**< 16-bit protected mode kernel+tss. */
234#define BS3_MODE_SYS_PE32 UINT8_C(0x20) /**< 32-bit protected mode kernel+tss. */
235#define BS3_MODE_SYS_PP16 UINT8_C(0x30) /**< 16-bit paged protected mode kernel+tss. */
236#define BS3_MODE_SYS_PP32 UINT8_C(0x40) /**< 32-bit paged protected mode kernel+tss. */
237#define BS3_MODE_SYS_PAE16 UINT8_C(0x50) /**< 16-bit PAE paged protected mode kernel+tss. */
238#define BS3_MODE_SYS_PAE32 UINT8_C(0x60) /**< 32-bit PAE paged protected mode kernel+tss. */
239#define BS3_MODE_SYS_LM UINT8_C(0x70) /**< 64-bit (paged) long mode protected mode kernel+tss. */
240
241/** Whether the mode has paging enabled. */
242#define BS3_MODE_IS_PAGED(a_fMode) ((a_fMode) >= BS3_MODE_PP16)
243/** Whether the mode has legacy paging enabled (legacy as opposed to PAE or
244 * long mode). */
245#define BS3_MODE_IS_LEGACY_PAGING(a_fMode) ((a_fMode) >= BS3_MODE_PP16 && (a_fMode) < BS3_MODE_PAE16)
246
247/** Whether the mode is running v8086 code. */
248#define BS3_MODE_IS_V86(a_fMode) (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_V86)
249/** Whether the we're executing in real mode or v8086 mode. */
250#define BS3_MODE_IS_RM_OR_V86(a_fMode) ((a_fMode) == BS3_MODE_RM || BS3_MODE_IS_V86(a_fMode))
251/** Whether the mode is running 16-bit code, except v8086. */
252#define BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode) (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_16)
253/** Whether the mode is running 16-bit code (includes v8086). */
254#define BS3_MODE_IS_16BIT_CODE(a_fMode) (BS3_MODE_IS_16BIT_CODE_NO_V86(a_fMode) || BS3_MODE_IS_V86(a_fMode))
255/** Whether the mode is running 32-bit code. */
256#define BS3_MODE_IS_32BIT_CODE(a_fMode) (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_32)
257/** Whether the mode is running 64-bit code. */
258#define BS3_MODE_IS_64BIT_CODE(a_fMode) (((a_fMode) & BS3_MODE_CODE_MASK) == BS3_MODE_CODE_64)
259
260/** Whether the system is in real mode. */
261#define BS3_MODE_IS_RM_SYS(a_fMode) (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_RM)
262/** Whether the system is some 16-bit mode that isn't real mode. */
263#define BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode) ( ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE16 \
264 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP16 \
265 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE16)
266/** Whether the system is some 16-bit mode (includes real mode). */
267#define BS3_MODE_IS_16BIT_SYS(a_fMode) (BS3_MODE_IS_16BIT_SYS_NO_RM(a_fMode) || BS3_MODE_IS_RM_SYS(a_fMode))
268/** Whether the system is some 32-bit mode. */
269#define BS3_MODE_IS_32BIT_SYS(a_fMode) ( ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PE32 \
270 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PP32 \
271 || ((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_PAE32)
272/** Whether the system is long mode. */
273#define BS3_MODE_IS_64BIT_SYS(a_fMode) (((a_fMode) & BS3_MODE_SYS_MASK) == BS3_MODE_SYS_LM)
274
275/** Whether the system is in protected mode (with or without paging).
276 * @note Long mode is not included. */
277#define BS3_MODE_IS_PM_SYS(a_fMode) ((a_fMode) >= BS3_MODE_SYS_PE16 && (a_fMode) < BS3_MODE_SYS_LM)
278
279/** @todo testcase: How would long-mode handle a 16-bit TSS loaded prior to the switch? (mainly stack switching wise) Hopefully, it will tripple fault, right? */
280/** @} */
281
282
283/** @name BS3_ADDR_XXX - Static Memory Allocation
284 * @{ */
285/** The flat load address for the code after the bootsector. */
286#define BS3_ADDR_LOAD 0x10000
287/** Where we save the boot registers during init.
288 * Located right before the code. */
289#define BS3_ADDR_REG_SAVE (BS3_ADDR_LOAD - sizeof(BS3REGCTX) - 8)
290/** Where the stack starts (initial RSP value).
291 * Located 16 bytes (assumed by boot sector) before the saved registers.
292 * SS.BASE=0. The size is a little short of 32KB */
293#define BS3_ADDR_STACK (BS3_ADDR_REG_SAVE - 16)
294/** The ring-0 stack (8KB) for ring transitions. */
295#define BS3_ADDR_STACK_R0 0x06000
296/** The ring-1 stack (8KB) for ring transitions. */
297#define BS3_ADDR_STACK_R1 0x04000
298/** The ring-2 stack (8KB) for ring transitions. */
299#define BS3_ADDR_STACK_R2 0x02000
300/** IST1 ring-0 stack for long mode (4KB), used for double faults elsewhere. */
301#define BS3_ADDR_STACK_R0_IST1 0x09000
302/** IST2 ring-0 stack for long mode (3KB), used for spare 0 stack elsewhere. */
303#define BS3_ADDR_STACK_R0_IST2 0x08000
304/** IST3 ring-0 stack for long mode (1KB). */
305#define BS3_ADDR_STACK_R0_IST3 0x07400
306/** IST4 ring-0 stack for long mode (1KB), used for spare 1 stack elsewhere. */
307#define BS3_ADDR_STACK_R0_IST4 0x07000
308/** IST5 ring-0 stack for long mode (1KB). */
309#define BS3_ADDR_STACK_R0_IST5 0x06c00
310/** IST6 ring-0 stack for long mode (1KB). */
311#define BS3_ADDR_STACK_R0_IST6 0x06800
312/** IST7 ring-0 stack for long mode (1KB). */
313#define BS3_ADDR_STACK_R0_IST7 0x06400
314
315/** The base address of the BS3TEXT16 segment (same as BS3_LOAD_ADDR).
316 * @sa BS3_SEL_TEXT16 */
317#define BS3_ADDR_BS3TEXT16 0x10000
318/** The base address of the BS3SYSTEM16 segment.
319 * @sa BS3_SEL_SYSTEM16 */
320#define BS3_ADDR_BS3SYSTEM16 0x20000
321/** The base address of the BS3DATA16/BS3KIT_GRPNM_DATA16 segment.
322 * @sa BS3_SEL_DATA16 */
323#define BS3_ADDR_BS3DATA16 0x29000
324/** @} */
325
326/** @name BS3_SEL_XXX - GDT selector assignments.
327 *
328 * The real mode segment numbers for BS16TEXT, BS16DATA and BS16SYSTEM are
329 * present in the GDT, this allows the 16-bit C/C++ and assembly code to
330 * continue using the real mode segment values in ring-0 protected mode.
331 *
332 * The three segments have fixed locations:
333 * | segment | flat address | real mode segment |
334 * | ----------- | ------------ | ----------------- |
335 * | BS3TEXT16 | 0x00010000 | 1000h |
336 * | BS3SYSTEM16 | 0x00020000 | 2000h |
337 * | BS3DATA16 | 0x00029000 | 2900h |
338 *
339 * This means that we've got a lot of GDT space to play around with.
340 *
341 * @{ */
342#define BS3_SEL_LDT 0x0010 /**< The LDT selector for Bs3Ldt. */
343#define BS3_SEL_TSS16 0x0020 /**< The 16-bit TSS selector. */
344#define BS3_SEL_TSS16_DF 0x0028 /**< The 16-bit TSS selector for double faults. */
345#define BS3_SEL_TSS16_SPARE0 0x0030 /**< The 16-bit TSS selector for testing. */
346#define BS3_SEL_TSS16_SPARE1 0x0038 /**< The 16-bit TSS selector for testing. */
347#define BS3_SEL_TSS32 0x0040 /**< The 32-bit TSS selector. */
348#define BS3_SEL_TSS32_DF 0x0048 /**< The 32-bit TSS selector for double faults. */
349#define BS3_SEL_TSS32_SPARE0 0x0050 /**< The 32-bit TSS selector for testing. */
350#define BS3_SEL_TSS32_SPARE1 0x0058 /**< The 32-bit TSS selector for testing. */
351#define BS3_SEL_TSS32_IOBP_IRB 0x0060 /**< The 32-bit TSS selector with I/O permission and interrupt redirection bitmaps. */
352#define BS3_SEL_TSS32_IRB 0x0068 /**< The 32-bit TSS selector with only interrupt redirection bitmap (IOPB stripped by limit). */
353#define BS3_SEL_TSS64 0x0070 /**< The 64-bit TSS selector. */
354#define BS3_SEL_TSS64_SPARE0 0x0080 /**< The 64-bit TSS selector. */
355#define BS3_SEL_TSS64_SPARE1 0x0090 /**< The 64-bit TSS selector. */
356#define BS3_SEL_TSS64_IOBP 0x00a0 /**< The 64-bit TSS selector. */
357
358#define BS3_SEL_RMTEXT16_CS 0x00e0 /**< Conforming code selector for accessing the BS3RMTEXT16 segment. Runtime config. */
359#define BS3_SEL_X0TEXT16_CS 0x00e8 /**< Conforming code selector for accessing the BS3X0TEXT16 segment. Runtime config. */
360#define BS3_SEL_X1TEXT16_CS 0x00f0 /**< Conforming code selector for accessing the BS3X1TEXT16 segment. Runtime config. */
361#define BS3_SEL_VMMDEV_MMIO16 0x00f8 /**< Selector for accessing the VMMDev MMIO segment at 00df000h from 16-bit code. */
362
363/** Checks if @a uSel is in the BS3_SEL_RX_XXX range. */
364#define BS3_SEL_IS_IN_RING_RANGE(uSel) ( (unsigned)(uSel - BS3_SEL_R0_FIRST) < (unsigned)(4 << BS3_SEL_RING_SHIFT) )
365#define BS3_SEL_RING_SHIFT 8 /**< For the formula: BS3_SEL_R0_XXX + ((cs & 3) << BS3_SEL_RING_SHIFT) */
366#define BS3_SEL_RING_SUB_MASK 0x00f8 /**< Mask for getting the sub-selector. For use with BS3_SEL_R*_FIRST. */
367
368/** Checks if @a uSel is in the BS3_SEL_R0_XXX range. */
369#define BS3_SEL_IS_IN_R0_RANGE(uSel) ( (unsigned)(uSel - BS3_SEL_R0_FIRST) < (unsigned)(1 << BS3_SEL_RING_SHIFT) )
370#define BS3_SEL_R0_FIRST 0x0100 /**< The first selector in the ring-0 block. */
371#define BS3_SEL_R0_CS16 0x0100 /**< ring-0: 16-bit code selector, base 0x10000. */
372#define BS3_SEL_R0_DS16 0x0108 /**< ring-0: 16-bit data selector, base 0x23000. */
373#define BS3_SEL_R0_SS16 0x0110 /**< ring-0: 16-bit stack selector, base 0x00000. */
374#define BS3_SEL_R0_CS32 0x0118 /**< ring-0: 32-bit flat code selector. */
375#define BS3_SEL_R0_DS32 0x0120 /**< ring-0: 32-bit flat data selector. */
376#define BS3_SEL_R0_SS32 0x0128 /**< ring-0: 32-bit flat stack selector. */
377#define BS3_SEL_R0_CS64 0x0130 /**< ring-0: 64-bit flat code selector. */
378#define BS3_SEL_R0_DS64 0x0138 /**< ring-0: 64-bit flat data & stack selector. */
379#define BS3_SEL_R0_CS16_EO 0x0140 /**< ring-0: 16-bit execute-only code selector, not accessed, 0xfffe limit, CS16 base. */
380#define BS3_SEL_R0_CS16_CNF 0x0148 /**< ring-0: 16-bit conforming code selector, not accessed, 0xfffe limit, CS16 base. */
381#define BS3_SEL_R0_CS16_CNF_EO 0x0150 /**< ring-0: 16-bit execute-only conforming code selector, not accessed, 0xfffe limit, CS16 base. */
382#define BS3_SEL_R0_CS32_EO 0x0158 /**< ring-0: 32-bit execute-only code selector, not accessed, flat. */
383#define BS3_SEL_R0_CS32_CNF 0x0160 /**< ring-0: 32-bit conforming code selector, not accessed, flat. */
384#define BS3_SEL_R0_CS32_CNF_EO 0x0168 /**< ring-0: 32-bit execute-only conforming code selector, not accessed, flat. */
385#define BS3_SEL_R0_CS64_EO 0x0170 /**< ring-0: 64-bit execute-only code selector, not accessed, flat. */
386#define BS3_SEL_R0_CS64_CNF 0x0178 /**< ring-0: 64-bit conforming code selector, not accessed, flat. */
387#define BS3_SEL_R0_CS64_CNF_EO 0x0180 /**< ring-0: 64-bit execute-only conforming code selector, not accessed, flat. */
388
389#define BS3_SEL_R1_FIRST 0x0200 /**< The first selector in the ring-1 block. */
390#define BS3_SEL_R1_CS16 0x0200 /**< ring-1: 16-bit code selector, base 0x10000. */
391#define BS3_SEL_R1_DS16 0x0208 /**< ring-1: 16-bit data selector, base 0x23000. */
392#define BS3_SEL_R1_SS16 0x0210 /**< ring-1: 16-bit stack selector, base 0x00000. */
393#define BS3_SEL_R1_CS32 0x0218 /**< ring-1: 32-bit flat code selector. */
394#define BS3_SEL_R1_DS32 0x0220 /**< ring-1: 32-bit flat data selector. */
395#define BS3_SEL_R1_SS32 0x0228 /**< ring-1: 32-bit flat stack selector. */
396#define BS3_SEL_R1_CS64 0x0230 /**< ring-1: 64-bit flat code selector. */
397#define BS3_SEL_R1_DS64 0x0238 /**< ring-1: 64-bit flat data & stack selector. */
398#define BS3_SEL_R1_CS16_EO 0x0240 /**< ring-1: 16-bit execute-only code selector, not accessed, 0xfffe limit, CS16 base. */
399#define BS3_SEL_R1_CS16_CNF 0x0248 /**< ring-1: 16-bit conforming code selector, not accessed, 0xfffe limit, CS16 base. */
400#define BS3_SEL_R1_CS16_CNF_EO 0x0250 /**< ring-1: 16-bit execute-only conforming code selector, not accessed, 0xfffe limit, CS16 base. */
401#define BS3_SEL_R1_CS32_EO 0x0258 /**< ring-1: 32-bit execute-only code selector, not accessed, flat. */
402#define BS3_SEL_R1_CS32_CNF 0x0260 /**< ring-1: 32-bit conforming code selector, not accessed, flat. */
403#define BS3_SEL_R1_CS32_CNF_EO 0x0268 /**< ring-1: 32-bit execute-only conforming code selector, not accessed, flat. */
404#define BS3_SEL_R1_CS64_EO 0x0270 /**< ring-1: 64-bit execute-only code selector, not accessed, flat. */
405#define BS3_SEL_R1_CS64_CNF 0x0278 /**< ring-1: 64-bit conforming code selector, not accessed, flat. */
406#define BS3_SEL_R1_CS64_CNF_EO 0x0280 /**< ring-1: 64-bit execute-only conforming code selector, not accessed, flat. */
407
408#define BS3_SEL_R2_FIRST 0x0300 /**< The first selector in the ring-2 block. */
409#define BS3_SEL_R2_CS16 0x0300 /**< ring-2: 16-bit code selector, base 0x10000. */
410#define BS3_SEL_R2_DS16 0x0308 /**< ring-2: 16-bit data selector, base 0x23000. */
411#define BS3_SEL_R2_SS16 0x0310 /**< ring-2: 16-bit stack selector, base 0x00000. */
412#define BS3_SEL_R2_CS32 0x0318 /**< ring-2: 32-bit flat code selector. */
413#define BS3_SEL_R2_DS32 0x0320 /**< ring-2: 32-bit flat data selector. */
414#define BS3_SEL_R2_SS32 0x0328 /**< ring-2: 32-bit flat stack selector. */
415#define BS3_SEL_R2_CS64 0x0330 /**< ring-2: 64-bit flat code selector. */
416#define BS3_SEL_R2_DS64 0x0338 /**< ring-2: 64-bit flat data & stack selector. */
417#define BS3_SEL_R2_CS16_EO 0x0340 /**< ring-2: 16-bit execute-only code selector, not accessed, 0xfffe limit, CS16 base. */
418#define BS3_SEL_R2_CS16_CNF 0x0348 /**< ring-2: 16-bit conforming code selector, not accessed, 0xfffe limit, CS16 base. */
419#define BS3_SEL_R2_CS16_CNF_EO 0x0350 /**< ring-2: 16-bit execute-only conforming code selector, not accessed, 0xfffe limit, CS16 base. */
420#define BS3_SEL_R2_CS32_EO 0x0358 /**< ring-2: 32-bit execute-only code selector, not accessed, flat. */
421#define BS3_SEL_R2_CS32_CNF 0x0360 /**< ring-2: 32-bit conforming code selector, not accessed, flat. */
422#define BS3_SEL_R2_CS32_CNF_EO 0x0368 /**< ring-2: 32-bit execute-only conforming code selector, not accessed, flat. */
423#define BS3_SEL_R2_CS64_EO 0x0370 /**< ring-2: 64-bit execute-only code selector, not accessed, flat. */
424#define BS3_SEL_R2_CS64_CNF 0x0378 /**< ring-2: 64-bit conforming code selector, not accessed, flat. */
425#define BS3_SEL_R2_CS64_CNF_EO 0x0380 /**< ring-2: 64-bit execute-only conforming code selector, not accessed, flat. */
426
427#define BS3_SEL_R3_FIRST 0x0400 /**< The first selector in the ring-3 block. */
428#define BS3_SEL_R3_CS16 0x0400 /**< ring-3: 16-bit code selector, base 0x10000. */
429#define BS3_SEL_R3_DS16 0x0408 /**< ring-3: 16-bit data selector, base 0x23000. */
430#define BS3_SEL_R3_SS16 0x0410 /**< ring-3: 16-bit stack selector, base 0x00000. */
431#define BS3_SEL_R3_CS32 0x0418 /**< ring-3: 32-bit flat code selector. */
432#define BS3_SEL_R3_DS32 0x0420 /**< ring-3: 32-bit flat data selector. */
433#define BS3_SEL_R3_SS32 0x0428 /**< ring-3: 32-bit flat stack selector. */
434#define BS3_SEL_R3_CS64 0x0430 /**< ring-3: 64-bit flat code selector. */
435#define BS3_SEL_R3_DS64 0x0438 /**< ring-3: 64-bit flat data & stack selector. */
436#define BS3_SEL_R3_CS16_EO 0x0440 /**< ring-3: 16-bit execute-only code selector, not accessed, 0xfffe limit, CS16 base. */
437#define BS3_SEL_R3_CS16_CNF 0x0448 /**< ring-3: 16-bit conforming code selector, not accessed, 0xfffe limit, CS16 base. */
438#define BS3_SEL_R3_CS16_CNF_EO 0x0450 /**< ring-3: 16-bit execute-only conforming code selector, not accessed, 0xfffe limit, CS16 base. */
439#define BS3_SEL_R3_CS32_EO 0x0458 /**< ring-3: 32-bit execute-only code selector, not accessed, flat. */
440#define BS3_SEL_R3_CS32_CNF 0x0460 /**< ring-3: 32-bit conforming code selector, not accessed, flat. */
441#define BS3_SEL_R3_CS32_CNF_EO 0x0468 /**< ring-3: 32-bit execute-only conforming code selector, not accessed, flat. */
442#define BS3_SEL_R3_CS64_EO 0x0470 /**< ring-3: 64-bit execute-only code selector, not accessed, flat. */
443#define BS3_SEL_R3_CS64_CNF 0x0478 /**< ring-3: 64-bit conforming code selector, not accessed, flat. */
444#define BS3_SEL_R3_CS64_CNF_EO 0x0480 /**< ring-3: 64-bit execute-only conforming code selector, not accessed, flat. */
445
446#define BS3_SEL_R3_LAST 0x04f8 /**< ring-3: Last of the BS3_SEL_RX_XXX range. */
447
448#define BS3_SEL_SPARE_FIRST 0x0500 /**< The first selector in the spare block */
449#define BS3_SEL_SPARE_00 0x0500 /**< Spare selector number 00h. */
450#define BS3_SEL_SPARE_01 0x0508 /**< Spare selector number 01h. */
451#define BS3_SEL_SPARE_02 0x0510 /**< Spare selector number 02h. */
452#define BS3_SEL_SPARE_03 0x0518 /**< Spare selector number 03h. */
453#define BS3_SEL_SPARE_04 0x0520 /**< Spare selector number 04h. */
454#define BS3_SEL_SPARE_05 0x0528 /**< Spare selector number 05h. */
455#define BS3_SEL_SPARE_06 0x0530 /**< Spare selector number 06h. */
456#define BS3_SEL_SPARE_07 0x0538 /**< Spare selector number 07h. */
457#define BS3_SEL_SPARE_08 0x0540 /**< Spare selector number 08h. */
458#define BS3_SEL_SPARE_09 0x0548 /**< Spare selector number 09h. */
459#define BS3_SEL_SPARE_0a 0x0550 /**< Spare selector number 0ah. */
460#define BS3_SEL_SPARE_0b 0x0558 /**< Spare selector number 0bh. */
461#define BS3_SEL_SPARE_0c 0x0560 /**< Spare selector number 0ch. */
462#define BS3_SEL_SPARE_0d 0x0568 /**< Spare selector number 0dh. */
463#define BS3_SEL_SPARE_0e 0x0570 /**< Spare selector number 0eh. */
464#define BS3_SEL_SPARE_0f 0x0578 /**< Spare selector number 0fh. */
465#define BS3_SEL_SPARE_10 0x0580 /**< Spare selector number 10h. */
466#define BS3_SEL_SPARE_11 0x0588 /**< Spare selector number 11h. */
467#define BS3_SEL_SPARE_12 0x0590 /**< Spare selector number 12h. */
468#define BS3_SEL_SPARE_13 0x0598 /**< Spare selector number 13h. */
469#define BS3_SEL_SPARE_14 0x05a0 /**< Spare selector number 14h. */
470#define BS3_SEL_SPARE_15 0x05a8 /**< Spare selector number 15h. */
471#define BS3_SEL_SPARE_16 0x05b0 /**< Spare selector number 16h. */
472#define BS3_SEL_SPARE_17 0x05b8 /**< Spare selector number 17h. */
473#define BS3_SEL_SPARE_18 0x05c0 /**< Spare selector number 18h. */
474#define BS3_SEL_SPARE_19 0x05c8 /**< Spare selector number 19h. */
475#define BS3_SEL_SPARE_1a 0x05d0 /**< Spare selector number 1ah. */
476#define BS3_SEL_SPARE_1b 0x05d8 /**< Spare selector number 1bh. */
477#define BS3_SEL_SPARE_1c 0x05e0 /**< Spare selector number 1ch. */
478#define BS3_SEL_SPARE_1d 0x05e8 /**< Spare selector number 1dh. */
479#define BS3_SEL_SPARE_1e 0x05f0 /**< Spare selector number 1eh. */
480#define BS3_SEL_SPARE_1f 0x05f8 /**< Spare selector number 1fh. */
481
482#define BS3_SEL_TILED 0x0600 /**< 16-bit data tiling: First - base=0x00000000, limit=64KB, DPL=3. */
483#define BS3_SEL_TILED_LAST 0x0df8 /**< 16-bit data tiling: Last - base=0x00ff0000, limit=64KB, DPL=3. */
484#define BS3_SEL_TILED_AREA_SIZE 0x001000000 /**< 16-bit data tiling: Size of addressable area, in bytes. (16 MB) */
485
486#define BS3_SEL_FREE_PART1 0x0e00 /**< Free selector space - part \#1. */
487#define BS3_SEL_FREE_PART1_LAST 0x0ff8 /**< Free selector space - part \#1, last entry. */
488
489#define BS3_SEL_TEXT16 0x1000 /**< The BS3TEXT16 selector. */
490
491#define BS3_SEL_FREE_PART2 0x1008 /**< Free selector space - part \#2. */
492#define BS3_SEL_FREE_PART2_LAST 0x17f8 /**< Free selector space - part \#2, last entry. */
493
494#define BS3_SEL_TILED_R0 0x1800 /**< 16-bit data/stack tiling: First - base=0x00000000, limit=64KB, DPL=0. */
495#define BS3_SEL_TILED_R0_LAST 0x1ff8 /**< 16-bit data/stack tiling: Last - base=0x00ff0000, limit=64KB, DPL=0. */
496
497#define BS3_SEL_SYSTEM16 0x2000 /**< The BS3SYSTEM16 selector. */
498
499#define BS3_SEL_FREE_PART3 0x2008 /**< Free selector space - part \#3. */
500#define BS3_SEL_FREE_PART3_LAST 0x28f8 /**< Free selector space - part \#3, last entry. */
501
502#define BS3_SEL_DATA16 0x2900 /**< The BS3DATA16/BS3KIT_GRPNM_DATA16 selector. */
503
504#define BS3_SEL_HIGH16_CS_00 0x2908 /**< 16-bit CS for high DLLs. */
505#define BS3_SEL_HIGH16_CS_01 0x2910 /**< 16-bit CS for high DLLs. */
506#define BS3_SEL_HIGH16_CS_02 0x2918 /**< 16-bit CS for high DLLs. */
507#define BS3_SEL_HIGH16_CS_03 0x2920 /**< 16-bit CS for high DLLs. */
508#define BS3_SEL_HIGH16_CS_04 0x2928 /**< 16-bit CS for high DLLs. */
509#define BS3_SEL_HIGH16_CS_05 0x2930 /**< 16-bit CS for high DLLs. */
510#define BS3_SEL_HIGH16_CS_06 0x2938 /**< 16-bit CS for high DLLs. */
511#define BS3_SEL_HIGH16_CS_07 0x2940 /**< 16-bit CS for high DLLs. */
512#define BS3_SEL_HIGH16_CS_08 0x2948 /**< 16-bit CS for high DLLs. */
513#define BS3_SEL_HIGH16_CS_09 0x2950 /**< 16-bit CS for high DLLs. */
514#define BS3_SEL_HIGH16_CS_0a 0x2958 /**< 16-bit CS for high DLLs. */
515#define BS3_SEL_HIGH16_CS_0b 0x2960 /**< 16-bit CS for high DLLs. */
516#define BS3_SEL_HIGH16_CS_0c 0x2968 /**< 16-bit CS for high DLLs. */
517#define BS3_SEL_HIGH16_CS_0d 0x2970 /**< 16-bit CS for high DLLs. */
518#define BS3_SEL_HIGH16_CS_0e 0x2978 /**< 16-bit CS for high DLLs. */
519#define BS3_SEL_HIGH16_CS_0f 0x2980 /**< 16-bit CS for high DLLs. */
520#define BS3_SEL_HIGH16_CS_10 0x2988 /**< 16-bit CS for high DLLs. */
521#define BS3_SEL_HIGH16_CS_11 0x2990 /**< 16-bit CS for high DLLs. */
522#define BS3_SEL_HIGH16_CS_12 0x2998 /**< 16-bit CS for high DLLs. */
523#define BS3_SEL_HIGH16_CS_13 0x29a0 /**< 16-bit CS for high DLLs. */
524#define BS3_SEL_HIGH16_CS_14 0x29a8 /**< 16-bit CS for high DLLs. */
525#define BS3_SEL_HIGH16_CS_15 0x29b0 /**< 16-bit CS for high DLLs. */
526#define BS3_SEL_HIGH16_CS_16 0x29b8 /**< 16-bit CS for high DLLs. */
527#define BS3_SEL_HIGH16_CS_17 0x29c0 /**< 16-bit CS for high DLLs. */
528
529#define BS3_SEL_HIGH16_DS_00 0x29c8 /**< 16-bit DS for high DLLs. */
530#define BS3_SEL_HIGH16_DS_01 0x29d0 /**< 16-bit DS for high DLLs. */
531#define BS3_SEL_HIGH16_DS_02 0x29d8 /**< 16-bit DS for high DLLs. */
532#define BS3_SEL_HIGH16_DS_03 0x29e0 /**< 16-bit DS for high DLLs. */
533#define BS3_SEL_HIGH16_DS_04 0x29e8 /**< 16-bit DS for high DLLs. */
534#define BS3_SEL_HIGH16_DS_05 0x29f0 /**< 16-bit DS for high DLLs. */
535#define BS3_SEL_HIGH16_DS_06 0x29f8 /**< 16-bit DS for high DLLs. */
536#define BS3_SEL_HIGH16_DS_07 0x2a00 /**< 16-bit DS for high DLLs. */
537
538#define BS3_SEL_FREE_PART4 0x2a08 /**< Free selector space - part \#4. */
539#define BS3_SEL_FREE_PART4_LAST 0x2f98 /**< Free selector space - part \#4, last entry. */
540
541#define BS3_SEL_PRE_TEST_PAGE_08 0x2fa0 /**< Selector located 8 selectors before the test page. */
542#define BS3_SEL_PRE_TEST_PAGE_07 0x2fa8 /**< Selector located 7 selectors before the test page. */
543#define BS3_SEL_PRE_TEST_PAGE_06 0x2fb0 /**< Selector located 6 selectors before the test page. */
544#define BS3_SEL_PRE_TEST_PAGE_05 0x2fb8 /**< Selector located 5 selectors before the test page. */
545#define BS3_SEL_PRE_TEST_PAGE_04 0x2fc0 /**< Selector located 4 selectors before the test page. */
546#define BS3_SEL_PRE_TEST_PAGE_03 0x2fc8 /**< Selector located 3 selectors before the test page. */
547#define BS3_SEL_PRE_TEST_PAGE_02 0x2fd0 /**< Selector located 2 selectors before the test page. */
548#define BS3_SEL_PRE_TEST_PAGE_01 0x2fd8 /**< Selector located 1 selector before the test page. */
549#define BS3_SEL_TEST_PAGE 0x2fe0 /**< Start of the test page intended for playing around with paging and GDT. */
550#define BS3_SEL_TEST_PAGE_00 0x2fe0 /**< Test page selector number 00h (convenience). */
551#define BS3_SEL_TEST_PAGE_01 0x2fe8 /**< Test page selector number 01h (convenience). */
552#define BS3_SEL_TEST_PAGE_02 0x2ff0 /**< Test page selector number 02h (convenience). */
553#define BS3_SEL_TEST_PAGE_03 0x2ff8 /**< Test page selector number 03h (convenience). */
554#define BS3_SEL_TEST_PAGE_04 0x3000 /**< Test page selector number 04h (convenience). */
555#define BS3_SEL_TEST_PAGE_05 0x3008 /**< Test page selector number 05h (convenience). */
556#define BS3_SEL_TEST_PAGE_06 0x3010 /**< Test page selector number 06h (convenience). */
557#define BS3_SEL_TEST_PAGE_07 0x3018 /**< Test page selector number 07h (convenience). */
558#define BS3_SEL_TEST_PAGE_LAST 0x3fd0 /**< The last selector in the spare page. */
559
560#define BS3_SEL_GDT_LIMIT 0x3fd8 /**< The GDT limit. */
561/** @} */
562
563/** @name BS3_SEL_IS_XXX - Predicates for standard selectors.
564 *
565 * Standard selectors are in the range BS3_SEL_R0_FIRST thru BS3_SEL_R3_LAST.
566 *
567 * @{ */
568#define BS3_SEL_IS_CS16(a_uSel) (((a_uSel) & 0xf8) == 0x00)
569#define BS3_SEL_IS_CS32(a_uSel) (((a_uSel) & 0xf8) == 0x18)
570#define BS3_SEL_IS_CS64(a_uSel) (((a_uSel) & 0xf8) == 0x30)
571
572#define BS3_SEL_IS_ANY_CS16(a_uSel) ( ((a_uSel) & 0xf8) == 0x00 \
573 || ((a_uSel) & 0xf8) == 0x40 \
574 || ((a_uSel) & 0xf8) == 0x48 \
575 || ((a_uSel) & 0xf8) == 0x50 )
576#define BS3_SEL_IS_ANY_CS32(a_uSel) ( ((a_uSel) & 0xf8) == 0x18 \
577 || ((a_uSel) & 0xf8) == 0x58 \
578 || ((a_uSel) & 0xf8) == 0x60 \
579 || ((a_uSel) & 0xf8) == 0x68 )
580#define BS3_SEL_IS_ANY_CS64(a_uSel) ( ((a_uSel) & 0xf8) == 0x18 \
581 || ((a_uSel) & 0xf8) == 0x58 \
582 || ((a_uSel) & 0xf8) == 0x60 \
583 || ((a_uSel) & 0xf8) == 0x68 )
584
585#define BS3_SEL_IS_DS16(a_uSel) (((a_uSel) & 0xf8) == 0x08)
586#define BS3_SEL_IS_DS32(a_uSel) (((a_uSel) & 0xf8) == 0x20)
587#define BS3_SEL_IS_DS64(a_uSel) (((a_uSel) & 0xf8) == 0x38)
588
589#define BS3_SEL_IS_SS16(a_uSel) (((a_uSel) & 0xf8) == 0x10)
590#define BS3_SEL_IS_SS32(a_uSel) (((a_uSel) & 0xf8) == 0x28)
591/** @} */
592
593
594/** @def BS3_FAR
595 * For indicating far pointers in 16-bit code.
596 * Does nothing in 32-bit and 64-bit code. */
597/** @def BS3_NEAR
598 * For indicating near pointers in 16-bit code.
599 * Does nothing in 32-bit and 64-bit code. */
600/** @def BS3_FAR_CODE
601 * For indicating far 16-bit functions.
602 * Does nothing in 32-bit and 64-bit code. */
603/** @def BS3_NEAR_CODE
604 * For indicating near 16-bit functions.
605 * Does nothing in 32-bit and 64-bit code. */
606/** @def BS3_FAR_DATA
607 * For indicating far 16-bit external data, i.e. in a segment other than DATA16.
608 * Does nothing in 32-bit and 64-bit code. */
609#ifdef M_I86
610# define BS3_FAR __far
611# define BS3_NEAR __near
612# define BS3_FAR_CODE __far
613# define BS3_NEAR_CODE __near
614# define BS3_FAR_DATA __far
615#else
616# define BS3_FAR
617# define BS3_NEAR
618# define BS3_FAR_CODE
619# define BS3_NEAR_CODE
620# define BS3_FAR_DATA
621#endif
622
623#if ARCH_BITS == 16 || defined(DOXYGEN_RUNNING)
624/** @def BS3_FP_SEG
625 * Get the selector (segment) part of a far pointer.
626 *
627 * @returns selector.
628 * @param a_pv Far pointer.
629 */
630# define BS3_FP_SEG(a_pv) ((uint16_t)(__segment)(void BS3_FAR *)(a_pv))
631/** @def BS3_FP_OFF
632 * Get the segment offset part of a far pointer.
633 *
634 * For sake of convenience, this works like a uintptr_t cast in 32-bit and
635 * 64-bit code.
636 *
637 * @returns offset.
638 * @param a_pv Far pointer.
639 */
640# define BS3_FP_OFF(a_pv) ((uint16_t)(void __near *)(a_pv))
641/** @def BS3_FP_MAKE
642 * Create a far pointer.
643 *
644 * @returns Far pointer.
645 * @param a_uSeg The selector/segment.
646 * @param a_off The offset into the segment.
647 */
648# define BS3_FP_MAKE(a_uSeg, a_off) (((__segment)(a_uSeg)) :> ((void __near *)(a_off)))
649#else
650# define BS3_FP_OFF(a_pv) ((uintptr_t)(a_pv))
651#endif
652
653/**
654 * Converts a far real mode address to a 32-bit flat address.
655 *
656 * @returns Flat address.
657 * @param a_pv The _real_ _mode_ far pointer to convert.
658 */
659#define BS3_FP_REAL_TO_FLAT(a_pv) (((uint32_t)BS3_FP_SEG(a_pv) << 4) + BS3_FP_OFF(a_pv))
660
661/** @def BS3_MAKE_PROT_R0PTR_FROM_FLAT
662 * Creates a protected mode pointer from a flat address.
663 *
664 * For sake of convenience, this macro also works in 32-bit and 64-bit mode,
665 * only there it doesn't return a far pointer but a flat point.
666 *
667 * @returns far void pointer if 16-bit code, near/flat void pointer in 32-bit
668 * and 64-bit.
669 * @param a_uFlat Flat address in the first 16MB. */
670#if ARCH_BITS == 16
671# define BS3_MAKE_PROT_R0PTR_FROM_FLAT(a_uFlat) \
672 BS3_FP_MAKE(((uint16_t)(a_uFlat >> 16) << 3) + BS3_SEL_TILED, (uint16_t)(a_uFlat))
673#else
674# define BS3_MAKE_PROT_R0PTR_FROM_FLAT(a_uFlat) ((void *)(uintptr_t)(a_uFlat))
675#endif
676
677/** @def BS3_MAKE_PROT_R0PTR_FROM_REAL
678 * Creates a protected mode pointer from a far real mode address.
679 *
680 * For sake of convenience, this macro also works in 32-bit and 64-bit mode,
681 * only there it doesn't return a far pointer but a flat point.
682 *
683 * @returns far void pointer if 16-bit code, near/flat void pointer in 32-bit
684 * and 64-bit.
685 * @param a_uSeg The selector/segment.
686 * @param a_off The offset into the segment.
687 */
688#if ARCH_BITS == 16
689# define BS3_MAKE_PROT_R0PTR_FROM_REAL(a_uSeg, a_off) BS3_MAKE_PROT_R0PTR_FROM_FLAT(((uint32_t)(a_uSeg) << 4) + (uint16_t)(a_off))
690#else
691# define BS3_MAKE_PROT_R0PTR_FROM_REAL(a_uSeg, a_off) ( (void *)(uintptr_t)(((uint32_t)(a_uSeg) << 4) + (uint16_t)(a_off)) )
692#endif
693
694
695/** @def BS3_CALL
696 * The calling convension used by BS3 functions. */
697#if ARCH_BITS != 64
698# define BS3_CALL __cdecl
699#elif !defined(_MSC_VER)
700# define BS3_CALL __attribute__((__ms_abi__))
701#else
702# define BS3_CALL
703#endif
704
705/** @def IN_BS3KIT
706 * Indicates that we're in the same link job as the BS3Kit code. */
707#ifdef DOXYGEN_RUNNING
708# define IN_BS3KIT
709#endif
710
711/** @def BS3_DECL
712 * Declares a BS3Kit function with default far/near.
713 *
714 * Until we outgrow BS3TEXT16, we use all near functions in 16-bit.
715 *
716 * @param a_Type The return type. */
717#if ARCH_BITS != 16 || !defined(BS3_USE_ALT_16BIT_TEXT_SEG)
718# define BS3_DECL(a_Type) BS3_DECL_NEAR(a_Type)
719#else
720# define BS3_DECL(a_Type) BS3_DECL_FAR(a_Type)
721#endif
722
723/** @def BS3_DECL_NEAR
724 * Declares a BS3Kit function, always near everywhere.
725 *
726 * Until we outgrow BS3TEXT16, we use all near functions in 16-bit.
727 *
728 * @param a_Type The return type. */
729#ifdef IN_BS3KIT
730# define BS3_DECL_NEAR(a_Type) DECLEXPORT(a_Type) BS3_NEAR_CODE BS3_CALL
731#else
732# define BS3_DECL_NEAR(a_Type) DECLIMPORT(a_Type) BS3_NEAR_CODE BS3_CALL
733#endif
734
735/** @def BS3_DECL_FAR
736 * Declares a BS3Kit function, far 16-bit, otherwise near.
737 *
738 * Until we outgrow BS3TEXT16, we use all near functions in 16-bit.
739 *
740 * @param a_Type The return type. */
741#ifdef IN_BS3KIT
742# define BS3_DECL_FAR(a_Type) DECLEXPORT(a_Type) BS3_FAR_CODE BS3_CALL
743#else
744# define BS3_DECL_FAR(a_Type) DECLIMPORT(a_Type) BS3_FAR_CODE BS3_CALL
745#endif
746
747/** @def BS3_DECL_CALLBACK
748 * Declares a BS3Kit callback function (typically static).
749 *
750 * @param a_Type The return type. */
751#ifdef IN_BS3KIT
752# define BS3_DECL_CALLBACK(a_Type) a_Type BS3_FAR_CODE BS3_CALL
753#else
754# define BS3_DECL_CALLBACK(a_Type) a_Type BS3_FAR_CODE BS3_CALL
755#endif
756
757/** @def BS3_DECL_NEAR_CALLBACK
758 * Declares a near BS3Kit callback function (typically static).
759 *
760 * 16-bit users must be in CGROUP16!
761 *
762 * @param a_Type The return type. */
763#ifdef IN_BS3KIT
764# define BS3_DECL_NEAR_CALLBACK(a_Type) a_Type BS3_NEAR_CODE BS3_CALL
765#else
766# define BS3_DECL_NEAR_CALLBACK(a_Type) a_Type BS3_NEAR_CODE BS3_CALL
767#endif
768
769/**
770 * Constructs a common name.
771 *
772 * Example: BS3_CMN_NM(Bs3Shutdown)
773 *
774 * @param a_Name The name of the function or global variable.
775 */
776#define BS3_CMN_NM(a_Name) RT_CONCAT3(a_Name,_c,ARCH_BITS)
777
778/**
779 * Constructs a common function name, far in 16-bit code.
780 *
781 * Example: BS3_CMN_FAR_NM(Bs3Shutdown)
782 *
783 * @param a_Name The name of the function.
784 */
785#if ARCH_BITS == 16
786# define BS3_CMN_FAR_NM(a_Name) RT_CONCAT(a_Name,_f16)
787#else
788# define BS3_CMN_FAR_NM(a_Name) RT_CONCAT3(a_Name,_c,ARCH_BITS)
789#endif
790
791/**
792 * Constructs a common function name, far or near as defined by the source.
793 *
794 * Which to use in 16-bit mode is defined by BS3_USE_ALT_16BIT_TEXT_SEG. In
795 * 32-bit and 64-bit mode there are no far symbols, only near ones.
796 *
797 * Example: BS3_CMN_FN_NM(Bs3Shutdown)
798 *
799 * @param a_Name The name of the function.
800 */
801#if ARCH_BITS != 16 || !defined(BS3_USE_ALT_16BIT_TEXT_SEG)
802# define BS3_CMN_FN_NM(a_Name) BS3_CMN_NM(a_Name)
803#else
804# define BS3_CMN_FN_NM(a_Name) BS3_CMN_FAR_NM(a_Name)
805#endif
806
807
808/**
809 * Constructs a data name.
810 *
811 * This glosses over the underscore prefix usage of our 16-bit, 32-bit and
812 * 64-bit compilers.
813 *
814 * Example: @code{.c}
815 * \#define Bs3Gdt BS3_DATA_NM(Bs3Gdt)
816 * extern X86DESC BS3_FAR_DATA Bs3Gdt
817 * @endcode
818 *
819 * @param a_Name The name of the global variable.
820 * @remarks Mainly used in bs3kit-mangling.h, internal headers and templates.
821 */
822//converter does this now//#if ARCH_BITS == 64
823//converter does this now//# define BS3_DATA_NM(a_Name) RT_CONCAT(_,a_Name)
824//converter does this now//#else
825# define BS3_DATA_NM(a_Name) a_Name
826//converter does this now//#endif
827
828/**
829 * Template for creating a pointer union type.
830 * @param a_BaseName The base type name.
831 * @param a_Modifiers The type modifier.
832 */
833#define BS3_PTR_UNION_TEMPLATE(a_BaseName, a_Modifiers) \
834 typedef union a_BaseName \
835 { \
836 /** Pointer into the void. */ \
837 a_Modifiers void BS3_FAR *pv; \
838 /** As a signed integer. */ \
839 intptr_t i; \
840 /** As an unsigned integer. */ \
841 uintptr_t u; \
842 /** Pointer to char value. */ \
843 a_Modifiers char BS3_FAR *pch; \
844 /** Pointer to char value. */ \
845 a_Modifiers unsigned char BS3_FAR *puch; \
846 /** Pointer to a int value. */ \
847 a_Modifiers int BS3_FAR *pi; \
848 /** Pointer to a unsigned int value. */ \
849 a_Modifiers unsigned int BS3_FAR *pu; \
850 /** Pointer to a long value. */ \
851 a_Modifiers long BS3_FAR *pl; \
852 /** Pointer to a long value. */ \
853 a_Modifiers unsigned long BS3_FAR *pul; \
854 /** Pointer to a memory size value. */ \
855 a_Modifiers size_t BS3_FAR *pcb; \
856 /** Pointer to a byte value. */ \
857 a_Modifiers uint8_t BS3_FAR *pb; \
858 /** Pointer to a 8-bit unsigned value. */ \
859 a_Modifiers uint8_t BS3_FAR *pu8; \
860 /** Pointer to a 16-bit unsigned value. */ \
861 a_Modifiers uint16_t BS3_FAR *pu16; \
862 /** Pointer to a 32-bit unsigned value. */ \
863 a_Modifiers uint32_t BS3_FAR *pu32; \
864 /** Pointer to a 64-bit unsigned value. */ \
865 a_Modifiers uint64_t BS3_FAR *pu64; \
866 /** Pointer to a UTF-16 character. */ \
867 a_Modifiers RTUTF16 BS3_FAR *pwc; \
868 /** Pointer to a UUID character. */ \
869 a_Modifiers RTUUID BS3_FAR *pUuid; \
870 } a_BaseName; \
871 /** Pointer to a pointer union. */ \
872 typedef a_BaseName *RT_CONCAT(P,a_BaseName)
873BS3_PTR_UNION_TEMPLATE(BS3PTRUNION, RT_NOTHING);
874BS3_PTR_UNION_TEMPLATE(BS3CPTRUNION, const);
875BS3_PTR_UNION_TEMPLATE(BS3VPTRUNION, volatile);
876BS3_PTR_UNION_TEMPLATE(BS3CVPTRUNION, const volatile);
877
878/** Generic far function type. */
879typedef BS3_DECL_FAR(void) FNBS3FAR(void);
880/** Generic far function pointer type. */
881typedef FNBS3FAR *FPFNBS3FAR;
882
883/** Generic near function type. */
884typedef BS3_DECL_NEAR(void) FNBS3NEAR(void);
885/** Generic near function pointer type. */
886typedef FNBS3NEAR *PFNBS3NEAR;
887
888/** Generic far 16:16 function pointer type for address conversion functions. */
889#if ARCH_BITS == 16
890typedef FPFNBS3FAR PFNBS3FARADDRCONV;
891#else
892typedef uint32_t PFNBS3FARADDRCONV;
893#endif
894
895/** The system call vector. */
896#define BS3_TRAP_SYSCALL UINT8_C(0x20)
897
898/** @name System call numbers (ax).
899 * Paramenters are generally passed in registers specific to each system call,
900 * however cx:xSI is used for passing a pointer parameter.
901 * @{ */
902/** Print char (cl). */
903#define BS3_SYSCALL_PRINT_CHR UINT16_C(0x0001)
904/** Print string (pointer in cx:xSI, length in dx). */
905#define BS3_SYSCALL_PRINT_STR UINT16_C(0x0002)
906/** Switch to ring-0. */
907#define BS3_SYSCALL_TO_RING0 UINT16_C(0x0003)
908/** Switch to ring-1. */
909#define BS3_SYSCALL_TO_RING1 UINT16_C(0x0004)
910/** Switch to ring-2. */
911#define BS3_SYSCALL_TO_RING2 UINT16_C(0x0005)
912/** Switch to ring-3. */
913#define BS3_SYSCALL_TO_RING3 UINT16_C(0x0006)
914/** Restore context (pointer in cx:xSI, flags in dx). */
915#define BS3_SYSCALL_RESTORE_CTX UINT16_C(0x0007)
916/** Set DRx register (value in ESI, register number in dl). */
917#define BS3_SYSCALL_SET_DRX UINT16_C(0x0008)
918/** Get DRx register (register number in dl, value returned in ax:dx). */
919#define BS3_SYSCALL_GET_DRX UINT16_C(0x0009)
920/** Set CRx register (value in ESI, register number in dl). */
921#define BS3_SYSCALL_SET_CRX UINT16_C(0x000a)
922/** Get CRx register (register number in dl, value returned in ax:dx). */
923#define BS3_SYSCALL_GET_CRX UINT16_C(0x000b)
924/** Set the task register (value in ESI). */
925#define BS3_SYSCALL_SET_TR UINT16_C(0x000c)
926/** Get the task register (value returned in ax). */
927#define BS3_SYSCALL_GET_TR UINT16_C(0x000d)
928/** Set the LDT register (value in ESI). */
929#define BS3_SYSCALL_SET_LDTR UINT16_C(0x000e)
930/** Get the LDT register (value returned in ax). */
931#define BS3_SYSCALL_GET_LDTR UINT16_C(0x000f)
932/** Set XCR0 register (value in edx:esi). */
933#define BS3_SYSCALL_SET_XCR0 UINT16_C(0x0010)
934/** Get XCR0 register (value returned in edx:eax). */
935#define BS3_SYSCALL_GET_XCR0 UINT16_C(0x0011)
936/** The last system call value. */
937#define BS3_SYSCALL_LAST BS3_SYSCALL_GET_XCR0
938/** @} */
939
940
941
942/** @defgroup grp_bs3kit_system System Structures
943 * @{ */
944/** The GDT, indexed by BS3_SEL_XXX shifted by 3. */
945extern X86DESC BS3_FAR_DATA Bs3Gdt[(BS3_SEL_GDT_LIMIT + 1) / 8];
946
947extern X86DESC64 BS3_FAR_DATA Bs3Gdt_Ldt; /**< @see BS3_SEL_LDT */
948extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss16; /**< @see BS3_SEL_TSS16 */
949extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss16DoubleFault; /**< @see BS3_SEL_TSS16_DF */
950extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss16Spare0; /**< @see BS3_SEL_TSS16_SPARE0 */
951extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss16Spare1; /**< @see BS3_SEL_TSS16_SPARE1 */
952extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32; /**< @see BS3_SEL_TSS32 */
953extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32DoubleFault; /**< @see BS3_SEL_TSS32_DF */
954extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32Spare0; /**< @see BS3_SEL_TSS32_SPARE0 */
955extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32Spare1; /**< @see BS3_SEL_TSS32_SPARE1 */
956extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32IobpIntRedirBm; /**< @see BS3_SEL_TSS32_IOBP_IRB */
957extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss32IntRedirBm; /**< @see BS3_SEL_TSS32_IRB */
958extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss64; /**< @see BS3_SEL_TSS64 */
959extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss64Spare0; /**< @see BS3_SEL_TSS64_SPARE0 */
960extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss64Spare1; /**< @see BS3_SEL_TSS64_SPARE1 */
961extern X86DESC BS3_FAR_DATA Bs3Gdte_Tss64Iobp; /**< @see BS3_SEL_TSS64_IOBP */
962extern X86DESC BS3_FAR_DATA Bs3Gdte_RMTEXT16_CS; /**< @see BS3_SEL_RMTEXT16_CS */
963extern X86DESC BS3_FAR_DATA Bs3Gdte_X0TEXT16_CS; /**< @see BS3_SEL_X0TEXT16_CS */
964extern X86DESC BS3_FAR_DATA Bs3Gdte_X1TEXT16_CS; /**< @see BS3_SEL_X1TEXT16_CS */
965extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_MMIO16; /**< @see BS3_SEL_VMMDEV_MMIO16 */
966
967extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_First; /**< @see BS3_SEL_R0_FIRST */
968extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS16; /**< @see BS3_SEL_R0_CS16 */
969extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_DS16; /**< @see BS3_SEL_R0_DS16 */
970extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_SS16; /**< @see BS3_SEL_R0_SS16 */
971extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS32; /**< @see BS3_SEL_R0_CS32 */
972extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_DS32; /**< @see BS3_SEL_R0_DS32 */
973extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_SS32; /**< @see BS3_SEL_R0_SS32 */
974extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS64; /**< @see BS3_SEL_R0_CS64 */
975extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_DS64; /**< @see BS3_SEL_R0_DS64 */
976extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS16_EO; /**< @see BS3_SEL_R0_CS16_EO */
977extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS16_CNF; /**< @see BS3_SEL_R0_CS16_CNF */
978extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS16_CND_EO; /**< @see BS3_SEL_R0_CS16_CNF_EO */
979extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS32_EO; /**< @see BS3_SEL_R0_CS32_EO */
980extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS32_CNF; /**< @see BS3_SEL_R0_CS32_CNF */
981extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS32_CNF_EO; /**< @see BS3_SEL_R0_CS32_CNF_EO */
982extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS64_EO; /**< @see BS3_SEL_R0_CS64_EO */
983extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS64_CNF; /**< @see BS3_SEL_R0_CS64_CNF */
984extern X86DESC BS3_FAR_DATA Bs3Gdte_R0_CS64_CNF_EO; /**< @see BS3_SEL_R0_CS64_CNF_EO */
985
986extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_First; /**< @see BS3_SEL_R1_FIRST */
987extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS16; /**< @see BS3_SEL_R1_CS16 */
988extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_DS16; /**< @see BS3_SEL_R1_DS16 */
989extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_SS16; /**< @see BS3_SEL_R1_SS16 */
990extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS32; /**< @see BS3_SEL_R1_CS32 */
991extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_DS32; /**< @see BS3_SEL_R1_DS32 */
992extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_SS32; /**< @see BS3_SEL_R1_SS32 */
993extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS64; /**< @see BS3_SEL_R1_CS64 */
994extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_DS64; /**< @see BS3_SEL_R1_DS64 */
995extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS16_EO; /**< @see BS3_SEL_R1_CS16_EO */
996extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS16_CNF; /**< @see BS3_SEL_R1_CS16_CNF */
997extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS16_CND_EO; /**< @see BS3_SEL_R1_CS16_CNF_EO */
998extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS32_EO; /**< @see BS3_SEL_R1_CS32_EO */
999extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS32_CNF; /**< @see BS3_SEL_R1_CS32_CNF */
1000extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS32_CNF_EO; /**< @see BS3_SEL_R1_CS32_CNF_EO */
1001extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS64_EO; /**< @see BS3_SEL_R1_CS64_EO */
1002extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS64_CNF; /**< @see BS3_SEL_R1_CS64_CNF */
1003extern X86DESC BS3_FAR_DATA Bs3Gdte_R1_CS64_CNF_EO; /**< @see BS3_SEL_R1_CS64_CNF_EO */
1004
1005extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_First; /**< @see BS3_SEL_R2_FIRST */
1006extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS16; /**< @see BS3_SEL_R2_CS16 */
1007extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_DS16; /**< @see BS3_SEL_R2_DS16 */
1008extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_SS16; /**< @see BS3_SEL_R2_SS16 */
1009extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS32; /**< @see BS3_SEL_R2_CS32 */
1010extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_DS32; /**< @see BS3_SEL_R2_DS32 */
1011extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_SS32; /**< @see BS3_SEL_R2_SS32 */
1012extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS64; /**< @see BS3_SEL_R2_CS64 */
1013extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_DS64; /**< @see BS3_SEL_R2_DS64 */
1014extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS16_EO; /**< @see BS3_SEL_R2_CS16_EO */
1015extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS16_CNF; /**< @see BS3_SEL_R2_CS16_CNF */
1016extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS16_CND_EO; /**< @see BS3_SEL_R2_CS16_CNF_EO */
1017extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS32_EO; /**< @see BS3_SEL_R2_CS32_EO */
1018extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS32_CNF; /**< @see BS3_SEL_R2_CS32_CNF */
1019extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS32_CNF_EO; /**< @see BS3_SEL_R2_CS32_CNF_EO */
1020extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS64_EO; /**< @see BS3_SEL_R2_CS64_EO */
1021extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS64_CNF; /**< @see BS3_SEL_R2_CS64_CNF */
1022extern X86DESC BS3_FAR_DATA Bs3Gdte_R2_CS64_CNF_EO; /**< @see BS3_SEL_R2_CS64_CNF_EO */
1023
1024extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_First; /**< @see BS3_SEL_R3_FIRST */
1025extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS16; /**< @see BS3_SEL_R3_CS16 */
1026extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_DS16; /**< @see BS3_SEL_R3_DS16 */
1027extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_SS16; /**< @see BS3_SEL_R3_SS16 */
1028extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS32; /**< @see BS3_SEL_R3_CS32 */
1029extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_DS32; /**< @see BS3_SEL_R3_DS32 */
1030extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_SS32; /**< @see BS3_SEL_R3_SS32 */
1031extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS64; /**< @see BS3_SEL_R3_CS64 */
1032extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_DS64; /**< @see BS3_SEL_R3_DS64 */
1033extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS16_EO; /**< @see BS3_SEL_R3_CS16_EO */
1034extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS16_CNF; /**< @see BS3_SEL_R3_CS16_CNF */
1035extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS16_CND_EO; /**< @see BS3_SEL_R3_CS16_CNF_EO */
1036extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS32_EO; /**< @see BS3_SEL_R3_CS32_EO */
1037extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS32_CNF; /**< @see BS3_SEL_R3_CS32_CNF */
1038extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS32_CNF_EO; /**< @see BS3_SEL_R3_CS32_CNF_EO */
1039extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS64_EO; /**< @see BS3_SEL_R3_CS64_EO */
1040extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS64_CNF; /**< @see BS3_SEL_R3_CS64_CNF */
1041extern X86DESC BS3_FAR_DATA Bs3Gdte_R3_CS64_CNF_EO; /**< @see BS3_SEL_R3_CS64_CNF_EO */
1042
1043extern X86DESC BS3_FAR_DATA Bs3GdteSpare00; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_00 */
1044extern X86DESC BS3_FAR_DATA Bs3GdteSpare01; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_01 */
1045extern X86DESC BS3_FAR_DATA Bs3GdteSpare02; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_02 */
1046extern X86DESC BS3_FAR_DATA Bs3GdteSpare03; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_03 */
1047extern X86DESC BS3_FAR_DATA Bs3GdteSpare04; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_04 */
1048extern X86DESC BS3_FAR_DATA Bs3GdteSpare05; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_05 */
1049extern X86DESC BS3_FAR_DATA Bs3GdteSpare06; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_06 */
1050extern X86DESC BS3_FAR_DATA Bs3GdteSpare07; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_07 */
1051extern X86DESC BS3_FAR_DATA Bs3GdteSpare08; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_08 */
1052extern X86DESC BS3_FAR_DATA Bs3GdteSpare09; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_09 */
1053extern X86DESC BS3_FAR_DATA Bs3GdteSpare0a; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0a */
1054extern X86DESC BS3_FAR_DATA Bs3GdteSpare0b; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0b */
1055extern X86DESC BS3_FAR_DATA Bs3GdteSpare0c; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0c */
1056extern X86DESC BS3_FAR_DATA Bs3GdteSpare0d; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0d */
1057extern X86DESC BS3_FAR_DATA Bs3GdteSpare0e; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0e */
1058extern X86DESC BS3_FAR_DATA Bs3GdteSpare0f; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_0f */
1059extern X86DESC BS3_FAR_DATA Bs3GdteSpare10; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_10 */
1060extern X86DESC BS3_FAR_DATA Bs3GdteSpare11; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_11 */
1061extern X86DESC BS3_FAR_DATA Bs3GdteSpare12; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_12 */
1062extern X86DESC BS3_FAR_DATA Bs3GdteSpare13; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_13 */
1063extern X86DESC BS3_FAR_DATA Bs3GdteSpare14; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_14 */
1064extern X86DESC BS3_FAR_DATA Bs3GdteSpare15; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_15 */
1065extern X86DESC BS3_FAR_DATA Bs3GdteSpare16; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_16 */
1066extern X86DESC BS3_FAR_DATA Bs3GdteSpare17; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_17 */
1067extern X86DESC BS3_FAR_DATA Bs3GdteSpare18; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_18 */
1068extern X86DESC BS3_FAR_DATA Bs3GdteSpare19; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_19 */
1069extern X86DESC BS3_FAR_DATA Bs3GdteSpare1a; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1a */
1070extern X86DESC BS3_FAR_DATA Bs3GdteSpare1b; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1b */
1071extern X86DESC BS3_FAR_DATA Bs3GdteSpare1c; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1c */
1072extern X86DESC BS3_FAR_DATA Bs3GdteSpare1d; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1d */
1073extern X86DESC BS3_FAR_DATA Bs3GdteSpare1e; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1e */
1074extern X86DESC BS3_FAR_DATA Bs3GdteSpare1f; /**< GDT entry for playing with in testcases. @see BS3_SEL_SPARE_1f */
1075
1076/** GDTs setting up the tiled 16-bit access to the first 16 MBs of memory.
1077 * @see BS3_SEL_TILED, BS3_SEL_TILED_LAST, BS3_SEL_TILED_AREA_SIZE */
1078extern X86DESC BS3_FAR_DATA Bs3GdteTiled[256];
1079/** Free GDTes, part \#1. */
1080extern X86DESC BS3_FAR_DATA Bs3GdteFreePart1[64];
1081/** The BS3TEXT16/BS3CLASS16CODE GDT entry. @see BS3_SEL_TEXT16 */
1082extern X86DESC BS3_FAR_DATA Bs3Gdte_CODE16;
1083/** Free GDTes, part \#2. */
1084extern X86DESC BS3_FAR_DATA Bs3GdteFreePart2[511];
1085/** The BS3SYSTEM16 GDT entry. */
1086extern X86DESC BS3_FAR_DATA Bs3Gdte_SYSTEM16;
1087/** Free GDTes, part \#3. */
1088extern X86DESC BS3_FAR_DATA Bs3GdteFreePart3[223];
1089/** The BS3DATA16/BS3KIT_GRPNM_DATA16 GDT entry. */
1090extern X86DESC BS3_FAR_DATA Bs3Gdte_DATA16;
1091
1092/** 16-bit CSes for high DLLs. */
1093extern X86DESC BS3_FAR_DATA Bs3GdteHighDllCSes[24];
1094/** 16-bit DSes for high DLLs. */
1095extern X86DESC BS3_FAR_DATA Bs3GdteHighDllDSes[8];
1096
1097/** Free GDTes, part \#4. */
1098extern X86DESC BS3_FAR_DATA Bs3GdteFreePart4[179];
1099
1100extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage08; /**< GDT entry 8 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_08 */
1101extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage07; /**< GDT entry 7 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_07 */
1102extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage06; /**< GDT entry 6 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_06 */
1103extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage05; /**< GDT entry 5 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_05 */
1104extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage04; /**< GDT entry 4 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_04 */
1105extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage03; /**< GDT entry 3 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_03 */
1106extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage02; /**< GDT entry 2 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_02 */
1107extern X86DESC BS3_FAR_DATA Bs3GdtePreTestPage01; /**< GDT entry 1 selectors prior to the test page, testcase resource. @see BS3_SEL_PRE_TEST_PAGE_01 */
1108/** Array of GDT entries starting on a page boundrary and filling (almost) the
1109 * whole page. This is for playing with paging and GDT usage.
1110 * @see BS3_SEL_TEST_PAGE */
1111extern X86DESC BS3_FAR_DATA Bs3GdteTestPage[2043];
1112extern X86DESC BS3_FAR_DATA Bs3GdteTestPage00; /**< GDT entry 0 on the test page (convenience). @see BS3_SEL_TEST_PAGE_00 */
1113extern X86DESC BS3_FAR_DATA Bs3GdteTestPage01; /**< GDT entry 1 on the test page (convenience). @see BS3_SEL_TEST_PAGE_01 */
1114extern X86DESC BS3_FAR_DATA Bs3GdteTestPage02; /**< GDT entry 2 on the test page (convenience). @see BS3_SEL_TEST_PAGE_02 */
1115extern X86DESC BS3_FAR_DATA Bs3GdteTestPage03; /**< GDT entry 3 on the test page (convenience). @see BS3_SEL_TEST_PAGE_03 */
1116extern X86DESC BS3_FAR_DATA Bs3GdteTestPage04; /**< GDT entry 4 on the test page (convenience). @see BS3_SEL_TEST_PAGE_04 */
1117extern X86DESC BS3_FAR_DATA Bs3GdteTestPage05; /**< GDT entry 5 on the test page (convenience). @see BS3_SEL_TEST_PAGE_05 */
1118extern X86DESC BS3_FAR_DATA Bs3GdteTestPage06; /**< GDT entry 6 on the test page (convenience). @see BS3_SEL_TEST_PAGE_06 */
1119extern X86DESC BS3_FAR_DATA Bs3GdteTestPage07; /**< GDT entry 7 on the test page (convenience). @see BS3_SEL_TEST_PAGE_07 */
1120
1121/** The end of the GDT (exclusive - contains eye-catcher string). */
1122extern X86DESC BS3_FAR_DATA Bs3GdtEnd;
1123
1124/** The default 16-bit TSS. */
1125extern X86TSS16 BS3_FAR_DATA Bs3Tss16;
1126extern X86TSS16 BS3_FAR_DATA Bs3Tss16DoubleFault;
1127extern X86TSS16 BS3_FAR_DATA Bs3Tss16Spare0;
1128extern X86TSS16 BS3_FAR_DATA Bs3Tss16Spare1;
1129/** The default 32-bit TSS. */
1130extern X86TSS32 BS3_FAR_DATA Bs3Tss32;
1131extern X86TSS32 BS3_FAR_DATA Bs3Tss32DoubleFault;
1132extern X86TSS32 BS3_FAR_DATA Bs3Tss32Spare0;
1133extern X86TSS32 BS3_FAR_DATA Bs3Tss32Spare1;
1134/** The default 64-bit TSS. */
1135extern X86TSS64 BS3_FAR_DATA Bs3Tss64;
1136extern X86TSS64 BS3_FAR_DATA Bs3Tss64Spare0;
1137extern X86TSS64 BS3_FAR_DATA Bs3Tss64Spare1;
1138extern X86TSS64 BS3_FAR_DATA Bs3Tss64WithIopb;
1139extern X86TSS32 BS3_FAR_DATA Bs3Tss32WithIopb;
1140/** Interrupt redirection bitmap used by Bs3Tss32WithIopb. */
1141extern uint8_t BS3_FAR_DATA Bs3SharedIntRedirBm[32];
1142/** I/O permission bitmap used by Bs3Tss32WithIopb and Bs3Tss64WithIopb. */
1143extern uint8_t BS3_FAR_DATA Bs3SharedIobp[8192+2];
1144/** End of the I/O permission bitmap (exclusive). */
1145extern uint8_t BS3_FAR_DATA Bs3SharedIobpEnd;
1146/** 16-bit IDT. */
1147extern X86DESC BS3_FAR_DATA Bs3Idt16[256];
1148/** 32-bit IDT. */
1149extern X86DESC BS3_FAR_DATA Bs3Idt32[256];
1150/** 64-bit IDT. */
1151extern X86DESC64 BS3_FAR_DATA Bs3Idt64[256];
1152/** Structure for the LIDT instruction for loading the 16-bit IDT. */
1153extern X86XDTR64 BS3_FAR_DATA Bs3Lidt_Idt16;
1154/** Structure for the LIDT instruction for loading the 32-bit IDT. */
1155extern X86XDTR64 BS3_FAR_DATA Bs3Lidt_Idt32;
1156/** Structure for the LIDT instruction for loading the 64-bit IDT. */
1157extern X86XDTR64 BS3_FAR_DATA Bs3Lidt_Idt64;
1158/** Structure for the LIDT instruction for loading the real mode interrupt
1159 * vector table. */
1160extern X86XDTR64 BS3_FAR_DATA Bs3Lidt_Ivt;
1161/** Structure for the LGDT instruction for loading the current GDT. */
1162extern X86XDTR64 BS3_FAR_DATA Bs3Lgdt_Gdt;
1163/** Structure for the LGDT instruction for loading the default GDT. */
1164extern X86XDTR64 BS3_FAR_DATA Bs3LgdtDef_Gdt;
1165/** The LDT (all entries are empty, fill in for testing). */
1166extern X86DESC BS3_FAR_DATA Bs3Ldt[116];
1167/** The end of the LDT (exclusive). */
1168extern X86DESC BS3_FAR_DATA Bs3LdtEnd;
1169
1170/** @} */
1171
1172
1173/** @name Segment start and end markers, sizes.
1174 * @{ */
1175/** Start of the BS3TEXT16 segment. */
1176extern uint8_t BS3_FAR_DATA Bs3Text16_StartOfSegment;
1177/** End of the BS3TEXT16 segment. */
1178extern uint8_t BS3_FAR_DATA Bs3Text16_EndOfSegment;
1179/** The size of the BS3TEXT16 segment. */
1180extern uint16_t BS3_FAR_DATA Bs3Text16_Size;
1181
1182/** Start of the BS3SYSTEM16 segment. */
1183extern uint8_t BS3_FAR_DATA Bs3System16_StartOfSegment;
1184/** End of the BS3SYSTEM16 segment. */
1185extern uint8_t BS3_FAR_DATA Bs3System16_EndOfSegment;
1186
1187/** Start of the BS3DATA16/BS3KIT_GRPNM_DATA16 segment. */
1188extern uint8_t BS3_FAR_DATA Bs3Data16_StartOfSegment;
1189/** End of the BS3DATA16/BS3KIT_GRPNM_DATA16 segment. */
1190extern uint8_t BS3_FAR_DATA Bs3Data16_EndOfSegment;
1191
1192/** Start of the BS3RMTEXT16 segment. */
1193extern uint8_t BS3_FAR_DATA Bs3RmText16_StartOfSegment;
1194/** End of the BS3RMTEXT16 segment. */
1195extern uint8_t BS3_FAR_DATA Bs3RmText16_EndOfSegment;
1196/** The size of the BS3RMTEXT16 segment. */
1197extern uint16_t BS3_FAR_DATA Bs3RmText16_Size;
1198/** The flat start address of the BS3X0TEXT16 segment. */
1199extern uint32_t BS3_FAR_DATA Bs3RmText16_FlatAddr;
1200
1201/** Start of the BS3X0TEXT16 segment. */
1202extern uint8_t BS3_FAR_DATA Bs3X0Text16_StartOfSegment;
1203/** End of the BS3X0TEXT16 segment. */
1204extern uint8_t BS3_FAR_DATA Bs3X0Text16_EndOfSegment;
1205/** The size of the BS3X0TEXT16 segment. */
1206extern uint16_t BS3_FAR_DATA Bs3X0Text16_Size;
1207/** The flat start address of the BS3X0TEXT16 segment. */
1208extern uint32_t BS3_FAR_DATA Bs3X0Text16_FlatAddr;
1209
1210/** Start of the BS3X1TEXT16 segment. */
1211extern uint8_t BS3_FAR_DATA Bs3X1Text16_StartOfSegment;
1212/** End of the BS3X1TEXT16 segment. */
1213extern uint8_t BS3_FAR_DATA Bs3X1Text16_EndOfSegment;
1214/** The size of the BS3X1TEXT16 segment. */
1215extern uint16_t BS3_FAR_DATA Bs3X1Text16_Size;
1216/** The flat start address of the BS3X1TEXT16 segment. */
1217extern uint32_t BS3_FAR_DATA Bs3X1Text16_FlatAddr;
1218
1219/** Start of the BS3TEXT32 segment. */
1220extern uint8_t BS3_FAR_DATA Bs3Text32_StartOfSegment;
1221/** Start of the BS3TEXT32 segment. */
1222extern uint8_t BS3_FAR_DATA Bs3Text32_EndOfSegment;
1223
1224/** Start of the BS3DATA32 segment. */
1225extern uint8_t BS3_FAR_DATA Bs3Data32_StartOfSegment;
1226/** Start of the BS3DATA32 segment. */
1227extern uint8_t BS3_FAR_DATA Bs3Data32_EndOfSegment;
1228
1229/** Start of the BS3TEXT64 segment. */
1230extern uint8_t BS3_FAR_DATA Bs3Text64_StartOfSegment;
1231/** Start of the BS3TEXT64 segment. */
1232extern uint8_t BS3_FAR_DATA Bs3Text64_EndOfSegment;
1233
1234/** Start of the BS3DATA64 segment. */
1235extern uint8_t BS3_FAR_DATA Bs3Data64_StartOfSegment;
1236/** Start of the BS3DATA64 segment. */
1237extern uint8_t BS3_FAR_DATA Bs3Data64_EndOfSegment;
1238
1239/** The size of the Data16, Text32, Text64, Data32 and Data64 blob. */
1240extern uint32_t BS3_FAR_DATA Bs3Data16Thru64Text32And64_TotalSize;
1241/** The total image size (from Text16 thu Data64). */
1242extern uint32_t BS3_FAR_DATA Bs3TotalImageSize;
1243/** @} */
1244
1245
1246/** Lower case hex digits. */
1247extern char const g_achBs3HexDigits[16+1];
1248/** Upper case hex digits. */
1249extern char const g_achBs3HexDigitsUpper[16+1];
1250
1251
1252/** The current mode (BS3_MODE_XXX) of CPU \#0. */
1253extern uint8_t g_bBs3CurrentMode;
1254
1255/** Hint for 16-bit trap handlers regarding the high word of EIP. */
1256extern uint32_t g_uBs3TrapEipHint;
1257
1258/** Set to disable special V8086 \#GP and \#UD handling in Bs3TrapDefaultHandler.
1259 * This is useful for getting */
1260extern bool volatile g_fBs3TrapNoV86Assist;
1261
1262/** Copy of the original real-mode interrupt vector table. */
1263extern RTFAR16 g_aBs3RmIvtOriginal[256];
1264
1265
1266#ifdef __WATCOMC__
1267/**
1268 * Executes the SMSW instruction and returns the value.
1269 *
1270 * @returns Machine status word.
1271 */
1272uint16_t Bs3AsmSmsw(void);
1273# pragma aux Bs3AsmSmsw = \
1274 ".286" \
1275 "smsw ax" \
1276 value [ax] modify exact [ax] nomemory;
1277#endif
1278
1279
1280/** @defgroup bs3kit_cross_ptr Cross Context Pointer Type
1281 *
1282 * The cross context pointer type is
1283 *
1284 * @{ */
1285
1286/**
1287 * Cross context pointer base type.
1288 */
1289typedef union BS3XPTR
1290{
1291 /** The flat pointer. */
1292 uint32_t uFlat;
1293 /** 16-bit view. */
1294 struct
1295 {
1296 uint16_t uLow;
1297 uint16_t uHigh;
1298 } u;
1299#if ARCH_BITS == 16
1300 /** 16-bit near pointer. */
1301 void __near *pvNear;
1302#elif ARCH_BITS == 32
1303 /** 32-bit pointer. */
1304 void *pvRaw;
1305#endif
1306} BS3XPTR;
1307AssertCompileSize(BS3XPTR, 4);
1308
1309
1310/** @def BS3_XPTR_DEF_INTERNAL
1311 * Internal worker.
1312 *
1313 * @param a_Scope RT_NOTHING if structure or global, static or extern
1314 * otherwise.
1315 * @param a_Type The type we're pointing to.
1316 * @param a_Name The member or variable name.
1317 * @internal
1318 */
1319#if ARCH_BITS == 16
1320# define BS3_XPTR_DEF_INTERNAL(a_Scope, a_Type, a_Name) \
1321 a_Scope union \
1322 { \
1323 BS3XPTR XPtr; \
1324 a_Type __near *pNearTyped; \
1325 } a_Name
1326#elif ARCH_BITS == 32
1327# define BS3_XPTR_DEF_INTERNAL(a_Scope, a_Type, a_Name) \
1328 a_Scope union \
1329 { \
1330 BS3XPTR XPtr; \
1331 a_Type *pTyped; \
1332 } a_Name
1333#elif ARCH_BITS == 64
1334# define BS3_XPTR_DEF_INTERNAL(a_Scope, a_Type, a_Name) \
1335 a_Scope union \
1336 { \
1337 BS3XPTR XPtr; \
1338 } a_Name
1339#else
1340# error "ARCH_BITS"
1341#endif
1342
1343/** @def BS3_XPTR_MEMBER
1344 * Defines a pointer member that can be shared by all CPU modes.
1345 *
1346 * @param a_Type The type we're pointing to.
1347 * @param a_Name The member or variable name.
1348 */
1349#define BS3_XPTR_MEMBER(a_Type, a_Name) BS3_XPTR_DEF_INTERNAL(RT_NOTHING, a_Type, a_Name)
1350
1351/** @def BS3_XPTR_AUTO
1352 * Defines a pointer static variable for working with an XPTR.
1353 *
1354 * This is typically used to convert flat pointers into context specific
1355 * pointers.
1356 *
1357 * @param a_Type The type we're pointing to.
1358 * @param a_Name The member or variable name.
1359 */
1360#define BS3_XPTR_AUTO(a_Type, a_Name) BS3_XPTR_DEF_INTERNAL(RT_NOTHING, a_Type, a_Name)
1361
1362/** @def BS3_XPTR_SET_FLAT
1363 * Sets a cross context pointer.
1364 *
1365 * @param a_Type The type we're pointing to.
1366 * @param a_Name The member or variable name.
1367 * @param a_uFlatPtr The flat pointer value to assign. If the x-pointer is
1368 * used in real mode, this must be less than 1MB.
1369 * Otherwise the limit is 16MB (due to selector tiling).
1370 */
1371#define BS3_XPTR_SET_FLAT(a_Type, a_Name, a_uFlatPtr) \
1372 do { a_Name.XPtr.uFlat = (a_uFlatPtr); } while (0)
1373
1374/** @def BS3_XPTR_GET_FLAT
1375 * Gets the flat address of a cross context pointer.
1376 *
1377 * @returns 32-bit flat pointer.
1378 * @param a_Type The type we're pointing to.
1379 * @param a_Name The member or variable name.
1380 */
1381#define BS3_XPTR_GET_FLAT(a_Type, a_Name) (a_Name.XPtr.uFlat)
1382
1383/** @def BS3_XPTR_GET_FLAT_LOW
1384 * Gets the low 16 bits of the flat address.
1385 *
1386 * @returns Low 16 bits of the flat pointer.
1387 * @param a_Type The type we're pointing to.
1388 * @param a_Name The member or variable name.
1389 */
1390#define BS3_XPTR_GET_FLAT_LOW(a_Type, a_Name) (a_Name.XPtr.u.uLow)
1391
1392
1393#if ARCH_BITS == 16
1394
1395/**
1396 * Gets the current ring number.
1397 * @returns Ring number.
1398 */
1399DECLINLINE(uint16_t) Bs3Sel16GetCurRing(void);
1400# pragma aux Bs3Sel16GetCurRing = \
1401 "mov ax, ss" \
1402 "and ax, 3" \
1403 value [ax] modify exact [ax] nomemory;
1404
1405/**
1406 * Converts the high word of a flat pointer into a 16-bit selector.
1407 *
1408 * This makes use of the tiled area. It also handles real mode.
1409 *
1410 * @returns Segment selector value.
1411 * @param uHigh The high part of flat pointer.
1412 * @sa BS3_XPTR_GET, BS3_XPTR_SET
1413 */
1414DECLINLINE(__segment) Bs3Sel16HighFlatPtrToSelector(uint16_t uHigh)
1415{
1416 if (!BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode))
1417 return (__segment)(((uHigh << 3) + BS3_SEL_TILED) | Bs3Sel16GetCurRing());
1418 return (__segment)(uHigh << 12);
1419}
1420
1421#endif /* ARCH_BITS == 16 */
1422
1423/** @def BS3_XPTR_GET
1424 * Gets the current context pointer value.
1425 *
1426 * @returns Usable pointer.
1427 * @param a_Type The type we're pointing to.
1428 * @param a_Name The member or variable name.
1429 */
1430#if ARCH_BITS == 16
1431# define BS3_XPTR_GET(a_Type, a_Name) \
1432 ((a_Type BS3_FAR *)BS3_FP_MAKE(Bs3Sel16HighFlatPtrToSelector((a_Name).XPtr.u.uHigh), (a_Name).pNearTyped))
1433#elif ARCH_BITS == 32
1434# define BS3_XPTR_GET(a_Type, a_Name) ((a_Name).pTyped)
1435#elif ARCH_BITS == 64
1436# define BS3_XPTR_GET(a_Type, a_Name) ((a_Type *)(uintptr_t)(a_Name).XPtr.uFlat)
1437#else
1438# error "ARCH_BITS"
1439#endif
1440
1441/** @def BS3_XPTR_SET
1442 * Gets the current context pointer value.
1443 *
1444 * @returns Usable pointer.
1445 * @param a_Type The type we're pointing to.
1446 * @param a_Name The member or variable name.
1447 * @param a_pValue The new pointer value, current context pointer.
1448 */
1449#if ARCH_BITS == 16
1450# define BS3_XPTR_SET(a_Type, a_Name, a_pValue) \
1451 do { \
1452 a_Type BS3_FAR *pTypeCheck = (a_pValue); \
1453 if (BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode)) \
1454 (a_Name).XPtr.uFlat = BS3_FP_OFF(pTypeCheck) + ((uint32_t)BS3_FP_SEG(pTypeCheck) << 4); \
1455 else \
1456 { \
1457 (a_Name).XPtr.u.uLow = BS3_FP_OFF(pTypeCheck); \
1458 (a_Name).XPtr.u.uHigh = ((BS3_FP_SEG(pTypeCheck) & UINT16_C(0xfff8)) - BS3_SEL_TILED) >> 3; \
1459 } \
1460 } while (0)
1461#elif ARCH_BITS == 32
1462# define BS3_XPTR_SET(a_Type, a_Name, a_pValue) \
1463 do { (a_Name).pTyped = (a_pValue); } while (0)
1464#elif ARCH_BITS == 64
1465# define BS3_XPTR_SET(a_Type, a_Name, a_pValue) \
1466 do { \
1467 a_Type *pTypeCheck = (a_pValue); \
1468 (a_Name).XPtr.uFlat = (uint32_t)(uintptr_t)pTypeCheck; \
1469 } while (0)
1470#else
1471# error "ARCH_BITS"
1472#endif
1473
1474
1475/** @def BS3_XPTR_IS_NULL
1476 * Checks if the cross context pointer is NULL.
1477 *
1478 * @returns true if NULL, false if not.
1479 * @param a_Type The type we're pointing to.
1480 * @param a_Name The member or variable name.
1481 */
1482#define BS3_XPTR_IS_NULL(a_Type, a_Name) ((a_Name).XPtr.uFlat == 0)
1483
1484/**
1485 * Gets a working pointer from a flat address.
1486 *
1487 * @returns Current context pointer.
1488 * @param uFlatPtr The flat address to convert (32-bit or 64-bit).
1489 */
1490DECLINLINE(void BS3_FAR *) Bs3XptrFlatToCurrent(RTCCUINTXREG uFlatPtr)
1491{
1492 BS3_XPTR_AUTO(void, pTmp);
1493 BS3_XPTR_SET_FLAT(void, pTmp, uFlatPtr);
1494 return BS3_XPTR_GET(void, pTmp);
1495}
1496
1497/** @} */
1498
1499
1500
1501/** @defgroup grp_bs3kit_cmn Common Functions and Data
1502 *
1503 * The common functions comes in three variations: 16-bit, 32-bit and 64-bit.
1504 * Templated code uses the #BS3_CMN_NM macro to mangle the name according to the
1505 * desired
1506 *
1507 * @{
1508 */
1509
1510/** @def BS3_CMN_PROTO_INT
1511 * Internal macro for prototyping all the variations of a common function.
1512 * @param a_RetType The return type.
1513 * @param a_Name The function basename.
1514 * @param a_Params The parameter list (in parentheses).
1515 * @sa BS3_CMN_PROTO_STUB, BS3_CMN_PROTO_NOSB
1516 */
1517#if ARCH_BITS == 16
1518# ifndef BS3_USE_ALT_16BIT_TEXT_SEG
1519# define BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params) \
1520 BS3_DECL_NEAR(a_RetType) BS3_CMN_NM(a_Name) a_Params; \
1521 BS3_DECL_FAR(a_RetType) BS3_CMN_FAR_NM(a_Name) a_Params
1522# else
1523# define BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params) \
1524 BS3_DECL_FAR(a_RetType) BS3_CMN_FAR_NM(a_Name) a_Params
1525# endif
1526#else
1527# define BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params) \
1528 BS3_DECL_NEAR(a_RetType) BS3_CMN_NM(a_Name) a_Params
1529#endif
1530
1531/** @def BS3_CMN_PROTO_STUB
1532 * Macro for prototyping all the variations of a common function with automatic
1533 * near -> far stub.
1534 *
1535 * @param a_RetType The return type.
1536 * @param a_Name The function basename.
1537 * @param a_Params The parameter list (in parentheses).
1538 * @sa BS3_CMN_PROTO_NOSB
1539 */
1540#define BS3_CMN_PROTO_STUB(a_RetType, a_Name, a_Params) BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params)
1541
1542/** @def BS3_CMN_PROTO_NOSB
1543 * Macro for prototyping all the variations of a common function without any
1544 * near > far stub.
1545 *
1546 * @param a_RetType The return type.
1547 * @param a_Name The function basename.
1548 * @param a_Params The parameter list (in parentheses).
1549 * @sa BS3_CMN_PROTO_STUB
1550 */
1551#define BS3_CMN_PROTO_NOSB(a_RetType, a_Name, a_Params) BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params)
1552
1553/** @def BS3_CMN_PROTO_FARSTUB
1554 * Macro for prototyping all the variations of a common function with automatic
1555 * far -> near stub.
1556 *
1557 * @param a_cbParam16 The size of the 16-bit parameter list in bytes.
1558 * @param a_RetType The return type.
1559 * @param a_Name The function basename.
1560 * @param a_Params The parameter list (in parentheses).
1561 * @sa BS3_CMN_PROTO_STUB
1562 */
1563#define BS3_CMN_PROTO_FARSTUB(a_cbParam16, a_RetType, a_Name, a_Params) BS3_CMN_PROTO_INT(a_RetType, a_Name, a_Params)
1564
1565
1566/** @def BS3_CMN_DEF
1567 * Macro for defining a common function.
1568 *
1569 * This makes 16-bit common function far, while 32-bit and 64-bit are near.
1570 *
1571 * @param a_RetType The return type.
1572 * @param a_Name The function basename.
1573 * @param a_Params The parameter list (in parentheses).
1574 */
1575#if ARCH_BITS == 16
1576# define BS3_CMN_DEF(a_RetType, a_Name, a_Params) \
1577 BS3_DECL_FAR(a_RetType) BS3_CMN_FAR_NM(a_Name) a_Params
1578#else
1579# define BS3_CMN_DEF(a_RetType, a_Name, a_Params) \
1580 BS3_DECL_NEAR(a_RetType) BS3_CMN_NM(a_Name) a_Params
1581#endif
1582
1583/** @def BS3_ASSERT
1584 * Assert that an expression is true.
1585 *
1586 * @param a_Expr The expression to assert.
1587 *
1588 * If the expression is false, prints a message and calls Bs3Panic.
1589 *
1590 * This can be controlled with two defines:
1591 *
1592 * - BS3_ASSERT_IGNORE disables it entirely.
1593 *
1594 * - BS3_ASSERT_FILE prints filenames rather than function names. This
1595 * is disabled by default, as it consumes noticeably more image space.
1596 * Function name + line number are generally enough to find an issue.
1597 *
1598 * The expression string also consumes space. If necessary, reduce that
1599 * by using a simpler ASSERT() inside more a complicated `if'.
1600 */
1601BS3_CMN_PROTO_STUB(DECL_NO_RETURN(void), Bs3Assert,(const char BS3_FAR *pszExpr, const char BS3_FAR *pszFileOrFunc, const unsigned uLine));
1602#define BS3_ASSERT_IGNORE
1603#if defined(BS3_ASSERT_IGNORE)
1604# define BS3_ASSERT(a_Expr) do { } while (0)
1605#elif defined(BS3_ASSERT_FILE)
1606# define BS3_ASSERT(a_Expr) do { if (!!(a_Expr)) { /* likely */ } else { Bs3Assert(#a_Expr, __FILE__, __LINE__); } } while (0)
1607#else
1608# define BS3_ASSERT(a_Expr) do { if (!!(a_Expr)) { /* likely */ } else { Bs3Assert(#a_Expr, __func__, __LINE__); } } while (0)
1609#endif
1610
1611/**
1612 * Panic, never return.
1613 *
1614 * The current implementation will only halt the CPU.
1615 */
1616BS3_CMN_PROTO_NOSB(DECL_NO_RETURN(void), Bs3Panic,(void));
1617#if !defined(BS3_KIT_WITH_NO_RETURN) && defined(__WATCOMC__)
1618# pragma aux Bs3Panic_c16 __aborts
1619# pragma aux Bs3Panic_f16 __aborts
1620# pragma aux Bs3Panic_c32 __aborts
1621#endif
1622
1623
1624/**
1625 * Translate a mode into a string.
1626 *
1627 * @returns Pointer to read-only mode name string.
1628 * @param bMode The mode value (BS3_MODE_XXX).
1629 */
1630BS3_CMN_PROTO_STUB(const char BS3_FAR *, Bs3GetModeName,(uint8_t bMode));
1631
1632/**
1633 * Translate a mode into a short lower case string.
1634 *
1635 * @returns Pointer to read-only short mode name string.
1636 * @param bMode The mode value (BS3_MODE_XXX).
1637 */
1638BS3_CMN_PROTO_STUB(const char BS3_FAR *, Bs3GetModeNameShortLower,(uint8_t bMode));
1639
1640/** CPU vendors. */
1641typedef enum BS3CPUVENDOR
1642{
1643 BS3CPUVENDOR_INVALID = 0,
1644 BS3CPUVENDOR_INTEL,
1645 BS3CPUVENDOR_AMD,
1646 BS3CPUVENDOR_VIA,
1647 BS3CPUVENDOR_CYRIX,
1648 BS3CPUVENDOR_SHANGHAI,
1649 BS3CPUVENDOR_HYGON,
1650 BS3CPUVENDOR_UNKNOWN,
1651 BS3CPUVENDOR_END
1652} BS3CPUVENDOR;
1653
1654/**
1655 * Tries to detect the CPU vendor.
1656 *
1657 * @returns CPU vendor.
1658 */
1659BS3_CMN_PROTO_STUB(BS3CPUVENDOR, Bs3GetCpuVendor,(void));
1660
1661/**
1662 * Shutdown the system, never returns.
1663 *
1664 * This currently only works for VMs. When running on real systems it will
1665 * just halt the CPU.
1666 */
1667BS3_CMN_PROTO_NOSB(void, Bs3Shutdown,(void));
1668
1669/**
1670 * Prints a 32-bit unsigned value as decimal to the screen.
1671 *
1672 * @param uValue The 32-bit value.
1673 */
1674BS3_CMN_PROTO_NOSB(void, Bs3PrintU32,(uint32_t uValue));
1675
1676/**
1677 * Prints a 32-bit unsigned value as hex to the screen.
1678 *
1679 * @param uValue The 32-bit value.
1680 */
1681BS3_CMN_PROTO_NOSB(void, Bs3PrintX32,(uint32_t uValue));
1682
1683/**
1684 * Formats and prints a string to the screen.
1685 *
1686 * See #Bs3StrFormatV for supported format types.
1687 *
1688 * @param pszFormat The format string.
1689 * @param ... Format arguments.
1690 */
1691BS3_CMN_PROTO_STUB(size_t, Bs3Printf,(const char BS3_FAR *pszFormat, ...));
1692
1693/**
1694 * Formats and prints a string to the screen, va_list version.
1695 *
1696 * See #Bs3StrFormatV for supported format types.
1697 *
1698 * @param pszFormat The format string.
1699 * @param va Format arguments.
1700 */
1701BS3_CMN_PROTO_STUB(size_t, Bs3PrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
1702
1703/**
1704 * Prints a string to the screen.
1705 *
1706 * @param pszString The string to print.
1707 */
1708BS3_CMN_PROTO_STUB(void, Bs3PrintStr,(const char BS3_FAR *pszString));
1709
1710/**
1711 * Prints a string to the screen.
1712 *
1713 * @param pszString The string to print. Any terminator charss will be printed.
1714 * @param cchString The exact number of characters to print.
1715 */
1716BS3_CMN_PROTO_NOSB(void, Bs3PrintStrN,(const char BS3_FAR *pszString, size_t cchString));
1717
1718/**
1719 * Prints a char to the screen.
1720 *
1721 * @param ch The character to print.
1722 */
1723BS3_CMN_PROTO_NOSB(void, Bs3PrintChr,(char ch));
1724
1725
1726/**
1727 * An output function for #Bs3StrFormatV.
1728 *
1729 * @returns Number of characters written.
1730 * @param ch The character to write. Zero in the final call.
1731 * @param pvUser User argument supplied to #Bs3StrFormatV.
1732 */
1733typedef BS3_DECL_CALLBACK(size_t) FNBS3STRFORMATOUTPUT(char ch, void BS3_FAR *pvUser);
1734/** Pointer to an output function for #Bs3StrFormatV. */
1735typedef FNBS3STRFORMATOUTPUT *PFNBS3STRFORMATOUTPUT;
1736
1737/**
1738 * Formats a string, sending the output to @a pfnOutput.
1739 *
1740 * Supported types:
1741 * - %RI8, %RI16, %RI32, %RI64
1742 * - %RU8, %RU16, %RU32, %RU64
1743 * - %RX8, %RX16, %RX32, %RX64
1744 * - %i, %d
1745 * - %u
1746 * - %x
1747 * - %c
1748 * - %p (far pointer)
1749 * - %s (far pointer)
1750 *
1751 * @returns Sum of @a pfnOutput return values.
1752 * @param pszFormat The format string.
1753 * @param va Format arguments.
1754 * @param pfnOutput The output function.
1755 * @param pvUser The user argument for the output function.
1756 */
1757BS3_CMN_PROTO_STUB(size_t, Bs3StrFormatV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va,
1758 PFNBS3STRFORMATOUTPUT pfnOutput, void BS3_FAR *pvUser));
1759
1760/**
1761 * Formats a string into a buffer.
1762 *
1763 * See #Bs3StrFormatV for supported format types.
1764 *
1765 * @returns The length of the formatted string (excluding terminator).
1766 * This will be higher or equal to @c cbBuf in case of an overflow.
1767 * @param pszBuf The output buffer.
1768 * @param cbBuf The size of the output buffer.
1769 * @param pszFormat The format string.
1770 * @param va Format arguments.
1771 */
1772BS3_CMN_PROTO_STUB(size_t, Bs3StrPrintfV,(char BS3_FAR *pszBuf, size_t cbBuf, const char BS3_FAR *pszFormat, va_list BS3_FAR va));
1773
1774/**
1775 * Formats a string into a buffer.
1776 *
1777 * See #Bs3StrFormatV for supported format types.
1778 *
1779 * @returns The length of the formatted string (excluding terminator).
1780 * This will be higher or equal to @c cbBuf in case of an overflow.
1781 * @param pszBuf The output buffer.
1782 * @param cbBuf The size of the output buffer.
1783 * @param pszFormat The format string.
1784 * @param ... Format arguments.
1785 */
1786BS3_CMN_PROTO_STUB(size_t, Bs3StrPrintf,(char BS3_FAR *pszBuf, size_t cbBuf, const char BS3_FAR *pszFormat, ...));
1787
1788
1789/**
1790 * Finds the length of a zero terminated string.
1791 *
1792 * @returns String length in chars/bytes.
1793 * @param pszString The string to examine.
1794 */
1795BS3_CMN_PROTO_STUB(size_t, Bs3StrLen,(const char BS3_FAR *pszString));
1796
1797/**
1798 * Finds the length of a zero terminated string, but with a max length.
1799 *
1800 * @returns String length in chars/bytes, or @a cchMax if no zero-terminator
1801 * was found before we reached the limit.
1802 * @param pszString The string to examine.
1803 * @param cchMax The max length to examine.
1804 */
1805BS3_CMN_PROTO_STUB(size_t, Bs3StrNLen,(const char BS3_FAR *pszString, size_t cchMax));
1806
1807/**
1808 * CRT style unsafe strcpy.
1809 *
1810 * @returns pszDst.
1811 * @param pszDst The destination buffer. Must be large enough to
1812 * hold the source string.
1813 * @param pszSrc The source string.
1814 */
1815BS3_CMN_PROTO_STUB(char BS3_FAR *, Bs3StrCpy,(char BS3_FAR *pszDst, const char BS3_FAR *pszSrc));
1816
1817/**
1818 * CRT style memcpy.
1819 *
1820 * @returns pvDst
1821 * @param pvDst The destination buffer.
1822 * @param pvSrc The source buffer.
1823 * @param cbToCopy The number of bytes to copy.
1824 * @sa Bs3MemCopyFlat
1825 */
1826BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemCpy,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1827
1828/**
1829 * GNU (?) style mempcpy.
1830 *
1831 * @returns pvDst + cbCopy
1832 * @param pvDst The destination buffer.
1833 * @param pvSrc The source buffer.
1834 * @param cbToCopy The number of bytes to copy.
1835 */
1836BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemPCpy,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1837
1838/**
1839 * CRT style memmove (overlapping buffers is fine).
1840 *
1841 * @returns pvDst
1842 * @param pvDst The destination buffer.
1843 * @param pvSrc The source buffer.
1844 * @param cbToCopy The number of bytes to copy.
1845 */
1846BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemMove,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1847
1848/**
1849 * BSD style bzero.
1850 *
1851 * @param pvDst The buffer to be zeroed.
1852 * @param cbDst The number of bytes to zero.
1853 */
1854BS3_CMN_PROTO_NOSB(void, Bs3MemZero,(void BS3_FAR *pvDst, size_t cbDst));
1855
1856/**
1857 * CRT style memset.
1858 *
1859 * @param pvDst The buffer to be fill.
1860 * @param bFiller The filler byte.
1861 * @param cbDst The number of bytes to fill.
1862 */
1863BS3_CMN_PROTO_NOSB(void, Bs3MemSet,(void BS3_FAR *pvDst, uint8_t bFiller, size_t cbDst));
1864
1865/**
1866 * CRT style memchr.
1867 *
1868 * @param pvHaystack The memory to scan for @a bNeedle.
1869 * @param bNeedle The byte to search for.
1870 * @param cbHaystack The amount of memory to search.
1871 */
1872BS3_CMN_PROTO_NOSB(void BS3_FAR *, Bs3MemChr,(void const BS3_FAR *pvHaystack, uint8_t bNeedle, size_t cbHaystack));
1873
1874/**
1875 * CRT style memcmp.
1876 *
1877 * @returns 0 if equal. Negative if the left side is 'smaller' than the right
1878 * side, and positive in the other case.
1879 * @param pv1 The left hand memory.
1880 * @param pv2 The right hand memory.
1881 * @param cb The number of bytes to compare.
1882 */
1883BS3_CMN_PROTO_NOSB(int, Bs3MemCmp,(void const BS3_FAR *pv1, void const BS3_FAR *pv2, size_t cb));
1884
1885BS3_CMN_PROTO_STUB(void, Bs3UInt64Div,(RTUINT64U uDividend, RTUINT64U uDivisor, RTUINT64U BS3_FAR *paQuotientReminder));
1886BS3_CMN_PROTO_STUB(void, Bs3UInt32Div,(RTUINT32U uDividend, RTUINT32U uDivisor, RTUINT32U BS3_FAR *paQuotientReminder));
1887
1888
1889/**
1890 * Converts a protected mode 32-bit far pointer to a 32-bit flat address.
1891 *
1892 * @returns 32-bit flat address.
1893 * @param off The segment offset.
1894 * @param uSel The protected mode segment selector.
1895 */
1896BS3_CMN_PROTO_STUB(uint32_t, Bs3SelProtFar32ToFlat32,(uint32_t off, uint16_t uSel));
1897
1898/**
1899 * Converts a current mode 32-bit far pointer to a 32-bit flat address.
1900 *
1901 * @returns 32-bit flat address.
1902 * @param off The segment offset.
1903 * @param uSel The current mode segment selector.
1904 */
1905BS3_CMN_PROTO_STUB(uint32_t, Bs3SelFar32ToFlat32,(uint32_t off, uint16_t uSel));
1906
1907/**
1908 * Wrapper around Bs3SelFar32ToFlat32 that makes it easier to use in tight
1909 * assembly spots.
1910 *
1911 * @returns 32-bit flat address.
1912 * @param off The segment offset.
1913 * @param uSel The current mode segment selector.
1914 * @remarks All register are preserved, except return.
1915 * @remarks No 20h scratch space required in 64-bit mode.
1916 */
1917BS3_CMN_PROTO_FARSTUB(6, uint32_t, Bs3SelFar32ToFlat32NoClobber,(uint32_t off, uint16_t uSel));
1918
1919/**
1920 * Converts a real mode code segment to a protected mode code segment selector.
1921 *
1922 * @returns protected mode segment selector.
1923 * @param uRealSeg Real mode code segment.
1924 * @remarks All register are preserved, except return and parameter.
1925 */
1926BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelRealModeCodeToProtMode,(uint16_t uRealSeg));
1927
1928/**
1929 * Converts a real mode code segment to a protected mode code segment selector.
1930 *
1931 * @returns protected mode segment selector.
1932 * @param uProtSel Real mode code segment.
1933 * @remarks All register are preserved, except return and parameter.
1934 */
1935BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelProtModeCodeToRealMode,(uint16_t uProtSel));
1936
1937/**
1938 * Converts a flat code address to a real mode segment and offset.
1939 *
1940 * @returns Far real mode address (high 16-bit is segment, low is offset).
1941 * @param uFlatAddr Flat code address.
1942 * @remarks All register are preserved, except return and parameter.
1943 */
1944BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToRealMode,(uint32_t uFlatAddr));
1945
1946/**
1947 * Converts a flat code address to a protected mode 16-bit far pointer (ring-0).
1948 *
1949 * @returns Far 16-bit protected mode address (high 16-bit is segment selector,
1950 * low is segment offset).
1951 * @param uFlatAddr Flat code address.
1952 * @remarks All register are preserved, except return and parameter.
1953 */
1954BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToProtFar16,(uint32_t uFlatAddr));
1955
1956/**
1957 * Converts a far 16:16 real mode (code) address to a flat address.
1958 *
1959 * @returns 32-bit flat address.
1960 * @param uFar1616 Far real mode address (high 16-bit is segment, low
1961 * is offset).
1962 * @remarks All register are preserved, except return.
1963 * @remarks No 20h scratch space required in 64-bit mode.
1964 * @remarks Exactly the same as Bs3SelRealModeDataToFlat, except for param.
1965 */
1966BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeCodeToFlat,(PFNBS3FARADDRCONV uFar1616));
1967
1968/**
1969 * Converts a far 16:16 real mode (code) address to a 16-bit protected mode
1970 * address.
1971 *
1972 * This is mainly for converting X0TEXT16 and X1TEXT16 linker pointers to
1973 * protected mode ones.
1974 *
1975 * @returns Protected mode function pointer.
1976 * @param uFar1616 Far real mode address (high 16-bit is segment, low
1977 * is offset). In 16-bit code, this is also the same
1978 * as a linker address.
1979 */
1980BS3_CMN_PROTO_FARSTUB(4, PFNBS3FARADDRCONV, Bs3SelRealModeCodeToProtFar16,(PFNBS3FARADDRCONV uFar1616));
1981
1982/**
1983 * Converts a flat data address to a real mode segment and offset.
1984 *
1985 * @returns Far real mode address (high 16-bit is segment, low is offset)
1986 * @param uFlatAddr Flat code address.
1987 * @remarks All register are preserved, except return.
1988 * @remarks No 20h scratch space required in 64-bit mode.
1989 */
1990BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToRealMode,(uint32_t uFlatAddr));
1991
1992/**
1993 * Converts a flat data address to a real mode segment and offset.
1994 *
1995 * @returns Far 16-bit protected mode address (high 16-bit is segment selector,
1996 * low is segment offset).
1997 * @param uFlatAddr Flat code address.
1998 * @remarks All register are preserved, except return.
1999 * @remarks No 20h scratch space required in 64-bit mode.
2000 */
2001BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToProtFar16,(uint32_t uFlatAddr));
2002
2003/**
2004 * Converts a far 16:16 data address to a real mode segment and offset.
2005 *
2006 * @returns Far real mode address (high 16-bit is segment, low is offset)
2007 * @param uFar1616 Far 16-bit protected mode address (high 16-bit is
2008 * segment selector, low is segment offset).
2009 * @remarks All register are preserved, except return.
2010 * @remarks No 20h scratch space required in 64-bit mode.
2011 */
2012BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToRealMode,(uint32_t uFar1616));
2013
2014/**
2015 * Converts a far 16:16 real mode address to a 16-bit protected mode address.
2016 *
2017 * @returns Far real mode address (high 16-bit is segment, low is offset)
2018 * @param uFar1616 Far real mode address (high 16-bit is segment, low
2019 * is offset).
2020 * @remarks All register are preserved, except return.
2021 * @remarks No 20h scratch space required in 64-bit mode.
2022 */
2023BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToProtFar16,(uint32_t uFar1616));
2024
2025/**
2026 * Converts a far 16:16 data address to a flat 32-bit address.
2027 *
2028 * @returns 32-bit flat address.
2029 * @param uFar1616 Far 16-bit protected mode address (high 16-bit is
2030 * segment selector, low is segment offset).
2031 * @remarks All register are preserved, except return.
2032 * @remarks No 20h scratch space required in 64-bit mode.
2033 */
2034BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToFlat,(uint32_t uFar1616));
2035
2036/**
2037 * Converts a far 16:16 real mode address to a flat address.
2038 *
2039 * @returns 32-bit flat address.
2040 * @param uFar1616 Far real mode address (high 16-bit is segment, low
2041 * is offset).
2042 * @remarks All register are preserved, except return.
2043 * @remarks No 20h scratch space required in 64-bit mode.
2044 */
2045BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToFlat,(uint32_t uFar1616));
2046
2047/**
2048 * Converts a link-time data pointer to a current context pointer.
2049 *
2050 * @returns Converted pointer.
2051 * @param pvLnkPtr The pointer the linker produced.
2052 */
2053BS3_CMN_PROTO_FARSTUB(4, void BS3_FAR *, Bs3SelLnkPtrToCurPtr,(void BS3_FAR *pvLnkPtr));
2054
2055/**
2056 * Converts a link-time data pointer to a flat address.
2057 *
2058 * @returns 32-bit flag address.
2059 * @param pvLnkPtr The pointer the linker produced.
2060 */
2061BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelLnkPtrToFlat,(void BS3_FAR *pvLnkPtr));
2062
2063/**
2064 * Converts a link-time code pointer to a current context pointer.
2065 *
2066 * @returns Converted function pointer.
2067 * @param pfnLnkPtr The function pointer the linker produced.
2068 */
2069BS3_CMN_PROTO_FARSTUB(4, FPFNBS3FAR, Bs3SelLnkCodePtrToCurPtr,(FPFNBS3FAR pfnLnkPtr));
2070
2071/**
2072 * Gets a flat address from a working poitner.
2073 *
2074 * @returns flat address (32-bit or 64-bit).
2075 * @param pv Current context pointer.
2076 */
2077DECLINLINE(RTCCUINTXREG) Bs3SelPtrToFlat(void BS3_FAR *pv)
2078{
2079#if ARCH_BITS == 16
2080 return BS3_CMN_FN_NM(Bs3SelFar32ToFlat32)(BS3_FP_OFF(pv), BS3_FP_SEG(pv));
2081#else
2082 return (uintptr_t)pv;
2083#endif
2084}
2085
2086/**
2087 * Sets up a 16-bit read-write data selector with ring-3 access and 64KB limit.
2088 *
2089 * @param pDesc Pointer to the descriptor table entry.
2090 * @param uBaseAddr The base address of the descriptor.
2091 */
2092BS3_CMN_PROTO_STUB(void, Bs3SelSetup16BitData,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr));
2093
2094/**
2095 * Sets up a 16-bit execute-read selector with a 64KB limit.
2096 *
2097 * @param pDesc Pointer to the descriptor table entry.
2098 * @param uBaseAddr The base address of the descriptor.
2099 * @param bDpl The descriptor privilege level.
2100 */
2101BS3_CMN_PROTO_STUB(void, Bs3SelSetup16BitCode,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr, uint8_t bDpl));
2102
2103/**
2104 * Sets up a 32-bit execute-read selector with a user specified limit.
2105 *
2106 * @param pDesc Pointer to the descriptor table entry.
2107 * @param uBaseAddr The base address of the descriptor.
2108 * @param uLimit The limit. (This is included here and not in the 16-bit
2109 * functions because we're more likely to want to set it
2110 * than for 16-bit selectors.)
2111 * @param bDpl The descriptor privilege level.
2112 */
2113BS3_CMN_PROTO_STUB(void, Bs3SelSetup32BitCode,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr, uint32_t uLimit, uint8_t bDpl));
2114
2115/**
2116 * Sets up a 16-bit or 32-bit gate descriptor.
2117 *
2118 * This can be used both for GDT/LDT and IDT.
2119 *
2120 * @param pDesc Pointer to the descriptor table entry.
2121 * @param bType The gate type.
2122 * @param bDpl The gate DPL.
2123 * @param uSel The gate selector value.
2124 * @param off The gate IP/EIP value.
2125 * @param cParams Number of parameters to copy if call-gate.
2126 */
2127BS3_CMN_PROTO_STUB(void, Bs3SelSetupGate,(X86DESC BS3_FAR *pDesc, uint8_t bType, uint8_t bDpl,
2128 uint16_t uSel, uint32_t off, uint8_t cParams));
2129
2130/**
2131 * Sets up a 64-bit gate descriptor.
2132 *
2133 * This can be used both for GDT/LDT and IDT.
2134 *
2135 * @param pDescPair Pointer to the _two_ descriptor table entries.
2136 * @param bType The gate type.
2137 * @param bDpl The gate DPL.
2138 * @param uSel The gate selector value.
2139 * @param off The gate IP/EIP value.
2140 */
2141BS3_CMN_PROTO_STUB(void, Bs3SelSetupGate64,(X86DESC BS3_FAR *pDescPair, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint64_t off));
2142
2143
2144/**
2145 * Slab control structure list head.
2146 *
2147 * The slabs on the list must all have the same chunk size.
2148 */
2149typedef struct BS3SLABHEAD
2150{
2151 /** Pointer to the first slab. */
2152 BS3_XPTR_MEMBER(struct BS3SLABCTL, pFirst);
2153 /** The allocation chunk size. */
2154 uint16_t cbChunk;
2155 /** Number of slabs in the list. */
2156 uint16_t cSlabs;
2157 /** Number of chunks in the list. */
2158 uint32_t cChunks;
2159 /** Number of free chunks. */
2160 uint32_t cFreeChunks;
2161} BS3SLABHEAD;
2162AssertCompileSize(BS3SLABHEAD, 16);
2163/** Pointer to a slab list head. */
2164typedef BS3SLABHEAD BS3_FAR *PBS3SLABHEAD;
2165
2166/**
2167 * Allocation slab control structure.
2168 *
2169 * This may live at the start of the slab for 4KB slabs, while in a separate
2170 * static location for the larger ones.
2171 */
2172typedef struct BS3SLABCTL
2173{
2174 /** Pointer to the next slab control structure in this list. */
2175 BS3_XPTR_MEMBER(struct BS3SLABCTL, pNext);
2176 /** Pointer to the slab list head. */
2177 BS3_XPTR_MEMBER(BS3SLABHEAD, pHead);
2178 /** The base address of the slab. */
2179 BS3_XPTR_MEMBER(uint8_t, pbStart);
2180 /** Number of chunks in this slab. */
2181 uint16_t cChunks;
2182 /** Number of currently free chunks. */
2183 uint16_t cFreeChunks;
2184 /** The chunk size. */
2185 uint16_t cbChunk;
2186 /** The shift count corresponding to cbChunk.
2187 * This is for turning a chunk number into a byte offset and vice versa. */
2188 uint16_t cChunkShift;
2189 /** Bitmap where set bits indicates allocated blocks (variable size,
2190 * multiple of 4). */
2191 uint8_t bmAllocated[4];
2192} BS3SLABCTL;
2193/** Pointer to a bs3kit slab control structure. */
2194typedef BS3SLABCTL BS3_FAR *PBS3SLABCTL;
2195
2196/** The chunks must all be in the same 16-bit segment tile. */
2197#define BS3_SLAB_ALLOC_F_SAME_TILE UINT16_C(0x0001)
2198
2199/**
2200 * Initializes a slab.
2201 *
2202 * @param pSlabCtl The slab control structure to initialize.
2203 * @param cbSlabCtl The size of the slab control structure.
2204 * @param uFlatSlabPtr The base address of the slab.
2205 * @param cbSlab The size of the slab.
2206 * @param cbChunk The chunk size.
2207 */
2208BS3_CMN_PROTO_STUB(void, Bs3SlabInit,(PBS3SLABCTL pSlabCtl, size_t cbSlabCtl, uint32_t uFlatSlabPtr,
2209 uint32_t cbSlab, uint16_t cbChunk));
2210
2211/**
2212 * Allocates one chunk from a slab.
2213 *
2214 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2215 * @param pSlabCtl The slab control structure to allocate from.
2216 */
2217BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabAlloc,(PBS3SLABCTL pSlabCtl));
2218
2219/**
2220 * Allocates one or more chunks from a slab.
2221 *
2222 * @returns Pointer to the request number of chunks on success, NULL if we're
2223 * out of chunks.
2224 * @param pSlabCtl The slab control structure to allocate from.
2225 * @param cChunks The number of contiguous chunks we want.
2226 * @param fFlags Flags, see BS3_SLAB_ALLOC_F_XXX
2227 */
2228BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabAllocEx,(PBS3SLABCTL pSlabCtl, uint16_t cChunks, uint16_t fFlags));
2229
2230/**
2231 * Allocates a specific range of chunks from a slab.
2232 *
2233 * @returns Number of chunks it was possible to allocate in the slab.
2234 * @retval 0 if the given address isn't in the slab.
2235 * @retval UINT16_MAX if one or more of the requested chunks are already in
2236 * use, so the request cannot be fulfilled.
2237 * @param pSlabCtl The slab control structure to allocate from.
2238 * @param uFlatAddr The flat address of the range to allocate.
2239 * @param cChunks The number of contiguous chunks we want.
2240 */
2241BS3_CMN_PROTO_STUB(uint16_t, Bs3SlabAllocFixed,(PBS3SLABCTL pSlabCtl, uint32_t uFlatAddr, uint16_t cChunks));
2242
2243/**
2244 * Frees one or more chunks from a slab.
2245 *
2246 * @returns Number of chunks actually freed. When correctly used, this will
2247 * match the @a cChunks parameter, of course.
2248 * @param pSlabCtl The slab control structure to free from.
2249 * @param uFlatChunkPtr The flat address of the chunks to free.
2250 * @param cChunks The number of contiguous chunks to free.
2251 */
2252BS3_CMN_PROTO_STUB(uint16_t, Bs3SlabFree,(PBS3SLABCTL pSlabCtl, uint32_t uFlatChunkPtr, uint16_t cChunks));
2253
2254
2255/**
2256 * Initializes the given slab list head.
2257 *
2258 * @param pHead The slab list head.
2259 * @param cbChunk The chunk size.
2260 */
2261BS3_CMN_PROTO_STUB(void, Bs3SlabListInit,(PBS3SLABHEAD pHead, uint16_t cbChunk));
2262
2263/**
2264 * Adds an initialized slab control structure to the list.
2265 *
2266 * @param pHead The slab list head to add it to.
2267 * @param pSlabCtl The slab control structure to add.
2268 */
2269BS3_CMN_PROTO_STUB(void, Bs3SlabListAdd,(PBS3SLABHEAD pHead, PBS3SLABCTL pSlabCtl));
2270
2271/**
2272 * Allocates one chunk.
2273 *
2274 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2275 * @param pHead The slab list to allocate from.
2276 */
2277BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabListAlloc,(PBS3SLABHEAD pHead));
2278
2279/**
2280 * Allocates one or more chunks.
2281 *
2282 * @returns Pointer to the request number of chunks on success, NULL if we're
2283 * out of chunks.
2284 * @param pHead The slab list to allocate from.
2285 * @param cChunks The number of contiguous chunks we want.
2286 * @param fFlags Flags, see BS3_SLAB_ALLOC_F_XXX
2287 */
2288BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabListAllocEx,(PBS3SLABHEAD pHead, uint16_t cChunks, uint16_t fFlags));
2289
2290/**
2291 * Frees one or more chunks from a slab list.
2292 *
2293 * @param pHead The slab list to allocate from.
2294 * @param pvChunks Pointer to the first chunk to free.
2295 * @param cChunks The number of contiguous chunks to free.
2296 */
2297BS3_CMN_PROTO_STUB(void, Bs3SlabListFree,(PBS3SLABHEAD pHead, void BS3_FAR *pvChunks, uint16_t cChunks));
2298
2299/**
2300 * Allocation addressing constraints.
2301 */
2302typedef enum BS3MEMKIND
2303{
2304 /** Invalid zero type. */
2305 BS3MEMKIND_INVALID = 0,
2306 /** Real mode addressable memory. */
2307 BS3MEMKIND_REAL,
2308 /** Memory addressable using the 16-bit protected mode tiling. */
2309 BS3MEMKIND_TILED,
2310 /** Memory addressable using 32-bit flat addressing. */
2311 BS3MEMKIND_FLAT32,
2312 /** Memory addressable using 64-bit flat addressing. */
2313 BS3MEMKIND_FLAT64,
2314 /** End of valid types. */
2315 BS3MEMKIND_END,
2316} BS3MEMKIND;
2317
2318/**
2319 * Allocates low memory.
2320 *
2321 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2322 * @param enmKind The kind of addressing constraints imposed on the
2323 * allocation.
2324 * @param cb How much to allocate.
2325 */
2326BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemAlloc,(BS3MEMKIND enmKind, size_t cb));
2327
2328/**
2329 * Allocates zero'ed memory.
2330 *
2331 * @param enmKind The kind of addressing constraints imposed on the
2332 * allocation.
2333 * @param cb How much to allocate.
2334 */
2335BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemAllocZ,(BS3MEMKIND enmKind, size_t cb));
2336
2337/**
2338 * Frees memory.
2339 *
2340 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2341 * @param pv The memory to free (returned by #Bs3MemAlloc).
2342 * @param cb The size of the allocation.
2343 */
2344BS3_CMN_PROTO_STUB(void, Bs3MemFree,(void BS3_FAR *pv, size_t cb));
2345
2346/**
2347 * Allocates a page with non-present pages on each side.
2348 *
2349 * @returns Pointer to the usable page. NULL on failure. Use
2350 * Bs3MemGuardedTestPageFree to free the allocation.
2351 * @param enmKind The kind of addressing constraints imposed on the
2352 * allocation.
2353 */
2354BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemGuardedTestPageAlloc,(BS3MEMKIND enmKind));
2355
2356/**
2357 * Allocates a page with pages on each side to the @a fPte specification.
2358 *
2359 * @returns Pointer to the usable page. NULL on failure. Use
2360 * Bs3MemGuardedTestPageFree to free the allocation.
2361 * @param enmKind The kind of addressing constraints imposed on the
2362 * allocation.
2363 * @param fPte The page table entry specification for the guard pages.
2364 */
2365BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemGuardedTestPageAllocEx,(BS3MEMKIND enmKind, uint64_t fPte));
2366
2367/**
2368 * Frees guarded page allocated by Bs3MemGuardedTestPageAlloc or
2369 * Bs3MemGuardedTestPageAllocEx.
2370 *
2371 * @param pvGuardedPage Pointer returned by Bs3MemGuardedTestPageAlloc or
2372 * Bs3MemGuardedTestPageAllocEx. NULL is ignored.
2373 */
2374BS3_CMN_PROTO_STUB(void, Bs3MemGuardedTestPageFree,(void BS3_FAR *pvGuardedPage));
2375
2376/**
2377 * Print all heap info.
2378 */
2379BS3_CMN_PROTO_STUB(void, Bs3MemPrintInfo, (void));
2380
2381/** The end RAM address below 4GB (approximately). */
2382extern uint32_t g_uBs3EndOfRamBelow4G;
2383/** The end RAM address above 4GB, zero if no memory above 4GB. */
2384extern uint64_t g_uBs3EndOfRamAbove4G;
2385
2386
2387/**
2388 * Enables the A20 gate.
2389 */
2390BS3_CMN_PROTO_NOSB(void, Bs3A20Enable,(void));
2391
2392/**
2393 * Enables the A20 gate via the keyboard controller
2394 */
2395BS3_CMN_PROTO_NOSB(void, Bs3A20EnableViaKbd,(void));
2396
2397/**
2398 * Enables the A20 gate via the PS/2 control port A.
2399 */
2400BS3_CMN_PROTO_NOSB(void, Bs3A20EnableViaPortA,(void));
2401
2402/**
2403 * Disables the A20 gate.
2404 */
2405BS3_CMN_PROTO_NOSB(void, Bs3A20Disable,(void));
2406
2407/**
2408 * Disables the A20 gate via the keyboard controller
2409 */
2410BS3_CMN_PROTO_NOSB(void, Bs3A20DisableViaKbd,(void));
2411
2412/**
2413 * Disables the A20 gate via the PS/2 control port A.
2414 */
2415BS3_CMN_PROTO_NOSB(void, Bs3A20DisableViaPortA,(void));
2416
2417
2418/**
2419 * Initializes root page tables for page protected mode (PP16, PP32).
2420 *
2421 * @returns IPRT status code.
2422 * @remarks Must not be called in real-mode!
2423 */
2424BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForPP,(void));
2425
2426/**
2427 * Initializes root page tables for PAE page protected mode (PAE16, PAE32).
2428 *
2429 * @returns IPRT status code.
2430 * @remarks The default long mode page tables depends on the PAE ones.
2431 * @remarks Must not be called in real-mode!
2432 */
2433BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForPAE,(void));
2434
2435/**
2436 * Initializes root page tables for long mode (LM16, LM32, LM64).
2437 *
2438 * @returns IPRT status code.
2439 * @remarks The default long mode page tables depends on the PAE ones.
2440 * @remarks Must not be called in real-mode!
2441 */
2442BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForLM,(void));
2443
2444/**
2445 * Maps all RAM above 4GB into the long mode page tables.
2446 *
2447 * This requires Bs3PagingInitRootForLM to have been called first.
2448 *
2449 * @returns IPRT status code.
2450 * @retval VERR_WRONG_ORDER if Bs3PagingInitRootForLM wasn't called.
2451 * @retval VINF_ALREADY_INITIALIZED if already called or someone mapped
2452 * something else above 4GiB already.
2453 * @retval VERR_OUT_OF_RANGE if too much RAM (more than 2^47 bytes).
2454 * @retval VERR_NO_MEMORY if no more memory for paging structures.
2455 * @retval VERR_UNSUPPORTED_ALIGNMENT if the bs3kit allocator malfunctioned and
2456 * didn't give us page aligned memory as it should.
2457 *
2458 * @param puFailurePoint Where to return the address where we encountered
2459 * a failure. Optional.
2460 *
2461 * @remarks Must be called in 32-bit or 64-bit mode as paging structures will be
2462 * allocated using BS3MEMKIND_FLAT32, as there might not be sufficient
2463 * BS3MEMKIND_TILED memory around. (Also, too it's simply too much of
2464 * a bother to deal with 16-bit for something that's long-mode only.)
2465 */
2466BS3_CMN_PROTO_STUB(int, Bs3PagingMapRamAbove4GForLM,(uint64_t *puFailurePoint));
2467
2468/**
2469 * Modifies the page table protection of an address range.
2470 *
2471 * This only works on the lowest level of the page tables in the current mode.
2472 *
2473 * Since we generally use the largest pages available when setting up the
2474 * initial page tables, this function will usually have to allocate and create
2475 * more tables. This may fail if we're low on memory.
2476 *
2477 * @returns IPRT status code.
2478 * @param uFlat The flat address of the first page in the range (rounded
2479 * down nearest page boundrary).
2480 * @param cb The range size from @a pv (rounded up to nearest page boundrary).
2481 * @param fSet Mask of zero or more X86_PTE_XXX values to set for the range.
2482 * @param fClear Mask of zero or more X86_PTE_XXX values to clear for the range.
2483 */
2484BS3_CMN_PROTO_STUB(int, Bs3PagingProtect,(uint64_t uFlat, uint64_t cb, uint64_t fSet, uint64_t fClear));
2485
2486/**
2487 * Modifies the page table protection of an address range.
2488 *
2489 * This only works on the lowest level of the page tables in the current mode.
2490 *
2491 * Since we generally use the largest pages available when setting up the
2492 * initial page tables, this function will usually have to allocate and create
2493 * more tables. This may fail if we're low on memory.
2494 *
2495 * @returns IPRT status code.
2496 * @param pv The address of the first page in the range (rounded
2497 * down nearest page boundrary).
2498 * @param cb The range size from @a pv (rounded up to nearest page boundrary).
2499 * @param fSet Mask of zero or more X86_PTE_XXX values to set for the range.
2500 * @param fClear Mask of zero or more X86_PTE_XXX values to clear for the range.
2501 */
2502BS3_CMN_PROTO_STUB(int, Bs3PagingProtectPtr,(void BS3_FAR *pv, size_t cb, uint64_t fSet, uint64_t fClear));
2503
2504/**
2505 * Aliases (maps) one or more contiguous physical pages to a virtual range.
2506 *
2507 * @returns VBox status code.
2508 * @retval VERR_INVALID_PARAMETER if we're in legacy paging mode and @a uDst or
2509 * @a uPhysToAlias are not compatible with legacy paging.
2510 * @retval VERR_OUT_OF_RANGE if we cannot traverse the page tables in this mode
2511 * (typically real mode or v86, maybe 16-bit PE).
2512 * @retval VERR_NO_MEMORY if we cannot allocate page tables for splitting up
2513 * the necessary large pages. No aliasing was performed.
2514 *
2515 * @param uDst The virtual address to map it at. Rounded down
2516 * to the nearest page (@a cbHowMuch is adjusted
2517 * up).
2518 * @param uPhysToAlias The physical address of the first page in the
2519 * (contiguous) range to map. Chopped down to
2520 * nearest page boundrary (@a cbHowMuch is not
2521 * adjusted).
2522 * @param cbHowMuch How much to map. Rounded up to nearest page.
2523 * @param fPte The PTE flags.
2524 */
2525BS3_CMN_PROTO_STUB(int, Bs3PagingAlias,(uint64_t uDst, uint64_t uPhysToAlias, uint32_t cbHowMuch, uint64_t fPte));
2526
2527/**
2528 * Unaliases memory, i.e. restores the 1:1 mapping.
2529 *
2530 * @returns VBox status code. Cannot fail if @a uDst and @a cbHowMuch specify
2531 * the range of a successful Bs3PagingAlias call, however it may run
2532 * out of memory if it's breaking new ground.
2533 *
2534 * @param uDst The virtual address to restore to 1:1 mapping.
2535 * Rounded down to the nearest page (@a cbHowMuch
2536 * is adjusted up).
2537 * @param cbHowMuch How much to restore. Rounded up to nearest page.
2538 */
2539BS3_CMN_PROTO_STUB(int, Bs3PagingUnalias,(uint64_t uDst, uint32_t cbHowMuch));
2540
2541/**
2542 * Get the pointer to the PTE for the given address.
2543 *
2544 * @returns Pointer to the PTE.
2545 * @param uFlat The flat address of the page which PTE we want.
2546 * @param prc Where to return additional error info. Optional.
2547 */
2548BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3PagingGetPte,(uint64_t uFlat, int *prc));
2549
2550/**
2551 * Paging information for an address.
2552 */
2553typedef struct BS3PAGINGINFO4ADDR
2554{
2555 /** The depth of the system's paging mode.
2556 * This is always 2 for legacy, 3 for PAE and 4 for long mode. */
2557 uint8_t cEntries;
2558 /** The size of the page structures (the entires). */
2559 uint8_t cbEntry;
2560 /** Flags defined for future fun, currently zero. */
2561 uint16_t fFlags;
2562 /** Union display different view on the entry pointers. */
2563 union
2564 {
2565 /** Pointer to the page structure entries, starting with the PTE as 0.
2566 * If large pages are involved, the first entry will be NULL (first two if 1GB
2567 * page). Same if the address is invalid on a higher level. */
2568 uint8_t BS3_FAR *apbEntries[4];
2569 /** Alternative view for legacy mode. */
2570 struct
2571 {
2572 X86PTE BS3_FAR *pPte;
2573 X86PDE BS3_FAR *pPde;
2574 void *pvUnused2;
2575 void *pvUnused3;
2576 } Legacy;
2577 /** Alternative view for PAE and Long mode. */
2578 struct
2579 {
2580 X86PTEPAE BS3_FAR *pPte;
2581 X86PDEPAE BS3_FAR *pPde;
2582 X86PDPE BS3_FAR *pPdpe;
2583 X86PML4E BS3_FAR *pPml4e;
2584 } Pae;
2585 } u;
2586} BS3PAGINGINFO4ADDR;
2587/** Pointer to paging information for and address. */
2588typedef BS3PAGINGINFO4ADDR BS3_FAR *PBS3PAGINGINFO4ADDR;
2589
2590/**
2591 * Queries paging information about the given virtual address.
2592 *
2593 * @returns VBox status code.
2594 * @param uFlat The flat address to query information about.
2595 * @param pPgInfo Where to return the information.
2596 */
2597BS3_CMN_PROTO_STUB(int, Bs3PagingQueryAddressInfo,(uint64_t uFlat, PBS3PAGINGINFO4ADDR pPgInfo));
2598
2599
2600/** The physical / flat address of the buffer backing the canonical traps.
2601 * This buffer is spread equally on each side of the 64-bit non-canonical
2602 * address divide. Non-64-bit code can use this to setup trick shots and
2603 * inspect their results. */
2604extern uint32_t g_uBs3PagingCanonicalTrapsAddr;
2605/** The size of the buffer at g_uPagingCanonicalTraps (both sides). */
2606extern uint16_t g_cbBs3PagingCanonicalTraps;
2607/** The size of one trap buffer (low or high).
2608 * This is g_cbBs3PagingCanonicalTraps divided by two. */
2609extern uint16_t g_cbBs3PagingOneCanonicalTrap;
2610
2611/**
2612 * Sets up the 64-bit canonical address space trap buffers, if neceessary.
2613 *
2614 * @returns Pointer to the buffers (i.e. the first page of the low one) on
2615 * success. NULL on failure.
2616 */
2617BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3PagingSetupCanonicalTraps,(void));
2618
2619
2620/**
2621 * Call 16-bit prot mode function from v8086 mode.
2622 *
2623 * This switches from v8086 mode to 16-bit protected mode (code) and executes
2624 * @a fpfnCall with @a cbParams bytes of parameters pushed on the stack.
2625 * Afterwards it switches back to v8086 mode and returns a 16-bit status code.
2626 *
2627 * @returns 16-bit status code if the function returned anything.
2628 * @param fpfnCall Far real mode pointer to the function to call.
2629 * @param cbParams The size of the parameter list, in bytes.
2630 * @param ... The parameters.
2631 * @sa Bs3SwitchTo32BitAndCallC
2632 */
2633BS3_CMN_PROTO_STUB(int, Bs3SwitchFromV86To16BitAndCallC,(FPFNBS3FAR fpfnCall, unsigned cbParams, ...));
2634
2635
2636/**
2637 * BS3 integer register.
2638 */
2639typedef union BS3REG
2640{
2641 /** 8-bit unsigned integer. */
2642 uint8_t u8;
2643 /** 16-bit unsigned integer. */
2644 uint16_t u16;
2645 /** 32-bit unsigned integer. */
2646 uint32_t u32;
2647 /** 64-bit unsigned integer. */
2648 uint64_t u64;
2649 /** Full unsigned integer. */
2650 uint64_t u;
2651 /** High/low byte view. */
2652 struct
2653 {
2654 uint8_t bLo;
2655 uint8_t bHi;
2656 } b;
2657 /** 8-bit view. */
2658 uint8_t au8[8];
2659 /** 16-bit view. */
2660 uint16_t au16[4];
2661 /** 32-bit view. */
2662 uint32_t au32[2];
2663 /** 64-bit view. */
2664 uint64_t au64[1];
2665 /** Unsigned integer, depending on compiler context.
2666 * This generally follows ARCH_BITS. */
2667 RTCCUINTREG uCcReg;
2668 /** Extended unsigned integer, depending on compiler context.
2669 * This is 32-bit in 16-bit and 32-bit compiler contexts, and 64-bit in
2670 * 64-bit. */
2671 RTCCUINTXREG uCcXReg;
2672} BS3REG;
2673/** Pointer to an integer register. */
2674typedef BS3REG BS3_FAR *PBS3REG;
2675/** Pointer to a const integer register. */
2676typedef BS3REG const BS3_FAR *PCBS3REG;
2677
2678/**
2679 * Register context (without FPU).
2680 */
2681typedef struct BS3REGCTX
2682{
2683 BS3REG rax; /**< 0x00 */
2684 BS3REG rcx; /**< 0x08 */
2685 BS3REG rdx; /**< 0x10 */
2686 BS3REG rbx; /**< 0x18 */
2687 BS3REG rsp; /**< 0x20 */
2688 BS3REG rbp; /**< 0x28 */
2689 BS3REG rsi; /**< 0x30 */
2690 BS3REG rdi; /**< 0x38 */
2691 BS3REG r8; /**< 0x40 */
2692 BS3REG r9; /**< 0x48 */
2693 BS3REG r10; /**< 0x50 */
2694 BS3REG r11; /**< 0x58 */
2695 BS3REG r12; /**< 0x60 */
2696 BS3REG r13; /**< 0x68 */
2697 BS3REG r14; /**< 0x70 */
2698 BS3REG r15; /**< 0x78 */
2699 BS3REG rflags; /**< 0x80 */
2700 BS3REG rip; /**< 0x88 */
2701 uint16_t cs; /**< 0x90 */
2702 uint16_t ds; /**< 0x92 */
2703 uint16_t es; /**< 0x94 */
2704 uint16_t fs; /**< 0x96 */
2705 uint16_t gs; /**< 0x98 */
2706 uint16_t ss; /**< 0x9a */
2707 uint16_t tr; /**< 0x9c */
2708 uint16_t ldtr; /**< 0x9e */
2709 uint8_t bMode; /**< 0xa0: BS3_MODE_XXX. */
2710 uint8_t bCpl; /**< 0xa1: 0-3, 0 is used for real mode. */
2711 uint8_t fbFlags; /**< 0xa2: BS3REG_CTX_F_XXX */
2712 uint8_t abPadding[3]; /**< 0xa3 */
2713 uint16_t cr2Range; /**< 0xa6 */
2714 BS3REG cr0; /**< 0xa8 */
2715 BS3REG cr2; /**< 0xb0 */
2716 BS3REG cr3; /**< 0xb8 */
2717 BS3REG cr4; /**< 0xc0 */
2718 uint64_t uUnused; /**< 0xc8 */
2719} BS3REGCTX;
2720AssertCompileSize(BS3REGCTX, 0xd0);
2721/** Pointer to a register context. */
2722typedef BS3REGCTX BS3_FAR *PBS3REGCTX;
2723/** Pointer to a const register context. */
2724typedef BS3REGCTX const BS3_FAR *PCBS3REGCTX;
2725
2726/** @name BS3REG_CTX_F_XXX - BS3REGCTX::fbFlags masks.
2727 * @{ */
2728/** The CR0 is MSW (only low 16-bit). */
2729#define BS3REG_CTX_F_NO_CR0_IS_MSW UINT8_C(0x01)
2730/** No CR2 and CR3 values. Not in CPL 0 or CPU too old for CR2 & CR3. */
2731#define BS3REG_CTX_F_NO_CR2_CR3 UINT8_C(0x02)
2732/** No CR4 value. The CPU is too old for CR4. */
2733#define BS3REG_CTX_F_NO_CR4 UINT8_C(0x04)
2734/** No TR and LDTR values. Context gathered in real mode or v8086 mode. */
2735#define BS3REG_CTX_F_NO_TR_LDTR UINT8_C(0x08)
2736/** The context doesn't have valid values for AMD64 GPR extensions. */
2737#define BS3REG_CTX_F_NO_AMD64 UINT8_C(0x10)
2738/** @} */
2739
2740/**
2741 * Saves the current register context.
2742 *
2743 * @param pRegCtx Where to store the register context.
2744 */
2745BS3_CMN_PROTO_NOSB(void, Bs3RegCtxSave,(PBS3REGCTX pRegCtx));
2746
2747/**
2748 * Switch to the specified CPU bitcount, reserve additional stack and save the
2749 * CPU context.
2750 *
2751 * This is for writing more flexible test drivers that can test more than the
2752 * CPU bitcount (16-bit, 32-bit, 64-bit, and virtual 8086) of the driver itself.
2753 * For instance a 32-bit driver can do V86 and 16-bit testing, thus saving space
2754 * by avoiding duplicate 16-bit driver code.
2755 *
2756 * @param pRegCtx Where to store the register context.
2757 * @param bBitMode Bit mode to switch to, BS3_MODE_CODE_XXX. Only
2758 * BS3_MODE_CODE_MASK is used, other bits are ignored
2759 * to make it possible to pass a full mode value.
2760 * @param cbExtraStack Number of bytes of additional stack to allocate.
2761 */
2762BS3_CMN_PROTO_FARSTUB(8, void, Bs3RegCtxSaveEx,(PBS3REGCTX pRegCtx, uint8_t bBitMode, uint16_t cbExtraStack));
2763
2764/**
2765 * This is Bs3RegCtxSaveEx with automatic Bs3RegCtxConvertV86ToRm thrown in.
2766 *
2767 * This is for simplifying writing 32-bit test drivers that covers real-mode as
2768 * well as virtual 8086, 16-bit, 32-bit, and 64-bit modes.
2769 *
2770 * @param pRegCtx Where to store the register context.
2771 * @param bMode The mode to get a context for. If this isn't
2772 * BS3_MODE_RM, the BS3_MODE_SYS_MASK has to match the
2773 * one of the current mode.
2774 * @param cbExtraStack Number of bytes of additional stack to allocate.
2775 */
2776BS3_CMN_PROTO_STUB(void, Bs3RegCtxSaveForMode,(PBS3REGCTX pRegCtx, uint8_t bMode, uint16_t cbExtraStack));
2777
2778/**
2779 * Transforms a register context to a different ring.
2780 *
2781 * @param pRegCtx The register context.
2782 * @param bRing The target ring (0..3).
2783 *
2784 * @note Do _NOT_ call this for creating real mode or v8086 contexts, because
2785 * it will always output a protected mode context!
2786 */
2787BS3_CMN_PROTO_STUB(void, Bs3RegCtxConvertToRingX,(PBS3REGCTX pRegCtx, uint8_t bRing));
2788
2789/**
2790 * Transforms a V8086 register context to a real mode one.
2791 *
2792 * @param pRegCtx The register context.
2793 *
2794 * @note Will assert if called on a non-V8086 context.
2795 */
2796BS3_CMN_PROTO_STUB(void, Bs3RegCtxConvertV86ToRm,(PBS3REGCTX pRegCtx));
2797
2798/**
2799 * Restores a register context.
2800 *
2801 * @param pRegCtx The register context to be restored and resumed.
2802 * @param fFlags BS3REGCTXRESTORE_F_XXX.
2803 *
2804 * @remarks Will switch to ring-0.
2805 * @remarks Does not return.
2806 */
2807BS3_CMN_PROTO_NOSB(DECL_NO_RETURN(void), Bs3RegCtxRestore,(PCBS3REGCTX pRegCtx, uint16_t fFlags));
2808#if !defined(BS3_KIT_WITH_NO_RETURN) && defined(__WATCOMC__)
2809# pragma aux Bs3RegCtxRestore_c16 "_Bs3RegCtxRestore_aborts_c16" __aborts
2810# pragma aux Bs3RegCtxRestore_f16 "_Bs3RegCtxRestore_aborts_f16" __aborts
2811# pragma aux Bs3RegCtxRestore_c32 "_Bs3RegCtxRestore_aborts_c32" __aborts
2812#endif
2813
2814/** @name Flags for Bs3RegCtxRestore
2815 * @{ */
2816/** Skip restoring the CRx registers. */
2817#define BS3REGCTXRESTORE_F_SKIP_CRX UINT16_C(0x0001)
2818/** Sets g_fBs3TrapNoV86Assist. */
2819#define BS3REGCTXRESTORE_F_NO_V86_ASSIST UINT16_C(0x0002)
2820/** @} */
2821
2822/**
2823 * Prints the register context.
2824 *
2825 * @param pRegCtx The register context to be printed.
2826 */
2827BS3_CMN_PROTO_STUB(void, Bs3RegCtxPrint,(PCBS3REGCTX pRegCtx));
2828
2829/**
2830 * Sets a GPR and segment register to point at the same location as @a uFlat.
2831 *
2832 * @param pRegCtx The register context.
2833 * @param pGpr The general purpose register to set (points within
2834 * @a pRegCtx).
2835 * @param pSel The selector register (points within @a pRegCtx).
2836 * @param uFlat Flat location address.
2837 */
2838BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpSegFromFlat,(PBS3REGCTX pRegCtx, PBS3REG pGpr, PRTSEL pSel, RTCCUINTXREG uFlat));
2839
2840/**
2841 * Sets a GPR and segment register to point at the same location as @a ovPtr.
2842 *
2843 * @param pRegCtx The register context.
2844 * @param pGpr The general purpose register to set (points within
2845 * @a pRegCtx).
2846 * @param pSel The selector register (points within @a pRegCtx).
2847 * @param pvPtr Current context pointer.
2848 */
2849BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpSegFromCurPtr,(PBS3REGCTX pRegCtx, PBS3REG pGpr, PRTSEL pSel, void BS3_FAR *pvPtr));
2850
2851/**
2852 * Sets a GPR and DS to point at the same location as @a pvPtr.
2853 *
2854 * @param pRegCtx The register context.
2855 * @param pGpr The general purpose register to set (points within
2856 * @a pRegCtx).
2857 * @param pvPtr Current context pointer.
2858 */
2859BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpDsFromCurPtr,(PBS3REGCTX pRegCtx, PBS3REG pGpr, void BS3_FAR *pvPtr));
2860
2861/**
2862 * Sets CS:RIP to point at the same piece of code as @a uFlatCode.
2863 *
2864 * @param pRegCtx The register context.
2865 * @param uFlatCode Flat code pointer
2866 * @sa Bs3RegCtxSetRipCsFromLnkPtr, Bs3RegCtxSetRipCsFromCurPtr
2867 */
2868BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromFlat,(PBS3REGCTX pRegCtx, RTCCUINTXREG uFlatCode));
2869
2870/**
2871 * Sets CS:RIP to point at the same piece of code as @a pfnCode.
2872 *
2873 * The 16-bit edition of this function expects a far 16:16 address as written by
2874 * the linker (i.e. real mode).
2875 *
2876 * @param pRegCtx The register context.
2877 * @param pfnCode Pointer to the code. In 32-bit and 64-bit mode this is a
2878 * flat address, while in 16-bit it's a far 16:16 address
2879 * as fixed up by the linker (real mode selector). This
2880 * address is converted to match the mode of the context.
2881 * @sa Bs3RegCtxSetRipCsFromCurPtr, Bs3RegCtxSetRipCsFromFlat
2882 */
2883BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromLnkPtr,(PBS3REGCTX pRegCtx, FPFNBS3FAR pfnCode));
2884
2885/**
2886 * Sets CS:RIP to point at the same piece of code as @a pfnCode.
2887 *
2888 * @param pRegCtx The register context.
2889 * @param pfnCode Pointer to the code. Current mode pointer.
2890 * @sa Bs3RegCtxSetRipCsFromLnkPtr, Bs3RegCtxSetRipCsFromFlat
2891 */
2892BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromCurPtr,(PBS3REGCTX pRegCtx, FPFNBS3FAR pfnCode));
2893
2894/**
2895 * Sets a GPR by number.
2896 *
2897 * @return true if @a iGpr is valid, false if not.
2898 * @param pRegCtx The register context.
2899 * @param iGpr The GPR number.
2900 * @param uValue The new value.
2901 * @param cbValue The size of the value: 1, 2, 4 or 8.
2902 */
2903BS3_CMN_PROTO_STUB(bool, Bs3RegCtxSetGpr,(PBS3REGCTX pRegCtx, uint8_t iGpr, uint64_t uValue, uint8_t cb));
2904
2905/**
2906 * Gets the stack pointer as a current context pointer.
2907 *
2908 * @return Pointer to the top of the stack. NULL on failure.
2909 * @param pRegCtx The register context.
2910 */
2911BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3RegCtxGetRspSsAsCurPtr,(PBS3REGCTX pRegCtx));
2912
2913
2914/**
2915 * The method to be used to save and restore the extended context.
2916 */
2917typedef enum BS3EXTCTXMETHOD
2918{
2919 BS3EXTCTXMETHOD_INVALID = 0,
2920 BS3EXTCTXMETHOD_ANCIENT, /**< Ancient fnsave/frstor format. */
2921 BS3EXTCTXMETHOD_FXSAVE, /**< fxsave/fxrstor format. */
2922 BS3EXTCTXMETHOD_XSAVE, /**< xsave/xrstor format. */
2923 BS3EXTCTXMETHOD_END,
2924} BS3EXTCTXMETHOD;
2925
2926
2927/**
2928 * Extended CPU context (FPU, SSE, AVX, ++).
2929 *
2930 * @remarks Also in bs3kit.inc
2931 */
2932typedef struct BS3EXTCTX
2933{
2934 /** Dummy/magic value. */
2935 uint16_t u16Magic;
2936 /** The size of the structure. */
2937 uint16_t cb;
2938 /** The method used to save and restore the context (BS3EXTCTXMETHOD). */
2939 uint8_t enmMethod;
2940 uint8_t abPadding0[3];
2941 /** Nominal XSAVE_C_XXX. */
2942 uint64_t fXcr0Nominal;
2943 /** The saved XCR0 mask (restored after xrstor). */
2944 uint64_t fXcr0Saved;
2945
2946 /** Explicit alignment padding. */
2947 uint8_t abPadding[64 - 2 - 2 - 1 - 3 - 8 - 8];
2948
2949 /** The context, variable size (see above).
2950 * This must be aligned on a 64 byte boundrary. */
2951 union
2952 {
2953 /** fnsave/frstor. */
2954 X86FPUSTATE Ancient;
2955 /** fxsave/fxrstor */
2956 X86FXSTATE x87;
2957 /** xsave/xrstor */
2958 X86XSAVEAREA x;
2959 /** Byte array view. */
2960 uint8_t ab[sizeof(X86XSAVEAREA)];
2961 } Ctx;
2962} BS3EXTCTX;
2963AssertCompileMemberAlignment(BS3EXTCTX, Ctx, 64);
2964/** Pointer to an extended CPU context. */
2965typedef BS3EXTCTX BS3_FAR *PBS3EXTCTX;
2966/** Pointer to a const extended CPU context. */
2967typedef BS3EXTCTX const BS3_FAR *PCBS3EXTCTX;
2968
2969/** Magic value for BS3EXTCTX. */
2970#define BS3EXTCTX_MAGIC UINT16_C(0x1980)
2971
2972/**
2973 * Allocates and initializes the extended CPU context structure.
2974 *
2975 * @returns The new extended CPU context structure.
2976 * @param enmKind The kind of allocation to make.
2977 */
2978BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxAlloc,(BS3MEMKIND enmKind));
2979
2980/**
2981 * Frees an extended CPU context structure.
2982 *
2983 * @param pExtCtx The extended CPU context (returned by
2984 * Bs3ExtCtxAlloc).
2985 */
2986BS3_CMN_PROTO_STUB(void, Bs3ExtCtxFree,(PBS3EXTCTX pExtCtx));
2987
2988/**
2989 * Get the size required for a BS3EXTCTX structure.
2990 *
2991 * @returns size in bytes of the whole structure.
2992 * @param pfFlags Where to return flags for Bs3ExtCtxInit.
2993 * @note Use Bs3ExtCtxAlloc when possible.
2994 */
2995BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetSize,(uint64_t *pfFlags));
2996
2997/**
2998 * Initializes the extended CPU context structure.
2999 * @returns pExtCtx
3000 * @param pExtCtx The extended CPU context.
3001 * @param cbExtCtx The size of the @a pExtCtx allocation.
3002 * @param fFlags XSAVE_C_XXX flags.
3003 */
3004BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxInit,(PBS3EXTCTX pExtCtx, uint16_t cbExtCtx, uint64_t fFlags));
3005
3006/**
3007 * Saves the extended CPU state to the given structure.
3008 *
3009 * @param pExtCtx The extended CPU context.
3010 * @remarks All GPRs preserved.
3011 */
3012BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxSave,(PBS3EXTCTX pExtCtx));
3013
3014/**
3015 * Saves the extended CPU state to the given structure, when in long mode this
3016 * is done from 64-bit mode to capture YMM8 thru YMM15.
3017 *
3018 * This is for testing 64-bit code from a 32-bit test driver.
3019 *
3020 * @param pExtCtx The extended CPU context.
3021 * @note Only safe to call from ring-0 at present.
3022 * @remarks All GPRs preserved.
3023 * @sa Bs3ExtCtxRestoreEx
3024 */
3025BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxSaveEx,(PBS3EXTCTX pExtCtx));
3026
3027/**
3028 * Restores the extended CPU state from the given structure.
3029 *
3030 * @param pExtCtx The extended CPU context.
3031 * @remarks All GPRs preserved.
3032 */
3033BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxRestore,(PCBS3EXTCTX pExtCtx));
3034
3035/**
3036 * Restores the extended CPU state from the given structure and in long mode
3037 * switch to 64-bit mode to do this so YMM8-YMM15 are also loaded.
3038 *
3039 * This is for testing 64-bit code from a 32-bit test driver.
3040 *
3041 * @param pExtCtx The extended CPU context.
3042 * @note Only safe to call from ring-0 at present.
3043 * @remarks All GPRs preserved.
3044 * @sa Bs3ExtCtxSaveEx
3045 */
3046BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxRestoreEx,(PCBS3EXTCTX pExtCtx));
3047
3048/**
3049 * Copies the state from one context to another.
3050 *
3051 * @returns pDst
3052 * @param pDst The destination extended CPU context.
3053 * @param pSrc The source extended CPU context.
3054 */
3055BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxCopy,(PBS3EXTCTX pDst, PCBS3EXTCTX pSrc));
3056
3057/**
3058 * Gets the FCW register value from @a pExtCtx.
3059 *
3060 * @returns FCW value.
3061 * @param pExtCtx The extended CPU context.
3062 */
3063BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetFcw,(PCBS3EXTCTX pExtCtx));
3064
3065/**
3066 * Sets the FCW register value in @a pExtCtx.
3067 *
3068 * @param pExtCtx The extended CPU context.
3069 * @param uValue The new FCW value.
3070 */
3071BS3_CMN_PROTO_STUB(void, Bs3ExtCtxSetFcw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3072
3073/**
3074 * Gets the FSW register value from @a pExtCtx.
3075 *
3076 * @returns FSW value.
3077 * @param pExtCtx The extended CPU context.
3078 */
3079BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetFsw,(PCBS3EXTCTX pExtCtx));
3080
3081/**
3082 * Sets the FSW register value in @a pExtCtx.
3083 *
3084 * @param pExtCtx The extended CPU context.
3085 * @param uValue The new FSW value.
3086 */
3087BS3_CMN_PROTO_STUB(void, Bs3ExtCtxSetFsw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3088
3089/**
3090 * Gets the abridged FTW register value from @a pExtCtx.
3091 *
3092 * @returns FTW value.
3093 * @param pExtCtx The extended CPU context.
3094 */
3095BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetAbridgedFtw,(PCBS3EXTCTX pExtCtx));
3096
3097/**
3098 * Sets the abridged FTW register value in @a pExtCtx.
3099 *
3100 * Currently this requires that the state stores teh abridged FTW, no conversion
3101 * to the two-bit variant will be attempted.
3102 *
3103 * @returns true if set successfully, false if not.
3104 * @param pExtCtx The extended CPU context.
3105 * @param uValue The new FTW value.
3106 */
3107BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetAbridgedFtw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3108
3109/**
3110 * Gets the MXCSR register value from @a pExtCtx.
3111 *
3112 * @returns MXCSR value, 0 if not part of context.
3113 * @param pExtCtx The extended CPU context.
3114 */
3115BS3_CMN_PROTO_STUB(uint32_t, Bs3ExtCtxGetMxCsr,(PCBS3EXTCTX pExtCtx));
3116
3117/**
3118 * Sets the MXCSR register value in @a pExtCtx.
3119 *
3120 * @returns true if set, false if not supported by the format.
3121 * @param pExtCtx The extended CPU context.
3122 * @param uValue The new MXCSR value.
3123 */
3124BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMxCsr,(PBS3EXTCTX pExtCtx, uint32_t uValue));
3125
3126/**
3127 * Gets the MXCSR MASK value from @a pExtCtx.
3128 *
3129 * @returns MXCSR MASK value, 0 if not part of context.
3130 * @param pExtCtx The extended CPU context.
3131 */
3132BS3_CMN_PROTO_STUB(uint32_t, Bs3ExtCtxGetMxCsrMask,(PCBS3EXTCTX pExtCtx));
3133
3134/**
3135 * Sets the MXCSR MASK value in @a pExtCtx.
3136 *
3137 * @returns true if set, false if not supported by the format.
3138 * @param pExtCtx The extended CPU context.
3139 * @param uValue The new MXCSR MASK value.
3140 */
3141BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMxCsrMask,(PBS3EXTCTX pExtCtx, uint32_t uValue));
3142
3143/**
3144 * Gets the value of MM register number @a iReg from @a pExtCtx.
3145 *
3146 * @returns The MM register value.
3147 * @param pExtCtx The extended CPU context.
3148 * @param iReg The register to get (0 thru 7).
3149 */
3150BS3_CMN_PROTO_STUB(uint64_t, Bs3ExtCtxGetMm,(PCBS3EXTCTX pExtCtx, uint8_t iReg));
3151
3152/** What to do about the 16-bit above the MM QWORD. */
3153typedef enum BS3EXTCTXTOPMM
3154{
3155 /** Invalid zero value. */
3156 BS3EXTCTXTOPMM_INVALID = 0,
3157 /** Set to 0FFFFh like real CPUs typically does when updating an MM register. */
3158 BS3EXTCTXTOPMM_SET,
3159 /** Set to zero. */
3160 BS3EXTCTXTOPMM_ZERO,
3161 /** Don't change the value, leaving it as-is. */
3162 BS3EXTCTXTOPMM_AS_IS,
3163 /** End of valid values. */
3164 BS3EXTCTXTOPMM_END
3165} BS3EXTCTXTOPMM;
3166
3167/**
3168 * Sets the value of YMM register number @a iReg in @a pExtCtx to @a pValue.
3169 *
3170 * @returns True if set, false if not.
3171 * @param pExtCtx The extended CPU context.
3172 * @param iReg The register to set.
3173 * @param uValue The new register value.
3174 * @param enmTop What to do about the 16-bit value above the MM
3175 * QWord.
3176 */
3177BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMm,(PBS3EXTCTX pExtCtx, uint8_t iReg, uint64_t uValue, BS3EXTCTXTOPMM enmTop));
3178
3179/**
3180 * Gets the value of XMM register number @a iReg from @a pExtCtx.
3181 *
3182 * @returns pValue
3183 * @param pExtCtx The extended CPU context.
3184 * @param iReg The register to get.
3185 * @param pValue Where to return the value. Zeroed if the state
3186 * doesn't support SSE or if @a iReg is invalid.
3187 */
3188BS3_CMN_PROTO_STUB(PRTUINT128U, Bs3ExtCtxGetXmm,(PCBS3EXTCTX pExtCtx, uint8_t iReg, PRTUINT128U pValue));
3189
3190/**
3191 * Sets the value of XMM register number @a iReg in @a pExtCtx to @a pValue.
3192 *
3193 * @returns True if set, false if not set (not supported by state format or
3194 * invalid iReg).
3195 * @param pExtCtx The extended CPU context.
3196 * @param iReg The register to set.
3197 * @param pValue The new register value.
3198 */
3199BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetXmm,(PBS3EXTCTX pExtCtx, uint8_t iReg, PCRTUINT128U pValue));
3200
3201/**
3202 * Gets the value of YMM register number @a iReg from @a pExtCtx.
3203 *
3204 * @returns pValue
3205 * @param pExtCtx The extended CPU context.
3206 * @param iReg The register to get.
3207 * @param pValue Where to return the value. Parts not in the
3208 * extended state are zeroed. For absent or invalid
3209 * @a iReg values this is set to zero.
3210 */
3211BS3_CMN_PROTO_STUB(PRTUINT256U, Bs3ExtCtxGetYmm,(PCBS3EXTCTX pExtCtx, uint8_t iReg, PRTUINT256U pValue));
3212
3213/**
3214 * Sets the value of YMM register number @a iReg in @a pExtCtx to @a pValue.
3215 *
3216 * @returns true if set (even if only partially). False if not set (not
3217 * supported by state format, unsupported/invalid iReg).
3218 * @param pExtCtx The extended CPU context.
3219 * @param iReg The register to set.
3220 * @param pValue The new register value.
3221 * @param cbValue Number of bytes to take from @a pValue, either 16 or
3222 * 32. If 16, the high part will be zeroed when present
3223 * in the state.
3224 */
3225BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetYmm,(PBS3EXTCTX pExtCtx, uint8_t iReg, PCRTUINT256U pValue, uint8_t cbValue));
3226
3227
3228/** @name Debug register accessors for V8086 mode (works everwhere).
3229 * @{ */
3230BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr0,(void));
3231BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr1,(void));
3232BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr2,(void));
3233BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr3,(void));
3234BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr6,(void));
3235BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr7,(void));
3236
3237BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr0,(RTCCUINTXREG uValue));
3238BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr1,(RTCCUINTXREG uValue));
3239BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr2,(RTCCUINTXREG uValue));
3240BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr3,(RTCCUINTXREG uValue));
3241BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr6,(RTCCUINTXREG uValue));
3242BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr7,(RTCCUINTXREG uValue));
3243
3244BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDrX,(uint8_t iReg));
3245BS3_CMN_PROTO_NOSB(void, Bs3RegSetDrX,(uint8_t iReg, RTCCUINTXREG uValue));
3246/** @} */
3247
3248
3249/** @name Control register accessors for V8086 mode (works everwhere).
3250 * @{ */
3251BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr0,(void));
3252BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr2,(void));
3253BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr3,(void));
3254BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr4,(void));
3255BS3_CMN_PROTO_NOSB(uint16_t, Bs3RegGetTr,(void));
3256BS3_CMN_PROTO_NOSB(uint16_t, Bs3RegGetLdtr,(void));
3257BS3_CMN_PROTO_NOSB(uint64_t, Bs3RegGetXcr0,(void));
3258
3259BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr0,(RTCCUINTXREG uValue));
3260BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr2,(RTCCUINTXREG uValue));
3261BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr3,(RTCCUINTXREG uValue));
3262BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr4,(RTCCUINTXREG uValue));
3263BS3_CMN_PROTO_NOSB(void, Bs3RegSetTr,(uint16_t uValue));
3264BS3_CMN_PROTO_NOSB(void, Bs3RegSetLdtr,(uint16_t uValue));
3265BS3_CMN_PROTO_NOSB(void, Bs3RegSetXcr0,(uint64_t uValue));
3266/** @} */
3267
3268
3269/**
3270 * Trap frame.
3271 */
3272typedef struct BS3TRAPFRAME
3273{
3274 /** 0x00: Exception/interrupt number. */
3275 uint8_t bXcpt;
3276 /** 0x01: The size of the IRET frame. */
3277 uint8_t cbIretFrame;
3278 /** 0x02: The handler CS. */
3279 uint16_t uHandlerCs;
3280 /** 0x04: The handler SS. */
3281 uint16_t uHandlerSs;
3282 /** 0x06: Explicit alignment. */
3283 uint16_t usAlignment;
3284 /** 0x08: The handler RSP (pointer to the iret frame, skipping ErrCd). */
3285 uint64_t uHandlerRsp;
3286 /** 0x10: The handler RFLAGS value. */
3287 uint64_t fHandlerRfl;
3288 /** 0x18: The error code (if applicable). */
3289 uint64_t uErrCd;
3290 /** 0x20: The register context. */
3291 BS3REGCTX Ctx;
3292} BS3TRAPFRAME;
3293AssertCompileSize(BS3TRAPFRAME, 0x20 + 0xd0);
3294/** Pointer to a trap frame. */
3295typedef BS3TRAPFRAME BS3_FAR *PBS3TRAPFRAME;
3296/** Pointer to a const trap frame. */
3297typedef BS3TRAPFRAME const BS3_FAR *PCBS3TRAPFRAME;
3298
3299
3300/**
3301 * Re-initializes the trap handling for the current mode.
3302 *
3303 * Useful after a test that messes with the IDT/IVT.
3304 *
3305 * @sa Bs3TrapInit
3306 */
3307BS3_CMN_PROTO_STUB(void, Bs3TrapReInit,(void));
3308
3309/**
3310 * Initializes real mode and v8086 trap handling.
3311 *
3312 * @remarks Does not install RM/V86 trap handling, just initializes the
3313 * structures.
3314 */
3315BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86Init,(void));
3316
3317/**
3318 * Initializes real mode and v8086 trap handling, extended version.
3319 *
3320 * @param f386Plus Set if the CPU is 80386 or later and
3321 * extended registers should be saved. Once initialized
3322 * with this parameter set to @a true, the effect cannot be
3323 * reversed.
3324 *
3325 * @remarks Does not install RM/V86 trap handling, just initializes the
3326 * structures.
3327 */
3328BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86InitEx,(bool f386Plus));
3329
3330/**
3331 * Initializes 16-bit (protected mode) trap handling.
3332 *
3333 * @remarks Does not install 16-bit trap handling, just initializes the
3334 * structures.
3335 */
3336BS3_CMN_PROTO_STUB(void, Bs3Trap16Init,(void));
3337
3338/**
3339 * Initializes 16-bit (protected mode) trap handling, extended version.
3340 *
3341 * @param f386Plus Set if the CPU is 80386 or later and
3342 * extended registers should be saved. Once initialized
3343 * with this parameter set to @a true, the effect cannot be
3344 * reversed.
3345 *
3346 * @remarks Does not install 16-bit trap handling, just initializes the
3347 * structures.
3348 */
3349BS3_CMN_PROTO_STUB(void, Bs3Trap16InitEx,(bool f386Plus));
3350
3351/**
3352 * Initializes 32-bit trap handling.
3353 *
3354 * @remarks Does not install 32-bit trap handling, just initializes the
3355 * structures.
3356 */
3357BS3_CMN_PROTO_STUB(void, Bs3Trap32Init,(void));
3358
3359/**
3360 * Initializes 64-bit trap handling
3361 *
3362 * @remarks Does not install 64-bit trap handling, just initializes the
3363 * structures.
3364 */
3365BS3_CMN_PROTO_STUB(void, Bs3Trap64Init,(void));
3366
3367/**
3368 * Initializes 64-bit trap handling, extended version.
3369 *
3370 * @remarks Does not install 64-bit trap handling, just initializes the
3371 * structures.
3372 * @param fMoreIstUsage Use the interrupt stacks for more CPU exceptions.
3373 * Default (false) is to only IST1 for the double fault
3374 * handler and the rest uses IST0.
3375 */
3376BS3_CMN_PROTO_STUB(void, Bs3Trap64InitEx,(bool fMoreIstUsage));
3377
3378/**
3379 * Modifies the real-mode / V86 IVT entry specified by @a iIvt.
3380 *
3381 * @param iIvt The index of the IDT entry to set.
3382 * @param uSeg The handler real-mode segment.
3383 * @param off The handler offset.
3384 */
3385BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86SetGate,(uint8_t iIvt, uint16_t uSeg, uint16_t off));
3386
3387/**
3388 * Modifies the 16-bit IDT entry (protected mode) specified by @a iIdt.
3389 *
3390 * @param iIdt The index of the IDT entry to set.
3391 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3392 * @param bDpl The DPL.
3393 * @param uSel The handler selector.
3394 * @param off The handler offset (if applicable).
3395 * @param cParams The parameter count (for call gates).
3396 */
3397BS3_CMN_PROTO_STUB(void, Bs3Trap16SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl,
3398 uint16_t uSel, uint16_t off, uint8_t cParams));
3399
3400/** The address of Bs3Trap16GenericEntries.
3401 * Bs3Trap16GenericEntries is an array of interrupt/trap/whatever entry
3402 * points, 8 bytes each, that will create a register frame and call the generic
3403 * C compatible trap handlers. */
3404extern uint32_t g_Bs3Trap16GenericEntriesFlatAddr;
3405
3406/**
3407 * Modifies the 32-bit IDT entry specified by @a iIdt.
3408 *
3409 * @param iIdt The index of the IDT entry to set.
3410 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3411 * @param bDpl The DPL.
3412 * @param uSel The handler selector.
3413 * @param off The handler offset (if applicable).
3414 * @param cParams The parameter count (for call gates).
3415 */
3416BS3_CMN_PROTO_STUB(void, Bs3Trap32SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl,
3417 uint16_t uSel, uint32_t off, uint8_t cParams));
3418
3419/** The address of Bs3Trap32GenericEntries.
3420 * Bs3Trap32GenericEntries is an array of interrupt/trap/whatever entry
3421 * points, 10 bytes each, that will create a register frame and call the generic
3422 * C compatible trap handlers. */
3423extern uint32_t g_Bs3Trap32GenericEntriesFlatAddr;
3424
3425/**
3426 * Modifies the 64-bit IDT entry specified by @a iIdt.
3427 *
3428 * @param iIdt The index of the IDT entry to set.
3429 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3430 * @param bDpl The DPL.
3431 * @param uSel The handler selector.
3432 * @param off The handler offset (if applicable).
3433 * @param bIst The interrupt stack to use.
3434 */
3435BS3_CMN_PROTO_STUB(void, Bs3Trap64SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint64_t off, uint8_t bIst));
3436
3437/** The address of Bs3Trap64GenericEntries.
3438 * Bs3Trap64GenericEntries is an array of interrupt/trap/whatever entry
3439 * points, 8 bytes each, that will create a register frame and call the generic
3440 * C compatible trap handlers. */
3441extern uint32_t g_Bs3Trap64GenericEntriesFlatAddr;
3442
3443/**
3444 * Adjusts the DPL the IDT entry specified by @a iIdt.
3445 *
3446 * The change is applied to the 16-bit, 32-bit and 64-bit IDTs.
3447 *
3448 * @returns Old DPL (from 64-bit IDT).
3449 * @param iIdt The index of the IDT and IVT entry to set.
3450 * @param bDpl The DPL.
3451 */
3452BS3_CMN_PROTO_STUB(uint8_t, Bs3TrapSetDpl,(uint8_t iIdt, uint8_t bDpl));
3453
3454/**
3455 * C-style trap handler.
3456 *
3457 * The caller will resume the context in @a pTrapFrame upon return.
3458 *
3459 * @param pTrapFrame The trap frame. Registers can be modified.
3460 * @note The 16-bit versions must be in CGROUP16!
3461 */
3462typedef BS3_DECL_NEAR_CALLBACK(void) FNBS3TRAPHANDLER(PBS3TRAPFRAME pTrapFrame);
3463/** Pointer to a trap handler (current template context). */
3464typedef FNBS3TRAPHANDLER *PFNBS3TRAPHANDLER;
3465
3466#if ARCH_BITS == 16
3467/** @copydoc FNBS3TRAPHANDLER */
3468typedef FNBS3FAR FNBS3TRAPHANDLER32;
3469/** @copydoc FNBS3TRAPHANDLER */
3470typedef FNBS3FAR FNBS3TRAPHANDLER64;
3471#else
3472/** @copydoc FNBS3TRAPHANDLER */
3473typedef FNBS3TRAPHANDLER FNBS3TRAPHANDLER32;
3474/** @copydoc FNBS3TRAPHANDLER */
3475typedef FNBS3TRAPHANDLER FNBS3TRAPHANDLER64;
3476#endif
3477/** @copydoc PFNBS3TRAPHANDLER */
3478typedef FNBS3TRAPHANDLER32 *PFNBS3TRAPHANDLER32;
3479/** @copydoc PFNBS3TRAPHANDLER */
3480typedef FNBS3TRAPHANDLER64 *PFNBS3TRAPHANDLER64;
3481
3482
3483/**
3484 * C-style trap handler, near 16-bit (CGROUP16).
3485 *
3486 * The caller will resume the context in @a pTrapFrame upon return.
3487 *
3488 * @param pTrapFrame The trap frame. Registers can be modified.
3489 */
3490typedef BS3_DECL_NEAR_CALLBACK(void) FNBS3TRAPHANDLER16(PBS3TRAPFRAME pTrapFrame);
3491/** Pointer to a trap handler (current template context). */
3492typedef FNBS3TRAPHANDLER16 *PFNBS3TRAPHANDLER16;
3493
3494/**
3495 * C-style trap handler, near 16-bit (CGROUP16).
3496 *
3497 * The caller will resume the context in @a pTrapFrame upon return.
3498 *
3499 * @param pTrapFrame The trap frame. Registers can be modified.
3500 */
3501typedef BS3_DECL_CALLBACK(void) FNBS3TRAPHANDLER3264(PBS3TRAPFRAME pTrapFrame);
3502/** Pointer to a trap handler (current template context). */
3503typedef FNBS3TRAPHANDLER3264 *FPFNBS3TRAPHANDLER3264;
3504
3505
3506/**
3507 * Sets a trap handler (C/C++/assembly) for the current bitcount.
3508 *
3509 * @returns Previous handler.
3510 * @param iIdt The index of the IDT entry to set.
3511 * @param pfnHandler Pointer to the handler.
3512 * @sa Bs3TrapSetHandlerEx
3513 */
3514BS3_CMN_PROTO_STUB(PFNBS3TRAPHANDLER, Bs3TrapSetHandler,(uint8_t iIdt, PFNBS3TRAPHANDLER pfnHandler));
3515
3516/**
3517 * Sets a trap handler (C/C++/assembly) for all the bitcounts.
3518 *
3519 * @param iIdt The index of the IDT and IVT entry to set.
3520 * @param pfnHandler16 Pointer to the 16-bit handler. (Assumes linker addresses.)
3521 * @param pfnHandler32 Pointer to the 32-bit handler. (Assumes linker addresses.)
3522 * @param pfnHandler64 Pointer to the 64-bit handler. (Assumes linker addresses.)
3523 * @sa Bs3TrapSetHandler
3524 */
3525BS3_CMN_PROTO_STUB(void, Bs3TrapSetHandlerEx,(uint8_t iIdt, PFNBS3TRAPHANDLER16 pfnHandler16,
3526 PFNBS3TRAPHANDLER32 pfnHandler32, PFNBS3TRAPHANDLER64 pfnHandler64));
3527
3528/**
3529 * Default C/C++ trap handler.
3530 *
3531 * This will check trap record and panic if no match was found.
3532 *
3533 * @param pTrapFrame Trap frame of the trap to handle.
3534 */
3535BS3_CMN_PROTO_STUB(void, Bs3TrapDefaultHandler,(PBS3TRAPFRAME pTrapFrame));
3536
3537/**
3538 * Prints the trap frame (to screen).
3539 * @param pTrapFrame Trap frame to print.
3540 */
3541BS3_CMN_PROTO_STUB(void, Bs3TrapPrintFrame,(PCBS3TRAPFRAME pTrapFrame));
3542
3543/**
3544 * Sets up a long jump from a trap handler.
3545 *
3546 * The long jump will only be performed once, but will catch any kind of trap,
3547 * fault, interrupt or irq.
3548 *
3549 * @retval true on the initial call.
3550 * @retval false on trap return.
3551 * @param pTrapFrame Where to store the trap information when
3552 * returning @c false.
3553 * @sa #Bs3TrapUnsetJmp
3554 */
3555BS3_CMN_PROTO_NOSB(DECL_RETURNS_TWICE(bool),Bs3TrapSetJmp,(PBS3TRAPFRAME pTrapFrame));
3556
3557/**
3558 * Combination of #Bs3TrapSetJmp and #Bs3RegCtxRestore.
3559 *
3560 * @param pCtxRestore The context to restore.
3561 * @param pTrapFrame Where to store the trap information.
3562 */
3563BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestore,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3564
3565/**
3566 * Variation of Bs3TrapSetJmpAndRestore that includes
3567 * #Bs3TrapSetJmpAndRestoreInRm and calls is if pCtxRestore is a real mode
3568 * context and we're not in real mode.
3569 *
3570 * This is useful for 32-bit test drivers running via #Bs3TestDoModesByOne using
3571 * BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY to allow them to test real-mode too.
3572 *
3573 * @param pCtxRestore The context to restore.
3574 * @param pTrapFrame Where to store the trap information.
3575 */
3576BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithRm,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3577
3578/**
3579 * Combination of #Bs3ExtCtxRestoreEx, #Bs3TrapSetJmp, #Bs3RegCtxRestore and
3580 * #Bs3ExtCtxSaveEx.
3581 *
3582 * @param pCtxRestore The context to restore.
3583 * @param pExtCtxRestore The extended context to restore.
3584 * @param pTrapFrame Where to store the trap information.
3585 * @param pExtCtxTrap Where to store the extended context after the trap.
3586 * Note, the saving isn't done from the trap handler,
3587 * but after #Bs3TrapSetJmp returns zero (i.e. for the
3588 * 2nd time).
3589 */
3590BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithExtCtx,(PCBS3REGCTX pCtxRestore, PCBS3EXTCTX pExtCtxRestore,
3591 PBS3TRAPFRAME pTrapFrame, PBS3EXTCTX pExtCtxTrap));
3592
3593/**
3594 * Variation of Bs3TrapSetJmpAndRestoreWithExtCtx that includes
3595 * #Bs3TrapSetJmpAndRestoreInRm and calls is if pCtxRestore is a real mode
3596 * context and we're not in real mode.
3597 *
3598 * This is useful for 32-bit test drivers running via #Bs3TestDoModesByOne using
3599 * BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY to allow them to test real-mode too.
3600 *
3601 * @param pCtxRestore The context to restore.
3602 * @param pExtCtxRestore The extended context to restore.
3603 * @param pTrapFrame Where to store the trap information.
3604 * @param pExtCtxTrap Where to store the extended context after the trap.
3605 * Note, the saving isn't done from the trap handler,
3606 * but after #Bs3TrapSetJmp returns zero (i.e. for the
3607 * 2nd time).
3608 */
3609BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithExtCtxAndRm,(PCBS3REGCTX pCtxRestore, PCBS3EXTCTX pExtCtxRestore,
3610 PBS3TRAPFRAME pTrapFrame, PBS3EXTCTX pExtCtxTrap));
3611
3612/**
3613 * Combination of Bs3SwitchToRM, #Bs3TrapSetJmp and #Bs3RegCtxRestore.
3614 *
3615 * @param pCtxRestore The context to restore. Must be real-mode
3616 * addressable.
3617 * @param pTrapFrame Where to store the trap information. Must be
3618 * real-mode addressable.
3619 */
3620BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreInRm,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3621
3622/**
3623 * Disables a previous #Bs3TrapSetJmp call.
3624 */
3625BS3_CMN_PROTO_STUB(void, Bs3TrapUnsetJmp,(void));
3626
3627/** Entry point for MSR_K8_LSTAR (64-bit).
3628 * This hooks into the default Bs3TrapSetJmp logic. */
3629BS3_CMN_PROTO_NOSB(void, Bs3Syscall64Generic,(void));
3630/** The 32-bit FLAT address of Bs3Syscall64Generic (for 16-bit code). */
3631extern uint32_t g_pfnBs3Syscall64GenericFlat;
3632
3633/** Entry point for MSR_K8_CSTAR (64-bit).
3634 * This hooks into the default Bs3TrapSetJmp logic. */
3635BS3_CMN_PROTO_NOSB(void, Bs3Syscall64GenericCompatibility,(void));
3636/** The 32-bit FLAT address of Bs3Syscall64Generic (for 16-bit code). */
3637extern uint32_t g_pfnBs3Syscall64GenericCompatibilityFlat;
3638
3639
3640
3641/**
3642 * The current test step.
3643 */
3644extern uint16_t g_usBs3TestStep;
3645
3646/** Test repetitions necessary to make sure of engaging native recompilation.
3647 * @note See iemTbCacheLookup() for the actual constant. */
3648#define BS3_THRESHOLD_NATIVE_RECOMPILER 18
3649
3650/** The host configurable BS3_THRESHOLD_NATIVE_RECOMPILER value.
3651 * The host configuration is read by Bs3TestInit, before that it has the same
3652 * value as the compiletime constant. */
3653extern uint16_t g_cBs3ThresholdNativeRecompiler;
3654
3655/**
3656 * Equivalent to RTTestCreate + RTTestBanner.
3657 *
3658 * @param pszTest The test name.
3659 */
3660BS3_CMN_PROTO_STUB(void, Bs3TestInit,(const char BS3_FAR *pszTest));
3661
3662
3663/**
3664 * Equivalent to RTTestSummaryAndDestroy.
3665 */
3666BS3_CMN_PROTO_STUB(void, Bs3TestTerm,(void));
3667
3668/**
3669 * Equivalent to RTTestISub.
3670 */
3671BS3_CMN_PROTO_STUB(void, Bs3TestSub,(const char BS3_FAR *pszSubTest));
3672
3673/**
3674 * Equivalent to RTTestISubF.
3675 */
3676BS3_CMN_PROTO_STUB(void, Bs3TestSubF,(const char BS3_FAR *pszFormat, ...));
3677
3678/**
3679 * Equivalent to RTTestISubV.
3680 */
3681BS3_CMN_PROTO_STUB(void, Bs3TestSubV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3682
3683/**
3684 * Equivalent to RTTestISubDone.
3685 */
3686BS3_CMN_PROTO_STUB(void, Bs3TestSubDone,(void));
3687
3688/**
3689 * Equivalent to RTTestISubSub.
3690 */
3691BS3_CMN_PROTO_STUB(void, Bs3TestSubSub,(const char BS3_FAR *pszSubTest));
3692
3693/**
3694 * Equivalent to RTTestISubSubF.
3695 */
3696BS3_CMN_PROTO_STUB(void, Bs3TestSubSubF,(const char BS3_FAR *pszFormat, ...));
3697
3698/**
3699 * Equivalent to RTTestISubSubV.
3700 */
3701BS3_CMN_PROTO_STUB(void, Bs3TestSubSubV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3702
3703/**
3704 * Equivalent to RTTestISubSubDone.
3705 */
3706BS3_CMN_PROTO_STUB(void, Bs3TestSubSubDone,(void));
3707
3708/**
3709 * Equivalent to RTTestIValue.
3710 */
3711BS3_CMN_PROTO_STUB(void, Bs3TestValue,(const char BS3_FAR *pszName, uint64_t u64Value, uint8_t bUnit));
3712
3713/**
3714 * Equivalent to RTTestSubErrorCount.
3715 */
3716BS3_CMN_PROTO_STUB(uint16_t, Bs3TestSubErrorCount,(void));
3717
3718/**
3719 * Get nanosecond host timestamp.
3720 *
3721 * This only works when testing is enabled and will not work in VMs configured
3722 * with a 286, 186 or 8086/8088 CPU profile.
3723 */
3724BS3_CMN_PROTO_STUB(uint64_t, Bs3TestNow,(void));
3725
3726
3727/**
3728 * Queries an unsigned 8-bit configuration value.
3729 *
3730 * @returns Value.
3731 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3732 * @param bDefault The default value to return if the VMMDev isn't
3733 * available or the query failed.
3734 */
3735BS3_CMN_PROTO_STUB(uint8_t, Bs3TestQueryCfgU8,(uint16_t uCfg, uint8_t bDefault));
3736
3737/**
3738 * Queries a boolean configuration value.
3739 *
3740 * @returns Value.
3741 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3742 * @param fDefault The default value to return if the VMMDev isn't
3743 * available or the query failed.
3744 */
3745BS3_CMN_PROTO_STUB(bool, Bs3TestQueryCfgBool,(uint16_t uCfg, bool fDefault));
3746
3747/**
3748 * Queries an unsigned 16-bit configuration value.
3749 *
3750 * @returns Value.
3751 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3752 * @param uDefault The default value to return if the VMMDev isn't
3753 * available or the query failed.
3754 */
3755BS3_CMN_PROTO_STUB(uint16_t, Bs3TestQueryCfgU16,(uint16_t uCfg, uint16_t uDefault));
3756
3757/**
3758 * Queries an unsigned 32-bit configuration value.
3759 *
3760 * @returns Value.
3761 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3762 * @param uDefault The default value to return if the VMMDev isn't
3763 * available or the query failed.
3764 */
3765BS3_CMN_PROTO_STUB(uint32_t, Bs3TestQueryCfgU32,(uint16_t uCfg, uint32_t uDefault));
3766
3767/**
3768 * Equivalent to RTTestIPrintf with RTTESTLVL_ALWAYS.
3769 *
3770 * @param pszFormat What to print, format string. Explicit newline char.
3771 * @param ... String format arguments.
3772 */
3773BS3_CMN_PROTO_STUB(void, Bs3TestPrintf,(const char BS3_FAR *pszFormat, ...));
3774
3775/**
3776 * Equivalent to RTTestIPrintfV with RTTESTLVL_ALWAYS.
3777 *
3778 * @param pszFormat What to print, format string. Explicit newline char.
3779 * @param va String format arguments.
3780 */
3781BS3_CMN_PROTO_STUB(void, Bs3TestPrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3782
3783/**
3784 * Same as Bs3TestPrintf, except no guest screen echo.
3785 *
3786 * @param pszFormat What to print, format string. Explicit newline char.
3787 * @param ... String format arguments.
3788 */
3789BS3_CMN_PROTO_STUB(void, Bs3TestHostPrintf,(const char BS3_FAR *pszFormat, ...));
3790
3791/**
3792 * Same as Bs3TestPrintfV, except no guest screen echo.
3793 *
3794 * @param pszFormat What to print, format string. Explicit newline char.
3795 * @param va String format arguments.
3796 */
3797BS3_CMN_PROTO_STUB(void, Bs3TestHostPrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3798
3799/**
3800 * Equivalent to RTTestIFailed.
3801 * @returns false.
3802 */
3803BS3_CMN_PROTO_STUB(bool, Bs3TestFailed,(const char BS3_FAR *pszMessage));
3804
3805/**
3806 * Equivalent to RTTestIFailedF.
3807 * @returns false.
3808 */
3809BS3_CMN_PROTO_STUB(bool, Bs3TestFailedF,(const char BS3_FAR *pszFormat, ...));
3810
3811/**
3812 * Equivalent to RTTestIFailedV.
3813 * @returns false.
3814 */
3815BS3_CMN_PROTO_STUB(bool, Bs3TestFailedV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3816
3817/**
3818 * Equivalent to RTTestISkipped.
3819 *
3820 * @param pszWhy Optional reason why it's being skipped.
3821 */
3822BS3_CMN_PROTO_STUB(void, Bs3TestSkipped,(const char BS3_FAR *pszWhy));
3823
3824/**
3825 * Equivalent to RTTestISkippedF.
3826 *
3827 * @param pszFormat Optional reason why it's being skipped.
3828 * @param ... Format arguments.
3829 */
3830BS3_CMN_PROTO_STUB(void, Bs3TestSkippedF,(const char BS3_FAR *pszFormat, ...));
3831
3832/**
3833 * Equivalent to RTTestISkippedV.
3834 *
3835 * @param pszFormat Optional reason why it's being skipped.
3836 * @param va Format arguments.
3837 */
3838BS3_CMN_PROTO_STUB(void, Bs3TestSkippedV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3839
3840/**
3841 * Compares two register contexts, with PC and SP adjustments.
3842 *
3843 * Differences will be reported as test failures.
3844 *
3845 * @returns true if equal, false if not.
3846 * @param pActualCtx The actual register context.
3847 * @param pExpectedCtx Expected register context.
3848 * @param cbPcAdjust Program counter adjustment (applied to @a pExpectedCtx).
3849 * @param cbSpAdjust Stack pointer adjustment (applied to @a pExpectedCtx).
3850 * @param fExtraEfl Extra EFLAGS to OR into @a pExepctedCtx.
3851 * @param pszMode CPU mode or some other helpful text.
3852 * @param idTestStep Test step identifier.
3853 */
3854BS3_CMN_PROTO_STUB(bool, Bs3TestCheckRegCtxEx,(PCBS3REGCTX pActualCtx, PCBS3REGCTX pExpectedCtx, uint16_t cbPcAdjust,
3855 int16_t cbSpAdjust, uint32_t fExtraEfl,
3856 const char BS3_FAR *pszMode, uint16_t idTestStep));
3857
3858/**
3859 * Compares two extended register contexts.
3860 *
3861 * Differences will be reported as test failures.
3862 *
3863 * @returns true if equal, false if not.
3864 * @param pActualExtCtx The actual register context.
3865 * @param pExpectedExtCtx Expected register context.
3866 * @param fFlags Reserved, pass 0.
3867 * @param pszMode CPU mode or some other helpful text.
3868 * @param idTestStep Test step identifier.
3869 */
3870BS3_CMN_PROTO_STUB(bool, Bs3TestCheckExtCtx,(PCBS3EXTCTX pActualExtCtx, PCBS3EXTCTX pExpectedExtCtx, uint16_t fFlags,
3871 const char BS3_FAR *pszMode, uint16_t idTestStep));
3872
3873/**
3874 * Performs the testing for the given mode.
3875 *
3876 * This is called with the CPU already switch to that mode.
3877 *
3878 * @returns 0 on success or directly Bs3TestFailed calls, non-zero to indicate
3879 * where the test when wrong. Special value BS3TESTDOMODE_SKIPPED
3880 * should be returned to indicate that the test has been skipped.
3881 * @param bMode The current CPU mode.
3882 */
3883typedef BS3_DECL_CALLBACK(uint8_t) FNBS3TESTDOMODE(uint8_t bMode);
3884/** Pointer (far) to a test (for 32-bit and 64-bit code, will be flatten). */
3885typedef FNBS3TESTDOMODE *PFNBS3TESTDOMODE;
3886
3887/** Special FNBS3TESTDOMODE return code for indicating a skipped mode test. */
3888#define BS3TESTDOMODE_SKIPPED UINT8_MAX
3889
3890/**
3891 * Mode sub-test entry.
3892 *
3893 * This can only be passed around to functions with the same bit count, as it
3894 * contains function pointers. In 16-bit mode, the 16-bit pointers are near and
3895 * implies BS3TEXT16, whereas the 32-bit and 64-bit pointers are far real mode
3896 * addresses that will be converted to flat address prior to calling them.
3897 * Similarly, in 32-bit and 64-bit the addresses are all flat and the 16-bit
3898 * ones will be converted to BS3TEXT16 based addresses prior to calling.
3899 */
3900typedef struct BS3TESTMODEENTRY
3901{
3902 /** The sub-test name to be passed to Bs3TestSub if not NULL. */
3903 const char * BS3_FAR pszSubTest;
3904
3905 PFNBS3TESTDOMODE pfnDoRM;
3906
3907 PFNBS3TESTDOMODE pfnDoPE16;
3908 PFNBS3TESTDOMODE pfnDoPE16_32;
3909 PFNBS3TESTDOMODE pfnDoPE16_V86;
3910 PFNBS3TESTDOMODE pfnDoPE32;
3911 PFNBS3TESTDOMODE pfnDoPE32_16;
3912 PFNBS3TESTDOMODE pfnDoPEV86;
3913
3914 PFNBS3TESTDOMODE pfnDoPP16;
3915 PFNBS3TESTDOMODE pfnDoPP16_32;
3916 PFNBS3TESTDOMODE pfnDoPP16_V86;
3917 PFNBS3TESTDOMODE pfnDoPP32;
3918 PFNBS3TESTDOMODE pfnDoPP32_16;
3919 PFNBS3TESTDOMODE pfnDoPPV86;
3920
3921 PFNBS3TESTDOMODE pfnDoPAE16;
3922 PFNBS3TESTDOMODE pfnDoPAE16_32;
3923 PFNBS3TESTDOMODE pfnDoPAE16_V86;
3924 PFNBS3TESTDOMODE pfnDoPAE32;
3925 PFNBS3TESTDOMODE pfnDoPAE32_16;
3926 PFNBS3TESTDOMODE pfnDoPAEV86;
3927
3928 PFNBS3TESTDOMODE pfnDoLM16;
3929 PFNBS3TESTDOMODE pfnDoLM32;
3930 PFNBS3TESTDOMODE pfnDoLM64;
3931
3932} BS3TESTMODEENTRY;
3933/** Pointer to a mode sub-test entry. */
3934typedef BS3TESTMODEENTRY const *PCBS3TESTMODEENTRY;
3935
3936/** @def BS3TESTMODEENTRY_CMN
3937 * Produces a BS3TESTMODEENTRY initializer for common (c16,c32,c64) test
3938 * functions. */
3939#define BS3TESTMODEENTRY_CMN(a_szTest, a_BaseNm) \
3940 { /*pszSubTest =*/ a_szTest, \
3941 /*RM*/ RT_CONCAT(a_BaseNm, _c16), \
3942 /*PE16*/ RT_CONCAT(a_BaseNm, _c16), \
3943 /*PE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3944 /*PE16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3945 /*PE32*/ RT_CONCAT(a_BaseNm, _c32), \
3946 /*PE32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3947 /*PEV86*/ RT_CONCAT(a_BaseNm, _c16), \
3948 /*PP16*/ RT_CONCAT(a_BaseNm, _c16), \
3949 /*PP16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3950 /*PP16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3951 /*PP32*/ RT_CONCAT(a_BaseNm, _c32), \
3952 /*PP32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3953 /*PPV86*/ RT_CONCAT(a_BaseNm, _c16), \
3954 /*PAE16*/ RT_CONCAT(a_BaseNm, _c16), \
3955 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3956 /*PAE16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3957 /*PAE32*/ RT_CONCAT(a_BaseNm, _c32), \
3958 /*PAE32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3959 /*PAEV86*/ RT_CONCAT(a_BaseNm, _c16), \
3960 /*LM16*/ RT_CONCAT(a_BaseNm, _c16), \
3961 /*LM32*/ RT_CONCAT(a_BaseNm, _c32), \
3962 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
3963 }
3964
3965/** @def BS3TESTMODE_PROTOTYPES_CMN
3966 * A set of standard protypes to go with #BS3TESTMODEENTRY_CMN. */
3967#define BS3TESTMODE_PROTOTYPES_CMN(a_BaseNm) \
3968 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c16); \
3969 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c32); \
3970 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
3971
3972/** @def BS3TESTMODEENTRY_CMN_64
3973 * Produces a BS3TESTMODEENTRY initializer for common 64-bit test functions. */
3974#define BS3TESTMODEENTRY_CMN_64(a_szTest, a_BaseNm) \
3975 { /*pszSubTest =*/ a_szTest, \
3976 /*RM*/ NULL, \
3977 /*PE16*/ NULL, \
3978 /*PE16_32*/ NULL, \
3979 /*PE16_V86*/ NULL, \
3980 /*PE32*/ NULL, \
3981 /*PE32_16*/ NULL, \
3982 /*PEV86*/ NULL, \
3983 /*PP16*/ NULL, \
3984 /*PP16_32*/ NULL, \
3985 /*PP16_V86*/ NULL, \
3986 /*PP32*/ NULL, \
3987 /*PP32_16*/ NULL, \
3988 /*PPV86*/ NULL, \
3989 /*PAE16*/ NULL, \
3990 /*PAE16_32*/ NULL, \
3991 /*PAE16_V86*/ NULL, \
3992 /*PAE32*/ NULL, \
3993 /*PAE32_16*/ NULL, \
3994 /*PAEV86*/ NULL, \
3995 /*LM16*/ NULL, \
3996 /*LM32*/ NULL, \
3997 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
3998 }
3999
4000/** @def BS3TESTMODE_PROTOTYPES_CMN
4001 * Standard protype to go with #BS3TESTMODEENTRY_CMN_64. */
4002#define BS3TESTMODE_PROTOTYPES_CMN_64(a_BaseNm) \
4003 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
4004
4005/** @def BS3TESTMODEENTRY_MODE
4006 * Produces a BS3TESTMODEENTRY initializer for a full set of mode test
4007 * functions. */
4008#define BS3TESTMODEENTRY_MODE(a_szTest, a_BaseNm) \
4009 { /*pszSubTest =*/ a_szTest, \
4010 /*RM*/ RT_CONCAT(a_BaseNm, _rm), \
4011 /*PE16*/ RT_CONCAT(a_BaseNm, _pe16), \
4012 /*PE16_32*/ RT_CONCAT(a_BaseNm, _pe16_32), \
4013 /*PE16_V86*/ RT_CONCAT(a_BaseNm, _pe16_v86), \
4014 /*PE32*/ RT_CONCAT(a_BaseNm, _pe32), \
4015 /*PE32_16*/ RT_CONCAT(a_BaseNm, _pe32_16), \
4016 /*PEV86*/ RT_CONCAT(a_BaseNm, _pev86), \
4017 /*PP16*/ RT_CONCAT(a_BaseNm, _pp16), \
4018 /*PP16_32*/ RT_CONCAT(a_BaseNm, _pp16_32), \
4019 /*PP16_V86*/ RT_CONCAT(a_BaseNm, _pp16_v86), \
4020 /*PP32*/ RT_CONCAT(a_BaseNm, _pp32), \
4021 /*PP32_16*/ RT_CONCAT(a_BaseNm, _pp32_16), \
4022 /*PPV86*/ RT_CONCAT(a_BaseNm, _ppv86), \
4023 /*PAE16*/ RT_CONCAT(a_BaseNm, _pae16), \
4024 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _pae16_32), \
4025 /*PAE16_V86*/ RT_CONCAT(a_BaseNm, _pae16_v86), \
4026 /*PAE32*/ RT_CONCAT(a_BaseNm, _pae32), \
4027 /*PAE32_16*/ RT_CONCAT(a_BaseNm, _pae32_16), \
4028 /*PAEV86*/ RT_CONCAT(a_BaseNm, _paev86), \
4029 /*LM16*/ RT_CONCAT(a_BaseNm, _lm16), \
4030 /*LM32*/ RT_CONCAT(a_BaseNm, _lm32), \
4031 /*LM64*/ RT_CONCAT(a_BaseNm, _lm64), \
4032 }
4033
4034/** @def BS3TESTMODE_PROTOTYPES_MODE
4035 * A set of standard protypes to go with #BS3TESTMODEENTRY_MODE. */
4036#define BS3TESTMODE_PROTOTYPES_MODE(a_BaseNm) \
4037 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _rm); \
4038 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16); \
4039 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_32); \
4040 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_v86); \
4041 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32); \
4042 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32_16); \
4043 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pev86); \
4044 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16); \
4045 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_32); \
4046 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_v86); \
4047 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32); \
4048 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32_16); \
4049 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _ppv86); \
4050 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16); \
4051 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_32); \
4052 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_v86); \
4053 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32); \
4054 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32_16); \
4055 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _paev86); \
4056 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm16); \
4057 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm32); \
4058 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm64)
4059
4060
4061/**
4062 * Mode sub-test entry, max bit-count driven
4063 *
4064 * This is an alternative to BS3TESTMODEENTRY where a few workers (test drivers)
4065 * does all the work, using faster 32-bit and 64-bit code where possible. This
4066 * avoids executing workers in V8086 mode. It allows for modifying and checking
4067 * 64-bit register content when testing LM16 and LM32.
4068 *
4069 * The 16-bit workers are only used for real mode and 16-bit protected mode.
4070 * So, the 16-bit version of the code template can be stripped of anything
4071 * related to paging and/or v8086, saving code space.
4072 */
4073typedef struct BS3TESTMODEBYMAXENTRY
4074{
4075 /** The sub-test name to be passed to Bs3TestSub if not NULL. */
4076 const char * BS3_FAR pszSubTest;
4077
4078 PFNBS3TESTDOMODE pfnDoRM;
4079 PFNBS3TESTDOMODE pfnDoPE16;
4080 PFNBS3TESTDOMODE pfnDoPE16_32;
4081 PFNBS3TESTDOMODE pfnDoPE32;
4082 PFNBS3TESTDOMODE pfnDoPP16_32;
4083 PFNBS3TESTDOMODE pfnDoPP32;
4084 PFNBS3TESTDOMODE pfnDoPAE16_32;
4085 PFNBS3TESTDOMODE pfnDoPAE32;
4086 PFNBS3TESTDOMODE pfnDoLM64;
4087
4088 bool fDoRM : 1;
4089
4090 bool fDoPE16 : 1;
4091 bool fDoPE16_32 : 1;
4092 bool fDoPE16_V86 : 1;
4093 bool fDoPE32 : 1;
4094 bool fDoPE32_16 : 1;
4095 bool fDoPEV86 : 1;
4096
4097 bool fDoPP16 : 1;
4098 bool fDoPP16_32 : 1;
4099 bool fDoPP16_V86 : 1;
4100 bool fDoPP32 : 1;
4101 bool fDoPP32_16 : 1;
4102 bool fDoPPV86 : 1;
4103
4104 bool fDoPAE16 : 1;
4105 bool fDoPAE16_32 : 1;
4106 bool fDoPAE16_V86 : 1;
4107 bool fDoPAE32 : 1;
4108 bool fDoPAE32_16 : 1;
4109 bool fDoPAEV86 : 1;
4110
4111 bool fDoLM16 : 1;
4112 bool fDoLM32 : 1;
4113 bool fDoLM64 : 1;
4114
4115} BS3TESTMODEBYMAXENTRY;
4116/** Pointer to a mode-by-max sub-test entry. */
4117typedef BS3TESTMODEBYMAXENTRY const *PCBS3TESTMODEBYMAXENTRY;
4118
4119/** @def BS3TESTMODEBYMAXENTRY_CMN
4120 * Produces a BS3TESTMODEBYMAXENTRY initializer for common (c16,c32,c64) test
4121 * functions. */
4122#define BS3TESTMODEBYMAXENTRY_CMN(a_szTest, a_BaseNm) \
4123 { /*pszSubTest =*/ a_szTest, \
4124 /*RM*/ RT_CONCAT(a_BaseNm, _c16), \
4125 /*PE16*/ RT_CONCAT(a_BaseNm, _c16), \
4126 /*PE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4127 /*PE32*/ RT_CONCAT(a_BaseNm, _c32), \
4128 /*PP16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4129 /*PP32*/ RT_CONCAT(a_BaseNm, _c32), \
4130 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4131 /*PAE32*/ RT_CONCAT(a_BaseNm, _c32), \
4132 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
4133 /*fDoRM*/ true, \
4134 /*fDoPE16*/ true, \
4135 /*fDoPE16_32*/ true, \
4136 /*fDoPE16_V86*/ true, \
4137 /*fDoPE32*/ true, \
4138 /*fDoPE32_16*/ true, \
4139 /*fDoPEV86*/ true, \
4140 /*fDoPP16*/ true, \
4141 /*fDoPP16_32*/ true, \
4142 /*fDoPP16_V86*/ true, \
4143 /*fDoPP32*/ true, \
4144 /*fDoPP32_16*/ true, \
4145 /*fDoPPV86*/ true, \
4146 /*fDoPAE16*/ true, \
4147 /*fDoPAE16_32*/ true, \
4148 /*fDoPAE16_V86*/ true, \
4149 /*fDoPAE32*/ true, \
4150 /*fDoPAE32_16*/ true, \
4151 /*fDoPAEV86*/ true, \
4152 /*fDoLM16*/ true, \
4153 /*fDoLM32*/ true, \
4154 /*fDoLM64*/ true, \
4155 }
4156
4157/** @def BS3TESTMODEBYMAX_PROTOTYPES_CMN
4158 * A set of standard protypes to go with #BS3TESTMODEBYMAXENTRY_CMN. */
4159#define BS3TESTMODEBYMAX_PROTOTYPES_CMN(a_BaseNm) \
4160 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c16); \
4161 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c32); \
4162 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
4163
4164
4165/** @def BS3TESTMODEBYMAXENTRY_MODE
4166 * Produces a BS3TESTMODEBYMAXENTRY initializer for a full set of mode test
4167 * functions. */
4168#define BS3TESTMODEBYMAXENTRY_MODE(a_szTest, a_BaseNm) \
4169 { /*pszSubTest =*/ a_szTest, \
4170 /*RM*/ RT_CONCAT(a_BaseNm, _rm), \
4171 /*PE16*/ RT_CONCAT(a_BaseNm, _pe16), \
4172 /*PE16_32*/ RT_CONCAT(a_BaseNm, _pe16_32), \
4173 /*PE32*/ RT_CONCAT(a_BaseNm, _pe32), \
4174 /*PP16_32*/ RT_CONCAT(a_BaseNm, _pp16_32), \
4175 /*PP32*/ RT_CONCAT(a_BaseNm, _pp32), \
4176 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _pae16_32), \
4177 /*PAE32*/ RT_CONCAT(a_BaseNm, _pae32), \
4178 /*LM64*/ RT_CONCAT(a_BaseNm, _lm64), \
4179 /*fDoRM*/ true, \
4180 /*fDoPE16*/ true, \
4181 /*fDoPE16_32*/ true, \
4182 /*fDoPE16_V86*/ true, \
4183 /*fDoPE32*/ true, \
4184 /*fDoPE32_16*/ true, \
4185 /*fDoPEV86*/ true, \
4186 /*fDoPP16*/ true, \
4187 /*fDoPP16_32*/ true, \
4188 /*fDoPP16_V86*/ true, \
4189 /*fDoPP32*/ true, \
4190 /*fDoPP32_16*/ true, \
4191 /*fDoPPV86*/ true, \
4192 /*fDoPAE16*/ true, \
4193 /*fDoPAE16_32*/ true, \
4194 /*fDoPAE16_V86*/ true, \
4195 /*fDoPAE32*/ true, \
4196 /*fDoPAE32_16*/ true, \
4197 /*fDoPAEV86*/ true, \
4198 /*fDoLM16*/ true, \
4199 /*fDoLM32*/ true, \
4200 /*fDoLM64*/ true, \
4201 }
4202
4203/** @def BS3TESTMODEBYMAX_PROTOTYPES_MODE
4204 * A set of standard protypes to go with #BS3TESTMODEBYMAXENTRY_MODE. */
4205#define BS3TESTMODEBYMAX_PROTOTYPES_MODE(a_BaseNm) \
4206 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _rm); \
4207 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16); \
4208 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_32); \
4209 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32); \
4210 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_32); \
4211 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32); \
4212 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_32); \
4213 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32); \
4214 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm64)
4215
4216
4217/**
4218 * One worker drives all modes.
4219 *
4220 * This is an alternative to BS3TESTMODEENTRY where one worker, typically
4221 * 16-bit, does all the test driver work. It's called repeatedly from all
4222 * the modes being tested.
4223 */
4224typedef struct BS3TESTMODEBYONEENTRY
4225{
4226 const char * BS3_FAR pszSubTest;
4227 PFNBS3TESTDOMODE pfnWorker;
4228 /** BS3TESTMODEBYONEENTRY_F_XXX. */
4229 uint32_t fFlags;
4230} BS3TESTMODEBYONEENTRY;
4231/** Pointer to a mode-by-one sub-test entry. */
4232typedef BS3TESTMODEBYONEENTRY const *PCBS3TESTMODEBYONEENTRY;
4233
4234/** @name BS3TESTMODEBYONEENTRY_F_XXX - flags.
4235 * @{ */
4236/** Only test modes that has paging enabled. */
4237#define BS3TESTMODEBYONEENTRY_F_ONLY_PAGING RT_BIT_32(0)
4238/** Minimal mode selection. */
4239#define BS3TESTMODEBYONEENTRY_F_MINIMAL RT_BIT_32(1)
4240/** The 32-bit worker is ready to handle real-mode by mode switching. */
4241#define BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY RT_BIT_32(2)
4242/** Skip all v8086 mode tests. */
4243#define BS3TESTMODEBYONEENTRY_F_SKIP_V8086 RT_BIT_32(3)
4244/** @} */
4245
4246
4247/**
4248 * Sets the full GDTR register.
4249 *
4250 * @param cbLimit The limit.
4251 * @param uBase The base address - 24, 32 or 64 bit depending on the
4252 * CPU mode.
4253 */
4254BS3_CMN_PROTO_NOSB(void, Bs3UtilSetFullGdtr,(uint16_t cbLimit, uint64_t uBase));
4255
4256/**
4257 * Sets the full IDTR register.
4258 *
4259 * @param cbLimit The limit.
4260 * @param uBase The base address - 24, 32 or 64 bit depending on the
4261 * CPU mode.
4262 */
4263BS3_CMN_PROTO_NOSB(void, Bs3UtilSetFullIdtr,(uint16_t cbLimit, uint64_t uBase));
4264
4265
4266/** @} */
4267
4268
4269/**
4270 * Initializes all of boot sector kit \#3.
4271 */
4272BS3_DECL(void) Bs3InitAll_rm(void);
4273
4274/**
4275 * Initializes all of boot sector kit \#3 as well as the high DLLs.
4276 */
4277BS3_DECL(void) Bs3InitAllWithHighDlls_rm(void);
4278
4279/**
4280 * Initializes the REAL and TILED memory pools.
4281 *
4282 * For proper operation on OLDer CPUs, call #Bs3CpuDetect_mmm first.
4283 */
4284BS3_DECL_FAR(void) Bs3InitMemory_rm_far(void);
4285
4286/**
4287 * Initializes the X0TEXT16 and X1TEXT16 GDT entries.
4288 */
4289BS3_DECL_FAR(void) Bs3InitGdt_rm_far(void);
4290
4291/**
4292 * Initializes (loads) any high DLLs.
4293 *
4294 * @note This cannot be called after the PIC or traps have been initialized!
4295 */
4296BS3_DECL_FAR(void) Bs3InitHighDlls_rm_far(void);
4297
4298
4299
4300/** @defgroup grp_bs3kit_mode Mode Specific Functions and Data
4301 *
4302 * The mode specific functions come in bit count variations and CPU mode
4303 * variations. The bs3kit-template-header.h/mac defines the BS3_NM macro to
4304 * mangle a function or variable name according to the target CPU mode. In
4305 * non-templated code, it's common to spell the name out in full.
4306 *
4307 * @{
4308 */
4309
4310
4311/** @def BS3_MODE_PROTO_INT
4312 * Internal macro for emitting prototypes for mode functions.
4313 *
4314 * @param a_RetType The return type.
4315 * @param a_Name The function basename.
4316 * @param a_Params The parameter list (in parentheses).
4317 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4318 */
4319#define BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params) \
4320 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_rm) a_Params; \
4321 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16) a_Params; \
4322 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16_32) a_Params; \
4323 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16_v86) a_Params; \
4324 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe32) a_Params; \
4325 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe32_16) a_Params; \
4326 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pev86) a_Params; \
4327 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16) a_Params; \
4328 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16_32) a_Params; \
4329 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16_v86) a_Params; \
4330 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp32) a_Params; \
4331 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp32_16) a_Params; \
4332 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_ppv86) a_Params; \
4333 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16) a_Params; \
4334 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16_32) a_Params; \
4335 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16_v86) a_Params; \
4336 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae32) a_Params; \
4337 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae32_16) a_Params; \
4338 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_paev86) a_Params; \
4339 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm16) a_Params; \
4340 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm32) a_Params; \
4341 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm64) a_Params; \
4342 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_rm_far) a_Params; \
4343 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe16_far) a_Params; \
4344 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe16_v86_far) a_Params; \
4345 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe32_16_far) a_Params; \
4346 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pev86_far) a_Params; \
4347 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp16_far) a_Params; \
4348 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp16_v86_far) a_Params; \
4349 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp32_16_far) a_Params; \
4350 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_ppv86_far) a_Params; \
4351 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae16_far) a_Params; \
4352 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae16_v86_far)a_Params; \
4353 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae32_16_far) a_Params; \
4354 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_paev86_far) a_Params; \
4355 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_lm16_far) a_Params
4356
4357/** @def BS3_MODE_PROTO_STUB
4358 * Macro for prototyping all the variations of a mod function with automatic
4359 * near -> far stub.
4360 *
4361 * @param a_RetType The return type.
4362 * @param a_Name The function basename.
4363 * @param a_Params The parameter list (in parentheses).
4364 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4365 */
4366#define BS3_MODE_PROTO_STUB(a_RetType, a_Name, a_Params) BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params)
4367
4368/** @def BS3_MODE_PROTO_STUB
4369 * Macro for prototyping all the variations of a mod function without any
4370 * near -> far stub.
4371 *
4372 * @param a_RetType The return type.
4373 * @param a_Name The function basename.
4374 * @param a_Params The parameter list (in parentheses).
4375 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4376 */
4377#define BS3_MODE_PROTO_NOSB(a_RetType, a_Name, a_Params) BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params)
4378
4379
4380/**
4381 * Macro for reducing typing.
4382 *
4383 * Doxygen knows how to expand this, well, kind of.
4384 *
4385 * @remarks Variables instantiated in assembly code should define two labels,
4386 * with and without leading underscore. Variables instantiated from
4387 * C/C++ code doesn't need to as the object file convert does this for
4388 * 64-bit object files.
4389 */
4390#define BS3_MODE_EXPAND_EXTERN_DATA16(a_VarType, a_VarName, a_Suffix) \
4391 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_rm) a_Suffix; \
4392 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16) a_Suffix; \
4393 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16_32) a_Suffix; \
4394 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16_v86) a_Suffix; \
4395 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe32) a_Suffix; \
4396 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe32_16) a_Suffix; \
4397 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pev86) a_Suffix; \
4398 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16) a_Suffix; \
4399 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16_32) a_Suffix; \
4400 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16_v86) a_Suffix; \
4401 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp32) a_Suffix; \
4402 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp32_16) a_Suffix; \
4403 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_ppv86) a_Suffix; \
4404 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16) a_Suffix; \
4405 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16_32) a_Suffix; \
4406 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16_v86)a_Suffix; \
4407 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae32) a_Suffix; \
4408 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae32_16) a_Suffix; \
4409 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_paev86) a_Suffix; \
4410 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm16) a_Suffix; \
4411 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm32) a_Suffix; \
4412 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm64) a_Suffix
4413
4414
4415/** The TMPL_MODE_STR value for each mode.
4416 * These are all in DATA16 so they can be accessed from any code. */
4417BS3_MODE_EXPAND_EXTERN_DATA16(const char, g_szBs3ModeName, []);
4418/** The TMPL_MODE_LNAME value for each mode.
4419 * These are all in DATA16 so they can be accessed from any code. */
4420BS3_MODE_EXPAND_EXTERN_DATA16(const char, g_szBs3ModeNameShortLower, []);
4421
4422
4423/**
4424 * Basic CPU detection.
4425 *
4426 * This sets the #g_uBs3CpuDetected global variable to the return value.
4427 *
4428 * @returns BS3CPU_XXX value with the BS3CPU_F_CPUID flag set depending on
4429 * capabilities.
4430 */
4431BS3_MODE_PROTO_NOSB(uint8_t, Bs3CpuDetect,(void));
4432
4433/** @name BS3CPU_XXX - CPU detected by BS3CpuDetect_c16() and friends.
4434 * @{ */
4435#define BS3CPU_8086 UINT16_C(0x0001) /**< Both 8086 and 8088. */
4436#define BS3CPU_V20 UINT16_C(0x0002) /**< Both NEC V20, V30 and relatives. */
4437#define BS3CPU_80186 UINT16_C(0x0003) /**< Both 80186 and 80188. */
4438#define BS3CPU_80286 UINT16_C(0x0004)
4439#define BS3CPU_80386 UINT16_C(0x0005)
4440#define BS3CPU_80486 UINT16_C(0x0006)
4441#define BS3CPU_Pentium UINT16_C(0x0007)
4442#define BS3CPU_PPro UINT16_C(0x0008)
4443#define BS3CPU_PProOrNewer UINT16_C(0x0009)
4444/** CPU type mask. This is a full byte so it's possible to use byte access
4445 * without and AND'ing to get the type value. */
4446#define BS3CPU_TYPE_MASK UINT16_C(0x00ff)
4447/** Flag indicating that the CPUID instruction is supported by the CPU. */
4448#define BS3CPU_F_CPUID UINT16_C(0x0100)
4449/** Flag indicating that extend CPUID leaves are available (at least two). */
4450#define BS3CPU_F_CPUID_EXT_LEAVES UINT16_C(0x0200)
4451/** Flag indicating that the CPU supports PAE. */
4452#define BS3CPU_F_PAE UINT16_C(0x0400)
4453/** Flag indicating that the CPU supports the page size extension (4MB pages). */
4454#define BS3CPU_F_PSE UINT16_C(0x0800)
4455/** Flag indicating that the CPU supports long mode. */
4456#define BS3CPU_F_LONG_MODE UINT16_C(0x1000)
4457/** Flag indicating that the CPU supports NX. */
4458#define BS3CPU_F_NX UINT16_C(0x2000)
4459/** @} */
4460
4461/** The return value of #Bs3CpuDetect_mmm. (Initial value is BS3CPU_TYPE_MASK.) */
4462extern uint16_t g_uBs3CpuDetected;
4463
4464/**
4465 * Call 32-bit prot mode C function.
4466 *
4467 * This switches to 32-bit mode and calls the 32-bit @a fpfnCall C code with @a
4468 * cbParams on the stack, then returns in the original mode. When called in
4469 * real mode, this will switch to PE32.
4470 *
4471 * @returns 32-bit status code if the function returned anything.
4472 * @param fpfnCall Address of the 32-bit C function to call. When
4473 * called from 16-bit code, this is a far real mode
4474 * function pointer, i.e. as fixed up by the linker.
4475 * In 32-bit and 64-bit code, this is a flat address.
4476 * @param cbParams The size of the parameter list, in bytes.
4477 * @param ... The parameters.
4478 * @sa Bs3SwitchFromV86To16BitAndCallC
4479 *
4480 * @remarks WARNING! This probably doesn't work in 64-bit mode yet.
4481 * Only tested for 16-bit real mode.
4482 */
4483BS3_MODE_PROTO_STUB(int32_t, Bs3SwitchTo32BitAndCallC,(FPFNBS3FAR fpfnCall, unsigned cbParams, ...));
4484
4485/**
4486 * Initializes trap handling for the current system.
4487 *
4488 * Calls the appropriate Bs3Trap16Init, Bs3Trap32Init or Bs3Trap64Init function.
4489 */
4490BS3_MODE_PROTO_STUB(void, Bs3TrapInit,(void));
4491
4492/**
4493 * Special version of memcpy for copying from/to real mode.
4494 *
4495 * @returns pvDst
4496 * @param uFlatDst The flat address of the destination buffer.
4497 * @param uFlatSrc The flat address of the source buffer.
4498 * @param cbToCopy The number of bytes to copy. Max 64KB.
4499 *
4500 * @todo Only work on 386+ at present. Could be made to work for 286 and
4501 * addresses < 16MB if we care...
4502 */
4503BS3_MODE_PROTO_STUB(void BS3_FAR *, Bs3MemCopyFlat,(uint32_t uFlatDst, uint32_t uFlatSrc, uint32_t cbToCopy));
4504
4505/**
4506 * Executes the array of tests in every possibly mode.
4507 *
4508 * @param paEntries The mode sub-test entries.
4509 * @param cEntries The number of sub-test entries.
4510 */
4511BS3_MODE_PROTO_NOSB(void, Bs3TestDoModes,(PCBS3TESTMODEENTRY paEntries, size_t cEntries));
4512
4513/**
4514 * Executes the array of tests in every possibly mode, unified driver.
4515 *
4516 * This requires much less code space than Bs3TestDoModes as there is only one
4517 * instace of each sub-test driver code, instead of 3 (cmn) or 22 (per-mode)
4518 * copies.
4519 *
4520 * @param paEntries The mode sub-test-by-one entries.
4521 * @param cEntries The number of sub-test-by-one entries.
4522 * @param fFlags BS3TESTMODEBYONEENTRY_F_XXX.
4523 */
4524BS3_MODE_PROTO_NOSB(void, Bs3TestDoModesByOne,(PCBS3TESTMODEBYONEENTRY paEntries, size_t cEntries, uint32_t fFlags));
4525
4526/**
4527 * Executes the array of tests in every possibly mode, using the max bit-count
4528 * worker for each.
4529 *
4530 * @param paEntries The mode sub-test entries.
4531 * @param cEntries The number of sub-test entries.
4532 */
4533BS3_MODE_PROTO_NOSB(void, Bs3TestDoModesByMax,(PCBS3TESTMODEBYMAXENTRY paEntries, size_t cEntries));
4534
4535/** @} */
4536
4537
4538/** @defgroup grp_bs3kit_bios_int15 BIOS - int 15h
4539 * @{ */
4540
4541/** An INT15E820 data entry. */
4542typedef struct INT15E820ENTRY
4543{
4544 uint64_t uBaseAddr;
4545 uint64_t cbRange;
4546 /** Memory type this entry describes, see INT15E820_TYPE_XXX. */
4547 uint32_t uType;
4548 /** Optional. */
4549 uint32_t fAcpi3;
4550} INT15E820ENTRY;
4551AssertCompileSize(INT15E820ENTRY,24);
4552
4553
4554/** @name INT15E820_TYPE_XXX - Memory types returned by int 15h function 0xe820.
4555 * @{ */
4556#define INT15E820_TYPE_USABLE 1 /**< Usable RAM. */
4557#define INT15E820_TYPE_RESERVED 2 /**< Reserved by the system, unusable. */
4558#define INT15E820_TYPE_ACPI_RECLAIMABLE 3 /**< ACPI reclaimable memory, whatever that means. */
4559#define INT15E820_TYPE_ACPI_NVS 4 /**< ACPI non-volatile storage? */
4560#define INT15E820_TYPE_BAD 5 /**< Bad memory, unusable. */
4561/** @} */
4562
4563
4564/**
4565 * Performs an int 15h function 0xe820 call.
4566 *
4567 * @returns Success indicator.
4568 * @param pEntry The return buffer.
4569 * @param pcbEntry Input: The size of the buffer (min 20 bytes);
4570 * Output: The size of the returned data.
4571 * @param puContinuationValue Where to get and return the continuation value (EBX)
4572 * Set to zero the for the first call. Returned as zero
4573 * after the last entry.
4574 */
4575BS3_MODE_PROTO_STUB(bool, Bs3BiosInt15hE820,(INT15E820ENTRY BS3_FAR *pEntry, uint32_t BS3_FAR *pcbEntry,
4576 uint32_t BS3_FAR *puContinuationValue));
4577
4578/**
4579 * Performs an int 15h function 0x88 call.
4580 *
4581 * @returns UINT32_MAX on failure, number of KBs above 1MB otherwise.
4582 */
4583#if ARCH_BITS != 16 || !defined(BS3_BIOS_INLINE_RM)
4584BS3_MODE_PROTO_STUB(uint32_t, Bs3BiosInt15h88,(void));
4585#else
4586BS3_DECL(uint32_t) Bs3BiosInt15h88(void);
4587# pragma aux Bs3BiosInt15h88 = \
4588 ".286" \
4589 "clc" \
4590 "mov ax, 08800h" \
4591 "int 15h" \
4592 "jc failed" \
4593 "xor dx, dx" \
4594 "jmp done" \
4595 "failed:" \
4596 "xor ax, ax" \
4597 "dec ax" \
4598 "mov dx, ax" \
4599 "done:" \
4600 value [ax dx] \
4601 modify exact [ax bx cx dx es];
4602#endif
4603
4604/** @} */
4605
4606
4607/** @defgroup grp_bs3kit_kbd Keyboard
4608 * @{
4609 */
4610
4611/**
4612 * Waits for the keyboard controller to become ready.
4613 */
4614BS3_CMN_PROTO_NOSB(void, Bs3KbdWait,(void));
4615
4616/**
4617 * Sends a read command to the keyboard controller and gets the result.
4618 *
4619 * The caller is responsible for making sure the keyboard controller is ready
4620 * for a command (call #Bs3KbdWait if unsure).
4621 *
4622 * @returns The value read is returned (in al).
4623 * @param bCmd The read command.
4624 */
4625BS3_CMN_PROTO_NOSB(uint8_t, Bs3KbdRead,(uint8_t bCmd));
4626
4627/**
4628 * Sends a write command to the keyboard controller and then sends the data.
4629 *
4630 * The caller is responsible for making sure the keyboard controller is ready
4631 * for a command (call #Bs3KbdWait if unsure).
4632 *
4633 * @param bCmd The write command.
4634 * @param bData The data to write.
4635 */
4636BS3_CMN_PROTO_NOSB(void, Bs3KbdWrite,(uint8_t bCmd, uint8_t bData));
4637
4638/** @} */
4639
4640
4641/** @defgroup grp_bs3kit_pic PIC
4642 * @{
4643 */
4644
4645/**
4646 * Configures the PIC, once only.
4647 *
4648 * Subsequent calls to this function will not do anything.
4649 *
4650 * The PIC will be programmed to use IDT/IVT vectors 0x70 thru 0x7f, auto
4651 * end-of-interrupt, and all IRQs masked. The individual PIC users will have to
4652 * use #Bs3PicUpdateMask unmask their IRQ once they've got all the handlers
4653 * installed.
4654 *
4655 * @param fForcedReInit Force a reinitialization.
4656 */
4657BS3_CMN_PROTO_STUB(void, Bs3PicSetup,(bool fForcedReInit));
4658
4659/**
4660 * Updates the PIC masks.
4661 *
4662 * @returns The new mask - master in low, slave in high byte.
4663 * @param fAndMask Things to keep as-is. Master in low, slave in high byte.
4664 * @param fOrMask Things to start masking. Ditto wrt bytes.
4665 */
4666BS3_CMN_PROTO_STUB(uint16_t, Bs3PicUpdateMask,(uint16_t fAndMask, uint16_t fOrMask));
4667
4668/**
4669 * Disables all IRQs on the PIC.
4670 */
4671BS3_CMN_PROTO_STUB(void, Bs3PicMaskAll,(void));
4672
4673/** @} */
4674
4675
4676/** @defgroup grp_bs3kit_pit PIT
4677 * @{
4678 */
4679
4680/**
4681 * Sets up the PIT for periodic callback.
4682 *
4683 * @param cHzDesired The desired Hz. Zero means max interval length
4684 * (18.2Hz). Plase check the various PIT globals for
4685 * the actual interval length.
4686 */
4687BS3_CMN_PROTO_STUB(void, Bs3PitSetupAndEnablePeriodTimer,(uint16_t cHzDesired));
4688
4689/**
4690 * Disables the PIT if active.
4691 */
4692BS3_CMN_PROTO_STUB(void, Bs3PitDisable,(void));
4693
4694/** The RIP/EIP value of where the PIT IRQ handle will return to. */
4695extern BS3REG volatile g_Bs3PitIrqRip;
4696/** Nanoseconds (approx) since last the PIT timer was started. */
4697extern uint64_t volatile g_cBs3PitNs;
4698/** Milliseconds seconds (very approx) since last the PIT timer was started. */
4699extern uint64_t volatile g_cBs3PitMs;
4700/** Number of ticks since last the PIT timer was started. */
4701extern uint32_t volatile g_cBs3PitTicks;
4702/** The current interval in nanoseconds.
4703 * This is 0 if not yet started (cleared by Bs3PitDisable). */
4704extern uint32_t g_cBs3PitIntervalNs;
4705/** The current interval in milliseconds (approximately).
4706 * This is 0 if not yet started (cleared by Bs3PitDisable). */
4707extern uint16_t g_cBs3PitIntervalMs;
4708/** The current PIT frequency (approximately).
4709 * 0 if not yet started (cleared by Bs3PitDisable; used for checking the
4710 * state internally). */
4711extern uint16_t volatile g_cBs3PitIntervalHz;
4712
4713/** @} */
4714
4715/** @defgroup grp_bs3kit_disk Disk via INT 13h
4716 * @{
4717 */
4718
4719/**
4720 * Performs a int 13h function AL=08h call to get the driver parameters.
4721 *
4722 * @returns 0 on success, non-zero error BIOS code on failure.
4723 * @param bDrive The drive to get parameters for.
4724 * @param puMaxCylinder Where to store the max cylinder value.
4725 * Range: 0 thru *pcMaxCylinder.
4726 * @param puMaxHead Where to store the max head value.
4727 * Range: 0 thru *pcMaxHead.
4728 * @param puMaxSector Where to store the max sector value.
4729 * Range: 1 thru *pcMaxSector.
4730 */
4731BS3_MODE_PROTO_STUB(uint8_t, Bs3DiskQueryGeometry,(uint8_t bDrive, uint16_t *puMaxCylinder,
4732 uint8_t *puMaxHead, uint8_t *puMaxSector));
4733
4734/**
4735 * Performs a int 13h function AL=08h call to get the driver parameters.
4736 *
4737 * @returns 0 on success, non-zero error BIOS code on failure.
4738 * @param bDrive The drive to read from.
4739 * @param uCylinder The cylinder to start read at (0-max).
4740 * @param uHead The head to start reading at (0-max).
4741 * @param uSector The sector to start reading at (1-max).
4742 * @param cSectors The number of sectors to read (1+).
4743 * @param pvBuf The buffer to read into. This MUST be addressable
4744 * from real mode!
4745 */
4746BS3_MODE_PROTO_STUB(uint8_t, Bs3DiskRead,(uint8_t bDrive, uint16_t uCylinder, uint8_t uHead, uint8_t uSector,
4747 uint8_t cSectors, void RT_FAR *pvBuf));
4748
4749/** @} */
4750
4751
4752/** @} */
4753
4754RT_C_DECLS_END
4755
4756
4757/*
4758 * Include default function symbol mangling.
4759 */
4760#include "bs3kit-mangling-code.h"
4761
4762/*
4763 * Change 16-bit text segment if requested.
4764 */
4765#if defined(BS3_USE_ALT_16BIT_TEXT_SEG) && ARCH_BITS == 16 && !defined(BS3_DONT_CHANGE_TEXT_SEG)
4766# if (defined(BS3_USE_RM_TEXT_SEG) + defined(BS3_USE_X0_TEXT_SEG) + defined(BS3_USE_X1_TEXT_SEG)) != 1
4767# error "Cannot set more than one alternative 16-bit text segment!"
4768# elif defined(BS3_USE_RM_TEXT_SEG)
4769# pragma code_seg("BS3RMTEXT16", "BS3CLASS16RMCODE")
4770# elif defined(BS3_USE_X0_TEXT_SEG)
4771# pragma code_seg("BS3X0TEXT16", "BS3CLASS16X0CODE")
4772# elif defined(BS3_USE_X1_TEXT_SEG)
4773# pragma code_seg("BS3X1TEXT16", "BS3CLASS16X1CODE")
4774# else
4775# error "Huh? Which alternative text segment did you want again?"
4776# endif
4777#endif
4778
4779#endif /* !BS3KIT_INCLUDED_bs3kit_h */
4780
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette