pal_host.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #if TRACE_HEAP_LEAK == 1
  49. struct heap_trace_info {
  50. /* maintaining a list of handles */
  51. struct pal_handle ** pprev, * next;
  52. /* trace the PC where the handle is created */
  53. PAL_PTR caller;
  54. } heap_trace;
  55. #endif
  56. } PAL_RESERVED_HDR;
  57. #define MAX_FDS 3
  58. typedef struct pal_handle
  59. {
  60. /* TSAI: Here we define the internal types of PAL_HANDLE
  61. * in PAL design, user has not to access the content inside the
  62. * handle, also there is no need to allocate the internal
  63. * handles, so we hide the type name of these handles on purpose.
  64. */
  65. PAL_HDR hdr;
  66. union {
  67. struct {
  68. PAL_IDX fds[MAX_FDS];
  69. } generic;
  70. struct {
  71. PAL_IDX fd;
  72. PAL_NUM offset;
  73. PAL_STR realpath;
  74. /*
  75. * map_start is to request this file should be mapped to this
  76. * address. When fork is emulated, the address is already
  77. * determined by parent process.
  78. */
  79. PAL_PTR map_start;
  80. } file;
  81. struct {
  82. PAL_IDX fd;
  83. PAL_NUM pipeid;
  84. PAL_BOL nonblocking;
  85. } pipe;
  86. struct {
  87. PAL_IDX fds[MAX_FDS];
  88. PAL_BOL nonblocking;
  89. } pipeprv;
  90. struct {
  91. PAL_IDX fd_in, fd_out;
  92. PAL_IDX dev_type;
  93. PAL_BOL destroy;
  94. PAL_STR realpath;
  95. } dev;
  96. struct {
  97. PAL_IDX fd;
  98. PAL_STR realpath;
  99. PAL_PTR buf;
  100. PAL_PTR ptr;
  101. PAL_PTR end;
  102. PAL_BOL endofstream;
  103. } dir;
  104. struct {
  105. PAL_IDX fd;
  106. PAL_NUM token;
  107. } gipc;
  108. struct {
  109. PAL_IDX fd;
  110. PAL_PTR bind;
  111. PAL_PTR conn;
  112. PAL_BOL nonblocking;
  113. PAL_BOL reuseaddr;
  114. PAL_NUM linger;
  115. PAL_NUM receivebuf;
  116. PAL_NUM sendbuf;
  117. PAL_NUM receivetimeout;
  118. PAL_NUM sendtimeout;
  119. PAL_BOL tcp_cork;
  120. PAL_BOL tcp_keepalive;
  121. PAL_BOL tcp_nodelay;
  122. } sock;
  123. struct {
  124. PAL_IDX stream_in, stream_out;
  125. PAL_IDX cargo;
  126. PAL_IDX pid;
  127. PAL_BOL nonblocking;
  128. } process;
  129. struct {
  130. PAL_IDX cli;
  131. PAL_IDX srv;
  132. PAL_IDX port;
  133. PAL_BOL nonblocking;
  134. PAL_PTR addr;
  135. } mcast;
  136. struct {
  137. PAL_IDX tid;
  138. PAL_PTR stack;
  139. } thread;
  140. struct {
  141. struct mutex_handle mut;
  142. } mutex;
  143. struct {
  144. struct atomic_int signaled;
  145. struct atomic_int nwaiters;
  146. PAL_BOL isnotification;
  147. } event;
  148. };
  149. } * PAL_HANDLE;
  150. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  151. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  152. #define WRITABLE(n) (1 << (MAX_FDS*2 + (n)))
  153. #define ERROR(n) (1 << (MAX_FDS*3 + (n)))
  154. #define HAS_FDS ((1 << MAX_FDS*2) - 1)
  155. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  156. extern void __check_pending_event (void);
  157. #define LEAVE_PAL_CALL() do { __check_pending_event(); } while (0)
  158. #define LEAVE_PAL_CALL_RETURN(retval) \
  159. do { __check_pending_event(); return (retval); } while (0)
  160. #if TRACE_HEAP_LEAK == 1
  161. /* The following code adds a piece of information
  162. in each handle to trace heap leakage. */
  163. extern PAL_HANDLE heap_alloc_head;
  164. extern PAL_LOCK heap_alloc_trace_lock;
  165. /* call the following function in GDB */
  166. typedef struct {
  167. PAL_PTR caller;
  168. PAL_NUM count;
  169. } HEAP_ALLOC_RECORD;
  170. extern HEAP_ALLOC_RECORD * collect_heap_alloc_records (PAL_NUM max_records);
  171. static inline
  172. void __trace_heap (PAL_HANDLE handle, struct pal_frame * frame)
  173. {
  174. _DkInternalLock(&heap_alloc_trace_lock);
  175. handle->hdr.heap_trace.caller = ((PAL_PTR *)frame->arch.rbp)[1];
  176. /* Add the handle to the list */
  177. if (heap_alloc_head)
  178. heap_alloc_head->hdr.heap_trace.pprev
  179. = &handle->hdr.heap_trace.next;
  180. handle->hdr.heap_trace.next = heap_alloc_head;
  181. handle->hdr.heap_trace.pprev = &heap_alloc_head;
  182. heap_alloc_head = handle;
  183. _DkInternalUnlock(&heap_alloc_trace_lock);
  184. }
  185. #define TRACE_HEAP(handle) \
  186. do { if (handle) __trace_heap(handle, &frame); } while (0)
  187. static inline
  188. void __untrace_heap (PAL_HANDLE handle)
  189. {
  190. _DkInternalLock(&heap_alloc_trace_lock);
  191. /* remove the handle from the list */
  192. *handle->hdr.heap_trace.pprev = handle->hdr.heap_trace.next;
  193. if (handle->hdr.heap_trace.next)
  194. handle->hdr.heap_trace.next->hdr.heap_trace.pprev
  195. = handle->hdr.heap_trace.pprev;
  196. handle->hdr.heap_trace.pprev = NULL;
  197. handle->hdr.heap_trace.next = NULL;
  198. _DkInternalUnlock(&heap_alloc_trace_lock);
  199. }
  200. #define UNTRACE_HEAP(handle) \
  201. do { if (handle) __untrace_heap(handle); } while (0)
  202. #endif /* TRACE_HEAP_LEAK == 1 */
  203. #endif /* PAL_HOST_H */