shim_vma.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. uint64_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. uint64_t length, int prot, int flags,
  53. struct shim_handle * file,
  54. uint64_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. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  174. if (addr < vma->start)
  175. goto none;
  176. if (test_vma_contain(vma, addr, addr + 1))
  177. goto out;
  178. assert(vma->end > vma->start);
  179. assert(!prev || prev->end <= vma->start);
  180. prev = vma;
  181. }
  182. none:
  183. vma = NULL;
  184. out:
  185. if (pprev) *pprev = prev;
  186. return vma;
  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, uint64_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. /* Initialize the allocator */
  262. if (!(vma_mgr = create_mem_mgr(init_align_up(VMA_MGR_ALLOC)))) {
  263. debug("failed creating the VMA allocator\n");
  264. return -ENOMEM;
  265. }
  266. for (int i = 0 ; i < RESERVED_VMAS ; i++) {
  267. if (!reserved_vmas[i]) {
  268. struct shim_vma * new = get_mem_obj_from_mgr(vma_mgr);
  269. assert(new);
  270. struct shim_vma * e = &early_vmas[i];
  271. struct shim_vma * prev = LISTP_PREV_ENTRY(e, &vma_list, list);
  272. debug("Converting early VMA [%p] %p-%p\n", e, e->start, e->end);
  273. memcpy(new, e, sizeof(*e));
  274. INIT_LIST_HEAD(new, list);
  275. __remove_vma(e, prev);
  276. __insert_vma(new, prev);
  277. }
  278. /* replace all reserved VMAs */
  279. reserved_vmas[i] = get_mem_obj_from_mgr(vma_mgr);
  280. assert(reserved_vmas[i]);
  281. }
  282. create_lock(&vma_list_lock);
  283. current_heap_top = PAL_CB(user_address.end);
  284. #if ENABLE_ASLR == 1
  285. /*
  286. * Randomize the heap top in top 5/6 of the user address space.
  287. * This is a simplified version of the mmap_base() logic in the Linux
  288. * kernel: https://elixir.bootlin.com/linux/v4.8/ident/mmap_base
  289. */
  290. uint64_t addr_rand_size =
  291. (PAL_CB(user_address.end) - PAL_CB(user_address.start)) * 5 / 6;
  292. uint64_t rand;
  293. ret = DkRandomBitsRead(&rand, sizeof(rand));
  294. if (ret < 0)
  295. return -convert_pal_errno(-ret);
  296. current_heap_top -= ALIGN_DOWN(rand % addr_rand_size);
  297. #endif
  298. debug("heap top adjusted to %p\n", current_heap_top);
  299. return 0;
  300. }
  301. static inline struct shim_vma * __get_new_vma (void)
  302. {
  303. struct shim_vma * tmp;
  304. if (vma_mgr) {
  305. tmp = get_mem_obj_from_mgr(vma_mgr);
  306. if (tmp)
  307. goto out;
  308. }
  309. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  310. if (reserved_vmas[i]) {
  311. tmp = reserved_vmas[i];
  312. reserved_vmas[i] = NULL;
  313. goto out;
  314. }
  315. /* Should never reach here; if this happens, increase RESERVED_VMAS */
  316. debug("failed to allocate new vma\n");
  317. BUG();
  318. return NULL;
  319. out:
  320. memset(tmp, 0, sizeof(*tmp));
  321. INIT_LIST_HEAD(tmp, list);
  322. return tmp;
  323. }
  324. static inline void __restore_reserved_vmas (void)
  325. {
  326. bool nothing_reserved;
  327. do {
  328. nothing_reserved = true;
  329. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  330. if (!reserved_vmas[i]) {
  331. struct shim_vma * new =
  332. get_mem_obj_from_mgr_enlarge(vma_mgr,
  333. size_align_up(VMA_MGR_ALLOC));
  334. /* this allocation must succeed */
  335. assert(new);
  336. reserved_vmas[i] = new;
  337. nothing_reserved = false;
  338. }
  339. } while (!nothing_reserved);
  340. }
  341. static inline void __drop_vma (struct shim_vma * vma)
  342. {
  343. if (vma->file)
  344. put_handle(vma->file);
  345. for (int i = 0 ; i < RESERVED_VMAS ; i++)
  346. if (!reserved_vmas[i]) {
  347. reserved_vmas[i] = vma;
  348. return;
  349. }
  350. free_mem_obj_to_mgr(vma_mgr, vma);
  351. }
  352. static inline void
  353. __assert_vma_flags (const struct shim_vma * vma, int flags)
  354. {
  355. if (!(vma->flags & VMA_UNMAPPED)
  356. && VMA_TYPE(vma->flags) != VMA_TYPE(flags)) {
  357. debug("Check vma flag failure: vma flags %x, checked flags %x\n",
  358. vma->flags, flags);
  359. BUG();
  360. }
  361. }
  362. static inline void
  363. __set_vma_comment (struct shim_vma * vma, const char * comment)
  364. {
  365. if (!comment) {
  366. vma->comment[0] = 0;
  367. return;
  368. }
  369. uint64_t len = strlen(comment);
  370. if (len > VMA_COMMENT_LEN - 1)
  371. len = VMA_COMMENT_LEN - 1;
  372. memcpy(vma->comment, comment, len);
  373. vma->comment[len] = 0;
  374. }
  375. /*
  376. * Add bookkeeping for mmap(). "prev" must point to the the immediately
  377. * precedent vma of the address to map, or be NULL if no vma is lower than
  378. * the address. If the bookkeeping area overlaps with some existing vmas,
  379. * we must check whether the caller (from user, internal code, or checkpointing
  380. * procedure) is allowed to overwrite the existing vmas.
  381. *
  382. * Bookkeeping convention (must follow):
  383. * Create the bookkeeping BEFORE any allocation PAL calls
  384. * (DkVirtualMemoryAlloc() or DkStreamMap()).
  385. */
  386. static int __bkeep_mmap (struct shim_vma * prev,
  387. void * start, void * end, int prot, int flags,
  388. struct shim_handle * file, uint64_t offset,
  389. const char * comment)
  390. {
  391. int ret = 0;
  392. struct shim_vma * new = __get_new_vma();
  393. /* First, remove any overlapping VMAs */
  394. ret = __bkeep_munmap(&prev, start, end, flags);
  395. if (ret < 0) {
  396. __drop_vma(new);
  397. return ret;
  398. }
  399. /* Inserting the new VMA */
  400. new->start = start;
  401. new->end = end;
  402. new->prot = prot;
  403. new->flags = flags|((file && (prot & PROT_WRITE)) ? VMA_TAINTED : 0);
  404. new->file = file;
  405. if (new->file)
  406. get_handle(new->file);
  407. new->offset = offset;
  408. __set_vma_comment(new, comment);
  409. __insert_vma(new, prev);
  410. return 0;
  411. }
  412. int bkeep_mmap (void * addr, uint64_t length, int prot, int flags,
  413. struct shim_handle * file, uint64_t offset,
  414. const char * comment)
  415. {
  416. if (!addr || !length)
  417. return -EINVAL;
  418. if (comment && !comment[0])
  419. comment = NULL;
  420. debug("bkeep_mmap: %p-%p\n", addr, addr + length);
  421. lock(&vma_list_lock);
  422. struct shim_vma * prev = NULL;
  423. __lookup_vma(addr, &prev);
  424. int ret = __bkeep_mmap(prev, addr, addr + length, prot, flags, file, offset,
  425. comment);
  426. assert_vma_list();
  427. __restore_reserved_vmas();
  428. unlock(&vma_list_lock);
  429. return ret;
  430. }
  431. /*
  432. * __shrink_vma() removes the area in a VMA that overlaps with [start, end).
  433. * The function deals with three cases:
  434. * (1) [start, end) overlaps with the beginning of the VMA.
  435. * (2) [start, end) overlaps with the ending of the VMA.
  436. * (3) [start, end) overlaps with the middle of the VMA. In this case, the VMA
  437. * is splitted into two. The new VMA is stored in 'tailptr'.
  438. * In either of these cases, "vma" is the only one changed among vma_list.
  439. */
  440. static inline void __shrink_vma (struct shim_vma * vma, void * start, void * end,
  441. struct shim_vma ** tailptr)
  442. {
  443. /*
  444. * Dealing with the head: if the starting address of "vma" is in
  445. * [start, end), move the starting address.
  446. */
  447. if (test_vma_startin(vma, start, end)) {
  448. if (end < vma->end) {
  449. if (vma->file) /* must adjust offset */
  450. vma->offset += end - vma->start;
  451. vma->start = end;
  452. } else {
  453. if (vma->file) /* must adjust offset */
  454. vma->offset += vma->end - vma->start;
  455. vma->start = vma->end;
  456. }
  457. goto finish;
  458. }
  459. /*
  460. * Dealing with the tail: if the ending address of "vma" is in
  461. * [start, end), move the ending address.
  462. */
  463. if (test_vma_endin(vma, start, end)) {
  464. if (start > vma->start) {
  465. vma->end = start;
  466. } else {
  467. vma->end = vma->start;
  468. }
  469. /* offset is not affected */
  470. goto finish;
  471. }
  472. /*
  473. * If [start, end) is inside the range of "vma", divide up
  474. * the VMA. A new VMA is created to represent the remaining tail.
  475. */
  476. if (test_vma_contain(vma, start, end)) {
  477. void * old_end = vma->end;
  478. vma->end = start;
  479. /* Remaining space after [start, end), creating a new VMA */
  480. if (old_end > end) {
  481. struct shim_vma * tail = __get_new_vma();
  482. tail->start = end;
  483. tail->end = old_end;
  484. tail->prot = vma->prot;
  485. tail->flags = vma->flags;
  486. tail->file = vma->file;
  487. if (tail->file) {
  488. get_handle(tail->file);
  489. tail->offset = vma->offset + (tail->start - vma->start);
  490. } else {
  491. tail->offset = 0;
  492. }
  493. memcpy(tail->comment, vma->comment, VMA_COMMENT_LEN);
  494. *tailptr = tail;
  495. }
  496. goto finish;
  497. }
  498. /* Never reach here */
  499. BUG();
  500. finish:
  501. assert(!test_vma_overlap(vma, start, end));
  502. assert(vma->start < vma->end);
  503. }
  504. /*
  505. * Update bookkeeping for munmap(). "*pprev" must point to the immediately
  506. * precedent vma of the address to unmap, or be NULL if no vma is lower than
  507. * the address. If the bookkeeping area overlaps with some existing vmas,
  508. * we must check whether the caller (from user, internal code, or checkpointing
  509. * procedure) is allowed to overwrite the existing vmas. "pprev" can be
  510. * updated if a new vma lower than the unmapping address is added.
  511. *
  512. * Bookkeeping convention (must follow):
  513. * Make deallocation PAL calls (DkVirtualMemoryFree() or DkStreamUnmap())
  514. * BEFORE updating the bookkeeping.
  515. */
  516. static int __bkeep_munmap (struct shim_vma ** pprev,
  517. void * start, void * end, int flags)
  518. {
  519. struct shim_vma * prev = *pprev;
  520. struct shim_vma * cur, * next;
  521. if (!prev) {
  522. cur = LISTP_FIRST_ENTRY(&vma_list, struct shim_vma, list);
  523. if (!cur)
  524. return 0;
  525. } else {
  526. cur = LISTP_NEXT_ENTRY(prev, &vma_list, list);
  527. }
  528. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  529. while (cur) {
  530. struct shim_vma * tail = NULL;
  531. /* Stop unmapping if "cur" no longer overlaps with [start, end) */
  532. if (!test_vma_overlap(cur, start, end))
  533. break;
  534. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  535. return -EACCES;
  536. /* If [start, end) contains the VMA, just drop the VMA. */
  537. if (start <= cur->start && cur->end <= end) {
  538. __remove_vma(cur, prev);
  539. __drop_vma(cur);
  540. goto cont;
  541. }
  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. cont:
  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, uint64_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. unlock(&vma_list_lock);
  584. return ret;
  585. }
  586. /*
  587. * Update bookkeeping for mprotect(). "prev" must point to the immediately
  588. * precedent vma of the address to protect, or be NULL if no vma is lower than
  589. * the address. If the bookkeeping area overlaps with some existing vmas,
  590. * we must check whether the caller (from user, internal code, or checkpointing
  591. * procedure) is allowed to overwrite the existing vmas.
  592. *
  593. * Bookkeeping convention (must follow):
  594. * Update the bookkeeping BEFORE calling DkVirtualMemoryProtect().
  595. */
  596. static int __bkeep_mprotect (struct shim_vma * prev,
  597. void * start, void * end, int prot, int flags)
  598. {
  599. struct shim_vma * cur, * next;
  600. if (!prev) {
  601. cur = LISTP_FIRST_ENTRY(&vma_list, struct shim_vma, list);
  602. if (!cur)
  603. return 0;
  604. } else {
  605. cur = LISTP_NEXT_ENTRY(prev, &vma_list, list);
  606. }
  607. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  608. while (cur) {
  609. struct shim_vma * new, * tail = NULL;
  610. /* Stop protecting if "cur" no longer overlaps with [start, end) */
  611. if (!test_vma_overlap(cur, start, end))
  612. break;
  613. if (VMA_TYPE(cur->flags) != VMA_TYPE(flags))
  614. /* For now, just shout loudly. */
  615. return -EACCES;
  616. /* If protection doesn't change anything, move on to the next */
  617. if (cur->prot == prot)
  618. goto cont;
  619. /* If [start, end) contains the VMA, just update its protection. */
  620. if (start <= cur->start && cur->end <= end) {
  621. cur->prot = prot;
  622. goto cont;
  623. }
  624. /* Create a new VMA for the protected area */
  625. new = __get_new_vma();
  626. new->start = cur->start > start ? cur->start : start;
  627. new->end = cur->end < end ? cur->end : end;
  628. new->prot = prot;
  629. new->flags = cur->flags;
  630. new->file = cur->file;
  631. if (new->file) {
  632. get_handle(new->file);
  633. new->offset = cur->offset + (new->start - cur->start);
  634. } else {
  635. new->offset = 0;
  636. }
  637. memcpy(new->comment, cur->comment, VMA_COMMENT_LEN);
  638. /* Like unmapping, shrink (and potentially split) the VMA first. */
  639. __shrink_vma(cur, start, end, &tail);
  640. if (cur->end <= start) {
  641. prev = cur;
  642. if (tail) {
  643. __insert_vma(tail, cur); /* insert "tail" after "cur" */
  644. cur = tail; /* "tail" is the new "cur" */
  645. /* "next" is the same */
  646. }
  647. } else if (cur->start >= end) {
  648. /* __shrink_vma() only creates a new VMA when the beginning of the
  649. * original VMA is preserved. */
  650. assert(!tail);
  651. } else {
  652. /* __shrink_vma() should never allow this case. */
  653. BUG();
  654. }
  655. /* Now insert the new protected vma between prev and cur */
  656. __insert_vma(new, prev);
  657. assert(!prev || prev->end <= new->end);
  658. assert(new->start < new->end);
  659. cont:
  660. prev = cur;
  661. cur = next;
  662. next = cur ? LISTP_NEXT_ENTRY(cur, &vma_list, list) : NULL;
  663. }
  664. return 0;
  665. }
  666. int bkeep_mprotect (void * addr, uint64_t length, int prot, int flags)
  667. {
  668. if (!addr || !length)
  669. return -EINVAL;
  670. debug("bkeep_mprotect: %p-%p\n", addr, addr + length);
  671. lock(&vma_list_lock);
  672. struct shim_vma * prev = NULL;
  673. __lookup_vma(addr, &prev);
  674. int ret = __bkeep_mprotect(prev, addr, addr + length, prot, flags);
  675. assert_vma_list();
  676. __restore_reserved_vmas();
  677. unlock(&vma_list_lock);
  678. return ret;
  679. }
  680. /*
  681. * Search for an unmapped area within [bottom, top) that is big enough
  682. * to allocate "length" bytes. The search approach is top-down.
  683. * If this function returns a non-NULL address, the corresponding VMA is
  684. * added to the VMA list.
  685. */
  686. static void * __bkeep_unmapped (void * top_addr, void * bottom_addr,
  687. uint64_t length, int prot, int flags,
  688. struct shim_handle * file,
  689. uint64_t offset, const char * comment)
  690. {
  691. assert(top_addr > bottom_addr);
  692. if (!length || length > top_addr - bottom_addr)
  693. return NULL;
  694. struct shim_vma * prev = NULL;
  695. struct shim_vma * cur = __lookup_vma(top_addr, &prev);
  696. while (true) {
  697. /* Set the range for searching */
  698. void * end = cur ? cur->start : top_addr;
  699. void * start =
  700. (prev && prev->end > bottom_addr) ? prev->end : bottom_addr;
  701. assert(start <= end);
  702. /* Check if there is enough space between prev and cur */
  703. if (length <= end - start) {
  704. /* create a new VMA at the top of the range */
  705. __bkeep_mmap(prev, end - length, end, prot, flags,
  706. file, offset, comment);
  707. assert_vma_list();
  708. debug("bkeep_unmapped: %p-%p%s%s\n", end - length, end,
  709. comment ? " => " : "", comment ? : "");
  710. return end - length;
  711. }
  712. if (!prev || prev->start <= bottom_addr)
  713. break;
  714. cur = prev;
  715. prev = LISTP_PREV_ENTRY(cur, &vma_list, list);
  716. }
  717. return NULL;
  718. }
  719. void * bkeep_unmapped (void * top_addr, void * bottom_addr, uint64_t length,
  720. int prot, int flags, struct shim_handle * file,
  721. uint64_t offset, const char * comment)
  722. {
  723. lock(&vma_list_lock);
  724. void * addr = __bkeep_unmapped(top_addr, bottom_addr, length, prot, flags,
  725. file, offset, comment);
  726. assert_vma_list();
  727. __restore_reserved_vmas();
  728. unlock(&vma_list_lock);
  729. return addr;
  730. }
  731. void * bkeep_unmapped_heap (uint64_t length, int prot, int flags,
  732. struct shim_handle * file,
  733. uint64_t offset, const char * comment)
  734. {
  735. lock(&vma_list_lock);
  736. void * bottom_addr = PAL_CB(user_address.start);
  737. void * top_addr = current_heap_top;
  738. void * heap_max = PAL_CB(user_address.end);
  739. void * addr = NULL;
  740. #ifdef MAP_32BIT
  741. /*
  742. * If MAP_32BIT is given in the flags, force the searching range to
  743. * be lower than 1ULL << 32.
  744. */
  745. #define ADDR_32BIT ((void*)(1ULL << 32))
  746. if (flags & MAP_32BIT) {
  747. /* Try the lower 4GB memory space */
  748. if (heap_max > ADDR_32BIT)
  749. heap_max = ADDR_32BIT;
  750. if (top_addr > heap_max)
  751. top_addr = heap_max;
  752. }
  753. #endif
  754. if (top_addr <= bottom_addr)
  755. goto again;
  756. /* Try first time */
  757. addr = __bkeep_unmapped(top_addr, bottom_addr,
  758. length, prot, flags,
  759. file, offset, comment);
  760. assert_vma_list();
  761. if (addr) {
  762. /*
  763. * we only update the current heap top when we get the
  764. * address from [bottom_addr, current_heap_top).
  765. */
  766. if (top_addr == current_heap_top) {
  767. debug("heap top adjusted to %p\n", addr);
  768. current_heap_top = addr;
  769. }
  770. goto out;
  771. }
  772. again:
  773. if (top_addr >= heap_max)
  774. goto out;
  775. /* Try to allocate above the current heap top */
  776. addr = __bkeep_unmapped(heap_max, bottom_addr,
  777. length, prot, flags,
  778. file, offset, comment);
  779. assert_vma_list();
  780. out:
  781. __restore_reserved_vmas();
  782. unlock(&vma_list_lock);
  783. #ifdef MAP_32BIT
  784. assert(!(flags & MAP_32BIT) || !addr || addr + length <= ADDR_32BIT);
  785. #endif
  786. return addr;
  787. }
  788. static inline void
  789. __dump_vma (struct shim_vma_val * val, const struct shim_vma * vma)
  790. {
  791. val->addr = vma->start;
  792. val->length = vma->end - vma->start;
  793. val->prot = vma->prot;
  794. val->flags = vma->flags;
  795. val->file = vma->file;
  796. if (val->file)
  797. get_handle(val->file);
  798. val->offset = vma->offset;
  799. memcpy(val->comment, vma->comment, VMA_COMMENT_LEN);
  800. }
  801. int lookup_vma (void * addr, struct shim_vma_val * res)
  802. {
  803. lock(&vma_list_lock);
  804. struct shim_vma * vma = __lookup_vma(addr, NULL);
  805. if (!vma) {
  806. unlock(&vma_list_lock);
  807. return -ENOENT;
  808. }
  809. if (res)
  810. __dump_vma(res, vma);
  811. unlock(&vma_list_lock);
  812. return 0;
  813. }
  814. int lookup_overlap_vma (void * addr, uint64_t length,
  815. struct shim_vma_val * res)
  816. {
  817. struct shim_vma * tmp, * vma = NULL;
  818. lock(&vma_list_lock);
  819. LISTP_FOR_EACH_ENTRY(tmp, &vma_list, list)
  820. if (test_vma_overlap (tmp, addr, addr + length)) {
  821. vma = tmp;
  822. break;
  823. }
  824. if (!vma) {
  825. unlock(&vma_list_lock);
  826. return -ENOENT;
  827. }
  828. if (res)
  829. __dump_vma(res, vma);
  830. unlock(&vma_list_lock);
  831. return 0;
  832. }
  833. bool is_in_one_vma (void * addr, size_t length)
  834. {
  835. struct shim_vma* vma;
  836. lock(&vma_list_lock);
  837. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list)
  838. if (test_vma_contain(vma, addr, addr + length)) {
  839. unlock(&vma_list_lock);
  840. return true;
  841. }
  842. unlock(&vma_list_lock);
  843. return false;
  844. }
  845. int dump_all_vmas (struct shim_vma_val * vmas, size_t max_count)
  846. {
  847. struct shim_vma_val * val = vmas;
  848. struct shim_vma * vma;
  849. int cnt = 0;
  850. lock(&vma_list_lock);
  851. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  852. if (VMA_TYPE(vma->flags))
  853. continue;
  854. if (vma->flags & VMA_UNMAPPED)
  855. continue;
  856. if (cnt == max_count) {
  857. cnt = -EOVERFLOW;
  858. for (int i = 0 ; i < max_count ; i++)
  859. if (vmas[i].file)
  860. put_handle(vmas[i].file);
  861. break;
  862. }
  863. __dump_vma(val, vma);
  864. cnt++;
  865. val++;
  866. }
  867. unlock(&vma_list_lock);
  868. return cnt;
  869. }
  870. BEGIN_CP_FUNC(vma)
  871. {
  872. assert(size == sizeof(struct shim_vma_val));
  873. struct shim_vma_val * vma = (struct shim_vma_val *) obj;
  874. struct shim_vma_val * new_vma = NULL;
  875. PAL_FLG pal_prot = PAL_PROT(vma->prot, 0);
  876. ptr_t off = GET_FROM_CP_MAP(obj);
  877. if (!off) {
  878. off = ADD_CP_OFFSET(sizeof(*vma));
  879. ADD_TO_CP_MAP(obj, off);
  880. new_vma = (struct shim_vma_val *) (base + off);
  881. memcpy(new_vma, vma, sizeof(*vma));
  882. if (vma->file)
  883. DO_CP(handle, vma->file, &new_vma->file);
  884. void * need_mapped = vma->addr;
  885. #if MIGRATE_MORE_GIPC == 1
  886. if (store->use_gipc ?
  887. !NEED_MIGRATE_MEMORY_IF_GIPC(vma) :
  888. !NEED_MIGRATE_MEMORY(vma))
  889. #else
  890. if (!NEED_MIGRATE_MEMORY(vma))
  891. #endif
  892. goto no_mem;
  893. void * send_addr = vma->addr;
  894. uint64_t send_size = vma->length;
  895. if (vma->file) {
  896. /*
  897. * Chia-Che 8/13/2017:
  898. * A fix for cloning a private VMA which maps a file to a process.
  899. *
  900. * (1) Application can access any page backed by the file, wholly
  901. * or partially.
  902. *
  903. * (2) Access beyond the last file-backed page will cause SIGBUS.
  904. * For reducing fork latency, the following code truncates the
  905. * memory size for migrating a process. The memory size is
  906. * truncated to the file size, round up to pages.
  907. *
  908. * (3) Data in the last file-backed page is valid before or after
  909. * forking. Has to be included in process migration.
  910. */
  911. int64_t file_len = get_file_size(vma->file);
  912. if (file_len >= 0 &&
  913. vma->offset + vma->length > file_len) {
  914. send_size = file_len > vma->offset ?
  915. file_len - vma->offset : 0;
  916. send_size = ALIGN_UP(send_size);
  917. }
  918. }
  919. if (!send_size)
  920. goto no_mem;
  921. #if HASH_GIPC == 1
  922. if (!(pal_prot & PAL_PROT_READ)) {
  923. #else
  924. if (!store->use_gipc && !(pal_prot & PAL_PROT_READ)) {
  925. #endif
  926. /* Make the area readable */
  927. DkVirtualMemoryProtect(send_addr, send_size,
  928. pal_prot|PAL_PROT_READ);
  929. }
  930. if (store->use_gipc) {
  931. struct shim_gipc_entry * gipc;
  932. DO_CP_SIZE(gipc, send_addr, send_size, &gipc);
  933. gipc->mem.prot = pal_prot;
  934. } else {
  935. struct shim_mem_entry * mem;
  936. DO_CP_SIZE(memory, send_addr, send_size, &mem);
  937. mem->prot = pal_prot;
  938. }
  939. need_mapped = vma->addr + vma->length;
  940. #if HASH_GIPC == 1
  941. if (store->use_gipc && !(pal_prot & PAL_PROT_READ))
  942. DkVirtualMemoryProtect(send_addr, send_size, pal_prot);
  943. #endif
  944. no_mem:
  945. ADD_CP_FUNC_ENTRY(off);
  946. ADD_CP_ENTRY(ADDR, need_mapped);
  947. } else {
  948. new_vma = (struct shim_vma_val *) (base + off);
  949. }
  950. if (objp)
  951. *objp = (void *) new_vma;
  952. }
  953. END_CP_FUNC(vma)
  954. DEFINE_PROFILE_CATEGORY(inside_rs_vma, resume_func);
  955. DEFINE_PROFILE_INTERVAL(vma_add_bookkeep, inside_rs_vma);
  956. DEFINE_PROFILE_INTERVAL(vma_map_file, inside_rs_vma);
  957. DEFINE_PROFILE_INTERVAL(vma_map_anonymous, inside_rs_vma);
  958. BEGIN_RS_FUNC(vma)
  959. {
  960. struct shim_vma_val * vma = (void *) (base + GET_CP_FUNC_ENTRY());
  961. void * need_mapped = (void *) GET_CP_ENTRY(ADDR);
  962. BEGIN_PROFILE_INTERVAL();
  963. CP_REBASE(vma->file);
  964. int ret = bkeep_mmap(vma->addr, vma->length, vma->prot, vma->flags,
  965. vma->file, vma->offset, vma->comment);
  966. if (ret < 0)
  967. return ret;
  968. SAVE_PROFILE_INTERVAL(vma_add_bookkeep);
  969. DEBUG_RS("vma: %p-%p flags %x prot 0x%08x\n",
  970. vma->addr, vma->addr + vma->length, vma->flags, vma->prot);
  971. if (!(vma->flags & VMA_UNMAPPED)) {
  972. if (vma->file) {
  973. struct shim_mount * fs = vma->file->fs;
  974. get_handle(vma->file);
  975. if (need_mapped < vma->addr + vma->length) {
  976. /* first try, use hstat to force it resumes pal handle */
  977. assert(vma->file->fs && vma->file->fs->fs_ops &&
  978. vma->file->fs->fs_ops->mmap);
  979. void * addr = need_mapped;
  980. int ret = fs->fs_ops->mmap(vma->file, &addr,
  981. vma->addr + vma->length -
  982. need_mapped,
  983. vma->prot,
  984. vma->flags,
  985. vma->offset +
  986. (need_mapped - vma->addr));
  987. if (ret < 0)
  988. return ret;
  989. if (!addr)
  990. return -ENOMEM;
  991. if (addr != need_mapped)
  992. return -EACCES;
  993. need_mapped += vma->length;
  994. SAVE_PROFILE_INTERVAL(vma_map_file);
  995. }
  996. }
  997. if (need_mapped < vma->addr + vma->length) {
  998. int pal_alloc_type = 0;
  999. int pal_prot = vma->prot;
  1000. if (DkVirtualMemoryAlloc(need_mapped,
  1001. vma->addr + vma->length - need_mapped,
  1002. pal_alloc_type, pal_prot)) {
  1003. need_mapped += vma->length;
  1004. SAVE_PROFILE_INTERVAL(vma_map_anonymous);
  1005. }
  1006. }
  1007. if (need_mapped < vma->addr + vma->length)
  1008. SYS_PRINTF("vma %p-%p cannot be allocated!\n", need_mapped,
  1009. vma->addr + vma->length);
  1010. }
  1011. if (vma->file)
  1012. get_handle(vma->file);
  1013. if (vma->file)
  1014. DEBUG_RS("%p-%p,size=%ld,prot=%08x,flags=%08x,off=%ld,path=%s,uri=%s",
  1015. vma->addr, vma->addr + vma->length, vma->length,
  1016. vma->prot, vma->flags, vma->offset,
  1017. qstrgetstr(&vma->file->path), qstrgetstr(&vma->file->uri));
  1018. else
  1019. DEBUG_RS("%p-%p,size=%ld,prot=%08x,flags=%08x,off=%ld",
  1020. vma->addr, vma->addr + vma->length, vma->length,
  1021. vma->prot, vma->flags, vma->offset);
  1022. }
  1023. END_RS_FUNC(vma)
  1024. BEGIN_CP_FUNC(all_vmas)
  1025. {
  1026. size_t count = DEFAULT_VMA_COUNT;
  1027. struct shim_vma_val * vmas = malloc(sizeof(*vmas) * count);
  1028. int ret;
  1029. if (!vmas)
  1030. return -ENOMEM;
  1031. retry_dump_vmas:
  1032. ret = dump_all_vmas(vmas, count);
  1033. if (ret == -EOVERFLOW) {
  1034. struct shim_vma_val * new_vmas
  1035. = malloc(sizeof(*new_vmas) * count * 2);
  1036. if (!new_vmas) {
  1037. free(vmas);
  1038. return -ENOMEM;
  1039. }
  1040. free(vmas);
  1041. vmas = new_vmas;
  1042. count *= 2;
  1043. goto retry_dump_vmas;
  1044. }
  1045. if (ret < 0)
  1046. return ret;
  1047. count = ret;
  1048. for (struct shim_vma_val * vma = &vmas[count - 1] ; vma >= vmas ; vma--)
  1049. DO_CP(vma, vma, NULL);
  1050. free_vma_val_array(vmas, count);
  1051. }
  1052. END_CP_FUNC_NO_RS(all_vmas)
  1053. void debug_print_vma_list (void)
  1054. {
  1055. SYS_PRINTF("vma bookkeeping:\n");
  1056. struct shim_vma * vma;
  1057. LISTP_FOR_EACH_ENTRY(vma, &vma_list, list) {
  1058. const char * type = "", * name = "";
  1059. if (vma->file) {
  1060. if (!qstrempty(&vma->file->path)) {
  1061. type = " path=";
  1062. name = qstrgetstr(&vma->file->path);
  1063. } else if (!qstrempty(&vma->file->uri)) {
  1064. type = " uri=";
  1065. name = qstrgetstr(&vma->file->uri);
  1066. }
  1067. }
  1068. SYS_PRINTF("[%p-%p] prot=%08x flags=%08x%s%s offset=%ld%s%s%s%s\n",
  1069. vma->start, vma->end,
  1070. vma->prot,
  1071. vma->flags & ~(VMA_INTERNAL|VMA_UNMAPPED|VMA_TAINTED|VMA_CP),
  1072. type, name,
  1073. vma->offset,
  1074. vma->flags & VMA_INTERNAL ? " (internal)" : "",
  1075. vma->flags & VMA_UNMAPPED ? " (unmapped)" : "",
  1076. vma->comment[0] ? " comment=" : "",
  1077. vma->comment[0] ? vma->comment : "");
  1078. }
  1079. }