shim_vma.c 35 KB

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