pal_host.h 7.4 KB

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