1 /*
2  * Copyright 2016, Chris Smart, IBM Corporation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  * Calls to copy_first which are not 128-byte aligned should be
10  * caught and sent a SIGBUS.
11  *
12  */
13 
14 #include <signal.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include "utils.h"
18 #include "instructions.h"
19 
20 unsigned int expected_instruction = PPC_INST_COPY_FIRST;
21 unsigned int instruction_mask = 0xfc2007fe;
22 
23 void signal_action_handler(int signal_num, siginfo_t *info, void *ptr)
24 {
25 	ucontext_t *ctx = ptr;
26 #ifdef __powerpc64__
27 	unsigned int *pc = (unsigned int *)ctx->uc_mcontext.gp_regs[PT_NIP];
28 #else
29 	unsigned int *pc = (unsigned int *)ctx->uc_mcontext.uc_regs->gregs[PT_NIP];
30 #endif
31 
32 	/*
33 	 * Check that the signal was on the correct instruction, using a
34 	 * mask because the compiler assigns the register at RB.
35 	 */
36 	if ((*pc & instruction_mask) == expected_instruction)
37 		_exit(0); /* We hit the right instruction */
38 
39 	_exit(1);
40 }
41 
42 void setup_signal_handler(void)
43 {
44 	struct sigaction signal_action;
45 
46 	memset(&signal_action, 0, sizeof(signal_action));
47 	signal_action.sa_sigaction = signal_action_handler;
48 	signal_action.sa_flags = SA_SIGINFO;
49 	sigaction(SIGBUS, &signal_action, NULL);
50 }
51 
52 char cacheline_buf[128] __cacheline_aligned;
53 
54 int test_copy_first_unaligned(void)
55 {
56 	/* Only run this test on a P9 or later */
57 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
58 
59 	/* Register our signal handler with SIGBUS */
60 	setup_signal_handler();
61 
62 	/* +1 makes buf unaligned */
63 	copy_first(cacheline_buf+1);
64 
65 	/* We should not get here */
66 	return 1;
67 }
68 
69 int main(int argc, char *argv[])
70 {
71 	return test_harness(test_copy_first_unaligned, "test_copy_first_unaligned");
72 }
73