shim_vma.c 39 KB

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