shim_sched.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_sched.c
  15. *
  16. * Implementation of system calls "sched_yield", "setpriority", "getpriority",
  17. * "sched_setparam", "sched_getparam", "sched_setscheduler", "sched_getscheduler",
  18. * "sched_get_priority_max", "sched_get_priority_min", "sched_rr_get_interval",
  19. * "sched_setaffinity", "sched_getaffinity".
  20. */
  21. #include <api.h>
  22. #include <errno.h>
  23. #include <linux/resource.h>
  24. #include <linux/sched.h>
  25. #include <pal.h>
  26. #include <shim_internal.h>
  27. #include <shim_table.h>
  28. int shim_do_sched_yield(void) {
  29. DkThreadYieldExecution();
  30. return 0;
  31. }
  32. /* dummy implementation: ignore user-supplied niceval and return success */
  33. int shim_do_setpriority(int which, int who, int niceval) {
  34. __UNUSED(who);
  35. if (which != PRIO_PROCESS && which != PRIO_PGRP && which != PRIO_USER)
  36. return -EINVAL;
  37. if (niceval < 1 || niceval > 40)
  38. return -EACCES;
  39. return 0;
  40. }
  41. /* dummy implementation: always return the default nice value of 0 */
  42. int shim_do_getpriority(int which, int who) {
  43. __UNUSED(who);
  44. if (which != PRIO_PROCESS && which != PRIO_PGRP && which != PRIO_USER)
  45. return -EINVAL;
  46. return 20; /* default nice value on Linux */
  47. }
  48. /* dummy implementation: ignore user-supplied param and return success */
  49. int shim_do_sched_setparam(pid_t pid, struct __kernel_sched_param* param) {
  50. if (pid < 0 || param == NULL)
  51. return -EINVAL;
  52. return 0;
  53. }
  54. /* dummy implementation: always return sched_priority of 0 (implies non-real-time sched policy) */
  55. int shim_do_sched_getparam(pid_t pid, struct __kernel_sched_param* param) {
  56. if (pid < 0 || param == NULL)
  57. return -EINVAL;
  58. param->__sched_priority = 0;
  59. return 0;
  60. }
  61. /* dummy implementation: ignore user-supplied policy & param and return success */
  62. int shim_do_sched_setscheduler(pid_t pid, int policy, struct __kernel_sched_param* param) {
  63. policy &= ~SCHED_RESET_ON_FORK; /* ignore reset-on-fork flag */
  64. if (pid < 0 || param == NULL)
  65. return -EINVAL;
  66. /* fail on unrecognized policies */
  67. if (policy != SCHED_NORMAL && policy != SCHED_BATCH && policy != SCHED_IDLE && /* non-real-time */
  68. policy != SCHED_FIFO && policy != SCHED_RR /* real-time */)
  69. return -EINVAL;
  70. /* non-real-time policies must have priority of 0 */
  71. if ((policy == SCHED_NORMAL || policy == SCHED_BATCH || policy == SCHED_IDLE) &&
  72. (param->__sched_priority != 0))
  73. return -EINVAL;
  74. /* real-time policies must have priority in range [1, 99] */
  75. if ((policy == SCHED_FIFO || policy == SCHED_RR) &&
  76. (param->__sched_priority < 1 || param->__sched_priority > 99))
  77. return -EINVAL;
  78. return 0;
  79. }
  80. /* dummy implementation: always return SCHED_NORMAL (default round-robin time-sharing policy) */
  81. int shim_do_sched_getscheduler(pid_t pid) {
  82. if (pid < 0)
  83. return -EINVAL;
  84. return SCHED_NORMAL;
  85. }
  86. int shim_do_sched_get_priority_max(int policy) {
  87. /* fail on unrecognized policies */
  88. if (policy != SCHED_NORMAL && policy != SCHED_BATCH && policy != SCHED_IDLE && /* non-real-time */
  89. policy != SCHED_FIFO && policy != SCHED_RR /* real-time */)
  90. return -EINVAL;
  91. /* real-time policies have max priority of 99 */
  92. if (policy == SCHED_FIFO || policy == SCHED_RR)
  93. return 99;
  94. /* non-real-time policies have max priority of 0 */
  95. return 0;
  96. }
  97. int shim_do_sched_get_priority_min(int policy) {
  98. /* fail on unrecognized policies */
  99. if (policy != SCHED_NORMAL && policy != SCHED_BATCH && policy != SCHED_IDLE && /* non-real-time */
  100. policy != SCHED_FIFO && policy != SCHED_RR /* real-time */)
  101. return -EINVAL;
  102. /* real-time policies have min priority of 1 */
  103. if (policy == SCHED_FIFO || policy == SCHED_RR)
  104. return 1;
  105. /* non-real-time policies have min priority of 0 */
  106. return 0;
  107. }
  108. /* dummy implementation: always return 100 ms (default in Linux) */
  109. int shim_do_sched_rr_get_interval(pid_t pid, struct timespec* interval) {
  110. if (pid < 0)
  111. return -EINVAL;
  112. if (test_user_memory(interval, sizeof(*interval), true))
  113. return -EFAULT;
  114. interval->tv_sec = 0;
  115. interval->tv_nsec = 100000000; /* default value of 100 ms in Linux */
  116. return 0;
  117. }
  118. static int check_affinity_params(int ncpus, size_t len, __kernel_cpu_set_t* user_mask_ptr) {
  119. /* Check that user_mask_ptr is valid; if not, should return -EFAULT */
  120. if (test_user_memory(user_mask_ptr, len, true))
  121. return -EFAULT;
  122. /* Linux kernel bitmap is based on long. So according to its
  123. * implementation, round up the result to sizeof(long) */
  124. size_t bitmask_long_count = (ncpus + sizeof(long) * 8 - 1) / (sizeof(long) * 8);
  125. size_t bitmask_size_in_bytes = bitmask_long_count * sizeof(long);
  126. if (len < bitmask_size_in_bytes)
  127. return -EINVAL;
  128. /* Linux kernel also rejects non-natural size */
  129. if (len & (sizeof(long) - 1))
  130. return -EINVAL;
  131. return bitmask_size_in_bytes;
  132. }
  133. /* dummy implementation: ignore user-supplied mask and return success */
  134. int shim_do_sched_setaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr) {
  135. __UNUSED(pid);
  136. int ncpus = PAL_CB(cpu_info.cpu_num);
  137. int bitmask_size_in_bytes = check_affinity_params(ncpus, len, user_mask_ptr);
  138. if (bitmask_size_in_bytes < 0)
  139. return bitmask_size_in_bytes;
  140. return 0;
  141. }
  142. /* dummy implementation: always return all-ones (as many as there are host CPUs) */
  143. int shim_do_sched_getaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr) {
  144. __UNUSED(pid);
  145. int ncpus = PAL_CB(cpu_info.cpu_num);
  146. int bitmask_size_in_bytes = check_affinity_params(ncpus, len, user_mask_ptr);
  147. if (bitmask_size_in_bytes < 0)
  148. return bitmask_size_in_bytes;
  149. memset(user_mask_ptr, 0, len);
  150. for (int i = 0; i < ncpus; i++) {
  151. ((uint8_t*)user_mask_ptr)[i / 8] |= 1 << (i % 8);
  152. }
  153. /* imitate the Linux kernel implementation
  154. * See SYSCALL_DEFINE3(sched_getaffinity) */
  155. return bitmask_size_in_bytes;
  156. }