shim_vma.c 42 KB

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