summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/bootsectors/bs3-cpu-state64-1.c64
blob: 3f53ed90c4580ce9f40470155ba6fb7a0725ef0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* $Id: bs3-cpu-state64-1.c64 $ */
/** @file
 * BS3Kit - bs3-cpu-state64-1, 64-bit C code.
 */

/*
 * Copyright (C) 2007-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <bs3kit.h>
#include <iprt/asm-amd64-x86.h>


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
BS3_DECL(void) bs3CpuState64Worker_Nop(void);
BS3_DECL(void) bs3CpuState64Worker_ModAll32BitGrps(void);
BS3_DECL(void) bs3CpuState64Worker_ModAll16BitGrps(void);
BS3_DECL(void) bs3CpuState64Worker_ModAll8BitGrps(void);
BS3_DECL(void) bs3CpuState64Worker_ModCr2(void);
BS3_DECL(void) bs3CpuState64Worker(PFNBS3NEAR pfnWorker);


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
extern BS3REGCTX        g_bs3CpuState64CtxToLoad;
extern BS3REGCTX const  g_bs3CpuState64CtxSaved;


static void bs3CpuState64InitCtx(uint64_t uTweak)
{
    Bs3RegCtxSave(&g_bs3CpuState64CtxToLoad);
    g_bs3CpuState64CtxToLoad.rax.u = UINT64_C(0x8877f055443322e0) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rcx.u = UINT64_C(0x8877f155443322e1) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rdx.u = UINT64_C(0x8877f255443322e2) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rbx.u = UINT64_C(0x8877f355443322e3) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rsp.u = UINT64_C(0x8877f455443322e4) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rbp.u = UINT64_C(0x8877f555443322e5) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rsi.u = UINT64_C(0x8877f655443322e6) ^ uTweak;
    g_bs3CpuState64CtxToLoad.rdi.u = UINT64_C(0x8877f755443322e7) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r8.u  = UINT64_C(0x8877f855443322e8) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r9.u  = UINT64_C(0x8877f955443322e9) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r10.u = UINT64_C(0x8877fa55443322ea) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r11.u = UINT64_C(0x8877fb55443322eb) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r12.u = UINT64_C(0x8877fc55443322ec) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r13.u = UINT64_C(0x8877fd55443322ed) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r14.u = UINT64_C(0x8877fe55443322ee) ^ uTweak;
    g_bs3CpuState64CtxToLoad.r15.u = UINT64_C(0x8877ff55443322ef) ^ uTweak;
    g_bs3CpuState64CtxToLoad.cr2.u = UINT64_C(0x89abcdef76543210) ^ uTweak;
    //Bs3TestPrintf("** Input:\n");
    //Bs3RegCtxPrint(&g_bs3CpuState64CtxToLoad);
}


static void bs3CpuState64Comp(bool fModRbx)
{
    //Bs3TestPrintf("** Result:\n");
    //Bs3RegCtxPrint(&g_bs3CpuState64CtxSaved);
    g_bs3CpuState64CtxToLoad.rax         = g_bs3CpuState64CtxSaved.rax;
    g_bs3CpuState64CtxToLoad.rip         = g_bs3CpuState64CtxSaved.rip;
    g_bs3CpuState64CtxToLoad.rflags      = g_bs3CpuState64CtxSaved.rflags;
    g_bs3CpuState64CtxToLoad.rsp         = g_bs3CpuState64CtxSaved.rsp;
    if (!fModRbx)
        g_bs3CpuState64CtxToLoad.rbx.au16[0] = g_bs3CpuState64CtxSaved.rbx.au16[0];
    if (1)
        g_bs3CpuState64CtxToLoad.ds      = g_bs3CpuState64CtxSaved.ds;

    Bs3TestCheckRegCtxEx(&g_bs3CpuState64CtxSaved, &g_bs3CpuState64CtxToLoad, 0 /*cbPcAdjust*/, 0 /*cbSpAdjust*/,
                         0 /*fExtraEfl*/, "lm64", 0 /*idTestStep*/);
}

