shim_vma.c 40 KB

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