cp_abort.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Adapted from Anton Blanchard's context switch microbenchmark.
  3. *
  4. * Copyright 2009, Anton Blanchard, IBM Corporation.
  5. * Copyright 2016, Mikey Neuling, Chris Smart, IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * This program tests the copy paste abort functionality of a P9
  13. * (or later) by setting up two processes on the same CPU, one
  14. * which executes the copy instruction and the other which
  15. * executes paste.
  16. *
  17. * The paste instruction should never succeed, as the cp_abort
  18. * instruction is called by the kernel during a context switch.
  19. *
  20. */
  21. #define _GNU_SOURCE
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include "utils.h"
  26. #include <sched.h>
  27. #define READ_FD 0
  28. #define WRITE_FD 1
  29. #define NUM_LOOPS 1000
  30. /* This defines the "paste" instruction from Power ISA 3.0 Book II, section 4.4. */
  31. #define PASTE(RA, RB, L, RC) \
  32. .long (0x7c00070c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10) | (RC) << (31-31))
  33. int paste(void *i)
  34. {
  35. int cr;
  36. asm volatile(str(PASTE(0, %1, 1, 1))";"
  37. "mfcr %0;"
  38. : "=r" (cr)
  39. : "b" (i)
  40. : "memory"
  41. );
  42. return cr;
  43. }
  44. /* This defines the "copy" instruction from Power ISA 3.0 Book II, section 4.4. */
  45. #define COPY(RA, RB, L) \
  46. .long (0x7c00060c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10))
  47. void copy(void *i)
  48. {
  49. asm volatile(str(COPY(0, %0, 1))";"
  50. :
  51. : "b" (i)
  52. : "memory"
  53. );
  54. }
  55. int test_cp_abort(void)
  56. {
  57. /* 128 bytes for a full cache line */
  58. char buf[128] __cacheline_aligned;
  59. cpu_set_t cpuset;
  60. int fd1[2], fd2[2], pid;
  61. char c;
  62. /* only run this test on a P9 or later */
  63. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
  64. /*
  65. * Run both processes on the same CPU, so that copy is more likely
  66. * to leak into a paste.
  67. */
  68. CPU_ZERO(&cpuset);
  69. CPU_SET(pick_online_cpu(), &cpuset);
  70. FAIL_IF(sched_setaffinity(0, sizeof(cpuset), &cpuset));
  71. FAIL_IF(pipe(fd1) || pipe(fd2));
  72. pid = fork();
  73. FAIL_IF(pid < 0);
  74. if (!pid) {
  75. for (int i = 0; i < NUM_LOOPS; i++) {
  76. FAIL_IF((write(fd1[WRITE_FD], &c, 1)) != 1);
  77. FAIL_IF((read(fd2[READ_FD], &c, 1)) != 1);
  78. /* A paste succeeds if CR0 EQ bit is set */
  79. FAIL_IF(paste(buf) & 0x20000000);
  80. }
  81. } else {
  82. for (int i = 0; i < NUM_LOOPS; i++) {
  83. FAIL_IF((read(fd1[READ_FD], &c, 1)) != 1);
  84. copy(buf);
  85. FAIL_IF((write(fd2[WRITE_FD], &c, 1) != 1));
  86. }
  87. }
  88. return 0;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. return test_harness(test_cp_abort, "cp_abort");
  93. }