shim_vma.c 39 KB

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