pal_host.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * pal_host.h
  17. *
  18. * This file contains definition of PAL host ABI.
  19. */
  20. #ifndef PAL_HOST_H
  21. #define PAL_HOST_H
  22. #ifndef IN_PAL
  23. # error "cannot be included outside PAL"
  24. #endif
  25. #include <atomic.h>
  26. /* Simpler mutex design: a single variable that tracks whether the
  27. * mutex is locked (just waste a 64 bit word for now). State is 1 (locked) or
  28. * 0 (unlocked).
  29. * Keep a count of how many threads are waiting on the mutex.
  30. * If DEBUG_MUTEX is defined,
  31. * mutex_handle will record the owner of mutex locking. */
  32. typedef struct mutex_handle {
  33. volatile int64_t locked;
  34. struct atomic_int nwaiters;
  35. #ifdef DEBUG_MUTEX
  36. int owner;
  37. #endif
  38. } PAL_LOCK;
  39. /* Initializer of Mutexes */
  40. #define MUTEX_HANDLE_INIT { .locked = 0, .nwaiters.counter = 0 }
  41. #define INIT_MUTEX_HANDLE(m) do { (m)->locked = 0; atomic_set(&(m)->nwaiters, 0); } while (0)
  42. #define LOCK_INIT MUTEX_HANDLE_INIT
  43. #define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock)
  44. /* Locking and unlocking of Mutexes */
  45. int _DkMutexLock (struct mutex_handle * mut);
  46. int _DkMutexLockTimeout (struct mutex_handle * mut, uint64_t timeout);
  47. int _DkMutexUnlock (struct mutex_handle * mut);
  48. typedef struct {
  49. PAL_HDR hdr;
  50. #if TRACE_HEAP_LEAK == 1
  51. struct heap_trace_info {
  52. /* maintaining a list of handles */
  53. struct pal_handle ** pprev, * next;
  54. /* trace the PC where the handle is created */
  55. PAL_PTR caller;
  56. } heap_trace;
  57. #endif
  58. } PAL_RESERVED_HDR;
  59. #define MAX_FDS 3
  60. typedef struct pal_handle
  61. {
  62. /* TSAI: Here we define the internal types of PAL_HANDLE
  63. * in PAL design, user has not to access the content inside the
  64. * handle, also there is no need to allocate the internal
  65. * handles, so we hide the type name of these handles on purpose.
  66. */
  67. PAL_HDR hdr;
  68. union {
  69. struct {
  70. PAL_IDX fds[MAX_FDS];
  71. } generic;
  72. struct {
  73. PAL_IDX fd;
  74. PAL_NUM offset;
  75. PAL_BOL append;
  76. PAL_BOL pass;
  77. PAL_STR realpath;
  78. /*
  79. * map_start is to request this file should be mapped to this
  80. * address. When fork is emulated, the address is already
  81. * determined by parent process.
  82. */
  83. PAL_PTR map_start;
  84. } file;
  85. struct {
  86. PAL_IDX fd;
  87. PAL_NUM pipeid;
  88. PAL_BOL nonblocking;
  89. } pipe;
  90. struct {
  91. PAL_IDX fds[MAX_FDS];
  92. PAL_BOL nonblocking;
  93. } pipeprv;
  94. struct {
  95. PAL_IDX fd_in, fd_out;
  96. PAL_IDX dev_type;
  97. PAL_BOL destroy;
  98. PAL_STR realpath;
  99. } dev;
  100. struct {
  101. PAL_IDX fd;
  102. PAL_STR realpath;
  103. PAL_PTR buf;
  104. PAL_PTR ptr;
  105. PAL_PTR end;
  106. PAL_BOL endofstream;
  107. } dir;
  108. struct {
  109. PAL_IDX fd;
  110. PAL_NUM token;
  111. } gipc;
  112. struct {
  113. PAL_IDX fd;
  114. PAL_PTR bind;
  115. PAL_PTR conn;
  116. PAL_BOL nonblocking;
  117. PAL_BOL reuseaddr;
  118. PAL_NUM linger;
  119. PAL_NUM receivebuf;
  120. PAL_NUM sendbuf;
  121. PAL_NUM receivetimeout;
  122. PAL_NUM sendtimeout;
  123. PAL_BOL tcp_cork;
  124. PAL_BOL tcp_keepalive;
  125. PAL_BOL tcp_nodelay;
  126. } sock;
  127. struct {
  128. PAL_IDX stream_in, stream_out;
  129. PAL_IDX cargo;
  130. PAL_IDX pid;
  131. PAL_BOL nonblocking;
  132. } process;
  133. struct {
  134. PAL_IDX cli;
  135. PAL_IDX srv;
  136. PAL_IDX port;
  137. PAL_BOL nonblocking;
  138. PAL_PTR addr;
  139. } mcast;
  140. struct {
  141. PAL_IDX tid;
  142. PAL_PTR stack;
  143. } thread;
  144. struct {
  145. struct mutex_handle mut;
  146. } mutex;
  147. struct {
  148. struct atomic_int signaled;
  149. struct atomic_int nwaiters;
  150. PAL_BOL isnotification;
  151. } event;
  152. };
  153. } * PAL_HANDLE;
  154. #define RFD(n) (00001 << (n))
  155. #define WFD(n) (00010 << (n))
  156. #define WRITEABLE(n) (00100 << (n))
  157. #define ERROR(n) (01000 << (n))
  158. #define HAS_FDS 00077
  159. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  160. extern void __check_pending_event (void);
  161. #define LEAVE_PAL_CALL() do { __check_pending_event(); } while (0)
  162. #define LEAVE_PAL_CALL_RETURN(retval) \
  163. do { __check_pending_event(); return (retval); } while (0)
  164. #if TRACE_HEAP_LEAK == 1
  165. /* The following code adds a piece of information
  166. in each handle to trace heap leakage. */
  167. extern PAL_HANDLE heap_alloc_head;
  168. extern PAL_LOCK heap_alloc_trace_lock;
  169. /* call the following function in GDB */
  170. typedef struct {
  171. PAL_PTR caller;
  172. PAL_NUM count;
  173. } HEAP_ALLOC_RECORD;
  174. extern HEAP_ALLOC_RECORD * collect_heap_alloc_records (PAL_NUM max_records);
  175. static inline
  176. void __trace_heap (PAL_HANDLE handle, struct pal_frame * frame)
  177. {
  178. _DkInternalLock(&heap_alloc_trace_lock);
  179. handle->hdr.heap_trace.caller = ((PAL_PTR *)frame->arch.rbp)[1];
  180. /* Add the handle to the list */
  181. if (heap_alloc_head)
  182. heap_alloc_head->hdr.heap_trace.pprev
  183. = &handle->hdr.heap_trace.next;
  184. handle->hdr.heap_trace.next = heap_alloc_head;
  185. handle->hdr.heap_trace.pprev = &heap_alloc_head;
  186. heap_alloc_head = handle;
  187. _DkInternalUnlock(&heap_alloc_trace_lock);
  188. }
  189. #define TRACE_HEAP(handle) \
  190. do { if (handle) __trace_heap(handle, &frame); } while (0)
  191. static inline
  192. void __untrace_heap (PAL_HANDLE handle)
  193. {
  194. _DkInternalLock(&heap_alloc_trace_lock);
  195. /* remove the handle from the list */
  196. *handle->hdr.heap_trace.pprev = handle->hdr.heap_trace.next;
  197. if (handle->hdr.heap_trace.next)
  198. handle->hdr.heap_trace.next->hdr.heap_trace.pprev
  199. = handle->hdr.heap_trace.pprev;
  200. handle->hdr.heap_trace.pprev = NULL;
  201. handle->hdr.heap_trace.next = NULL;
  202. _DkInternalUnlock(&heap_alloc_trace_lock);
  203. }
  204. #define UNTRACE_HEAP(handle) \
  205. do { if (handle) __untrace_heap(handle); } while (0)
  206. #endif /* TRACE_HEAP_LEAK == 1 */
  207. #endif /* PAL_HOST_H */