flush_utils.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 IBM Corporation.
  4. */
  5. #define __SANE_USERSPACE_TYPES__
  6. #include <sys/types.h>
  7. #include <stdint.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <sys/utsname.h>
  14. #include "reg.h"
  15. #include "utils.h"
  16. #include "flush_utils.h"
  17. static inline __u64 load(void *addr)
  18. {
  19. __u64 tmp;
  20. asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr));
  21. return tmp;
  22. }
  23. void syscall_loop(char *p, unsigned long iterations,
  24. unsigned long zero_size)
  25. {
  26. for (unsigned long i = 0; i < iterations; i++) {
  27. for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE)
  28. load(p + j);
  29. getppid();
  30. }
  31. }
  32. void syscall_loop_uaccess(char *p, unsigned long iterations,
  33. unsigned long zero_size)
  34. {
  35. struct utsname utsname;
  36. for (unsigned long i = 0; i < iterations; i++) {
  37. for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE)
  38. load(p + j);
  39. uname(&utsname);
  40. }
  41. }
  42. static void sigill_handler(int signr, siginfo_t *info, void *unused)
  43. {
  44. static int warned;
  45. ucontext_t *ctx = (ucontext_t *)unused;
  46. unsigned long *pc = &UCONTEXT_NIA(ctx);
  47. /* mtspr 3,RS to check for move to DSCR below */
  48. if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) {
  49. if (!warned++)
  50. printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n");
  51. *pc += 4;
  52. } else {
  53. printf("SIGILL at %p\n", pc);
  54. abort();
  55. }
  56. }
  57. void set_dscr(unsigned long val)
  58. {
  59. static int init;
  60. struct sigaction sa;
  61. if (!init) {
  62. memset(&sa, 0, sizeof(sa));
  63. sa.sa_sigaction = sigill_handler;
  64. sa.sa_flags = SA_SIGINFO;
  65. if (sigaction(SIGILL, &sa, NULL))
  66. perror("sigill_handler");
  67. init = 1;
  68. }
  69. mtspr(SPRN_DSCR, val);
  70. }