copy_first_unaligned.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <signal.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include "utils.h"
  17. #include "instructions.h"
  18. unsigned int expected_instruction = PPC_INST_COPY_FIRST;
  19. unsigned int instruction_mask = 0xfc2007fe;
  20. void signal_action_handler(int signal_num, siginfo_t *info, void *ptr)
  21. {
  22. ucontext_t *ctx = ptr;
  23. #ifdef __powerpc64__
  24. unsigned int *pc = (unsigned int *)ctx->uc_mcontext.gp_regs[PT_NIP];
  25. #else
  26. unsigned int *pc = (unsigned int *)ctx->uc_mcontext.uc_regs->gregs[PT_NIP];
  27. #endif
  28. /*
  29. * Check that the signal was on the correct instruction, using a
  30. * mask because the compiler assigns the register at RB.
  31. */
  32. if ((*pc & instruction_mask) == expected_instruction)
  33. _exit(0); /* We hit the right instruction */
  34. _exit(1);
  35. }
  36. void setup_signal_handler(void)
  37. {
  38. struct sigaction signal_action;
  39. memset(&signal_action, 0, sizeof(signal_action));
  40. signal_action.sa_sigaction = signal_action_handler;
  41. signal_action.sa_flags = SA_SIGINFO;
  42. sigaction(SIGBUS, &signal_action, NULL);
  43. }
  44. char cacheline_buf[128] __cacheline_aligned;
  45. int test_copy_first_unaligned(void)
  46. {
  47. /* Only run this test on a P9 or later */
  48. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
  49. /* Register our signal handler with SIGBUS */
  50. setup_signal_handler();
  51. /* +1 makes buf unaligned */
  52. copy_first(cacheline_buf+1);
  53. /* We should not get here */
  54. return 1;
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. return test_harness(test_copy_first_unaligned, "test_copy_first_unaligned");
  59. }