shim_vma.c 39 KB

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