pal_host.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /* internal Mutex design, the structure has to align at integer boundary
  24. because it is required by futex call. If DEBUG_MUTEX is defined,
  25. mutex_handle will record the owner of mutex locking. */
  26. typedef struct mutex_handle {
  27. struct atomic_int value;
  28. #ifdef DEBUG_MUTEX
  29. int owner;
  30. #endif
  31. } PAL_LOCK;
  32. /* Initializer of Mutexes */
  33. #define MUTEX_HANDLE_INIT { .value = { .counter = 1 } }
  34. #define INIT_MUTEX_HANDLE(mut) \
  35. do { atomic_set(&(mut)->value, 1); } while (0)
  36. #define LOCK_INIT MUTEX_HANDLE_INIT
  37. #define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock);
  38. #define _DkInternalLock _DkMutexLock
  39. #define _DkInternalUnlock _DkMutexUnlock
  40. typedef union pal_handle
  41. {
  42. /* TSAI: Here we define the internal types of PAL_HANDLE
  43. * in PAL design, user has not to access the content inside the
  44. * handle, also there is no need to allocate the internal
  45. * handles, so we hide the type name of these handles on purpose.
  46. */
  47. struct {
  48. PAL_HDR hdr;
  49. struct mutex_handle mut;
  50. } mutex;
  51. struct {
  52. PAL_IDX type;
  53. PAL_FLG flags;
  54. PAL_REF ref;
  55. PAL_IDX fds[];
  56. } hdr;
  57. struct {
  58. PAL_HDR hdr;
  59. PAL_IDX fd;
  60. PAL_NUM offset;
  61. PAL_BOL append;
  62. PAL_BOL pass;
  63. PAL_STR realpath;
  64. } file;
  65. struct {
  66. PAL_HDR hdr;
  67. PAL_IDX fd;
  68. PAL_NUM pipeid;
  69. PAL_BOL nonblocking;
  70. } pipe;
  71. struct {
  72. PAL_HDR hdr;
  73. PAL_IDX fds[MAX_FDS];
  74. PAL_BOL nonblocking;
  75. } pipeprv;
  76. struct {
  77. PAL_HDR hdr;
  78. PAL_IDX fd_in, fd_out;
  79. PAL_IDX dev_type;
  80. PAL_BOL destroy;
  81. PAL_STR realpath;
  82. } dev;
  83. struct {
  84. PAL_HDR hdr;
  85. PAL_IDX fd;
  86. PAL_STR realpath;
  87. PAL_PTR buf;
  88. PAL_PTR ptr;
  89. PAL_PTR end;
  90. PAL_BOL endofstream;
  91. } dir;
  92. struct {
  93. PAL_HDR hdr;
  94. PAL_IDX fd;
  95. PAL_PTR bind;
  96. PAL_PTR conn;
  97. PAL_BOL nonblocking;
  98. PAL_BOL reuseaddr;
  99. PAL_NUM linger;
  100. PAL_NUM receivebuf;
  101. PAL_NUM sendbuf;
  102. PAL_NUM receivetimeout;
  103. PAL_NUM sendtimeout;
  104. PAL_BOL tcp_cork;
  105. PAL_BOL tcp_keepalive;
  106. PAL_BOL tcp_nodelay;
  107. } sock;
  108. struct {
  109. PAL_HDR hdr;
  110. PAL_IDX stream;
  111. PAL_IDX cargo;
  112. PAL_IDX pid;
  113. PAL_BOL nonblocking;
  114. } process;
  115. struct {
  116. PAL_HDR hdr;
  117. PAL_IDX cli;
  118. PAL_IDX srv;
  119. PAL_IDX port;
  120. PAL_BOL nonblocking;
  121. } mcast;
  122. struct {
  123. PAL_HDR hdr;
  124. PAL_IDX tid;
  125. } thread;
  126. struct {
  127. PAL_HDR hdr;
  128. struct atomic_int nwaiters;
  129. PAL_NUM max_value;
  130. union {
  131. struct mutex_handle mut;
  132. struct atomic_int i;
  133. } value;
  134. } semaphore;
  135. struct {
  136. PAL_HDR hdr;
  137. struct atomic_int signaled;
  138. struct atomic_int nwaiters;
  139. PAL_BOL isnotification;
  140. } event;
  141. } * PAL_HANDLE;
  142. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  143. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  144. #define WRITABLE(n) (1 << (MAX_FDS*2 + (n)))
  145. #define ERROR(n) (1 << (MAX_FDS*3 + (n)))
  146. #define HAS_FDS ((1 << MAX_FDS*2) - 1)
  147. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  148. struct arch_frame {
  149. #ifdef __x86_64__
  150. uint64_t rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  151. #else
  152. # error "unsupported architecture"
  153. #endif
  154. };
  155. #ifdef __x86_64__
  156. # define store_register(reg, var) \
  157. asm volatile ("movq %%" #reg ", %0" : "=a" (var) :: "memory");
  158. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  159. # define arch_store_frame(f) \
  160. store_register_in_frame(rsp, f) \
  161. store_register_in_frame(rbp, f) \
  162. store_register_in_frame(rbx, f) \
  163. store_register_in_frame(rsi, f) \
  164. store_register_in_frame(rdi, f) \
  165. store_register_in_frame(r12, f) \
  166. store_register_in_frame(r13, f) \
  167. store_register_in_frame(r14, f) \
  168. store_register_in_frame(r15, f)
  169. # define restore_register(reg, var, clobber...) \
  170. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  171. # define restore_register_in_frame(reg, f) \
  172. restore_register(reg, (f)->reg, \
  173. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  174. # define arch_restore_frame(f) \
  175. restore_register_in_frame(r15, f) \
  176. restore_register_in_frame(r14, f) \
  177. restore_register_in_frame(r13, f) \
  178. restore_register_in_frame(r12, f) \
  179. restore_register_in_frame(rdi, f) \
  180. restore_register_in_frame(rsi, f) \
  181. restore_register_in_frame(rbx, f) \
  182. restore_register_in_frame(rbp, f) \
  183. restore_register_in_frame(rsp, f)
  184. #else /* __x86_64__ */
  185. # error "unsupported architecture"
  186. #endif
  187. #define PAL_FRAME_IDENTIFIER (0xdeaddeadbeefbeef)
  188. struct pal_frame {
  189. volatile uint64_t identifier;
  190. void * func;
  191. const char * funcname;
  192. struct arch_frame arch;
  193. };
  194. /* When a PAL call is issued, a special PAL_FRAME is placed on the stack.
  195. * This stores both a magic identifier, debugging information,
  196. * as well as callee-saved state. This is used as a way to deal
  197. * with PAL-internal failures where the goal is to exit the PAL and return a
  198. * failure.
  199. *
  200. * Arguably, an alternative is to unwind the stack and handle error cases at
  201. * each stage. In general, this is probably more robust, but would take work
  202. * in the short term. The one exception where the current strategy is
  203. * probably better is when the PAL gets in a state where the code is
  204. * unrecoverable, but ideally, this shouldn't happen.
  205. */
  206. /* DEP 12/25/17: This frame storage thing is important to mark volatile.
  207. * The compiler should not optimize out any of these changes, and
  208. * because some accesses can happen during an exception, these are not
  209. * visible to the compiler in an otherwise stack-local variable (so the
  210. * compiler will try to optimize out these assignments.
  211. */
  212. static inline
  213. void __store_frame (volatile struct pal_frame * frame,
  214. void * func, const char * funcname)
  215. {
  216. arch_store_frame(&frame->arch)
  217. frame->func = func;
  218. frame->funcname = funcname;
  219. asm volatile ("nop" ::: "memory");
  220. frame->identifier = PAL_FRAME_IDENTIFIER;
  221. }
  222. #define ENTER_PAL_CALL(name) \
  223. struct pal_frame frame; \
  224. __store_frame(&frame, &(name), #name)
  225. static inline
  226. void __clear_frame (volatile struct pal_frame * frame)
  227. {
  228. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  229. asm volatile ("nop" ::: "memory");
  230. frame->identifier = 0;
  231. }
  232. }
  233. #define LEAVE_PAL_CALL() \
  234. do { \
  235. __clear_frame(&frame); \
  236. } while (0)
  237. #define LEAVE_PAL_CALL_RETURN(retval) \
  238. do { \
  239. __clear_frame(&frame); \
  240. return (retval); \
  241. } while (0)
  242. #endif /* PAL_HOST_H */