VirtualBox

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

Last change on this file since 66450 was 66450, checked in by vboxsync, 8 years ago

IEM: More vstmxcsr tests.

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