shim_vma.c 38 KB

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