pal_host.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. union {
  30. unsigned int u;
  31. struct {
  32. unsigned char locked;
  33. unsigned char contended;
  34. } b;
  35. };
  36. #ifdef DEBUG_MUTEX
  37. int owner;
  38. #endif
  39. } PAL_LOCK;
  40. /* Initializer of Mutexes */
  41. #define MUTEX_HANDLE_INIT { .u = 0 }
  42. #define INIT_MUTEX_HANDLE(m) do { m->u = 0; } while (0)
  43. #define LOCK_INIT MUTEX_HANDLE_INIT
  44. #define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock);
  45. #define _DkInternalLock _DkMutexLock
  46. #define _DkInternalUnlock _DkMutexUnlock
  47. typedef union pal_handle
  48. {
  49. /* TSAI: Here we define the internal types of PAL_HANDLE
  50. * in PAL design, user has not to access the content inside the
  51. * handle, also there is no need to allocate the internal
  52. * handles, so we hide the type name of these handles on purpose.
  53. */
  54. struct {
  55. PAL_IDX type;
  56. PAL_REF ref;
  57. PAL_FLG flags;
  58. PAL_IDX fds[];
  59. } hdr;
  60. struct {
  61. PAL_HDR reserved;
  62. PAL_IDX fd;
  63. PAL_NUM offset;
  64. PAL_BOL append;
  65. PAL_BOL pass;
  66. PAL_STR realpath;
  67. } file;
  68. struct {
  69. PAL_HDR reserved;
  70. PAL_IDX fd;
  71. PAL_NUM pipeid;
  72. PAL_BOL nonblocking;
  73. } pipe;
  74. struct {
  75. PAL_HDR reserved;
  76. PAL_IDX fds[2];
  77. PAL_BOL nonblocking;
  78. } pipeprv;
  79. struct {
  80. PAL_HDR reserved;
  81. PAL_IDX fd_in, fd_out;
  82. PAL_IDX dev_type;
  83. PAL_BOL destroy;
  84. PAL_STR realpath;
  85. } dev;
  86. struct {
  87. PAL_HDR reserved;
  88. PAL_IDX fd;
  89. PAL_STR realpath;
  90. PAL_PTR buf;
  91. PAL_PTR ptr;
  92. PAL_PTR end;
  93. PAL_BOL endofstream;
  94. } dir;
  95. struct {
  96. PAL_HDR reserved;
  97. PAL_IDX fd;
  98. PAL_NUM token;
  99. } gipc;
  100. struct {
  101. PAL_HDR reserved;
  102. PAL_IDX fd;
  103. PAL_PTR bind;
  104. PAL_PTR conn;
  105. PAL_BOL nonblocking;
  106. PAL_BOL reuseaddr;
  107. PAL_NUM linger;
  108. PAL_NUM receivebuf;
  109. PAL_NUM sendbuf;
  110. PAL_NUM receivetimeout;
  111. PAL_NUM sendtimeout;
  112. PAL_BOL tcp_cork;
  113. PAL_BOL tcp_keepalive;
  114. PAL_BOL tcp_nodelay;
  115. } sock;
  116. struct {
  117. PAL_HDR reserved;
  118. PAL_IDX stream_in, stream_out;
  119. PAL_IDX cargo;
  120. PAL_IDX pid;
  121. PAL_BOL nonblocking;
  122. } process;
  123. struct {
  124. PAL_HDR reserved;
  125. PAL_IDX cli;
  126. PAL_IDX srv;
  127. PAL_IDX port;
  128. PAL_BOL nonblocking;
  129. PAL_PTR addr;
  130. } mcast;
  131. struct {
  132. PAL_HDR reserved;
  133. PAL_IDX tid;
  134. } thread;
  135. struct {
  136. PAL_HDR reserved;
  137. struct atomic_int nwaiters;
  138. PAL_NUM max_value;
  139. union {
  140. struct mutex_handle mut;
  141. struct atomic_int i;
  142. } value;
  143. } semaphore;
  144. struct {
  145. PAL_HDR reserved;
  146. struct atomic_int signaled;
  147. struct atomic_int nwaiters;
  148. PAL_BOL isnotification;
  149. } event;
  150. } * PAL_HANDLE;
  151. #define RFD(n) (00001 << (n))
  152. #define WFD(n) (00010 << (n))
  153. #define WRITEABLE(n) (00100 << (n))
  154. #define ERROR(n) (01000 << (n))
  155. #define MAX_FDS (3)
  156. #define HAS_FDS (00077)
  157. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  158. struct arch_frame {
  159. #ifdef __x86_64__
  160. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  161. #else
  162. # error "unsupported architecture"
  163. #endif
  164. };
  165. #ifdef __x86_64__
  166. # define store_register(reg, var) \
  167. asm volatile ("movq %%" #reg ", %0" : "=a" (var) :: "memory");
  168. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  169. # define arch_store_frame(f) \
  170. store_register_in_frame(rsp, f) \
  171. store_register_in_frame(rbp, f) \
  172. store_register_in_frame(rbx, f) \
  173. store_register_in_frame(rsi, f) \
  174. store_register_in_frame(rdi, f) \
  175. store_register_in_frame(r12, f) \
  176. store_register_in_frame(r13, f) \
  177. store_register_in_frame(r14, f) \
  178. store_register_in_frame(r15, f)
  179. # define restore_register(reg, var, clobber...) \
  180. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  181. # define restore_register_in_frame(reg, f) \
  182. restore_register(reg, (f)->reg, \
  183. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  184. # define arch_restore_frame(f) \
  185. restore_register_in_frame(r15, f) \
  186. restore_register_in_frame(r14, f) \
  187. restore_register_in_frame(r13, f) \
  188. restore_register_in_frame(r12, f) \
  189. restore_register_in_frame(rdi, f) \
  190. restore_register_in_frame(rsi, f) \
  191. restore_register_in_frame(rbx, f) \
  192. restore_register_in_frame(rbp, f) \
  193. restore_register_in_frame(rsp, f)
  194. #else /* __x86_64__ */
  195. # error "unsupported architecture"
  196. #endif
  197. #define PAL_FRAME_IDENTIFIER (0xdeaddeadbeefbeef)
  198. struct pal_frame {
  199. volatile uint64_t identifier;
  200. void * func;
  201. const char * funcname;
  202. struct arch_frame arch;
  203. };
  204. static inline
  205. void __store_frame (struct pal_frame * frame,
  206. void * func, const char * funcname)
  207. {
  208. arch_store_frame(&frame->arch)
  209. frame->func = func;
  210. frame->funcname = funcname;
  211. asm volatile ("nop" ::: "memory");
  212. frame->identifier = PAL_FRAME_IDENTIFIER;
  213. }
  214. #define ENTER_PAL_CALL(name) \
  215. struct pal_frame frame; \
  216. __store_frame(&frame, &(name), #name)
  217. static inline
  218. void __clear_frame (struct pal_frame * frame)
  219. {
  220. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  221. asm volatile ("nop" ::: "memory");
  222. frame->identifier = 0;
  223. }
  224. }
  225. #define LEAVE_PAL_CALL() \
  226. do { \
  227. __clear_frame(&frame); \
  228. } while (0)
  229. #define LEAVE_PAL_CALL_RETURN(retval) \
  230. do { \
  231. __clear_frame(&frame); \
  232. return (retval); \
  233. } while (0)
  234. #endif /* PAL_HOST_H */