dscr_inherit_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. pid = fork();
  38. if (pid == -1) {
  39. perror("fork() failed");
  40. exit(1);
  41. } else if (pid) {
  42. int status;
  43. if (waitpid(pid, &status, 0) == -1) {
  44. perror("waitpid() failed");
  45. exit(1);
  46. }
  47. if (!WIFEXITED(status)) {
  48. fprintf(stderr, "Child didn't exit cleanly\n");
  49. exit(1);
  50. }
  51. if (WEXITSTATUS(status) != 0) {
  52. fprintf(stderr, "Child didn't exit cleanly\n");
  53. return 1;
  54. }
  55. } else {
  56. cur_dscr = get_dscr();
  57. if (cur_dscr != dscr) {
  58. fprintf(stderr, "Kernel DSCR should be %ld "
  59. "but is %ld\n", dscr, cur_dscr);
  60. exit(1);
  61. }
  62. cur_dscr_usr = get_dscr_usr();
  63. if (cur_dscr_usr != dscr) {
  64. fprintf(stderr, "User DSCR should be %ld "
  65. "but is %ld\n", dscr, cur_dscr_usr);
  66. exit(1);
  67. }
  68. exit(0);
  69. }
  70. }
  71. return 0;
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. return test_harness(dscr_inherit, "dscr_inherit_test");
  76. }