dscr_inherit_test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * POWER Data Stream Control Register (DSCR) fork test
  3. *
  4. * This testcase modifies the DSCR using mtspr, forks and then
  5. * verifies that the child process has the correct changed DSCR
  6. * value using mfspr.
  7. *
  8. * When using the privilege state SPR, the instructions such as
  9. * mfspr or mtspr are priviledged and the kernel emulates them
  10. * for us. Instructions using problem state SPR can be exuecuted
  11. * directly without any emulation if the HW supports them. Else
  12. * they also get emulated by the kernel.
  13. *
  14. * Copyright 2012, Anton Blanchard, IBM Corporation.
  15. * Copyright 2015, Anshuman Khandual, IBM Corporation.
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License version 2 as published
  19. * by the Free Software Foundation.
  20. */
  21. #include "dscr.h"
  22. int dscr_inherit(void)
  23. {
  24. unsigned long i, dscr = 0;
  25. pid_t pid;
  26. srand(getpid());
  27. set_dscr(dscr);
  28. for (i = 0; i < COUNT; i++) {
  29. unsigned long cur_dscr, cur_dscr_usr;
  30. dscr++;
  31. if (dscr > DSCR_MAX)
  32. dscr = 0;
  33. if (i % 2 == 0)
  34. set_dscr_usr(dscr);
  35. else
  36. set_dscr(dscr);
  37. /*
  38. * XXX: Force a context switch out so that DSCR
  39. * current value is copied into the thread struct
  40. * which is required for the child to inherit the
  41. * changed value.
  42. */
  43. sleep(1);
  44. pid = fork();
  45. if (pid == -1) {
  46. perror("fork() failed");
  47. exit(1);
  48. } else if (pid) {
  49. int status;
  50. if (waitpid(pid, &status, 0) == -1) {
  51. perror("waitpid() failed");
  52. exit(1);
  53. }
  54. if (!WIFEXITED(status)) {
  55. fprintf(stderr, "Child didn't exit cleanly\n");
  56. exit(1);
  57. }
  58. if (WEXITSTATUS(status) != 0) {
  59. fprintf(stderr, "Child didn't exit cleanly\n");
  60. return 1;
  61. }
  62. } else {
  63. cur_dscr = get_dscr();
  64. if (cur_dscr != dscr) {
  65. fprintf(stderr, "Kernel DSCR should be %ld "
  66. "but is %ld\n", dscr, cur_dscr);
  67. exit(1);
  68. }
  69. cur_dscr_usr = get_dscr_usr();
  70. if (cur_dscr_usr != dscr) {
  71. fprintf(stderr, "User DSCR should be %ld "
  72. "but is %ld\n", dscr, cur_dscr_usr);
  73. exit(1);
  74. }
  75. exit(0);
  76. }
  77. }
  78. return 0;
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. return test_harness(dscr_inherit, "dscr_inherit_test");
  83. }