shim_internal.h 28 KB

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