VirtualBox

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

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

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

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

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