shim_vma.c 39 KB

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