VirtualBox

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

Last change on this file since 102157 was 102157, checked in by vboxsync, 13 months ago

ValKit/bs3kit,bs3-cpu-basic-3: Experimental support for loading a 2nd test image above 1MB (at 8MB by default). This should allow for larger testcases, esp. for 32-bit and 64-bit code. bugref:10371

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

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