pal_host.h 8.3 KB

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