aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/hexagon/reg_mut.c
blob: 910e663ace73a96840e6c26e3d8ace3183f87cf5 (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

/*
 *  Copyright(c) 2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>

static int err;

#define check(N, EXPECT) \
    do { \
        uint64_t value = N; \
        uint64_t expect = EXPECT; \
        if (value != EXPECT) { \
            printf("ERROR: \"%s\" 0x%04llx != 0x%04llx at %s:%d\n", #N, value, \
                   expect, __FILE__, __LINE__); \
            err++; \
        } \
    } while (0)

#define check_ne(N, EXPECT) \
    do { \
        uint64_t value = N; \
        uint64_t expect = EXPECT; \
        if (value == EXPECT) { \
            printf("ERROR: \"%s\" 0x%04llx == 0x%04llx at %s:%d\n", #N, value, \
                   expect, __FILE__, __LINE__); \
            err++; \
        } \
    } while (0)

#define WRITE_REG_NOCLOBBER(output, reg_name, input) \
    asm volatile(reg_name " = %1\n\t" \
                 "%0 = " reg_name "\n\t" \
                 : "=r"(output) \
                 : "r"(input) \
                 : );

#define WRITE_REG_ENCODED(output, reg_name, input, encoding) \
    asm volatile("r0 = %1\n\t" \
                 encoding "\n\t" \
                 "%0 = " reg_name "\n\t" \
                 : "=r"(output) \
                 : "r"(input) \
                 : "r0");

#define WRITE_REG_PAIR_ENCODED(output, reg_name, input, encoding) \
    asm volatile("r1:0 = %1\n\t" \
                 encoding "\n\t" \
                 "%0 = " reg_name "\n\t" \
                 : "=r"(output) \
                 : "r"(input) \
                 : "r1:0");

/*
 * Instruction word: { pc = r0 }
 *
 * This instruction is barred by the assembler.
 *
 *    3                   2                   1
 *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |    Opc[A2_tfrrcr]   | Src[R0] |P P|                 |  C9/PC  |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
#define PC_EQ_R0        ".word 0x6220c009"
#define C9_8_EQ_R1_0    ".word 0x6320c008"

static inline void write_control_registers(void)
{
    uint32_t result = 0;

    WRITE_REG_NOCLOBBER(result, "usr", 0xffffffff);
    check(result, 0x3ecfff3f);

    WRITE_REG_NOCLOBBER(result, "gp", 0xffffffff);
    check(result, 0xffffffc0);

    WRITE_REG_NOCLOBBER(result, "upcyclelo", 0xffffffff);
    check(result, 0x00000000);

    WRITE_REG_NOCLOBBER(result, "upcyclehi", 0xffffffff);
    check(result, 0x00000000);

    WRITE_REG_NOCLOBBER(result, "utimerlo", 0xffffffff);
    check(result, 0x00000000);

    WRITE_REG_NOCLOBBER(result, "utimerhi", 0xffffffff);
    check(result, 0x00000000);

    /*
     * PC is special.  Setting it to these values
     * should cause a catastrophic failure.
     */
    WRITE_REG_ENCODED(result, "pc", 0x00000000, PC_EQ_R0);
    check_ne(result, 0x00000000);

    WRITE_REG_ENCODED(result, "pc", 0x00000001, PC_EQ_R0);
    check_ne(result, 0x00000001);

    WRITE_REG_ENCODED(result, "pc", 0xffffffff, PC_EQ_R0);
    check_ne(result, 0xffffffff);
}

static inline void write_control_register_pairs(void)
{
    uint64_t result = 0;

    WRITE_REG_NOCLOBBER(result, "c11:10", 0xffffffffffffffff);
    check(result, 0xffffffc0ffffffff);

    WRITE_REG_NOCLOBBER(result, "c15:14", 0xffffffffffffffff);
    check(result, 0x0000000000000000);

    WRITE_REG_NOCLOBBER(result, "c31:30", 0xffffffffffffffff);
    check(result, 0x0000000000000000);

    WRITE_REG_PAIR_ENCODED(result, "c9:8", (uint64_t) 0x0000000000000000,
                           C9_8_EQ_R1_0);
    check_ne(result, 0x000000000000000);

    WRITE_REG_PAIR_ENCODED(result, "c9:8", 0x0000000100000000, C9_8_EQ_R1_0);
    check_ne(result, 0x0000000100000000);

    WRITE_REG_PAIR_ENCODED(result, "c9:8", 0xffffffffffffffff, C9_8_EQ_R1_0);
    check_ne(result, 0xffffffffffffffff);
}

int main()
{
    err = 0;

    write_control_registers();
    write_control_register_pairs();

    puts(err ? "FAIL" : "PASS");
    return err;
}