shim_utils.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_utils.h
  15. */
  16. #ifndef _SHIM_UTILS_H_
  17. #define _SHIM_UTILS_H_
  18. #include <shim_handle.h>
  19. #include <pal.h>
  20. #include <list.h>
  21. #include <api.h>
  22. struct shim_handle;
  23. void sysparser_printf (const char * fmt, ...);
  24. /* string object */
  25. struct shim_str * get_str_obj (void);
  26. int free_str_obj (struct shim_str * str);
  27. int init_str_mgr (void);
  28. /* qstring object */
  29. #define QSTR_INIT { .len = 0, .oflow = NULL }
  30. static inline const char * qstrgetstr (const struct shim_qstr * qstr)
  31. {
  32. return qstr->oflow ? qstr->oflow->str : qstr->name;
  33. }
  34. static inline void qstrfree (struct shim_qstr * qstr)
  35. {
  36. if (qstr->oflow) {
  37. free_str_obj(qstr->oflow);
  38. qstr->oflow = NULL;
  39. }
  40. qstr->name[0] = 0;
  41. qstr->len = 0;
  42. }
  43. static inline char * qstrsetstr (struct shim_qstr * qstr,
  44. const char * str, size_t size)
  45. {
  46. if (!str) {
  47. qstrfree(qstr);
  48. return NULL;
  49. }
  50. if (size >= STR_SIZE)
  51. return NULL;
  52. char * buf = qstr->name;
  53. if (size >= QSTR_SIZE) {
  54. if (!qstr->oflow) {
  55. qstr->oflow = get_str_obj();
  56. if (!qstr->oflow)
  57. return NULL;
  58. }
  59. buf = qstr->oflow->str;
  60. } else {
  61. if (qstr->oflow) {
  62. free_str_obj(qstr->oflow);
  63. qstr->oflow = NULL;
  64. }
  65. }
  66. memcpy(buf, str, size);
  67. buf[size] = 0;
  68. qstr->len = size;
  69. return buf;
  70. }
  71. static inline char * qstrsetstrs (struct shim_qstr * qstr,
  72. int nstrs,
  73. const char ** strs, size_t * sizes)
  74. {
  75. size_t total_size = 0;
  76. for (int i = 0 ; i < nstrs ; i++)
  77. total_size += sizes[i];
  78. if (total_size >= STR_SIZE)
  79. return NULL;
  80. char * buf = qstr->name;
  81. if (total_size >= QSTR_SIZE) {
  82. if (!qstr->oflow) {
  83. // TODO: alloc proper size.
  84. qstr->oflow = get_str_obj();
  85. if (!qstr->oflow)
  86. return NULL;
  87. }
  88. buf = qstr->oflow->str;
  89. }
  90. char * ptr = buf;
  91. qstr->len = 0;
  92. for (int i = 0 ; i < nstrs ; i++) {
  93. int size = sizes[i];
  94. memcpy(ptr, strs[i], size);
  95. ptr[size] = 0;
  96. qstr->len += size;
  97. ptr += size;
  98. }
  99. return buf;
  100. }
  101. static inline int qstrempty (const struct shim_qstr * qstr)
  102. {
  103. return qstr->len == 0;
  104. }
  105. static inline void qstrcopy (struct shim_qstr * to,
  106. const struct shim_qstr * from)
  107. {
  108. qstrsetstr(to, qstrgetstr(from), from->len);
  109. to->hash = from->hash;
  110. }
  111. static inline int qstrcmpstr (const struct shim_qstr * qstr,
  112. const char * str, size_t size)
  113. {
  114. if (qstr->len != size)
  115. return 1;
  116. return memcmp(qstrgetstr(qstr), str, size);
  117. }
  118. //#define SLAB_DEBUG_PRINT
  119. //#define SLAB_DEBUG_TRACE
  120. /* heap allocation functions */
  121. int init_slab (void);
  122. #if defined(SLAB_DEBUG_PRINT) || defined(SLAB_DEBUG_TRACE)
  123. void * __malloc_debug (size_t size, const char * file, int line);
  124. #define malloc(size) __malloc_debug(size, __FILE__, __LINE__)
  125. void __free_debug (void * mem, const char * file, int line);
  126. #define free(mem) __free_debug(mem, __FILE__, __LINE__)
  127. void * __malloc_copy_debug (const void * mem, size_t size,
  128. const char * file, int line);
  129. #define malloc_copy(mem, size) __malloc_copy_debug(mem, size, __FILE__, __LINE__)
  130. #else
  131. void * malloc (size_t size);
  132. void free (void * mem);
  133. void * malloc_copy (const void * mem, size_t size);
  134. #endif
  135. static_always_inline
  136. char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
  137. {
  138. int len = qstr->len;
  139. char * buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
  140. if (!buf)
  141. return NULL;
  142. memcpy(buf, qstrgetstr(qstr), len);
  143. buf[len] = 0;
  144. return buf;
  145. }
  146. /* typedef a 32 bit type */
  147. # ifndef UINT4
  148. # define UINT4 uint32_t
  149. # endif
  150. /* Data structure for MD5 (Message Digest) computation */
  151. struct shim_md5_ctx {
  152. UINT4 i[2]; /* number of _bits_ handled mod 2^64 */
  153. UINT4 buf[4]; /* scratch buffer */
  154. unsigned char in[64]; /* input buffer */
  155. unsigned char digest[16]; /* actual digest after MD5Final call */
  156. };
  157. void md5_init (struct shim_md5_ctx * mdContext);
  158. void md5_update (struct shim_md5_ctx * mdContext, const void * buf,
  159. size_t len);
  160. void md5_final (struct shim_md5_ctx * mdContext);
  161. /* prompt user for confirmation */
  162. int message_confirm (const char * message, const char * options);
  163. /* ELF binary loading */
  164. int check_elf_object (struct shim_handle * file);
  165. int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
  166. int load_elf_interp (struct shim_handle * exec);
  167. int free_elf_interp (void);
  168. void execute_elf_object (struct shim_handle * exec,
  169. int * argcp, const char ** argp,
  170. elf_auxv_t * auxp);
  171. int remove_loaded_libraries (void);
  172. /* gdb debugging support */
  173. void remove_r_debug (void * addr);
  174. void append_r_debug (const char * uri, void * addr, void * dyn_addr);
  175. void clean_link_map_list (void);
  176. /* create unique files/pipes */
  177. #define PIPE_URI_SIZE 40
  178. int create_pipe (IDTYPE * pipeid, char * uri, size_t size, PAL_HANDLE * hdl,
  179. struct shim_qstr * qstr);
  180. int create_dir (const char * prefix, char * path, size_t size,
  181. struct shim_handle ** hdl);
  182. int create_file (const char * prefix, char * path, size_t size,
  183. struct shim_handle ** hdl);
  184. int create_handle (const char * prefix, char * path, size_t size,
  185. PAL_HANDLE * hdl, unsigned int * id);
  186. /* Asynchronous event support */
  187. int init_async (void);
  188. int64_t install_async_event (PAL_HANDLE object, unsigned long time,
  189. void (*callback) (IDTYPE caller, void * arg),
  190. void * arg);
  191. int create_async_helper (void);
  192. struct shim_thread * terminate_async_helper (void);
  193. extern struct config_store * root_config;
  194. #endif /* _SHIM_UTILS_H */