pal_host.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <atomic.h>
  24. /* Spinlocking */
  25. typedef struct spinlock {
  26. struct atomic_int value;
  27. } PAL_LOCK;
  28. int _DkSpinLock (struct spinlock * lock);
  29. int _DkSpinUnlock (struct spinlock * lock);
  30. #define LOCK_INIT { .value = { 0 } }
  31. #define _DkInternalLock _DkSpinLock
  32. #define _DkInternalUnlock _DkSpinUnlock
  33. #define MAX_FDS 3
  34. void * malloc_untrusted (int size);
  35. void free_untrusted (void * mem);
  36. #include <list.h>
  37. /* Simpler mutex design: a single variable that tracks whether the mutex
  38. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  39. * 0 (unlocked).
  40. *
  41. * Keep a count of how many threads are waiting on the mutex.
  42. *
  43. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  44. * mutex locking. */
  45. struct mutex_handle {
  46. volatile int64_t * locked;
  47. struct atomic_int nwaiters;
  48. #ifdef DEBUG_MUTEX
  49. int owner;
  50. #endif
  51. };
  52. /* Initializer of Mutexes */
  53. #define MUTEX_HANDLE_INIT { .u = 0 }
  54. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  55. DEFINE_LIST(pal_handle_thread);
  56. struct pal_handle_thread {
  57. PAL_HDR reserved;
  58. PAL_IDX tid;
  59. PAL_PTR tcs;
  60. LIST_TYPE(pal_handle_thread) list;
  61. void * param;
  62. };
  63. /* RPC streams are encrypted with 256-bit AES keys */
  64. typedef uint8_t PAL_SESSION_KEY[32];
  65. typedef struct pal_handle
  66. {
  67. /*
  68. * Here we define the internal structure of PAL_HANDLE.
  69. * user has no access to the content inside these handles.
  70. */
  71. PAL_HDR hdr;
  72. union {
  73. struct {
  74. PAL_IDX fds[MAX_FDS];
  75. } generic;
  76. struct {
  77. PAL_IDX fd;
  78. PAL_STR realpath;
  79. PAL_NUM total;
  80. PAL_NUM offset;
  81. /* below fields are used only for trusted files */
  82. PAL_PTR stubs; /* contains hashes of file chunks */
  83. PAL_PTR umem; /* valid only when stubs != NULL */
  84. } file;
  85. struct {
  86. PAL_IDX fd;
  87. PAL_NUM pipeid;
  88. PAL_BOL nonblocking;
  89. } pipe;
  90. struct {
  91. PAL_IDX fds[MAX_FDS];
  92. PAL_BOL nonblocking;
  93. } pipeprv;
  94. struct {
  95. PAL_IDX fd_in, fd_out;
  96. PAL_IDX dev_type;
  97. PAL_BOL destroy;
  98. PAL_STR realpath;
  99. } dev;
  100. struct {
  101. PAL_IDX fd;
  102. PAL_STR realpath;
  103. PAL_PTR buf;
  104. PAL_PTR ptr;
  105. PAL_PTR end;
  106. PAL_BOL endofstream;
  107. } dir;
  108. struct {
  109. PAL_IDX fd;
  110. } gipc;
  111. struct {
  112. PAL_IDX fd;
  113. PAL_PTR bind;
  114. PAL_PTR conn;
  115. PAL_BOL nonblocking;
  116. PAL_NUM linger;
  117. PAL_NUM receivebuf;
  118. PAL_NUM sendbuf;
  119. PAL_NUM receivetimeout;
  120. PAL_NUM sendtimeout;
  121. PAL_BOL tcp_cork;
  122. PAL_BOL tcp_keepalive;
  123. PAL_BOL tcp_nodelay;
  124. } sock;
  125. struct {
  126. PAL_IDX stream_in, stream_out;
  127. PAL_IDX cargo;
  128. PAL_IDX pid;
  129. PAL_BOL nonblocking;
  130. PAL_SESSION_KEY session_key;
  131. } process;
  132. struct {
  133. PAL_IDX cli;
  134. PAL_IDX srv;
  135. PAL_IDX port;
  136. PAL_BOL nonblocking;
  137. } mcast;
  138. struct pal_handle_thread thread;
  139. struct {
  140. struct atomic_int nwaiters;
  141. PAL_NUM max_value;
  142. union {
  143. struct mutex_handle mut;
  144. } mutex;
  145. struct {
  146. struct atomic_int * signaled;
  147. struct atomic_int nwaiters;
  148. PAL_BOL isnotification;
  149. } event;
  150. };
  151. };
  152. } * PAL_HANDLE;
  153. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  154. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  155. #define WRITABLE(n) (1 << (MAX_FDS*2 + (n)))
  156. #define ERROR(n) (1 << (MAX_FDS*3 + (n)))
  157. #define HAS_FDS ((1 << MAX_FDS*2) - 1)
  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" : "=g" (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. /* DEP 12/25/17: This frame storage thing is important to mark volatile.
  206. * The compiler should not optimize out any of these changes, and
  207. * because some accesses can happen during an exception, these are not
  208. * visible to the compiler in an otherwise stack-local variable (so the
  209. * compiler will try to optimize out these assignments.
  210. */
  211. static inline
  212. void __store_frame (volatile struct pal_frame * frame,
  213. void * func, const char * funcname)
  214. {
  215. arch_store_frame(&frame->arch)
  216. frame->func = func;
  217. frame->funcname = funcname;
  218. __asm__ volatile ("nop" ::: "memory");
  219. frame->identifier = PAL_FRAME_IDENTIFIER;
  220. }
  221. #define ENTER_PAL_CALL(name) \
  222. struct pal_frame frame; \
  223. __store_frame(&frame, &(name), #name)
  224. static inline
  225. void __clear_frame (volatile struct pal_frame * frame)
  226. {
  227. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  228. __asm__ volatile ("nop" ::: "memory");
  229. frame->identifier = 0;
  230. }
  231. }
  232. #define LEAVE_PAL_CALL() \
  233. do { \
  234. __clear_frame(&frame); \
  235. } while (0)
  236. #define LEAVE_PAL_CALL_RETURN(retval) \
  237. do { \
  238. __clear_frame(&frame); \
  239. return (retval); \
  240. } while (0)
  241. #endif /* PAL_HOST_H */