pal_host.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #define _DkInternalLock _DkMutexLock
  45. #define _DkInternalUnlock _DkMutexUnlock
  46. /* Locking and unlocking of Mutexes */
  47. int _DkMutexLock (struct mutex_handle * mut);
  48. int _DkMutexLockTimeout (struct mutex_handle * mut, uint64_t timeout);
  49. int _DkMutexUnlock (struct mutex_handle * mut);
  50. typedef struct {
  51. PAL_HDR hdr;
  52. #if TRACE_HEAP_LEAK == 1
  53. struct heap_trace_info {
  54. /* maintaining a list of handles */
  55. struct pal_handle ** pprev, * next;
  56. /* trace the PC where the handle is created */
  57. PAL_PTR caller;
  58. } heap_trace;
  59. #endif
  60. } PAL_RESERVED_HDR;
  61. typedef struct pal_handle
  62. {
  63. /* TSAI: Here we define the internal types of PAL_HANDLE
  64. * in PAL design, user has not to access the content inside the
  65. * handle, also there is no need to allocate the internal
  66. * handles, so we hide the type name of these handles on purpose.
  67. */
  68. PAL_HDR hdr;
  69. union {
  70. struct {
  71. PAL_IDX fds[2];
  72. } generic;
  73. struct {
  74. PAL_IDX fd;
  75. PAL_NUM offset;
  76. PAL_BOL append;
  77. PAL_BOL pass;
  78. PAL_STR realpath;
  79. } file;
  80. struct {
  81. PAL_IDX fd;
  82. PAL_NUM pipeid;
  83. PAL_BOL nonblocking;
  84. } pipe;
  85. struct {
  86. PAL_IDX fds[2];
  87. PAL_BOL nonblocking;
  88. } pipeprv;
  89. struct {
  90. PAL_IDX fd_in, fd_out;
  91. PAL_IDX dev_type;
  92. PAL_BOL destroy;
  93. PAL_STR realpath;
  94. } dev;
  95. struct {
  96. PAL_IDX fd;
  97. PAL_STR realpath;
  98. PAL_PTR buf;
  99. PAL_PTR ptr;
  100. PAL_PTR end;
  101. PAL_BOL endofstream;
  102. } dir;
  103. struct {
  104. PAL_IDX fd;
  105. PAL_NUM token;
  106. } gipc;
  107. struct {
  108. PAL_IDX fd;
  109. PAL_PTR bind;
  110. PAL_PTR conn;
  111. PAL_BOL nonblocking;
  112. PAL_BOL reuseaddr;
  113. PAL_NUM linger;
  114. PAL_NUM receivebuf;
  115. PAL_NUM sendbuf;
  116. PAL_NUM receivetimeout;
  117. PAL_NUM sendtimeout;
  118. PAL_BOL tcp_cork;
  119. PAL_BOL tcp_keepalive;
  120. PAL_BOL tcp_nodelay;
  121. } sock;
  122. struct {
  123. PAL_IDX stream_in, stream_out;
  124. PAL_IDX cargo;
  125. PAL_IDX pid;
  126. PAL_BOL nonblocking;
  127. } process;
  128. struct {
  129. PAL_IDX cli;
  130. PAL_IDX srv;
  131. PAL_IDX port;
  132. PAL_BOL nonblocking;
  133. PAL_PTR addr;
  134. } mcast;
  135. struct {
  136. PAL_IDX tid;
  137. } thread;
  138. struct {
  139. struct mutex_handle mut;
  140. } mutex;
  141. struct {
  142. struct atomic_int signaled;
  143. struct atomic_int nwaiters;
  144. PAL_BOL isnotification;
  145. } event;
  146. };
  147. } * PAL_HANDLE;
  148. #define RFD(n) (00001 << (n))
  149. #define WFD(n) (00010 << (n))
  150. #define WRITEABLE(n) (00100 << (n))
  151. #define ERROR(n) (01000 << (n))
  152. #define MAX_FDS (3)
  153. #define HAS_FDS (00077)
  154. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  155. struct arch_frame {
  156. #ifdef __x86_64__
  157. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  158. #else
  159. # error "unsupported architecture"
  160. #endif
  161. };
  162. #ifdef __x86_64__
  163. # define store_register(reg, var) \
  164. asm volatile ("movq %%" #reg ", %0" : "=a" (var) :: "memory");
  165. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  166. # define arch_store_frame(f) \
  167. store_register_in_frame(rsp, f) \
  168. store_register_in_frame(rbp, f) \
  169. store_register_in_frame(rbx, f) \
  170. store_register_in_frame(rsi, f) \
  171. store_register_in_frame(rdi, f) \
  172. store_register_in_frame(r12, f) \
  173. store_register_in_frame(r13, f) \
  174. store_register_in_frame(r14, f) \
  175. store_register_in_frame(r15, f)
  176. # define restore_register(reg, var, clobber...) \
  177. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  178. # define restore_register_in_frame(reg, f) \
  179. restore_register(reg, (f)->reg, \
  180. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  181. # define arch_restore_frame(f) \
  182. restore_register_in_frame(r15, f) \
  183. restore_register_in_frame(r14, f) \
  184. restore_register_in_frame(r13, f) \
  185. restore_register_in_frame(r12, f) \
  186. restore_register_in_frame(rdi, f) \
  187. restore_register_in_frame(rsi, f) \
  188. restore_register_in_frame(rbx, f) \
  189. restore_register_in_frame(rbp, f) \
  190. restore_register_in_frame(rsp, f)
  191. #else /* __x86_64__ */
  192. # error "unsupported architecture"
  193. #endif
  194. #define PAL_FRAME_IDENTIFIER (0xdeaddeadbeefbeef)
  195. struct pal_frame {
  196. volatile uint64_t identifier;
  197. void * func;
  198. const char * funcname;
  199. struct arch_frame arch;
  200. };
  201. static inline
  202. void __store_frame (struct pal_frame * frame,
  203. void * func, const char * funcname)
  204. {
  205. arch_store_frame(&frame->arch)
  206. frame->func = func;
  207. frame->funcname = funcname;
  208. asm volatile ("nop" ::: "memory");
  209. frame->identifier = PAL_FRAME_IDENTIFIER;
  210. }
  211. #define ENTER_PAL_CALL(name) \
  212. struct pal_frame frame; \
  213. __store_frame(&frame, &(name), #name)
  214. static inline
  215. void __clear_frame (struct pal_frame * frame)
  216. {
  217. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  218. asm volatile ("nop" ::: "memory");
  219. frame->identifier = 0;
  220. }
  221. }
  222. #define LEAVE_PAL_CALL() \
  223. do { \
  224. __clear_frame(&frame); \
  225. } while (0)
  226. #define LEAVE_PAL_CALL_RETURN(retval) \
  227. do { \
  228. __clear_frame(&frame); \
  229. return (retval); \
  230. } while (0)
  231. #if TRACE_HEAP_LEAK == 1
  232. /* The following code adds a piece of information
  233. in each handle to trace heap leakage. */
  234. extern PAL_HANDLE heap_alloc_head;
  235. extern PAL_LOCK heap_alloc_trace_lock;
  236. /* call the following function in GDB */
  237. typedef struct {
  238. PAL_PTR caller;
  239. PAL_NUM count;
  240. } HEAP_ALLOC_RECORD;
  241. extern HEAP_ALLOC_RECORD * collect_heap_alloc_records (PAL_NUM max_records);
  242. static inline
  243. void __trace_heap (PAL_HANDLE handle, struct pal_frame * frame)
  244. {
  245. _DkInternalLock(&heap_alloc_trace_lock);
  246. handle->hdr.heap_trace.caller = ((PAL_PTR *)frame->arch.rbp)[1];
  247. /* Add the handle to the list */
  248. if (heap_alloc_head)
  249. heap_alloc_head->hdr.heap_trace.pprev
  250. = &handle->hdr.heap_trace.next;
  251. handle->hdr.heap_trace.next = heap_alloc_head;
  252. handle->hdr.heap_trace.pprev = &heap_alloc_head;
  253. heap_alloc_head = handle;
  254. _DkInternalUnlock(&heap_alloc_trace_lock);
  255. }
  256. #define TRACE_HEAP(handle) \
  257. do { if (handle) __trace_heap(handle, &frame); } while (0)
  258. static inline
  259. void __untrace_heap (PAL_HANDLE handle)
  260. {
  261. _DkInternalLock(&heap_alloc_trace_lock);
  262. /* remove the handle from the list */
  263. *handle->hdr.heap_trace.pprev = handle->hdr.heap_trace.next;
  264. if (handle->hdr.heap_trace.next)
  265. handle->hdr.heap_trace.next->hdr.heap_trace.pprev
  266. = handle->hdr.heap_trace.pprev;
  267. handle->hdr.heap_trace.pprev = NULL;
  268. handle->hdr.heap_trace.next = NULL;
  269. _DkInternalUnlock(&heap_alloc_trace_lock);
  270. }
  271. #define UNTRACE_HEAP(handle) \
  272. do { if (handle) __untrace_heap(handle); } while (0)
  273. #endif /* TRACE_HEAP_LEAK == 1 */
  274. #endif /* PAL_HOST_H */