shim_internal.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. #define BEGIN_SHIM(name, args ...) \
  152. SHIM_ARG_TYPE __shim_##name (args) { \
  153. SHIM_ARG_TYPE ret = 0; \
  154. /* handle_signal(true); */ \
  155. /* check_stack_hook(); */ \
  156. BEGIN_SYSCALL_PROFILE();
  157. #define END_SHIM(name) \
  158. END_SYSCALL_PROFILE(name); \
  159. handle_signal(false); \
  160. return ret; \
  161. }
  162. #define DEFINE_SHIM_SYSCALL(name, n, func, ...) \
  163. DEFINE_PROFILE_INTERVAL(syscall_##name##_slow, syscall); \
  164. DEFINE_PROFILE_INTERVAL(syscall_##name, syscall); \
  165. SHIM_SYSCALL_##n (name, func, __VA_ARGS__) \
  166. EXPORT_SHIM_SYSCALL (name, n, __VA_ARGS__)
  167. #define PROTO_ARGS_0() void
  168. #define PROTO_ARGS_1(t, a) t a
  169. #define PROTO_ARGS_2(t, a, rest ...) t a, PROTO_ARGS_1(rest)
  170. #define PROTO_ARGS_3(t, a, rest ...) t a, PROTO_ARGS_2(rest)
  171. #define PROTO_ARGS_4(t, a, rest ...) t a, PROTO_ARGS_3(rest)
  172. #define PROTO_ARGS_5(t, a, rest ...) t a, PROTO_ARGS_4(rest)
  173. #define PROTO_ARGS_6(t, a, rest ...) t a, PROTO_ARGS_5(rest)
  174. #define CAST_ARGS_0()
  175. #define CAST_ARGS_1(t, a) (SHIM_ARG_TYPE) a
  176. #define CAST_ARGS_2(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_1(rest)
  177. #define CAST_ARGS_3(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_2(rest)
  178. #define CAST_ARGS_4(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_3(rest)
  179. #define CAST_ARGS_5(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_4(rest)
  180. #define CAST_ARGS_6(t, a, rest ...) (SHIM_ARG_TYPE) a, CAST_ARGS_5(rest)
  181. #define DEFINE_SHIM_FUNC(func, n, r, args ...) \
  182. r func (PROTO_ARGS_##n (args));
  183. #define TYPE_HASH(t) ({ const char * _s = #t; \
  184. ((uint16_t) _s[0] << 8) + _s[1]; })
  185. #define POINTER_TYPE(t) ({ int _h = TYPE_HASH(t); \
  186. _h == TYPE_HASH(void *) || _h == TYPE_HASH(char *) || \
  187. _h == TYPE_HASH(const); })
  188. #define EXPORT_SHIM_SYSCALL(name, n, r, args ...) \
  189. r shim_##name (PROTO_ARGS_##n (args)) { \
  190. SHIM_ARG_TYPE ret = __shim_##name (CAST_ARGS_##n (args)); \
  191. if (POINTER_TYPE(r)) { \
  192. if ((unsigned long) ret >= -4095L) return (r) 0; \
  193. } else { \
  194. if ((int) ret < 0) return (r) -1; \
  195. } \
  196. return (r) ret; \
  197. }
  198. #define PARSE_SYSCALL1(name, ...) \
  199. if (debug_handle) \
  200. parse_syscall_before(__NR_##name, #name, ##__VA_ARGS__);
  201. #define PARSE_SYSCALL2(name, ...) \
  202. if (debug_handle) \
  203. parse_syscall_after(__NR_##name, #name, ##__VA_ARGS__);
  204. void parse_syscall_before (int sysno, const char * name, int nr, ...);
  205. void parse_syscall_after (int sysno, const char * name, int nr, ...);
  206. #define SHIM_SYSCALL_0(name, func, r) \
  207. BEGIN_SHIM(name, void) \
  208. PARSE_SYSCALL1(name, 0); \
  209. r __ret = func(); \
  210. PARSE_SYSCALL2(name, 0, #r, __ret); \
  211. ret = (SHIM_ARG_TYPE) __ret; \
  212. END_SHIM(name)
  213. #define SHIM_SYSCALL_1(name, func, r, t1, a1) \
  214. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1) \
  215. t1 a1 = (t1) __arg1; \
  216. PARSE_SYSCALL1(name, 1, #t1, a1); \
  217. r __ret = func(a1); \
  218. PARSE_SYSCALL2(name, 1, #r, __ret, #t1, a1); \
  219. ret = (SHIM_ARG_TYPE) __ret; \
  220. END_SHIM(name)
  221. #define SHIM_SYSCALL_2(name, func, r, t1, a1, t2, a2) \
  222. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2) \
  223. t1 a1 = (t1) __arg1; \
  224. t2 a2 = (t2) __arg2; \
  225. PARSE_SYSCALL1(name, 2, #t1, a1, #t2, a2); \
  226. r __ret = func(a1, a2); \
  227. PARSE_SYSCALL2(name, 2, #r, __ret, #t1, a1, #t2, a2); \
  228. ret = (SHIM_ARG_TYPE) __ret; \
  229. END_SHIM(name)
  230. #define SHIM_SYSCALL_3(name, func, r, t1, a1, t2, a2, t3, a3) \
  231. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  232. SHIM_ARG_TYPE __arg3) \
  233. t1 a1 = (t1) __arg1; \
  234. t2 a2 = (t2) __arg2; \
  235. t3 a3 = (t3) __arg3; \
  236. PARSE_SYSCALL1(name, 3, #t1, a1, #t2, a2, #t3, a3); \
  237. r __ret = func(a1, a2, a3); \
  238. PARSE_SYSCALL2(name, 3, #r, __ret, #t1, a1, #t2, a2, #t3, a3); \
  239. ret = (SHIM_ARG_TYPE) __ret; \
  240. END_SHIM(name)
  241. #define SHIM_SYSCALL_4(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4) \
  242. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  243. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4) \
  244. t1 a1 = (t1) __arg1; \
  245. t2 a2 = (t2) __arg2; \
  246. t3 a3 = (t3) __arg3; \
  247. t4 a4 = (t4) __arg4; \
  248. PARSE_SYSCALL1(name, 4, #t1, a1, #t2, a2, #t3, a3, #t4, a4); \
  249. r __ret = func(a1, a2, a3, a4); \
  250. PARSE_SYSCALL2(name, 4, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  251. #t4, a4); \
  252. ret = (SHIM_ARG_TYPE) __ret; \
  253. END_SHIM(name)
  254. #define SHIM_SYSCALL_5(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
  255. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  256. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4, \
  257. SHIM_ARG_TYPE __arg5) \
  258. t1 a1 = (t1) __arg1; \
  259. t2 a2 = (t2) __arg2; \
  260. t3 a3 = (t3) __arg3; \
  261. t4 a4 = (t4) __arg4; \
  262. t5 a5 = (t5) __arg5; \
  263. PARSE_SYSCALL1(name, 5, #t1, a1, #t2, a2, #t3, a3, #t4, a4, \
  264. #t5, a5); \
  265. r __ret = func(a1, a2, a3, a4, a5); \
  266. PARSE_SYSCALL2(name, 5, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  267. #t4, a4, #t5, a5); \
  268. ret = (SHIM_ARG_TYPE) __ret; \
  269. END_SHIM(name)
  270. #define SHIM_SYSCALL_6(name, func, r, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
  271. BEGIN_SHIM(name, SHIM_ARG_TYPE __arg1, SHIM_ARG_TYPE __arg2, \
  272. SHIM_ARG_TYPE __arg3, SHIM_ARG_TYPE __arg4, \
  273. SHIM_ARG_TYPE __arg5, SHIM_ARG_TYPE __arg6) \
  274. t1 a1 = (t1) __arg1; \
  275. t2 a2 = (t2) __arg2; \
  276. t3 a3 = (t3) __arg3; \
  277. t4 a4 = (t4) __arg4; \
  278. t5 a5 = (t5) __arg5; \
  279. t6 a6 = (t6) __arg6; \
  280. PARSE_SYSCALL1(name, 6, #t1, a1, #t2, a2, #t3, a3, #t4, a4, \
  281. #t5, a5, #t6, a6); \
  282. r __ret = func(a1, a2, a3, a4, a5, a6); \
  283. PARSE_SYSCALL2(name, 6, #r, __ret, #t1, a1, #t2, a2, #t3, a3, \
  284. #t4, a4, #t5, a5, #t6, a6); \
  285. ret = (SHIM_ARG_TYPE) __ret; \
  286. END_SHIM(name)
  287. #define SHIM_PROTO_ARGS_0 void
  288. #define SHIM_PROTO_ARGS_1 SHIM_ARG_TYPE __arg1
  289. #define SHIM_PROTO_ARGS_2 SHIM_PROTO_ARGS_1, SHIM_ARG_TYPE __arg2
  290. #define SHIM_PROTO_ARGS_3 SHIM_PROTO_ARGS_2, SHIM_ARG_TYPE __arg3
  291. #define SHIM_PROTO_ARGS_4 SHIM_PROTO_ARGS_3, SHIM_ARG_TYPE __arg4
  292. #define SHIM_PROTO_ARGS_5 SHIM_PROTO_ARGS_4, SHIM_ARG_TYPE __arg5
  293. #define SHIM_PROTO_ARGS_6 SHIM_PROTO_ARGS_5, SHIM_ARG_TYPE __arg6
  294. #define SHIM_PASS_ARGS_1 __arg1
  295. #define SHIM_PASS_ARGS_2 SHIM_PASS_ARGS_1, __arg2
  296. #define SHIM_PASS_ARGS_3 SHIM_PASS_ARGS_2, __arg3
  297. #define SHIM_PASS_ARGS_4 SHIM_PASS_ARGS_3, __arg4
  298. #define SHIM_PASS_ARGS_5 SHIM_PASS_ARGS_4, __arg5
  299. #define SHIM_PASS_ARGS_6 SHIM_PASS_ARGS_5, __arg6
  300. #define DO_SYSCALL(...) DO_SYSCALL2(__VA_ARGS__)
  301. #define DO_SYSCALL2(n, ...) -ENOSYS
  302. #define DO_SYSCALL_0(sysno) -ENOSYS
  303. #define DO_SYSCALL_1(sysno, ...) DO_SYSCALL(1, sysno, SHIM_PASS_ARGS_1)
  304. #define DO_SYSCALL_2(sysno, ...) DO_SYSCALL(2, sysno, SHIM_PASS_ARGS_2)
  305. #define DO_SYSCALL_3(sysno, ...) DO_SYSCALL(3, sysno, SHIM_PASS_ARGS_3)
  306. #define DO_SYSCALL_4(sysno, ...) DO_SYSCALL(4, sysno, SHIM_PASS_ARGS_4)
  307. #define DO_SYSCALL_5(sysno, ...) DO_SYSCALL(5, sysno, SHIM_PASS_ARGS_5)
  308. #define DO_SYSCALL_6(sysno, ...) DO_SYSCALL(6, sysno, SHIM_PASS_ARGS_6)
  309. #define SHIM_SYSCALL_PASSTHROUGH(name, n, ...) \
  310. DEFINE_PROFILE_INTERVAL(syscall_##name##_slow, syscall); \
  311. DEFINE_PROFILE_INTERVAL(syscall_##name, syscall); \
  312. BEGIN_SHIM(name, SHIM_PROTO_ARGS_##n) \
  313. debug("WARNING: shim_" #name " not implemented\n"); \
  314. ret = DO_SYSCALL_##n(__NR_##name); \
  315. END_SHIM(name) \
  316. EXPORT_SHIM_SYSCALL(name, n, __VA_ARGS__)
  317. #ifndef container_of
  318. /**
  319. * container_of - cast a member of a structure out to the containing structure
  320. * @ptr: the pointer to the member.
  321. * @type: the type of the container struct this is embedded in.
  322. * @member: the name of the member within the struct.
  323. *
  324. */
  325. #define container_of(ptr, type, member) ({ \
  326. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  327. (type *)( (char *)__mptr - offsetof(type,member) );})
  328. #endif
  329. #define CONCAT2(t1, t2) __CONCAT2(t1, t2)
  330. #define __CONCAT2(t1, t2) t1##_##t2
  331. #define CONCAT3(t1, t2, t3) __CONCAT3(t1, t2, t3)
  332. #define __CONCAT3(t1, t2, t3) t1##_##t2##_##t3
  333. /* Some SHIM internal errno */
  334. #define EISLINK 141 /* the path is a link */
  335. #define ECONTAINLINK 142 /* part of path contains a link */
  336. #define ENOTLINK 143 /* the path is not a link */
  337. #define ESKIPPED 144 /* skip looking up current path */
  338. #define PAL_CB(member) (pal_control.member)
  339. #define LOCK_FREE ((IDTYPE) -1)
  340. extern bool lock_enabled;
  341. static inline void enable_locking (void)
  342. {
  343. if (!lock_enabled)
  344. lock_enabled = true;
  345. }
  346. static inline PAL_HANDLE thread_create (void * func, void * arg, int option)
  347. {
  348. assert(lock_enabled);
  349. return DkThreadCreate(func, arg, option);
  350. }
  351. static inline void __disable_preempt (shim_tcb_t * tcb)
  352. {
  353. //tcb->context.syscall_nr += SYSCALL_NR_PREEMPT_INC;
  354. tcb->context.preempt++;
  355. //debug("disable preempt: %d\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  356. }
  357. static inline void disable_preempt (shim_tcb_t * tcb)
  358. {
  359. if (!tcb && !(tcb = SHIM_GET_TLS()))
  360. return;
  361. __disable_preempt(tcb);
  362. }
  363. static inline void __enable_preempt (shim_tcb_t * tcb)
  364. {
  365. //tcb->context.syscall_nr -= SYSCALL_NR_PREEMPT_INC;
  366. tcb->context.preempt--;
  367. //debug("enable preempt: %d\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  368. }
  369. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc);
  370. static inline void enable_preempt (shim_tcb_t * tcb)
  371. {
  372. if (!tcb && !(tcb = SHIM_GET_TLS()))
  373. return;
  374. if (!(tcb->context.preempt & ~SIGNAL_DELAYED))
  375. return;
  376. if ((tcb->context.preempt & ~SIGNAL_DELAYED) == 1)
  377. __handle_signal(tcb, 0, NULL);
  378. __enable_preempt(tcb);
  379. }
  380. #define DEBUG_LOCK 0
  381. #define lock_created(l) ((l).lock != NULL)
  382. #define clear_lock(l) do { (l).lock = NULL; (l).owner = 0; } while (0)
  383. #define create_lock(l) \
  384. do { \
  385. (l).lock = DkMutexCreate(0); \
  386. /* (l).owner = LOCK_FREE; */ \
  387. /* (l).reowned = 0; */ \
  388. } while (0)
  389. #define destroy_lock(l) \
  390. do { \
  391. DkObjectClose((l).lock); \
  392. } while (0)
  393. #define try_create_lock(l) \
  394. do { if (!lock_created(l)) create_lock(l); } while (0)
  395. #if DEBUG_LOCK == 1
  396. # define lock(l) __lock(&(l), #l, __FILE__, __LINE__)
  397. static inline void __lock (LOCKTYPE * l,
  398. const char * name, const char * file, int line)
  399. #else
  400. # define lock(l) __lock(&(l))
  401. static inline void __lock (LOCKTYPE * l)
  402. #endif
  403. {
  404. if (!lock_enabled || !l->lock)
  405. return;
  406. shim_tcb_t * tcb = SHIM_GET_TLS();
  407. disable_preempt(tcb);
  408. #if DEBUG_LOCK == 1
  409. debug("try lock(%s=%p) %s:%d\n", name, l, file, line);
  410. #endif
  411. while (!DkObjectsWaitAny(1, &l->lock, NO_TIMEOUT));
  412. l->owner = tcb->tid;
  413. #if DEBUG_LOCK == 1
  414. debug("lock(%s=%p) by %s:%d\n", name, l, file, line);
  415. #endif
  416. }
  417. #if DEBUG_LOCK == 1
  418. # define unlock(l) __unlock(&(l), #l, __FILE__, __LINE__)
  419. static inline void __unlock (LOCKTYPE * l,
  420. const char * name, const char * file, int line)
  421. #else
  422. # define unlock(l) __unlock(&(l))
  423. static inline void __unlock (LOCKTYPE * l)
  424. #endif
  425. {
  426. if (!lock_enabled || !l->lock)
  427. return;
  428. shim_tcb_t * tcb = SHIM_GET_TLS();
  429. #if DEBUG_LOCK == 1
  430. debug("unlock(%s=%p) %s:%d\n", name, l, file, line);
  431. #endif
  432. l->owner = 0;
  433. DkMutexRelease(l->lock);
  434. enable_preempt(tcb);
  435. }
  436. static inline bool __locked (LOCKTYPE * l)
  437. {
  438. if (!lock_enabled || !l->lock)
  439. return false;
  440. shim_tcb_t * tcb = SHIM_GET_TLS();
  441. return tcb->tid == l->owner;
  442. }
  443. #define locked(l) __locked(&(l))
  444. #define DEBUG_MASTER_LOCK 0
  445. extern LOCKTYPE __master_lock;
  446. #if DEBUG_MASTER_LOCK == 1
  447. # define master_lock() \
  448. do { \
  449. lock(__master_lock); \
  450. pal_printf("master lock " __FILE__ ":%d\n", __LINE__); \
  451. } while (0)
  452. # define master_unlock() \
  453. do { \
  454. pal_printf("master unlock " __FILE__ ":%d\n", __LINE__); \
  455. unlock(__master_lock); \
  456. } while (0)
  457. #else
  458. # define master_lock() do { lock(__master_lock); } while (0)
  459. # define master_unlock() do { unlock(__master_lock); } while (0)
  460. #endif
  461. static inline void create_lock_runtime (LOCKTYPE * l)
  462. {
  463. if (!lock_created(*l)) {
  464. master_lock();
  465. if (!lock_created(*l))
  466. create_lock(*l);
  467. master_unlock();
  468. }
  469. }
  470. static inline void create_event (AEVENTTYPE * e)
  471. {
  472. if (!e->event)
  473. e->event = DkStreamOpen("pipe:", PAL_ACCESS_RDWR, 0, 0,
  474. PAL_OPTION_NONBLOCK);
  475. }
  476. #define event_created(e) ((e)->event != NULL)
  477. #define event_handle(e) ((e)->event)
  478. static inline void destroy_event (AEVENTTYPE * e)
  479. {
  480. if (e->event) {
  481. DkObjectClose(e->event);
  482. e->event = NULL;
  483. }
  484. }
  485. static inline void set_event (AEVENTTYPE * e, int n)
  486. {
  487. if (e->event) {
  488. char bytes[n];
  489. DkStreamWrite(e->event, 0, n, bytes, NULL);
  490. }
  491. }
  492. static inline void wait_event (AEVENTTYPE * e)
  493. {
  494. if (e->event) {
  495. char byte;
  496. int n;
  497. do {
  498. if (!DkObjectsWaitAny(1, &e->event, NO_TIMEOUT))
  499. continue;
  500. n = DkStreamRead(e->event, 0, 1, &byte, NULL, 0);
  501. } while (!n);
  502. }
  503. }
  504. static inline void clear_event (AEVENTTYPE * e)
  505. {
  506. if (e->event) {
  507. char bytes[100];
  508. int n;
  509. do {
  510. n = DkStreamRead(e->event, 0, 100, bytes, NULL, 0);
  511. } while (n == 100);
  512. }
  513. }
  514. static inline void do_pause (void)
  515. {
  516. bool go = false;
  517. while (!go)
  518. DkThreadDelayExecution(60 * 60 * 1000000ULL);
  519. }
  520. /* reference counter APIs */
  521. #define REF_GET(ref) atomic_read(&ref)
  522. #define REF_SET(ref, count) atomic_set(&ref, count)
  523. static inline int __ref_inc (REFTYPE * ref)
  524. {
  525. register int _c;
  526. do {
  527. _c = atomic_read(ref);
  528. assert(_c >= 0);
  529. } while (atomic_cmpxchg(ref, _c, _c + 1) != _c);
  530. return _c + 1;
  531. }
  532. #define REF_INC(ref) __ref_inc(&(ref))
  533. static inline int __ref_dec (REFTYPE * ref)
  534. {
  535. register int _c;
  536. do {
  537. _c = atomic_read(ref);
  538. if (!_c) {
  539. debug("Fail: Trying to drop reference count below 0\n");
  540. bug();
  541. return 0;
  542. }
  543. } while (atomic_cmpxchg(ref, _c, _c - 1) != _c);
  544. return _c - 1;
  545. }
  546. #define REF_DEC(ref) __ref_dec(&(ref))
  547. /* interger hash functions */
  548. static inline uint32_t hash32 (uint32_t key)
  549. {
  550. key = ~key + (key << 15);
  551. key = key ^ (key >> 12);
  552. key = key + (key << 2);
  553. key = key ^ (key >> 4);
  554. key = (key + (key << 3)) + (key << 11);
  555. key = key ^ (key >> 16);
  556. return key;
  557. }
  558. static inline uint64_t hash64 (uint64_t key)
  559. {
  560. key = (~key) + (key << 21);
  561. key = key ^ (key >> 24);
  562. key = (key + (key << 3)) + (key << 8);
  563. key = key ^ (key >> 14);
  564. key = (key + (key << 2)) + (key << 4);
  565. key = key ^ (key >> 28);
  566. key = key + (key << 31);
  567. return key;
  568. }
  569. #ifndef __alloca
  570. # define __alloca __builtin_alloca
  571. #endif
  572. extern unsigned long allocsize;
  573. extern unsigned long allocshift;
  574. extern unsigned long allocmask;
  575. void * __system_malloc (size_t size);
  576. void __system_free (void * addr, size_t size);
  577. #define system_malloc(size) __system_malloc(size)
  578. #define system_free(addr, size) __system_free(addr, size)
  579. extern void * migrated_memory_start;
  580. extern void * migrated_memory_end;
  581. #define MEMORY_MIGRATED(mem) \
  582. ((void *) (mem) >= migrated_memory_start && \
  583. (void *) (mem) < migrated_memory_end)
  584. extern void * __load_address, * __load_address_end;
  585. extern void * __code_address, * __code_address_end;
  586. int shim_clean (void);
  587. unsigned long parse_int (const char * str);
  588. extern void * initial_stack;
  589. extern const char ** initial_envp;
  590. #define ALIGNED(addr) (!(((unsigned long) addr) & allocshift))
  591. #define ALIGN_UP(addr) \
  592. ((typeof(addr)) ((((unsigned long) addr) + allocshift) & allocmask))
  593. #define ALIGN_DOWN(addr) \
  594. ((typeof(addr)) (((unsigned long) addr) & allocmask))
  595. #define switch_stack(stack_top) \
  596. ({ \
  597. void * _rsp, * _rbp; \
  598. void * _stack = (stack_top); \
  599. asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory"); \
  600. asm volatile ("movq %%rbp, %0" : "=r"(_rbp) :: "memory"); \
  601. _rsp = _stack - (_rbp - _rsp); \
  602. _rbp = _stack; \
  603. asm volatile ("movq %0, %%rsp" :: "r"(_rsp) : "memory"); \
  604. asm volatile ("movq %0, %%rbp" :: "r"(_rbp) : "memory"); \
  605. asm volatile ("movq %%rbp, %0" : "=r"(_stack) :: "memory"); \
  606. _stack; \
  607. })
  608. #define current_stack() \
  609. ({ \
  610. void * _rsp; \
  611. asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory"); \
  612. _rsp; \
  613. })
  614. void get_brk_region (void ** start, void ** end, void ** current);
  615. int init_randgen (void);
  616. int reset_brk (void);
  617. int init_brk_region (void * brk_region);
  618. int init_heap (void);
  619. int init_internal_map (void);
  620. int init_loader (void);
  621. int init_manifest (PAL_HANDLE manifest_handle);
  622. bool test_user_memory (void * addr, size_t size, bool write);
  623. bool test_user_string (const char * addr);
  624. #endif /* _PAL_INTERNAL_H_ */