shim_vma.c 40 KB

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