copy_first_unaligned.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016, Chris Smart, IBM Corporation.
  4. *
  5. * Calls to copy_first which are not 128-byte aligned should be
  6. * caught and sent a SIGBUS.
  7. */
  8. #include <signal.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include "utils.h"
  12. #include "instructions.h"
  13. unsigned int expected_instruction = PPC_INST_COPY_FIRST;
  14. unsigned int instruction_mask = 0xfc2007fe;
  15. void signal_action_handler(int signal_num, siginfo_t *info, void *ptr)
  16. {
  17. ucontext_t *ctx = ptr;
  18. #ifdef __powerpc64__
  19. unsigned int *pc = (unsigned int *)ctx->uc_mcontext.gp_regs[PT_NIP];
  20. #else
  21. unsigned int *pc = (unsigned int *)ctx->uc_mcontext.uc_regs->gregs[PT_NIP];
  22. #endif
  23. /*
  24. * Check that the signal was on the correct instruction, using a
  25. * mask because the compiler assigns the register at RB.
  26. */
  27. if ((*pc & instruction_mask) == expected_instruction)
  28. _exit(0); /* We hit the right instruction */
  29. _exit(1);
  30. }
  31. void setup_signal_handler(void)
  32. {
  33. struct sigaction signal_action;
  34. memset(&signal_action, 0, sizeof(signal_action));
  35. signal_action.sa_sigaction = signal_action_handler;
  36. signal_action.sa_flags = SA_SIGINFO;
  37. sigaction(SIGBUS, &signal_action, NULL);
  38. }
  39. char cacheline_buf[128] __cacheline_aligned;
  40. int test_copy_first_unaligned(void)
  41. {
  42. /* Only run this test on a P9 or later */
  43. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
  44. /* Register our signal handler with SIGBUS */
  45. setup_signal_handler();
  46. /* +1 makes buf unaligned */
  47. copy_first(cacheline_buf+1);
  48. /* We should not get here */
  49. return 1;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. return test_harness(test_copy_first_unaligned, "test_copy_first_unaligned");
  54. }