pal_host.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * pal_host.h
  15. *
  16. * This file contains definition of PAL host ABI.
  17. */
  18. #ifndef PAL_HOST_H
  19. #define PAL_HOST_H
  20. #ifndef IN_PAL
  21. # error "cannot be included outside PAL"
  22. #endif
  23. #include <atomic.h>
  24. /* Simpler mutex design: a single variable that tracks whether the
  25. * mutex is locked (just waste a 64 bit word for now). State is 1 (locked) or
  26. * 0 (unlocked).
  27. * Keep a count of how many threads are waiting on the mutex.
  28. * If DEBUG_MUTEX is defined,
  29. * mutex_handle will record the owner of mutex locking. */
  30. typedef struct mutex_handle {
  31. volatile int64_t locked;
  32. struct atomic_int nwaiters;
  33. #ifdef DEBUG_MUTEX
  34. int owner;
  35. #endif
  36. } PAL_LOCK;
  37. /* Initializer of Mutexes */
  38. #define MUTEX_HANDLE_INIT { .locked = 0, .nwaiters.counter = 0 }
  39. #define INIT_MUTEX_HANDLE(m) do { (m)->locked = 0; atomic_set(&(m)->nwaiters, 0); } while (0)
  40. #define LOCK_INIT MUTEX_HANDLE_INIT
  41. #define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock)
  42. /* Locking and unlocking of Mutexes */
  43. int _DkMutexLock(struct mutex_handle* mut);
  44. int _DkMutexLockTimeout(struct mutex_handle* mut, int64_t timeout_us);
  45. int _DkMutexUnlock(struct mutex_handle* mut);
  46. typedef struct {
  47. PAL_HDR hdr;
  48. } PAL_RESERVED_HDR;
  49. typedef struct pal_handle
  50. {
  51. /* TSAI: Here we define the internal types of PAL_HANDLE
  52. * in PAL design, user has not to access the content inside the
  53. * handle, also there is no need to allocate the internal
  54. * handles, so we hide the type name of these handles on purpose.
  55. */
  56. PAL_HDR hdr;
  57. union {
  58. struct {
  59. PAL_IDX fds[MAX_FDS];
  60. } generic;
  61. struct {
  62. PAL_IDX fd;
  63. PAL_STR realpath;
  64. /*
  65. * map_start is to request this file should be mapped to this
  66. * address. When fork is emulated, the address is already
  67. * determined by parent process.
  68. */
  69. PAL_PTR map_start;
  70. } file;
  71. struct {
  72. PAL_IDX fd;
  73. PAL_NUM pipeid;
  74. PAL_BOL nonblocking;
  75. } pipe;
  76. struct {
  77. PAL_IDX fds[MAX_FDS];
  78. PAL_BOL nonblocking;
  79. } pipeprv;
  80. struct {
  81. PAL_IDX fd;
  82. /* TODO: add other flags in future, if needed (e.g., semaphore) */
  83. PAL_BOL nonblocking;
  84. } eventfd;
  85. struct {
  86. PAL_IDX fd_in, fd_out;
  87. PAL_IDX dev_type;
  88. PAL_BOL destroy;
  89. PAL_STR realpath;
  90. } dev;
  91. struct {
  92. PAL_IDX fd;
  93. PAL_STR realpath;
  94. PAL_PTR buf;
  95. PAL_PTR ptr;
  96. PAL_PTR end;
  97. PAL_BOL endofstream;
  98. } dir;
  99. struct {
  100. PAL_IDX fd;
  101. PAL_PTR bind;
  102. PAL_PTR conn;
  103. PAL_BOL nonblocking;
  104. PAL_BOL reuseaddr;
  105. PAL_NUM linger;
  106. PAL_NUM receivebuf;
  107. PAL_NUM sendbuf;
  108. PAL_NUM receivetimeout;
  109. PAL_NUM sendtimeout;
  110. PAL_BOL tcp_cork;
  111. PAL_BOL tcp_keepalive;
  112. PAL_BOL tcp_nodelay;
  113. } sock;
  114. struct {
  115. PAL_IDX stream;
  116. PAL_IDX cargo;
  117. PAL_IDX pid;
  118. PAL_BOL nonblocking;
  119. } process;
  120. struct {
  121. PAL_IDX tid;
  122. PAL_PTR stack;
  123. } thread;
  124. struct {
  125. struct mutex_handle mut;
  126. } mutex;
  127. struct {
  128. struct atomic_int signaled;
  129. struct atomic_int nwaiters;
  130. PAL_BOL isnotification;
  131. } event;
  132. };
  133. } * PAL_HANDLE;
  134. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  135. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  136. #define ERROR(n) (1 << (MAX_FDS*2 + (n)))
  137. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  138. extern void __check_pending_event (void);
  139. #define LEAVE_PAL_CALL() do { __check_pending_event(); } while (0)
  140. #define LEAVE_PAL_CALL_RETURN(retval) \
  141. do { __check_pending_event(); return (retval); } while (0)
  142. #endif /* PAL_HOST_H */