shim_internal.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. * shim_internal.h
  17. */
  18. #ifndef _SHIM_INTERNAL_H_
  19. #define _SHIM_INTERNAL_H_
  20. #ifndef IN_SHIM
  21. #error "this header file can only be used inside SHIM"
  22. #endif
  23. #define attribute_hidden __attribute__ ((visibility ("hidden")))
  24. #define alias_str(name) #name
  25. #define extern_alias(name) \
  26. extern __typeof(name) shim_##name __attribute ((alias (alias_str(name))))
  27. #define static_always_inline static inline __attribute__((always_inline))
  28. #include <shim_types.h>
  29. #include <shim_defs.h>
  30. #include <atomic.h>
  31. #include <shim_tls.h>
  32. /* important macros */
  33. #define get_cur_tid() (SHIM_GET_TLS()->tid)
  34. #define PAL_NATIVE_ERRNO (SHIM_GET_TLS()->pal_errno)
  35. #define INTERNAL_TID_BASE ((IDTYPE) 1 << (sizeof(IDTYPE) * 8 - 1))
  36. #define IS_INTERNAL_TID(tid) ((tid) >= INTERNAL_TID_BASE)
  37. #define IS_INTERNAL(thread) ((thread)->tid >= INTERNAL_TID_BASE)
  38. #define TID_PRINTFMT
  39. struct debug_buf {
  40. int start;
  41. int end;
  42. char buf[DEBUGBUF_SIZE];
  43. };
  44. # include <pal.h>
  45. # include <pal_debug.h>
  46. # include <pal_error.h>
  47. extern PAL_HANDLE debug_handle;
  48. # include <stdarg.h>
  49. void debug_printf (const char * fmt, ...);
  50. void debug_puts (const char * str);
  51. void debug_putch (int ch);
  52. void debug_vprintf (const char * fmt, va_list * ap);
  53. # define VMID_PREFIX "[P%05u] "
  54. # define TID_PREFIX "[%-6u] "
  55. # define NOID_PREFIX "[ ] "
  56. # define debug(fmt, ...) \
  57. do { \
  58. if (debug_handle) \
  59. debug_printf((fmt), ##__VA_ARGS__); \
  60. } while (0)
  61. /* print system messages */
  62. #define SYSPRINT_BUFFER_SIZE 256
  63. void handle_printf (PAL_HANDLE hdl, const char * fmt, ...);
  64. void handle_vprintf (PAL_HANDLE hdl, const char * fmt, va_list * ap);
  65. #define __sys_printf(fmt, ...) \
  66. do { \
  67. PAL_HANDLE _hdl = __open_shim_stdio(); \
  68. if (_hdl) \
  69. handle_printf(_hdl, (fmt), ##__VA_ARGS__); \
  70. } while (0)
  71. #define __sys_vprintf(fmt, va) \
  72. do { \
  73. PAL_HANDLE _hdl = __open_shim_stdio(); \
  74. if (_hdl) \
  75. handle_vprintf(_hdl, (fmt), (va)); \
  76. } while (0)
  77. #define __sys_fprintf(hdl, fmt, ...) \
  78. do { \
  79. handle_printf((hdl), (fmt), ##__VA_ARGS__); \
  80. } while (0)
  81. #define sys_printf(fmt, ...) \
  82. do { \
  83. master_lock(); \
  84. __sys_printf((fmt), ##__VA_ARGS__); \
  85. master_unlock(); \
  86. } while (0)
  87. #define sys_fprintf(hdl, fmt, ...) \
  88. do { \
  89. master_lock(); \
  90. __sys_fprintf((hdl), (fmt), ##__VA_ARGS__); \
  91. master_unlock(); \
  92. } while (0)
  93. extern PAL_HANDLE shim_stdio;
  94. static inline PAL_HANDLE __open_shim_stdio (void)
  95. {
  96. if (shim_stdio == (PAL_HANDLE) -1)
  97. return NULL;
  98. if (shim_stdio)
  99. return shim_stdio;
  100. shim_stdio = DkStreamOpen("dev:tty", PAL_ACCESS_RDWR, 0, 0, 0);
  101. if (!shim_stdio) {
  102. shim_stdio = (PAL_HANDLE) -1;
  103. return NULL;
  104. }
  105. return shim_stdio;
  106. }
  107. int shim_terminate (void);
  108. /* assertions */
  109. #define USE_PAUSE 0
  110. #define USE_ASSERT 1
  111. static inline void do_pause (void);
  112. #if USE_PAUSE == 1
  113. # define pause() do { do_pause(); } while (0)
  114. #else
  115. # define pause() do { asm volatile ("int $3"); } while (0)
  116. #endif
  117. #define bug() \
  118. do { \
  119. __sys_printf("bug() " __FILE__ ":%d\n", __LINE__); \
  120. pause(); \
  121. shim_terminate(); \
  122. } while (0)
  123. #if USE_ASSERT == 1
  124. #include <assert.h>
  125. #else
  126. # define assert(test) do {} while (0)
  127. #endif
  128. #define DEBUG_HERE() \
  129. do { debug("%s (" __FILE__ ":%d)\n", __func__, __LINE__); } while (0)
  130. /* definition for syscall table */
  131. void handle_signal (bool delayed_only);
  132. long convert_pal_errno (long err);
  133. #define PAL_ERRNO convert_pal_errno(PAL_NATIVE_ERRNO)
  134. #define SHIM_ARG_TYPE long
  135. #ifdef PROFILE
  136. # define ENTER_TIME SHIM_GET_TLS()->context.enter_time
  137. # define BEGIN_SYSCALL_PROFILE() \
  138. do { ENTER_TIME = GET_PROFILE_INTERVAL(); } while (0)
  139. # define END_SYSCALL_PROFILE(name) \
  140. do { unsigned long _interval = GET_PROFILE_INTERVAL(); \
  141. if (_interval - ENTER_TIME > 1000) \
  142. SAVE_PROFILE_INTERVAL_SET(syscall_##name##_slow, ENTER_TIME, _interval); \
  143. else \
  144. SAVE_PROFILE_INTERVAL_SET(syscall_##name, ENTER_TIME, _interval); \
  145. ENTER_TIME = 0; } while (0)
  146. #else
  147. # define BEGIN_SYSCALL_PROFILE() do {} while (0)
  148. # define END_SYSCALL_PROFILE(name) do {} while (0)
  149. #endif
  150. void check_stack_hook (void);
  151. static inline uint64_t get_cur_preempt (void) {
  152. shim_tcb_t* tcb = SHIM_GET_TLS();
  153. assert(tcb);
  154. return tcb->context.preempt;
  155. }
  156. #define BEGIN_SHIM(name, args ...) \
  157. SHIM_ARG_TYPE __shim_##name (args) { \
  158. SHIM_ARG_TYPE ret = 0; \
  159. uint64_t preempt = get_cur_preempt(); \
  160. /* handle_signal(true); */ \
  161. /* check_stack_hook(); */ \
  162. BEGIN_SYSCALL_PROFILE();
  163. #define END_SHIM(name) \
  164. END_SYSCALL_PROFILE(name); \
  165. handle_signal(false); \
  166. assert(preempt == get_cur_preempt()); \
  167. return ret; \
  168. }
  169. #define DEFINE_SHIM_SYSCALL(name, n, func, ...) \
  170. DEFINE_PROFILE_INTERVAL(syscall_##name##_slow, syscall); \
  171. DEFINE_PROFILE_INTERVAL(syscall_##name, syscall); \
  172. SHIM_SYSCALL_##n (name, func, __VA_ARGS__) \
  173. EXPORT_SHIM_SYSCALL (name, n, __VA_ARGS__)
  174. #define PROTO_ARGS_0() void
  175. #define PROTO_ARGS_1(t, a) t a
  176. #define PROTO_ARGS_2(t, a, rest ...) t a, PROTO_ARGS_1(rest)
  177. #define PROTO_ARGS_3(t, a, rest ...) t a, PROTO_ARGS_2(rest)
  178. #define PROTO_ARGS_4(t, a, rest ...) t a, PROTO_ARGS_3(rest)
  179. #define PROTO_ARGS_5(t, a, rest ...) t a, PROTO_ARGS_4(rest)
  180. #define PROTO_ARGS_6(t, a, rest ...) t a, PROTO_ARGS_5(rest)
  181. #define CAST_ARGS_0()
  182. #define CAST_ARGS_1(t, a) (SHIM_ARG_TYPE) a
  183. #define CAST_ARGS_2(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_1(rest)
  184. #define CAST_ARGS_3(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_2(rest)
  185. #define CAST_ARGS_4(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_3(rest)
  186. #define CAST_ARGS_5(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_4(rest)
  187. #define CAST_ARGS_6(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_5(rest)
  188. #define DEFINE_SHIM_FUNC(func, n, r, args ...) \
  189. r func (PROTO_ARGS_##n (args));
  190. #define TYPE_HASH(t) ({ const char * _s = #t; \
  191. ((uint16_t) _s[0] << 8) + _s[1]; })
  192. #define POINTER_TYPE(t) ({ int _h = TYPE_HASH(t); \
  193. _h == TYPE_HASH(void *) || _h == TYPE_HASH(char *) || \
  194. _h == TYPE_HASH(const); })
  195. #define EXPORT_SHIM_SYSCALL(name, n, r, args ...) \
  196. r shim_##name (PROTO_ARGS_##n (args)) { \
  197. SHIM_ARG_TYPE ret = __shim_##name (CAST_ARGS_##n (args)); \
  198. if (POINTER_TYPE(r)) { \
  199. if ((unsigned long) ret >= -4095L) return (r) 0; \
  200. } else { \
  201. if ((int) ret < 0) return (r) -1; \
  202. } \
  203. return (r) ret; \
  204. }
  205. #define PARSE_SYSCALL1(name, ...) \
  206. if (debug_handle) \
  207. parse_syscall_before(__NR_##name, #name, ##__VA_ARGS__);
  208. #define PARSE_SYSCALL2(name, ...) \
  209. if (debug_handle) \
  210. parse_syscall_after(__NR_##name, #name, ##__VA_ARGS__);
  211. void parse_syscall_before (int sysno, const char * name, int nr, ...);
  212. void parse_syscall_after (int sysno, const char * name, int nr, ...);
  213. #define SHIM_SYSCALL_0(name, func, r) \
  214. BEGIN_SHIM(name, void) \
  215. PARSE_SYSCALL1(name, 0); \
  216. r __ret = func(); \
  217. PARSE_SYSCALL2(name, 0, #r, __ret); \
  218. ret = (SHIM_ARG_TYPE) __ret; \
  219. END_SHIM(name)
  220. #define SHIM_SYSCALL_1(name, func, r, t1, a1) \
  221. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1) \
  222. t1 a1 = (t1) __arg1; \
  223. PARSE_SYSCALL1(name, 1, #t1, a1); \
  224. r __ret = func(a1); \
  225. PARSE_SYSCALL2(name, 1, #r, __ret, #t1, a1); \
  226. ret = (SHIM_ARG_TYPE) __ret; \
  227. END_SHIM(name)
  228. #define SHIM_SYSCALL_2(name, func, r, t1, a1, t2, a2) \
  229. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2) \
  230. t1 a1 = (t1) __arg1; \
  231. t2 a2 = (t2) __arg2; \
  232. PARSE_SYSCALL1(name, 2, #t1, a1, #t2, a2); \
  233. r __ret = func(a1, a2); \
  234. PARSE_SYSCALL2(name, 2, #r, __ret, #t1, a1, #t2, a2); \
  235. ret = (SHIM_ARG_TYPE) __ret; \
  236. END_SHIM(name)
  237. #define SHIM_SYSCALL_3(name, func, r, t1, a1, t2, a2, t3, a3) \
  238. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  239. SHIM_ARG_TYPE __arg3) \
  240. t1 a1 = (t1) __arg1; \
  241. t2 a2 = (t2) __arg2; \
  242. t3 a3 = (t3) __arg3; \
  243. PARSE_SYSCALL1(name, 3, #t1, a1, #t2, a2, #t3, a3); \
  244. r __ret = func(a1, a2, a3); \
  245. PARSE_SYSCALL2(name, 3, #r, __ret, #t1, a1, #t2, a2, #t3, a3); \
  246. ret = (SHIM_ARG_TYPE) __ret; \
  247. END_SHIM(name)
  248. #define SHIM_SYSCALL_4(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4) \
  249. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  250. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4) \
  251. t1 a1 = (t1) __arg1; \
  252. t2 a2 = (t2) __arg2; \
  253. t3 a3 = (t3) __arg3; \
  254. t4 a4 = (t4) __arg4; \
  255. PARSE_SYSCALL1(name, 4, #t1, a1, #t2, a2, #t3, a3, #t4, a4); \
  256. r __ret = func(a1, a2, a3, a4); \
  257. PARSE_SYSCALL2(name, 4, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  258. #t4, a4); \
  259. ret = (SHIM_ARG_TYPE) __ret; \
  260. END_SHIM(name)
  261. #define SHIM_SYSCALL_5(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
  262. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  263. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4, \
  264. SHIM_ARG_TYPE __arg5) \
  265. t1 a1 = (t1) __arg1; \
  266. t2 a2 = (t2) __arg2; \
  267. t3 a3 = (t3) __arg3; \
  268. t4 a4 = (t4) __arg4; \
  269. t5 a5 = (t5) __arg5; \
  270. PARSE_SYSCALL1(name, 5, #t1, a1, #t2, a2, #t3, a3, #t4, a4, \
  271. #t5, a5); \
  272. r __ret = func(a1, a2, a3, a4, a5); \
  273. PARSE_SYSCALL2(name, 5, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  274. #t4, a4, #t5, a5); \
  275. ret = (SHIM_ARG_TYPE) __ret; \
  276. END_SHIM(name)
  277. #define SHIM_SYSCALL_6(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
  278. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  279. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4, \
  280. SHIM_ARG_TYPE __arg5, SHIM_ARG_TYPE __arg6) \
  281. t1 a1 = (t1) __arg1; \
  282. t2 a2 = (t2) __arg2; \
  283. t3 a3 = (t3) __arg3; \
  284. t4 a4 = (t4) __arg4; \
  285. t5 a5 = (t5) __arg5; \
  286. t6 a6 = (t6) __arg6; \
  287. PARSE_SYSCALL1(name, 6, #t1, a1, #t2, a2, #t3, a3, #t4, a4, \
  288. #t5, a5, #t6, a6); \
  289. r __ret = func(a1, a2, a3, a4, a5, a6); \
  290. PARSE_SYSCALL2(name, 6, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  291. #t4, a4, #t5, a5, #t6, a6); \
  292. ret = (SHIM_ARG_TYPE) __ret; \
  293. END_SHIM(name)
  294. #define SHIM_PROTO_ARGS_0 void
  295. #define SHIM_PROTO_ARGS_1 SHIM_ARG_TYPE __arg1
  296. #define SHIM_PROTO_ARGS_2 SHIM_PROTO_ARGS_1, SHIM_ARG_TYPE __arg2
  297. #define SHIM_PROTO_ARGS_3 SHIM_PROTO_ARGS_2, SHIM_ARG_TYPE __arg3
  298. #define SHIM_PROTO_ARGS_4 SHIM_PROTO_ARGS_3, SHIM_ARG_TYPE __arg4
  299. #define SHIM_PROTO_ARGS_5 SHIM_PROTO_ARGS_4, SHIM_ARG_TYPE __arg5
  300. #define SHIM_PROTO_ARGS_6 SHIM_PROTO_ARGS_5, SHIM_ARG_TYPE __arg6
  301. #define SHIM_PASS_ARGS_1 __arg1
  302. #define SHIM_PASS_ARGS_2 SHIM_PASS_ARGS_1, __arg2
  303. #define SHIM_PASS_ARGS_3 SHIM_PASS_ARGS_2, __arg3
  304. #define SHIM_PASS_ARGS_4 SHIM_PASS_ARGS_3, __arg4
  305. #define SHIM_PASS_ARGS_5 SHIM_PASS_ARGS_4, __arg5
  306. #define SHIM_PASS_ARGS_6 SHIM_PASS_ARGS_5, __arg6
  307. #define DO_SYSCALL(...) DO_SYSCALL2(__VA_ARGS__)
  308. #define DO_SYSCALL2(n, ...) -ENOSYS
  309. #define DO_SYSCALL_0(sysno) -ENOSYS
  310. #define DO_SYSCALL_1(sysno, ...) DO_SYSCALL(1, sysno, SHIM_PASS_ARGS_1)
  311. #define DO_SYSCALL_2(sysno, ...) DO_SYSCALL(2, sysno, SHIM_PASS_ARGS_2)
  312. #define DO_SYSCALL_3(sysno, ...) DO_SYSCALL(3, sysno, SHIM_PASS_ARGS_3)
  313. #define DO_SYSCALL_4(sysno, ...) DO_SYSCALL(4, sysno, SHIM_PASS_ARGS_4)
  314. #define DO_SYSCALL_5(sysno, ...) DO_SYSCALL(5, sysno, SHIM_PASS_ARGS_5)
  315. #define DO_SYSCALL_6(sysno, ...) DO_SYSCALL(6, sysno, SHIM_PASS_ARGS_6)
  316. #define SHIM_SYSCALL_PASSTHROUGH(name, n, ...) \
  317. DEFINE_PROFILE_INTERVAL(syscall_##name##_slow, syscall); \
  318. DEFINE_PROFILE_INTERVAL(syscall_##name, syscall); \
  319. BEGIN_SHIM(name, SHIM_PROTO_ARGS_##n) \
  320. debug("WARNING: shim_" #name " not implemented\n"); \
  321. ret = DO_SYSCALL_##n(__NR_##name); \
  322. END_SHIM(name) \
  323. EXPORT_SHIM_SYSCALL(name, n, __VA_ARGS__)
  324. #ifndef container_of
  325. /**
  326. * container_of - cast a member of a structure out to the containing structure
  327. * @ptr: the pointer to the member.
  328. * @type: the type of the container struct this is embedded in.
  329. * @field: the name of the field within the struct.
  330. *
  331. */
  332. #define container_of(ptr, type, field) ((type *)((char *)(ptr) - offsetof(type, field)))
  333. #endif
  334. #define CONCAT2(t1, t2) __CONCAT2(t1, t2)
  335. #define __CONCAT2(t1, t2) t1##_##t2
  336. #define CONCAT3(t1, t2, t3) __CONCAT3(t1, t2, t3)
  337. #define __CONCAT3(t1, t2, t3) t1##_##t2##_##t3
  338. /* Some SHIM internal errno */
  339. #define EISLINK 141 /* the path is a link */
  340. #define ECONTAINLINK 142 /* part of path contains a link */
  341. #define ENOTLINK 143 /* the path is not a link */
  342. #define ESKIPPED 144 /* skip looking up current path */
  343. #define PAL_CB(member) (pal_control.member)
  344. #define LOCK_FREE ((IDTYPE) -1)
  345. extern bool lock_enabled;
  346. static inline void enable_locking (void)
  347. {
  348. if (!lock_enabled)
  349. lock_enabled = true;
  350. }
  351. static inline PAL_HANDLE thread_create (void * func, void * arg, int option)
  352. {
  353. assert(lock_enabled);
  354. return DkThreadCreate(func, arg, option);
  355. }
  356. static inline void __disable_preempt (shim_tcb_t * tcb)
  357. {
  358. //tcb->context.syscall_nr += SYSCALL_NR_PREEMPT_INC;
  359. /* Assert if this counter overflows */
  360. assert((tcb->context.preempt & ~SIGNAL_DELAYED) != ~SIGNAL_DELAYED);
  361. tcb->context.preempt++;
  362. //debug("disable preempt: %d\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  363. }
  364. static inline void disable_preempt (shim_tcb_t * tcb)
  365. {
  366. if (!tcb && !(tcb = SHIM_GET_TLS()))
  367. return;
  368. __disable_preempt(tcb);
  369. }
  370. static inline void __enable_preempt (shim_tcb_t * tcb)
  371. {
  372. //tcb->context.syscall_nr -= SYSCALL_NR_PREEMPT_INC;
  373. /* Assert if this counter underflows */
  374. assert(tcb->context.preempt > 0);
  375. tcb->context.preempt--;
  376. //debug("enable preempt: %d\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  377. }
  378. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc);
  379. static inline void enable_preempt (shim_tcb_t * tcb)
  380. {
  381. if (!tcb && !(tcb = SHIM_GET_TLS()))
  382. return;
  383. if (!(tcb->context.preempt & ~SIGNAL_DELAYED))
  384. return;
  385. if ((tcb->context.preempt & ~SIGNAL_DELAYED) == 1)
  386. __handle_signal(tcb, 0, NULL);
  387. __enable_preempt(tcb);
  388. }
  389. #define DEBUG_LOCK 0
  390. #define lock_created(l) ((l).lock != NULL)
  391. #define clear_lock(l) do { (l).lock = NULL; (l).owner = 0; } while (0)
  392. #define create_lock(l) \
  393. do { \
  394. (l).lock = DkMutexCreate(0); \
  395. /* (l).owner = LOCK_FREE; */ \
  396. /* (l).reowned = 0; */ \
  397. } while (0)
  398. #define destroy_lock(l) \
  399. do { \
  400. DkObjectClose((l).lock); \
  401. } while (0)
  402. #define try_create_lock(l) \
  403. do { if (!lock_created(l)) create_lock(l); } while (0)
  404. #if DEBUG_LOCK == 1
  405. # define lock(l) __lock(&(l), #l, __FILE__, __LINE__)
  406. static inline void __lock (LOCKTYPE * l,
  407. const char * name, const char * file, int line)
  408. #else
  409. # define lock(l) __lock(&(l))
  410. static inline void __lock (LOCKTYPE * l)
  411. #endif
  412. {
  413. if (!lock_enabled || !l->lock)
  414. return;
  415. shim_tcb_t * tcb = SHIM_GET_TLS();
  416. disable_preempt(tcb);
  417. #if DEBUG_LOCK == 1
  418. debug("try lock(%s=%p) %s:%d\n", name, l, file, line);
  419. #endif
  420. while (!DkObjectsWaitAny(1, &l->lock, NO_TIMEOUT));
  421. l->owner = tcb->tid;
  422. #if DEBUG_LOCK == 1
  423. debug("lock(%s=%p) by %s:%d\n", name, l, file, line);
  424. #endif
  425. }
  426. #if DEBUG_LOCK == 1
  427. # define unlock(l) __unlock(&(l), #l, __FILE__, __LINE__)
  428. static inline void __unlock (LOCKTYPE * l,
  429. const char * name, const char * file, int line)
  430. #else
  431. # define unlock(l) __unlock(&(l))
  432. static inline void __unlock (LOCKTYPE * l)
  433. #endif
  434. {
  435. if (!lock_enabled || !l->lock)
  436. return;
  437. shim_tcb_t * tcb = SHIM_GET_TLS();
  438. #if DEBUG_LOCK == 1
  439. debug("unlock(%s=%p) %s:%d\n", name, l, file, line);
  440. #endif
  441. l->owner = 0;
  442. DkMutexRelease(l->lock);
  443. enable_preempt(tcb);
  444. }
  445. static inline bool __locked (LOCKTYPE * l)
  446. {
  447. if (!lock_enabled || !l->lock)
  448. return false;
  449. shim_tcb_t * tcb = SHIM_GET_TLS();
  450. return tcb->tid == l->owner;
  451. }
  452. #define locked(l) __locked(&(l))
  453. #define DEBUG_MASTER_LOCK 0
  454. extern LOCKTYPE __master_lock;
  455. #if DEBUG_MASTER_LOCK == 1
  456. # define master_lock() \
  457. do { \
  458. lock(__master_lock); \
  459. pal_printf("master lock " __FILE__ ":%d\n", __LINE__); \
  460. } while (0)
  461. # define master_unlock() \
  462. do { \
  463. pal_printf("master unlock " __FILE__ ":%d\n", __LINE__); \
  464. unlock(__master_lock); \
  465. } while (0)
  466. #else
  467. # define master_lock() do { lock(__master_lock); } while (0)
  468. # define master_unlock() do { unlock(__master_lock); } while (0)
  469. #endif
  470. static inline void create_lock_runtime (LOCKTYPE * l)
  471. {
  472. if (!lock_created(*l)) {
  473. master_lock();
  474. if (!lock_created(*l))
  475. create_lock(*l);
  476. master_unlock();
  477. }
  478. }
  479. static inline void create_event (AEVENTTYPE * e)
  480. {
  481. if (!e->event)
  482. e->event = DkStreamOpen("pipe:", PAL_ACCESS_RDWR, 0, 0,
  483. PAL_OPTION_NONBLOCK);
  484. }
  485. #define event_created(e) ((e)->event != NULL)
  486. #define event_handle(e) ((e)->event)
  487. static inline void destroy_event (AEVENTTYPE * e)
  488. {
  489. if (e->event) {
  490. DkObjectClose(e->event);
  491. e->event = NULL;
  492. }
  493. }
  494. static inline void set_event (AEVENTTYPE * e, int n)
  495. {
  496. if (e->event) {
  497. char bytes[n];
  498. DkStreamWrite(e->event, 0, n, bytes, NULL);
  499. }
  500. }
  501. static inline void wait_event (AEVENTTYPE * e)
  502. {
  503. if (e->event) {
  504. char byte;
  505. int n;
  506. do {
  507. if (!DkObjectsWaitAny(1, &e->event, NO_TIMEOUT))
  508. continue;
  509. n = DkStreamRead(e->event, 0, 1, &byte, NULL, 0);
  510. } while (!n);
  511. }
  512. }
  513. static inline void clear_event (AEVENTTYPE * e)
  514. {
  515. if (e->event) {
  516. char bytes[100];
  517. int n;
  518. do {
  519. n = DkStreamRead(e->event, 0, 100, bytes, NULL, 0);
  520. } while (n == 100);
  521. }
  522. }
  523. static inline void do_pause (void)
  524. {
  525. bool go = false;
  526. while (!go)
  527. DkThreadDelayExecution(60 * 60 * 1000000ULL);
  528. }
  529. /* reference counter APIs */
  530. #define REF_GET(ref) atomic_read(&ref)
  531. #define REF_SET(ref, count) atomic_set(&ref, count)
  532. static inline int __ref_inc (REFTYPE * ref)
  533. {
  534. register int _c;
  535. do {
  536. _c = atomic_read(ref);
  537. assert(_c >= 0);
  538. } while (atomic_cmpxchg(ref, _c, _c + 1) != _c);
  539. return _c + 1;
  540. }
  541. #define REF_INC(ref) __ref_inc(&(ref))
  542. static inline int __ref_dec (REFTYPE * ref)
  543. {
  544. register int _c;
  545. do {
  546. _c = atomic_read(ref);
  547. if (!_c) {
  548. debug("Fail: Trying to drop reference count below 0\n");
  549. bug();
  550. return 0;
  551. }
  552. } while (atomic_cmpxchg(ref, _c, _c - 1) != _c);
  553. return _c - 1;
  554. }
  555. #define REF_DEC(ref) __ref_dec(&(ref))
  556. /* interger hash functions */
  557. static inline uint32_t hash32 (uint32_t key)
  558. {
  559. key = ~key + (key << 15);
  560. key = key ^ (key >> 12);
  561. key = key + (key << 2);
  562. key = key ^ (key >> 4);
  563. key = (key + (key << 3)) + (key << 11);
  564. key = key ^ (key >> 16);
  565. return key;
  566. }
  567. static inline uint64_t hash64 (uint64_t key)
  568. {
  569. key = (~key) + (key << 21);
  570. key = key ^ (key >> 24);
  571. key = (key + (key << 3)) + (key << 8);
  572. key = key ^ (key >> 14);
  573. key = (key + (key << 2)) + (key << 4);
  574. key = key ^ (key >> 28);
  575. key = key + (key << 31);
  576. return key;
  577. }
  578. #ifndef __alloca
  579. # define __alloca __builtin_alloca
  580. #endif
  581. extern unsigned long allocsize;
  582. extern unsigned long allocshift;
  583. extern unsigned long allocmask;
  584. void * __system_malloc (size_t size);
  585. void __system_free (void * addr, size_t size);
  586. #define system_malloc(size) __system_malloc(size)
  587. #define system_free(addr, size) __system_free(addr, size)
  588. extern void * migrated_memory_start;
  589. extern void * migrated_memory_end;
  590. #define MEMORY_MIGRATED(mem) \
  591. ((void *) (mem) >= migrated_memory_start && \
  592. (void *) (mem) < migrated_memory_end)
  593. extern void * __load_address, * __load_address_end;
  594. extern void * __code_address, * __code_address_end;
  595. int shim_clean (void);
  596. unsigned long parse_int (const char * str);
  597. extern void * initial_stack;
  598. extern const char ** initial_envp;
  599. #define ALIGNED(addr) (!(((unsigned long) addr) & allocshift))
  600. #define ALIGN_UP(addr) \
  601. ((typeof(addr)) ((((unsigned long) addr) + allocshift) & allocmask))
  602. #define ALIGN_DOWN(addr) \
  603. ((typeof(addr)) (((unsigned long) addr) & allocmask))
  604. #define switch_stack(stack_top) \
  605. ({ \
  606. void * _rsp, * _rbp; \
  607. void * _stack = (stack_top); \
  608. asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory"); \
  609. asm volatile ("movq %%rbp, %0" : "=r"(_rbp) :: "memory"); \
  610. _rsp = _stack - (_rbp - _rsp); \
  611. _rbp = _stack; \
  612. asm volatile ("movq %0, %%rsp" :: "r"(_rsp) : "memory"); \
  613. asm volatile ("movq %0, %%rbp" :: "r"(_rbp) : "memory"); \
  614. asm volatile ("movq %%rbp, %0" : "=r"(_stack) :: "memory"); \
  615. _stack; \
  616. })
  617. #define current_stack() \
  618. ({ \
  619. void * _rsp; \
  620. asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory"); \
  621. _rsp; \
  622. })
  623. void get_brk_region (void ** start, void ** end, void ** current);
  624. int init_randgen (void);
  625. int reset_brk (void);
  626. int init_brk_region (void * brk_region);
  627. int init_heap (void);
  628. int init_internal_map (void);
  629. int init_loader (void);
  630. int init_manifest (PAL_HANDLE manifest_handle);
  631. bool test_user_memory (void * addr, size_t size, bool write);
  632. bool test_user_string (const char * addr);
  633. #endif /* _PAL_INTERNAL_H_ */