shim_internal.h 28 KB

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