shim_internal.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. #include <shim_types.h>
  28. #include <shim_defs.h>
  29. #include <shim_atomic.h>
  30. #include <shim_tls.h>
  31. /* important macros */
  32. #define get_cur_tid() (SHIM_GET_TLS()->tid)
  33. #define PAL_NATIVE_ERRNO (SHIM_GET_TLS()->pal_errno)
  34. #define INTERNAL_TID_BASE ((IDTYPE) 1 << (sizeof(IDTYPE) * 8 - 1))
  35. #define IS_INTERNAL_TID(tid) ((tid) >= INTERNAL_TID_BASE)
  36. #define IS_INTERNAL(thread) ((thread)->tid >= INTERNAL_TID_BASE)
  37. #define TID_PRINTFMT
  38. /* debug message printout */
  39. # define DEBUGBUF_SIZE 80
  40. struct debug_buf {
  41. int start;
  42. int end;
  43. char buf[DEBUGBUF_SIZE];
  44. };
  45. # include <pal.h>
  46. # include <pal_debug.h>
  47. # include <pal_error.h>
  48. extern PAL_HANDLE debug_handle;
  49. # include <stdarg.h>
  50. void debug_printf (const char * fmt, ...);
  51. void debug_puts (const char * str);
  52. void debug_putch (int ch);
  53. void debug_vprintf (const char * fmt, va_list ap);
  54. # define VMID_PREFIX "[P%04u] "
  55. # define TID_PREFIX "[%-5u] "
  56. # define NOID_PREFIX "[ ] "
  57. # define debug(fmt, ...) \
  58. do { \
  59. if (debug_handle) \
  60. debug_printf((fmt), ##__VA_ARGS__); \
  61. } while (0)
  62. /* print system messages */
  63. #define SYSPRINT_BUFFER_SIZE 256
  64. void handle_printf (PAL_HANDLE hdl, const char * fmt, ...);
  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_fprintf(hdl, fmt, ...) \
  72. do { \
  73. handle_printf((hdl), (fmt), ##__VA_ARGS__); \
  74. } while (0)
  75. #define sys_printf(fmt, ...) \
  76. do { \
  77. master_lock(); \
  78. __sys_printf((fmt), ##__VA_ARGS__); \
  79. master_unlock(); \
  80. } while (0)
  81. #define sys_fprintf(hdl, fmt, ...) \
  82. do { \
  83. master_lock(); \
  84. __sys_fprintf((hdl), (fmt), ##__VA_ARGS__); \
  85. master_unlock(); \
  86. } while (0)
  87. extern PAL_HANDLE shim_stdio;
  88. static inline PAL_HANDLE __open_shim_stdio (void)
  89. {
  90. if (shim_stdio == (PAL_HANDLE) -1)
  91. return NULL;
  92. if (shim_stdio)
  93. return shim_stdio;
  94. shim_stdio = DkStreamOpen("dev:tty", PAL_ACCESS_RDWR, 0, 0, 0);
  95. if (!shim_stdio) {
  96. shim_stdio = (PAL_HANDLE) -1;
  97. return NULL;
  98. }
  99. return shim_stdio;
  100. }
  101. int shim_terminate (void);
  102. /* assertions */
  103. #define USE_PAUSE 1
  104. #define USE_ASSERT 1
  105. extern bool in_gdb;
  106. static inline void do_pause (void);
  107. #if USE_PAUSE == 1
  108. # define pause() \
  109. do { \
  110. if (in_gdb) \
  111. asm volatile("int $3"); \
  112. else \
  113. do_pause(); \
  114. } while (0)
  115. #else
  116. # define pause() do { if (in_gdb) asm volatile ("int $3"); } while (0)
  117. #endif
  118. #define bug() \
  119. do { \
  120. __sys_printf("bug() " __FILE__ ":%d\n", __LINE__); \
  121. pause(); \
  122. shim_terminate(); \
  123. } while (0)
  124. #if USE_ASSERT == 1
  125. # define assert(test) \
  126. ({ \
  127. long _val = (long) (test); \
  128. (!(_val)) \
  129. ? ({ \
  130. __sys_printf("assert failed " __FILE__ ":%d " #test " (value:%x)\n", \
  131. __LINE__, _val); \
  132. pause(); \
  133. shim_terminate(); }) \
  134. : (void) 0; \
  135. })
  136. #else
  137. # define assert(test) do {} while (0)
  138. #endif
  139. /* definition for syscall table */
  140. void handle_signal (bool delayed_only);
  141. long convert_pal_errno (long err);
  142. #define PAL_ERRNO convert_pal_errno(PAL_NATIVE_ERRNO)
  143. #define SHIM_ARG_TYPE long
  144. #ifdef PROFILE
  145. # define ENTER_TIME SHIM_GET_TLS()->context.enter_time
  146. # define BEGIN_SYSCALL_PROFILE() \
  147. do { ENTER_TIME = GET_PROFILE_INTERVAL(); } while (0)
  148. # define END_SYSCALL_PROFILE(name) \
  149. do { unsigned long _interval = GET_PROFILE_INTERVAL(); \
  150. if (_interval - ENTER_TIME > 1000) \
  151. SAVE_PROFILE_INTERVAL_SET(syscall_##name##_slow, ENTER_TIME, _interval); \
  152. else \
  153. SAVE_PROFILE_INTERVAL_SET(syscall_##name, ENTER_TIME, _interval); \
  154. ENTER_TIME = 0; } while (0)
  155. #else
  156. # define BEGIN_SYSCALL_PROFILE() do {} while (0)
  157. # define END_SYSCALL_PROFILE(name) do {} while (0)
  158. #endif
  159. #define BEGIN_SHIM(name, args ...) \
  160. SHIM_ARG_TYPE __shim_##name (args) { \
  161. SHIM_ARG_TYPE ret = 0; \
  162. /* handle_signal(true); */ \
  163. BEGIN_SYSCALL_PROFILE();
  164. #define END_SHIM(name) \
  165. END_SYSCALL_PROFILE(name); \
  166. handle_signal(false); \
  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. * @member: the name of the member within the struct.
  330. *
  331. */
  332. #define container_of(ptr, type, member) ({ \
  333. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  334. (type *)( (char *)__mptr - offsetof(type,member) );})
  335. #endif
  336. #define CONCAT2(t1, t2) __CONCAT2(t1, t2)
  337. #define __CONCAT2(t1, t2) t1##_##t2
  338. #define CONCAT3(t1, t2, t3) __CONCAT3(t1, t2, t3)
  339. #define __CONCAT3(t1, t2, t3) t1##_##t2##_##t3
  340. /* Some SHIM internal errno */
  341. #define EISLINK 141 /* the path is a link */
  342. #define ECONTAINLINK 142 /* part of path contains a link */
  343. #define ENOTLINK 143 /* the path is not a link */
  344. #define ESKIPPED 144 /* skip looking up current path */
  345. #define PAL_CB(member) (pal_control.member)
  346. #define LOCK_FREE ((IDTYPE) -1)
  347. extern bool lock_enabled;
  348. static inline void enable_locking (void)
  349. {
  350. if (!lock_enabled)
  351. lock_enabled = true;
  352. }
  353. static inline PAL_HANDLE thread_create (void * func, void * arg, int option)
  354. {
  355. assert(lock_enabled);
  356. return DkThreadCreate(func, arg, option);
  357. }
  358. static inline void __disable_preempt (shim_tcb_t * tcb)
  359. {
  360. //tcb->context.syscall_nr += SYSCALL_NR_PREEMPT_INC;
  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. tcb->context.preempt--;
  374. //debug("enable preempt: %d\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  375. }
  376. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc);
  377. static inline void enable_preempt (shim_tcb_t * tcb)
  378. {
  379. if (!tcb && !(tcb = SHIM_GET_TLS()))
  380. return;
  381. if (!(tcb->context.preempt & ~SIGNAL_DELAYED))
  382. return;
  383. if ((tcb->context.preempt & ~SIGNAL_DELAYED) == 1)
  384. __handle_signal(tcb, 0, NULL);
  385. __enable_preempt(tcb);
  386. }
  387. #define DEBUG_LOCK 0
  388. #define lock_created(l) ((l).lock != NULL)
  389. #define clear_lock(l) do { (l).lock = NULL; (l).owner = 0; } while (0)
  390. #define create_lock(l) \
  391. do { \
  392. (l).lock = DkSemaphoreCreate(0, 1); \
  393. /* (l).owner = LOCK_FREE; */ \
  394. /* (l).reowned = 0; */ \
  395. } while (0)
  396. #define destroy_lock(l) \
  397. do { \
  398. DkObjectClose((l).lock); \
  399. } while (0)
  400. #define try_create_lock(l) \
  401. do { if (!lock_created(l)) create_lock(l); } while (0)
  402. #if DEBUG_LOCK == 1
  403. # define lock(l) __lock(&(l), #l, __FILE__, __LINE__)
  404. static inline void __lock (LOCKTYPE * l,
  405. const char * name, const char * file, int line)
  406. #else
  407. # define lock(l) __lock(&(l))
  408. static inline void __lock (LOCKTYPE * l)
  409. #endif
  410. {
  411. if (!lock_enabled || !l->lock)
  412. return;
  413. shim_tcb_t * tcb = SHIM_GET_TLS();
  414. disable_preempt(tcb);
  415. #if DEBUG_LOCK == 1
  416. debug("try lock(%s=%p) %s:%d\n", name, l, file, line);
  417. #endif
  418. while (!DkObjectsWaitAny(1, &l->lock, NO_TIMEOUT));
  419. l->owner = tcb->tid;
  420. #if DEBUG_LOCK == 1
  421. debug("lock(%s=%p) by %s:%d\n", name, l, file, line);
  422. #endif
  423. }
  424. #if DEBUG_LOCK == 1
  425. # define unlock(l) __unlock(&(l), #l, __FILE__, __LINE__)
  426. static inline void __unlock (LOCKTYPE * l,
  427. const char * name, const char * file, int line)
  428. #else
  429. # define unlock(l) __unlock(&(l))
  430. static inline void __unlock (LOCKTYPE * l)
  431. #endif
  432. {
  433. if (!lock_enabled || !l->lock)
  434. return;
  435. shim_tcb_t * tcb = SHIM_GET_TLS();
  436. #if DEBUG_LOCK == 1
  437. debug("unlock(%s=%p) %s:%d\n", name, l, file, line);
  438. #endif
  439. l->owner = 0;
  440. DkSemaphoreRelease(l->lock, 1);
  441. enable_preempt(tcb);
  442. }
  443. static inline bool __locked (LOCKTYPE * l)
  444. {
  445. if (!lock_enabled || !l->lock)
  446. return false;
  447. shim_tcb_t * tcb = SHIM_GET_TLS();
  448. return tcb->tid == l->owner;
  449. }
  450. #define locked(l) __locked(&(l))
  451. #define DEBUG_MASTER_LOCK 0
  452. extern LOCKTYPE __master_lock;
  453. #if DEBUG_MASTER_LOCK == 1
  454. # define master_lock() \
  455. do { \
  456. lock(__master_lock); \
  457. pal_printf("maste lock " __FILE__ ":%d\n", __LINE__); \
  458. } while (0)
  459. # define master_unlock() \
  460. do { \
  461. pal_printf("maste unlock " __FILE__ ":%d\n", __LINE__); \
  462. unlock(__master_lock); \
  463. } while (0)
  464. #else
  465. # define master_lock() do { lock(__master_lock); } while (0)
  466. # define master_unlock() do { unlock(__master_lock); } while (0)
  467. #endif
  468. static inline void create_lock_runtime (LOCKTYPE * l)
  469. {
  470. if (!lock_created(*l)) {
  471. master_lock();
  472. if (!lock_created(*l))
  473. create_lock(*l);
  474. master_unlock();
  475. }
  476. }
  477. static inline void create_event (AEVENTTYPE * e)
  478. {
  479. if (!e->event)
  480. e->event = DkStreamOpen("pipe:", PAL_ACCESS_RDWR, 0, 0,
  481. PAL_OPTION_NONBLOCK);
  482. }
  483. #define event_created(e) ((e)->event != NULL)
  484. #define event_handle(e) ((e)->event)
  485. static inline void destroy_event (AEVENTTYPE * e)
  486. {
  487. if (e->event) {
  488. DkObjectClose(e->event);
  489. e->event = NULL;
  490. }
  491. }
  492. static inline void set_event (AEVENTTYPE * e, int n)
  493. {
  494. if (e->event) {
  495. char bytes[n];
  496. DkStreamWrite(e->event, 0, n, bytes, NULL);
  497. }
  498. }
  499. static inline void wait_event (AEVENTTYPE * e)
  500. {
  501. if (e->event) {
  502. char byte;
  503. int n;
  504. do {
  505. if (!DkObjectsWaitAny(1, &e->event, NO_TIMEOUT))
  506. continue;
  507. n = DkStreamRead(e->event, 0, 1, &byte, NULL, 0);
  508. } while (!n);
  509. }
  510. }
  511. static inline void clear_event (AEVENTTYPE * e)
  512. {
  513. if (e->event) {
  514. char bytes[100];
  515. int n;
  516. do {
  517. n = DkStreamRead(e->event, 0, 100, bytes, NULL, 0);
  518. } while (n == 100);
  519. }
  520. }
  521. static inline void do_pause (void)
  522. {
  523. bool go = false;
  524. while (!go)
  525. DkThreadDelayExecution(60 * 60 * 1000000ULL);
  526. }
  527. /* reference counter APIs */
  528. #define REF_GET(ref) atomic_read(&ref)
  529. #define REF_SET(ref, count) atomic_set(&ref, count)
  530. static inline int __ref_inc (REFTYPE * ref)
  531. {
  532. register int _c;
  533. do {
  534. _c = atomic_read(ref);
  535. assert(_c >= 0);
  536. } while (atomic_cmpxchg(ref, _c, _c + 1) != _c);
  537. return _c + 1;
  538. }
  539. #define REF_INC(ref) __ref_inc(&(ref))
  540. static inline int __ref_dec (REFTYPE * ref)
  541. {
  542. register int _c;
  543. do {
  544. _c = atomic_read(ref);
  545. assert(_c > 0);
  546. if (!_c)
  547. return 0;
  548. } while (atomic_cmpxchg(ref, _c, _c - 1) != _c);
  549. return _c - 1;
  550. }
  551. #define REF_DEC(ref) __ref_dec(&(ref))
  552. /* interger hash functions */
  553. static inline uint32_t hash32 (uint32_t key)
  554. {
  555. key = ~key + (key << 15);
  556. key = key ^ (key >> 12);
  557. key = key + (key << 2);
  558. key = key ^ (key >> 4);
  559. key = (key + (key << 3)) + (key << 11);
  560. key = key ^ (key >> 16);
  561. return key;
  562. }
  563. static inline uint64_t hash64 (uint64_t key)
  564. {
  565. key = (~key) + (key << 21);
  566. key = key ^ (key >> 24);
  567. key = (key + (key << 3)) + (key << 8);
  568. key = key ^ (key >> 14);
  569. key = (key + (key << 2)) + (key << 4);
  570. key = key ^ (key >> 28);
  571. key = key + (key << 31);
  572. return key;
  573. }
  574. #ifndef __alloca
  575. # define __alloca __builtin_alloca
  576. #endif
  577. extern unsigned long allocsize;
  578. extern unsigned long allocshift;
  579. extern unsigned long allocmask;
  580. void * __system_malloc (size_t size);
  581. void __system_free (void * addr, size_t size);
  582. #define system_malloc(size) __system_malloc(size)
  583. #define system_free(addr, size) __system_free(addr, size)
  584. extern void * migrated_memory_start;
  585. extern void * migrated_memory_end;
  586. #define MEMORY_MIGRATED(mem) \
  587. ((void *) mem >= migrated_memory_start && \
  588. (void *) mem < migrated_memory_end)
  589. extern void * __load_address, * __load_address_end;
  590. extern void * __code_address, * __code_address_end;
  591. int shim_clean (void);
  592. extern void * initial_stack;
  593. extern const char ** initial_envp;
  594. #define ALIGNED(addr) (!(((unsigned long) addr) & allocshift))
  595. #define ALIGN_UP(addr) \
  596. ((typeof(addr)) ((((unsigned long) addr) + allocshift) & allocmask))
  597. #define ALIGN_DOWN(addr) \
  598. ((typeof(addr)) (((unsigned long) addr) & allocmask))
  599. #define switch_stack(stack_top) \
  600. ({ \
  601. void * _rsp, * _rbp; \
  602. void * _stack = (stack_top); \
  603. asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory"); \
  604. asm volatile ("movq %%rbp, %0" : "=r"(_rbp) :: "memory"); \
  605. _rsp = _stack - (_rbp - _rsp); \
  606. _rbp = _stack; \
  607. asm volatile ("movq %0, %%rsp" :: "r"(_rsp) : "memory"); \
  608. asm volatile ("movq %0, %%rbp" :: "r"(_rbp) : "memory"); \
  609. asm volatile ("movq %%rbp, %0" : "=r"(_stack) :: "memory"); \
  610. _stack; \
  611. })
  612. void get_brk_region (void ** start, void ** end, void ** current);
  613. int init_randgen (void);
  614. int init_brk (void);
  615. int init_heap (void);
  616. int init_internal_map (void);
  617. int init_loader (void);
  618. int init_manifest (PAL_HANDLE manifest_handle);
  619. #endif /* _PAL_INTERNAL_H_ */