syscall_server.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef SYSCALL_SERVER_H
  2. #define SYSCALL_SERVER_H
  3. #include "fixed_types.h"
  4. #include "subsecond_time.h"
  5. #include <iostream>
  6. #include <unordered_map>
  7. #include <list>
  8. // -- For futexes --
  9. #include <linux/futex.h>
  10. #include <sys/time.h>
  11. #include <errno.h>
  12. class Core;
  13. // -- Special Class to Handle Futexes
  14. class SimFutex
  15. {
  16. public:
  17. struct Waiter
  18. {
  19. Waiter(thread_id_t _thread_id, int _mask, SubsecondTime _timeout)
  20. : thread_id(_thread_id), mask(_mask), timeout(_timeout)
  21. {}
  22. thread_id_t thread_id;
  23. int mask;
  24. SubsecondTime timeout;
  25. };
  26. typedef std::list<Waiter> ThreadQueue;
  27. private:
  28. ThreadQueue m_waiting;
  29. public:
  30. SimFutex();
  31. ~SimFutex();
  32. bool enqueueWaiter(thread_id_t thread_id, int mask, SubsecondTime time, SubsecondTime timeout_time, SubsecondTime &time_end);
  33. thread_id_t dequeueWaiter(thread_id_t thread_by, int mask, SubsecondTime time);
  34. thread_id_t requeueWaiter(SimFutex *requeue_futex);
  35. void wakeTimedOut(SubsecondTime time);
  36. SubsecondTime getNextTimeout(SubsecondTime time);
  37. };
  38. class SyscallServer
  39. {
  40. public:
  41. struct futex_args_t {
  42. int *uaddr;
  43. int op;
  44. int val;
  45. const struct timespec *timeout;
  46. int val2;
  47. int *uaddr2;
  48. int val3;
  49. };
  50. SyscallServer();
  51. ~SyscallServer();
  52. void handleSleepCall(thread_id_t thread_id, SubsecondTime wake_time, SubsecondTime curr_time, SubsecondTime &end_time);
  53. IntPtr handleFutexCall(thread_id_t thread_id, futex_args_t &args, SubsecondTime curr_time, SubsecondTime &end_time);
  54. SubsecondTime getNextTimeout(SubsecondTime time);
  55. private:
  56. // Handling Futexes
  57. IntPtr futexWait(thread_id_t thread_id, int *uaddr, int val, int act_val, int val3, SubsecondTime curr_time, SubsecondTime timeout_time, SubsecondTime &end_time);
  58. IntPtr futexWake(thread_id_t thread_id, int *uaddr, int nr_wake, int val3, SubsecondTime curr_time, SubsecondTime &end_time);
  59. IntPtr futexWakeOp(thread_id_t thread_id, int *uaddr, int *uaddr2, int nr_wake, int nr_wake2, int op, SubsecondTime curr_time, SubsecondTime &end_time);
  60. IntPtr futexCmpRequeue(thread_id_t thread_id, int *uaddr, int val, int *uaddr2, int val3, int act_val, SubsecondTime curr_time, SubsecondTime &end_time);
  61. SimFutex* findFutexByUaddr(int *uaddr, thread_id_t thread_id);
  62. thread_id_t wakeFutexOne(SimFutex *sim_futex, thread_id_t thread_by, int mask, SubsecondTime curr_time);
  63. int futexDoOp(Core *core, int op, int *uaddr);
  64. void futexPeriodic(SubsecondTime time);
  65. SubsecondTime applyRescheduleCost(thread_id_t thread_id, bool conditional = true);
  66. static SInt64 hook_periodic(UInt64 ptr, UInt64 time)
  67. {
  68. ((SyscallServer*)ptr)->futexPeriodic(*(subsecond_time_t*)(&time));
  69. return 0;
  70. }
  71. SubsecondTime m_reschedule_cost;
  72. // Sleeping threads
  73. SimFutex::ThreadQueue m_sleeping;
  74. // Handling Futexes
  75. typedef std::unordered_map<IntPtr, SimFutex> FutexMap;
  76. FutexMap m_futexes;
  77. friend class ThreadManager;
  78. };
  79. #endif