shim_vma.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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_vma.c
  17. *
  18. * This file contains codes to maintain bookkeeping of VMAs in library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_fs.h>
  26. #include <pal.h>
  27. #include <list.h>
  28. #include <asm/mman.h>
  29. #include <errno.h>
  30. unsigned long mem_max_npages __attribute_migratable = DEFAULT_MEM_MAX_NPAGES;
  31. static void * heap_top, * heap_bottom;
  32. #define VMA_MGR_ALLOC 64
  33. #define PAGE_SIZE allocsize
  34. static LOCKTYPE vma_mgr_lock;
  35. #define system_lock() lock(vma_mgr_lock)
  36. #define system_unlock() unlock(vma_mgr_lock)
  37. static inline void * __vma_malloc (size_t size)
  38. {
  39. struct shim_thread * thread = get_cur_thread();
  40. if (!thread)
  41. return system_malloc(size);
  42. size = ALIGN_UP(size);
  43. void * addr = (void *) DkVirtualMemoryAlloc(NULL, size, 0,
  44. PAL_PROT_WRITE|PAL_PROT_READ);
  45. debug("allocate %p-%p for vmas\n", addr, addr + size);
  46. thread->delayed_bkeep_mmap.addr = addr;
  47. thread->delayed_bkeep_mmap.length = size;
  48. return addr;
  49. }
  50. #undef system_malloc
  51. #define system_malloc(size) __vma_malloc(size)
  52. #define OBJ_TYPE struct shim_vma
  53. #include <memmgr.h>
  54. static MEM_MGR vma_mgr = NULL;
  55. DEFINE_LISTP(shim_vma);
  56. static LISTP_TYPE(shim_vma) vma_list = LISTP_INIT;
  57. static LOCKTYPE vma_list_lock;
  58. static inline bool test_vma_equal (struct shim_vma * tmp,
  59. const void * addr, uint64_t length)
  60. {
  61. return tmp->addr == addr && tmp->length == length;
  62. }
  63. static inline bool test_vma_contain (struct shim_vma * tmp,
  64. const void * addr, uint64_t length)
  65. {
  66. return tmp->addr <= addr &&
  67. tmp->addr + tmp->length >= addr + length;
  68. }
  69. static inline bool test_vma_startin (struct shim_vma * tmp,
  70. const void * addr, uint64_t length)
  71. {
  72. return tmp->addr >= addr &&
  73. tmp->addr < addr + length;
  74. }
  75. static inline bool test_vma_endin (struct shim_vma * tmp,
  76. const void * addr, uint64_t length)
  77. {
  78. return tmp->addr + tmp->length > addr &&
  79. tmp->addr + tmp->length <= addr + length;
  80. }
  81. static inline bool test_vma_overlap (struct shim_vma * tmp,
  82. const void * addr, uint64_t length)
  83. {
  84. return test_vma_contain(tmp, addr, 1) ||
  85. test_vma_contain(tmp, addr + length - 1, 1) ||
  86. test_vma_startin(tmp, addr, length);
  87. }
  88. int bkeep_shim_heap (void);
  89. static void __set_heap_top (void * bottom, void * top);
  90. int init_vma (void)
  91. {
  92. if (!(vma_mgr = create_mem_mgr(init_align_up(VMA_MGR_ALLOC)))) {
  93. debug("failed allocating VMAs\n");
  94. return -ENOMEM;
  95. }
  96. debug("User space range given from PAL: %p-%p\n",
  97. (void *) PAL_CB(user_address.start),
  98. (void *) PAL_CB(user_address.end));
  99. heap_bottom = (void *) PAL_CB(user_address.start);
  100. if (heap_bottom + DEFAULT_HEAP_MIN_SIZE > PAL_CB(executable_range.start)
  101. && heap_bottom < PAL_CB(executable_range.end)
  102. && heap_top > PAL_CB(executable_range.start))
  103. heap_bottom = (void *) ALIGN_UP(PAL_CB(executable_range.end));
  104. debug("setting initial heap to %p-%p\n", heap_bottom,
  105. (void *) PAL_CB(user_address.end));
  106. __set_heap_top(heap_bottom, (void *) PAL_CB(user_address.end));
  107. bkeep_shim_heap();
  108. create_lock(vma_list_lock);
  109. return 0;
  110. }
  111. /* This might not give the same vma but we might need to
  112. split after we find something */
  113. static inline void assert_vma (void)
  114. {
  115. struct shim_vma * tmp;
  116. struct shim_vma * prev __attribute__((unused)) = NULL;
  117. listp_for_each_entry(tmp, &vma_list, list) {
  118. /* Assert we are really sorted */
  119. assert(tmp->length > 0);
  120. assert(!prev || prev->addr + prev->length <= tmp->addr);
  121. prev = tmp;
  122. }
  123. }
  124. static struct shim_vma * __lookup_vma (const void * addr, uint64_t len);
  125. static struct shim_vma * __lookup_supervma (const void * addr, uint64_t length,
  126. struct shim_vma ** prev);
  127. static struct shim_vma * __lookup_overlap_vma (const void * addr, uint64_t length,
  128. struct shim_vma ** prev);
  129. void get_vma (struct shim_vma * vma)
  130. {
  131. #ifdef DEBUG_REF
  132. int ref_count = REF_INC(vma->ref_count);
  133. debug("get vma %p(%p-%p) (ref_count = %d)\n", vma, vma->addr,
  134. vma->addr + vma->length, ref_count);
  135. #else
  136. REF_INC(vma->ref_count);
  137. #endif
  138. }
  139. void put_vma (struct shim_vma * vma)
  140. {
  141. int ref_count = REF_DEC(vma->ref_count);
  142. #ifdef DEBUG_REF
  143. debug("put vma %p(%p-%p) (ref_count = %d)\n", vma,
  144. vma->addr, vma->addr + vma->length, ref_count - 1);
  145. #endif
  146. if (ref_count < 1) {
  147. if (vma->file)
  148. put_handle(vma->file);
  149. if (MEMORY_MIGRATED(vma))
  150. memset(vma, 0, sizeof(struct shim_vma));
  151. else
  152. free_mem_obj_to_mgr(vma_mgr, vma);
  153. }
  154. }
  155. static void __remove_vma (struct shim_vma * vma)
  156. {
  157. /* We assume all vmas are on the vma_list.
  158. * Perhaps worth asserting? */
  159. listp_del(vma, &vma_list, list);
  160. put_vma(vma);
  161. }
  162. static int __bkeep_mmap (void * addr, uint64_t length, int prot, int flags,
  163. struct shim_handle * file, uint64_t offset,
  164. const char * comment);
  165. static int __bkeep_mprotect (void * addr, uint64_t length, int prot,
  166. const int * flags);
  167. static void __check_delayed_bkeep (void)
  168. {
  169. struct shim_thread * thread = get_cur_thread();
  170. if (!thread)
  171. return;
  172. if (!thread->delayed_bkeep_mmap.addr)
  173. return;
  174. void * bkeep_addr = thread->delayed_bkeep_mmap.addr;
  175. uint64_t bkeep_length = thread->delayed_bkeep_mmap.length;
  176. thread->delayed_bkeep_mmap.addr = NULL;
  177. thread->delayed_bkeep_mmap.length = 0;
  178. __bkeep_mmap(bkeep_addr, bkeep_length,
  179. PROT_READ|PROT_WRITE,
  180. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  181. NULL, 0, NULL);
  182. }
  183. static struct shim_vma * get_new_vma (void)
  184. {
  185. struct shim_vma * tmp =
  186. get_mem_obj_from_mgr_enlarge(vma_mgr, size_align_up(VMA_MGR_ALLOC));
  187. if (!tmp)
  188. return NULL;
  189. memset(tmp, 0, sizeof(struct shim_vma));
  190. INIT_LIST_HEAD(tmp, list);
  191. REF_SET(tmp->ref_count, 1);
  192. return tmp;
  193. }
  194. static bool check_vma_flags (const struct shim_vma * vma, const int * flags)
  195. {
  196. if (!flags)
  197. return true;
  198. if (vma->flags & VMA_UNMAPPED)
  199. return true;
  200. if ((vma->flags & VMA_INTERNAL) != ((*flags) & VMA_INTERNAL)) {
  201. debug("Check vma flag failure: vma flags %x, checked flags %x\n", vma->flags, *flags);
  202. bug();
  203. return false;
  204. }
  205. return true;
  206. }
  207. static inline void __set_comment (struct shim_vma * vma, const char * comment)
  208. {
  209. if (!comment) {
  210. vma->comment[0] = 0;
  211. return;
  212. }
  213. uint64_t len = strlen(comment);
  214. if (len > VMA_COMMENT_LEN - 1)
  215. len = VMA_COMMENT_LEN - 1;
  216. memcpy(vma->comment, comment, len);
  217. vma->comment[len] = 0;
  218. }
  219. static int __bkeep_mmap (void * addr, uint64_t length,
  220. int prot, int flags,
  221. struct shim_handle * file, uint64_t offset,
  222. const char * comment)
  223. {
  224. struct shim_vma * prev = NULL;
  225. struct shim_vma * tmp = __lookup_supervma(addr, length, &prev);
  226. int ret = 0;
  227. debug("bkeep_mmap: %p-%p\n", addr, addr + length);
  228. if (file)
  229. get_handle(file);
  230. if (tmp) { /* the range is included in a vma */
  231. if (tmp->addr != addr || tmp->length != length) {
  232. /* we are inside some unmapped area, do a split case */
  233. ret = __bkeep_mprotect(addr, length, prot, &flags);
  234. if (ret < 0)
  235. goto err;
  236. /* now we get the exact vma handle */
  237. tmp = __lookup_vma(addr, length);
  238. assert(tmp);
  239. assert(check_vma_flags(tmp, &flags));
  240. }
  241. } else {
  242. struct shim_vma * cont = NULL, * n; /* cont: continue to scan vmas */
  243. struct shim_vma * pos = NULL; /* pos: position to add the vma */
  244. if (prev && prev->addr == addr &&
  245. prev->length <= length) { /* find a vma at the same addr */
  246. cont = tmp = prev;
  247. } else { /* need to add a new vma */
  248. if (!(tmp = get_new_vma()))
  249. return -ENOMEM;
  250. if (prev) { /* has a precendent vma */
  251. if (test_vma_endin(prev, addr, length)) {
  252. if (!check_vma_flags(prev, &flags)) {
  253. ret = -EACCES;
  254. goto err;
  255. }
  256. /* the previous vma ends in the range; otherwise, there is
  257. * no overlapping. Another case is handled by the supervma
  258. * case. */
  259. prev->length = addr - prev->addr;
  260. }
  261. assert(prev->addr + prev->length <= addr);
  262. cont = prev;
  263. pos = prev;
  264. } else { /* has no precendent vma */
  265. cont = tmp;
  266. listp_add(tmp, &vma_list, list);
  267. }
  268. }
  269. if (cont)
  270. listp_for_each_entry_safe_continue(cont, n, &vma_list, list) {
  271. if (!test_vma_startin(cont, addr, length))
  272. break;
  273. if (!check_vma_flags(cont, &flags)) {
  274. ret = -EACCES;
  275. goto err;
  276. }
  277. if (test_vma_endin(cont, addr, length)) {
  278. __remove_vma(cont);
  279. continue;
  280. }
  281. long offset = addr + length - cont->addr;
  282. assert(offset > 0);
  283. if (cont->file)
  284. cont->offset += offset;
  285. cont->addr += offset;
  286. cont->length -= offset;
  287. break;
  288. }
  289. if (tmp && pos)
  290. list_add(tmp, pos, list);
  291. }
  292. tmp->addr = addr;
  293. tmp->length = length;
  294. tmp->prot = prot;
  295. tmp->flags = flags|((file && (prot & PROT_WRITE)) ? VMA_TAINTED : 0);
  296. tmp->file = file;
  297. tmp->offset = offset;
  298. __set_comment(tmp, comment);
  299. assert(!prev || prev == tmp || prev->addr + prev->length <= tmp->addr);
  300. return 0;
  301. err:
  302. if (file)
  303. put_handle(file);
  304. return ret;
  305. }
  306. int bkeep_mmap (void * addr, uint64_t length, int prot, int flags,
  307. struct shim_handle * file, uint64_t offset, const char * comment)
  308. {
  309. if (!addr || !length)
  310. return -EINVAL;
  311. lock(vma_list_lock);
  312. int ret = __bkeep_mmap(addr, length, prot, flags, file, offset,
  313. comment);
  314. //assert_vma();
  315. __check_delayed_bkeep();
  316. unlock(vma_list_lock);
  317. return ret;
  318. }
  319. /*
  320. * munmap start at any address and it might be split in between so
  321. * We need to split the area aur reduce the size
  322. * Check the address falls between alread allocated area or not
  323. */
  324. static int __bkeep_munmap (void * addr, uint64_t length, const int * flags)
  325. {
  326. struct shim_vma * tmp, * n;
  327. debug("bkeep_unmmap: %p-%p\n", addr, addr + length);
  328. listp_for_each_entry_safe(tmp, n, &vma_list, list) {
  329. if (test_vma_equal (tmp, addr, length)) {
  330. if (!check_vma_flags(tmp, flags))
  331. return -EACCES;
  332. __remove_vma(tmp);
  333. } else if (test_vma_overlap (tmp, addr, length)) {
  334. uint64_t before_length;
  335. uint64_t after_length;
  336. uint64_t after_offset;
  337. if (addr > tmp->addr)
  338. before_length = addr - tmp->addr;
  339. else
  340. before_length = 0;
  341. if (tmp->addr + tmp->length > addr + length)
  342. after_length = (tmp->addr + tmp->length) - (addr + length);
  343. else
  344. after_length = 0;
  345. after_offset = tmp->file ? tmp->offset + tmp->length -
  346. after_length : 0;
  347. /* split case
  348. * it is Unlikely that a process does an partical unmap
  349. * but We take care of it by splitting the book-keep
  350. *
  351. * case 1 if the vma is entirely between a mapped area
  352. * .e.g See case:
  353. * ---unmap--
  354. * ------map-----------
  355. */
  356. if (before_length) {
  357. /* Case 1: Space in the vma before */
  358. if (!check_vma_flags(tmp, flags))
  359. return -EACCES;
  360. tmp->length = before_length;
  361. if (after_length) {
  362. /* Case 2: Space before and also space after */
  363. int ret = __bkeep_mmap((void *) addr + length, after_length,
  364. tmp->prot, tmp->flags,
  365. tmp->file, after_offset,
  366. tmp->comment);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. } else if (after_length) {
  371. /* Case 3: Only after length */
  372. if (!check_vma_flags(tmp, flags))
  373. return -EACCES;
  374. tmp->addr = (void *) addr + length;
  375. tmp->length = after_length;
  376. tmp->offset = after_offset;
  377. } else {
  378. if (!check_vma_flags(tmp, flags))
  379. return -EACCES;
  380. __remove_vma(tmp);
  381. }
  382. } else if (tmp->addr > (addr + length))
  383. break;
  384. }
  385. return 0;
  386. }
  387. int bkeep_munmap (void * addr, uint64_t length, const int * flags)
  388. {
  389. if (!addr || !length)
  390. return -EINVAL;
  391. lock(vma_list_lock);
  392. int ret = __bkeep_munmap(addr, length, flags);
  393. //assert_vma();
  394. __check_delayed_bkeep();
  395. unlock(vma_list_lock);
  396. return ret;
  397. }
  398. static int __bkeep_mprotect (void * addr, uint64_t length, int prot,
  399. const int * flags)
  400. {
  401. struct shim_vma * tmp = __lookup_vma(addr, length);
  402. int ret;
  403. debug("bkeep_mprotect: %p-%p\n", addr, addr + length);
  404. if (tmp) {
  405. /* exact match */
  406. if (!check_vma_flags(tmp, flags))
  407. return -EACCES;
  408. tmp->prot = prot;
  409. if (tmp->file && (prot & PROT_WRITE))
  410. tmp->flags |= VMA_TAINTED;
  411. return 0;
  412. }
  413. /* split case
  414. * it is Unlikely that a process does an partical unmap
  415. * but We take care of it by splitting the book-keep
  416. *
  417. * case 1 if the vma is entirely between a mapped area .e.g See case:
  418. * ---unmap--
  419. * ------map-----------
  420. */
  421. tmp = __lookup_supervma(addr, length, NULL);
  422. if (tmp) {
  423. if (!check_vma_flags(tmp, flags))
  424. return -EACCES;
  425. uint64_t before_length = addr - tmp->addr;
  426. uint64_t after_length = tmp->addr + tmp->length - addr - length;
  427. uint64_t after_offset = tmp->file ? tmp->offset + tmp->length -
  428. after_length : 0;
  429. uint64_t inside_offset = tmp->file ? tmp->offset + before_length : 0;
  430. /* split the handler first, because we might call bkeep_mmap */
  431. tmp->addr = (void *) addr;
  432. tmp->length = length;
  433. if (before_length) {
  434. ret = __bkeep_mmap((void *) addr - before_length, before_length,
  435. tmp->prot, tmp->flags,
  436. tmp->file, tmp->offset,
  437. tmp->comment);
  438. if (ret < 0)
  439. return ret;
  440. }
  441. if (after_length) {
  442. ret = __bkeep_mmap((void *)addr + length, after_length,
  443. tmp->prot, tmp->flags,
  444. tmp->file, after_offset,
  445. tmp->comment);
  446. if (ret < 0)
  447. return ret;
  448. }
  449. tmp->prot = prot;
  450. tmp->offset = inside_offset;
  451. if (tmp->file && (prot & PROT_WRITE))
  452. tmp->flags |= VMA_TAINTED;
  453. return 0;
  454. }
  455. /* split case
  456. * if the unmap are in between to mapped
  457. * area then we need to split two VMA here
  458. * This is the most unlikely case
  459. *
  460. * case 2
  461. * ------unmap------
  462. * ----map1-----;-----map2-------
  463. *
  464. * TODO: this algorithm is very inefficient, and may change
  465. * the mapping if it fails
  466. */
  467. uint64_t o_length = length;
  468. while (length) {
  469. struct shim_vma * candidate = NULL;
  470. listp_for_each_entry(tmp, &vma_list, list) {
  471. if (test_vma_contain (tmp, addr, 1)) {
  472. if (!check_vma_flags(tmp, flags))
  473. return -EACCES;
  474. uint64_t before_length = addr - tmp->addr;
  475. uint64_t after_length = tmp->addr + tmp->length > addr + length ?
  476. tmp->addr + tmp->length - addr - length : 0;
  477. uint64_t after_offset = tmp->file ? tmp->offset + tmp->length -
  478. after_length : 0;
  479. uint64_t inside_length = tmp->addr + tmp->length > addr + length ?
  480. length :
  481. addr + length - tmp->addr - tmp->length;
  482. uint64_t inside_offset = tmp->file ? tmp->offset + before_length : 0;
  483. /* split the handler first, because we might call bkeep_mmap */
  484. tmp->addr = (void *) addr;
  485. tmp->length = inside_length;
  486. if (before_length) {
  487. ret = __bkeep_mmap((void *) addr - before_length, before_length,
  488. tmp->prot, tmp->flags,
  489. tmp->file, tmp->offset,
  490. tmp->comment);
  491. if (ret < 0)
  492. return ret;
  493. }
  494. if (after_length) {
  495. ret = __bkeep_mmap((void *) addr + length, after_length,
  496. tmp->prot, tmp->flags,
  497. tmp->file, after_offset,
  498. tmp->comment);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. tmp->prot = prot;
  503. tmp->offset = inside_offset;
  504. if (tmp->file && (prot & PROT_WRITE))
  505. tmp->flags |= VMA_TAINTED;
  506. addr += inside_length;
  507. length -= inside_length;
  508. break;
  509. }
  510. if (test_vma_startin(tmp, addr, length))
  511. if (!candidate || candidate->addr > tmp->addr)
  512. candidate = tmp;
  513. }
  514. if (o_length == length) {
  515. if (!candidate) {
  516. /* no more vmas, protect the whole area */
  517. ret = __bkeep_mmap((void *) addr, length, prot,
  518. VMA_UNMAPPED|(flags ? *flags : 0),
  519. NULL, 0, NULL);
  520. if (ret < 0)
  521. return ret;
  522. candidate = __lookup_vma((void *) addr, length);
  523. assert(candidate);
  524. /* DEP 10/19/16: If we make a vma that perfectly matches this
  525. * region, we want to break the loop and stop. */
  526. length = 0;
  527. }
  528. length -= candidate->addr - addr;
  529. }
  530. o_length = length;
  531. }
  532. return 0;
  533. }
  534. int bkeep_mprotect (void * addr, uint64_t length, int prot, const int * flags)
  535. {
  536. if (!addr || !length)
  537. return -EINVAL;
  538. lock(vma_list_lock);
  539. int ret = __bkeep_mprotect(addr, length, prot, flags);
  540. //assert_vma();
  541. unlock(vma_list_lock);
  542. return ret;
  543. }
  544. static void __set_heap_top (void * bottom, void * top)
  545. {
  546. bottom += DEFAULT_HEAP_MIN_SIZE;
  547. if (bottom >= top) {
  548. heap_top = top;
  549. return;
  550. }
  551. uint64_t rand;
  552. while (getrand(&rand, sizeof(uint64_t)) < sizeof(uint64_t));
  553. rand %= (uint64_t) (top - bottom) / allocsize;
  554. heap_top = bottom + rand * allocsize;
  555. debug("heap top adjusted to %p\n", heap_top);
  556. }
  557. void * get_unmapped_vma (uint64_t length, int flags)
  558. {
  559. struct shim_vma * new = get_new_vma(), * prev = NULL;
  560. if (!new)
  561. return NULL;
  562. lock(vma_list_lock);
  563. __check_delayed_bkeep();
  564. if (heap_top - heap_bottom < length) {
  565. debug("current heap %p-%p is not enough for allocating %lld bytes\n",
  566. heap_bottom, heap_top, length);
  567. unlock(vma_list_lock);
  568. put_vma(new);
  569. return NULL;
  570. }
  571. debug("find unmapped vma between %p-%p\n", heap_bottom, heap_top);
  572. do {
  573. int found = 0;
  574. new->addr = heap_top - length;
  575. new->length = length;
  576. new->flags = flags|VMA_UNMAPPED;
  577. new->prot = PROT_NONE;
  578. listp_for_each_entry_reverse(prev, &vma_list, list) {
  579. if (new->addr >= prev->addr + prev->length) {
  580. found = 1;
  581. break;
  582. }
  583. if (new->addr < heap_bottom) {
  584. found = 1;
  585. break;
  586. }
  587. if (prev->addr - heap_bottom < length) {
  588. unlock(vma_list_lock);
  589. put_vma(new);
  590. return NULL;
  591. }
  592. if (new->addr > prev->addr - length)
  593. new->addr = prev->addr - length;
  594. }
  595. /* DEP 6/4/17: This case appears to be detecting whether you wrapped around the
  596. * list wtihout finding anything. Let's add an explicit variable for
  597. * this case, but keep the check for now to be safe. */
  598. if (listp_empty(&vma_list)
  599. || (!found && (prev == listp_last_entry(&vma_list, shim_vma, list)))) {
  600. prev = NULL;
  601. break;
  602. }
  603. if (new->addr < heap_bottom) {
  604. if (heap_top == PAL_CB(user_address.end)) {
  605. unlock(vma_list_lock);
  606. put_vma(new);
  607. return NULL;
  608. } else {
  609. __set_heap_top(heap_top, (void *) PAL_CB(user_address.end));
  610. new->addr = NULL;
  611. }
  612. }
  613. } while (!new->addr);
  614. assert(!prev || prev->addr + prev->length <= new->addr);
  615. get_vma(new);
  616. listp_add_after(new, prev, &vma_list, list);
  617. debug("get unmapped: %p-%p\n", new->addr, new->addr + new->length);
  618. unlock(vma_list_lock);
  619. return new->addr;
  620. }
  621. #define NTRIES 4
  622. void * get_unmapped_vma_for_cp (uint64_t length)
  623. {
  624. struct shim_vma * new = get_new_vma(), * prev = NULL;
  625. if (!new)
  626. return NULL;
  627. if (length > PAL_CB(user_address.end) - PAL_CB(user_address.start)) {
  628. debug("user space is not enough for allocating %lld bytes\n", length);
  629. return NULL;
  630. }
  631. lock(vma_list_lock);
  632. __check_delayed_bkeep();
  633. uint64_t top = (uint64_t) PAL_CB(user_address.end) - length;
  634. uint64_t bottom = (uint64_t) heap_top;
  635. int flags = MAP_ANONYMOUS|VMA_UNMAPPED|VMA_INTERNAL;
  636. void * addr;
  637. if (bottom >= top) {
  638. unlock(vma_list_lock);
  639. return get_unmapped_vma(length, flags);
  640. }
  641. debug("find unmapped vma between %p-%p\n", bottom, top);
  642. for (int i = 0 ; i < NTRIES ; i++) {
  643. uint64_t rand;
  644. while (getrand(&rand, sizeof(uint64_t)) < sizeof(uint64_t));
  645. rand %= (uint64_t) (top - bottom) / allocsize;
  646. addr = (void *) bottom + rand * allocsize;
  647. if (!__lookup_overlap_vma(addr, length, &prev))
  648. break;
  649. addr = NULL;
  650. }
  651. if (!addr) {
  652. unlock(vma_list_lock);
  653. debug("cannot find unmapped vma for checkpoint\n");
  654. return NULL;
  655. }
  656. new->addr = addr;
  657. new->length = length;
  658. new->flags = flags;
  659. new->prot = PROT_NONE;
  660. listp_add_after(new, prev, &vma_list, list);
  661. unlock(vma_list_lock);
  662. return addr;
  663. }
  664. /* This might not give the same vma but we might need to
  665. split after we find something */
  666. static struct shim_vma * __lookup_overlap_vma (const void * addr, uint64_t length,
  667. struct shim_vma ** pprev)
  668. {
  669. struct shim_vma * tmp, * prev = NULL;
  670. listp_for_each_entry(tmp, &vma_list, list) {
  671. if (test_vma_overlap (tmp, addr, length)) {
  672. if (pprev)
  673. *pprev = prev;
  674. return tmp;
  675. }
  676. /* Assert we are really sorted */
  677. assert(!prev || prev->addr < tmp->addr);
  678. /* Insert in order; break once we are past the appropriate point */
  679. if (tmp->addr > addr)
  680. break;
  681. prev = tmp;
  682. }
  683. if (pprev)
  684. *pprev = prev;
  685. return NULL;
  686. }
  687. int lookup_overlap_vma (const void * addr, uint64_t length,
  688. struct shim_vma ** vma)
  689. {
  690. struct shim_vma * tmp = NULL;
  691. void * tmp_addr = NULL;
  692. uint64_t tmp_length;
  693. lock(vma_list_lock);
  694. if ((tmp = __lookup_overlap_vma(addr, length, NULL)) && vma)
  695. get_vma((tmp));
  696. if (tmp) {
  697. tmp_addr = tmp->addr;
  698. tmp_length = tmp->length;
  699. }
  700. unlock(vma_list_lock);
  701. if (tmp)
  702. debug("vma overlapped at %p-%p\n", tmp_addr, tmp_addr + tmp_length);
  703. if (vma)
  704. *vma = tmp;
  705. return tmp ? 0: -ENOENT;
  706. }
  707. static struct shim_vma * __lookup_vma (const void * addr, uint64_t length)
  708. {
  709. struct shim_vma * tmp;
  710. struct shim_vma * prev __attribute__((unused)) = NULL;
  711. listp_for_each_entry(tmp, &vma_list, list) {
  712. if (test_vma_equal(tmp, addr, length))
  713. return tmp;
  714. /* Assert we are really sorted */
  715. assert(!prev || prev->addr + prev->length <= tmp->addr);
  716. prev = tmp;
  717. }
  718. return NULL;
  719. }
  720. static struct shim_vma * __lookup_supervma (const void * addr, uint64_t length,
  721. struct shim_vma ** pprev)
  722. {
  723. struct shim_vma * tmp, * prev = NULL;
  724. listp_for_each_entry(tmp, &vma_list, list) {
  725. if (test_vma_contain(tmp, addr, length)) {
  726. if (pprev)
  727. *pprev = prev;
  728. return tmp;
  729. }
  730. /* Assert we are really sorted */
  731. if (!(!prev || prev->addr + prev->length <= tmp->addr)) {
  732. struct shim_vma * tmp2;
  733. warn("Failure\n");
  734. listp_for_each_entry(tmp2, &vma_list, list) {
  735. warn ("Entry: %llx..%llx (%llx)\n", tmp2->addr, tmp2->addr + tmp2->length, tmp2->length);
  736. }
  737. warn("Prev is %p, tmp->addr = %llx, len is %llx\n", prev, tmp->addr, tmp->length);
  738. if (prev)
  739. warn("prev addr is %llx, len is %llx\n", prev->addr, prev->length);
  740. }
  741. assert(!prev || prev->addr + prev->length <= tmp->addr);
  742. /* Insert in order; break once we are past the appropriate point */
  743. if (tmp->addr > addr)
  744. break;
  745. prev = tmp;
  746. }
  747. if (pprev)
  748. *pprev = prev;
  749. return NULL;
  750. }
  751. int lookup_supervma (const void * addr, uint64_t length, struct shim_vma ** vma)
  752. {
  753. struct shim_vma * tmp = NULL;
  754. lock(vma_list_lock);
  755. if ((tmp = __lookup_supervma(addr, length, NULL)) && vma)
  756. get_vma((tmp));
  757. unlock(vma_list_lock);
  758. if (vma)
  759. *vma = tmp;
  760. return tmp ? 0 : -ENOENT;
  761. }
  762. struct shim_vma * next_vma (struct shim_vma * vma)
  763. {
  764. struct shim_vma * tmp = vma;
  765. lock(vma_list_lock);
  766. if (!tmp) {
  767. if (!listp_empty(&vma_list) &&
  768. (tmp = listp_first_entry(&vma_list, struct shim_vma, list)))
  769. get_vma(tmp);
  770. unlock(vma_list_lock);
  771. return tmp;
  772. }
  773. if (tmp->list.next == listp_first_entry(&vma_list, NULL, NULL)) {
  774. tmp = NULL;
  775. } else if (tmp->list.next == tmp) {
  776. struct shim_vma * tmp2;
  777. tmp = NULL;
  778. listp_for_each_entry(tmp2, &vma_list, list)
  779. if (tmp2->addr >= vma->addr) {
  780. tmp = tmp2;
  781. get_vma(tmp);
  782. break;
  783. }
  784. } else {
  785. tmp = list_entry(tmp->list.next, struct shim_vma, list);
  786. get_vma(tmp);
  787. }
  788. put_vma(vma);
  789. unlock(vma_list_lock);
  790. return tmp;
  791. }
  792. /* to speed up the checkpointing, go organize the VMAs */
  793. void __shrink_vmas (void)
  794. {
  795. struct shim_vma * vma, * n, * last;
  796. listp_for_each_entry_safe(vma, n, &vma_list, list) {
  797. if (!last)
  798. goto unmap;
  799. if (last->addr + last->length != vma->addr ||
  800. last->prot != vma->prot ||
  801. last->flags != vma->flags ||
  802. last->file != vma->file)
  803. goto unmap;
  804. if (last->file && last->offset + last->length != vma->offset)
  805. goto unmap;
  806. debug("shrink vma %p-%p and %p-%p\n", last->addr,
  807. last->addr + last->length, vma->addr, vma->addr + vma->length);
  808. last->length += vma->length;
  809. __remove_vma(vma);
  810. continue;
  811. next:
  812. last = vma;
  813. continue;
  814. unmap:
  815. if (vma->prot == PROT_NONE && !(vma->flags & VMA_TAINTED))
  816. vma->flags |= VMA_UNMAPPED;
  817. goto next;
  818. }
  819. }
  820. int dump_all_vmas (struct shim_thread * thread, char * buf, uint64_t size)
  821. {
  822. struct shim_vma * vma;
  823. int cnt = 0;
  824. lock(vma_list_lock);
  825. listp_for_each_entry(vma, &vma_list, list) {
  826. void * start = vma->addr, * end = vma->addr + vma->length;
  827. if ((vma->flags & (VMA_INTERNAL|VMA_UNMAPPED)) && !vma->comment[0])
  828. continue;
  829. char prot[3] = {'-', '-', '-'};
  830. if (vma->prot & PROT_READ)
  831. prot[0] = 'r';
  832. if (vma->prot & PROT_WRITE)
  833. prot[1] = 'w';
  834. if (vma->prot & PROT_EXEC)
  835. prot[2] = 'x';
  836. if (vma->file) {
  837. int dev_major = 0, dev_minor = 0;
  838. unsigned long ino = vma->file->dentry ? vma->file->dentry->ino : 0;
  839. const char * name = "[unknown]";
  840. if (!qstrempty(&vma->file->path))
  841. name = qstrgetstr(&vma->file->path);
  842. cnt += snprintf(buf + cnt, size - cnt,
  843. start > (void *) 0xffffffff ? "%lx" : "%08x",
  844. start);
  845. cnt += snprintf(buf + cnt, size - cnt,
  846. end > (void *) 0xffffffff ? "-%lx" : "-%08x", end);
  847. cnt += snprintf(buf + cnt, size - cnt,
  848. " %c%c%cp %08x %02d:%02d %u %s\n",
  849. prot[0], prot[1], prot[2],
  850. vma->offset, dev_major, dev_minor, ino, name);
  851. } else {
  852. cnt += snprintf(buf + cnt, size - cnt,
  853. start > (void *) 0xffffffff ? "%lx" : "%08x",
  854. start);
  855. cnt += snprintf(buf + cnt, size - cnt,
  856. end > (void *) 0xffffffff ? "-%lx" : "-%08x", end);
  857. if (vma->comment[0])
  858. cnt += snprintf(buf + cnt, size - cnt,
  859. " %c%c%cp 00000000 00:00 0 [%s]\n",
  860. prot[0], prot[1], prot[2], vma->comment);
  861. else
  862. cnt += snprintf(buf + cnt, size - cnt,
  863. " %c%c%cp 00000000 00:00 0\n",
  864. prot[0], prot[1], prot[2]);
  865. }
  866. if (cnt >= size) {
  867. cnt = -EOVERFLOW;
  868. break;
  869. }
  870. }
  871. unlock(vma_list_lock);
  872. return cnt;
  873. }
  874. void unmap_all_vmas (void)
  875. {
  876. struct shim_thread * cur_thread = get_cur_thread();
  877. struct shim_vma * tmp, * n;
  878. void * start = NULL, * end = NULL;
  879. lock(vma_list_lock);
  880. listp_for_each_entry_safe(tmp, n, &vma_list, list) {
  881. /* a adhoc vma can never be removed */
  882. if (tmp->flags & VMA_INTERNAL)
  883. continue;
  884. if (tmp->flags & VMA_UNMAPPED) {
  885. __remove_vma(tmp);
  886. continue;
  887. }
  888. if (cur_thread->stack &&
  889. test_vma_overlap(tmp, cur_thread->stack,
  890. cur_thread->stack_top - cur_thread->stack))
  891. continue;
  892. if (start == NULL)
  893. start = end = tmp->addr;
  894. if (end == tmp->addr) {
  895. end += tmp->length;
  896. __remove_vma(tmp);
  897. continue;
  898. }
  899. debug("removing vma %p - %p\n", start, end);
  900. DkVirtualMemoryFree(start, end - start);
  901. start = end = tmp->addr;
  902. end += tmp->length;
  903. __remove_vma(tmp);
  904. }
  905. if (start != NULL && start < end) {
  906. debug("removing vma %p - %p\n", start, end);
  907. DkVirtualMemoryFree(start, end - start);
  908. }
  909. unlock(vma_list_lock);
  910. }
  911. BEGIN_CP_FUNC(vma)
  912. {
  913. assert(size == sizeof(struct shim_vma));
  914. struct shim_vma * vma = (struct shim_vma *) obj;
  915. struct shim_vma * new_vma = NULL;
  916. PAL_FLG pal_prot = PAL_PROT(vma->prot, 0);
  917. ptr_t off = GET_FROM_CP_MAP(obj);
  918. if (!off) {
  919. off = ADD_CP_OFFSET(sizeof(struct shim_vma));
  920. ADD_TO_CP_MAP(obj, off);
  921. new_vma = (struct shim_vma *) (base + off);
  922. memcpy(new_vma, vma, sizeof(struct shim_vma));
  923. if (vma->file)
  924. DO_CP(handle, vma->file, &new_vma->file);
  925. REF_SET(new_vma->ref_count, 0);
  926. INIT_LIST_HEAD(new_vma, list);
  927. void * need_mapped = vma->addr;
  928. #if MIGRATE_MORE_GIPC == 1
  929. if (store->use_gipc) {
  930. if (!NEED_MIGRATE_MEMORY_IF_GIPC(vma))
  931. goto no_mem;
  932. } else {
  933. if (!NEED_MIGRATE_MEMORY(vma))
  934. goto no_mem;
  935. }
  936. #else
  937. if (!NEED_MIGRATE_MEMORY(vma))
  938. goto no_mem;
  939. #endif
  940. void * send_addr = vma->addr;
  941. uint64_t send_size = vma->length;
  942. bool protected = false;
  943. if (vma->file) {
  944. /*
  945. * Chia-Che 8/13/2017:
  946. * A fix for cloning a private VMA which maps a file to a process.
  947. *
  948. * (1) Application can access any page backed by the file, wholly
  949. * or partially.
  950. *
  951. * (2) Access beyond the last file-backed page will cause SIGBUS.
  952. * For reducing fork latency, the following code truncates the
  953. * memory size for migrating a process. The memory size is
  954. * truncated to the file size, round up to pages.
  955. *
  956. * (3) Data in the last file-backed page is valid before or after
  957. * forking. Has to be included in process migration.
  958. */
  959. uint64_t file_len = get_file_size(vma->file);
  960. if (file_len >= 0 &&
  961. vma->offset + vma->length > file_len) {
  962. send_size = file_len > vma->offset ?
  963. file_len - vma->offset : 0;
  964. send_size = ALIGN_UP(send_size);
  965. }
  966. }
  967. if (!send_size)
  968. goto no_mem;
  969. if (store->use_gipc) {
  970. #if HASH_GIPC == 1
  971. if (!(pal_prot & PAL_PROT_READ)) {
  972. protected = true;
  973. DkVirtualMemoryProtect(send_addr,
  974. send_size,
  975. pal_prot|PAL_PROT_READ);
  976. }
  977. #endif /* HASH_GIPC == 1 */
  978. struct shim_gipc_entry * gipc;
  979. DO_CP_SIZE(gipc, send_addr, send_size, &gipc);
  980. gipc->mem.prot = pal_prot;
  981. } else {
  982. if (!(pal_prot & PROT_READ)) {
  983. protected = true;
  984. DkVirtualMemoryProtect(send_addr,
  985. send_size,
  986. pal_prot|PAL_PROT_READ);
  987. }
  988. struct shim_mem_entry * mem;
  989. DO_CP_SIZE(memory, send_addr, send_size, &mem);
  990. mem->prot = pal_prot;
  991. }
  992. need_mapped = vma->addr + vma->length;
  993. if (protected)
  994. DkVirtualMemoryProtect(send_addr, send_size, pal_prot);
  995. no_mem:
  996. ADD_CP_FUNC_ENTRY(off);
  997. ADD_CP_ENTRY(ADDR, need_mapped);
  998. } else {
  999. new_vma = (struct shim_vma *) (base + off);
  1000. }
  1001. if (objp)
  1002. *objp = (void *) new_vma;
  1003. }
  1004. END_CP_FUNC(vma)
  1005. DEFINE_PROFILE_CATAGORY(inside_rs_vma, resume_func);
  1006. DEFINE_PROFILE_INTERVAL(vma_lookup_overlap, inside_rs_vma);
  1007. DEFINE_PROFILE_INTERVAL(vma_add_bookkeep, inside_rs_vma);
  1008. DEFINE_PROFILE_INTERVAL(vma_map_file, inside_rs_vma);
  1009. DEFINE_PROFILE_INTERVAL(vma_map_anonymous, inside_rs_vma);
  1010. BEGIN_RS_FUNC(vma)
  1011. {
  1012. struct shim_vma * vma = (void *) (base + GET_CP_FUNC_ENTRY());
  1013. struct shim_vma * tmp, * prev = NULL;
  1014. void * need_mapped = (void *) GET_CP_ENTRY(ADDR);
  1015. int ret = 0;
  1016. CP_REBASE(vma->file);
  1017. CP_REBASE(vma->list);
  1018. lock(vma_list_lock);
  1019. BEGIN_PROFILE_INTERVAL();
  1020. tmp = __lookup_overlap_vma(vma->addr, vma->length, &prev);
  1021. SAVE_PROFILE_INTERVAL(vma_lookup_overlap);
  1022. if (tmp) {
  1023. if ((ret = __bkeep_munmap(vma->addr, vma->length, &vma->flags)) < 0)
  1024. return ret;
  1025. if (prev->list.next == tmp &&
  1026. tmp->addr < vma->addr)
  1027. prev = tmp;
  1028. }
  1029. get_vma(vma);
  1030. listp_add_after(vma, prev, &vma_list, list);
  1031. assert_vma();
  1032. SAVE_PROFILE_INTERVAL(vma_add_bookkeep);
  1033. __check_delayed_bkeep();
  1034. unlock(vma_list_lock);
  1035. debug("vma: %p-%p flags %x prot %p\n", vma->addr, vma->addr + vma->length,
  1036. vma->flags, vma->prot);
  1037. if (!(vma->flags & VMA_UNMAPPED)) {
  1038. if (vma->file) {
  1039. struct shim_mount * fs = vma->file->fs;
  1040. get_handle(vma->file);
  1041. if (need_mapped < vma->addr + vma->length) {
  1042. /* first try, use hstat to force it resumes pal handle */
  1043. assert(vma->file->fs && vma->file->fs->fs_ops &&
  1044. vma->file->fs->fs_ops->mmap);
  1045. void * addr = need_mapped;
  1046. int ret = fs->fs_ops->mmap(vma->file, &addr,
  1047. vma->addr + vma->length -
  1048. need_mapped,
  1049. vma->prot,
  1050. vma->flags,
  1051. vma->offset +
  1052. (need_mapped - vma->addr));
  1053. if (ret < 0)
  1054. return ret;
  1055. if (!addr)
  1056. return -ENOMEM;
  1057. if (addr != need_mapped)
  1058. return -EACCES;
  1059. need_mapped += vma->length;
  1060. SAVE_PROFILE_INTERVAL(vma_map_file);
  1061. }
  1062. }
  1063. if (need_mapped < vma->addr + vma->length) {
  1064. int pal_alloc_type = 0;
  1065. int pal_prot = vma->prot;
  1066. if (DkVirtualMemoryAlloc(need_mapped,
  1067. vma->addr + vma->length - need_mapped,
  1068. pal_alloc_type, pal_prot)) {
  1069. need_mapped += vma->length;
  1070. SAVE_PROFILE_INTERVAL(vma_map_anonymous);
  1071. }
  1072. }
  1073. if (need_mapped < vma->addr + vma->length)
  1074. sys_printf("vma %p-%p cannot be allocated!\n", need_mapped,
  1075. vma->addr + vma->length);
  1076. }
  1077. if (vma->file)
  1078. get_handle(vma->file);
  1079. if (vma->file)
  1080. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d,path=%s,uri=%s",
  1081. vma->addr, vma->addr + vma->length, vma->length,
  1082. vma->prot, vma->flags, vma->offset,
  1083. qstrgetstr(&vma->file->path), qstrgetstr(&vma->file->uri));
  1084. else
  1085. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d",
  1086. vma->addr, vma->addr + vma->length, vma->length,
  1087. vma->prot, vma->flags, vma->offset);
  1088. }
  1089. END_RS_FUNC(vma)
  1090. BEGIN_CP_FUNC(all_vmas)
  1091. {
  1092. struct shim_vma * tmp, ** vmas;
  1093. int nvmas = 0, cnt = 0;
  1094. lock(vma_list_lock);
  1095. __shrink_vmas();
  1096. listp_for_each_entry(tmp, &vma_list, list)
  1097. if (!(tmp->flags & VMA_INTERNAL))
  1098. nvmas++;
  1099. if (!nvmas) {
  1100. unlock(vma_list_lock);
  1101. return 0;
  1102. }
  1103. vmas = __alloca(sizeof(struct shim_vam *) * nvmas);
  1104. listp_for_each_entry(tmp, &vma_list, list)
  1105. if (!(tmp->flags & VMA_INTERNAL)) {
  1106. get_vma(tmp);
  1107. vmas[cnt++] = tmp;
  1108. }
  1109. unlock(vma_list_lock);
  1110. for (cnt = 0 ; cnt < nvmas ; cnt++) {
  1111. DO_CP(vma, vmas[cnt], NULL);
  1112. put_vma(vmas[cnt]);
  1113. }
  1114. }
  1115. END_CP_FUNC_NO_RS(all_vmas)
  1116. void debug_print_vma_list (void)
  1117. {
  1118. sys_printf("vma bookkeeping:\n");
  1119. struct shim_vma * vma;
  1120. listp_for_each_entry(vma, &vma_list, list) {
  1121. const char * type = "", * name = "";
  1122. if (vma->file) {
  1123. if (!qstrempty(&vma->file->path)) {
  1124. type = " path=";
  1125. name = qstrgetstr(&vma->file->path);
  1126. } else if (!qstrempty(&vma->file->uri)) {
  1127. type = " uri=";
  1128. name = qstrgetstr(&vma->file->uri);
  1129. }
  1130. }
  1131. sys_printf("[%p-%p] prot=%08x flags=%08x%s%s offset=%d%s%s%s%s\n",
  1132. vma->addr, vma->addr + vma->length,
  1133. vma->prot,
  1134. vma->flags & ~(VMA_INTERNAL|VMA_UNMAPPED|VMA_TAINTED),
  1135. type, name,
  1136. vma->offset,
  1137. vma->flags & VMA_INTERNAL ? " (internal)" : "",
  1138. vma->flags & VMA_UNMAPPED ? " (unmapped)" : "",
  1139. vma->comment[0] ? " comment=" : "",
  1140. vma->comment[0] ? vma->comment : "");
  1141. }
  1142. }
  1143. void print_vma_hash (struct shim_vma * vma, void * addr, uint64_t len,
  1144. bool force_protect)
  1145. {
  1146. if (!addr)
  1147. addr = vma->addr;
  1148. if (!len)
  1149. len = vma->length - (addr - vma->addr);
  1150. if (addr < vma->addr || addr + len > vma->addr + vma->length)
  1151. return;
  1152. if (!(vma->prot & PROT_READ)) {
  1153. if (!force_protect)
  1154. return;
  1155. DkVirtualMemoryProtect(vma->addr, vma->length, PAL_PROT_READ);
  1156. }
  1157. for (uint64_t p = (uint64_t) addr ;
  1158. p < (uint64_t) addr + len ; p += allocsize) {
  1159. uint64_t hash = 0;
  1160. struct shim_md5_ctx ctx;
  1161. md5_init(&ctx);
  1162. md5_update(&ctx, (void *) p, allocsize);
  1163. md5_final(&ctx);
  1164. memcpy(&hash, ctx.digest, sizeof(uint64_t));
  1165. }
  1166. if (!(vma->prot & PROT_READ))
  1167. DkVirtualMemoryProtect(vma->addr, vma->length, vma->prot);
  1168. }