pal_host.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. void * malloc_untrusted (int size);
  34. void free_untrusted (void * mem);
  35. #include <list.h>
  36. /* Simpler mutex design: a single variable that tracks whether the mutex
  37. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  38. * 0 (unlocked).
  39. *
  40. * Keep a count of how many threads are waiting on the mutex.
  41. *
  42. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  43. * mutex locking. */
  44. struct mutex_handle {
  45. volatile int64_t * locked;
  46. struct atomic_int nwaiters;
  47. #ifdef DEBUG_MUTEX
  48. int owner;
  49. #endif
  50. };
  51. /* Initializer of Mutexes */
  52. #define MUTEX_HANDLE_INIT { .u = 0 }
  53. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  54. DEFINE_LIST(pal_handle_thread);
  55. struct pal_handle_thread {
  56. PAL_HDR reserved;
  57. PAL_IDX tid;
  58. PAL_PTR tcs;
  59. LIST_TYPE(pal_handle_thread) list;
  60. void * param;
  61. };
  62. /* RPC streams are encrypted with 256-bit AES keys */
  63. typedef uint8_t PAL_SESSION_KEY[32];
  64. typedef struct pal_handle
  65. {
  66. /*
  67. * Here we define the internal structure of PAL_HANDLE.
  68. * user has no access to the content inside these handles.
  69. */
  70. PAL_HDR hdr;
  71. union {
  72. struct {
  73. PAL_IDX fds[MAX_FDS];
  74. } generic;
  75. struct {
  76. PAL_IDX fd;
  77. PAL_STR realpath;
  78. PAL_NUM total;
  79. PAL_NUM offset;
  80. /* below fields are used only for trusted files */
  81. PAL_PTR stubs; /* contains hashes of file chunks */
  82. PAL_PTR umem; /* valid only when stubs != NULL */
  83. } file;
  84. struct {
  85. PAL_IDX fd;
  86. PAL_NUM pipeid;
  87. PAL_BOL nonblocking;
  88. } pipe;
  89. struct {
  90. PAL_IDX fds[MAX_FDS];
  91. PAL_BOL nonblocking;
  92. } pipeprv;
  93. struct {
  94. PAL_IDX fd;
  95. /* TODO: add other flags in future, if needed (e.g., semaphore) */
  96. PAL_BOL nonblocking;
  97. } eventfd;
  98. struct {
  99. PAL_IDX fd_in, fd_out;
  100. PAL_IDX dev_type;
  101. PAL_BOL destroy;
  102. PAL_STR realpath;
  103. } dev;
  104. struct {
  105. PAL_IDX fd;
  106. PAL_STR realpath;
  107. PAL_PTR buf;
  108. PAL_PTR ptr;
  109. PAL_PTR end;
  110. PAL_BOL endofstream;
  111. } dir;
  112. struct {
  113. PAL_IDX fd;
  114. PAL_PTR bind;
  115. PAL_PTR conn;
  116. PAL_BOL nonblocking;
  117. PAL_NUM linger;
  118. PAL_NUM receivebuf;
  119. PAL_NUM sendbuf;
  120. PAL_NUM receivetimeout;
  121. PAL_NUM sendtimeout;
  122. PAL_BOL tcp_cork;
  123. PAL_BOL tcp_keepalive;
  124. PAL_BOL tcp_nodelay;
  125. } sock;
  126. struct {
  127. PAL_IDX stream_in, stream_out;
  128. PAL_IDX cargo;
  129. PAL_IDX pid;
  130. PAL_BOL nonblocking;
  131. PAL_SESSION_KEY session_key;
  132. } process;
  133. struct {
  134. PAL_IDX cli;
  135. PAL_IDX srv;
  136. PAL_IDX port;
  137. PAL_BOL nonblocking;
  138. } mcast;
  139. struct pal_handle_thread thread;
  140. struct {
  141. struct atomic_int nwaiters;
  142. PAL_NUM max_value;
  143. union {
  144. struct mutex_handle mut;
  145. } mutex;
  146. struct {
  147. struct atomic_int * signaled;
  148. struct atomic_int nwaiters;
  149. PAL_BOL isnotification;
  150. } event;
  151. };
  152. };
  153. } * PAL_HANDLE;
  154. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  155. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  156. #define WRITABLE(n) (1 << (MAX_FDS*2 + (n)))
  157. #define ERROR(n) (1 << (MAX_FDS*3 + (n)))
  158. #define HAS_FDS ((1 << MAX_FDS*2) - 1)
  159. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  160. struct arch_frame {
  161. #ifdef __x86_64__
  162. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  163. #else
  164. # error "unsupported architecture"
  165. #endif
  166. };
  167. #ifdef __x86_64__
  168. # define store_register(reg, var) \
  169. __asm__ volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  170. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  171. # define arch_store_frame(f) \
  172. store_register_in_frame(rsp, f) \
  173. store_register_in_frame(rbp, f) \
  174. store_register_in_frame(rbx, f) \
  175. store_register_in_frame(rsi, f) \
  176. store_register_in_frame(rdi, f) \
  177. store_register_in_frame(r12, f) \
  178. store_register_in_frame(r13, f) \
  179. store_register_in_frame(r14, f) \
  180. store_register_in_frame(r15, f)
  181. # define restore_register(reg, var, clobber...) \
  182. __asm__ volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  183. # define restore_register_in_frame(reg, f) \
  184. restore_register(reg, (f)->reg, \
  185. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  186. # define arch_restore_frame(f) \
  187. restore_register_in_frame(r15, f) \
  188. restore_register_in_frame(r14, f) \
  189. restore_register_in_frame(r13, f) \
  190. restore_register_in_frame(r12, f) \
  191. restore_register_in_frame(rdi, f) \
  192. restore_register_in_frame(rsi, f) \
  193. restore_register_in_frame(rbx, f) \
  194. restore_register_in_frame(rbp, f) \
  195. restore_register_in_frame(rsp, f)
  196. #else /* __x86_64__ */
  197. # error "unsupported architecture"
  198. #endif
  199. #define PAL_FRAME_IDENTIFIER 0xdeaddeadbeefbeef
  200. struct pal_frame {
  201. volatile uint64_t identifier;
  202. void * func;
  203. const char * funcname;
  204. struct arch_frame arch;
  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 */