shim_vma.c 38 KB

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