shim_vma.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 struct shim_lock 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. assert(s < e);
  104. return vma->start == s && vma->end == e;
  105. }
  106. /*
  107. * Return true if [s, e) is part of the area represented by vma.
  108. */
  109. static inline bool test_vma_contain (struct shim_vma * vma,
  110. void * s, void * e)
  111. {
  112. assert(s < e);
  113. return vma->start <= s && vma->end >= e;
  114. }
  115. /*
  116. * Return true if [s, e) contains the starting address of vma.
  117. */
  118. static inline bool test_vma_startin (struct shim_vma * vma,
  119. void * s, void * e)
  120. {
  121. assert(s < e);
  122. return vma->start >= s && vma->start < e;
  123. }
  124. /*
  125. * Return true if [s, e) contains the ending address of vma.
  126. */
  127. static inline bool test_vma_endin (struct shim_vma * vma,
  128. void * s, void * e)
  129. {
  130. assert(s < e);
  131. return vma->end > s && vma->end <= e;
  132. }
  133. /*
  134. * Return true if [s, e) overlaps with the area represented by vma.
  135. */
  136. static inline bool test_vma_overlap (struct shim_vma * vma,
  137. void * s, void * e)
  138. {
  139. assert(s < e);
  140. return test_vma_contain(vma, s, s + 1) ||
  141. test_vma_contain(vma, e - 1, e) ||
  142. test_vma_startin(vma, s, e);
  143. }
  144. static inline void __assert_vma_list (void)
  145. {
  146. struct shim_vma * tmp;
  147. struct shim_vma * prev __attribute__((unused)) = NULL;
  148. LISTP_FOR_EACH_ENTRY(tmp, &vma_list, list) {
  149. /* Assert we are really sorted */
  150. assert(tmp->end > tmp->start);
  151. assert(!prev || prev->end <= tmp->start);
  152. prev = tmp;
  153. }
  154. }
  155. // In a debug build only, assert that the VMA list is
  156. // sorted. This should be called with the vma_list_lock held.
  157. static inline void assert_vma_list (void)
  158. {
  159. #ifdef DEBUG
  160. __assert_vma_list();
  161. #endif
  162. }
  163. /*
  164. * __lookup_vma() returns the VMA that contains the address; otherwise,
  165. * returns NULL. "pprev" returns the highest VMA below the address.
  166. * __lookup_vma() fills "pprev" even when the function cannot find a
  167. * matching vma for "addr".
  168. *
  169. * vma_list_lock must be held when calling this function.
  170. */
  171. static inline struct shim_vma *
  172. __lookup_vma (void * addr, struct shim_vma ** pprev)
  173. {
  174. struct shim_vma * vma, * prev = NULL;
  175. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  176. if (addr < vma->start)
  177. goto none;
  178. if (test_vma_contain(vma, addr, addr + 1))
  179. goto out;
  180. assert(vma->end > vma->start);
  181. assert(!prev || prev->end <= vma->start);
  182. prev = vma;
  183. }
  184. none:
  185. vma = NULL;
  186. out:
  187. if (pprev) *pprev = prev;
  188. return vma;
  189. }
  190. /*
  191. * __insert_vma() places "vma" after "prev", or at the beginning of
  192. * vma_list if "prev" is NULL. vma_list_lock must be held when calling
  193. * this function.
  194. */
  195. static inline void
  196. __insert_vma (struct shim_vma * vma, struct shim_vma * prev)
  197. {
  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. assert(!next || vma->end <= next->start);
  205. if (prev)
  206. LISTP_ADD_AFTER(vma, prev, &vma_list, list);
  207. else
  208. LISTP_ADD(vma, &vma_list, list);
  209. }
  210. /*
  211. * __remove_vma() removes "vma" after "prev", or at the beginnning of
  212. * vma_list if "prev" is NULL. vma_list_lock must be held when calling
  213. * this function.
  214. */
  215. static inline void
  216. __remove_vma (struct shim_vma * vma, struct shim_vma * prev)
  217. {
  218. assert(vma != prev);
  219. LISTP_DEL(vma, &vma_list, list);
  220. }
  221. /*
  222. * Storing a cursor pointing to the current heap top. With ASLR, the cursor
  223. * is randomized at initialization. The cursor is monotonically decremented
  224. * when allocating user VMAs. Updating this cursor needs holding vma_list_lock.
  225. */
  226. static void * current_heap_top;
  227. static int __bkeep_mmap (struct shim_vma * prev,
  228. void * start, void * end, int prot, int flags,
  229. struct shim_handle * file, uint64_t offset,
  230. const char * comment);
  231. static int __bkeep_munmap (struct shim_vma ** prev,
  232. void * start, void * end, int flags);
  233. static int __bkeep_mprotect (struct shim_vma * prev,
  234. void * start, void * end, int prot, int flags);
  235. static int
  236. __bkeep_preloaded (void * start, void * end, int prot, int flags,
  237. const char * comment)
  238. {
  239. if (!start || !end || start == end)
  240. return 0;
  241. struct shim_vma * prev = NULL;
  242. __lookup_vma(start, &prev);
  243. return __bkeep_mmap(prev, start, end, prot, flags, NULL, 0, comment);
  244. }
  245. int init_vma (void)
  246. {
  247. int ret;
  248. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  249. reserved_vmas[i] = &early_vmas[i];
  250. /* Bookkeeping for preloaded areas */
  251. ret = __bkeep_preloaded(PAL_CB(executable_range.start),
  252. PAL_CB(executable_range.end),
  253. PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|VMA_UNMAPPED,
  254. "exec");
  255. if (ret < 0)
  256. return ret;
  257. ret = __bkeep_preloaded(PAL_CB(manifest_preload.start),
  258. PAL_CB(manifest_preload.end),
  259. PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  260. "manifest");
  261. if (ret < 0)
  262. return ret;
  263. /* Initialize the allocator */
  264. if (!(vma_mgr = create_mem_mgr(init_align_up(VMA_MGR_ALLOC)))) {
  265. debug("failed creating the VMA allocator\n");
  266. return -ENOMEM;
  267. }
  268. for (int i = 0 ; i < RESERVED_VMAS ; i++) {
  269. if (!reserved_vmas[i]) {
  270. struct shim_vma * new = get_mem_obj_from_mgr(vma_mgr);
  271. assert(new);
  272. struct shim_vma * e = &early_vmas[i];
  273. struct shim_vma * prev = LISTP_PREV_ENTRY(e, &vma_list, list);
  274. debug("Converting early VMA [%p] %p-%p\n", e, e->start, e->end);
  275. memcpy(new, e, sizeof(*e));
  276. INIT_LIST_HEAD(new, list);
  277. __remove_vma(e, prev);
  278. __insert_vma(new, prev);
  279. }
  280. /* replace all reserved VMAs */
  281. reserved_vmas[i] = get_mem_obj_from_mgr(vma_mgr);
  282. assert(reserved_vmas[i]);
  283. }
  284. create_lock(&vma_list_lock);
  285. current_heap_top = PAL_CB(user_address.end);
  286. #if ENABLE_ASLR == 1
  287. /*
  288. * Randomize the heap top in top 5/6 of the user address space.
  289. * This is a simplified version of the mmap_base() logic in the Linux
  290. * kernel: https://elixir.bootlin.com/linux/v4.8/ident/mmap_base
  291. */
  292. uint64_t addr_rand_size =
  293. (PAL_CB(user_address.end) - PAL_CB(user_address.start)) * 5 / 6;
  294. uint64_t rand;
  295. ret = DkRandomBitsRead(&rand, sizeof(rand));
  296. if (ret < 0)
  297. return -convert_pal_errno(-ret);
  298. current_heap_top -= ALIGN_DOWN(rand % addr_rand_size);
  299. #endif
  300. debug("heap top adjusted to %p\n", current_heap_top);
  301. return 0;
  302. }
  303. static inline struct shim_vma * __get_new_vma (void)
  304. {
  305. struct shim_vma * tmp;
  306. if (vma_mgr) {
  307. tmp = get_mem_obj_from_mgr(vma_mgr);
  308. if (tmp)
  309. goto out;
  310. }
  311. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  312. if (reserved_vmas[i]) {
  313. tmp = reserved_vmas[i];
  314. reserved_vmas[i] = NULL;
  315. goto out;
  316. }
  317. /* Should never reach here; if this happens, increase RESERVED_VMAS */
  318. debug("failed to allocate new vma\n");
  319. BUG();
  320. return NULL;
  321. out:
  322. memset(tmp, 0, sizeof(*tmp));
  323. INIT_LIST_HEAD(tmp, list);
  324. return tmp;
  325. }
  326. static inline void __restore_reserved_vmas (void)
  327. {
  328. bool nothing_reserved;
  329. do {
  330. nothing_reserved = true;
  331. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  332. if (!reserved_vmas[i]) {
  333. struct shim_vma * new =
  334. get_mem_obj_from_mgr_enlarge(vma_mgr,
  335. size_align_up(VMA_MGR_ALLOC));
  336. /* this allocation must succeed */
  337. assert(new);
  338. reserved_vmas[i] = new;
  339. nothing_reserved = false;
  340. }
  341. } while (!nothing_reserved);
  342. }
  343. static inline void __drop_vma (struct shim_vma * vma)
  344. {
  345. if (vma->file)
  346. put_handle(vma->file);
  347. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  348. if (!reserved_vmas[i]) {
  349. reserved_vmas[i] = vma;
  350. return;
  351. }
  352. free_mem_obj_to_mgr(vma_mgr, vma);
  353. }
  354. static inline void
  355. __assert_vma_flags (const struct shim_vma * vma, int flags)
  356. {
  357. if (!(vma->flags & VMA_UNMAPPED)
  358. && VMA_TYPE(vma->flags) != VMA_TYPE(flags)) {
  359. debug("Check vma flag failure: vma flags %x, checked flags %x\n",
  360. vma->flags, flags);
  361. BUG();
  362. }
  363. }
  364. static inline void
  365. __set_vma_comment (struct shim_vma * vma, const char * comment)
  366. {
  367. if (!comment) {
  368. vma->comment[0] = 0;
  369. return;
  370. }
  371. uint64_t len = strlen(comment);
  372. if (len > VMA_COMMENT_LEN - 1)
  373. len = VMA_COMMENT_LEN - 1;
  374. memcpy(vma->comment, comment, len);
  375. vma->comment[len] = 0;
  376. }
  377. /*
  378. * Add bookkeeping for mmap(). "prev" must point to the the immediately
  379. * precedent vma of the address to map, or be NULL if no vma is lower than
  380. * the address. If the bookkeeping area overlaps with some existing vmas,
  381. * we must check whether the caller (from user, internal code, or checkpointing
  382. * procedure) is allowed to overwrite the existing vmas.
  383. *
  384. * Bookkeeping convention (must follow):
  385. * Create the bookkeeping BEFORE any allocation PAL calls
  386. * (DkVirtualMemoryAlloc() or DkStreamMap()).
  387. */
  388. static int __bkeep_mmap (struct shim_vma * prev,
  389. void * start, void * end, int prot, int flags,
  390. struct shim_handle * file, uint64_t offset,
  391. const char * comment)
  392. {
  393. int ret = 0;
  394. struct shim_vma * new = __get_new_vma();
  395. /* First, remove any overlapping VMAs */
  396. ret = __bkeep_munmap(&prev, start, end, flags);
  397. if (ret < 0) {
  398. __drop_vma(new);
  399. return ret;
  400. }
  401. /* Inserting the new VMA */
  402. new->start = start;
  403. new->end = end;
  404. new->prot = prot;
  405. new->flags = flags|((file && (prot & PROT_WRITE)) ? VMA_TAINTED : 0);
  406. new->file = file;
  407. if (new->file)
  408. get_handle(new->file);
  409. new->offset = offset;
  410. __set_vma_comment(new, comment);
  411. __insert_vma(new, prev);
  412. return 0;
  413. }
  414. int bkeep_mmap (void * addr, uint64_t length, int prot, int flags,
  415. struct shim_handle * file, uint64_t offset,
  416. const char * comment)
  417. {
  418. if (!addr || !length)
  419. return -EINVAL;
  420. if (comment && !comment[0])
  421. comment = NULL;
  422. debug("bkeep_mmap: %p-%p\n", addr, addr + length);
  423. lock(&vma_list_lock);
  424. struct shim_vma * prev = NULL;
  425. __lookup_vma(addr, &prev);
  426. int ret = __bkeep_mmap(prev, addr, addr + length, prot, flags, file, offset,
  427. comment);
  428. assert_vma_list();
  429. __restore_reserved_vmas();
  430. unlock(&vma_list_lock);
  431. return ret;
  432. }
  433. /*
  434. * __shrink_vma() removes the area in a VMA that overlaps with [start, end).
  435. * The function deals with three cases:
  436. * (1) [start, end) overlaps with the beginning of the VMA.
  437. * (2) [start, end) overlaps with the ending of the VMA.
  438. * (3) [start, end) overlaps with the middle of the VMA. In this case, the VMA
  439. * is splitted into two. The new VMA is stored in 'tailptr'.
  440. * In either of these cases, "vma" is the only one changed among vma_list.
  441. */
  442. static inline void __shrink_vma (struct shim_vma * vma, void * start, void * end,
  443. struct shim_vma ** tailptr)
  444. {
  445. /*
  446. * Dealing with the head: if the starting address of "vma" is in
  447. * [start, end), move the starting address.
  448. */
  449. if (test_vma_startin(vma, start, end)) {
  450. if (end < vma->end) {
  451. if (vma->file) /* must adjust offset */
  452. vma->offset += end - vma->start;
  453. vma->start = end;
  454. } else {
  455. if (vma->file) /* must adjust offset */
  456. vma->offset += vma->end - vma->start;
  457. vma->start = vma->end;
  458. }
  459. goto finish;
  460. }
  461. /*
  462. * Dealing with the tail: if the ending address of "vma" is in
  463. * [start, end), move the ending address.
  464. */
  465. if (test_vma_endin(vma, start, end)) {
  466. if (start > vma->start) {
  467. vma->end = start;
  468. } else {
  469. vma->end = vma->start;
  470. }
  471. /* offset is not affected */
  472. goto finish;
  473. }
  474. /*
  475. * If [start, end) is inside the range of "vma", divide up
  476. * the VMA. A new VMA is created to represent the remaining tail.
  477. */
  478. if (test_vma_contain(vma, start, end)) {
  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. goto finish;
  499. }
  500. /* Never reach here */
  501. BUG();
  502. finish:
  503. assert(!test_vma_overlap(vma, start, end));
  504. assert(vma->start < vma->end);
  505. }
  506. /*
  507. * Update bookkeeping for munmap(). "*pprev" must point to the immediately
  508. * precedent vma of the address to unmap, or be NULL if no vma is lower than
  509. * the address. If the bookkeeping area overlaps with some existing vmas,
  510. * we must check whether the caller (from user, internal code, or checkpointing
  511. * procedure) is allowed to overwrite the existing vmas. "pprev" can be
  512. * updated if a new vma lower than the unmapping address is added.
  513. *
  514. * Bookkeeping convention (must follow):
  515. * Make deallocation PAL calls (DkVirtualMemoryFree() or DkStreamUnmap())
  516. * BEFORE updating the bookkeeping.
  517. */
  518. static int __bkeep_munmap (struct shim_vma ** pprev,
  519. void * start, void * end, int flags)
  520. {
  521. struct shim_vma * prev = *pprev;
  522. struct shim_vma * cur, * next;
  523. if (!prev) {
  524. cur = LISTP_FIRST_ENTRY(&vma_list, struct shim_vma, list);
  525. if (!cur)
  526. return 0;
  527. } else {
  528. cur = LISTP_NEXT_ENTRY(prev, &vma_list, list);
  529. }
  530. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  531. while (cur) {
  532. struct shim_vma * tail = NULL;
  533. /* Stop unmapping if "cur" no longer overlaps with [start, end) */
  534. if (!test_vma_overlap(cur, start, end))
  535. break;
  536. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  537. return -EACCES;
  538. /* If [start, end) contains the VMA, just drop the VMA. */
  539. if (start <= cur->start && cur->end <= end) {
  540. __remove_vma(cur, prev);
  541. __drop_vma(cur);
  542. goto cont;
  543. }
  544. __shrink_vma(cur, start, end, &tail);
  545. if (cur->end <= start) {
  546. prev = cur;
  547. if (tail) {
  548. __insert_vma(tail, cur); /* insert "tail" after "cur" */
  549. cur = tail; /* "tail" is the new "cur" */
  550. break;
  551. }
  552. } else if (cur->start >= end) {
  553. /* __shrink_vma() only creates a new VMA when the beginning of the
  554. * original VMA is preserved. */
  555. assert(!tail);
  556. break;
  557. } else {
  558. /* __shrink_vma() should never allow this case. */
  559. BUG();
  560. }
  561. cont:
  562. cur = next;
  563. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  564. }
  565. if (prev)
  566. assert(cur == LISTP_NEXT_ENTRY(prev, &vma_list, list));
  567. else
  568. assert(cur == LISTP_FIRST_ENTRY(&vma_list, struct shim_vma, list));
  569. assert(!prev || prev->end <= start);
  570. assert(!cur || end <= cur->start);
  571. *pprev = prev;
  572. return 0;
  573. }
  574. int bkeep_munmap (void * addr, uint64_t length, int flags)
  575. {
  576. if (!length)
  577. return -EINVAL;
  578. debug("bkeep_munmap: %p-%p\n", addr, addr + length);
  579. lock(&vma_list_lock);
  580. struct shim_vma * prev = NULL;
  581. __lookup_vma(addr, &prev);
  582. int ret = __bkeep_munmap(&prev, addr, addr + length, flags);
  583. assert_vma_list();
  584. __restore_reserved_vmas();
  585. unlock(&vma_list_lock);
  586. return ret;
  587. }
  588. /*
  589. * Update bookkeeping for mprotect(). "prev" must point to the immediately
  590. * precedent vma of the address to protect, or be NULL if no vma is lower than
  591. * the address. If the bookkeeping area overlaps with some existing vmas,
  592. * we must check whether the caller (from user, internal code, or checkpointing
  593. * procedure) is allowed to overwrite the existing vmas.
  594. *
  595. * Bookkeeping convention (must follow):
  596. * Update the bookkeeping BEFORE calling DkVirtualMemoryProtect().
  597. */
  598. static int __bkeep_mprotect (struct shim_vma * prev,
  599. void * start, void * end, int prot, int flags)
  600. {
  601. struct shim_vma * cur, * next;
  602. if (!prev) {
  603. cur = LISTP_FIRST_ENTRY(&vma_list, struct shim_vma, list);
  604. if (!cur)
  605. return 0;
  606. } else {
  607. cur = LISTP_NEXT_ENTRY(prev, &vma_list, list);
  608. }
  609. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  610. while (cur) {
  611. struct shim_vma * new, * tail = NULL;
  612. /* Stop protecting if "cur" no longer overlaps with [start, end) */
  613. if (!test_vma_overlap(cur, start, end))
  614. break;
  615. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  616. /* For now, just shout loudly. */
  617. return -EACCES;
  618. /* If protection doesn't change anything, move on to the next */
  619. if (cur->prot == prot)
  620. goto cont;
  621. /* If [start, end) contains the VMA, just update its protection. */
  622. if (start <= cur->start && cur->end <= end) {
  623. cur->prot = prot;
  624. goto cont;
  625. }
  626. /* Create a new VMA for the protected area */
  627. new = __get_new_vma();
  628. new->start = cur->start > start ? cur->start : start;
  629. new->end = cur->end < end ? cur->end : end;
  630. new->prot = prot;
  631. new->flags = cur->flags;
  632. new->file = cur->file;
  633. if (new->file) {
  634. get_handle(new->file);
  635. new->offset = cur->offset + (new->start - cur->start);
  636. } else {
  637. new->offset = 0;
  638. }
  639. memcpy(new->comment, cur->comment, VMA_COMMENT_LEN);
  640. /* Like unmapping, shrink (and potentially split) the VMA first. */
  641. __shrink_vma(cur, start, end, &tail);
  642. if (cur->end <= start) {
  643. prev = cur;
  644. if (tail) {
  645. __insert_vma(tail, cur); /* insert "tail" after "cur" */
  646. cur = tail; /* "tail" is the new "cur" */
  647. /* "next" is the same */
  648. }
  649. } else if (cur->start >= end) {
  650. /* __shrink_vma() only creates a new VMA when the beginning of the
  651. * original VMA is preserved. */
  652. assert(!tail);
  653. } else {
  654. /* __shrink_vma() should never allow this case. */
  655. BUG();
  656. }
  657. /* Now insert the new protected vma between prev and cur */
  658. __insert_vma(new, prev);
  659. assert(!prev || prev->end <= new->end);
  660. assert(new->start < new->end);
  661. cont:
  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, uint64_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. uint64_t length, int prot, int flags,
  690. struct shim_handle * file,
  691. uint64_t offset, const char * comment)
  692. {
  693. assert(top_addr > bottom_addr);
  694. if (!length || length > top_addr - 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 <= end - 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, uint64_t length,
  722. int prot, int flags, struct shim_handle * file,
  723. uint64_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 (uint64_t length, int prot, int flags,
  734. struct shim_handle * file,
  735. uint64_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. goto again;
  758. /* Try first time */
  759. addr = __bkeep_unmapped(top_addr, bottom_addr,
  760. length, prot, flags,
  761. file, offset, comment);
  762. assert_vma_list();
  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. goto out;
  773. }
  774. again:
  775. if (top_addr >= heap_max)
  776. goto out;
  777. /* Try to allocate above the current heap top */
  778. addr = __bkeep_unmapped(heap_max, bottom_addr,
  779. length, prot, flags,
  780. file, offset, comment);
  781. assert_vma_list();
  782. out:
  783. __restore_reserved_vmas();
  784. unlock(&vma_list_lock);
  785. #ifdef MAP_32BIT
  786. assert(!(flags & MAP_32BIT) || !addr || addr + length <= ADDR_32BIT);
  787. #endif
  788. return addr;
  789. }
  790. static inline void
  791. __dump_vma (struct shim_vma_val * val, const struct shim_vma * vma)
  792. {
  793. val->addr = vma->start;
  794. val->length = vma->end - vma->start;
  795. val->prot = vma->prot;
  796. val->flags = vma->flags;
  797. val->file = vma->file;
  798. if (val->file)
  799. get_handle(val->file);
  800. val->offset = vma->offset;
  801. memcpy(val->comment, vma->comment, VMA_COMMENT_LEN);
  802. }
  803. int lookup_vma (void * addr, struct shim_vma_val * res)
  804. {
  805. lock(&vma_list_lock);
  806. struct shim_vma * vma = __lookup_vma(addr, NULL);
  807. if (!vma) {
  808. unlock(&vma_list_lock);
  809. return -ENOENT;
  810. }
  811. if (res)
  812. __dump_vma(res, vma);
  813. unlock(&vma_list_lock);
  814. return 0;
  815. }
  816. int lookup_overlap_vma (void * addr, uint64_t length,
  817. struct shim_vma_val * res)
  818. {
  819. struct shim_vma * tmp, * vma = NULL;
  820. lock(&vma_list_lock);
  821. LISTP_FOR_EACH_ENTRY(tmp, &vma_list, list)
  822. if (test_vma_overlap (tmp, addr, addr + length)) {
  823. vma = tmp;
  824. break;
  825. }
  826. if (!vma) {
  827. unlock(&vma_list_lock);
  828. return -ENOENT;
  829. }
  830. if (res)
  831. __dump_vma(res, vma);
  832. unlock(&vma_list_lock);
  833. return 0;
  834. }
  835. bool is_in_one_vma (void * addr, size_t length)
  836. {
  837. struct shim_vma* vma;
  838. lock(&vma_list_lock);
  839. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list)
  840. if (test_vma_contain(vma, addr, addr + length)) {
  841. unlock(&vma_list_lock);
  842. return true;
  843. }
  844. unlock(&vma_list_lock);
  845. return false;
  846. }
  847. int dump_all_vmas (struct shim_vma_val * vmas, size_t max_count)
  848. {
  849. struct shim_vma_val * val = vmas;
  850. struct shim_vma * vma;
  851. int cnt = 0;
  852. lock(&vma_list_lock);
  853. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  854. if (VMA_TYPE(vma->flags))
  855. continue;
  856. if (vma->flags & VMA_UNMAPPED)
  857. continue;
  858. if (cnt == max_count) {
  859. cnt = -EOVERFLOW;
  860. for (int i = 0 ; i < max_count ; i++)
  861. if (vmas[i].file)
  862. put_handle(vmas[i].file);
  863. break;
  864. }
  865. __dump_vma(val, vma);
  866. cnt++;
  867. val++;
  868. }
  869. unlock(&vma_list_lock);
  870. return cnt;
  871. }
  872. BEGIN_CP_FUNC(vma)
  873. {
  874. assert(size == sizeof(struct shim_vma_val));
  875. struct shim_vma_val * vma = (struct shim_vma_val *) obj;
  876. struct shim_vma_val * new_vma = NULL;
  877. PAL_FLG pal_prot = PAL_PROT(vma->prot, 0);
  878. ptr_t off = GET_FROM_CP_MAP(obj);
  879. if (!off) {
  880. off = ADD_CP_OFFSET(sizeof(*vma));
  881. ADD_TO_CP_MAP(obj, off);
  882. new_vma = (struct shim_vma_val *) (base + off);
  883. memcpy(new_vma, vma, sizeof(*vma));
  884. if (vma->file)
  885. DO_CP(handle, vma->file, &new_vma->file);
  886. void * need_mapped = vma->addr;
  887. #if MIGRATE_MORE_GIPC == 1
  888. if (store->use_gipc ?
  889. !NEED_MIGRATE_MEMORY_IF_GIPC(vma) :
  890. !NEED_MIGRATE_MEMORY(vma))
  891. #else
  892. if (!NEED_MIGRATE_MEMORY(vma))
  893. #endif
  894. goto no_mem;
  895. void * send_addr = vma->addr;
  896. uint64_t send_size = vma->length;
  897. if (vma->file) {
  898. /*
  899. * Chia-Che 8/13/2017:
  900. * A fix for cloning a private VMA which maps a file to a process.
  901. *
  902. * (1) Application can access any page backed by the file, wholly
  903. * or partially.
  904. *
  905. * (2) Access beyond the last file-backed page will cause SIGBUS.
  906. * For reducing fork latency, the following code truncates the
  907. * memory size for migrating a process. The memory size is
  908. * truncated to the file size, round up to pages.
  909. *
  910. * (3) Data in the last file-backed page is valid before or after
  911. * forking. Has to be included in process migration.
  912. */
  913. int64_t file_len = get_file_size(vma->file);
  914. if (file_len >= 0 &&
  915. vma->offset + vma->length > file_len) {
  916. send_size = file_len > vma->offset ?
  917. file_len - vma->offset : 0;
  918. send_size = ALIGN_UP(send_size);
  919. }
  920. }
  921. if (!send_size)
  922. goto no_mem;
  923. #if HASH_GIPC == 1
  924. if (!(pal_prot & PAL_PROT_READ)) {
  925. #else
  926. if (!store->use_gipc && !(pal_prot & PAL_PROT_READ)) {
  927. #endif
  928. /* Make the area readable */
  929. DkVirtualMemoryProtect(send_addr, send_size,
  930. pal_prot|PAL_PROT_READ);
  931. }
  932. if (store->use_gipc) {
  933. struct shim_gipc_entry * gipc;
  934. DO_CP_SIZE(gipc, send_addr, send_size, &gipc);
  935. gipc->mem.prot = pal_prot;
  936. } else {
  937. struct shim_mem_entry * mem;
  938. DO_CP_SIZE(memory, send_addr, send_size, &mem);
  939. mem->prot = pal_prot;
  940. }
  941. need_mapped = vma->addr + vma->length;
  942. #if HASH_GIPC == 1
  943. if (store->use_gipc && !(pal_prot & PAL_PROT_READ))
  944. DkVirtualMemoryProtect(send_addr, send_size, pal_prot);
  945. #endif
  946. no_mem:
  947. ADD_CP_FUNC_ENTRY(off);
  948. ADD_CP_ENTRY(ADDR, need_mapped);
  949. } else {
  950. new_vma = (struct shim_vma_val *) (base + off);
  951. }
  952. if (objp)
  953. *objp = (void *) new_vma;
  954. }
  955. END_CP_FUNC(vma)
  956. DEFINE_PROFILE_CATAGORY(inside_rs_vma, resume_func);
  957. DEFINE_PROFILE_INTERVAL(vma_add_bookkeep, inside_rs_vma);
  958. DEFINE_PROFILE_INTERVAL(vma_map_file, inside_rs_vma);
  959. DEFINE_PROFILE_INTERVAL(vma_map_anonymous, inside_rs_vma);
  960. BEGIN_RS_FUNC(vma)
  961. {
  962. struct shim_vma_val * vma = (void *) (base + GET_CP_FUNC_ENTRY());
  963. void * need_mapped = (void *) GET_CP_ENTRY(ADDR);
  964. BEGIN_PROFILE_INTERVAL();
  965. CP_REBASE(vma->file);
  966. int ret = bkeep_mmap(vma->addr, vma->length, vma->prot, vma->flags,
  967. vma->file, vma->offset, vma->comment);
  968. if (ret < 0)
  969. return ret;
  970. SAVE_PROFILE_INTERVAL(vma_add_bookkeep);
  971. DEBUG_RS("vma: %p-%p flags %x prot 0x%08x\n",
  972. vma->addr, vma->addr + vma->length, vma->flags, vma->prot);
  973. if (!(vma->flags & VMA_UNMAPPED)) {
  974. if (vma->file) {
  975. struct shim_mount * fs = vma->file->fs;
  976. get_handle(vma->file);
  977. if (need_mapped < vma->addr + vma->length) {
  978. /* first try, use hstat to force it resumes pal handle */
  979. assert(vma->file->fs && vma->file->fs->fs_ops &&
  980. vma->file->fs->fs_ops->mmap);
  981. void * addr = need_mapped;
  982. int ret = fs->fs_ops->mmap(vma->file, &addr,
  983. vma->addr + vma->length -
  984. need_mapped,
  985. vma->prot,
  986. vma->flags,
  987. vma->offset +
  988. (need_mapped - vma->addr));
  989. if (ret < 0)
  990. return ret;
  991. if (!addr)
  992. return -ENOMEM;
  993. if (addr != need_mapped)
  994. return -EACCES;
  995. need_mapped += vma->length;
  996. SAVE_PROFILE_INTERVAL(vma_map_file);
  997. }
  998. }
  999. if (need_mapped < vma->addr + vma->length) {
  1000. int pal_alloc_type = 0;
  1001. int pal_prot = vma->prot;
  1002. if (DkVirtualMemoryAlloc(need_mapped,
  1003. vma->addr + vma->length - need_mapped,
  1004. pal_alloc_type, pal_prot)) {
  1005. need_mapped += vma->length;
  1006. SAVE_PROFILE_INTERVAL(vma_map_anonymous);
  1007. }
  1008. }
  1009. if (need_mapped < vma->addr + vma->length)
  1010. SYS_PRINTF("vma %p-%p cannot be allocated!\n", need_mapped,
  1011. vma->addr + vma->length);
  1012. }
  1013. if (vma->file)
  1014. get_handle(vma->file);
  1015. if (vma->file)
  1016. DEBUG_RS("%p-%p,size=%ld,prot=%08x,flags=%08x,off=%ld,path=%s,uri=%s",
  1017. vma->addr, vma->addr + vma->length, vma->length,
  1018. vma->prot, vma->flags, vma->offset,
  1019. qstrgetstr(&vma->file->path), qstrgetstr(&vma->file->uri));
  1020. else
  1021. DEBUG_RS("%p-%p,size=%ld,prot=%08x,flags=%08x,off=%ld",
  1022. vma->addr, vma->addr + vma->length, vma->length,
  1023. vma->prot, vma->flags, vma->offset);
  1024. }
  1025. END_RS_FUNC(vma)
  1026. BEGIN_CP_FUNC(all_vmas)
  1027. {
  1028. size_t count = DEFAULT_VMA_COUNT;
  1029. struct shim_vma_val * vmas = malloc(sizeof(*vmas) * count);
  1030. int ret;
  1031. if (!vmas)
  1032. return -ENOMEM;
  1033. retry_dump_vmas:
  1034. ret = dump_all_vmas(vmas, count);
  1035. if (ret == -EOVERFLOW) {
  1036. struct shim_vma_val * new_vmas
  1037. = malloc(sizeof(*new_vmas) * count * 2);
  1038. if (!new_vmas) {
  1039. free(vmas);
  1040. return -ENOMEM;
  1041. }
  1042. free(vmas);
  1043. vmas = new_vmas;
  1044. count *= 2;
  1045. goto retry_dump_vmas;
  1046. }
  1047. if (ret < 0)
  1048. return ret;
  1049. count = ret;
  1050. for (struct shim_vma_val * vma = &vmas[count - 1] ; vma >= vmas ; vma--)
  1051. DO_CP(vma, vma, NULL);
  1052. free_vma_val_array(vmas, count);
  1053. }
  1054. END_CP_FUNC_NO_RS(all_vmas)
  1055. void debug_print_vma_list (void)
  1056. {
  1057. SYS_PRINTF("vma bookkeeping:\n");
  1058. struct shim_vma * vma;
  1059. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  1060. const char * type = "", * name = "";
  1061. if (vma->file) {
  1062. if (!qstrempty(&vma->file->path)) {
  1063. type = " path=";
  1064. name = qstrgetstr(&vma->file->path);
  1065. } else if (!qstrempty(&vma->file->uri)) {
  1066. type = " uri=";
  1067. name = qstrgetstr(&vma->file->uri);
  1068. }
  1069. }
  1070. SYS_PRINTF("[%p-%p] prot=%08x flags=%08x%s%s offset=%ld%s%s%s%s\n",
  1071. vma->start, vma->end,
  1072. vma->prot,
  1073. vma->flags & ~(VMA_INTERNAL|VMA_UNMAPPED|VMA_TAINTED|VMA_CP),
  1074. type, name,
  1075. vma->offset,
  1076. vma->flags & VMA_INTERNAL ? " (internal)" : "",
  1077. vma->flags & VMA_UNMAPPED ? " (unmapped)" : "",
  1078. vma->comment[0] ? " comment=" : "",
  1079. vma->comment[0] ? vma->comment : "");
  1080. }
  1081. }