pal_host.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. void * malloc_untrusted (int size);
  36. void free_untrusted (void * mem);
  37. #include <list.h>
  38. /* Simpler mutex design: a single variable that tracks whether the mutex
  39. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  40. * 0 (unlocked).
  41. *
  42. * Keep a count of how many threads are waiting on the mutex.
  43. *
  44. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  45. * mutex locking. */
  46. struct mutex_handle {
  47. volatile int64_t * locked;
  48. struct atomic_int nwaiters;
  49. #ifdef DEBUG_MUTEX
  50. int owner;
  51. #endif
  52. };
  53. /* Initializer of Mutexes */
  54. #define MUTEX_HANDLE_INIT { .u = 0 }
  55. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  56. DEFINE_LIST(pal_handle_thread);
  57. struct pal_handle_thread {
  58. PAL_HDR reserved;
  59. PAL_IDX tid;
  60. PAL_PTR tcs;
  61. LIST_TYPE(pal_handle_thread) list;
  62. void * param;
  63. };
  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[2];
  74. } generic;
  75. struct {
  76. PAL_IDX fd;
  77. PAL_BOL append;
  78. PAL_BOL pass;
  79. PAL_STR realpath;
  80. PAL_NUM total;
  81. PAL_PTR stubs;
  82. } file;
  83. struct {
  84. PAL_IDX fd;
  85. PAL_NUM pipeid;
  86. PAL_BOL nonblocking;
  87. } pipe;
  88. struct {
  89. PAL_IDX fds[2];
  90. PAL_BOL nonblocking;
  91. } pipeprv;
  92. struct {
  93. PAL_IDX fd_in, fd_out;
  94. PAL_IDX dev_type;
  95. PAL_BOL destroy;
  96. PAL_STR realpath;
  97. } dev;
  98. struct {
  99. PAL_IDX fd;
  100. PAL_STR realpath;
  101. PAL_PTR buf;
  102. PAL_PTR ptr;
  103. PAL_PTR end;
  104. PAL_BOL endofstream;
  105. } dir;
  106. struct {
  107. PAL_IDX fd;
  108. } gipc;
  109. struct {
  110. PAL_IDX fd;
  111. PAL_PTR bind;
  112. PAL_PTR conn;
  113. PAL_BOL nonblocking;
  114. PAL_NUM linger;
  115. PAL_NUM receivebuf;
  116. PAL_NUM sendbuf;
  117. PAL_NUM receivetimeout;
  118. PAL_NUM sendtimeout;
  119. PAL_BOL tcp_cork;
  120. PAL_BOL tcp_keepalive;
  121. PAL_BOL tcp_nodelay;
  122. } sock;
  123. struct {
  124. PAL_IDX stream_in, stream_out;
  125. PAL_IDX cargo;
  126. PAL_IDX pid;
  127. PAL_BOL nonblocking;
  128. } process;
  129. struct {
  130. PAL_IDX cli;
  131. PAL_IDX srv;
  132. PAL_IDX port;
  133. PAL_BOL nonblocking;
  134. } mcast;
  135. struct pal_handle_thread thread;
  136. struct {
  137. struct atomic_int nwaiters;
  138. PAL_NUM max_value;
  139. union {
  140. struct mutex_handle mut;
  141. } mutex;
  142. struct {
  143. struct atomic_int * signaled;
  144. struct atomic_int nwaiters;
  145. PAL_BOL isnotification;
  146. } event;
  147. };
  148. };
  149. } * PAL_HANDLE;
  150. #define RFD(n) (00001 << (n))
  151. #define WFD(n) (00010 << (n))
  152. #define WRITEABLE(n) (00100 << (n))
  153. #define ERROR(n) (01000 << (n))
  154. #define MAX_FDS (3)
  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. static inline
  204. void __store_frame (struct pal_frame * frame,
  205. void * func, const char * funcname)
  206. {
  207. arch_store_frame(&frame->arch)
  208. frame->func = func;
  209. frame->funcname = funcname;
  210. asm volatile ("nop" ::: "memory");
  211. frame->identifier = PAL_FRAME_IDENTIFIER;
  212. }
  213. #define ENTER_PAL_CALL(name) \
  214. struct pal_frame frame; \
  215. __store_frame(&frame, &(name), #name)
  216. static inline
  217. void __clear_frame (struct pal_frame * frame)
  218. {
  219. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  220. asm volatile ("nop" ::: "memory");
  221. frame->identifier = 0;
  222. }
  223. }
  224. #define LEAVE_PAL_CALL() \
  225. do { \
  226. __clear_frame(&frame); \
  227. } while (0)
  228. #define LEAVE_PAL_CALL_RETURN(retval) \
  229. do { \
  230. __clear_frame(&frame); \
  231. return (retval); \
  232. } while (0)
  233. #endif /* PAL_HOST_H */