shim_alarm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_alarm.c
  17. *
  18. * Implementation of system call "alarm", "setitmer" and "getitimer".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_utils.h>
  24. #include <shim_signal.h>
  25. void signal_alarm (IDTYPE target, void * arg)
  26. {
  27. debug("alarm goes off, signaling thread %u\n", target);
  28. struct shim_thread * thread = lookup_thread(target);
  29. if (!thread)
  30. return;
  31. append_signal(thread, SIGALRM, NULL, true);
  32. }
  33. int shim_do_alarm (unsigned int seconds)
  34. {
  35. uint64_t usecs = 1000000ULL * seconds;
  36. uint64_t usecs_left = install_async_event(NULL, usecs, &signal_alarm, NULL);
  37. // Alarm expects the number of seconds remaining. Round up.
  38. int secs = usecs_left / 1000000ULL;
  39. if (usecs_left % 1000000ULL) secs++;
  40. return secs;
  41. }
  42. static struct {
  43. unsigned long timeout;
  44. unsigned long reset;
  45. } real_itimer;
  46. void signal_itimer (IDTYPE target, void * arg)
  47. {
  48. master_lock();
  49. if (real_itimer.timeout != (unsigned long) arg) {
  50. master_unlock();
  51. return;
  52. }
  53. real_itimer.timeout += real_itimer.reset;
  54. real_itimer.reset = 0;
  55. master_unlock();
  56. }
  57. #ifndef ITIMER_REAL
  58. # define ITIMER_REAL 0
  59. #endif
  60. int shim_do_setitimer (int which, struct __kernel_itimerval * value,
  61. struct __kernel_itimerval * ovalue)
  62. {
  63. if (which != ITIMER_REAL)
  64. return -ENOSYS;
  65. if (!value)
  66. return -EFAULT;
  67. if (test_user_memory(value, sizeof(*value), false))
  68. return -EFAULT;
  69. if (ovalue && test_user_memory(ovalue, sizeof(*ovalue), true))
  70. return -EFAULT;
  71. unsigned long setup_time = DkSystemTimeQuery();
  72. unsigned long next_value = value->it_value.tv_sec * 1000000
  73. + value->it_value.tv_usec;
  74. unsigned long next_reset = value->it_interval.tv_sec * 1000000
  75. + value->it_interval.tv_usec;
  76. master_lock();
  77. unsigned long current_timeout = real_itimer.timeout > setup_time ?
  78. real_itimer.timeout - setup_time : 0;
  79. unsigned long current_reset = real_itimer.reset;
  80. uint64_t ret = install_async_event(NULL, next_value, &signal_itimer,
  81. (void *) (setup_time + next_value));
  82. if (ret < 0) {
  83. master_unlock();
  84. return ret;
  85. }
  86. real_itimer.timeout = setup_time + next_value;
  87. real_itimer.reset = next_reset;
  88. master_unlock();
  89. if (ovalue) {
  90. ovalue->it_interval.tv_sec = current_reset / 1000000;
  91. ovalue->it_interval.tv_usec = current_reset % 1000000;
  92. ovalue->it_value.tv_sec = current_timeout / 1000000;
  93. ovalue->it_value.tv_usec = current_timeout % 1000000;
  94. }
  95. return 0;
  96. }
  97. int shim_do_getitimer (int which, struct __kernel_itimerval * value)
  98. {
  99. if (which != ITIMER_REAL)
  100. return -ENOSYS;
  101. if (!value)
  102. return -EFAULT;
  103. if (test_user_memory(value, sizeof(*value), true))
  104. return -EFAULT;
  105. unsigned long setup_time = DkSystemTimeQuery();
  106. master_lock();
  107. unsigned long current_timeout = real_itimer.timeout > setup_time ?
  108. real_itimer.timeout - setup_time : 0;
  109. unsigned long current_reset = real_itimer.reset;
  110. master_unlock();
  111. value->it_interval.tv_sec = current_reset / 1000000;
  112. value->it_interval.tv_usec = current_reset % 1000000;
  113. value->it_value.tv_sec = current_timeout / 1000000;
  114. value->it_value.tv_usec = current_timeout % 1000000;
  115. return 0;
  116. }