shim_vma.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_vma.c
  17. *
  18. * This file contains code to maintain bookkeeping of VMAs in library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_fs.h>
  26. #include <pal.h>
  27. #include <list.h>
  28. #include <asm/mman.h>
  29. #include <errno.h>
  30. #include <stdbool.h>
  31. /*
  32. * Internal bookkeeping for VMAs (virtual memory areas). This data
  33. * structure can only be accessed in this source file, with vma_list_lock
  34. * held. No reference counting needed in this data structure.
  35. */
  36. DEFINE_LIST(shim_vma);
  37. /* struct shim_vma tracks the area of [start, end) */
  38. struct shim_vma {
  39. LIST_TYPE(shim_vma) list;
  40. void * start;
  41. void * end;
  42. int prot;
  43. int flags;
  44. uint64_t offset;
  45. struct shim_handle * file;
  46. char comment[VMA_COMMENT_LEN];
  47. };
  48. #define VMA_MGR_ALLOC DEFAULT_VMA_COUNT
  49. #define PAGE_SIZE allocsize
  50. #define RESERVED_VMAS 4
  51. static struct shim_vma * reserved_vmas[RESERVED_VMAS];
  52. static struct shim_vma early_vmas[RESERVED_VMAS];
  53. static void * __bkeep_unmapped (void * top_addr, void * bottom_addr,
  54. uint64_t length, int prot, int flags,
  55. struct shim_handle * file,
  56. uint64_t offset, const char * comment);
  57. /*
  58. * Because the default system_malloc() must create VMA(s), we need
  59. * a new system_malloc() to avoid cicular dependency. This __malloc()
  60. * stores the VMA address and size in the current thread to delay the
  61. * bookkeeping until the allocator finishes extension.
  62. */
  63. static inline void * __malloc (size_t size)
  64. {
  65. void * addr;
  66. size = ALIGN_UP(size);
  67. /*
  68. * Chia-Che 3/3/18: We must enforce the policy that all VMAs have to
  69. * be created before issuing the PAL calls.
  70. */
  71. addr = __bkeep_unmapped(PAL_CB(user_address.end),
  72. PAL_CB(user_address.start), size,
  73. PROT_READ|PROT_WRITE,
  74. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  75. NULL, 0, "vma");
  76. debug("allocate %p-%p for vmas\n", addr, addr + size);
  77. return (void *) DkVirtualMemoryAlloc(addr, size, 0,
  78. PAL_PROT_WRITE|PAL_PROT_READ);
  79. }
  80. #undef system_malloc
  81. #define system_malloc __malloc
  82. #define OBJ_TYPE struct shim_vma
  83. #include <memmgr.h>
  84. /*
  85. * "vma_mgr" has no specific lock. "vma_list_lock" must be held when
  86. * allocating or freeing any VMAs.
  87. */
  88. static MEM_MGR vma_mgr = NULL;
  89. /*
  90. * "vma_list" contains a sorted list of non-overlapping VMAs.
  91. * "vma_list_lock" must be held when accessing either the vma_list or any
  92. * field of a VMA.
  93. */
  94. DEFINE_LISTP(shim_vma);
  95. static LISTP_TYPE(shim_vma) vma_list = LISTP_INIT;
  96. static LOCKTYPE vma_list_lock;
  97. /*
  98. * Return true if [s, e) is exactly the area represented by vma.
  99. */
  100. static inline bool test_vma_equal (struct shim_vma * vma,
  101. void * s, void * e)
  102. {
  103. return vma->start == s && vma->end == e;
  104. }
  105. /*
  106. * Return true if [s, e) is part of the area represented by vma.
  107. */
  108. static inline bool test_vma_contain (struct shim_vma * vma,
  109. void * s, void * e)
  110. {
  111. return vma->start <= s && vma->end >= e;
  112. }
  113. /*
  114. * Return true if [s, e) contains the starting address of vma.
  115. */
  116. static inline bool test_vma_startin (struct shim_vma * vma,
  117. void * s, void * e)
  118. {
  119. return vma->start >= s && vma->start < e;
  120. }
  121. /*
  122. * Return true if [s, e) contains the ending address of vma.
  123. */
  124. static inline bool test_vma_endin (struct shim_vma * vma,
  125. void * s, void * e)
  126. {
  127. return vma->end > s && vma->end <= e;
  128. }
  129. /*
  130. * Return true if [s, e) overlaps with the area represented by vma.
  131. */
  132. static inline bool test_vma_overlap (struct shim_vma * vma,
  133. void * s, void * e)
  134. {
  135. return test_vma_contain(vma, s, s + 1) ||
  136. test_vma_contain(vma, e - 1, e) ||
  137. test_vma_startin(vma, s, e);
  138. }
  139. static inline void __assert_vma_list (void)
  140. {
  141. struct shim_vma * tmp;
  142. struct shim_vma * prev __attribute__((unused)) = NULL;
  143. listp_for_each_entry(tmp, &vma_list, list) {
  144. /* Assert we are really sorted */
  145. assert(tmp->end > tmp->start);
  146. assert(!prev || prev->end <= tmp->start);
  147. prev = tmp;
  148. }
  149. }
  150. /*
  151. * __lookup_vma() returns the VMA that contains the address; otherwise,
  152. * returns NULL. "pprev" returns the highest VMA below the address.
  153. * __lookup_vma() fills "pprev" even when the function cannot find a
  154. * matching vma for "addr".
  155. *
  156. * vma_list_lock must be held when calling this function.
  157. */
  158. static inline struct shim_vma *
  159. __lookup_vma (void * addr, struct shim_vma ** pprev)
  160. {
  161. struct shim_vma * vma, * prev = NULL;
  162. listp_for_each_entry(vma, &vma_list, list) {
  163. if (addr < vma->start)
  164. goto none;
  165. if (test_vma_contain(vma, addr, addr + 1))
  166. goto out;
  167. assert(vma->end > vma->start);
  168. assert(!prev || prev->end <= vma->start);
  169. prev = vma;
  170. }
  171. none:
  172. vma = NULL;
  173. out:
  174. if (pprev) *pprev = prev;
  175. return vma;
  176. }
  177. /*
  178. * __insert_vma() places "vma" after "prev", or at the beginning of
  179. * vma_list if "prev" is NULL. vma_list_lock must be held when calling
  180. * this function.
  181. */
  182. static inline void
  183. __insert_vma (struct shim_vma * vma, struct shim_vma * prev)
  184. {
  185. assert(!prev || prev->end <= vma->start);
  186. assert(vma != prev);
  187. /* check the next entry */
  188. struct shim_vma * next = prev ?
  189. listp_next_entry(prev, &vma_list, list) :
  190. listp_first_entry(&vma_list, struct shim_vma, list);
  191. assert(!next || vma->end <= next->start);
  192. if (prev)
  193. listp_add_after(vma, prev, &vma_list, list);
  194. else
  195. listp_add(vma, &vma_list, list);
  196. }
  197. /*
  198. * __remove_vma() removes "vma" after "prev", or at the beginnning of
  199. * vma_list if "prev" is NULL. vma_list_lock must be held when calling
  200. * this function.
  201. */
  202. static inline void
  203. __remove_vma (struct shim_vma * vma, struct shim_vma * prev)
  204. {
  205. assert(vma != prev);
  206. listp_del(vma, &vma_list, list);
  207. }
  208. /*
  209. * Storing a cursor pointing to the current heap top. With ASLR, the cursor
  210. * is randomized at initialization. The cursor is monotonically decremented
  211. * when allocating user VMAs. Updating this cursor needs holding vma_list_lock.
  212. */
  213. static void * current_heap_top;
  214. static int __bkeep_mmap (struct shim_vma * prev,
  215. void * start, void * end, int prot, int flags,
  216. struct shim_handle * file, uint64_t offset,
  217. const char * comment);
  218. static int __bkeep_munmap (struct shim_vma ** prev,
  219. void * start, void * end, int flags);
  220. static int __bkeep_mprotect (struct shim_vma * prev,
  221. void * start, void * end, int prot, int flags);
  222. static int
  223. __bkeep_preloaded (void * start, void * end, int prot, int flags,
  224. const char * comment)
  225. {
  226. if (!start || !end || start == end)
  227. return 0;
  228. struct shim_vma * prev = NULL;
  229. __lookup_vma(start, &prev);
  230. return __bkeep_mmap(prev, start, end, prot, flags, NULL, 0, comment);
  231. }
  232. int init_vma (void)
  233. {
  234. int ret;
  235. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  236. reserved_vmas[i] = &early_vmas[i];
  237. /* Bookkeeping for preloaded areas */
  238. ret = __bkeep_preloaded(PAL_CB(executable_range.start),
  239. PAL_CB(executable_range.end),
  240. PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|VMA_UNMAPPED,
  241. "exec");
  242. if (ret < 0)
  243. return ret;
  244. ret = __bkeep_preloaded(PAL_CB(manifest_preload.start),
  245. PAL_CB(manifest_preload.end),
  246. PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  247. "manifest");
  248. if (ret < 0)
  249. return ret;
  250. /* Initialize the allocator */
  251. if (!(vma_mgr = create_mem_mgr(init_align_up(VMA_MGR_ALLOC)))) {
  252. debug("failed creating the VMA allocator\n");
  253. return -ENOMEM;
  254. }
  255. for (int i = 0 ; i < RESERVED_VMAS ; i++) {
  256. if (!reserved_vmas[i]) {
  257. struct shim_vma * new = get_mem_obj_from_mgr(vma_mgr);
  258. assert(new);
  259. struct shim_vma * e = &early_vmas[i];
  260. struct shim_vma * prev = listp_prev_entry(e, &vma_list, list);
  261. debug("Converting early VMA [%p] %p-%p\n", e, e->start, e->end);
  262. memcpy(new, e, sizeof(*e));
  263. INIT_LIST_HEAD(new, list);
  264. __remove_vma(e, prev);
  265. __insert_vma(new, prev);
  266. }
  267. /* replace all reserved VMAs */
  268. reserved_vmas[i] = get_mem_obj_from_mgr(vma_mgr);
  269. assert(reserved_vmas[i]);
  270. }
  271. create_lock(vma_list_lock);
  272. current_heap_top = PAL_CB(user_address.end);
  273. #if ENABLE_ASLR == 1
  274. /*
  275. * Randomize the heap top in top 5/6 of the user address space.
  276. * This is a simplified version of the mmap_base() logic in the Linux
  277. * kernel: https://elixir.bootlin.com/linux/v4.8/ident/mmap_base
  278. */
  279. uint64_t addr_rand_size =
  280. (PAL_CB(user_address.end) - PAL_CB(user_address.start)) * 5 / 6;
  281. uint64_t rand;
  282. getrand(&rand, sizeof(rand));
  283. current_heap_top -= ALIGN_DOWN(rand % addr_rand_size);
  284. #endif
  285. debug("heap top adjusted to %p\n", current_heap_top);
  286. return 0;
  287. }
  288. static inline struct shim_vma * __get_new_vma (void)
  289. {
  290. struct shim_vma * tmp;
  291. if (vma_mgr) {
  292. tmp = get_mem_obj_from_mgr(vma_mgr);
  293. if (tmp)
  294. goto out;
  295. }
  296. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  297. if (reserved_vmas[i]) {
  298. tmp = reserved_vmas[i];
  299. reserved_vmas[i] = NULL;
  300. goto out;
  301. }
  302. /* Should never reach here; if this happens, increase RESERVED_VMAS */
  303. debug("failed to allocate new vma\n");
  304. bug();
  305. return NULL;
  306. out:
  307. memset(tmp, 0, sizeof(*tmp));
  308. INIT_LIST_HEAD(tmp, list);
  309. return tmp;
  310. }
  311. static inline void __restore_reserved_vmas (void)
  312. {
  313. bool nothing_reserved;
  314. do {
  315. nothing_reserved = true;
  316. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  317. if (!reserved_vmas[i]) {
  318. struct shim_vma * new =
  319. get_mem_obj_from_mgr_enlarge(vma_mgr,
  320. size_align_up(VMA_MGR_ALLOC));
  321. /* this allocation must succeed */
  322. assert(new);
  323. reserved_vmas[i] = new;
  324. nothing_reserved = false;
  325. }
  326. } while (!nothing_reserved);
  327. }
  328. static inline void __drop_vma (struct shim_vma * vma)
  329. {
  330. if (vma->file)
  331. put_handle(vma->file);
  332. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  333. if (!reserved_vmas[i]) {
  334. reserved_vmas[i] = vma;
  335. return;
  336. }
  337. free_mem_obj_to_mgr(vma_mgr, vma);
  338. }
  339. static inline void
  340. __assert_vma_flags (const struct shim_vma * vma, int flags)
  341. {
  342. if (!(vma->flags & VMA_UNMAPPED)
  343. && VMA_TYPE(vma->flags) != VMA_TYPE(flags)) {
  344. debug("Check vma flag failure: vma flags %x, checked flags %x\n",
  345. vma->flags, flags);
  346. bug();
  347. }
  348. }
  349. static inline void
  350. __set_vma_comment (struct shim_vma * vma, const char * comment)
  351. {
  352. if (!comment) {
  353. vma->comment[0] = 0;
  354. return;
  355. }
  356. uint64_t len = strlen(comment);
  357. if (len > VMA_COMMENT_LEN - 1)
  358. len = VMA_COMMENT_LEN - 1;
  359. memcpy(vma->comment, comment, len);
  360. vma->comment[len] = 0;
  361. }
  362. /*
  363. * Add bookkeeping for mmap(). "prev" must point to the the immediately
  364. * precedent vma of the address to map, or be NULL if no vma is lower than
  365. * the address. If the bookkeeping area overlaps with some existing vmas,
  366. * we must check whether the caller (from user, internal code, or checkpointing
  367. * procedure) is allowed to overwrite the existing vmas.
  368. *
  369. * Bookkeeping convention (must follow):
  370. * Create the bookkeeping BEFORE any allocation PAL calls
  371. * (DkVirtualMemoryAlloc() or DkStreamMap()).
  372. */
  373. static int __bkeep_mmap (struct shim_vma * prev,
  374. void * start, void * end, int prot, int flags,
  375. struct shim_handle * file, uint64_t offset,
  376. const char * comment)
  377. {
  378. int ret = 0;
  379. struct shim_vma * new = __get_new_vma();
  380. /* First, remove any overlapping VMAs */
  381. ret = __bkeep_munmap(&prev, start, end, flags);
  382. if (ret < 0) {
  383. __drop_vma(new);
  384. return ret;
  385. }
  386. /* Inserting the new VMA */
  387. new->start = start;
  388. new->end = end;
  389. new->prot = prot;
  390. new->flags = flags|((file && (prot & PROT_WRITE)) ? VMA_TAINTED : 0);
  391. new->file = file;
  392. if (new->file)
  393. get_handle(new->file);
  394. new->offset = offset;
  395. __set_vma_comment(new, comment);
  396. __insert_vma(new, prev);
  397. return 0;
  398. }
  399. int bkeep_mmap (void * addr, uint64_t length, int prot, int flags,
  400. struct shim_handle * file, uint64_t offset,
  401. const char * comment)
  402. {
  403. if (!addr || !length)
  404. return -EINVAL;
  405. if (comment && !comment[0])
  406. comment = NULL;
  407. debug("bkeep_mmap: %p-%p\n", addr, addr + length);
  408. lock(vma_list_lock);
  409. struct shim_vma * prev = NULL;
  410. __lookup_vma(addr, &prev);
  411. int ret = __bkeep_mmap(prev, addr, addr + length, prot, flags, file, offset,
  412. comment);
  413. __restore_reserved_vmas();
  414. unlock(vma_list_lock);
  415. return ret;
  416. }
  417. /*
  418. * __shrink_vma() removes the area in a VMA that overlaps with [start, end).
  419. * The function deals with three cases:
  420. * (1) [start, end) overlaps with the beginning of the VMA.
  421. * (2) [start, end) overlaps with the ending of the VMA.
  422. * (3) [start, end) overlaps with the middle of the VMA. In this case, the VMA
  423. * is splitted into two. The new VMA is stored in 'tailptr'.
  424. * In either of these cases, "vma" is the only one changed among vma_list.
  425. */
  426. static inline void __shrink_vma (struct shim_vma * vma, void * start, void * end,
  427. struct shim_vma ** tailptr)
  428. {
  429. /*
  430. * Dealing with the head: if the starting address of "vma" is in
  431. * [start, end), move the starting address.
  432. */
  433. if (test_vma_startin(vma, start, end)) {
  434. if (end < vma->end) {
  435. if (vma->file) /* must adjust offset */
  436. vma->offset += end - vma->start;
  437. vma->start = end;
  438. } else {
  439. if (vma->file) /* must adjust offset */
  440. vma->offset += vma->end - vma->start;
  441. vma->start = vma->end;
  442. }
  443. goto finish;
  444. }
  445. /*
  446. * Dealing with the tail: if the ending address of "vma" is in
  447. * [start, end), move the ending address.
  448. */
  449. if (test_vma_endin(vma, start, end)) {
  450. if (start > vma->start) {
  451. vma->end = start;
  452. } else {
  453. vma->end = vma->start;
  454. }
  455. /* offset is not affected */
  456. goto finish;
  457. }
  458. /*
  459. * If [start, end) is inside the range of "vma", divide up
  460. * the VMA. A new VMA is created to represent the remaining tail.
  461. */
  462. if (test_vma_contain(vma, start, end)) {
  463. void * old_end = vma->end;
  464. vma->end = start;
  465. /* Remaining space after [start, end), creating a new VMA */
  466. if (old_end > end) {
  467. struct shim_vma * tail = __get_new_vma();
  468. tail->start = end;
  469. tail->end = old_end;
  470. tail->prot = vma->prot;
  471. tail->flags = vma->flags;
  472. tail->file = vma->file;
  473. if (tail->file) {
  474. get_handle(tail->file);
  475. tail->offset = vma->offset + (tail->start - vma->start);
  476. } else {
  477. tail->offset = 0;
  478. }
  479. memcpy(tail->comment, vma->comment, VMA_COMMENT_LEN);
  480. *tailptr = tail;
  481. }
  482. goto finish;
  483. }
  484. /* Never reach here */
  485. bug();
  486. finish:
  487. assert(!test_vma_overlap(vma, start, end));
  488. assert(vma->start < vma->end);
  489. }
  490. /*
  491. * Update bookkeeping for munmap(). "*pprev" must point to the immediately
  492. * precedent vma of the address to unmap, or be NULL if no vma is lower than
  493. * the address. If the bookkeeping area overlaps with some existing vmas,
  494. * we must check whether the caller (from user, internal code, or checkpointing
  495. * procedure) is allowed to overwrite the existing vmas. "pprev" can be
  496. * updated if a new vma lower than the unmapping address is added.
  497. *
  498. * Bookkeeping convention (must follow):
  499. * Make deallocation PAL calls (DkVirtualMemoryFree() or DkStreamUnmap())
  500. * BEFORE updating the bookkeeping.
  501. */
  502. static int __bkeep_munmap (struct shim_vma ** pprev,
  503. void * start, void * end, int flags)
  504. {
  505. struct shim_vma * prev = *pprev;
  506. struct shim_vma * cur, * next;
  507. if (!prev) {
  508. cur = listp_first_entry(&vma_list, struct shim_vma, list);
  509. if (!cur)
  510. return 0;
  511. } else {
  512. cur = listp_next_entry(prev, &vma_list, list);
  513. }
  514. next = cur ? listp_next_entry(cur, &vma_list, list) : NULL;
  515. while (cur) {
  516. struct shim_vma * tail = NULL;
  517. /* Stop unmapping if "cur" no longer overlaps with [start, end) */
  518. if (!test_vma_overlap(cur, start, end))
  519. break;
  520. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  521. return -EACCES;
  522. /* If [start, end) contains the VMA, just drop the VMA. */
  523. if (start <= cur->start && cur->end <= end) {
  524. __remove_vma(cur, prev);
  525. __drop_vma(cur);
  526. goto cont;
  527. }
  528. __shrink_vma(cur, start, end, &tail);
  529. if (cur->end <= start) {
  530. prev = cur;
  531. if (tail) {
  532. __insert_vma(tail, cur); /* insert "tail" after "cur" */
  533. cur = tail; /* "tail" is the new "cur" */
  534. break;
  535. }
  536. } else if (cur->start >= end) {
  537. /* __shrink_vma() only creates a new VMA when the beginning of the
  538. * original VMA is preserved. */
  539. assert(!tail);
  540. break;
  541. } else {
  542. /* __shrink_vma() should never allow this case. */
  543. bug();
  544. }
  545. cont:
  546. cur = next;
  547. next = cur ? listp_next_entry(cur, &vma_list, list) : NULL;
  548. }
  549. if (prev)
  550. assert(cur == listp_next_entry(prev, &vma_list, list));
  551. else
  552. assert(cur == listp_first_entry(&vma_list, struct shim_vma, list));
  553. assert(!prev || prev->end <= start);
  554. assert(!cur || end <= cur->start);
  555. *pprev = prev;
  556. return 0;
  557. }
  558. int bkeep_munmap (void * addr, uint64_t length, int flags)
  559. {
  560. if (!length)
  561. return -EINVAL;
  562. debug("bkeep_munmap: %p-%p\n", addr, addr + length);
  563. lock(vma_list_lock);
  564. struct shim_vma * prev = NULL;
  565. __lookup_vma(addr, &prev);
  566. int ret = __bkeep_munmap(&prev, addr, addr + length, flags);
  567. __restore_reserved_vmas();
  568. unlock(vma_list_lock);
  569. return ret;
  570. }
  571. /*
  572. * Update bookkeeping for mprotect(). "prev" must point to the immediately
  573. * precedent vma of the address to protect, or be NULL if no vma is lower than
  574. * the address. If the bookkeeping area overlaps with some existing vmas,
  575. * we must check whether the caller (from user, internal code, or checkpointing
  576. * procedure) is allowed to overwrite the existing vmas.
  577. *
  578. * Bookkeeping convention (must follow):
  579. * Update the bookkeeping BEFORE calling DkVirtualMemoryProtect().
  580. */
  581. static int __bkeep_mprotect (struct shim_vma * prev,
  582. void * start, void * end, int prot, int flags)
  583. {
  584. struct shim_vma * cur, * next;
  585. if (!prev) {
  586. cur = listp_first_entry(&vma_list, struct shim_vma, list);
  587. if (!cur)
  588. return 0;
  589. } else {
  590. cur = listp_next_entry(prev, &vma_list, list);
  591. }
  592. next = cur ? listp_next_entry(cur, &vma_list, list) : NULL;
  593. while (cur) {
  594. struct shim_vma * new, * tail = NULL;
  595. /* Stop protecting if "cur" no longer overlaps with [start, end) */
  596. if (!test_vma_overlap(cur, start, end))
  597. break;
  598. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  599. /* For now, just shout loudly. */
  600. return -EACCES;
  601. /* If protection doesn't change anything, move on to the next */
  602. if (cur->prot == prot)
  603. goto cont;
  604. /* If [start, end) contains the VMA, just update its protection. */
  605. if (start <= cur->start && cur->end <= end) {
  606. cur->prot = prot;
  607. goto cont;
  608. }
  609. /* Create a new VMA for the protected area */
  610. new = __get_new_vma();
  611. new->start = cur->start > start ? cur->start : start;
  612. new->end = cur->end < end ? cur->end : end;
  613. new->prot = prot;
  614. new->flags = cur->flags;
  615. new->file = cur->file;
  616. if (new->file) {
  617. get_handle(new->file);
  618. new->offset = cur->offset + (new->start - cur->start);
  619. } else {
  620. new->offset = 0;
  621. }
  622. memcpy(new->comment, cur->comment, VMA_COMMENT_LEN);
  623. /* Like unmapping, shrink (and potentially split) the VMA first. */
  624. __shrink_vma(cur, start, end, &tail);
  625. if (cur->end <= start) {
  626. prev = cur;
  627. if (tail) {
  628. __insert_vma(tail, cur); /* insert "tail" after "cur" */
  629. cur = tail; /* "tail" is the new "cur" */
  630. /* "next" is the same */
  631. }
  632. } else if (cur->start >= end) {
  633. /* __shrink_vma() only creates a new VMA when the beginning of the
  634. * original VMA is preserved. */
  635. assert(!tail);
  636. } else {
  637. /* __shrink_vma() should never allow this case. */
  638. bug();
  639. }
  640. /* Now insert the new protected vma between prev and cur */
  641. __insert_vma(new, prev);
  642. assert(!prev || prev->end <= new->end);
  643. assert(new->start < new->end);
  644. cont:
  645. prev = cur;
  646. cur = next;
  647. next = cur ? listp_next_entry(cur, &vma_list, list) : NULL;
  648. }
  649. return 0;
  650. }
  651. int bkeep_mprotect (void * addr, uint64_t length, int prot, int flags)
  652. {
  653. if (!addr || !length)
  654. return -EINVAL;
  655. debug("bkeep_mprotect: %p-%p\n", addr, addr + length);
  656. lock(vma_list_lock);
  657. struct shim_vma * prev = NULL;
  658. __lookup_vma(addr, &prev);
  659. int ret = __bkeep_mprotect(prev, addr, addr + length, prot, flags);
  660. __restore_reserved_vmas();
  661. unlock(vma_list_lock);
  662. return ret;
  663. }
  664. /*
  665. * Search for an unmapped area within [bottom, top) that is big enough
  666. * to allocate "length" bytes. The search approach is top-down.
  667. * If this function returns a non-NULL address, the corresponding VMA is
  668. * added to the VMA list.
  669. */
  670. static void * __bkeep_unmapped (void * top_addr, void * bottom_addr,
  671. uint64_t length, int prot, int flags,
  672. struct shim_handle * file,
  673. uint64_t offset, const char * comment)
  674. {
  675. assert(top_addr > bottom_addr);
  676. if (!length || length > top_addr - bottom_addr)
  677. return NULL;
  678. struct shim_vma * prev = NULL;
  679. struct shim_vma * cur = __lookup_vma(top_addr, &prev);
  680. while (true) {
  681. /* Set the range for searching */
  682. void * end = cur ? cur->start : top_addr;
  683. void * start =
  684. (prev && prev->end > bottom_addr) ? prev->end : bottom_addr;
  685. assert(start <= end);
  686. /* Check if there is enough space between prev and cur */
  687. if (length <= end - start) {
  688. /* create a new VMA at the top of the range */
  689. __bkeep_mmap(prev, end - length, end, prot, flags,
  690. file, offset, comment);
  691. debug("bkeep_unmapped: %p-%p%s%s\n", end - length, end,
  692. comment ? " => " : "", comment ? : "");
  693. return end - length;
  694. }
  695. if (!prev || prev->start <= bottom_addr)
  696. break;
  697. cur = prev;
  698. prev = listp_prev_entry(cur, &vma_list, list);
  699. }
  700. return NULL;
  701. }
  702. void * bkeep_unmapped (void * top_addr, void * bottom_addr, uint64_t length,
  703. int prot, int flags, struct shim_handle * file,
  704. uint64_t offset, const char * comment)
  705. {
  706. lock(vma_list_lock);
  707. void * addr = __bkeep_unmapped(top_addr, bottom_addr, length, prot, flags,
  708. file, offset, comment);
  709. __restore_reserved_vmas();
  710. unlock(vma_list_lock);
  711. return addr;
  712. }
  713. void * bkeep_unmapped_heap (uint64_t length, int prot, int flags,
  714. struct shim_handle * file,
  715. uint64_t offset, const char * comment)
  716. {
  717. lock(vma_list_lock);
  718. void * bottom_addr = PAL_CB(user_address.start);
  719. void * top_addr = current_heap_top;
  720. void * heap_max = PAL_CB(user_address.end);
  721. void * addr = NULL;
  722. #ifdef MAP_32BIT
  723. /*
  724. * If MAP_32BIT is given in the flags, force the searching range to
  725. * be lower than 1ULL << 32.
  726. */
  727. #define ADDR_32BIT ((void *) (1ULL << 32))
  728. if (flags & MAP_32BIT) {
  729. /* Try the lower 4GB memory space */
  730. if (heap_max > ADDR_32BIT)
  731. heap_max = ADDR_32BIT;
  732. if (top_addr > heap_max)
  733. top_addr = heap_max;
  734. }
  735. #endif
  736. if (top_addr <= bottom_addr)
  737. goto again;
  738. /* Try first time */
  739. addr = __bkeep_unmapped(top_addr, bottom_addr,
  740. length, prot, flags,
  741. file, offset, comment);
  742. if (addr) {
  743. /*
  744. * we only update the current heap top when we get the
  745. * address from [bottom_addr, current_heap_top).
  746. */
  747. if (top_addr == current_heap_top) {
  748. debug("heap top adjusted to %p\n", addr);
  749. current_heap_top = addr;
  750. }
  751. goto out;
  752. }
  753. again:
  754. if (top_addr >= heap_max)
  755. goto out;
  756. /* Try to allocate above the current heap top */
  757. addr = __bkeep_unmapped(heap_max, bottom_addr,
  758. length, prot, flags,
  759. file, offset, comment);
  760. out:
  761. __restore_reserved_vmas();
  762. unlock(vma_list_lock);
  763. #ifdef MAP_32BIT
  764. assert(!(flags & MAP_32BIT) || !addr || addr + length <= ADDR_32BIT);
  765. #endif
  766. return addr;
  767. }
  768. static inline void
  769. __dump_vma (struct shim_vma_val * val, const struct shim_vma * vma)
  770. {
  771. val->addr = vma->start;
  772. val->length = vma->end - vma->start;
  773. val->prot = vma->prot;
  774. val->flags = vma->flags;
  775. val->file = vma->file;
  776. if (val->file)
  777. get_handle(val->file);
  778. val->offset = vma->offset;
  779. memcpy(val->comment, vma->comment, VMA_COMMENT_LEN);
  780. }
  781. int lookup_vma (void * addr, struct shim_vma_val * res)
  782. {
  783. lock(vma_list_lock);
  784. struct shim_vma * vma = __lookup_vma(addr, NULL);
  785. if (!vma) {
  786. unlock(vma_list_lock);
  787. return -ENOENT;
  788. }
  789. if (res)
  790. __dump_vma(res, vma);
  791. unlock(vma_list_lock);
  792. return 0;
  793. }
  794. int lookup_overlap_vma (void * addr, uint64_t length,
  795. struct shim_vma_val * res)
  796. {
  797. struct shim_vma * tmp, * vma = NULL;
  798. lock(vma_list_lock);
  799. listp_for_each_entry(tmp, &vma_list, list)
  800. if (test_vma_overlap (tmp, addr, addr + length)) {
  801. vma = tmp;
  802. break;
  803. }
  804. if (!vma) {
  805. unlock(vma_list_lock);
  806. return -ENOENT;
  807. }
  808. if (res)
  809. __dump_vma(res, vma);
  810. unlock(vma_list_lock);
  811. return 0;
  812. }
  813. int dump_all_vmas (struct shim_vma_val * vmas, size_t max_count)
  814. {
  815. struct shim_vma_val * val = vmas;
  816. struct shim_vma * vma;
  817. int cnt = 0;
  818. lock(vma_list_lock);
  819. listp_for_each_entry(vma, &vma_list, list) {
  820. if (VMA_TYPE(vma->flags))
  821. continue;
  822. if (vma->flags & VMA_UNMAPPED)
  823. continue;
  824. if (cnt == max_count) {
  825. cnt = -EOVERFLOW;
  826. for (int i = 0 ; i < max_count ; i++)
  827. if (vmas[i].file)
  828. put_handle(vmas[i].file);
  829. break;
  830. }
  831. __dump_vma(val, vma);
  832. cnt++;
  833. val++;
  834. }
  835. unlock(vma_list_lock);
  836. return cnt;
  837. }
  838. BEGIN_CP_FUNC(vma)
  839. {
  840. assert(size == sizeof(struct shim_vma_val));
  841. struct shim_vma_val * vma = (struct shim_vma_val *) obj;
  842. struct shim_vma_val * new_vma = NULL;
  843. PAL_FLG pal_prot = PAL_PROT(vma->prot, 0);
  844. ptr_t off = GET_FROM_CP_MAP(obj);
  845. if (!off) {
  846. off = ADD_CP_OFFSET(sizeof(*vma));
  847. ADD_TO_CP_MAP(obj, off);
  848. new_vma = (struct shim_vma_val *) (base + off);
  849. memcpy(new_vma, vma, sizeof(*vma));
  850. if (vma->file)
  851. DO_CP(handle, vma->file, &new_vma->file);
  852. void * need_mapped = vma->addr;
  853. #if MIGRATE_MORE_GIPC == 1
  854. if (store->use_gipc ?
  855. !NEED_MIGRATE_MEMORY_IF_GIPC(vma) :
  856. !NEED_MIGRATE_MEMORY(vma))
  857. #else
  858. if (!NEED_MIGRATE_MEMORY(vma))
  859. #endif
  860. goto no_mem;
  861. void * send_addr = vma->addr;
  862. uint64_t send_size = vma->length;
  863. if (vma->file) {
  864. /*
  865. * Chia-Che 8/13/2017:
  866. * A fix for cloning a private VMA which maps a file to a process.
  867. *
  868. * (1) Application can access any page backed by the file, wholly
  869. * or partially.
  870. *
  871. * (2) Access beyond the last file-backed page will cause SIGBUS.
  872. * For reducing fork latency, the following code truncates the
  873. * memory size for migrating a process. The memory size is
  874. * truncated to the file size, round up to pages.
  875. *
  876. * (3) Data in the last file-backed page is valid before or after
  877. * forking. Has to be included in process migration.
  878. */
  879. uint64_t file_len = get_file_size(vma->file);
  880. if (file_len >= 0 &&
  881. vma->offset + vma->length > file_len) {
  882. send_size = file_len > vma->offset ?
  883. file_len - vma->offset : 0;
  884. send_size = ALIGN_UP(send_size);
  885. }
  886. }
  887. if (!send_size)
  888. goto no_mem;
  889. #if HASH_GIPC == 1
  890. if (!(pal_prot & PAL_PROT_READ)) {
  891. #else
  892. if (!store->use_gipc && !(pal_prot & PAL_PROT_READ)) {
  893. #endif
  894. /* Make the area readable */
  895. DkVirtualMemoryProtect(send_addr, send_size,
  896. pal_prot|PAL_PROT_READ);
  897. }
  898. if (store->use_gipc) {
  899. struct shim_gipc_entry * gipc;
  900. DO_CP_SIZE(gipc, send_addr, send_size, &gipc);
  901. gipc->mem.prot = pal_prot;
  902. } else {
  903. struct shim_mem_entry * mem;
  904. DO_CP_SIZE(memory, send_addr, send_size, &mem);
  905. mem->prot = pal_prot;
  906. }
  907. need_mapped = vma->addr + vma->length;
  908. #if HASH_GIPC == 1
  909. if (store->use_gipc && !(pal_prot & PAL_PROT_READ))
  910. DkVirtualMemoryProtect(send_addr, send_size, pal_prot);
  911. #endif
  912. no_mem:
  913. ADD_CP_FUNC_ENTRY(off);
  914. ADD_CP_ENTRY(ADDR, need_mapped);
  915. } else {
  916. new_vma = (struct shim_vma_val *) (base + off);
  917. }
  918. if (objp)
  919. *objp = (void *) new_vma;
  920. }
  921. END_CP_FUNC(vma)
  922. DEFINE_PROFILE_CATAGORY(inside_rs_vma, resume_func);
  923. DEFINE_PROFILE_INTERVAL(vma_add_bookkeep, inside_rs_vma);
  924. DEFINE_PROFILE_INTERVAL(vma_map_file, inside_rs_vma);
  925. DEFINE_PROFILE_INTERVAL(vma_map_anonymous, inside_rs_vma);
  926. BEGIN_RS_FUNC(vma)
  927. {
  928. struct shim_vma_val * vma = (void *) (base + GET_CP_FUNC_ENTRY());
  929. void * need_mapped = (void *) GET_CP_ENTRY(ADDR);
  930. BEGIN_PROFILE_INTERVAL();
  931. CP_REBASE(vma->file);
  932. int ret = bkeep_mmap(vma->addr, vma->length, vma->prot, vma->flags,
  933. vma->file, vma->offset, vma->comment);
  934. if (ret < 0)
  935. return ret;
  936. SAVE_PROFILE_INTERVAL(vma_add_bookkeep);
  937. DEBUG_RS("vma: %p-%p flags %x prot %p\n", vma->addr, vma->addr + vma->length,
  938. vma->flags, vma->prot);
  939. if (!(vma->flags & VMA_UNMAPPED)) {
  940. if (vma->file) {
  941. struct shim_mount * fs = vma->file->fs;
  942. get_handle(vma->file);
  943. if (need_mapped < vma->addr + vma->length) {
  944. /* first try, use hstat to force it resumes pal handle */
  945. assert(vma->file->fs && vma->file->fs->fs_ops &&
  946. vma->file->fs->fs_ops->mmap);
  947. void * addr = need_mapped;
  948. int ret = fs->fs_ops->mmap(vma->file, &addr,
  949. vma->addr + vma->length -
  950. need_mapped,
  951. vma->prot,
  952. vma->flags,
  953. vma->offset +
  954. (need_mapped - vma->addr));
  955. if (ret < 0)
  956. return ret;
  957. if (!addr)
  958. return -ENOMEM;
  959. if (addr != need_mapped)
  960. return -EACCES;
  961. need_mapped += vma->length;
  962. SAVE_PROFILE_INTERVAL(vma_map_file);
  963. }
  964. }
  965. if (need_mapped < vma->addr + vma->length) {
  966. int pal_alloc_type = 0;
  967. int pal_prot = vma->prot;
  968. if (DkVirtualMemoryAlloc(need_mapped,
  969. vma->addr + vma->length - need_mapped,
  970. pal_alloc_type, pal_prot)) {
  971. need_mapped += vma->length;
  972. SAVE_PROFILE_INTERVAL(vma_map_anonymous);
  973. }
  974. }
  975. if (need_mapped < vma->addr + vma->length)
  976. sys_printf("vma %p-%p cannot be allocated!\n", need_mapped,
  977. vma->addr + vma->length);
  978. }
  979. if (vma->file)
  980. get_handle(vma->file);
  981. if (vma->file)
  982. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d,path=%s,uri=%s",
  983. vma->addr, vma->addr + vma->length, vma->length,
  984. vma->prot, vma->flags, vma->offset,
  985. qstrgetstr(&vma->file->path), qstrgetstr(&vma->file->uri));
  986. else
  987. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d",
  988. vma->addr, vma->addr + vma->length, vma->length,
  989. vma->prot, vma->flags, vma->offset);
  990. }
  991. END_RS_FUNC(vma)
  992. BEGIN_CP_FUNC(all_vmas)
  993. {
  994. size_t count = DEFAULT_VMA_COUNT;
  995. struct shim_vma_val * vmas = malloc(sizeof(*vmas) * count);
  996. int ret;
  997. if (!vmas)
  998. return -ENOMEM;
  999. retry_dump_vmas:
  1000. ret = dump_all_vmas(vmas, count);
  1001. if (ret == -EOVERFLOW) {
  1002. struct shim_vma_val * new_vmas
  1003. = malloc(sizeof(*new_vmas) * count * 2);
  1004. if (!new_vmas) {
  1005. free(vmas);
  1006. return -ENOMEM;
  1007. }
  1008. free(vmas);
  1009. vmas = new_vmas;
  1010. count *= 2;
  1011. goto retry_dump_vmas;
  1012. }
  1013. if (ret < 0)
  1014. return ret;
  1015. count = ret;
  1016. for (struct shim_vma_val * vma = &vmas[count - 1] ; vma >= vmas ; vma--)
  1017. DO_CP(vma, vma, NULL);
  1018. free_vma_val_array(vmas, count);
  1019. }
  1020. END_CP_FUNC_NO_RS(all_vmas)
  1021. void debug_print_vma_list (void)
  1022. {
  1023. sys_printf("vma bookkeeping:\n");
  1024. struct shim_vma * vma;
  1025. listp_for_each_entry(vma, &vma_list, list) {
  1026. const char * type = "", * name = "";
  1027. if (vma->file) {
  1028. if (!qstrempty(&vma->file->path)) {
  1029. type = " path=";
  1030. name = qstrgetstr(&vma->file->path);
  1031. } else if (!qstrempty(&vma->file->uri)) {
  1032. type = " uri=";
  1033. name = qstrgetstr(&vma->file->uri);
  1034. }
  1035. }
  1036. sys_printf("[%p-%p] prot=%08x flags=%08x%s%s offset=%d%s%s%s%s\n",
  1037. vma->start, vma->end,
  1038. vma->prot,
  1039. vma->flags & ~(VMA_INTERNAL|VMA_UNMAPPED|VMA_TAINTED|VMA_CP),
  1040. type, name,
  1041. vma->offset,
  1042. vma->flags & VMA_INTERNAL ? " (internal)" : "",
  1043. vma->flags & VMA_UNMAPPED ? " (unmapped)" : "",
  1044. vma->comment[0] ? " comment=" : "",
  1045. vma->comment[0] ? vma->comment : "");
  1046. }
  1047. }