pal_host.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include <atomic.h>
  26. /* Spinlocking */
  27. typedef struct spinlock {
  28. struct atomic_int value;
  29. } PAL_LOCK;
  30. int _DkSpinLock (struct spinlock * lock);
  31. int _DkSpinUnlock (struct spinlock * lock);
  32. #define LOCK_INIT { .value = { 0 } }
  33. #define _DkInternalLock _DkSpinLock
  34. #define _DkInternalUnlock _DkSpinUnlock
  35. #define MAX_FDS 3
  36. void * malloc_untrusted (int size);
  37. void free_untrusted (void * mem);
  38. #include <list.h>
  39. /* Simpler mutex design: a single variable that tracks whether the mutex
  40. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  41. * 0 (unlocked).
  42. *
  43. * Keep a count of how many threads are waiting on the mutex.
  44. *
  45. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  46. * mutex locking. */
  47. struct mutex_handle {
  48. volatile int64_t * locked;
  49. struct atomic_int nwaiters;
  50. #ifdef DEBUG_MUTEX
  51. int owner;
  52. #endif
  53. };
  54. /* Initializer of Mutexes */
  55. #define MUTEX_HANDLE_INIT { .u = 0 }
  56. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  57. DEFINE_LIST(pal_handle_thread);
  58. struct pal_handle_thread {
  59. PAL_HDR reserved;
  60. PAL_IDX tid;
  61. PAL_PTR tcs;
  62. LIST_TYPE(pal_handle_thread) list;
  63. void * param;
  64. };
  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_BOL append;
  79. PAL_BOL pass;
  80. PAL_STR realpath;
  81. PAL_NUM total;
  82. PAL_PTR stubs;
  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_in, fd_out;
  95. PAL_IDX dev_type;
  96. PAL_BOL destroy;
  97. PAL_STR realpath;
  98. } dev;
  99. struct {
  100. PAL_IDX fd;
  101. PAL_STR realpath;
  102. PAL_PTR buf;
  103. PAL_PTR ptr;
  104. PAL_PTR end;
  105. PAL_BOL endofstream;
  106. } dir;
  107. struct {
  108. PAL_IDX fd;
  109. } gipc;
  110. struct {
  111. PAL_IDX fd;
  112. PAL_PTR bind;
  113. PAL_PTR conn;
  114. PAL_BOL nonblocking;
  115. PAL_NUM linger;
  116. PAL_NUM receivebuf;
  117. PAL_NUM sendbuf;
  118. PAL_NUM receivetimeout;
  119. PAL_NUM sendtimeout;
  120. PAL_BOL tcp_cork;
  121. PAL_BOL tcp_keepalive;
  122. PAL_BOL tcp_nodelay;
  123. } sock;
  124. struct {
  125. PAL_IDX stream_in, stream_out;
  126. PAL_IDX cargo;
  127. PAL_IDX pid;
  128. PAL_BOL nonblocking;
  129. } process;
  130. struct {
  131. PAL_IDX cli;
  132. PAL_IDX srv;
  133. PAL_IDX port;
  134. PAL_BOL nonblocking;
  135. } mcast;
  136. struct pal_handle_thread thread;
  137. struct {
  138. struct atomic_int nwaiters;
  139. PAL_NUM max_value;
  140. union {
  141. struct mutex_handle mut;
  142. } mutex;
  143. struct {
  144. struct atomic_int * signaled;
  145. struct atomic_int nwaiters;
  146. PAL_BOL isnotification;
  147. } event;
  148. };
  149. };
  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 HAS_FDS 00077
  156. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  157. struct arch_frame {
  158. #ifdef __x86_64__
  159. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  160. #else
  161. # error "unsupported architecture"
  162. #endif
  163. };
  164. #ifdef __x86_64__
  165. # define store_register(reg, var) \
  166. __asm__ volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  167. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  168. # define arch_store_frame(f) \
  169. store_register_in_frame(rsp, f) \
  170. store_register_in_frame(rbp, f) \
  171. store_register_in_frame(rbx, f) \
  172. store_register_in_frame(rsi, f) \
  173. store_register_in_frame(rdi, f) \
  174. store_register_in_frame(r12, f) \
  175. store_register_in_frame(r13, f) \
  176. store_register_in_frame(r14, f) \
  177. store_register_in_frame(r15, f)
  178. # define restore_register(reg, var, clobber...) \
  179. __asm__ volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  180. # define restore_register_in_frame(reg, f) \
  181. restore_register(reg, (f)->reg, \
  182. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  183. # define arch_restore_frame(f) \
  184. restore_register_in_frame(r15, f) \
  185. restore_register_in_frame(r14, f) \
  186. restore_register_in_frame(r13, f) \
  187. restore_register_in_frame(r12, f) \
  188. restore_register_in_frame(rdi, f) \
  189. restore_register_in_frame(rsi, f) \
  190. restore_register_in_frame(rbx, f) \
  191. restore_register_in_frame(rbp, f) \
  192. restore_register_in_frame(rsp, f)
  193. #else /* __x86_64__ */
  194. # error "unsupported architecture"
  195. #endif
  196. #define PAL_FRAME_IDENTIFIER 0xdeaddeadbeefbeef
  197. struct pal_frame {
  198. volatile uint64_t identifier;
  199. void * func;
  200. const char * funcname;
  201. struct arch_frame arch;
  202. };
  203. /* DEP 12/25/17: This frame storage thing is important to mark volatile.
  204. * The compiler should not optimize out any of these changes, and
  205. * because some accesses can happen during an exception, these are not
  206. * visible to the compiler in an otherwise stack-local variable (so the
  207. * compiler will try to optimize out these assignments.
  208. */
  209. static inline
  210. void __store_frame (volatile struct pal_frame * frame,
  211. void * func, const char * funcname)
  212. {
  213. arch_store_frame(&frame->arch)
  214. frame->func = func;
  215. frame->funcname = funcname;
  216. __asm__ volatile ("nop" ::: "memory");
  217. frame->identifier = PAL_FRAME_IDENTIFIER;
  218. }
  219. #define ENTER_PAL_CALL(name) \
  220. struct pal_frame frame; \
  221. __store_frame(&frame, &(name), #name)
  222. static inline
  223. void __clear_frame (volatile struct pal_frame * frame)
  224. {
  225. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  226. __asm__ volatile ("nop" ::: "memory");
  227. frame->identifier = 0;
  228. }
  229. }
  230. #define LEAVE_PAL_CALL() \
  231. do { \
  232. __clear_frame(&frame); \
  233. } while (0)
  234. #define LEAVE_PAL_CALL_RETURN(retval) \
  235. do { \
  236. __clear_frame(&frame); \
  237. return (retval); \
  238. } while (0)
  239. #endif /* PAL_HOST_H */