dscr_inherit_exec_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. pid = fork();
  54. if (pid == -1) {
  55. perror("fork() failed");
  56. exit(1);
  57. } else if (pid) {
  58. int status;
  59. if (waitpid(pid, &status, 0) == -1) {
  60. perror("waitpid() failed");
  61. exit(1);
  62. }
  63. if (!WIFEXITED(status)) {
  64. fprintf(stderr, "Child didn't exit cleanly\n");
  65. exit(1);
  66. }
  67. if (WEXITSTATUS(status) != 0) {
  68. fprintf(stderr, "Child didn't exit cleanly\n");
  69. return 1;
  70. }
  71. } else {
  72. char dscr_str[16];
  73. sprintf(dscr_str, "%ld", dscr);
  74. execlp(prog, prog, "exec", dscr_str, NULL);
  75. exit(1);
  76. }
  77. }
  78. return 0;
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. if (argc == 3 && !strcmp(argv[1], "exec")) {
  83. unsigned long parent_dscr;
  84. parent_dscr = atoi(argv[2]);
  85. do_exec(parent_dscr);
  86. } else if (argc != 1) {
  87. fprintf(stderr, "Usage: %s\n", argv[0]);
  88. exit(1);
  89. }
  90. strncpy(prog, argv[0], strlen(argv[0]));
  91. return test_harness(dscr_inherit_exec, "dscr_inherit_exec_test");
  92. }