shim_sleep.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_sleep.c
  15. *
  16. * Implementation of system call "pause" and "nanosleep".
  17. */
  18. #include <errno.h>
  19. #include <pal.h>
  20. #include <pal_error.h>
  21. #include <shim_handle.h>
  22. #include <shim_internal.h>
  23. #include <shim_table.h>
  24. #include <shim_thread.h>
  25. #include <shim_utils.h>
  26. #include <shim_vma.h>
  27. static bool signal_pending(void) {
  28. struct shim_thread* cur = get_cur_thread();
  29. if (!cur)
  30. return false;
  31. lock(&cur->lock);
  32. if (!cur->signal_logs || !cur->has_signal.counter) {
  33. unlock(&cur->lock);
  34. return false;
  35. }
  36. for (int sig = 1; sig <= NUM_SIGS; sig++) {
  37. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  38. atomic_read(&cur->signal_logs[sig - 1].tail)) {
  39. /* at least one signal of type sig... */
  40. if (!__sigismember(&cur->signal_mask, sig)) {
  41. /* ...and this type is not blocked */
  42. unlock(&cur->lock);
  43. return true;
  44. }
  45. }
  46. }
  47. unlock(&cur->lock);
  48. return false;
  49. }
  50. int shim_do_pause(void) {
  51. if (signal_pending())
  52. return -EINTR;
  53. /* ~0ULL micro sec ~= 805675 years */
  54. DkThreadDelayExecution(~((PAL_NUM)0));
  55. return -EINTR;
  56. }
  57. int shim_do_nanosleep(const struct __kernel_timespec* rqtp, struct __kernel_timespec* rmtp) {
  58. if (!rqtp)
  59. return -EFAULT;
  60. if (signal_pending()) {
  61. if (rmtp) {
  62. /* no time elapsed, so copy time interval from rqtp to rmtp */
  63. rmtp->tv_sec = rqtp->tv_sec;
  64. rmtp->tv_nsec = rqtp->tv_nsec;
  65. }
  66. return -EINTR;
  67. }
  68. unsigned long time = rqtp->tv_sec * 1000000L + rqtp->tv_nsec / 1000;
  69. unsigned long ret = DkThreadDelayExecution(time);
  70. if (ret < time) {
  71. if (rmtp) {
  72. unsigned long remtime = time - ret;
  73. rmtp->tv_sec = remtime / 1000000L;
  74. rmtp->tv_nsec = (remtime - rmtp->tv_sec * 1000) * 1000;
  75. }
  76. return -EINTR;
  77. }
  78. return 0;
  79. }