shim_alarm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_alarm.c
  15. *
  16. * Implementation of system call "alarm", "setitmer" and "getitimer".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_signal.h>
  20. #include <shim_table.h>
  21. #include <shim_thread.h>
  22. #include <shim_utils.h>
  23. static void signal_alarm(IDTYPE target, void* arg) {
  24. // Kept for API compatibility wtih signal_itimer
  25. __UNUSED(arg);
  26. debug("alarm goes off, signaling thread %u\n", target);
  27. struct shim_thread* thread = lookup_thread(target);
  28. if (!thread)
  29. return;
  30. lock(&thread->lock);
  31. append_signal(thread, SIGALRM, NULL, true);
  32. unlock(&thread->lock);
  33. put_thread(thread);
  34. }
  35. int shim_do_alarm(unsigned int seconds) {
  36. uint64_t usecs = 1000000ULL * seconds;
  37. int64_t ret = install_async_event(NULL, usecs, &signal_alarm, NULL);
  38. if (ret < 0)
  39. return ret;
  40. uint64_t usecs_left = (uint64_t)ret;
  41. int secs = usecs_left / 1000000ULL;
  42. if (usecs_left % 1000000ULL)
  43. secs++;
  44. return secs;
  45. }
  46. static struct {
  47. unsigned long timeout;
  48. unsigned long reset;
  49. } real_itimer;
  50. void signal_itimer(IDTYPE target, void* arg) {
  51. // XXX: Can we simplify this code or streamline with the other callback?
  52. __UNUSED(target);
  53. MASTER_LOCK();
  54. if (real_itimer.timeout != (unsigned long)arg) {
  55. MASTER_UNLOCK();
  56. return;
  57. }
  58. real_itimer.timeout += real_itimer.reset;
  59. real_itimer.reset = 0;
  60. MASTER_UNLOCK();
  61. }
  62. #ifndef ITIMER_REAL
  63. #define ITIMER_REAL 0
  64. #endif
  65. int shim_do_setitimer(int which, struct __kernel_itimerval* value,
  66. struct __kernel_itimerval* ovalue) {
  67. if (which != ITIMER_REAL)
  68. return -ENOSYS;
  69. if (!value)
  70. return -EFAULT;
  71. if (test_user_memory(value, sizeof(*value), false))
  72. return -EFAULT;
  73. if (ovalue && test_user_memory(ovalue, sizeof(*ovalue), true))
  74. return -EFAULT;
  75. unsigned long setup_time = DkSystemTimeQuery();
  76. unsigned long next_value = value->it_value.tv_sec * 1000000 + value->it_value.tv_usec;
  77. unsigned long next_reset = value->it_interval.tv_sec * 1000000 + value->it_interval.tv_usec;
  78. MASTER_LOCK();
  79. unsigned long current_timeout =
  80. real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
  81. unsigned long current_reset = real_itimer.reset;
  82. int64_t ret =
  83. install_async_event(NULL, next_value, &signal_itimer, (void*)(setup_time + next_value));
  84. if (ret < 0) {
  85. MASTER_UNLOCK();
  86. return ret;
  87. }
  88. real_itimer.timeout = setup_time + next_value;
  89. real_itimer.reset = next_reset;
  90. MASTER_UNLOCK();
  91. if (ovalue) {
  92. ovalue->it_interval.tv_sec = current_reset / 1000000;
  93. ovalue->it_interval.tv_usec = current_reset % 1000000;
  94. ovalue->it_value.tv_sec = current_timeout / 1000000;
  95. ovalue->it_value.tv_usec = current_timeout % 1000000;
  96. }
  97. return 0;
  98. }
  99. int shim_do_getitimer(int which, struct __kernel_itimerval* value) {
  100. if (which != ITIMER_REAL)
  101. return -ENOSYS;
  102. if (!value)
  103. return -EFAULT;
  104. if (test_user_memory(value, sizeof(*value), true))
  105. return -EFAULT;
  106. unsigned long setup_time = DkSystemTimeQuery();
  107. MASTER_LOCK();
  108. unsigned long current_timeout =
  109. real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
  110. unsigned long current_reset = real_itimer.reset;
  111. MASTER_UNLOCK();
  112. value->it_interval.tv_sec = current_reset / 1000000;
  113. value->it_interval.tv_usec = current_reset % 1000000;
  114. value->it_value.tv_sec = current_timeout / 1000000;
  115. value->it_value.tv_usec = current_timeout % 1000000;
  116. return 0;
  117. }