shim_vma.c 38 KB

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