VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 4 days ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 192.4 KB
Line 
1/* $Id: bs3kit.h 106061 2024-09-16 14:03:52Z 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.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef BS3KIT_INCLUDED_bs3kit_h
38#define BS3KIT_INCLUDED_bs3kit_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#ifndef DOXYGEN_RUNNING
44# undef IN_RING0
45# define IN_RING0
46#endif
47
48#define RT_NO_STRICT /* Don't drag in IPRT assertion code in inline code we may use (asm.h). */
49#include <iprt/cdefs.h>
50#include <iprt/types.h>
51
52#ifndef DOXYGEN_RUNNING
53# undef IN_RING0
54#endif
55
56/*
57 * Make asm.h and friend compatible with our 64-bit assembly config (ASM_CALL64_MSC).
58 */
59#if defined(__GNUC__) && ARCH_BITS == 64
60# undef DECLASM
61# ifdef __cplusplus
62# define DECLASM(type) extern "C" type BS3_CALL
63# else
64# define DECLASM(type) type BS3_CALL
65# endif
66#endif
67
68
69/*
70 * Work around ms_abi trouble in the gcc camp (gcc bugzilla #50818).
71 * ASSUMES all va_lists are in functions with
72 */
73#if defined(__GNUC__) && ARCH_BITS == 64
74# undef va_list
75# undef va_start
76# undef va_end
77# undef va_copy
78# define va_list __builtin_ms_va_list
79# define va_start(a_Va, a_Arg) __builtin_ms_va_start(a_Va, a_Arg)
80# define va_end(a_Va) __builtin_ms_va_end(a_Va)
81# define va_copy(a_DstVa, a_SrcVa) __builtin_ms_va_copy(a_DstVa, a_SrcVa)
82#endif
83
84
85/** @def BS3_USE_ALT_16BIT_TEXT_SEG
86 * @ingroup grp_bs3kit
87 * Combines the BS3_USE_RM_TEXT_SEG, BS3_USE_X0_TEXT_SEG, and
88 * BS3_USE_X1_TEXT_SEG indicators into a single one.
89 */
90#if defined(BS3_USE_RM_TEXT_SEG) || defined(BS3_USE_X0_TEXT_SEG) || defined(BS3_USE_X1_TEXT_SEG) || defined(DOXYGEN_RUNNING)
91# define BS3_USE_ALT_16BIT_TEXT_SEG
92#else
93# undef BS3_USE_ALT_16BIT_TEXT_SEG
94#endif
95
96/** @def BS3_USE_X0_TEXT_SEG
97 * @ingroup grp_bs3kit
98 * Emit 16-bit code to the BS3X0TEXT16 segment - ignored for 32-bit and 64-bit.
99 *
100 * Calling directly into the BS3X0TEXT16 segment is only possible in real-mode
101 * and v8086 mode. In protected mode the real far pointer have to be converted
102 * to a protected mode pointer that uses BS3_SEL_X0TEXT16_CS, Bs3TestDoModes and
103 * associates does this automatically.
104 */
105#ifdef DOXYGEN_RUNNING
106# define BS3_USE_X0_TEXT_SEG
107#endif
108
109/** @def BS3_USE_X1_TEXT_SEG
110 * @ingroup grp_bs3kit
111 * Emit 16-bit code to the BS3X1TEXT16 segment - ignored for 32-bit and 64-bit.
112 *
113 * Calling directly into the BS3X1TEXT16 segment is only possible in real-mode
114 * and v8086 mode. In protected mode the real far pointer have to be converted
115 * to a protected mode pointer that uses BS3_SEL_X1TEXT16_CS, Bs3TestDoModes and
116 * associates does this automatically.
117 */
118#ifdef DOXYGEN_RUNNING
119# define BS3_USE_X1_TEXT_SEG
120#endif
121
122/** @def BS3_USE_RM_TEXT_SEG
123 * @ingroup grp_bs3kit
124 * Emit 16-bit code to the BS3RMTEXT16 segment - ignored for 32-bit and 64-bit.
125 *
126 * This segment is normally used for real-mode only code, though
127 * BS3_SEL_RMTEXT16_CS can be used to call it from protected mode. Unlike the
128 * BS3X0TEXT16 and BS3X1TEXT16 segments which are empty by default, this segment
129 * is used by common BS3Kit code.
130 */
131#ifdef DOXYGEN_RUNNING
132# define BS3_USE_X0_TEXT_SEG
133#endif
134
135/** @def BS3_MODEL_FAR_CODE
136 * @ingroup grp_bs3kit
137 * Default compiler model indicates far code.
138 */
139#ifdef DOXYGEN_RUNNING
140# define BS3_MODEL_FAR_CODE
141#elif !defined(BS3_MODEL_FAR_CODE) && (defined(__LARGE__) || defined(__MEDIUM__) || defined(__HUGE__)) && ARCH_BITS == 16
142# define BS3_MODEL_FAR_CODE
143#endif
144
145
146/*
147 * We normally don't want the noreturn / aborts attributes as they mess up stack traces.
148 *
149 * Note! pragma aux <fnname> aborts can only be used with functions
150 * implemented in C and functions that does not have parameters.
151 */
152#define BS3_KIT_WITH_NO_RETURN
153#ifndef BS3_KIT_WITH_NO_RETURN
154# undef DECL_NO_RETURN
155# define DECL_NO_RETURN(type) type
156#endif
157
158
159/*
160 * We may want to reuse some IPRT code in the common name space, so we
161 * redefine the RT_MANGLER to work like BS3_CMN_NM. (We cannot use
162 * BS3_CMN_NM yet, as we need to include IPRT headers with function
163 * declarations before we can define it. Thus the duplciate effort.)
164 */
165#if ARCH_BITS == 16
166# undef RTCALL
167# if defined(BS3_USE_ALT_16BIT_TEXT_SEG)
168# define RTCALL __cdecl __far
169# define RT_MANGLER(a_Name) RT_CONCAT(a_Name,_f16)
170# else
171# define RTCALL __cdecl __near
172# define RT_MANGLER(a_Name) RT_CONCAT(a_Name,_c16)
173# endif
174#else
175# define RT_MANGLER(a_Name) RT_CONCAT3(a_Name,_c,ARCH_BITS)
176#endif
177#include <iprt/mangling.h>
178#include <iprt/x86.h>
179#include <iprt/err.h>
180
181/*
182 * Include data symbol mangling (function mangling/mapping must be done
183 * after the protypes).
184 */
185#include "bs3kit-mangling-data.h"
186
187
188
189RT_C_DECLS_BEGIN
190
191/** @defgroup grp_bs3kit BS3Kit - Boot Sector Kit \#3
192 *
193 * The BS3Kit is a framework for bare metal floppy/usb image tests,
194 * see the @ref pg_bs3kit "doc page" for more.
195 *
196 * @{ */
197
198
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 * Calls Bs3Panic if false and it's a strict build. Does nothing in
1587 * non-strict builds. */
1588#ifdef BS3_STRICT
1589# define BS3_ASSERT(a_Expr) do { if (!!(a_Expr)) { /* likely */ } else { Bs3Panic(); } } while (0) /**< @todo later */
1590#else
1591# define BS3_ASSERT(a_Expr) do { } while (0)
1592#endif
1593
1594/**
1595 * Panic, never return.
1596 *
1597 * The current implementation will only halt the CPU.
1598 */
1599BS3_CMN_PROTO_NOSB(DECL_NO_RETURN(void), Bs3Panic,(void));
1600#if !defined(BS3_KIT_WITH_NO_RETURN) && defined(__WATCOMC__)
1601# pragma aux Bs3Panic_c16 __aborts
1602# pragma aux Bs3Panic_f16 __aborts
1603# pragma aux Bs3Panic_c32 __aborts
1604#endif
1605
1606
1607/**
1608 * Translate a mode into a string.
1609 *
1610 * @returns Pointer to read-only mode name string.
1611 * @param bMode The mode value (BS3_MODE_XXX).
1612 */
1613BS3_CMN_PROTO_STUB(const char BS3_FAR *, Bs3GetModeName,(uint8_t bMode));
1614
1615/**
1616 * Translate a mode into a short lower case string.
1617 *
1618 * @returns Pointer to read-only short mode name string.
1619 * @param bMode The mode value (BS3_MODE_XXX).
1620 */
1621BS3_CMN_PROTO_STUB(const char BS3_FAR *, Bs3GetModeNameShortLower,(uint8_t bMode));
1622
1623/** CPU vendors. */
1624typedef enum BS3CPUVENDOR
1625{
1626 BS3CPUVENDOR_INVALID = 0,
1627 BS3CPUVENDOR_INTEL,
1628 BS3CPUVENDOR_AMD,
1629 BS3CPUVENDOR_VIA,
1630 BS3CPUVENDOR_CYRIX,
1631 BS3CPUVENDOR_SHANGHAI,
1632 BS3CPUVENDOR_HYGON,
1633 BS3CPUVENDOR_UNKNOWN,
1634 BS3CPUVENDOR_END
1635} BS3CPUVENDOR;
1636
1637/**
1638 * Tries to detect the CPU vendor.
1639 *
1640 * @returns CPU vendor.
1641 */
1642BS3_CMN_PROTO_STUB(BS3CPUVENDOR, Bs3GetCpuVendor,(void));
1643
1644/**
1645 * Shutdown the system, never returns.
1646 *
1647 * This currently only works for VMs. When running on real systems it will
1648 * just halt the CPU.
1649 */
1650BS3_CMN_PROTO_NOSB(void, Bs3Shutdown,(void));
1651
1652/**
1653 * Prints a 32-bit unsigned value as decimal to the screen.
1654 *
1655 * @param uValue The 32-bit value.
1656 */
1657BS3_CMN_PROTO_NOSB(void, Bs3PrintU32,(uint32_t uValue));
1658
1659/**
1660 * Prints a 32-bit unsigned value as hex to the screen.
1661 *
1662 * @param uValue The 32-bit value.
1663 */
1664BS3_CMN_PROTO_NOSB(void, Bs3PrintX32,(uint32_t uValue));
1665
1666/**
1667 * Formats and prints a string to the screen.
1668 *
1669 * See #Bs3StrFormatV for supported format types.
1670 *
1671 * @param pszFormat The format string.
1672 * @param ... Format arguments.
1673 */
1674BS3_CMN_PROTO_STUB(size_t, Bs3Printf,(const char BS3_FAR *pszFormat, ...));
1675
1676/**
1677 * Formats and prints a string to the screen, va_list version.
1678 *
1679 * See #Bs3StrFormatV for supported format types.
1680 *
1681 * @param pszFormat The format string.
1682 * @param va Format arguments.
1683 */
1684BS3_CMN_PROTO_STUB(size_t, Bs3PrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
1685
1686/**
1687 * Prints a string to the screen.
1688 *
1689 * @param pszString The string to print.
1690 */
1691BS3_CMN_PROTO_STUB(void, Bs3PrintStr,(const char BS3_FAR *pszString));
1692
1693/**
1694 * Prints a string to the screen.
1695 *
1696 * @param pszString The string to print. Any terminator charss will be printed.
1697 * @param cchString The exact number of characters to print.
1698 */
1699BS3_CMN_PROTO_NOSB(void, Bs3PrintStrN,(const char BS3_FAR *pszString, size_t cchString));
1700
1701/**
1702 * Prints a char to the screen.
1703 *
1704 * @param ch The character to print.
1705 */
1706BS3_CMN_PROTO_NOSB(void, Bs3PrintChr,(char ch));
1707
1708
1709/**
1710 * An output function for #Bs3StrFormatV.
1711 *
1712 * @returns Number of characters written.
1713 * @param ch The character to write. Zero in the final call.
1714 * @param pvUser User argument supplied to #Bs3StrFormatV.
1715 */
1716typedef BS3_DECL_CALLBACK(size_t) FNBS3STRFORMATOUTPUT(char ch, void BS3_FAR *pvUser);
1717/** Pointer to an output function for #Bs3StrFormatV. */
1718typedef FNBS3STRFORMATOUTPUT *PFNBS3STRFORMATOUTPUT;
1719
1720/**
1721 * Formats a string, sending the output to @a pfnOutput.
1722 *
1723 * Supported types:
1724 * - %RI8, %RI16, %RI32, %RI64
1725 * - %RU8, %RU16, %RU32, %RU64
1726 * - %RX8, %RX16, %RX32, %RX64
1727 * - %i, %d
1728 * - %u
1729 * - %x
1730 * - %c
1731 * - %p (far pointer)
1732 * - %s (far pointer)
1733 *
1734 * @returns Sum of @a pfnOutput return values.
1735 * @param pszFormat The format string.
1736 * @param va Format arguments.
1737 * @param pfnOutput The output function.
1738 * @param pvUser The user argument for the output function.
1739 */
1740BS3_CMN_PROTO_STUB(size_t, Bs3StrFormatV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va,
1741 PFNBS3STRFORMATOUTPUT pfnOutput, void BS3_FAR *pvUser));
1742
1743/**
1744 * Formats a string into a buffer.
1745 *
1746 * See #Bs3StrFormatV for supported format types.
1747 *
1748 * @returns The length of the formatted string (excluding terminator).
1749 * This will be higher or equal to @c cbBuf in case of an overflow.
1750 * @param pszBuf The output buffer.
1751 * @param cbBuf The size of the output buffer.
1752 * @param pszFormat The format string.
1753 * @param va Format arguments.
1754 */
1755BS3_CMN_PROTO_STUB(size_t, Bs3StrPrintfV,(char BS3_FAR *pszBuf, size_t cbBuf, const char BS3_FAR *pszFormat, va_list BS3_FAR va));
1756
1757/**
1758 * Formats a string into a buffer.
1759 *
1760 * See #Bs3StrFormatV for supported format types.
1761 *
1762 * @returns The length of the formatted string (excluding terminator).
1763 * This will be higher or equal to @c cbBuf in case of an overflow.
1764 * @param pszBuf The output buffer.
1765 * @param cbBuf The size of the output buffer.
1766 * @param pszFormat The format string.
1767 * @param ... Format arguments.
1768 */
1769BS3_CMN_PROTO_STUB(size_t, Bs3StrPrintf,(char BS3_FAR *pszBuf, size_t cbBuf, const char BS3_FAR *pszFormat, ...));
1770
1771
1772/**
1773 * Finds the length of a zero terminated string.
1774 *
1775 * @returns String length in chars/bytes.
1776 * @param pszString The string to examine.
1777 */
1778BS3_CMN_PROTO_STUB(size_t, Bs3StrLen,(const char BS3_FAR *pszString));
1779
1780/**
1781 * Finds the length of a zero terminated string, but with a max length.
1782 *
1783 * @returns String length in chars/bytes, or @a cchMax if no zero-terminator
1784 * was found before we reached the limit.
1785 * @param pszString The string to examine.
1786 * @param cchMax The max length to examine.
1787 */
1788BS3_CMN_PROTO_STUB(size_t, Bs3StrNLen,(const char BS3_FAR *pszString, size_t cchMax));
1789
1790/**
1791 * CRT style unsafe strcpy.
1792 *
1793 * @returns pszDst.
1794 * @param pszDst The destination buffer. Must be large enough to
1795 * hold the source string.
1796 * @param pszSrc The source string.
1797 */
1798BS3_CMN_PROTO_STUB(char BS3_FAR *, Bs3StrCpy,(char BS3_FAR *pszDst, const char BS3_FAR *pszSrc));
1799
1800/**
1801 * CRT style memcpy.
1802 *
1803 * @returns pvDst
1804 * @param pvDst The destination buffer.
1805 * @param pvSrc The source buffer.
1806 * @param cbToCopy The number of bytes to copy.
1807 * @sa Bs3MemCopyFlat
1808 */
1809BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemCpy,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1810
1811/**
1812 * GNU (?) style mempcpy.
1813 *
1814 * @returns pvDst + cbCopy
1815 * @param pvDst The destination buffer.
1816 * @param pvSrc The source buffer.
1817 * @param cbToCopy The number of bytes to copy.
1818 */
1819BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemPCpy,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1820
1821/**
1822 * CRT style memmove (overlapping buffers is fine).
1823 *
1824 * @returns pvDst
1825 * @param pvDst The destination buffer.
1826 * @param pvSrc The source buffer.
1827 * @param cbToCopy The number of bytes to copy.
1828 */
1829BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemMove,(void BS3_FAR *pvDst, const void BS3_FAR *pvSrc, size_t cbToCopy));
1830
1831/**
1832 * BSD style bzero.
1833 *
1834 * @param pvDst The buffer to be zeroed.
1835 * @param cbDst The number of bytes to zero.
1836 */
1837BS3_CMN_PROTO_NOSB(void, Bs3MemZero,(void BS3_FAR *pvDst, size_t cbDst));
1838
1839/**
1840 * CRT style memset.
1841 *
1842 * @param pvDst The buffer to be fill.
1843 * @param bFiller The filler byte.
1844 * @param cbDst The number of bytes to fill.
1845 */
1846BS3_CMN_PROTO_NOSB(void, Bs3MemSet,(void BS3_FAR *pvDst, uint8_t bFiller, size_t cbDst));
1847
1848/**
1849 * CRT style memchr.
1850 *
1851 * @param pvHaystack The memory to scan for @a bNeedle.
1852 * @param bNeedle The byte to search for.
1853 * @param cbHaystack The amount of memory to search.
1854 */
1855BS3_CMN_PROTO_NOSB(void BS3_FAR *, Bs3MemChr,(void const BS3_FAR *pvHaystack, uint8_t bNeedle, size_t cbHaystack));
1856
1857/**
1858 * CRT style memcmp.
1859 *
1860 * @returns 0 if equal. Negative if the left side is 'smaller' than the right
1861 * side, and positive in the other case.
1862 * @param pv1 The left hand memory.
1863 * @param pv2 The right hand memory.
1864 * @param cb The number of bytes to compare.
1865 */
1866BS3_CMN_PROTO_NOSB(int, Bs3MemCmp,(void const BS3_FAR *pv1, void const BS3_FAR *pv2, size_t cb));
1867
1868BS3_CMN_PROTO_STUB(void, Bs3UInt64Div,(RTUINT64U uDividend, RTUINT64U uDivisor, RTUINT64U BS3_FAR *paQuotientReminder));
1869BS3_CMN_PROTO_STUB(void, Bs3UInt32Div,(RTUINT32U uDividend, RTUINT32U uDivisor, RTUINT32U BS3_FAR *paQuotientReminder));
1870
1871
1872/**
1873 * Converts a protected mode 32-bit far pointer to a 32-bit flat address.
1874 *
1875 * @returns 32-bit flat address.
1876 * @param off The segment offset.
1877 * @param uSel The protected mode segment selector.
1878 */
1879BS3_CMN_PROTO_STUB(uint32_t, Bs3SelProtFar32ToFlat32,(uint32_t off, uint16_t uSel));
1880
1881/**
1882 * Converts a current mode 32-bit far pointer to a 32-bit flat address.
1883 *
1884 * @returns 32-bit flat address.
1885 * @param off The segment offset.
1886 * @param uSel The current mode segment selector.
1887 */
1888BS3_CMN_PROTO_STUB(uint32_t, Bs3SelFar32ToFlat32,(uint32_t off, uint16_t uSel));
1889
1890/**
1891 * Wrapper around Bs3SelFar32ToFlat32 that makes it easier to use in tight
1892 * assembly spots.
1893 *
1894 * @returns 32-bit flat address.
1895 * @param off The segment offset.
1896 * @param uSel The current mode segment selector.
1897 * @remarks All register are preserved, except return.
1898 * @remarks No 20h scratch space required in 64-bit mode.
1899 */
1900BS3_CMN_PROTO_FARSTUB(6, uint32_t, Bs3SelFar32ToFlat32NoClobber,(uint32_t off, uint16_t uSel));
1901
1902/**
1903 * Converts a real mode code segment to a protected mode code segment selector.
1904 *
1905 * @returns protected mode segment selector.
1906 * @param uRealSeg Real mode code segment.
1907 * @remarks All register are preserved, except return and parameter.
1908 */
1909BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelRealModeCodeToProtMode,(uint16_t uRealSeg));
1910
1911/**
1912 * Converts a real mode code segment to a protected mode code segment selector.
1913 *
1914 * @returns protected mode segment selector.
1915 * @param uProtSel Real mode code segment.
1916 * @remarks All register are preserved, except return and parameter.
1917 */
1918BS3_CMN_PROTO_NOSB(uint16_t, Bs3SelProtModeCodeToRealMode,(uint16_t uProtSel));
1919
1920/**
1921 * Converts a flat code address to a real mode segment and offset.
1922 *
1923 * @returns Far real mode address (high 16-bit is segment, low is offset).
1924 * @param uFlatAddr Flat code address.
1925 * @remarks All register are preserved, except return and parameter.
1926 */
1927BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToRealMode,(uint32_t uFlatAddr));
1928
1929/**
1930 * Converts a flat code address to a protected mode 16-bit far pointer (ring-0).
1931 *
1932 * @returns Far 16-bit protected mode address (high 16-bit is segment selector,
1933 * low is segment offset).
1934 * @param uFlatAddr Flat code address.
1935 * @remarks All register are preserved, except return and parameter.
1936 */
1937BS3_CMN_PROTO_NOSB(uint32_t, Bs3SelFlatCodeToProtFar16,(uint32_t uFlatAddr));
1938
1939/**
1940 * Converts a far 16:16 real mode (code) address to a flat address.
1941 *
1942 * @returns 32-bit flat address.
1943 * @param uFar1616 Far real mode address (high 16-bit is segment, low
1944 * is offset).
1945 * @remarks All register are preserved, except return.
1946 * @remarks No 20h scratch space required in 64-bit mode.
1947 * @remarks Exactly the same as Bs3SelRealModeDataToFlat, except for param.
1948 */
1949BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeCodeToFlat,(PFNBS3FARADDRCONV uFar1616));
1950
1951/**
1952 * Converts a far 16:16 real mode (code) address to a 16-bit protected mode
1953 * address.
1954 *
1955 * This is mainly for converting X0TEXT16 and X1TEXT16 linker pointers to
1956 * protected mode ones.
1957 *
1958 * @returns Protected mode function pointer.
1959 * @param uFar1616 Far real mode address (high 16-bit is segment, low
1960 * is offset). In 16-bit code, this is also the same
1961 * as a linker address.
1962 */
1963BS3_CMN_PROTO_FARSTUB(4, PFNBS3FARADDRCONV, Bs3SelRealModeCodeToProtFar16,(PFNBS3FARADDRCONV uFar1616));
1964
1965/**
1966 * Converts a flat data address to a real mode segment and offset.
1967 *
1968 * @returns Far real mode address (high 16-bit is segment, low is offset)
1969 * @param uFlatAddr Flat code address.
1970 * @remarks All register are preserved, except return.
1971 * @remarks No 20h scratch space required in 64-bit mode.
1972 */
1973BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToRealMode,(uint32_t uFlatAddr));
1974
1975/**
1976 * Converts a flat data address to a real mode segment and offset.
1977 *
1978 * @returns Far 16-bit protected mode address (high 16-bit is segment selector,
1979 * low is segment offset).
1980 * @param uFlatAddr Flat code address.
1981 * @remarks All register are preserved, except return.
1982 * @remarks No 20h scratch space required in 64-bit mode.
1983 */
1984BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelFlatDataToProtFar16,(uint32_t uFlatAddr));
1985
1986/**
1987 * Converts a far 16:16 data address to a real mode segment and offset.
1988 *
1989 * @returns Far real mode address (high 16-bit is segment, low is offset)
1990 * @param uFar1616 Far 16-bit protected mode address (high 16-bit is
1991 * segment selector, low is segment offset).
1992 * @remarks All register are preserved, except return.
1993 * @remarks No 20h scratch space required in 64-bit mode.
1994 */
1995BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToRealMode,(uint32_t uFar1616));
1996
1997/**
1998 * Converts a far 16:16 real mode address to a 16-bit protected mode address.
1999 *
2000 * @returns Far real mode address (high 16-bit is segment, low is offset)
2001 * @param uFar1616 Far real mode address (high 16-bit is segment, low
2002 * is offset).
2003 * @remarks All register are preserved, except return.
2004 * @remarks No 20h scratch space required in 64-bit mode.
2005 */
2006BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToProtFar16,(uint32_t uFar1616));
2007
2008/**
2009 * Converts a far 16:16 data address to a flat 32-bit address.
2010 *
2011 * @returns 32-bit flat address.
2012 * @param uFar1616 Far 16-bit protected mode address (high 16-bit is
2013 * segment selector, low is segment offset).
2014 * @remarks All register are preserved, except return.
2015 * @remarks No 20h scratch space required in 64-bit mode.
2016 */
2017BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelProtFar16DataToFlat,(uint32_t uFar1616));
2018
2019/**
2020 * Converts a far 16:16 real mode address to a flat address.
2021 *
2022 * @returns 32-bit flat address.
2023 * @param uFar1616 Far real mode address (high 16-bit is segment, low
2024 * is offset).
2025 * @remarks All register are preserved, except return.
2026 * @remarks No 20h scratch space required in 64-bit mode.
2027 */
2028BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelRealModeDataToFlat,(uint32_t uFar1616));
2029
2030/**
2031 * Converts a link-time data pointer to a current context pointer.
2032 *
2033 * @returns Converted pointer.
2034 * @param pvLnkPtr The pointer the linker produced.
2035 */
2036BS3_CMN_PROTO_FARSTUB(4, void BS3_FAR *, Bs3SelLnkPtrToCurPtr,(void BS3_FAR *pvLnkPtr));
2037
2038/**
2039 * Converts a link-time data pointer to a flat address.
2040 *
2041 * @returns 32-bit flag address.
2042 * @param pvLnkPtr The pointer the linker produced.
2043 */
2044BS3_CMN_PROTO_FARSTUB(4, uint32_t, Bs3SelLnkPtrToFlat,(void BS3_FAR *pvLnkPtr));
2045
2046/**
2047 * Converts a link-time code pointer to a current context pointer.
2048 *
2049 * @returns Converted function pointer.
2050 * @param pfnLnkPtr The function pointer the linker produced.
2051 */
2052BS3_CMN_PROTO_FARSTUB(4, FPFNBS3FAR, Bs3SelLnkCodePtrToCurPtr,(FPFNBS3FAR pfnLnkPtr));
2053
2054/**
2055 * Gets a flat address from a working poitner.
2056 *
2057 * @returns flat address (32-bit or 64-bit).
2058 * @param pv Current context pointer.
2059 */
2060DECLINLINE(RTCCUINTXREG) Bs3SelPtrToFlat(void BS3_FAR *pv)
2061{
2062#if ARCH_BITS == 16
2063 return BS3_CMN_FN_NM(Bs3SelFar32ToFlat32)(BS3_FP_OFF(pv), BS3_FP_SEG(pv));
2064#else
2065 return (uintptr_t)pv;
2066#endif
2067}
2068
2069/**
2070 * Sets up a 16-bit read-write data selector with ring-3 access and 64KB limit.
2071 *
2072 * @param pDesc Pointer to the descriptor table entry.
2073 * @param uBaseAddr The base address of the descriptor.
2074 */
2075BS3_CMN_PROTO_STUB(void, Bs3SelSetup16BitData,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr));
2076
2077/**
2078 * Sets up a 16-bit execute-read selector with a 64KB limit.
2079 *
2080 * @param pDesc Pointer to the descriptor table entry.
2081 * @param uBaseAddr The base address of the descriptor.
2082 * @param bDpl The descriptor privilege level.
2083 */
2084BS3_CMN_PROTO_STUB(void, Bs3SelSetup16BitCode,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr, uint8_t bDpl));
2085
2086/**
2087 * Sets up a 32-bit execute-read selector with a user specified limit.
2088 *
2089 * @param pDesc Pointer to the descriptor table entry.
2090 * @param uBaseAddr The base address of the descriptor.
2091 * @param uLimit The limit. (This is included here and not in the 16-bit
2092 * functions because we're more likely to want to set it
2093 * than for 16-bit selectors.)
2094 * @param bDpl The descriptor privilege level.
2095 */
2096BS3_CMN_PROTO_STUB(void, Bs3SelSetup32BitCode,(X86DESC BS3_FAR *pDesc, uint32_t uBaseAddr, uint32_t uLimit, uint8_t bDpl));
2097
2098/**
2099 * Sets up a 16-bit or 32-bit gate descriptor.
2100 *
2101 * This can be used both for GDT/LDT and IDT.
2102 *
2103 * @param pDesc Pointer to the descriptor table entry.
2104 * @param bType The gate type.
2105 * @param bDpl The gate DPL.
2106 * @param uSel The gate selector value.
2107 * @param off The gate IP/EIP value.
2108 * @param cParams Number of parameters to copy if call-gate.
2109 */
2110BS3_CMN_PROTO_STUB(void, Bs3SelSetupGate,(X86DESC BS3_FAR *pDesc, uint8_t bType, uint8_t bDpl,
2111 uint16_t uSel, uint32_t off, uint8_t cParams));
2112
2113/**
2114 * Sets up a 64-bit gate descriptor.
2115 *
2116 * This can be used both for GDT/LDT and IDT.
2117 *
2118 * @param pDescPair Pointer to the _two_ descriptor table entries.
2119 * @param bType The gate type.
2120 * @param bDpl The gate DPL.
2121 * @param uSel The gate selector value.
2122 * @param off The gate IP/EIP value.
2123 */
2124BS3_CMN_PROTO_STUB(void, Bs3SelSetupGate64,(X86DESC BS3_FAR *pDescPair, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint64_t off));
2125
2126
2127/**
2128 * Slab control structure list head.
2129 *
2130 * The slabs on the list must all have the same chunk size.
2131 */
2132typedef struct BS3SLABHEAD
2133{
2134 /** Pointer to the first slab. */
2135 BS3_XPTR_MEMBER(struct BS3SLABCTL, pFirst);
2136 /** The allocation chunk size. */
2137 uint16_t cbChunk;
2138 /** Number of slabs in the list. */
2139 uint16_t cSlabs;
2140 /** Number of chunks in the list. */
2141 uint32_t cChunks;
2142 /** Number of free chunks. */
2143 uint32_t cFreeChunks;
2144} BS3SLABHEAD;
2145AssertCompileSize(BS3SLABHEAD, 16);
2146/** Pointer to a slab list head. */
2147typedef BS3SLABHEAD BS3_FAR *PBS3SLABHEAD;
2148
2149/**
2150 * Allocation slab control structure.
2151 *
2152 * This may live at the start of the slab for 4KB slabs, while in a separate
2153 * static location for the larger ones.
2154 */
2155typedef struct BS3SLABCTL
2156{
2157 /** Pointer to the next slab control structure in this list. */
2158 BS3_XPTR_MEMBER(struct BS3SLABCTL, pNext);
2159 /** Pointer to the slab list head. */
2160 BS3_XPTR_MEMBER(BS3SLABHEAD, pHead);
2161 /** The base address of the slab. */
2162 BS3_XPTR_MEMBER(uint8_t, pbStart);
2163 /** Number of chunks in this slab. */
2164 uint16_t cChunks;
2165 /** Number of currently free chunks. */
2166 uint16_t cFreeChunks;
2167 /** The chunk size. */
2168 uint16_t cbChunk;
2169 /** The shift count corresponding to cbChunk.
2170 * This is for turning a chunk number into a byte offset and vice versa. */
2171 uint16_t cChunkShift;
2172 /** Bitmap where set bits indicates allocated blocks (variable size,
2173 * multiple of 4). */
2174 uint8_t bmAllocated[4];
2175} BS3SLABCTL;
2176/** Pointer to a bs3kit slab control structure. */
2177typedef BS3SLABCTL BS3_FAR *PBS3SLABCTL;
2178
2179/** The chunks must all be in the same 16-bit segment tile. */
2180#define BS3_SLAB_ALLOC_F_SAME_TILE UINT16_C(0x0001)
2181
2182/**
2183 * Initializes a slab.
2184 *
2185 * @param pSlabCtl The slab control structure to initialize.
2186 * @param cbSlabCtl The size of the slab control structure.
2187 * @param uFlatSlabPtr The base address of the slab.
2188 * @param cbSlab The size of the slab.
2189 * @param cbChunk The chunk size.
2190 */
2191BS3_CMN_PROTO_STUB(void, Bs3SlabInit,(PBS3SLABCTL pSlabCtl, size_t cbSlabCtl, uint32_t uFlatSlabPtr,
2192 uint32_t cbSlab, uint16_t cbChunk));
2193
2194/**
2195 * Allocates one chunk from a slab.
2196 *
2197 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2198 * @param pSlabCtl The slab control structure to allocate from.
2199 */
2200BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabAlloc,(PBS3SLABCTL pSlabCtl));
2201
2202/**
2203 * Allocates one or more chunks from a slab.
2204 *
2205 * @returns Pointer to the request number of chunks on success, NULL if we're
2206 * out of chunks.
2207 * @param pSlabCtl The slab control structure to allocate from.
2208 * @param cChunks The number of contiguous chunks we want.
2209 * @param fFlags Flags, see BS3_SLAB_ALLOC_F_XXX
2210 */
2211BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabAllocEx,(PBS3SLABCTL pSlabCtl, uint16_t cChunks, uint16_t fFlags));
2212
2213/**
2214 * Allocates a specific range of chunks from a slab.
2215 *
2216 * @returns Number of chunks it was possible to allocate in the slab.
2217 * @retval 0 if the given address isn't in the slab.
2218 * @retval UINT16_MAX if one or more of the requested chunks are already in
2219 * use, so the request cannot be fulfilled.
2220 * @param pSlabCtl The slab control structure to allocate from.
2221 * @param uFlatAddr The flat address of the range to allocate.
2222 * @param cChunks The number of contiguous chunks we want.
2223 */
2224BS3_CMN_PROTO_STUB(uint16_t, Bs3SlabAllocFixed,(PBS3SLABCTL pSlabCtl, uint32_t uFlatAddr, uint16_t cChunks));
2225
2226/**
2227 * Frees one or more chunks from a slab.
2228 *
2229 * @returns Number of chunks actually freed. When correctly used, this will
2230 * match the @a cChunks parameter, of course.
2231 * @param pSlabCtl The slab control structure to free from.
2232 * @param uFlatChunkPtr The flat address of the chunks to free.
2233 * @param cChunks The number of contiguous chunks to free.
2234 */
2235BS3_CMN_PROTO_STUB(uint16_t, Bs3SlabFree,(PBS3SLABCTL pSlabCtl, uint32_t uFlatChunkPtr, uint16_t cChunks));
2236
2237
2238/**
2239 * Initializes the given slab list head.
2240 *
2241 * @param pHead The slab list head.
2242 * @param cbChunk The chunk size.
2243 */
2244BS3_CMN_PROTO_STUB(void, Bs3SlabListInit,(PBS3SLABHEAD pHead, uint16_t cbChunk));
2245
2246/**
2247 * Adds an initialized slab control structure to the list.
2248 *
2249 * @param pHead The slab list head to add it to.
2250 * @param pSlabCtl The slab control structure to add.
2251 */
2252BS3_CMN_PROTO_STUB(void, Bs3SlabListAdd,(PBS3SLABHEAD pHead, PBS3SLABCTL pSlabCtl));
2253
2254/**
2255 * Allocates one chunk.
2256 *
2257 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2258 * @param pHead The slab list to allocate from.
2259 */
2260BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabListAlloc,(PBS3SLABHEAD pHead));
2261
2262/**
2263 * Allocates one or more chunks.
2264 *
2265 * @returns Pointer to the request number of chunks on success, NULL if we're
2266 * out of chunks.
2267 * @param pHead The slab list to allocate from.
2268 * @param cChunks The number of contiguous chunks we want.
2269 * @param fFlags Flags, see BS3_SLAB_ALLOC_F_XXX
2270 */
2271BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3SlabListAllocEx,(PBS3SLABHEAD pHead, uint16_t cChunks, uint16_t fFlags));
2272
2273/**
2274 * Frees one or more chunks from a slab list.
2275 *
2276 * @param pHead The slab list to allocate from.
2277 * @param pvChunks Pointer to the first chunk to free.
2278 * @param cChunks The number of contiguous chunks to free.
2279 */
2280BS3_CMN_PROTO_STUB(void, Bs3SlabListFree,(PBS3SLABHEAD pHead, void BS3_FAR *pvChunks, uint16_t cChunks));
2281
2282/**
2283 * Allocation addressing constraints.
2284 */
2285typedef enum BS3MEMKIND
2286{
2287 /** Invalid zero type. */
2288 BS3MEMKIND_INVALID = 0,
2289 /** Real mode addressable memory. */
2290 BS3MEMKIND_REAL,
2291 /** Memory addressable using the 16-bit protected mode tiling. */
2292 BS3MEMKIND_TILED,
2293 /** Memory addressable using 32-bit flat addressing. */
2294 BS3MEMKIND_FLAT32,
2295 /** Memory addressable using 64-bit flat addressing. */
2296 BS3MEMKIND_FLAT64,
2297 /** End of valid types. */
2298 BS3MEMKIND_END,
2299} BS3MEMKIND;
2300
2301/**
2302 * Allocates low memory.
2303 *
2304 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2305 * @param enmKind The kind of addressing constraints imposed on the
2306 * allocation.
2307 * @param cb How much to allocate.
2308 */
2309BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemAlloc,(BS3MEMKIND enmKind, size_t cb));
2310
2311/**
2312 * Allocates zero'ed memory.
2313 *
2314 * @param enmKind The kind of addressing constraints imposed on the
2315 * allocation.
2316 * @param cb How much to allocate.
2317 */
2318BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemAllocZ,(BS3MEMKIND enmKind, size_t cb));
2319
2320/**
2321 * Frees memory.
2322 *
2323 * @returns Pointer to a chunk on success, NULL if we're out of chunks.
2324 * @param pv The memory to free (returned by #Bs3MemAlloc).
2325 * @param cb The size of the allocation.
2326 */
2327BS3_CMN_PROTO_STUB(void, Bs3MemFree,(void BS3_FAR *pv, size_t cb));
2328
2329/**
2330 * Allocates a page with non-present pages on each side.
2331 *
2332 * @returns Pointer to the usable page. NULL on failure. Use
2333 * Bs3MemGuardedTestPageFree to free the allocation.
2334 * @param enmKind The kind of addressing constraints imposed on the
2335 * allocation.
2336 */
2337BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemGuardedTestPageAlloc,(BS3MEMKIND enmKind));
2338
2339/**
2340 * Allocates a page with pages on each side to the @a fPte specification.
2341 *
2342 * @returns Pointer to the usable page. NULL on failure. Use
2343 * Bs3MemGuardedTestPageFree to free the allocation.
2344 * @param enmKind The kind of addressing constraints imposed on the
2345 * allocation.
2346 * @param fPte The page table entry specification for the guard pages.
2347 */
2348BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3MemGuardedTestPageAllocEx,(BS3MEMKIND enmKind, uint64_t fPte));
2349
2350/**
2351 * Frees guarded page allocated by Bs3MemGuardedTestPageAlloc or
2352 * Bs3MemGuardedTestPageAllocEx.
2353 *
2354 * @param pvGuardedPage Pointer returned by Bs3MemGuardedTestPageAlloc or
2355 * Bs3MemGuardedTestPageAllocEx. NULL is ignored.
2356 */
2357BS3_CMN_PROTO_STUB(void, Bs3MemGuardedTestPageFree,(void BS3_FAR *pvGuardedPage));
2358
2359/**
2360 * Print all heap info.
2361 */
2362BS3_CMN_PROTO_STUB(void, Bs3MemPrintInfo, (void));
2363
2364/** The end RAM address below 4GB (approximately). */
2365extern uint32_t g_uBs3EndOfRamBelow4G;
2366/** The end RAM address above 4GB, zero if no memory above 4GB. */
2367extern uint64_t g_uBs3EndOfRamAbove4G;
2368
2369
2370/**
2371 * Enables the A20 gate.
2372 */
2373BS3_CMN_PROTO_NOSB(void, Bs3A20Enable,(void));
2374
2375/**
2376 * Enables the A20 gate via the keyboard controller
2377 */
2378BS3_CMN_PROTO_NOSB(void, Bs3A20EnableViaKbd,(void));
2379
2380/**
2381 * Enables the A20 gate via the PS/2 control port A.
2382 */
2383BS3_CMN_PROTO_NOSB(void, Bs3A20EnableViaPortA,(void));
2384
2385/**
2386 * Disables the A20 gate.
2387 */
2388BS3_CMN_PROTO_NOSB(void, Bs3A20Disable,(void));
2389
2390/**
2391 * Disables the A20 gate via the keyboard controller
2392 */
2393BS3_CMN_PROTO_NOSB(void, Bs3A20DisableViaKbd,(void));
2394
2395/**
2396 * Disables the A20 gate via the PS/2 control port A.
2397 */
2398BS3_CMN_PROTO_NOSB(void, Bs3A20DisableViaPortA,(void));
2399
2400
2401/**
2402 * Initializes root page tables for page protected mode (PP16, PP32).
2403 *
2404 * @returns IPRT status code.
2405 * @remarks Must not be called in real-mode!
2406 */
2407BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForPP,(void));
2408
2409/**
2410 * Initializes root page tables for PAE page protected mode (PAE16, PAE32).
2411 *
2412 * @returns IPRT status code.
2413 * @remarks The default long mode page tables depends on the PAE ones.
2414 * @remarks Must not be called in real-mode!
2415 */
2416BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForPAE,(void));
2417
2418/**
2419 * Initializes root page tables for long mode (LM16, LM32, LM64).
2420 *
2421 * @returns IPRT status code.
2422 * @remarks The default long mode page tables depends on the PAE ones.
2423 * @remarks Must not be called in real-mode!
2424 */
2425BS3_CMN_PROTO_STUB(int, Bs3PagingInitRootForLM,(void));
2426
2427/**
2428 * Maps all RAM above 4GB into the long mode page tables.
2429 *
2430 * This requires Bs3PagingInitRootForLM to have been called first.
2431 *
2432 * @returns IPRT status code.
2433 * @retval VERR_WRONG_ORDER if Bs3PagingInitRootForLM wasn't called.
2434 * @retval VINF_ALREADY_INITIALIZED if already called or someone mapped
2435 * something else above 4GiB already.
2436 * @retval VERR_OUT_OF_RANGE if too much RAM (more than 2^47 bytes).
2437 * @retval VERR_NO_MEMORY if no more memory for paging structures.
2438 * @retval VERR_UNSUPPORTED_ALIGNMENT if the bs3kit allocator malfunctioned and
2439 * didn't give us page aligned memory as it should.
2440 *
2441 * @param puFailurePoint Where to return the address where we encountered
2442 * a failure. Optional.
2443 *
2444 * @remarks Must be called in 32-bit or 64-bit mode as paging structures will be
2445 * allocated using BS3MEMKIND_FLAT32, as there might not be sufficient
2446 * BS3MEMKIND_TILED memory around. (Also, too it's simply too much of
2447 * a bother to deal with 16-bit for something that's long-mode only.)
2448 */
2449BS3_CMN_PROTO_STUB(int, Bs3PagingMapRamAbove4GForLM,(uint64_t *puFailurePoint));
2450
2451/**
2452 * Modifies the page table protection of an address range.
2453 *
2454 * This only works on the lowest level of the page tables in the current mode.
2455 *
2456 * Since we generally use the largest pages available when setting up the
2457 * initial page tables, this function will usually have to allocate and create
2458 * more tables. This may fail if we're low on memory.
2459 *
2460 * @returns IPRT status code.
2461 * @param uFlat The flat address of the first page in the range (rounded
2462 * down nearest page boundrary).
2463 * @param cb The range size from @a pv (rounded up to nearest page boundrary).
2464 * @param fSet Mask of zero or more X86_PTE_XXX values to set for the range.
2465 * @param fClear Mask of zero or more X86_PTE_XXX values to clear for the range.
2466 */
2467BS3_CMN_PROTO_STUB(int, Bs3PagingProtect,(uint64_t uFlat, uint64_t cb, uint64_t fSet, uint64_t fClear));
2468
2469/**
2470 * Modifies the page table protection of an address range.
2471 *
2472 * This only works on the lowest level of the page tables in the current mode.
2473 *
2474 * Since we generally use the largest pages available when setting up the
2475 * initial page tables, this function will usually have to allocate and create
2476 * more tables. This may fail if we're low on memory.
2477 *
2478 * @returns IPRT status code.
2479 * @param pv The address of the first page in the range (rounded
2480 * down nearest page boundrary).
2481 * @param cb The range size from @a pv (rounded up to nearest page boundrary).
2482 * @param fSet Mask of zero or more X86_PTE_XXX values to set for the range.
2483 * @param fClear Mask of zero or more X86_PTE_XXX values to clear for the range.
2484 */
2485BS3_CMN_PROTO_STUB(int, Bs3PagingProtectPtr,(void BS3_FAR *pv, size_t cb, uint64_t fSet, uint64_t fClear));
2486
2487/**
2488 * Aliases (maps) one or more contiguous physical pages to a virtual range.
2489 *
2490 * @returns VBox status code.
2491 * @retval VERR_INVALID_PARAMETER if we're in legacy paging mode and @a uDst or
2492 * @a uPhysToAlias are not compatible with legacy paging.
2493 * @retval VERR_OUT_OF_RANGE if we cannot traverse the page tables in this mode
2494 * (typically real mode or v86, maybe 16-bit PE).
2495 * @retval VERR_NO_MEMORY if we cannot allocate page tables for splitting up
2496 * the necessary large pages. No aliasing was performed.
2497 *
2498 * @param uDst The virtual address to map it at. Rounded down
2499 * to the nearest page (@a cbHowMuch is adjusted
2500 * up).
2501 * @param uPhysToAlias The physical address of the first page in the
2502 * (contiguous) range to map. Chopped down to
2503 * nearest page boundrary (@a cbHowMuch is not
2504 * adjusted).
2505 * @param cbHowMuch How much to map. Rounded up to nearest page.
2506 * @param fPte The PTE flags.
2507 */
2508BS3_CMN_PROTO_STUB(int, Bs3PagingAlias,(uint64_t uDst, uint64_t uPhysToAlias, uint32_t cbHowMuch, uint64_t fPte));
2509
2510/**
2511 * Unaliases memory, i.e. restores the 1:1 mapping.
2512 *
2513 * @returns VBox status code. Cannot fail if @a uDst and @a cbHowMuch specify
2514 * the range of a successful Bs3PagingAlias call, however it may run
2515 * out of memory if it's breaking new ground.
2516 *
2517 * @param uDst The virtual address to restore to 1:1 mapping.
2518 * Rounded down to the nearest page (@a cbHowMuch
2519 * is adjusted up).
2520 * @param cbHowMuch How much to restore. Rounded up to nearest page.
2521 */
2522BS3_CMN_PROTO_STUB(int, Bs3PagingUnalias,(uint64_t uDst, uint32_t cbHowMuch));
2523
2524/**
2525 * Get the pointer to the PTE for the given address.
2526 *
2527 * @returns Pointer to the PTE.
2528 * @param uFlat The flat address of the page which PTE we want.
2529 * @param prc Where to return additional error info. Optional.
2530 */
2531BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3PagingGetPte,(uint64_t uFlat, int *prc));
2532
2533/**
2534 * Paging information for an address.
2535 */
2536typedef struct BS3PAGINGINFO4ADDR
2537{
2538 /** The depth of the system's paging mode.
2539 * This is always 2 for legacy, 3 for PAE and 4 for long mode. */
2540 uint8_t cEntries;
2541 /** The size of the page structures (the entires). */
2542 uint8_t cbEntry;
2543 /** Flags defined for future fun, currently zero. */
2544 uint16_t fFlags;
2545 /** Union display different view on the entry pointers. */
2546 union
2547 {
2548 /** Pointer to the page structure entries, starting with the PTE as 0.
2549 * If large pages are involved, the first entry will be NULL (first two if 1GB
2550 * page). Same if the address is invalid on a higher level. */
2551 uint8_t BS3_FAR *apbEntries[4];
2552 /** Alternative view for legacy mode. */
2553 struct
2554 {
2555 X86PTE BS3_FAR *pPte;
2556 X86PDE BS3_FAR *pPde;
2557 void *pvUnused2;
2558 void *pvUnused3;
2559 } Legacy;
2560 /** Alternative view for PAE and Long mode. */
2561 struct
2562 {
2563 X86PTEPAE BS3_FAR *pPte;
2564 X86PDEPAE BS3_FAR *pPde;
2565 X86PDPE BS3_FAR *pPdpe;
2566 X86PML4E BS3_FAR *pPml4e;
2567 } Pae;
2568 } u;
2569} BS3PAGINGINFO4ADDR;
2570/** Pointer to paging information for and address. */
2571typedef BS3PAGINGINFO4ADDR BS3_FAR *PBS3PAGINGINFO4ADDR;
2572
2573/**
2574 * Queries paging information about the given virtual address.
2575 *
2576 * @returns VBox status code.
2577 * @param uFlat The flat address to query information about.
2578 * @param pPgInfo Where to return the information.
2579 */
2580BS3_CMN_PROTO_STUB(int, Bs3PagingQueryAddressInfo,(uint64_t uFlat, PBS3PAGINGINFO4ADDR pPgInfo));
2581
2582
2583/** The physical / flat address of the buffer backing the canonical traps.
2584 * This buffer is spread equally on each side of the 64-bit non-canonical
2585 * address divide. Non-64-bit code can use this to setup trick shots and
2586 * inspect their results. */
2587extern uint32_t g_uBs3PagingCanonicalTrapsAddr;
2588/** The size of the buffer at g_uPagingCanonicalTraps (both sides). */
2589extern uint16_t g_cbBs3PagingCanonicalTraps;
2590/** The size of one trap buffer (low or high).
2591 * This is g_cbBs3PagingCanonicalTraps divided by two. */
2592extern uint16_t g_cbBs3PagingOneCanonicalTrap;
2593
2594/**
2595 * Sets up the 64-bit canonical address space trap buffers, if neceessary.
2596 *
2597 * @returns Pointer to the buffers (i.e. the first page of the low one) on
2598 * success. NULL on failure.
2599 */
2600BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3PagingSetupCanonicalTraps,(void));
2601
2602
2603/**
2604 * Call 16-bit prot mode function from v8086 mode.
2605 *
2606 * This switches from v8086 mode to 16-bit protected mode (code) and executes
2607 * @a fpfnCall with @a cbParams bytes of parameters pushed on the stack.
2608 * Afterwards it switches back to v8086 mode and returns a 16-bit status code.
2609 *
2610 * @returns 16-bit status code if the function returned anything.
2611 * @param fpfnCall Far real mode pointer to the function to call.
2612 * @param cbParams The size of the parameter list, in bytes.
2613 * @param ... The parameters.
2614 * @sa Bs3SwitchTo32BitAndCallC
2615 */
2616BS3_CMN_PROTO_STUB(int, Bs3SwitchFromV86To16BitAndCallC,(FPFNBS3FAR fpfnCall, unsigned cbParams, ...));
2617
2618
2619/**
2620 * BS3 integer register.
2621 */
2622typedef union BS3REG
2623{
2624 /** 8-bit unsigned integer. */
2625 uint8_t u8;
2626 /** 16-bit unsigned integer. */
2627 uint16_t u16;
2628 /** 32-bit unsigned integer. */
2629 uint32_t u32;
2630 /** 64-bit unsigned integer. */
2631 uint64_t u64;
2632 /** Full unsigned integer. */
2633 uint64_t u;
2634 /** High/low byte view. */
2635 struct
2636 {
2637 uint8_t bLo;
2638 uint8_t bHi;
2639 } b;
2640 /** 8-bit view. */
2641 uint8_t au8[8];
2642 /** 16-bit view. */
2643 uint16_t au16[4];
2644 /** 32-bit view. */
2645 uint32_t au32[2];
2646 /** 64-bit view. */
2647 uint64_t au64[1];
2648 /** Unsigned integer, depending on compiler context.
2649 * This generally follows ARCH_BITS. */
2650 RTCCUINTREG uCcReg;
2651 /** Extended unsigned integer, depending on compiler context.
2652 * This is 32-bit in 16-bit and 32-bit compiler contexts, and 64-bit in
2653 * 64-bit. */
2654 RTCCUINTXREG uCcXReg;
2655} BS3REG;
2656/** Pointer to an integer register. */
2657typedef BS3REG BS3_FAR *PBS3REG;
2658/** Pointer to a const integer register. */
2659typedef BS3REG const BS3_FAR *PCBS3REG;
2660
2661/**
2662 * Register context (without FPU).
2663 */
2664typedef struct BS3REGCTX
2665{
2666 BS3REG rax; /**< 0x00 */
2667 BS3REG rcx; /**< 0x08 */
2668 BS3REG rdx; /**< 0x10 */
2669 BS3REG rbx; /**< 0x18 */
2670 BS3REG rsp; /**< 0x20 */
2671 BS3REG rbp; /**< 0x28 */
2672 BS3REG rsi; /**< 0x30 */
2673 BS3REG rdi; /**< 0x38 */
2674 BS3REG r8; /**< 0x40 */
2675 BS3REG r9; /**< 0x48 */
2676 BS3REG r10; /**< 0x50 */
2677 BS3REG r11; /**< 0x58 */
2678 BS3REG r12; /**< 0x60 */
2679 BS3REG r13; /**< 0x68 */
2680 BS3REG r14; /**< 0x70 */
2681 BS3REG r15; /**< 0x78 */
2682 BS3REG rflags; /**< 0x80 */
2683 BS3REG rip; /**< 0x88 */
2684 uint16_t cs; /**< 0x90 */
2685 uint16_t ds; /**< 0x92 */
2686 uint16_t es; /**< 0x94 */
2687 uint16_t fs; /**< 0x96 */
2688 uint16_t gs; /**< 0x98 */
2689 uint16_t ss; /**< 0x9a */
2690 uint16_t tr; /**< 0x9c */
2691 uint16_t ldtr; /**< 0x9e */
2692 uint8_t bMode; /**< 0xa0: BS3_MODE_XXX. */
2693 uint8_t bCpl; /**< 0xa1: 0-3, 0 is used for real mode. */
2694 uint8_t fbFlags; /**< 0xa2: BS3REG_CTX_F_XXX */
2695 uint8_t abPadding[3]; /**< 0xa3 */
2696 uint16_t cr2Range; /**< 0xa6 */
2697 BS3REG cr0; /**< 0xa8 */
2698 BS3REG cr2; /**< 0xb0 */
2699 BS3REG cr3; /**< 0xb8 */
2700 BS3REG cr4; /**< 0xc0 */
2701 uint64_t uUnused; /**< 0xc8 */
2702} BS3REGCTX;
2703AssertCompileSize(BS3REGCTX, 0xd0);
2704/** Pointer to a register context. */
2705typedef BS3REGCTX BS3_FAR *PBS3REGCTX;
2706/** Pointer to a const register context. */
2707typedef BS3REGCTX const BS3_FAR *PCBS3REGCTX;
2708
2709/** @name BS3REG_CTX_F_XXX - BS3REGCTX::fbFlags masks.
2710 * @{ */
2711/** The CR0 is MSW (only low 16-bit). */
2712#define BS3REG_CTX_F_NO_CR0_IS_MSW UINT8_C(0x01)
2713/** No CR2 and CR3 values. Not in CPL 0 or CPU too old for CR2 & CR3. */
2714#define BS3REG_CTX_F_NO_CR2_CR3 UINT8_C(0x02)
2715/** No CR4 value. The CPU is too old for CR4. */
2716#define BS3REG_CTX_F_NO_CR4 UINT8_C(0x04)
2717/** No TR and LDTR values. Context gathered in real mode or v8086 mode. */
2718#define BS3REG_CTX_F_NO_TR_LDTR UINT8_C(0x08)
2719/** The context doesn't have valid values for AMD64 GPR extensions. */
2720#define BS3REG_CTX_F_NO_AMD64 UINT8_C(0x10)
2721/** @} */
2722
2723/**
2724 * Saves the current register context.
2725 *
2726 * @param pRegCtx Where to store the register context.
2727 */
2728BS3_CMN_PROTO_NOSB(void, Bs3RegCtxSave,(PBS3REGCTX pRegCtx));
2729
2730/**
2731 * Switch to the specified CPU bitcount, reserve additional stack and save the
2732 * CPU context.
2733 *
2734 * This is for writing more flexible test drivers that can test more than the
2735 * CPU bitcount (16-bit, 32-bit, 64-bit, and virtual 8086) of the driver itself.
2736 * For instance a 32-bit driver can do V86 and 16-bit testing, thus saving space
2737 * by avoiding duplicate 16-bit driver code.
2738 *
2739 * @param pRegCtx Where to store the register context.
2740 * @param bBitMode Bit mode to switch to, BS3_MODE_CODE_XXX. Only
2741 * BS3_MODE_CODE_MASK is used, other bits are ignored
2742 * to make it possible to pass a full mode value.
2743 * @param cbExtraStack Number of bytes of additional stack to allocate.
2744 */
2745BS3_CMN_PROTO_FARSTUB(8, void, Bs3RegCtxSaveEx,(PBS3REGCTX pRegCtx, uint8_t bBitMode, uint16_t cbExtraStack));
2746
2747/**
2748 * This is Bs3RegCtxSaveEx with automatic Bs3RegCtxConvertV86ToRm thrown in.
2749 *
2750 * This is for simplifying writing 32-bit test drivers that covers real-mode as
2751 * well as virtual 8086, 16-bit, 32-bit, and 64-bit modes.
2752 *
2753 * @param pRegCtx Where to store the register context.
2754 * @param bMode The mode to get a context for. If this isn't
2755 * BS3_MODE_RM, the BS3_MODE_SYS_MASK has to match the
2756 * one of the current mode.
2757 * @param cbExtraStack Number of bytes of additional stack to allocate.
2758 */
2759BS3_CMN_PROTO_STUB(void, Bs3RegCtxSaveForMode,(PBS3REGCTX pRegCtx, uint8_t bMode, uint16_t cbExtraStack));
2760
2761/**
2762 * Transforms a register context to a different ring.
2763 *
2764 * @param pRegCtx The register context.
2765 * @param bRing The target ring (0..3).
2766 *
2767 * @note Do _NOT_ call this for creating real mode or v8086 contexts, because
2768 * it will always output a protected mode context!
2769 */
2770BS3_CMN_PROTO_STUB(void, Bs3RegCtxConvertToRingX,(PBS3REGCTX pRegCtx, uint8_t bRing));
2771
2772/**
2773 * Transforms a V8086 register context to a real mode one.
2774 *
2775 * @param pRegCtx The register context.
2776 *
2777 * @note Will assert if called on a non-V8086 context.
2778 */
2779BS3_CMN_PROTO_STUB(void, Bs3RegCtxConvertV86ToRm,(PBS3REGCTX pRegCtx));
2780
2781/**
2782 * Restores a register context.
2783 *
2784 * @param pRegCtx The register context to be restored and resumed.
2785 * @param fFlags BS3REGCTXRESTORE_F_XXX.
2786 *
2787 * @remarks Will switch to ring-0.
2788 * @remarks Does not return.
2789 */
2790BS3_CMN_PROTO_NOSB(DECL_NO_RETURN(void), Bs3RegCtxRestore,(PCBS3REGCTX pRegCtx, uint16_t fFlags));
2791#if !defined(BS3_KIT_WITH_NO_RETURN) && defined(__WATCOMC__)
2792# pragma aux Bs3RegCtxRestore_c16 "_Bs3RegCtxRestore_aborts_c16" __aborts
2793# pragma aux Bs3RegCtxRestore_f16 "_Bs3RegCtxRestore_aborts_f16" __aborts
2794# pragma aux Bs3RegCtxRestore_c32 "_Bs3RegCtxRestore_aborts_c32" __aborts
2795#endif
2796
2797/** @name Flags for Bs3RegCtxRestore
2798 * @{ */
2799/** Skip restoring the CRx registers. */
2800#define BS3REGCTXRESTORE_F_SKIP_CRX UINT16_C(0x0001)
2801/** Sets g_fBs3TrapNoV86Assist. */
2802#define BS3REGCTXRESTORE_F_NO_V86_ASSIST UINT16_C(0x0002)
2803/** @} */
2804
2805/**
2806 * Prints the register context.
2807 *
2808 * @param pRegCtx The register context to be printed.
2809 */
2810BS3_CMN_PROTO_STUB(void, Bs3RegCtxPrint,(PCBS3REGCTX pRegCtx));
2811
2812/**
2813 * Sets a GPR and segment register to point at the same location as @a uFlat.
2814 *
2815 * @param pRegCtx The register context.
2816 * @param pGpr The general purpose register to set (points within
2817 * @a pRegCtx).
2818 * @param pSel The selector register (points within @a pRegCtx).
2819 * @param uFlat Flat location address.
2820 */
2821BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpSegFromFlat,(PBS3REGCTX pRegCtx, PBS3REG pGpr, PRTSEL pSel, RTCCUINTXREG uFlat));
2822
2823/**
2824 * Sets a GPR and segment register to point at the same location as @a ovPtr.
2825 *
2826 * @param pRegCtx The register context.
2827 * @param pGpr The general purpose register to set (points within
2828 * @a pRegCtx).
2829 * @param pSel The selector register (points within @a pRegCtx).
2830 * @param pvPtr Current context pointer.
2831 */
2832BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpSegFromCurPtr,(PBS3REGCTX pRegCtx, PBS3REG pGpr, PRTSEL pSel, void BS3_FAR *pvPtr));
2833
2834/**
2835 * Sets a GPR and DS to point at the same location as @a pvPtr.
2836 *
2837 * @param pRegCtx The register context.
2838 * @param pGpr The general purpose register to set (points within
2839 * @a pRegCtx).
2840 * @param pvPtr Current context pointer.
2841 */
2842BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetGrpDsFromCurPtr,(PBS3REGCTX pRegCtx, PBS3REG pGpr, void BS3_FAR *pvPtr));
2843
2844/**
2845 * Sets CS:RIP to point at the same piece of code as @a uFlatCode.
2846 *
2847 * @param pRegCtx The register context.
2848 * @param uFlatCode Flat code pointer
2849 * @sa Bs3RegCtxSetRipCsFromLnkPtr, Bs3RegCtxSetRipCsFromCurPtr
2850 */
2851BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromFlat,(PBS3REGCTX pRegCtx, RTCCUINTXREG uFlatCode));
2852
2853/**
2854 * Sets CS:RIP to point at the same piece of code as @a pfnCode.
2855 *
2856 * The 16-bit edition of this function expects a far 16:16 address as written by
2857 * the linker (i.e. real mode).
2858 *
2859 * @param pRegCtx The register context.
2860 * @param pfnCode Pointer to the code. In 32-bit and 64-bit mode this is a
2861 * flat address, while in 16-bit it's a far 16:16 address
2862 * as fixed up by the linker (real mode selector). This
2863 * address is converted to match the mode of the context.
2864 * @sa Bs3RegCtxSetRipCsFromCurPtr, Bs3RegCtxSetRipCsFromFlat
2865 */
2866BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromLnkPtr,(PBS3REGCTX pRegCtx, FPFNBS3FAR pfnCode));
2867
2868/**
2869 * Sets CS:RIP to point at the same piece of code as @a pfnCode.
2870 *
2871 * @param pRegCtx The register context.
2872 * @param pfnCode Pointer to the code. Current mode pointer.
2873 * @sa Bs3RegCtxSetRipCsFromLnkPtr, Bs3RegCtxSetRipCsFromFlat
2874 */
2875BS3_CMN_PROTO_STUB(void, Bs3RegCtxSetRipCsFromCurPtr,(PBS3REGCTX pRegCtx, FPFNBS3FAR pfnCode));
2876
2877/**
2878 * Sets a GPR by number.
2879 *
2880 * @return true if @a iGpr is valid, false if not.
2881 * @param pRegCtx The register context.
2882 * @param iGpr The GPR number.
2883 * @param uValue The new value.
2884 * @param cbValue The size of the value: 1, 2, 4 or 8.
2885 */
2886BS3_CMN_PROTO_STUB(bool, Bs3RegCtxSetGpr,(PBS3REGCTX pRegCtx, uint8_t iGpr, uint64_t uValue, uint8_t cb));
2887
2888/**
2889 * Gets the stack pointer as a current context pointer.
2890 *
2891 * @return Pointer to the top of the stack. NULL on failure.
2892 * @param pRegCtx The register context.
2893 */
2894BS3_CMN_PROTO_STUB(void BS3_FAR *, Bs3RegCtxGetRspSsAsCurPtr,(PBS3REGCTX pRegCtx));
2895
2896
2897/**
2898 * The method to be used to save and restore the extended context.
2899 */
2900typedef enum BS3EXTCTXMETHOD
2901{
2902 BS3EXTCTXMETHOD_INVALID = 0,
2903 BS3EXTCTXMETHOD_ANCIENT, /**< Ancient fnsave/frstor format. */
2904 BS3EXTCTXMETHOD_FXSAVE, /**< fxsave/fxrstor format. */
2905 BS3EXTCTXMETHOD_XSAVE, /**< xsave/xrstor format. */
2906 BS3EXTCTXMETHOD_END,
2907} BS3EXTCTXMETHOD;
2908
2909
2910/**
2911 * Extended CPU context (FPU, SSE, AVX, ++).
2912 *
2913 * @remarks Also in bs3kit.inc
2914 */
2915typedef struct BS3EXTCTX
2916{
2917 /** Dummy/magic value. */
2918 uint16_t u16Magic;
2919 /** The size of the structure. */
2920 uint16_t cb;
2921 /** The method used to save and restore the context (BS3EXTCTXMETHOD). */
2922 uint8_t enmMethod;
2923 uint8_t abPadding0[3];
2924 /** Nominal XSAVE_C_XXX. */
2925 uint64_t fXcr0Nominal;
2926 /** The saved XCR0 mask (restored after xrstor). */
2927 uint64_t fXcr0Saved;
2928
2929 /** Explicit alignment padding. */
2930 uint8_t abPadding[64 - 2 - 2 - 1 - 3 - 8 - 8];
2931
2932 /** The context, variable size (see above).
2933 * This must be aligned on a 64 byte boundrary. */
2934 union
2935 {
2936 /** fnsave/frstor. */
2937 X86FPUSTATE Ancient;
2938 /** fxsave/fxrstor */
2939 X86FXSTATE x87;
2940 /** xsave/xrstor */
2941 X86XSAVEAREA x;
2942 /** Byte array view. */
2943 uint8_t ab[sizeof(X86XSAVEAREA)];
2944 } Ctx;
2945} BS3EXTCTX;
2946AssertCompileMemberAlignment(BS3EXTCTX, Ctx, 64);
2947/** Pointer to an extended CPU context. */
2948typedef BS3EXTCTX BS3_FAR *PBS3EXTCTX;
2949/** Pointer to a const extended CPU context. */
2950typedef BS3EXTCTX const BS3_FAR *PCBS3EXTCTX;
2951
2952/** Magic value for BS3EXTCTX. */
2953#define BS3EXTCTX_MAGIC UINT16_C(0x1980)
2954
2955/**
2956 * Allocates and initializes the extended CPU context structure.
2957 *
2958 * @returns The new extended CPU context structure.
2959 * @param enmKind The kind of allocation to make.
2960 */
2961BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxAlloc,(BS3MEMKIND enmKind));
2962
2963/**
2964 * Frees an extended CPU context structure.
2965 *
2966 * @param pExtCtx The extended CPU context (returned by
2967 * Bs3ExtCtxAlloc).
2968 */
2969BS3_CMN_PROTO_STUB(void, Bs3ExtCtxFree,(PBS3EXTCTX pExtCtx));
2970
2971/**
2972 * Get the size required for a BS3EXTCTX structure.
2973 *
2974 * @returns size in bytes of the whole structure.
2975 * @param pfFlags Where to return flags for Bs3ExtCtxInit.
2976 * @note Use Bs3ExtCtxAlloc when possible.
2977 */
2978BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetSize,(uint64_t *pfFlags));
2979
2980/**
2981 * Initializes the extended CPU context structure.
2982 * @returns pExtCtx
2983 * @param pExtCtx The extended CPU context.
2984 * @param cbExtCtx The size of the @a pExtCtx allocation.
2985 * @param fFlags XSAVE_C_XXX flags.
2986 */
2987BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxInit,(PBS3EXTCTX pExtCtx, uint16_t cbExtCtx, uint64_t fFlags));
2988
2989/**
2990 * Saves the extended CPU state to the given structure.
2991 *
2992 * @param pExtCtx The extended CPU context.
2993 * @remarks All GPRs preserved.
2994 */
2995BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxSave,(PBS3EXTCTX pExtCtx));
2996
2997/**
2998 * Saves the extended CPU state to the given structure, when in long mode this
2999 * is done from 64-bit mode to capture YMM8 thru YMM15.
3000 *
3001 * This is for testing 64-bit code from a 32-bit test driver.
3002 *
3003 * @param pExtCtx The extended CPU context.
3004 * @note Only safe to call from ring-0 at present.
3005 * @remarks All GPRs preserved.
3006 * @sa Bs3ExtCtxRestoreEx
3007 */
3008BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxSaveEx,(PBS3EXTCTX pExtCtx));
3009
3010/**
3011 * Restores the extended CPU state from the given structure.
3012 *
3013 * @param pExtCtx The extended CPU context.
3014 * @remarks All GPRs preserved.
3015 */
3016BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxRestore,(PCBS3EXTCTX pExtCtx));
3017
3018/**
3019 * Restores the extended CPU state from the given structure and in long mode
3020 * switch to 64-bit mode to do this so YMM8-YMM15 are also loaded.
3021 *
3022 * This is for testing 64-bit code from a 32-bit test driver.
3023 *
3024 * @param pExtCtx The extended CPU context.
3025 * @note Only safe to call from ring-0 at present.
3026 * @remarks All GPRs preserved.
3027 * @sa Bs3ExtCtxSaveEx
3028 */
3029BS3_CMN_PROTO_FARSTUB(4, void, Bs3ExtCtxRestoreEx,(PCBS3EXTCTX pExtCtx));
3030
3031/**
3032 * Copies the state from one context to another.
3033 *
3034 * @returns pDst
3035 * @param pDst The destination extended CPU context.
3036 * @param pSrc The source extended CPU context.
3037 */
3038BS3_CMN_PROTO_STUB(PBS3EXTCTX, Bs3ExtCtxCopy,(PBS3EXTCTX pDst, PCBS3EXTCTX pSrc));
3039
3040/**
3041 * Gets the FCW register value from @a pExtCtx.
3042 *
3043 * @returns FCW value.
3044 * @param pExtCtx The extended CPU context.
3045 */
3046BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetFcw,(PCBS3EXTCTX pExtCtx));
3047
3048/**
3049 * Sets the FCW register value in @a pExtCtx.
3050 *
3051 * @param pExtCtx The extended CPU context.
3052 * @param uValue The new FCW value.
3053 */
3054BS3_CMN_PROTO_STUB(void, Bs3ExtCtxSetFcw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3055
3056/**
3057 * Gets the FSW register value from @a pExtCtx.
3058 *
3059 * @returns FSW value.
3060 * @param pExtCtx The extended CPU context.
3061 */
3062BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetFsw,(PCBS3EXTCTX pExtCtx));
3063
3064/**
3065 * Sets the FSW register value in @a pExtCtx.
3066 *
3067 * @param pExtCtx The extended CPU context.
3068 * @param uValue The new FSW value.
3069 */
3070BS3_CMN_PROTO_STUB(void, Bs3ExtCtxSetFsw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3071
3072/**
3073 * Gets the abridged FTW register value from @a pExtCtx.
3074 *
3075 * @returns FTW value.
3076 * @param pExtCtx The extended CPU context.
3077 */
3078BS3_CMN_PROTO_STUB(uint16_t, Bs3ExtCtxGetAbridgedFtw,(PCBS3EXTCTX pExtCtx));
3079
3080/**
3081 * Sets the abridged FTW register value in @a pExtCtx.
3082 *
3083 * Currently this requires that the state stores teh abridged FTW, no conversion
3084 * to the two-bit variant will be attempted.
3085 *
3086 * @returns true if set successfully, false if not.
3087 * @param pExtCtx The extended CPU context.
3088 * @param uValue The new FTW value.
3089 */
3090BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetAbridgedFtw,(PBS3EXTCTX pExtCtx, uint16_t uValue));
3091
3092/**
3093 * Gets the MXCSR register value from @a pExtCtx.
3094 *
3095 * @returns MXCSR value, 0 if not part of context.
3096 * @param pExtCtx The extended CPU context.
3097 */
3098BS3_CMN_PROTO_STUB(uint32_t, Bs3ExtCtxGetMxCsr,(PCBS3EXTCTX pExtCtx));
3099
3100/**
3101 * Sets the MXCSR register value in @a pExtCtx.
3102 *
3103 * @returns true if set, false if not supported by the format.
3104 * @param pExtCtx The extended CPU context.
3105 * @param uValue The new MXCSR value.
3106 */
3107BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMxCsr,(PBS3EXTCTX pExtCtx, uint32_t uValue));
3108
3109/**
3110 * Gets the MXCSR MASK value from @a pExtCtx.
3111 *
3112 * @returns MXCSR MASK value, 0 if not part of context.
3113 * @param pExtCtx The extended CPU context.
3114 */
3115BS3_CMN_PROTO_STUB(uint32_t, Bs3ExtCtxGetMxCsrMask,(PCBS3EXTCTX pExtCtx));
3116
3117/**
3118 * Sets the MXCSR MASK value in @a pExtCtx.
3119 *
3120 * @returns true if set, false if not supported by the format.
3121 * @param pExtCtx The extended CPU context.
3122 * @param uValue The new MXCSR MASK value.
3123 */
3124BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMxCsrMask,(PBS3EXTCTX pExtCtx, uint32_t uValue));
3125
3126/**
3127 * Gets the value of MM register number @a iReg from @a pExtCtx.
3128 *
3129 * @returns The MM register value.
3130 * @param pExtCtx The extended CPU context.
3131 * @param iReg The register to get (0 thru 7).
3132 */
3133BS3_CMN_PROTO_STUB(uint64_t, Bs3ExtCtxGetMm,(PCBS3EXTCTX pExtCtx, uint8_t iReg));
3134
3135/** What to do about the 16-bit above the MM QWORD. */
3136typedef enum BS3EXTCTXTOPMM
3137{
3138 /** Invalid zero value. */
3139 BS3EXTCTXTOPMM_INVALID = 0,
3140 /** Set to 0FFFFh like real CPUs typically does when updating an MM register. */
3141 BS3EXTCTXTOPMM_SET,
3142 /** Set to zero. */
3143 BS3EXTCTXTOPMM_ZERO,
3144 /** Don't change the value, leaving it as-is. */
3145 BS3EXTCTXTOPMM_AS_IS,
3146 /** End of valid values. */
3147 BS3EXTCTXTOPMM_END
3148} BS3EXTCTXTOPMM;
3149
3150/**
3151 * Sets the value of YMM register number @a iReg in @a pExtCtx to @a pValue.
3152 *
3153 * @returns True if set, false if not.
3154 * @param pExtCtx The extended CPU context.
3155 * @param iReg The register to set.
3156 * @param uValue The new register value.
3157 * @param enmTop What to do about the 16-bit value above the MM
3158 * QWord.
3159 */
3160BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetMm,(PBS3EXTCTX pExtCtx, uint8_t iReg, uint64_t uValue, BS3EXTCTXTOPMM enmTop));
3161
3162/**
3163 * Gets the value of XMM register number @a iReg from @a pExtCtx.
3164 *
3165 * @returns pValue
3166 * @param pExtCtx The extended CPU context.
3167 * @param iReg The register to get.
3168 * @param pValue Where to return the value. Zeroed if the state
3169 * doesn't support SSE or if @a iReg is invalid.
3170 */
3171BS3_CMN_PROTO_STUB(PRTUINT128U, Bs3ExtCtxGetXmm,(PCBS3EXTCTX pExtCtx, uint8_t iReg, PRTUINT128U pValue));
3172
3173/**
3174 * Sets the value of XMM register number @a iReg in @a pExtCtx to @a pValue.
3175 *
3176 * @returns True if set, false if not set (not supported by state format or
3177 * invalid iReg).
3178 * @param pExtCtx The extended CPU context.
3179 * @param iReg The register to set.
3180 * @param pValue The new register value.
3181 */
3182BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetXmm,(PBS3EXTCTX pExtCtx, uint8_t iReg, PCRTUINT128U pValue));
3183
3184/**
3185 * Gets the value of YMM register number @a iReg from @a pExtCtx.
3186 *
3187 * @returns pValue
3188 * @param pExtCtx The extended CPU context.
3189 * @param iReg The register to get.
3190 * @param pValue Where to return the value. Parts not in the
3191 * extended state are zeroed. For absent or invalid
3192 * @a iReg values this is set to zero.
3193 */
3194BS3_CMN_PROTO_STUB(PRTUINT256U, Bs3ExtCtxGetYmm,(PCBS3EXTCTX pExtCtx, uint8_t iReg, PRTUINT256U pValue));
3195
3196/**
3197 * Sets the value of YMM register number @a iReg in @a pExtCtx to @a pValue.
3198 *
3199 * @returns true if set (even if only partially). False if not set (not
3200 * supported by state format, unsupported/invalid iReg).
3201 * @param pExtCtx The extended CPU context.
3202 * @param iReg The register to set.
3203 * @param pValue The new register value.
3204 * @param cbValue Number of bytes to take from @a pValue, either 16 or
3205 * 32. If 16, the high part will be zeroed when present
3206 * in the state.
3207 */
3208BS3_CMN_PROTO_STUB(bool, Bs3ExtCtxSetYmm,(PBS3EXTCTX pExtCtx, uint8_t iReg, PCRTUINT256U pValue, uint8_t cbValue));
3209
3210
3211/** @name Debug register accessors for V8086 mode (works everwhere).
3212 * @{ */
3213BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr0,(void));
3214BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr1,(void));
3215BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr2,(void));
3216BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr3,(void));
3217BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr6,(void));
3218BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDr7,(void));
3219
3220BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr0,(RTCCUINTXREG uValue));
3221BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr1,(RTCCUINTXREG uValue));
3222BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr2,(RTCCUINTXREG uValue));
3223BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr3,(RTCCUINTXREG uValue));
3224BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr6,(RTCCUINTXREG uValue));
3225BS3_CMN_PROTO_NOSB(void, Bs3RegSetDr7,(RTCCUINTXREG uValue));
3226
3227BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetDrX,(uint8_t iReg));
3228BS3_CMN_PROTO_NOSB(void, Bs3RegSetDrX,(uint8_t iReg, RTCCUINTXREG uValue));
3229/** @} */
3230
3231
3232/** @name Control register accessors for V8086 mode (works everwhere).
3233 * @{ */
3234BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr0,(void));
3235BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr2,(void));
3236BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr3,(void));
3237BS3_CMN_PROTO_NOSB(RTCCUINTXREG, Bs3RegGetCr4,(void));
3238BS3_CMN_PROTO_NOSB(uint16_t, Bs3RegGetTr,(void));
3239BS3_CMN_PROTO_NOSB(uint16_t, Bs3RegGetLdtr,(void));
3240BS3_CMN_PROTO_NOSB(uint64_t, Bs3RegGetXcr0,(void));
3241
3242BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr0,(RTCCUINTXREG uValue));
3243BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr2,(RTCCUINTXREG uValue));
3244BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr3,(RTCCUINTXREG uValue));
3245BS3_CMN_PROTO_NOSB(void, Bs3RegSetCr4,(RTCCUINTXREG uValue));
3246BS3_CMN_PROTO_NOSB(void, Bs3RegSetTr,(uint16_t uValue));
3247BS3_CMN_PROTO_NOSB(void, Bs3RegSetLdtr,(uint16_t uValue));
3248BS3_CMN_PROTO_NOSB(void, Bs3RegSetXcr0,(uint64_t uValue));
3249/** @} */
3250
3251
3252/**
3253 * Trap frame.
3254 */
3255typedef struct BS3TRAPFRAME
3256{
3257 /** 0x00: Exception/interrupt number. */
3258 uint8_t bXcpt;
3259 /** 0x01: The size of the IRET frame. */
3260 uint8_t cbIretFrame;
3261 /** 0x02: The handler CS. */
3262 uint16_t uHandlerCs;
3263 /** 0x04: The handler SS. */
3264 uint16_t uHandlerSs;
3265 /** 0x06: Explicit alignment. */
3266 uint16_t usAlignment;
3267 /** 0x08: The handler RSP (pointer to the iret frame, skipping ErrCd). */
3268 uint64_t uHandlerRsp;
3269 /** 0x10: The handler RFLAGS value. */
3270 uint64_t fHandlerRfl;
3271 /** 0x18: The error code (if applicable). */
3272 uint64_t uErrCd;
3273 /** 0x20: The register context. */
3274 BS3REGCTX Ctx;
3275} BS3TRAPFRAME;
3276AssertCompileSize(BS3TRAPFRAME, 0x20 + 0xd0);
3277/** Pointer to a trap frame. */
3278typedef BS3TRAPFRAME BS3_FAR *PBS3TRAPFRAME;
3279/** Pointer to a const trap frame. */
3280typedef BS3TRAPFRAME const BS3_FAR *PCBS3TRAPFRAME;
3281
3282
3283/**
3284 * Re-initializes the trap handling for the current mode.
3285 *
3286 * Useful after a test that messes with the IDT/IVT.
3287 *
3288 * @sa Bs3TrapInit
3289 */
3290BS3_CMN_PROTO_STUB(void, Bs3TrapReInit,(void));
3291
3292/**
3293 * Initializes real mode and v8086 trap handling.
3294 *
3295 * @remarks Does not install RM/V86 trap handling, just initializes the
3296 * structures.
3297 */
3298BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86Init,(void));
3299
3300/**
3301 * Initializes real mode and v8086 trap handling, extended version.
3302 *
3303 * @param f386Plus Set if the CPU is 80386 or later and
3304 * extended registers should be saved. Once initialized
3305 * with this parameter set to @a true, the effect cannot be
3306 * reversed.
3307 *
3308 * @remarks Does not install RM/V86 trap handling, just initializes the
3309 * structures.
3310 */
3311BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86InitEx,(bool f386Plus));
3312
3313/**
3314 * Initializes 16-bit (protected mode) trap handling.
3315 *
3316 * @remarks Does not install 16-bit trap handling, just initializes the
3317 * structures.
3318 */
3319BS3_CMN_PROTO_STUB(void, Bs3Trap16Init,(void));
3320
3321/**
3322 * Initializes 16-bit (protected mode) trap handling, extended version.
3323 *
3324 * @param f386Plus Set if the CPU is 80386 or later and
3325 * extended registers should be saved. Once initialized
3326 * with this parameter set to @a true, the effect cannot be
3327 * reversed.
3328 *
3329 * @remarks Does not install 16-bit trap handling, just initializes the
3330 * structures.
3331 */
3332BS3_CMN_PROTO_STUB(void, Bs3Trap16InitEx,(bool f386Plus));
3333
3334/**
3335 * Initializes 32-bit trap handling.
3336 *
3337 * @remarks Does not install 32-bit trap handling, just initializes the
3338 * structures.
3339 */
3340BS3_CMN_PROTO_STUB(void, Bs3Trap32Init,(void));
3341
3342/**
3343 * Initializes 64-bit trap handling
3344 *
3345 * @remarks Does not install 64-bit trap handling, just initializes the
3346 * structures.
3347 */
3348BS3_CMN_PROTO_STUB(void, Bs3Trap64Init,(void));
3349
3350/**
3351 * Initializes 64-bit trap handling, extended version.
3352 *
3353 * @remarks Does not install 64-bit trap handling, just initializes the
3354 * structures.
3355 * @param fMoreIstUsage Use the interrupt stacks for more CPU exceptions.
3356 * Default (false) is to only IST1 for the double fault
3357 * handler and the rest uses IST0.
3358 */
3359BS3_CMN_PROTO_STUB(void, Bs3Trap64InitEx,(bool fMoreIstUsage));
3360
3361/**
3362 * Modifies the real-mode / V86 IVT entry specified by @a iIvt.
3363 *
3364 * @param iIvt The index of the IDT entry to set.
3365 * @param uSeg The handler real-mode segment.
3366 * @param off The handler offset.
3367 */
3368BS3_CMN_PROTO_STUB(void, Bs3TrapRmV86SetGate,(uint8_t iIvt, uint16_t uSeg, uint16_t off));
3369
3370/**
3371 * Modifies the 16-bit IDT entry (protected mode) specified by @a iIdt.
3372 *
3373 * @param iIdt The index of the IDT entry to set.
3374 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3375 * @param bDpl The DPL.
3376 * @param uSel The handler selector.
3377 * @param off The handler offset (if applicable).
3378 * @param cParams The parameter count (for call gates).
3379 */
3380BS3_CMN_PROTO_STUB(void, Bs3Trap16SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl,
3381 uint16_t uSel, uint16_t off, uint8_t cParams));
3382
3383/** The address of Bs3Trap16GenericEntries.
3384 * Bs3Trap16GenericEntries is an array of interrupt/trap/whatever entry
3385 * points, 8 bytes each, that will create a register frame and call the generic
3386 * C compatible trap handlers. */
3387extern uint32_t g_Bs3Trap16GenericEntriesFlatAddr;
3388
3389/**
3390 * Modifies the 32-bit IDT entry specified by @a iIdt.
3391 *
3392 * @param iIdt The index of the IDT entry to set.
3393 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3394 * @param bDpl The DPL.
3395 * @param uSel The handler selector.
3396 * @param off The handler offset (if applicable).
3397 * @param cParams The parameter count (for call gates).
3398 */
3399BS3_CMN_PROTO_STUB(void, Bs3Trap32SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl,
3400 uint16_t uSel, uint32_t off, uint8_t cParams));
3401
3402/** The address of Bs3Trap32GenericEntries.
3403 * Bs3Trap32GenericEntries is an array of interrupt/trap/whatever entry
3404 * points, 10 bytes each, that will create a register frame and call the generic
3405 * C compatible trap handlers. */
3406extern uint32_t g_Bs3Trap32GenericEntriesFlatAddr;
3407
3408/**
3409 * Modifies the 64-bit IDT entry specified by @a iIdt.
3410 *
3411 * @param iIdt The index of the IDT entry to set.
3412 * @param bType The gate type (X86_SEL_TYPE_SYS_XXX).
3413 * @param bDpl The DPL.
3414 * @param uSel The handler selector.
3415 * @param off The handler offset (if applicable).
3416 * @param bIst The interrupt stack to use.
3417 */
3418BS3_CMN_PROTO_STUB(void, Bs3Trap64SetGate,(uint8_t iIdt, uint8_t bType, uint8_t bDpl, uint16_t uSel, uint64_t off, uint8_t bIst));
3419
3420/** The address of Bs3Trap64GenericEntries.
3421 * Bs3Trap64GenericEntries is an array of interrupt/trap/whatever entry
3422 * points, 8 bytes each, that will create a register frame and call the generic
3423 * C compatible trap handlers. */
3424extern uint32_t g_Bs3Trap64GenericEntriesFlatAddr;
3425
3426/**
3427 * Adjusts the DPL the IDT entry specified by @a iIdt.
3428 *
3429 * The change is applied to the 16-bit, 32-bit and 64-bit IDTs.
3430 *
3431 * @returns Old DPL (from 64-bit IDT).
3432 * @param iIdt The index of the IDT and IVT entry to set.
3433 * @param bDpl The DPL.
3434 */
3435BS3_CMN_PROTO_STUB(uint8_t, Bs3TrapSetDpl,(uint8_t iIdt, uint8_t bDpl));
3436
3437/**
3438 * C-style trap handler.
3439 *
3440 * The caller will resume the context in @a pTrapFrame upon return.
3441 *
3442 * @param pTrapFrame The trap frame. Registers can be modified.
3443 * @note The 16-bit versions must be in CGROUP16!
3444 */
3445typedef BS3_DECL_NEAR_CALLBACK(void) FNBS3TRAPHANDLER(PBS3TRAPFRAME pTrapFrame);
3446/** Pointer to a trap handler (current template context). */
3447typedef FNBS3TRAPHANDLER *PFNBS3TRAPHANDLER;
3448
3449#if ARCH_BITS == 16
3450/** @copydoc FNBS3TRAPHANDLER */
3451typedef FNBS3FAR FNBS3TRAPHANDLER32;
3452/** @copydoc FNBS3TRAPHANDLER */
3453typedef FNBS3FAR FNBS3TRAPHANDLER64;
3454#else
3455/** @copydoc FNBS3TRAPHANDLER */
3456typedef FNBS3TRAPHANDLER FNBS3TRAPHANDLER32;
3457/** @copydoc FNBS3TRAPHANDLER */
3458typedef FNBS3TRAPHANDLER FNBS3TRAPHANDLER64;
3459#endif
3460/** @copydoc PFNBS3TRAPHANDLER */
3461typedef FNBS3TRAPHANDLER32 *PFNBS3TRAPHANDLER32;
3462/** @copydoc PFNBS3TRAPHANDLER */
3463typedef FNBS3TRAPHANDLER64 *PFNBS3TRAPHANDLER64;
3464
3465
3466/**
3467 * C-style trap handler, near 16-bit (CGROUP16).
3468 *
3469 * The caller will resume the context in @a pTrapFrame upon return.
3470 *
3471 * @param pTrapFrame The trap frame. Registers can be modified.
3472 */
3473typedef BS3_DECL_NEAR_CALLBACK(void) FNBS3TRAPHANDLER16(PBS3TRAPFRAME pTrapFrame);
3474/** Pointer to a trap handler (current template context). */
3475typedef FNBS3TRAPHANDLER16 *PFNBS3TRAPHANDLER16;
3476
3477/**
3478 * C-style trap handler, near 16-bit (CGROUP16).
3479 *
3480 * The caller will resume the context in @a pTrapFrame upon return.
3481 *
3482 * @param pTrapFrame The trap frame. Registers can be modified.
3483 */
3484typedef BS3_DECL_CALLBACK(void) FNBS3TRAPHANDLER3264(PBS3TRAPFRAME pTrapFrame);
3485/** Pointer to a trap handler (current template context). */
3486typedef FNBS3TRAPHANDLER3264 *FPFNBS3TRAPHANDLER3264;
3487
3488
3489/**
3490 * Sets a trap handler (C/C++/assembly) for the current bitcount.
3491 *
3492 * @returns Previous handler.
3493 * @param iIdt The index of the IDT entry to set.
3494 * @param pfnHandler Pointer to the handler.
3495 * @sa Bs3TrapSetHandlerEx
3496 */
3497BS3_CMN_PROTO_STUB(PFNBS3TRAPHANDLER, Bs3TrapSetHandler,(uint8_t iIdt, PFNBS3TRAPHANDLER pfnHandler));
3498
3499/**
3500 * Sets a trap handler (C/C++/assembly) for all the bitcounts.
3501 *
3502 * @param iIdt The index of the IDT and IVT entry to set.
3503 * @param pfnHandler16 Pointer to the 16-bit handler. (Assumes linker addresses.)
3504 * @param pfnHandler32 Pointer to the 32-bit handler. (Assumes linker addresses.)
3505 * @param pfnHandler64 Pointer to the 64-bit handler. (Assumes linker addresses.)
3506 * @sa Bs3TrapSetHandler
3507 */
3508BS3_CMN_PROTO_STUB(void, Bs3TrapSetHandlerEx,(uint8_t iIdt, PFNBS3TRAPHANDLER16 pfnHandler16,
3509 PFNBS3TRAPHANDLER32 pfnHandler32, PFNBS3TRAPHANDLER64 pfnHandler64));
3510
3511/**
3512 * Default C/C++ trap handler.
3513 *
3514 * This will check trap record and panic if no match was found.
3515 *
3516 * @param pTrapFrame Trap frame of the trap to handle.
3517 */
3518BS3_CMN_PROTO_STUB(void, Bs3TrapDefaultHandler,(PBS3TRAPFRAME pTrapFrame));
3519
3520/**
3521 * Prints the trap frame (to screen).
3522 * @param pTrapFrame Trap frame to print.
3523 */
3524BS3_CMN_PROTO_STUB(void, Bs3TrapPrintFrame,(PCBS3TRAPFRAME pTrapFrame));
3525
3526/**
3527 * Sets up a long jump from a trap handler.
3528 *
3529 * The long jump will only be performed once, but will catch any kind of trap,
3530 * fault, interrupt or irq.
3531 *
3532 * @retval true on the initial call.
3533 * @retval false on trap return.
3534 * @param pTrapFrame Where to store the trap information when
3535 * returning @c false.
3536 * @sa #Bs3TrapUnsetJmp
3537 */
3538BS3_CMN_PROTO_NOSB(DECL_RETURNS_TWICE(bool),Bs3TrapSetJmp,(PBS3TRAPFRAME pTrapFrame));
3539
3540/**
3541 * Combination of #Bs3TrapSetJmp and #Bs3RegCtxRestore.
3542 *
3543 * @param pCtxRestore The context to restore.
3544 * @param pTrapFrame Where to store the trap information.
3545 */
3546BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestore,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3547
3548/**
3549 * Variation of Bs3TrapSetJmpAndRestore that includes
3550 * #Bs3TrapSetJmpAndRestoreInRm and calls is if pCtxRestore is a real mode
3551 * context and we're not in real mode.
3552 *
3553 * This is useful for 32-bit test drivers running via #Bs3TestDoModesByOne using
3554 * BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY to allow them to test real-mode too.
3555 *
3556 * @param pCtxRestore The context to restore.
3557 * @param pTrapFrame Where to store the trap information.
3558 */
3559BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithRm,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3560
3561/**
3562 * Combination of #Bs3ExtCtxRestoreEx, #Bs3TrapSetJmp, #Bs3RegCtxRestore and
3563 * #Bs3ExtCtxSaveEx.
3564 *
3565 * @param pCtxRestore The context to restore.
3566 * @param pExtCtxRestore The extended context to restore.
3567 * @param pTrapFrame Where to store the trap information.
3568 * @param pExtCtxTrap Where to store the extended context after the trap.
3569 * Note, the saving isn't done from the trap handler,
3570 * but after #Bs3TrapSetJmp returns zero (i.e. for the
3571 * 2nd time).
3572 */
3573BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithExtCtx,(PCBS3REGCTX pCtxRestore, PCBS3EXTCTX pExtCtxRestore,
3574 PBS3TRAPFRAME pTrapFrame, PBS3EXTCTX pExtCtxTrap));
3575
3576/**
3577 * Variation of Bs3TrapSetJmpAndRestoreWithExtCtx that includes
3578 * #Bs3TrapSetJmpAndRestoreInRm and calls is if pCtxRestore is a real mode
3579 * context and we're not in real mode.
3580 *
3581 * This is useful for 32-bit test drivers running via #Bs3TestDoModesByOne using
3582 * BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY to allow them to test real-mode too.
3583 *
3584 * @param pCtxRestore The context to restore.
3585 * @param pExtCtxRestore The extended context to restore.
3586 * @param pTrapFrame Where to store the trap information.
3587 * @param pExtCtxTrap Where to store the extended context after the trap.
3588 * Note, the saving isn't done from the trap handler,
3589 * but after #Bs3TrapSetJmp returns zero (i.e. for the
3590 * 2nd time).
3591 */
3592BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreWithExtCtxAndRm,(PCBS3REGCTX pCtxRestore, PCBS3EXTCTX pExtCtxRestore,
3593 PBS3TRAPFRAME pTrapFrame, PBS3EXTCTX pExtCtxTrap));
3594
3595/**
3596 * Combination of Bs3SwitchToRM, #Bs3TrapSetJmp and #Bs3RegCtxRestore.
3597 *
3598 * @param pCtxRestore The context to restore. Must be real-mode
3599 * addressable.
3600 * @param pTrapFrame Where to store the trap information. Must be
3601 * real-mode addressable.
3602 */
3603BS3_CMN_PROTO_STUB(void, Bs3TrapSetJmpAndRestoreInRm,(PCBS3REGCTX pCtxRestore, PBS3TRAPFRAME pTrapFrame));
3604
3605/**
3606 * Disables a previous #Bs3TrapSetJmp call.
3607 */
3608BS3_CMN_PROTO_STUB(void, Bs3TrapUnsetJmp,(void));
3609
3610/** Entry point for MSR_K8_LSTAR (64-bit).
3611 * This hooks into the default Bs3TrapSetJmp logic. */
3612BS3_CMN_PROTO_NOSB(void, Bs3Syscall64Generic,(void));
3613/** The 32-bit FLAT address of Bs3Syscall64Generic (for 16-bit code). */
3614extern uint32_t g_pfnBs3Syscall64GenericFlat;
3615
3616/** Entry point for MSR_K8_CSTAR (64-bit).
3617 * This hooks into the default Bs3TrapSetJmp logic. */
3618BS3_CMN_PROTO_NOSB(void, Bs3Syscall64GenericCompatibility,(void));
3619/** The 32-bit FLAT address of Bs3Syscall64Generic (for 16-bit code). */
3620extern uint32_t g_pfnBs3Syscall64GenericCompatibilityFlat;
3621
3622
3623
3624/**
3625 * The current test step.
3626 */
3627extern uint16_t g_usBs3TestStep;
3628
3629/** Test repetitions necessary to make sure of engaging native recompilation.
3630 * @note See iemTbCacheLookup() for the actual constant. */
3631#define BS3_THRESHOLD_NATIVE_RECOMPILER 18
3632
3633/** The host configurable BS3_THRESHOLD_NATIVE_RECOMPILER value.
3634 * The host configuration is read by Bs3TestInit, before that it has the same
3635 * value as the compiletime constant. */
3636extern uint16_t g_cBs3ThresholdNativeRecompiler;
3637
3638/**
3639 * Equivalent to RTTestCreate + RTTestBanner.
3640 *
3641 * @param pszTest The test name.
3642 */
3643BS3_CMN_PROTO_STUB(void, Bs3TestInit,(const char BS3_FAR *pszTest));
3644
3645
3646/**
3647 * Equivalent to RTTestSummaryAndDestroy.
3648 */
3649BS3_CMN_PROTO_STUB(void, Bs3TestTerm,(void));
3650
3651/**
3652 * Equivalent to RTTestISub.
3653 */
3654BS3_CMN_PROTO_STUB(void, Bs3TestSub,(const char BS3_FAR *pszSubTest));
3655
3656/**
3657 * Equivalent to RTTestISubF.
3658 */
3659BS3_CMN_PROTO_STUB(void, Bs3TestSubF,(const char BS3_FAR *pszFormat, ...));
3660
3661/**
3662 * Equivalent to RTTestISubV.
3663 */
3664BS3_CMN_PROTO_STUB(void, Bs3TestSubV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3665
3666/**
3667 * Equivalent to RTTestISubDone.
3668 */
3669BS3_CMN_PROTO_STUB(void, Bs3TestSubDone,(void));
3670
3671/**
3672 * Equivalent to RTTestISubSub.
3673 */
3674BS3_CMN_PROTO_STUB(void, Bs3TestSubSub,(const char BS3_FAR *pszSubTest));
3675
3676/**
3677 * Equivalent to RTTestISubSubF.
3678 */
3679BS3_CMN_PROTO_STUB(void, Bs3TestSubSubF,(const char BS3_FAR *pszFormat, ...));
3680
3681/**
3682 * Equivalent to RTTestISubSubV.
3683 */
3684BS3_CMN_PROTO_STUB(void, Bs3TestSubSubV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3685
3686/**
3687 * Equivalent to RTTestISubSubDone.
3688 */
3689BS3_CMN_PROTO_STUB(void, Bs3TestSubSubDone,(void));
3690
3691/**
3692 * Equivalent to RTTestIValue.
3693 */
3694BS3_CMN_PROTO_STUB(void, Bs3TestValue,(const char BS3_FAR *pszName, uint64_t u64Value, uint8_t bUnit));
3695
3696/**
3697 * Equivalent to RTTestSubErrorCount.
3698 */
3699BS3_CMN_PROTO_STUB(uint16_t, Bs3TestSubErrorCount,(void));
3700
3701/**
3702 * Get nanosecond host timestamp.
3703 *
3704 * This only works when testing is enabled and will not work in VMs configured
3705 * with a 286, 186 or 8086/8088 CPU profile.
3706 */
3707BS3_CMN_PROTO_STUB(uint64_t, Bs3TestNow,(void));
3708
3709
3710/**
3711 * Queries an unsigned 8-bit configuration value.
3712 *
3713 * @returns Value.
3714 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3715 * @param bDefault The default value to return if the VMMDev isn't
3716 * available or the query failed.
3717 */
3718BS3_CMN_PROTO_STUB(uint8_t, Bs3TestQueryCfgU8,(uint16_t uCfg, uint8_t bDefault));
3719
3720/**
3721 * Queries a boolean configuration value.
3722 *
3723 * @returns Value.
3724 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3725 * @param fDefault The default value to return if the VMMDev isn't
3726 * available or the query failed.
3727 */
3728BS3_CMN_PROTO_STUB(bool, Bs3TestQueryCfgBool,(uint16_t uCfg, bool fDefault));
3729
3730/**
3731 * Queries an unsigned 16-bit configuration value.
3732 *
3733 * @returns Value.
3734 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3735 * @param uDefault The default value to return if the VMMDev isn't
3736 * available or the query failed.
3737 */
3738BS3_CMN_PROTO_STUB(uint16_t, Bs3TestQueryCfgU16,(uint16_t uCfg, uint16_t uDefault));
3739
3740/**
3741 * Queries an unsigned 32-bit configuration value.
3742 *
3743 * @returns Value.
3744 * @param uCfg A VMMDEV_TESTING_CFG_XXX value.
3745 * @param uDefault The default value to return if the VMMDev isn't
3746 * available or the query failed.
3747 */
3748BS3_CMN_PROTO_STUB(uint32_t, Bs3TestQueryCfgU32,(uint16_t uCfg, uint32_t uDefault));
3749
3750/**
3751 * Equivalent to RTTestIPrintf with RTTESTLVL_ALWAYS.
3752 *
3753 * @param pszFormat What to print, format string. Explicit newline char.
3754 * @param ... String format arguments.
3755 */
3756BS3_CMN_PROTO_STUB(void, Bs3TestPrintf,(const char BS3_FAR *pszFormat, ...));
3757
3758/**
3759 * Equivalent to RTTestIPrintfV with RTTESTLVL_ALWAYS.
3760 *
3761 * @param pszFormat What to print, format string. Explicit newline char.
3762 * @param va String format arguments.
3763 */
3764BS3_CMN_PROTO_STUB(void, Bs3TestPrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3765
3766/**
3767 * Same as Bs3TestPrintf, except no guest screen echo.
3768 *
3769 * @param pszFormat What to print, format string. Explicit newline char.
3770 * @param ... String format arguments.
3771 */
3772BS3_CMN_PROTO_STUB(void, Bs3TestHostPrintf,(const char BS3_FAR *pszFormat, ...));
3773
3774/**
3775 * Same as Bs3TestPrintfV, except no guest screen echo.
3776 *
3777 * @param pszFormat What to print, format string. Explicit newline char.
3778 * @param va String format arguments.
3779 */
3780BS3_CMN_PROTO_STUB(void, Bs3TestHostPrintfV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3781
3782/**
3783 * Equivalent to RTTestIFailed.
3784 * @returns false.
3785 */
3786BS3_CMN_PROTO_STUB(bool, Bs3TestFailed,(const char BS3_FAR *pszMessage));
3787
3788/**
3789 * Equivalent to RTTestIFailedF.
3790 * @returns false.
3791 */
3792BS3_CMN_PROTO_STUB(bool, Bs3TestFailedF,(const char BS3_FAR *pszFormat, ...));
3793
3794/**
3795 * Equivalent to RTTestIFailedV.
3796 * @returns false.
3797 */
3798BS3_CMN_PROTO_STUB(bool, Bs3TestFailedV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3799
3800/**
3801 * Equivalent to RTTestISkipped.
3802 *
3803 * @param pszWhy Optional reason why it's being skipped.
3804 */
3805BS3_CMN_PROTO_STUB(void, Bs3TestSkipped,(const char BS3_FAR *pszWhy));
3806
3807/**
3808 * Equivalent to RTTestISkippedF.
3809 *
3810 * @param pszFormat Optional reason why it's being skipped.
3811 * @param ... Format arguments.
3812 */
3813BS3_CMN_PROTO_STUB(void, Bs3TestSkippedF,(const char BS3_FAR *pszFormat, ...));
3814
3815/**
3816 * Equivalent to RTTestISkippedV.
3817 *
3818 * @param pszFormat Optional reason why it's being skipped.
3819 * @param va Format arguments.
3820 */
3821BS3_CMN_PROTO_STUB(void, Bs3TestSkippedV,(const char BS3_FAR *pszFormat, va_list BS3_FAR va));
3822
3823/**
3824 * Compares two register contexts, with PC and SP adjustments.
3825 *
3826 * Differences will be reported as test failures.
3827 *
3828 * @returns true if equal, false if not.
3829 * @param pActualCtx The actual register context.
3830 * @param pExpectedCtx Expected register context.
3831 * @param cbPcAdjust Program counter adjustment (applied to @a pExpectedCtx).
3832 * @param cbSpAdjust Stack pointer adjustment (applied to @a pExpectedCtx).
3833 * @param fExtraEfl Extra EFLAGS to OR into @a pExepctedCtx.
3834 * @param pszMode CPU mode or some other helpful text.
3835 * @param idTestStep Test step identifier.
3836 */
3837BS3_CMN_PROTO_STUB(bool, Bs3TestCheckRegCtxEx,(PCBS3REGCTX pActualCtx, PCBS3REGCTX pExpectedCtx, uint16_t cbPcAdjust,
3838 int16_t cbSpAdjust, uint32_t fExtraEfl,
3839 const char BS3_FAR *pszMode, uint16_t idTestStep));
3840
3841/**
3842 * Compares two extended register contexts.
3843 *
3844 * Differences will be reported as test failures.
3845 *
3846 * @returns true if equal, false if not.
3847 * @param pActualExtCtx The actual register context.
3848 * @param pExpectedExtCtx Expected register context.
3849 * @param fFlags Reserved, pass 0.
3850 * @param pszMode CPU mode or some other helpful text.
3851 * @param idTestStep Test step identifier.
3852 */
3853BS3_CMN_PROTO_STUB(bool, Bs3TestCheckExtCtx,(PCBS3EXTCTX pActualExtCtx, PCBS3EXTCTX pExpectedExtCtx, uint16_t fFlags,
3854 const char BS3_FAR *pszMode, uint16_t idTestStep));
3855
3856/**
3857 * Performs the testing for the given mode.
3858 *
3859 * This is called with the CPU already switch to that mode.
3860 *
3861 * @returns 0 on success or directly Bs3TestFailed calls, non-zero to indicate
3862 * where the test when wrong. Special value BS3TESTDOMODE_SKIPPED
3863 * should be returned to indicate that the test has been skipped.
3864 * @param bMode The current CPU mode.
3865 */
3866typedef BS3_DECL_CALLBACK(uint8_t) FNBS3TESTDOMODE(uint8_t bMode);
3867/** Pointer (far) to a test (for 32-bit and 64-bit code, will be flatten). */
3868typedef FNBS3TESTDOMODE *PFNBS3TESTDOMODE;
3869
3870/** Special FNBS3TESTDOMODE return code for indicating a skipped mode test. */
3871#define BS3TESTDOMODE_SKIPPED UINT8_MAX
3872
3873/**
3874 * Mode sub-test entry.
3875 *
3876 * This can only be passed around to functions with the same bit count, as it
3877 * contains function pointers. In 16-bit mode, the 16-bit pointers are near and
3878 * implies BS3TEXT16, whereas the 32-bit and 64-bit pointers are far real mode
3879 * addresses that will be converted to flat address prior to calling them.
3880 * Similarly, in 32-bit and 64-bit the addresses are all flat and the 16-bit
3881 * ones will be converted to BS3TEXT16 based addresses prior to calling.
3882 */
3883typedef struct BS3TESTMODEENTRY
3884{
3885 /** The sub-test name to be passed to Bs3TestSub if not NULL. */
3886 const char * BS3_FAR pszSubTest;
3887
3888 PFNBS3TESTDOMODE pfnDoRM;
3889
3890 PFNBS3TESTDOMODE pfnDoPE16;
3891 PFNBS3TESTDOMODE pfnDoPE16_32;
3892 PFNBS3TESTDOMODE pfnDoPE16_V86;
3893 PFNBS3TESTDOMODE pfnDoPE32;
3894 PFNBS3TESTDOMODE pfnDoPE32_16;
3895 PFNBS3TESTDOMODE pfnDoPEV86;
3896
3897 PFNBS3TESTDOMODE pfnDoPP16;
3898 PFNBS3TESTDOMODE pfnDoPP16_32;
3899 PFNBS3TESTDOMODE pfnDoPP16_V86;
3900 PFNBS3TESTDOMODE pfnDoPP32;
3901 PFNBS3TESTDOMODE pfnDoPP32_16;
3902 PFNBS3TESTDOMODE pfnDoPPV86;
3903
3904 PFNBS3TESTDOMODE pfnDoPAE16;
3905 PFNBS3TESTDOMODE pfnDoPAE16_32;
3906 PFNBS3TESTDOMODE pfnDoPAE16_V86;
3907 PFNBS3TESTDOMODE pfnDoPAE32;
3908 PFNBS3TESTDOMODE pfnDoPAE32_16;
3909 PFNBS3TESTDOMODE pfnDoPAEV86;
3910
3911 PFNBS3TESTDOMODE pfnDoLM16;
3912 PFNBS3TESTDOMODE pfnDoLM32;
3913 PFNBS3TESTDOMODE pfnDoLM64;
3914
3915} BS3TESTMODEENTRY;
3916/** Pointer to a mode sub-test entry. */
3917typedef BS3TESTMODEENTRY const *PCBS3TESTMODEENTRY;
3918
3919/** @def BS3TESTMODEENTRY_CMN
3920 * Produces a BS3TESTMODEENTRY initializer for common (c16,c32,c64) test
3921 * functions. */
3922#define BS3TESTMODEENTRY_CMN(a_szTest, a_BaseNm) \
3923 { /*pszSubTest =*/ a_szTest, \
3924 /*RM*/ RT_CONCAT(a_BaseNm, _c16), \
3925 /*PE16*/ RT_CONCAT(a_BaseNm, _c16), \
3926 /*PE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3927 /*PE16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3928 /*PE32*/ RT_CONCAT(a_BaseNm, _c32), \
3929 /*PE32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3930 /*PEV86*/ RT_CONCAT(a_BaseNm, _c16), \
3931 /*PP16*/ RT_CONCAT(a_BaseNm, _c16), \
3932 /*PP16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3933 /*PP16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3934 /*PP32*/ RT_CONCAT(a_BaseNm, _c32), \
3935 /*PP32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3936 /*PPV86*/ RT_CONCAT(a_BaseNm, _c16), \
3937 /*PAE16*/ RT_CONCAT(a_BaseNm, _c16), \
3938 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
3939 /*PAE16_V86*/ RT_CONCAT(a_BaseNm, _c16), \
3940 /*PAE32*/ RT_CONCAT(a_BaseNm, _c32), \
3941 /*PAE32_16*/ RT_CONCAT(a_BaseNm, _c16), \
3942 /*PAEV86*/ RT_CONCAT(a_BaseNm, _c16), \
3943 /*LM16*/ RT_CONCAT(a_BaseNm, _c16), \
3944 /*LM32*/ RT_CONCAT(a_BaseNm, _c32), \
3945 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
3946 }
3947
3948/** @def BS3TESTMODE_PROTOTYPES_CMN
3949 * A set of standard protypes to go with #BS3TESTMODEENTRY_CMN. */
3950#define BS3TESTMODE_PROTOTYPES_CMN(a_BaseNm) \
3951 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c16); \
3952 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c32); \
3953 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
3954
3955/** @def BS3TESTMODEENTRY_CMN_64
3956 * Produces a BS3TESTMODEENTRY initializer for common 64-bit test functions. */
3957#define BS3TESTMODEENTRY_CMN_64(a_szTest, a_BaseNm) \
3958 { /*pszSubTest =*/ a_szTest, \
3959 /*RM*/ NULL, \
3960 /*PE16*/ NULL, \
3961 /*PE16_32*/ NULL, \
3962 /*PE16_V86*/ NULL, \
3963 /*PE32*/ NULL, \
3964 /*PE32_16*/ NULL, \
3965 /*PEV86*/ NULL, \
3966 /*PP16*/ NULL, \
3967 /*PP16_32*/ NULL, \
3968 /*PP16_V86*/ NULL, \
3969 /*PP32*/ NULL, \
3970 /*PP32_16*/ NULL, \
3971 /*PPV86*/ NULL, \
3972 /*PAE16*/ NULL, \
3973 /*PAE16_32*/ NULL, \
3974 /*PAE16_V86*/ NULL, \
3975 /*PAE32*/ NULL, \
3976 /*PAE32_16*/ NULL, \
3977 /*PAEV86*/ NULL, \
3978 /*LM16*/ NULL, \
3979 /*LM32*/ NULL, \
3980 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
3981 }
3982
3983/** @def BS3TESTMODE_PROTOTYPES_CMN
3984 * Standard protype to go with #BS3TESTMODEENTRY_CMN_64. */
3985#define BS3TESTMODE_PROTOTYPES_CMN_64(a_BaseNm) \
3986 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
3987
3988/** @def BS3TESTMODEENTRY_MODE
3989 * Produces a BS3TESTMODEENTRY initializer for a full set of mode test
3990 * functions. */
3991#define BS3TESTMODEENTRY_MODE(a_szTest, a_BaseNm) \
3992 { /*pszSubTest =*/ a_szTest, \
3993 /*RM*/ RT_CONCAT(a_BaseNm, _rm), \
3994 /*PE16*/ RT_CONCAT(a_BaseNm, _pe16), \
3995 /*PE16_32*/ RT_CONCAT(a_BaseNm, _pe16_32), \
3996 /*PE16_V86*/ RT_CONCAT(a_BaseNm, _pe16_v86), \
3997 /*PE32*/ RT_CONCAT(a_BaseNm, _pe32), \
3998 /*PE32_16*/ RT_CONCAT(a_BaseNm, _pe32_16), \
3999 /*PEV86*/ RT_CONCAT(a_BaseNm, _pev86), \
4000 /*PP16*/ RT_CONCAT(a_BaseNm, _pp16), \
4001 /*PP16_32*/ RT_CONCAT(a_BaseNm, _pp16_32), \
4002 /*PP16_V86*/ RT_CONCAT(a_BaseNm, _pp16_v86), \
4003 /*PP32*/ RT_CONCAT(a_BaseNm, _pp32), \
4004 /*PP32_16*/ RT_CONCAT(a_BaseNm, _pp32_16), \
4005 /*PPV86*/ RT_CONCAT(a_BaseNm, _ppv86), \
4006 /*PAE16*/ RT_CONCAT(a_BaseNm, _pae16), \
4007 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _pae16_32), \
4008 /*PAE16_V86*/ RT_CONCAT(a_BaseNm, _pae16_v86), \
4009 /*PAE32*/ RT_CONCAT(a_BaseNm, _pae32), \
4010 /*PAE32_16*/ RT_CONCAT(a_BaseNm, _pae32_16), \
4011 /*PAEV86*/ RT_CONCAT(a_BaseNm, _paev86), \
4012 /*LM16*/ RT_CONCAT(a_BaseNm, _lm16), \
4013 /*LM32*/ RT_CONCAT(a_BaseNm, _lm32), \
4014 /*LM64*/ RT_CONCAT(a_BaseNm, _lm64), \
4015 }
4016
4017/** @def BS3TESTMODE_PROTOTYPES_MODE
4018 * A set of standard protypes to go with #BS3TESTMODEENTRY_MODE. */
4019#define BS3TESTMODE_PROTOTYPES_MODE(a_BaseNm) \
4020 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _rm); \
4021 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16); \
4022 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_32); \
4023 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_v86); \
4024 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32); \
4025 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32_16); \
4026 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pev86); \
4027 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16); \
4028 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_32); \
4029 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_v86); \
4030 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32); \
4031 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32_16); \
4032 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _ppv86); \
4033 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16); \
4034 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_32); \
4035 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_v86); \
4036 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32); \
4037 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32_16); \
4038 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _paev86); \
4039 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm16); \
4040 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm32); \
4041 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm64)
4042
4043
4044/**
4045 * Mode sub-test entry, max bit-count driven
4046 *
4047 * This is an alternative to BS3TESTMODEENTRY where a few workers (test drivers)
4048 * does all the work, using faster 32-bit and 64-bit code where possible. This
4049 * avoids executing workers in V8086 mode. It allows for modifying and checking
4050 * 64-bit register content when testing LM16 and LM32.
4051 *
4052 * The 16-bit workers are only used for real mode and 16-bit protected mode.
4053 * So, the 16-bit version of the code template can be stripped of anything
4054 * related to paging and/or v8086, saving code space.
4055 */
4056typedef struct BS3TESTMODEBYMAXENTRY
4057{
4058 /** The sub-test name to be passed to Bs3TestSub if not NULL. */
4059 const char * BS3_FAR pszSubTest;
4060
4061 PFNBS3TESTDOMODE pfnDoRM;
4062 PFNBS3TESTDOMODE pfnDoPE16;
4063 PFNBS3TESTDOMODE pfnDoPE16_32;
4064 PFNBS3TESTDOMODE pfnDoPE32;
4065 PFNBS3TESTDOMODE pfnDoPP16_32;
4066 PFNBS3TESTDOMODE pfnDoPP32;
4067 PFNBS3TESTDOMODE pfnDoPAE16_32;
4068 PFNBS3TESTDOMODE pfnDoPAE32;
4069 PFNBS3TESTDOMODE pfnDoLM64;
4070
4071 bool fDoRM : 1;
4072
4073 bool fDoPE16 : 1;
4074 bool fDoPE16_32 : 1;
4075 bool fDoPE16_V86 : 1;
4076 bool fDoPE32 : 1;
4077 bool fDoPE32_16 : 1;
4078 bool fDoPEV86 : 1;
4079
4080 bool fDoPP16 : 1;
4081 bool fDoPP16_32 : 1;
4082 bool fDoPP16_V86 : 1;
4083 bool fDoPP32 : 1;
4084 bool fDoPP32_16 : 1;
4085 bool fDoPPV86 : 1;
4086
4087 bool fDoPAE16 : 1;
4088 bool fDoPAE16_32 : 1;
4089 bool fDoPAE16_V86 : 1;
4090 bool fDoPAE32 : 1;
4091 bool fDoPAE32_16 : 1;
4092 bool fDoPAEV86 : 1;
4093
4094 bool fDoLM16 : 1;
4095 bool fDoLM32 : 1;
4096 bool fDoLM64 : 1;
4097
4098} BS3TESTMODEBYMAXENTRY;
4099/** Pointer to a mode-by-max sub-test entry. */
4100typedef BS3TESTMODEBYMAXENTRY const *PCBS3TESTMODEBYMAXENTRY;
4101
4102/** @def BS3TESTMODEBYMAXENTRY_CMN
4103 * Produces a BS3TESTMODEBYMAXENTRY initializer for common (c16,c32,c64) test
4104 * functions. */
4105#define BS3TESTMODEBYMAXENTRY_CMN(a_szTest, a_BaseNm) \
4106 { /*pszSubTest =*/ a_szTest, \
4107 /*RM*/ RT_CONCAT(a_BaseNm, _c16), \
4108 /*PE16*/ RT_CONCAT(a_BaseNm, _c16), \
4109 /*PE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4110 /*PE32*/ RT_CONCAT(a_BaseNm, _c32), \
4111 /*PP16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4112 /*PP32*/ RT_CONCAT(a_BaseNm, _c32), \
4113 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _c32), \
4114 /*PAE32*/ RT_CONCAT(a_BaseNm, _c32), \
4115 /*LM64*/ RT_CONCAT(a_BaseNm, _c64), \
4116 /*fDoRM*/ true, \
4117 /*fDoPE16*/ true, \
4118 /*fDoPE16_32*/ true, \
4119 /*fDoPE16_V86*/ true, \
4120 /*fDoPE32*/ true, \
4121 /*fDoPE32_16*/ true, \
4122 /*fDoPEV86*/ true, \
4123 /*fDoPP16*/ true, \
4124 /*fDoPP16_32*/ true, \
4125 /*fDoPP16_V86*/ true, \
4126 /*fDoPP32*/ true, \
4127 /*fDoPP32_16*/ true, \
4128 /*fDoPPV86*/ true, \
4129 /*fDoPAE16*/ true, \
4130 /*fDoPAE16_32*/ true, \
4131 /*fDoPAE16_V86*/ true, \
4132 /*fDoPAE32*/ true, \
4133 /*fDoPAE32_16*/ true, \
4134 /*fDoPAEV86*/ true, \
4135 /*fDoLM16*/ true, \
4136 /*fDoLM32*/ true, \
4137 /*fDoLM64*/ true, \
4138 }
4139
4140/** @def BS3TESTMODEBYMAX_PROTOTYPES_CMN
4141 * A set of standard protypes to go with #BS3TESTMODEBYMAXENTRY_CMN. */
4142#define BS3TESTMODEBYMAX_PROTOTYPES_CMN(a_BaseNm) \
4143 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c16); \
4144 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c32); \
4145 FNBS3TESTDOMODE /*BS3_FAR_CODE*/ RT_CONCAT(a_BaseNm, _c64)
4146
4147
4148/** @def BS3TESTMODEBYMAXENTRY_MODE
4149 * Produces a BS3TESTMODEBYMAXENTRY initializer for a full set of mode test
4150 * functions. */
4151#define BS3TESTMODEBYMAXENTRY_MODE(a_szTest, a_BaseNm) \
4152 { /*pszSubTest =*/ a_szTest, \
4153 /*RM*/ RT_CONCAT(a_BaseNm, _rm), \
4154 /*PE16*/ RT_CONCAT(a_BaseNm, _pe16), \
4155 /*PE16_32*/ RT_CONCAT(a_BaseNm, _pe16_32), \
4156 /*PE32*/ RT_CONCAT(a_BaseNm, _pe32), \
4157 /*PP16_32*/ RT_CONCAT(a_BaseNm, _pp16_32), \
4158 /*PP32*/ RT_CONCAT(a_BaseNm, _pp32), \
4159 /*PAE16_32*/ RT_CONCAT(a_BaseNm, _pae16_32), \
4160 /*PAE32*/ RT_CONCAT(a_BaseNm, _pae32), \
4161 /*LM64*/ RT_CONCAT(a_BaseNm, _lm64), \
4162 /*fDoRM*/ true, \
4163 /*fDoPE16*/ true, \
4164 /*fDoPE16_32*/ true, \
4165 /*fDoPE16_V86*/ true, \
4166 /*fDoPE32*/ true, \
4167 /*fDoPE32_16*/ true, \
4168 /*fDoPEV86*/ true, \
4169 /*fDoPP16*/ true, \
4170 /*fDoPP16_32*/ true, \
4171 /*fDoPP16_V86*/ true, \
4172 /*fDoPP32*/ true, \
4173 /*fDoPP32_16*/ true, \
4174 /*fDoPPV86*/ true, \
4175 /*fDoPAE16*/ true, \
4176 /*fDoPAE16_32*/ true, \
4177 /*fDoPAE16_V86*/ true, \
4178 /*fDoPAE32*/ true, \
4179 /*fDoPAE32_16*/ true, \
4180 /*fDoPAEV86*/ true, \
4181 /*fDoLM16*/ true, \
4182 /*fDoLM32*/ true, \
4183 /*fDoLM64*/ true, \
4184 }
4185
4186/** @def BS3TESTMODEBYMAX_PROTOTYPES_MODE
4187 * A set of standard protypes to go with #BS3TESTMODEBYMAXENTRY_MODE. */
4188#define BS3TESTMODEBYMAX_PROTOTYPES_MODE(a_BaseNm) \
4189 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _rm); \
4190 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16); \
4191 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe16_32); \
4192 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pe32); \
4193 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp16_32); \
4194 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pp32); \
4195 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae16_32); \
4196 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _pae32); \
4197 FNBS3TESTDOMODE RT_CONCAT(a_BaseNm, _lm64)
4198
4199
4200/**
4201 * One worker drives all modes.
4202 *
4203 * This is an alternative to BS3TESTMODEENTRY where one worker, typically
4204 * 16-bit, does all the test driver work. It's called repeatedly from all
4205 * the modes being tested.
4206 */
4207typedef struct BS3TESTMODEBYONEENTRY
4208{
4209 const char * BS3_FAR pszSubTest;
4210 PFNBS3TESTDOMODE pfnWorker;
4211 /** BS3TESTMODEBYONEENTRY_F_XXX. */
4212 uint32_t fFlags;
4213} BS3TESTMODEBYONEENTRY;
4214/** Pointer to a mode-by-one sub-test entry. */
4215typedef BS3TESTMODEBYONEENTRY const *PCBS3TESTMODEBYONEENTRY;
4216
4217/** @name BS3TESTMODEBYONEENTRY_F_XXX - flags.
4218 * @{ */
4219/** Only test modes that has paging enabled. */
4220#define BS3TESTMODEBYONEENTRY_F_ONLY_PAGING RT_BIT_32(0)
4221/** Minimal mode selection. */
4222#define BS3TESTMODEBYONEENTRY_F_MINIMAL RT_BIT_32(1)
4223/** The 32-bit worker is ready to handle real-mode by mode switching. */
4224#define BS3TESTMODEBYONEENTRY_F_REAL_MODE_READY RT_BIT_32(2)
4225/** Skip all v8086 mode tests. */
4226#define BS3TESTMODEBYONEENTRY_F_SKIP_V8086 RT_BIT_32(3)
4227/** @} */
4228
4229
4230/**
4231 * Sets the full GDTR register.
4232 *
4233 * @param cbLimit The limit.
4234 * @param uBase The base address - 24, 32 or 64 bit depending on the
4235 * CPU mode.
4236 */
4237BS3_CMN_PROTO_NOSB(void, Bs3UtilSetFullGdtr,(uint16_t cbLimit, uint64_t uBase));
4238
4239/**
4240 * Sets the full IDTR register.
4241 *
4242 * @param cbLimit The limit.
4243 * @param uBase The base address - 24, 32 or 64 bit depending on the
4244 * CPU mode.
4245 */
4246BS3_CMN_PROTO_NOSB(void, Bs3UtilSetFullIdtr,(uint16_t cbLimit, uint64_t uBase));
4247
4248
4249/** @} */
4250
4251
4252/**
4253 * Initializes all of boot sector kit \#3.
4254 */
4255BS3_DECL(void) Bs3InitAll_rm(void);
4256
4257/**
4258 * Initializes all of boot sector kit \#3 as well as the high DLLs.
4259 */
4260BS3_DECL(void) Bs3InitAllWithHighDlls_rm(void);
4261
4262/**
4263 * Initializes the REAL and TILED memory pools.
4264 *
4265 * For proper operation on OLDer CPUs, call #Bs3CpuDetect_mmm first.
4266 */
4267BS3_DECL_FAR(void) Bs3InitMemory_rm_far(void);
4268
4269/**
4270 * Initializes the X0TEXT16 and X1TEXT16 GDT entries.
4271 */
4272BS3_DECL_FAR(void) Bs3InitGdt_rm_far(void);
4273
4274/**
4275 * Initializes (loads) any high DLLs.
4276 *
4277 * @note This cannot be called after the PIC or traps have been initialized!
4278 */
4279BS3_DECL_FAR(void) Bs3InitHighDlls_rm_far(void);
4280
4281
4282
4283/** @defgroup grp_bs3kit_mode Mode Specific Functions and Data
4284 *
4285 * The mode specific functions come in bit count variations and CPU mode
4286 * variations. The bs3kit-template-header.h/mac defines the BS3_NM macro to
4287 * mangle a function or variable name according to the target CPU mode. In
4288 * non-templated code, it's common to spell the name out in full.
4289 *
4290 * @{
4291 */
4292
4293
4294/** @def BS3_MODE_PROTO_INT
4295 * Internal macro for emitting prototypes for mode functions.
4296 *
4297 * @param a_RetType The return type.
4298 * @param a_Name The function basename.
4299 * @param a_Params The parameter list (in parentheses).
4300 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4301 */
4302#define BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params) \
4303 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_rm) a_Params; \
4304 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16) a_Params; \
4305 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16_32) a_Params; \
4306 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe16_v86) a_Params; \
4307 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe32) a_Params; \
4308 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pe32_16) a_Params; \
4309 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pev86) a_Params; \
4310 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16) a_Params; \
4311 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16_32) a_Params; \
4312 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp16_v86) a_Params; \
4313 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp32) a_Params; \
4314 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pp32_16) a_Params; \
4315 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_ppv86) a_Params; \
4316 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16) a_Params; \
4317 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16_32) a_Params; \
4318 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae16_v86) a_Params; \
4319 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae32) a_Params; \
4320 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_pae32_16) a_Params; \
4321 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_paev86) a_Params; \
4322 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm16) a_Params; \
4323 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm32) a_Params; \
4324 BS3_DECL_NEAR(a_RetType) RT_CONCAT(a_Name,_lm64) a_Params; \
4325 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_rm_far) a_Params; \
4326 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe16_far) a_Params; \
4327 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe16_v86_far) a_Params; \
4328 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pe32_16_far) a_Params; \
4329 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pev86_far) a_Params; \
4330 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp16_far) a_Params; \
4331 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp16_v86_far) a_Params; \
4332 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pp32_16_far) a_Params; \
4333 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_ppv86_far) a_Params; \
4334 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae16_far) a_Params; \
4335 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae16_v86_far)a_Params; \
4336 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_pae32_16_far) a_Params; \
4337 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_paev86_far) a_Params; \
4338 BS3_DECL_FAR(a_RetType) RT_CONCAT(a_Name,_lm16_far) a_Params
4339
4340/** @def BS3_MODE_PROTO_STUB
4341 * Macro for prototyping all the variations of a mod function with automatic
4342 * near -> far stub.
4343 *
4344 * @param a_RetType The return type.
4345 * @param a_Name The function basename.
4346 * @param a_Params The parameter list (in parentheses).
4347 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4348 */
4349#define BS3_MODE_PROTO_STUB(a_RetType, a_Name, a_Params) BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params)
4350
4351/** @def BS3_MODE_PROTO_STUB
4352 * Macro for prototyping all the variations of a mod function without any
4353 * near -> far stub.
4354 *
4355 * @param a_RetType The return type.
4356 * @param a_Name The function basename.
4357 * @param a_Params The parameter list (in parentheses).
4358 * @sa BS3_MODE_PROTO_STUB, BS3_MODE_PROTO_NOSB
4359 */
4360#define BS3_MODE_PROTO_NOSB(a_RetType, a_Name, a_Params) BS3_MODE_PROTO_INT(a_RetType, a_Name, a_Params)
4361
4362
4363/**
4364 * Macro for reducing typing.
4365 *
4366 * Doxygen knows how to expand this, well, kind of.
4367 *
4368 * @remarks Variables instantiated in assembly code should define two labels,
4369 * with and without leading underscore. Variables instantiated from
4370 * C/C++ code doesn't need to as the object file convert does this for
4371 * 64-bit object files.
4372 */
4373#define BS3_MODE_EXPAND_EXTERN_DATA16(a_VarType, a_VarName, a_Suffix) \
4374 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_rm) a_Suffix; \
4375 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16) a_Suffix; \
4376 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16_32) a_Suffix; \
4377 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe16_v86) a_Suffix; \
4378 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe32) a_Suffix; \
4379 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pe32_16) a_Suffix; \
4380 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pev86) a_Suffix; \
4381 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16) a_Suffix; \
4382 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16_32) a_Suffix; \
4383 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp16_v86) a_Suffix; \
4384 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp32) a_Suffix; \
4385 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pp32_16) a_Suffix; \
4386 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_ppv86) a_Suffix; \
4387 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16) a_Suffix; \
4388 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16_32) a_Suffix; \
4389 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae16_v86)a_Suffix; \
4390 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae32) a_Suffix; \
4391 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_pae32_16) a_Suffix; \
4392 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_paev86) a_Suffix; \
4393 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm16) a_Suffix; \
4394 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm32) a_Suffix; \
4395 extern a_VarType BS3_FAR_DATA RT_CONCAT(a_VarName,_lm64) a_Suffix
4396
4397
4398/** The TMPL_MODE_STR value for each mode.
4399 * These are all in DATA16 so they can be accessed from any code. */
4400BS3_MODE_EXPAND_EXTERN_DATA16(const char, g_szBs3ModeName, []);
4401/** The TMPL_MODE_LNAME value for each mode.
4402 * These are all in DATA16 so they can be accessed from any code. */
4403BS3_MODE_EXPAND_EXTERN_DATA16(const char, g_szBs3ModeNameShortLower, []);
4404
4405
4406/**
4407 * Basic CPU detection.
4408 *
4409 * This sets the #g_uBs3CpuDetected global variable to the return value.
4410 *
4411 * @returns BS3CPU_XXX value with the BS3CPU_F_CPUID flag set depending on
4412 * capabilities.
4413 */
4414BS3_MODE_PROTO_NOSB(uint8_t, Bs3CpuDetect,(void));
4415
4416/** @name BS3CPU_XXX - CPU detected by BS3CpuDetect_c16() and friends.
4417 * @{ */
4418#define BS3CPU_8086 UINT16_C(0x0001) /**< Both 8086 and 8088. */
4419#define BS3CPU_V20 UINT16_C(0x0002) /**< Both NEC V20, V30 and relatives. */
4420#define BS3CPU_80186 UINT16_C(0x0003) /**< Both 80186 and 80188. */
4421#define BS3CPU_80286 UINT16_C(0x0004)
4422#define BS3CPU_80386 UINT16_C(0x0005)
4423#define BS3CPU_80486 UINT16_C(0x0006)
4424#define BS3CPU_Pentium UINT16_C(0x0007)
4425#define BS3CPU_PPro UINT16_C(0x0008)
4426#define BS3CPU_PProOrNewer UINT16_C(0x0009)
4427/** CPU type mask. This is a full byte so it's possible to use byte access
4428 * without and AND'ing to get the type value. */
4429#define BS3CPU_TYPE_MASK UINT16_C(0x00ff)
4430/** Flag indicating that the CPUID instruction is supported by the CPU. */
4431#define BS3CPU_F_CPUID UINT16_C(0x0100)
4432/** Flag indicating that extend CPUID leaves are available (at least two). */
4433#define BS3CPU_F_CPUID_EXT_LEAVES UINT16_C(0x0200)
4434/** Flag indicating that the CPU supports PAE. */
4435#define BS3CPU_F_PAE UINT16_C(0x0400)
4436/** Flag indicating that the CPU supports the page size extension (4MB pages). */
4437#define BS3CPU_F_PSE UINT16_C(0x0800)
4438/** Flag indicating that the CPU supports long mode. */
4439#define BS3CPU_F_LONG_MODE UINT16_C(0x1000)
4440/** Flag indicating that the CPU supports NX. */
4441#define BS3CPU_F_NX UINT16_C(0x2000)
4442/** @} */
4443
4444/** The return value of #Bs3CpuDetect_mmm. (Initial value is BS3CPU_TYPE_MASK.) */
4445extern uint16_t g_uBs3CpuDetected;
4446
4447/**
4448 * Call 32-bit prot mode C function.
4449 *
4450 * This switches to 32-bit mode and calls the 32-bit @a fpfnCall C code with @a
4451 * cbParams on the stack, then returns in the original mode. When called in
4452 * real mode, this will switch to PE32.
4453 *
4454 * @returns 32-bit status code if the function returned anything.
4455 * @param fpfnCall Address of the 32-bit C function to call. When
4456 * called from 16-bit code, this is a far real mode
4457 * function pointer, i.e. as fixed up by the linker.
4458 * In 32-bit and 64-bit code, this is a flat address.
4459 * @param cbParams The size of the parameter list, in bytes.
4460 * @param ... The parameters.
4461 * @sa Bs3SwitchFromV86To16BitAndCallC
4462 *
4463 * @remarks WARNING! This probably doesn't work in 64-bit mode yet.
4464 * Only tested for 16-bit real mode.
4465 */
4466BS3_MODE_PROTO_STUB(int32_t, Bs3SwitchTo32BitAndCallC,(FPFNBS3FAR fpfnCall, unsigned cbParams, ...));
4467
4468/**
4469 * Initializes trap handling for the current system.
4470 *
4471 * Calls the appropriate Bs3Trap16Init, Bs3Trap32Init or Bs3Trap64Init function.
4472 */
4473BS3_MODE_PROTO_STUB(void, Bs3TrapInit,(void));
4474
4475/**
4476 * Special version of memcpy for copying from/to real mode.
4477 *
4478 * @returns pvDst
4479 * @param uFlatDst The flat address of the destination buffer.
4480 * @param uFlatSrc The flat address of the source buffer.
4481 * @param cbToCopy The number of bytes to copy. Max 64KB.
4482 *
4483 * @todo Only work on 386+ at present. Could be made to work for 286 and
4484 * addresses < 16MB if we care...
4485 */
4486BS3_MODE_PROTO_STUB(void BS3_FAR *, Bs3MemCopyFlat,(uint32_t uFlatDst, uint32_t uFlatSrc, uint32_t cbToCopy));
4487
4488/**
4489 * Executes the array of tests in every possibly mode.
4490 *
4491 * @param paEntries The mode sub-test entries.
4492 * @param cEntries The number of sub-test entries.
4493 */
4494BS3_MODE_PROTO_NOSB(void, Bs3TestDoModes,(PCBS3TESTMODEENTRY paEntries, size_t cEntries));
4495
4496/**
4497 * Executes the array of tests in every possibly mode, unified driver.
4498 *
4499 * This requires much less code space than Bs3TestDoModes as there is only one
4500 * instace of each sub-test driver code, instead of 3 (cmn) or 22 (per-mode)
4501 * copies.
4502 *
4503 * @param paEntries The mode sub-test-by-one entries.
4504 * @param cEntries The number of sub-test-by-one entries.
4505 * @param fFlags BS3TESTMODEBYONEENTRY_F_XXX.
4506 */
4507BS3_MODE_PROTO_NOSB(void, Bs3TestDoModesByOne,(PCBS3TESTMODEBYONEENTRY paEntries, size_t cEntries, uint32_t fFlags));
4508
4509/**
4510 * Executes the array of tests in every possibly mode, using the max bit-count
4511 * worker for each.
4512 *
4513 * @param paEntries The mode sub-test entries.
4514 * @param cEntries The number of sub-test entries.
4515 */
4516BS3_MODE_PROTO_NOSB(void, Bs3TestDoModesByMax,(PCBS3TESTMODEBYMAXENTRY paEntries, size_t cEntries));
4517
4518/** @} */
4519
4520
4521/** @defgroup grp_bs3kit_bios_int15 BIOS - int 15h
4522 * @{ */
4523
4524/** An INT15E820 data entry. */
4525typedef struct INT15E820ENTRY
4526{
4527 uint64_t uBaseAddr;
4528 uint64_t cbRange;
4529 /** Memory type this entry describes, see INT15E820_TYPE_XXX. */
4530 uint32_t uType;
4531 /** Optional. */
4532 uint32_t fAcpi3;
4533} INT15E820ENTRY;
4534AssertCompileSize(INT15E820ENTRY,24);
4535
4536
4537/** @name INT15E820_TYPE_XXX - Memory types returned by int 15h function 0xe820.
4538 * @{ */
4539#define INT15E820_TYPE_USABLE 1 /**< Usable RAM. */
4540#define INT15E820_TYPE_RESERVED 2 /**< Reserved by the system, unusable. */
4541#define INT15E820_TYPE_ACPI_RECLAIMABLE 3 /**< ACPI reclaimable memory, whatever that means. */
4542#define INT15E820_TYPE_ACPI_NVS 4 /**< ACPI non-volatile storage? */
4543#define INT15E820_TYPE_BAD 5 /**< Bad memory, unusable. */
4544/** @} */
4545
4546
4547/**
4548 * Performs an int 15h function 0xe820 call.
4549 *
4550 * @returns Success indicator.
4551 * @param pEntry The return buffer.
4552 * @param pcbEntry Input: The size of the buffer (min 20 bytes);
4553 * Output: The size of the returned data.
4554 * @param puContinuationValue Where to get and return the continuation value (EBX)
4555 * Set to zero the for the first call. Returned as zero
4556 * after the last entry.
4557 */
4558BS3_MODE_PROTO_STUB(bool, Bs3BiosInt15hE820,(INT15E820ENTRY BS3_FAR *pEntry, uint32_t BS3_FAR *pcbEntry,
4559 uint32_t BS3_FAR *puContinuationValue));
4560
4561/**
4562 * Performs an int 15h function 0x88 call.
4563 *
4564 * @returns UINT32_MAX on failure, number of KBs above 1MB otherwise.
4565 */
4566#if ARCH_BITS != 16 || !defined(BS3_BIOS_INLINE_RM)
4567BS3_MODE_PROTO_STUB(uint32_t, Bs3BiosInt15h88,(void));
4568#else
4569BS3_DECL(uint32_t) Bs3BiosInt15h88(void);
4570# pragma aux Bs3BiosInt15h88 = \
4571 ".286" \
4572 "clc" \
4573 "mov ax, 08800h" \
4574 "int 15h" \
4575 "jc failed" \
4576 "xor dx, dx" \
4577 "jmp done" \
4578 "failed:" \
4579 "xor ax, ax" \
4580 "dec ax" \
4581 "mov dx, ax" \
4582 "done:" \
4583 value [ax dx] \
4584 modify exact [ax bx cx dx es];
4585#endif
4586
4587/** @} */
4588
4589
4590/** @defgroup grp_bs3kit_kbd Keyboard
4591 * @{
4592 */
4593
4594/**
4595 * Waits for the keyboard controller to become ready.
4596 */
4597BS3_CMN_PROTO_NOSB(void, Bs3KbdWait,(void));
4598
4599/**
4600 * Sends a read command to the keyboard controller and gets the result.
4601 *
4602 * The caller is responsible for making sure the keyboard controller is ready
4603 * for a command (call #Bs3KbdWait if unsure).
4604 *
4605 * @returns The value read is returned (in al).
4606 * @param bCmd The read command.
4607 */
4608BS3_CMN_PROTO_NOSB(uint8_t, Bs3KbdRead,(uint8_t bCmd));
4609
4610/**
4611 * Sends a write command to the keyboard controller and then sends the data.
4612 *
4613 * The caller is responsible for making sure the keyboard controller is ready
4614 * for a command (call #Bs3KbdWait if unsure).
4615 *
4616 * @param bCmd The write command.
4617 * @param bData The data to write.
4618 */
4619BS3_CMN_PROTO_NOSB(void, Bs3KbdWrite,(uint8_t bCmd, uint8_t bData));
4620
4621/** @} */
4622
4623
4624/** @defgroup grp_bs3kit_pic PIC
4625 * @{
4626 */
4627
4628/**
4629 * Configures the PIC, once only.
4630 *
4631 * Subsequent calls to this function will not do anything.
4632 *
4633 * The PIC will be programmed to use IDT/IVT vectors 0x70 thru 0x7f, auto
4634 * end-of-interrupt, and all IRQs masked. The individual PIC users will have to
4635 * use #Bs3PicUpdateMask unmask their IRQ once they've got all the handlers
4636 * installed.
4637 *
4638 * @param fForcedReInit Force a reinitialization.
4639 */
4640BS3_CMN_PROTO_STUB(void, Bs3PicSetup,(bool fForcedReInit));
4641
4642/**
4643 * Updates the PIC masks.
4644 *
4645 * @returns The new mask - master in low, slave in high byte.
4646 * @param fAndMask Things to keep as-is. Master in low, slave in high byte.
4647 * @param fOrMask Things to start masking. Ditto wrt bytes.
4648 */
4649BS3_CMN_PROTO_STUB(uint16_t, Bs3PicUpdateMask,(uint16_t fAndMask, uint16_t fOrMask));
4650
4651/**
4652 * Disables all IRQs on the PIC.
4653 */
4654BS3_CMN_PROTO_STUB(void, Bs3PicMaskAll,(void));
4655
4656/** @} */
4657
4658
4659/** @defgroup grp_bs3kit_pit PIT
4660 * @{
4661 */
4662
4663/**
4664 * Sets up the PIT for periodic callback.
4665 *
4666 * @param cHzDesired The desired Hz. Zero means max interval length
4667 * (18.2Hz). Plase check the various PIT globals for
4668 * the actual interval length.
4669 */
4670BS3_CMN_PROTO_STUB(void, Bs3PitSetupAndEnablePeriodTimer,(uint16_t cHzDesired));
4671
4672/**
4673 * Disables the PIT if active.
4674 */
4675BS3_CMN_PROTO_STUB(void, Bs3PitDisable,(void));
4676
4677/** The RIP/EIP value of where the PIT IRQ handle will return to. */
4678extern BS3REG volatile g_Bs3PitIrqRip;
4679/** Nanoseconds (approx) since last the PIT timer was started. */
4680extern uint64_t volatile g_cBs3PitNs;
4681/** Milliseconds seconds (very approx) since last the PIT timer was started. */
4682extern uint64_t volatile g_cBs3PitMs;
4683/** Number of ticks since last the PIT timer was started. */
4684extern uint32_t volatile g_cBs3PitTicks;
4685/** The current interval in nanoseconds.
4686 * This is 0 if not yet started (cleared by Bs3PitDisable). */
4687extern uint32_t g_cBs3PitIntervalNs;
4688/** The current interval in milliseconds (approximately).
4689 * This is 0 if not yet started (cleared by Bs3PitDisable). */
4690extern uint16_t g_cBs3PitIntervalMs;
4691/** The current PIT frequency (approximately).
4692 * 0 if not yet started (cleared by Bs3PitDisable; used for checking the
4693 * state internally). */
4694extern uint16_t volatile g_cBs3PitIntervalHz;
4695
4696/** @} */
4697
4698/** @defgroup grp_bs3kit_disk Disk via INT 13h
4699 * @{
4700 */
4701
4702/**
4703 * Performs a int 13h function AL=08h call to get the driver parameters.
4704 *
4705 * @returns 0 on success, non-zero error BIOS code on failure.
4706 * @param bDrive The drive to get parameters for.
4707 * @param puMaxCylinder Where to store the max cylinder value.
4708 * Range: 0 thru *pcMaxCylinder.
4709 * @param puMaxHead Where to store the max head value.
4710 * Range: 0 thru *pcMaxHead.
4711 * @param puMaxSector Where to store the max sector value.
4712 * Range: 1 thru *pcMaxSector.
4713 */
4714BS3_MODE_PROTO_STUB(uint8_t, Bs3DiskQueryGeometry,(uint8_t bDrive, uint16_t *puMaxCylinder,
4715 uint8_t *puMaxHead, uint8_t *puMaxSector));
4716
4717/**
4718 * Performs a int 13h function AL=08h call to get the driver parameters.
4719 *
4720 * @returns 0 on success, non-zero error BIOS code on failure.
4721 * @param bDrive The drive to read from.
4722 * @param uCylinder The cylinder to start read at (0-max).
4723 * @param uHead The head to start reading at (0-max).
4724 * @param uSector The sector to start reading at (1-max).
4725 * @param cSectors The number of sectors to read (1+).
4726 * @param pvBuf The buffer to read into. This MUST be addressable
4727 * from real mode!
4728 */
4729BS3_MODE_PROTO_STUB(uint8_t, Bs3DiskRead,(uint8_t bDrive, uint16_t uCylinder, uint8_t uHead, uint8_t uSector,
4730 uint8_t cSectors, void RT_FAR *pvBuf));
4731
4732/** @} */
4733
4734
4735/** @} */
4736
4737RT_C_DECLS_END
4738
4739
4740/*
4741 * Include default function symbol mangling.
4742 */
4743#include "bs3kit-mangling-code.h"
4744
4745/*
4746 * Change 16-bit text segment if requested.
4747 */
4748#if defined(BS3_USE_ALT_16BIT_TEXT_SEG) && ARCH_BITS == 16 && !defined(BS3_DONT_CHANGE_TEXT_SEG)
4749# if (defined(BS3_USE_RM_TEXT_SEG) + defined(BS3_USE_X0_TEXT_SEG) + defined(BS3_USE_X1_TEXT_SEG)) != 1
4750# error "Cannot set more than one alternative 16-bit text segment!"
4751# elif defined(BS3_USE_RM_TEXT_SEG)
4752# pragma code_seg("BS3RMTEXT16", "BS3CLASS16RMCODE")
4753# elif defined(BS3_USE_X0_TEXT_SEG)
4754# pragma code_seg("BS3X0TEXT16", "BS3CLASS16X0CODE")
4755# elif defined(BS3_USE_X1_TEXT_SEG)
4756# pragma code_seg("BS3X1TEXT16", "BS3CLASS16X1CODE")
4757# else
4758# error "Huh? Which alternative text segment did you want again?"
4759# endif
4760#endif
4761
4762#endif /* !BS3KIT_INCLUDED_bs3kit_h */
4763
Note: See TracBrowser for help on using the repository browser.

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