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
|
/*
* Copyright(c) 2021-2023 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 <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
int err;
#include "hex_test.h"
static int32_t satub(int32_t src, int32_t *p, bool *ovf_result)
{
int32_t result;
uint32_t usr;
/*
* This instruction can set bit 0 (OVF/overflow) in usr
* Clear the bit first, then return that bit to the caller
*
* We also store the src into *p in the same packet, so we
* can ensure the overflow doesn't get set when an exception
* is generated.
*/
asm volatile("r2 = usr\n\t"
"r2 = clrbit(r2, #0)\n\t" /* clear overflow bit */
"usr = r2\n\t"
"{\n\t"
" %0 = satub(%2)\n\t"
" memw(%3) = %2\n\t"
"}\n\t"
"%1 = usr\n\t"
: "=r"(result), "=r"(usr)
: "r"(src), "r"(p)
: "r2", "usr", "memory");
*ovf_result = (usr & 1);
return result;
}
bool read_usr_overflow(void)
{
uint32_t usr;
asm volatile("%0 = usr\n\t" : "=r"(usr));
return usr & 1;
}
bool get_usr_overflow(uint32_t usr)
{
return usr & 1;
}
bool get_usr_fp_invalid(uint32_t usr)
{
return (usr >> 1) & 1;
}
int32_t get_usr_lpcfg(uint32_t usr)
{
return (usr >> 8) & 0x3;
}
jmp_buf jmp_env;
bool usr_overflow;
static void sig_segv(int sig, siginfo_t *info, void *puc)
{
usr_overflow = read_usr_overflow();
longjmp(jmp_env, 1);
}
static void test_packet(void)
{
int32_t convres;
int32_t satres;
uint32_t usr;
asm("r2 = usr\n\t"
"r2 = clrbit(r2, #0)\n\t" /* clear overflow bit */
"r2 = clrbit(r2, #1)\n\t" /* clear FP invalid bit */
"usr = r2\n\t"
"{\n\t"
" %0 = convert_sf2uw(%3):chop\n\t"
" %1 = satb(%4)\n\t"
"}\n\t"
"%2 = usr\n\t"
: "=r"(convres), "=r"(satres), "=r"(usr)
: "r"(0x6a051b86), "r"(0x0410eec0)
: "r2", "usr");
check32(convres, 0xffffffff);
check32(satres, 0x7f);
check32(get_usr_overflow(usr), true);
check32(get_usr_fp_invalid(usr), true);
asm("r2 = usr\n\t"
"r2 = clrbit(r2, #0)\n\t" /* clear overflow bit */
"usr = r2\n\t"
"%2 = r2\n\t"
"p3 = sp3loop0(1f, #1)\n\t"
"1:\n\t"
"{\n\t"
" %0 = satb(%2)\n\t"
"}:endloop0\n\t"
"%1 = usr\n\t"
: "=r"(satres), "=r"(usr)
: "r"(0x0410eec0)
: "r2", "usr", "p3", "sa0", "lc0");
check32(satres, 0x7f);
check32(get_usr_overflow(usr), true);
check32(get_usr_lpcfg(usr), 2);
}
int main()
{
struct sigaction act;
bool ovf;
/* SIGSEGV test */
act.sa_sigaction = sig_segv;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &act, NULL);
if (setjmp(jmp_env) == 0) {
satub(300, 0, &ovf);
}
act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
check32(usr_overflow, false);
test_packet();
puts(err ? "FAIL" : "PASS");
return err ? EXIT_FAILURE : EXIT_SUCCESS;
}
|