VirtualBox

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

Last change on this file since 103068 was 102277, checked in by vboxsync, 14 months ago

bs3kit: Speed up high DLL loading by using a larger buffer. Extended the linker to process the segment table as well and output more appropriate address symbols in the base module assembly file. TODO: missing 16-bit selector setup in the bs3kit loader code. bugref:10371

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