shim_utils.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_utils.h
  17. */
  18. #ifndef _SHIM_UTILITIES_H_
  19. #define _SHIM_UTILITIES_H_
  20. #include <shim_handle.h>
  21. #include <pal.h>
  22. #include <linux_list.h>
  23. #include <api.h>
  24. struct shim_handle;
  25. void sysparser_printf (const char * fmt, ...);
  26. /* string object */
  27. struct shim_str * get_str_obj (void);
  28. int free_str_obj (struct shim_str * str);
  29. int init_str_mgr (void);
  30. /* qstring object */
  31. #define QSTR_INIT { .len = 0, .oflow = NULL }
  32. static inline const char * qstrgetstr (const struct shim_qstr * qstr)
  33. {
  34. return qstr->oflow ? qstr->oflow->str : qstr->name;
  35. }
  36. static inline void qstrfree (struct shim_qstr * qstr)
  37. {
  38. if (qstr->oflow) {
  39. free_str_obj(qstr->oflow);
  40. qstr->oflow = NULL;
  41. }
  42. qstr->name[0] = 0;
  43. qstr->len = 0;
  44. }
  45. static inline char * qstrsetstr (struct shim_qstr * qstr,
  46. const char * str, size_t size)
  47. {
  48. if (!str) {
  49. qstrfree(qstr);
  50. return NULL;
  51. }
  52. if (size >= STR_SIZE)
  53. return NULL;
  54. char * buf = qstr->name;
  55. if (size >= QSTR_SIZE) {
  56. if (!qstr->oflow)
  57. qstr->oflow = get_str_obj();
  58. buf = qstr->oflow->str;
  59. } else {
  60. if (qstr->oflow) {
  61. free_str_obj(qstr->oflow);
  62. qstr->oflow = NULL;
  63. }
  64. }
  65. qstr->len = 0;
  66. if (str) {
  67. if (size)
  68. memcpy(buf, str, size);
  69. buf[size] = 0;
  70. qstr->len = size;
  71. }
  72. return buf;
  73. }
  74. static inline char * qstrsetstrs (struct shim_qstr * qstr,
  75. int nstrs,
  76. const char ** strs, size_t * sizes)
  77. {
  78. size_t total_size = 0;
  79. for (int i = 0 ; i < nstrs ; i++)
  80. total_size += sizes[i];
  81. if (total_size >= STR_SIZE)
  82. return NULL;
  83. char * buf = qstr->name;
  84. if (total_size >= QSTR_SIZE) {
  85. if (!qstr->oflow)
  86. qstr->oflow = get_str_obj();
  87. buf = qstr->oflow->str;
  88. }
  89. char * ptr = buf;
  90. qstr->len = 0;
  91. for (int i = 0 ; i < nstrs ; i++) {
  92. int size = sizes[i];
  93. memcpy(ptr, strs[i], size);
  94. ptr[size] = 0;
  95. qstr->len += size;
  96. ptr += size;
  97. }
  98. return buf;
  99. }
  100. static inline int qstrempty (const struct shim_qstr * qstr)
  101. {
  102. return qstr->len == 0;
  103. }
  104. static inline void qstrcopy (struct shim_qstr * to,
  105. const struct shim_qstr * from)
  106. {
  107. qstrsetstr(to, qstrgetstr(from), from->len);
  108. to->hash = from->hash;
  109. }
  110. static inline int qstrcmpstr (const struct shim_qstr * qstr,
  111. const char * str, size_t size)
  112. {
  113. if (qstr->len != size)
  114. return 1;
  115. return memcmp(qstrgetstr(qstr), str, size);
  116. }
  117. //#define SLAB_DEBUG_PRINT
  118. //#define SLAB_DEBUG_TRACE
  119. /* heap allocation functions */
  120. int init_slab (void);
  121. #if defined(SLAB_DEBUG_PRINT) || defined(SLAB_DEBUG_TRACE)
  122. void * __malloc_debug (size_t size, const char * file, int line);
  123. #define malloc(size) __malloc_debug((size), __FILE__, __LINE__)
  124. void __free_debug (void * mem, const char * file, int line);
  125. #define free(mem) __free_debug((mem), __FILE__, __LINE__)
  126. void * __remalloc_debug (const void * mem, size_t size,
  127. const char * file, int line);
  128. #define remalloc(mem, size) __remalloc_debug((mem), (size), __FILE__, __LINE__)
  129. #else
  130. void * malloc (size_t size);
  131. void free (void * mem);
  132. void * remalloc (const void * mem, size_t size);
  133. #endif
  134. static_inline char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
  135. {
  136. int len = qstr->len;
  137. char * buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
  138. if (!buf)
  139. return NULL;
  140. if (len)
  141. memcpy(buf, qstrgetstr(qstr), len);
  142. buf[len] = 0;
  143. return buf;
  144. }
  145. /* typedef a 32 bit type */
  146. # ifndef UINT4
  147. # define UINT4 uint32_t
  148. # endif
  149. /* Data structure for MD5 (Message Digest) computation */
  150. struct shim_md5_ctx {
  151. UINT4 i[2]; /* number of _bits_ handled mod 2^64 */
  152. UINT4 buf[4]; /* scratch buffer */
  153. unsigned char in[64]; /* input buffer */
  154. unsigned char digest[16]; /* actual digest after MD5Final call */
  155. };
  156. void md5_init (struct shim_md5_ctx * mdContext);
  157. void md5_update (struct shim_md5_ctx * mdContext, const void * buf,
  158. size_t len);
  159. void md5_final (struct shim_md5_ctx * mdContext);
  160. /* prompt user for confirmation */
  161. int message_confirm (const char * message, const char * options);
  162. /* get random number */
  163. int getrand (void * buffer, size_t size);
  164. /* ELF binary loading */
  165. int check_elf_object (struct shim_handle * file);
  166. int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
  167. int load_elf_interp (struct shim_handle * exec);
  168. int free_elf_interp (void);
  169. int execute_elf_object (struct shim_handle * exec, int argc, const char ** argp,
  170. int nauxv, 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. int 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. int terminate_async_helper (void);
  193. extern struct config_store * root_config;
  194. #endif /* _SHIM_UTILITIES_H */