Skip to content

Commit c23b576

Browse files
committed
Hexagon (target/hexagon) probe the stores in a packet at start of commit
When a packet has 2 stores, either both commit or neither commit. At the beginning of gen_commit_packet, we check for multiple stores. If there are multiple stores, call a helper that will probe each of them before proceeding with the commit. Note that we don't call the probe helper for packets with only one store. Therefore, we call process_store_log before anything else involved in committing the packet. We also fix a typo in the comment in process_store_log. Test case added in tests/tcg/hexagon/hex_sigsegv.c Reviewed-by: Richard Henderson <[email protected]> Signed-off-by: Taylor Simpson <[email protected]> Message-Id: <[email protected]>
1 parent e3acc2c commit c23b576

File tree

5 files changed

+160
-3
lines changed

5 files changed

+160
-3
lines changed

target/hexagon/helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ DEF_HELPER_4(sffms_lib, f32, env, f32, f32, f32)
8989

9090
DEF_HELPER_3(dfmpyfix, f64, env, f64, f64)
9191
DEF_HELPER_4(dfmpyhh, f64, env, f64, f64, f64)
92+
93+
DEF_HELPER_2(probe_pkt_scalar_store_s0, void, env, int)

target/hexagon/op_helper.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,22 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,
377377
return PeV;
378378
}
379379

