pal_host.h 6.9 KB

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