dscr_inherit_exec_test.c 2.6 KB

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