380+
static void probe_store(CPUHexagonState *env, int slot, int mmu_idx)
381+
{
382+
if (!(env->slot_cancelled & (1 << slot))) {
383+
size1u_t width = env->mem_log_stores[slot].width;
384+
target_ulong va = env->mem_log_stores[slot].va;
385+
uintptr_t ra = GETPC();
386+
probe_write(env, va, width, mmu_idx, ra);
387+
}
388+
}
389+
390+
/* Called during packet commit when there are two scalar stores */
391+
void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState *env, int mmu_idx)
392+
{
393+
probe_store(env, 0, mmu_idx);
394+
}
395+
380396
/*
381397
* mem_noshuf
382398
* Section 5.5 of the Hexagon V67 Programmer's Reference Manual

target/hexagon/translate.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ static void process_store_log(DisasContext *ctx, Packet *pkt)
419419
{
420420
/*
421421
* When a packet has two stores, the hardware processes
422-
* slot 1 and then slot 2. This will be important when
422+
* slot 1 and then slot 0. This will be important when
423423
* the memory accesses overlap.
424424
*/
425425
if (pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa) {
@@ -471,10 +471,42 @@ static void update_exec_counters(DisasContext *ctx, Packet *pkt)
471471

472472
static void gen_commit_packet(DisasContext *ctx, Packet *pkt)
473473
{
474+
/*
475+
* If there is more than one store in a packet, make sure they are all OK
476+
* before proceeding with the rest of the packet commit.
477+
*
478+
* dczeroa has to be the only store operation in the packet, so we go
479+
* ahead and process that first.
480+
*
481+
* When there are two scalar stores, we probe the one in slot 0.
482+
*
483+
* Note that we don't call the probe helper for packets with only one
484+
* store. Therefore, we call process_store_log before anything else
485+
* involved in committing the packet.
486+
*/
487+
bool has_store_s0 = pkt->pkt_has_store_s0;
488+
bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
489+
if (pkt->pkt_has_dczeroa) {
490+
/*
491+
* The dczeroa will be the store in slot 0, check that we don't have
492+
* a store in slot 1.
493+
*/
494+
g_assert(has_store_s0 && !has_store_s1);
495+
process_dczeroa(ctx, pkt);
496+
} else if (has_store_s0 && has_store_s1) {
497+
/*
498+
* process_store_log will execute the slot 1 store first,
499+
* so we only have to probe the store in slot 0
500+
*/
501+
TCGv mem_idx = tcg_const_tl(ctx->mem_idx);
502+
gen_helper_probe_pkt_scalar_store_s0(cpu_env, mem_idx);
503+
tcg_temp_free(mem_idx);
504+
}
505+
506+
process_store_log(ctx, pkt);
507+
474508
gen_reg_writes(ctx);
475509
gen_pred_writes(ctx, pkt);
476-
process_store_log(ctx, pkt);
477-
process_dczeroa(ctx, pkt);
478510
update_exec_counters(ctx, pkt);
479511
if (HEX_DEBUG) {
480512
TCGv has_st0 =

tests/tcg/hexagon/Makefile.target

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ first: $(HEX_SRC)/first.S
2828
$(CC) -static -mv67 -nostdlib $^ -o $@
2929

3030
HEX_TESTS = first
31+
HEX_TESTS += hex_sigsegv
3132
HEX_TESTS += misc
3233
HEX_TESTS += preg_alias
3334
HEX_TESTS += dual_stores

tests/tcg/hexagon/hex_sigsegv.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright(c) 2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
* Test the VLIW semantics of two stores in a packet
20+
*
21+
* When a packet has 2 stores, either both commit or neither commit.
22+
* We test this with a packet that does stores to both NULL and a global
23+
* variable, "should_not_change". After the SIGSEGV is caught, we check
24+
* that the "should_not_change" value is the same.
25+
*/
26+
27+
#include <stdlib.h>
28+
#include <stdio.h>
29+
#include <unistd.h>
30+
#include <sys/types.h>
31+
#include <fcntl.h>
32+
#include <setjmp.h>
33+
#include <signal.h>
34+
35+
typedef unsigned char uint8_t;
36+
37+
int err;
38+
int segv_caught;
39+
40+
#define SHOULD_NOT_CHANGE_VAL 5
41+
int should_not_change = SHOULD_NOT_CHANGE_VAL;
42+
43+
#define BUF_SIZE 300
44+
unsigned char buf[BUF_SIZE];
45+
46+
47+
static void __check(const char *filename, int line, int x, int expect)
48+
{
49+
if (x != expect) {
50+
printf("ERROR %s:%d - %d != %d\n",
51+
filename, line, x, expect);
52+
err++;
53+
}
54+
}
55+
56+
#define check(x, expect) __check(__FILE__, __LINE__, (x), (expect))
57+
58+
static void __chk_error(const char *filename, int line, int ret)
59+
{
60+
if (ret < 0) {
61+
printf("ERROR %s:%d - %d\n", filename, line, ret);
62+
err++;
63+
}
64+
}
65+
66+
#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
67+
68+
jmp_buf jmp_env;
69+
70+
static void sig_segv(int sig, siginfo_t *info, void *puc)
71+
{
72+
check(sig, SIGSEGV);
73+
segv_caught = 1;
74+
longjmp(jmp_env, 1);
75+
}
76+
77+
int main()
78+
{
79+
struct sigaction act;
80+
81+
/* SIGSEGV test */
82+
act.sa_sigaction = sig_segv;
83+
sigemptyset(&act.sa_mask);
84+
act.sa_flags = SA_SIGINFO;
85+
chk_error(sigaction(SIGSEGV, &act, NULL));
86+
if (setjmp(jmp_env) == 0) {
87+
asm volatile("r18 = ##should_not_change\n\t"
88+
"r19 = #0\n\t"
89+
"{\n\t"
90+
" memw(r18) = #7\n\t"
91+
" memw(r19) = #0\n\t"
92+
"}\n\t"
93+
: : : "r18", "r19", "memory");
94+
}
95+
96+
act.sa_handler = SIG_DFL;
97+
sigemptyset(&act.sa_mask);
98+
act.sa_flags = 0;
99+
chk_error(sigaction(SIGSEGV, &act, NULL));
100+
101+
check(segv_caught, 1);
102+
check(should_not_change, SHOULD_NOT_CHANGE_VAL);
103+
104+
puts(err ? "FAIL" : "PASS");
105+
return err ? EXIT_FAILURE : EXIT_SUCCESS;
106+
}

0 commit comments

Comments
 (0)