shim_vma.c 37 KB

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