BS3_DECL(void) Main_lm64()
{
    Bs3TestInit("bs3-cpu-state64-1");
    /*
     * Switch to 64-bit mode and back to rm.
     */
    Bs3TestSub("noop");
    bs3CpuState64InitCtx(0);
    bs3CpuState64Worker(bs3CpuState64Worker_Nop);
    bs3CpuState64Comp(false /*fModRbx*/);

    Bs3TestSub("Modify all 32-bit GPRs");
    bs3CpuState64InitCtx(0);
    bs3CpuState64Worker(bs3CpuState64Worker_ModAll32BitGrps);
    g_bs3CpuState64CtxToLoad.rax.u = UINT64_C(0xc0ffee0d);
    g_bs3CpuState64CtxToLoad.rcx.u = UINT64_C(0xc0ffee1d);
    g_bs3CpuState64CtxToLoad.rdx.u = UINT64_C(0xc0ffee2d);
    g_bs3CpuState64CtxToLoad.rbx.u = UINT64_C(0xc0ffee3d);
    g_bs3CpuState64CtxToLoad.rsp.u = UINT64_C(0xc0ffee4d);
    g_bs3CpuState64CtxToLoad.rbp.u = UINT64_C(0xc0ffee5d);
    g_bs3CpuState64CtxToLoad.rsi.u = UINT64_C(0xc0ffee6d);
    g_bs3CpuState64CtxToLoad.rdi.u = UINT64_C(0xc0ffee7d);
    bs3CpuState64Comp(true /*fModRbx*/);

    Bs3TestSub("Modify all 16-bit GPRs");
    bs3CpuState64InitCtx(0);
    bs3CpuState64Worker(bs3CpuState64Worker_ModAll16BitGrps);
    g_bs3CpuState64CtxToLoad.rax.au16[0] = UINT16_C(0xfad0);
    g_bs3CpuState64CtxToLoad.rcx.au16[0] = UINT16_C(0xfad1);
    g_bs3CpuState64CtxToLoad.rdx.au16[0] = UINT16_C(0xfad2);
    g_bs3CpuState64CtxToLoad.rbx.au16[0] = UINT16_C(0xfad3);
    g_bs3CpuState64CtxToLoad.rsp.au16[0] = UINT16_C(0xfad4);
    g_bs3CpuState64CtxToLoad.rbp.au16[0] = UINT16_C(0xfad5);
    g_bs3CpuState64CtxToLoad.rsi.au16[0] = UINT16_C(0xfad6);
    g_bs3CpuState64CtxToLoad.rdi.au16[0] = UINT16_C(0xfad7);
    bs3CpuState64Comp(true /*fModRbx*/);

    Bs3TestSub("Modify all 8-bit GPRs");
    bs3CpuState64InitCtx(0);
    bs3CpuState64Worker(bs3CpuState64Worker_ModAll8BitGrps);
    g_bs3CpuState64CtxToLoad.rax.au8[0] = 0x10;
    g_bs3CpuState64CtxToLoad.rax.au8[1] = 0x11;
    g_bs3CpuState64CtxToLoad.rcx.au8[0] = 0x20;
    g_bs3CpuState64CtxToLoad.rcx.au8[1] = 0x21;
    g_bs3CpuState64CtxToLoad.rdx.au8[0] = 0x30;
    g_bs3CpuState64CtxToLoad.rdx.au8[1] = 0x31;
    g_bs3CpuState64CtxToLoad.rbx.au8[0] = 0x40;
    g_bs3CpuState64CtxToLoad.rbx.au8[1] = 0x41;
    bs3CpuState64Comp(true /*fModRbx*/);

    Bs3TestSub("Modify CR2");
    bs3CpuState64InitCtx(0);
    bs3CpuState64Worker(bs3CpuState64Worker_ModCr2);
    g_bs3CpuState64CtxToLoad.cr2.u = 0xf00dface;
    bs3CpuState64Comp(true /*fModRbx*/);

        /** @todo DRx */
    Bs3TestTerm();
    for (;;)
        ASMHalt();
}