Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2016, Chris Smart, IBM Corporation.
0004  *
0005  * Calls to copy_first which are not 128-byte aligned should be
0006  * caught and sent a SIGBUS.
0007  */
0008 
0009 #include <signal.h>
0010 #include <string.h>
0011 #include <unistd.h>
0012 #include "utils.h"
0013 #include "instructions.h"
0014 
0015 unsigned int expected_instruction = PPC_INST_COPY_FIRST;
0016 unsigned int instruction_mask = 0xfc2007fe;
0017 
0018 void signal_action_handler(int signal_num, siginfo_t *info, void *ptr)
0019 {
0020     ucontext_t *ctx = ptr;
0021 #ifdef __powerpc64__
0022     unsigned int *pc = (unsigned int *)ctx->uc_mcontext.gp_regs[PT_NIP];
0023 #else
0024     unsigned int *pc = (unsigned int *)ctx->uc_mcontext.uc_regs->gregs[PT_NIP];
0025 #endif
0026 
0027     /*
0028      * Check that the signal was on the correct instruction, using a
0029      * mask because the compiler assigns the register at RB.
0030      */
0031     if ((*pc & instruction_mask) == expected_instruction)
0032         _exit(0); /* We hit the right instruction */
0033 
0034     _exit(1);
0035 }
0036 
0037 void setup_signal_handler(void)
0038 {
0039     struct sigaction signal_action;
0040 
0041     memset(&signal_action, 0, sizeof(signal_action));
0042     signal_action.sa_sigaction = signal_action_handler;
0043     signal_action.sa_flags = SA_SIGINFO;
0044     sigaction(SIGBUS, &signal_action, NULL);
0045 }
0046 
0047 char cacheline_buf[128] __cacheline_aligned;
0048 
0049 int test_copy_first_unaligned(void)
0050 {
0051     /* Only run this test on a P9 or later */
0052     SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
0053 
0054     /* Register our signal handler with SIGBUS */
0055     setup_signal_handler();
0056 
0057     /* +1 makes buf unaligned */
0058     copy_first(cacheline_buf+1);
0059 
0060     /* We should not get here */
0061     return 1;
0062 }
0063 
0064 int main(int argc, char *argv[])
0065 {
0066     return test_harness(test_copy_first_unaligned, "test_copy_first_unaligned");
0067 }