12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
243750880SChris Smart /*
343750880SChris Smart  * Copyright 2016, Chris Smart, IBM Corporation.
443750880SChris Smart  *
543750880SChris Smart  * Calls to copy_first which are not 128-byte aligned should be
643750880SChris Smart  * caught and sent a SIGBUS.
743750880SChris Smart  */
843750880SChris Smart 
9dbc3f77cSMichael Ellerman #include <signal.h>
1043750880SChris Smart #include <string.h>
1143750880SChris Smart #include <unistd.h>
1243750880SChris Smart #include "utils.h"
1343750880SChris Smart #include "instructions.h"
1443750880SChris Smart 
1543750880SChris Smart unsigned int expected_instruction = PPC_INST_COPY_FIRST;
1643750880SChris Smart unsigned int instruction_mask = 0xfc2007fe;
1743750880SChris Smart 
signal_action_handler(int signal_num,siginfo_t * info,void * ptr)18dbc3f77cSMichael Ellerman void signal_action_handler(int signal_num, siginfo_t *info, void *ptr)
19dbc3f77cSMichael Ellerman {
20dbc3f77cSMichael Ellerman 	ucontext_t *ctx = ptr;
21dbc3f77cSMichael Ellerman #ifdef __powerpc64__
22dbc3f77cSMichael Ellerman 	unsigned int *pc = (unsigned int *)ctx->uc_mcontext.gp_regs[PT_NIP];
23dbc3f77cSMichael Ellerman #else
24dbc3f77cSMichael Ellerman 	unsigned int *pc = (unsigned int *)ctx->uc_mcontext.uc_regs->gregs[PT_NIP];
25dbc3f77cSMichael Ellerman #endif
26dbc3f77cSMichael Ellerman 
27dbc3f77cSMichael Ellerman 	/*
28dbc3f77cSMichael Ellerman 	 * Check that the signal was on the correct instruction, using a
29dbc3f77cSMichael Ellerman 	 * mask because the compiler assigns the register at RB.
30dbc3f77cSMichael Ellerman 	 */
31dbc3f77cSMichael Ellerman 	if ((*pc & instruction_mask) == expected_instruction)
32dbc3f77cSMichael Ellerman 		_exit(0); /* We hit the right instruction */
33dbc3f77cSMichael Ellerman 
34dbc3f77cSMichael Ellerman 	_exit(1);
35dbc3f77cSMichael Ellerman }
36dbc3f77cSMichael Ellerman 
setup_signal_handler(void)37dbc3f77cSMichael Ellerman void setup_signal_handler(void)
38dbc3f77cSMichael Ellerman {
39dbc3f77cSMichael Ellerman 	struct sigaction signal_action;
40dbc3f77cSMichael Ellerman 
41dbc3f77cSMichael Ellerman 	memset(&signal_action, 0, sizeof(signal_action));
42dbc3f77cSMichael Ellerman 	signal_action.sa_sigaction = signal_action_handler;
43dbc3f77cSMichael Ellerman 	signal_action.sa_flags = SA_SIGINFO;
44dbc3f77cSMichael Ellerman 	sigaction(SIGBUS, &signal_action, NULL);
45dbc3f77cSMichael Ellerman }
46dbc3f77cSMichael Ellerman 
47dbc3f77cSMichael Ellerman char cacheline_buf[128] __cacheline_aligned;
48dbc3f77cSMichael Ellerman 
test_copy_first_unaligned(void)4943750880SChris Smart int test_copy_first_unaligned(void)
5043750880SChris Smart {
5143750880SChris Smart 	/* Only run this test on a P9 or later */
5243750880SChris Smart 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
5343750880SChris Smart 
5443750880SChris Smart 	/* Register our signal handler with SIGBUS */
5543750880SChris Smart 	setup_signal_handler();
5643750880SChris Smart 
5743750880SChris Smart 	/* +1 makes buf unaligned */
5843750880SChris Smart 	copy_first(cacheline_buf+1);
5943750880SChris Smart 
6043750880SChris Smart 	/* We should not get here */
6143750880SChris Smart 	return 1;
6243750880SChris Smart }
6343750880SChris Smart 
main(int argc,char * argv[])6443750880SChris Smart int main(int argc, char *argv[])
6543750880SChris Smart {
6643750880SChris Smart 	return test_harness(test_copy_first_unaligned, "test_copy_first_unaligned");
6743750880SChris Smart }
68