shim_vma.c 41 KB

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