shim_vma.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 codes 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 <linux_list.h>
  28. #include <asm/mman.h>
  29. #include <errno.h>
  30. unsigned long mem_max_npages __attribute_migratable = DEFAULT_MEM_MAX_NPAGES;
  31. static void * heap_top __attribute_migratable;
  32. static void * heap_bottom __attribute_migratable;
  33. #define VMA_MGR_ALLOC 64
  34. #define PAGE_SIZE allocsize
  35. static LOCKTYPE vma_mgr_lock;
  36. #define system_lock() lock(vma_mgr_lock)
  37. #define system_unlock() unlock(vma_mgr_lock)
  38. #define OBJ_TYPE struct shim_vma
  39. #include <memmgr.h>
  40. static MEM_MGR vma_mgr = NULL;
  41. static LIST_HEAD(vma_list);
  42. static LOCKTYPE vma_list_lock;
  43. static inline int test_vma_equal (struct shim_vma * tmp,
  44. const void * addr, int length)
  45. {
  46. return tmp->addr == addr &&
  47. tmp->addr + tmp->length == addr + length;
  48. }
  49. static inline int test_vma_contain (struct shim_vma * tmp,
  50. const void * addr, int length)
  51. {
  52. return tmp->addr <= addr &&
  53. tmp->addr + tmp->length >= addr + length;
  54. }
  55. static inline int test_vma_startin (struct shim_vma * tmp,
  56. const void * addr, int length)
  57. {
  58. return tmp->addr >= addr &&
  59. tmp->addr < addr + length;
  60. }
  61. static inline int test_vma_endin (struct shim_vma * tmp,
  62. const void * addr, int length)
  63. {
  64. return tmp->addr + tmp->length > addr &&
  65. tmp->addr + tmp->length <= addr + length;
  66. }
  67. static inline int test_vma_overlap (struct shim_vma * tmp,
  68. const void * addr, int length)
  69. {
  70. return test_vma_contain (tmp, addr + 1, 0) ||
  71. test_vma_contain (tmp, addr + length - 1, 0) ||
  72. test_vma_startin (tmp, addr, length - 1);
  73. }
  74. int bkeep_shim_heap (void);
  75. static void __set_heap_top (void * bottom, void * top);
  76. int init_vma (void)
  77. {
  78. if (!(vma_mgr = create_mem_mgr(init_align_up(VMA_MGR_ALLOC))))
  79. return -ENOMEM;
  80. heap_bottom = (void *) PAL_CB(user_address.start);
  81. if (heap_bottom + DEFAULT_HEAP_MIN_SIZE > PAL_CB(executable_range.start) &&
  82. heap_bottom < PAL_CB(executable_range.end))
  83. heap_bottom = (void *) ALIGN_UP(PAL_CB(executable_range.end));
  84. __set_heap_top(heap_bottom, (void *) PAL_CB(user_address.end));
  85. bkeep_shim_heap();
  86. create_lock(vma_list_lock);
  87. return 0;
  88. }
  89. /* This might not give the same vma but we might need to
  90. split after we find something */
  91. static inline void assert_vma (void)
  92. {
  93. struct shim_vma * tmp;
  94. struct shim_vma * prev __attribute__((unused)) = NULL;
  95. list_for_each_entry(tmp, &vma_list, list) {
  96. /* Assert we are really sorted */
  97. assert(tmp->length > 0);
  98. assert(!prev || prev->addr + prev->length <= tmp->addr);
  99. prev = tmp;
  100. }
  101. }
  102. static struct shim_vma * __lookup_vma (const void * addr, int len);
  103. static struct shim_vma * __lookup_supervma (const void * addr, int length,
  104. struct shim_vma ** prev);
  105. static struct shim_vma * __lookup_overlap_vma (const void * addr, int length,
  106. struct shim_vma ** prev);
  107. void get_vma (struct shim_vma * vma)
  108. {
  109. #ifdef DEBUG_REF
  110. int ref_count = REF_INC(vma->ref_count);
  111. debug("get vma %p(%p-%p) (ref_count = %d)\n", vma, vma->addr,
  112. vma->addr + vma->length, ref_count);
  113. #else
  114. REF_INC(vma->ref_count);
  115. #endif
  116. }
  117. void put_vma (struct shim_vma * vma)
  118. {
  119. int ref_count = REF_DEC(vma->ref_count);
  120. #ifdef DEBUG_REF
  121. debug("put vma %p(%p-%p) (ref_count = %d)\n", vma,
  122. vma->addr, vma->addr + vma->length, ref_count - 1);
  123. #endif
  124. if (ref_count < 1) {
  125. if (vma->file)
  126. put_handle(vma->file);
  127. if (MEMORY_MIGRATED(vma))
  128. memset(vma, 0, sizeof(struct shim_vma));
  129. else
  130. free_mem_obj_to_mgr(vma_mgr, vma);
  131. }
  132. }
  133. static void __remove_vma (struct shim_vma * vma)
  134. {
  135. list_del_init(&vma->list);
  136. put_vma(vma);
  137. }
  138. static int __bkeep_mmap (void * addr, int length, int prot, int flags,
  139. struct shim_handle * file, int offset,
  140. const char * comment);
  141. static int __bkeep_mprotect (void * addr, int length, int prot,
  142. const int * flags);
  143. static struct shim_vma * get_new_vma (void)
  144. {
  145. struct shim_vma * tmp =
  146. get_mem_obj_from_mgr_enlarge(vma_mgr, size_align_up(VMA_MGR_ALLOC));
  147. if (!tmp)
  148. return NULL;
  149. memset(tmp, 0, sizeof(struct shim_vma));
  150. REF_SET(tmp->ref_count, 1);
  151. return tmp;
  152. }
  153. static bool check_vma_flags (const struct shim_vma * vma, const int * flags)
  154. {
  155. if (!flags)
  156. return true;
  157. if ((vma->flags & VMA_INTERNAL) != ((*flags) & VMA_INTERNAL)) {
  158. bug();
  159. return false;
  160. }
  161. return true;
  162. }
  163. static inline void __set_comment (struct shim_vma * vma, const char * comment)
  164. {
  165. if (!comment) {
  166. vma->comment[0] = 0;
  167. return;
  168. }
  169. int len = strlen(comment);
  170. if (len > VMA_COMMENT_LEN - 1)
  171. len = VMA_COMMENT_LEN - 1;
  172. memcpy(vma->comment, comment, len + 1);
  173. }
  174. static int __bkeep_mmap (void * addr, int length,
  175. int prot, int flags,
  176. struct shim_handle * file, int offset,
  177. const char * comment)
  178. {
  179. struct shim_vma * prev = NULL;
  180. struct shim_vma * tmp = __lookup_supervma(addr, length, &prev);
  181. int ret = 0;
  182. if (file)
  183. get_handle(file);
  184. if (tmp) { /* the range is included in a vma */
  185. if (tmp->addr != addr || tmp->length != length) {
  186. /* we are inside some unmapped area, do a split case */
  187. ret = __bkeep_mprotect(addr, length, prot, &flags);
  188. if (ret < 0)
  189. goto err;
  190. /* now we get the exact vma handle */
  191. tmp = __lookup_vma(addr, length);
  192. assert(tmp);
  193. assert(check_vma_flags(tmp, &flags));
  194. }
  195. } else {
  196. struct shim_vma * cont = NULL, * n; /* cont: continue to scan vmas */
  197. struct list_head * pos = NULL; /* pos: position to add the vma */
  198. if (prev && prev->addr == addr &&
  199. prev->length <= length) { /* find a vma at the same addr */
  200. cont = tmp = prev;
  201. } else { /* need to add a new vma */
  202. unlock(vma_list_lock);
  203. if (!(tmp = get_new_vma()))
  204. return -ENOMEM;
  205. lock(vma_list_lock);
  206. if (prev) { /* has a precendent vma */
  207. if (test_vma_endin(prev, addr, length)) {
  208. if (!check_vma_flags(prev, &flags)) {
  209. ret = -EACCES;
  210. goto err;
  211. }
  212. /* the previous vma ends in the range; otherwise, there is
  213. * no overlapping. Another case is handled by the supervma
  214. * case. */
  215. prev->length = addr - prev->addr;
  216. }
  217. assert(prev->addr + prev->length <= addr);
  218. cont = prev;
  219. pos = &prev->list;
  220. } else { /* has no precendent vma */
  221. cont = tmp;
  222. list_add(&tmp->list, &vma_list);
  223. }
  224. }
  225. if (cont)
  226. list_for_each_entry_safe_continue(cont, n, &vma_list, list) {
  227. if (!test_vma_startin(cont, addr, length))
  228. break;
  229. if (!check_vma_flags(cont, &flags)) {
  230. ret = -EACCES;
  231. goto err;
  232. }
  233. if (test_vma_endin(cont, addr, length)) {
  234. __remove_vma(cont);
  235. continue;
  236. }
  237. long offset = addr + length - cont->addr;
  238. assert(offset > 0);
  239. if (cont->file)
  240. cont->offset += offset;
  241. cont->addr += offset;
  242. cont->length -= offset;
  243. break;
  244. }
  245. if (tmp && pos)
  246. list_add(&tmp->list, pos);
  247. }
  248. tmp->addr = addr;
  249. tmp->length = length;
  250. tmp->prot = prot;
  251. tmp->flags = flags|((file && (prot & PROT_WRITE)) ? VMA_TAINTED : 0);
  252. tmp->file = file;
  253. tmp->offset = offset;
  254. __set_comment(tmp, comment);
  255. return 0;
  256. err:
  257. if (file)
  258. put_handle(file);
  259. return ret;
  260. }
  261. int bkeep_mmap (void * addr, int length, int prot, int flags,
  262. struct shim_handle * file, int offset, const char * comment)
  263. {
  264. if (!addr || !length)
  265. return -EINVAL;
  266. lock(vma_list_lock);
  267. int ret = __bkeep_mmap(addr, length, prot, flags, file, offset,
  268. comment);
  269. assert_vma();
  270. unlock(vma_list_lock);
  271. return ret;
  272. }
  273. /*
  274. * munmap start at any address and it might be split in between so
  275. * We need to split the area aur reduce the size
  276. * Check the address falls between alread allocated area or not
  277. */
  278. static int __bkeep_munmap (void * addr, int length, const int * flags)
  279. {
  280. struct shim_vma * tmp, * n;
  281. list_for_each_entry_safe(tmp, n, &vma_list, list) {
  282. if (test_vma_equal (tmp, addr, length)) {
  283. if (!check_vma_flags(tmp, flags))
  284. return -EACCES;
  285. __remove_vma(tmp);
  286. } else if (test_vma_overlap (tmp, addr, length)) {
  287. unsigned long before_length;
  288. unsigned long after_length;
  289. unsigned long after_offset;
  290. if (addr > tmp->addr)
  291. before_length = addr - tmp->addr;
  292. else
  293. before_length = 0;
  294. if (tmp->addr + tmp->length > addr + length)
  295. after_length = (tmp->addr + tmp->length) - (addr + length);
  296. else
  297. after_length = 0;
  298. after_offset = tmp->file ? tmp->offset + tmp->length -
  299. after_length : 0;
  300. /* split case
  301. * it is Unlikely that a process does an partical unmap
  302. * but We take care of it by splitting the book-keep
  303. *
  304. * case 1 if the vma is entirely between a mapped area
  305. * .e.g See case:
  306. * ---unmap--
  307. * ------map-----------
  308. */
  309. if (before_length) {
  310. /* Case 1: Space in the vma before */
  311. if (!check_vma_flags(tmp, flags))
  312. return -EACCES;
  313. tmp->length = before_length;
  314. if (after_length) {
  315. /* Case 2: Space before and also space after */
  316. int ret = __bkeep_mmap((void *) addr + length, after_length,
  317. tmp->prot, tmp->flags,
  318. tmp->file, after_offset,
  319. tmp->comment);
  320. if (ret < 0)
  321. return ret;
  322. }
  323. } else if (after_length) {
  324. /* Case 3: Only after length */
  325. if (!check_vma_flags(tmp, flags))
  326. return -EACCES;
  327. tmp->addr = (void *) addr + length;
  328. tmp->length = after_length;
  329. tmp->offset = after_offset;
  330. } else {
  331. if (!check_vma_flags(tmp, flags))
  332. return -EACCES;
  333. __remove_vma(tmp);
  334. }
  335. } else if (tmp->addr > (addr + length))
  336. break;
  337. }
  338. return 0;
  339. }
  340. int bkeep_munmap (void * addr, int length, const int * flags)
  341. {
  342. if (!addr || !length)
  343. return -EINVAL;
  344. lock(vma_list_lock);
  345. int ret = __bkeep_munmap(addr, length, flags);
  346. assert_vma();
  347. unlock(vma_list_lock);
  348. return ret;
  349. }
  350. static int __bkeep_mprotect (void * addr, int length, int prot,
  351. const int * flags)
  352. {
  353. struct shim_vma * tmp = __lookup_vma(addr, length);
  354. int ret;
  355. if (tmp) {
  356. /* exact match */
  357. if (!check_vma_flags(tmp, flags))
  358. return -EACCES;
  359. tmp->prot = prot;
  360. if (tmp->file && (prot & PROT_WRITE))
  361. tmp->flags |= VMA_TAINTED;
  362. return 0;
  363. }
  364. /* split case
  365. * it is Unlikely that a process does an partical unmap
  366. * but We take care of it by splitting the book-keep
  367. *
  368. * case 1 if the vma is entirely between a mapped area .e.g See case:
  369. * ---unmap--
  370. * ------map-----------
  371. */
  372. tmp = __lookup_supervma(addr, length, NULL);
  373. if (tmp) {
  374. if (!check_vma_flags(tmp, flags))
  375. return -EACCES;
  376. int before_length = addr - tmp->addr;
  377. int after_length = tmp->addr + tmp->length - addr - length;
  378. int after_offset = tmp->file ? tmp->offset + tmp->length -
  379. after_length : 0;
  380. int inside_offset = tmp->file ? tmp->offset + before_length : 0;
  381. /* split the handler first, because we might call bkeep_mmap */
  382. tmp->addr = (void *) addr;
  383. tmp->length = length;
  384. if (before_length) {
  385. ret = __bkeep_mmap((void *) addr - before_length, before_length,
  386. tmp->prot, tmp->flags,
  387. tmp->file, tmp->offset,
  388. tmp->comment);
  389. if (ret < 0)
  390. return ret;
  391. }
  392. if (after_length) {
  393. ret = __bkeep_mmap((void *)addr + length, after_length,
  394. tmp->prot, tmp->flags,
  395. tmp->file, after_offset,
  396. tmp->comment);
  397. if (ret < 0)
  398. return ret;
  399. }
  400. tmp->prot = prot;
  401. tmp->offset = inside_offset;
  402. if (tmp->file && (prot & PROT_WRITE))
  403. tmp->flags |= VMA_TAINTED;
  404. return 0;
  405. }
  406. /* split case
  407. * if the unmap are in between to mapped
  408. * area then we need to split two VMA here
  409. * This is the most unlikely case
  410. *
  411. * case 2
  412. * ------unmap------
  413. * ----map1-----;-----map2-------
  414. *
  415. * TODO: this algorithm is very inefficient, and may change
  416. * the mapping if it fails
  417. */
  418. int o_length = length;
  419. while (length) {
  420. struct shim_vma * candidate = NULL;
  421. list_for_each_entry(tmp, &vma_list, list) {
  422. if (test_vma_contain (tmp, addr, 1)) {
  423. if (!check_vma_flags(tmp, flags))
  424. return -EACCES;
  425. int before_length = addr - tmp->addr;
  426. int after_length = tmp->addr + tmp->length > addr + length ?
  427. tmp->addr + tmp->length - addr - length : 0;
  428. int after_offset = tmp->file ? tmp->offset + tmp->length -
  429. after_length : 0;
  430. int inside_length = tmp->addr + tmp->length > addr + length ?
  431. length :
  432. addr + length - tmp->addr - tmp->length;
  433. int inside_offset = tmp->file ? tmp->offset + before_length : 0;
  434. /* split the handler first, because we might call bkeep_mmap */
  435. tmp->addr = (void *) addr;
  436. tmp->length = inside_length;
  437. if (before_length) {
  438. ret = __bkeep_mmap((void *) addr - before_length, before_length,
  439. tmp->prot, tmp->flags,
  440. tmp->file, tmp->offset,
  441. tmp->comment);
  442. if (ret < 0)
  443. return ret;
  444. }
  445. if (after_length) {
  446. ret = __bkeep_mmap((void *) addr + length, after_length,
  447. tmp->prot, tmp->flags,
  448. tmp->file, after_offset,
  449. tmp->comment);
  450. if (ret < 0)
  451. return ret;
  452. }
  453. tmp->prot = prot;
  454. tmp->offset = inside_offset;
  455. if (tmp->file && (prot & PROT_WRITE))
  456. tmp->flags |= VMA_TAINTED;
  457. addr += inside_length;
  458. length -= inside_length;
  459. break;
  460. }
  461. if (test_vma_startin(tmp, addr, length))
  462. if (!candidate || candidate->addr > tmp->addr)
  463. candidate = tmp;
  464. }
  465. if (o_length == length) {
  466. if (!candidate) {
  467. /* no more vmas, protect the whole area */
  468. ret = __bkeep_mmap((void *) addr, length, prot,
  469. VMA_UNMAPPED|(flags ? *flags : 0),
  470. NULL, 0, NULL);
  471. if (ret < 0)
  472. return ret;
  473. candidate = __lookup_vma((void *) addr, length);
  474. assert(candidate);
  475. }
  476. length -= candidate->addr - addr;
  477. }
  478. o_length = length;
  479. }
  480. return 0;
  481. }
  482. int bkeep_mprotect (void * addr, int length, int prot, const int * flags)
  483. {
  484. if (!addr || !length)
  485. return -EINVAL;
  486. lock(vma_list_lock);
  487. int ret = __bkeep_mprotect(addr, length, prot, flags);
  488. assert_vma();
  489. unlock(vma_list_lock);
  490. return ret;
  491. }
  492. static void __set_heap_top (void * bottom, void * top)
  493. {
  494. bottom += DEFAULT_HEAP_MIN_SIZE;
  495. if (bottom >= top) {
  496. heap_top = top;
  497. return;
  498. }
  499. unsigned long rand;
  500. while (getrand(&rand, sizeof(unsigned long)) < sizeof(unsigned long));
  501. rand %= (unsigned long) (top - bottom) / allocsize;
  502. heap_top = bottom + rand * allocsize;
  503. debug("heap top adjusted to %p\n", heap_top);
  504. }
  505. void * get_unmapped_vma (int length, int flags)
  506. {
  507. struct shim_vma * new = get_new_vma(), * tmp = NULL;
  508. if (!new)
  509. return NULL;
  510. lock(vma_list_lock);
  511. do {
  512. new->addr = heap_top - length;
  513. new->length = length;
  514. new->flags = flags|VMA_UNMAPPED;
  515. list_for_each_entry_reverse(tmp, &vma_list, list) {
  516. if (new->addr >= tmp->addr + tmp->length)
  517. break;
  518. if (new->addr < heap_bottom)
  519. break;
  520. if (new->addr > tmp->addr - length)
  521. new->addr = tmp->addr - length;
  522. }
  523. if (new->addr < heap_bottom) {
  524. if (heap_top == PAL_CB(user_address.end)) {
  525. unlock(vma_list_lock);
  526. put_vma(new);
  527. return NULL;
  528. } else {
  529. __set_heap_top(heap_top, (void *) PAL_CB(user_address.end));
  530. new->addr = NULL;
  531. }
  532. }
  533. } while (!new->addr);
  534. get_vma(new);
  535. list_add(&new->list, tmp ? &tmp->list : &vma_list);
  536. unlock(vma_list_lock);
  537. return new->addr;
  538. }
  539. #define NTRIES 4
  540. void * get_unmapped_vma_for_cp (int length)
  541. {
  542. struct shim_vma * new = get_new_vma(), * prev = NULL;
  543. if (!new)
  544. return NULL;
  545. lock(vma_list_lock);
  546. unsigned long top = (unsigned long) PAL_CB(user_address.end) - length;
  547. unsigned long bottom = (unsigned long) heap_top;
  548. void * addr;
  549. if (bottom >= top)
  550. return NULL;
  551. for (int i = 0 ; i < NTRIES ; i++) {
  552. unsigned long rand;
  553. while (getrand(&rand, sizeof(unsigned long)) < sizeof(unsigned long));
  554. rand %= (unsigned long) (top - bottom) / allocsize;
  555. addr = (void *) bottom + rand * allocsize;
  556. if (!__lookup_overlap_vma(addr, length, &prev))
  557. break;
  558. addr = NULL;
  559. }
  560. if (!addr)
  561. return NULL;
  562. new->addr = addr;
  563. new->length = length;
  564. new->flags = MAP_ANONYMOUS|VMA_UNMAPPED|VMA_INTERNAL;
  565. new->prot = PROT_READ|PROT_WRITE;
  566. list_add(&new->list, prev ? &prev->list : &vma_list);
  567. unlock(vma_list_lock);
  568. return addr;
  569. }
  570. /* This might not give the same vma but we might need to
  571. split after we find something */
  572. static struct shim_vma * __lookup_overlap_vma (const void * addr, int length,
  573. struct shim_vma ** pprev)
  574. {
  575. struct shim_vma * tmp, * prev = NULL;
  576. list_for_each_entry(tmp, &vma_list, list) {
  577. if (test_vma_overlap (tmp, addr, length)) {
  578. if (pprev)
  579. *pprev = prev;
  580. return tmp;
  581. }
  582. /* Assert we are really sorted */
  583. assert(!prev || prev->addr < tmp->addr);
  584. /* Insert in order; break once we are past the appropriate point */
  585. if (tmp->addr > addr)
  586. break;
  587. prev = tmp;
  588. }
  589. if (pprev)
  590. *pprev = prev;
  591. return NULL;
  592. }
  593. int lookup_overlap_vma (const void * addr, int length,
  594. struct shim_vma ** vma)
  595. {
  596. struct shim_vma * tmp = NULL;
  597. lock(vma_list_lock);
  598. if ((tmp = __lookup_overlap_vma(addr, length, NULL)) && vma)
  599. get_vma((tmp));
  600. unlock(vma_list_lock);
  601. if (vma)
  602. *vma = tmp;
  603. return tmp ? 0: -ENOENT;
  604. }
  605. static struct shim_vma * __lookup_vma (const void * addr, int length)
  606. {
  607. struct shim_vma * tmp;
  608. struct shim_vma * prev __attribute__((unused)) = NULL;
  609. list_for_each_entry(tmp, &vma_list, list) {
  610. if (test_vma_equal(tmp, addr, length))
  611. return tmp;
  612. /* Assert we are really sorted */
  613. assert(!prev || prev->addr + prev->length <= tmp->addr);
  614. prev = tmp;
  615. }
  616. return NULL;
  617. }
  618. static struct shim_vma * __lookup_supervma (const void * addr, int length,
  619. struct shim_vma ** pprev)
  620. {
  621. struct shim_vma * tmp, * prev = NULL;
  622. list_for_each_entry(tmp, &vma_list, list) {
  623. if (test_vma_contain(tmp, addr, length)) {
  624. if (pprev)
  625. *pprev = prev;
  626. return tmp;
  627. }
  628. /* Assert we are really sorted */
  629. assert(!prev || prev->addr + prev->length <= tmp->addr);
  630. /* Insert in order; break once we are past the appropriate point */
  631. if (tmp->addr > addr)
  632. break;
  633. prev = tmp;
  634. }
  635. if (pprev)
  636. *pprev = prev;
  637. return NULL;
  638. }
  639. int lookup_supervma (const void * addr, int length, struct shim_vma ** vma)
  640. {
  641. struct shim_vma * tmp = NULL;
  642. lock(vma_list_lock);
  643. if ((tmp = __lookup_supervma(addr, length, NULL)) && vma)
  644. get_vma((tmp));
  645. unlock(vma_list_lock);
  646. if (vma)
  647. *vma = tmp;
  648. return tmp ? 0 : -ENOENT;
  649. }
  650. struct shim_vma * next_vma (struct shim_vma * vma)
  651. {
  652. struct shim_vma * tmp = vma;
  653. lock(vma_list_lock);
  654. if (!tmp) {
  655. if (!list_empty(&vma_list) &&
  656. (tmp = list_first_entry(&vma_list, struct shim_vma, list)))
  657. get_vma(tmp);
  658. unlock(vma_list_lock);
  659. return tmp;
  660. }
  661. if (tmp->list.next == &vma_list) {
  662. tmp = NULL;
  663. } else if (tmp->list.next == &tmp->list) {
  664. struct shim_vma * tmp2;
  665. tmp = NULL;
  666. list_for_each_entry(tmp2, &vma_list, list)
  667. if (tmp2->addr >= vma->addr) {
  668. tmp = tmp2;
  669. get_vma(tmp);
  670. break;
  671. }
  672. } else {
  673. tmp = list_entry(tmp->list.next, struct shim_vma, list);
  674. get_vma(tmp);
  675. }
  676. put_vma(vma);
  677. unlock(vma_list_lock);
  678. return tmp;
  679. }
  680. /* to speed up the checkpointing, go organize the VMAs */
  681. void __shrink_vmas (void)
  682. {
  683. struct shim_vma * vma, * n, * last;
  684. list_for_each_entry_safe(vma, n, &vma_list, list) {
  685. if (!last)
  686. goto unmap;
  687. if (last->addr + last->length != vma->addr ||
  688. last->prot != vma->prot ||
  689. last->flags != vma->flags ||
  690. last->file != vma->file)
  691. goto unmap;
  692. if (last->file && last->offset + last->length != vma->offset)
  693. goto unmap;
  694. debug("shrink vma %p-%p and %p-%p\n", last->addr,
  695. last->addr + last->length, vma->addr, vma->addr + vma->length);
  696. last->length += vma->length;
  697. __remove_vma(vma);
  698. continue;
  699. next:
  700. last = vma;
  701. continue;
  702. unmap:
  703. if (vma->prot == PROT_NONE && !(vma->flags & VMA_TAINTED))
  704. vma->flags |= VMA_UNMAPPED;
  705. goto next;
  706. }
  707. }
  708. int dump_all_vmas (struct shim_thread * thread, char * buf, int size)
  709. {
  710. struct shim_vma * vma;
  711. int cnt = 0;
  712. lock(vma_list_lock);
  713. list_for_each_entry(vma, &vma_list, list) {
  714. void * start = vma->addr, * end = vma->addr + vma->length;
  715. if ((vma->flags & (VMA_INTERNAL|VMA_UNMAPPED)) && !vma->comment[0])
  716. continue;
  717. char prot[3] = {'-', '-', '-'};
  718. if (vma->prot & PROT_READ)
  719. prot[0] = 'r';
  720. if (vma->prot & PROT_WRITE)
  721. prot[1] = 'w';
  722. if (vma->prot & PROT_EXEC)
  723. prot[2] = 'x';
  724. if (vma->file) {
  725. int dev_major = 0, dev_minor = 0;
  726. unsigned long ino = vma->file->dentry ? vma->file->dentry->ino : 0;
  727. const char * name = "[unknown]";
  728. if (!qstrempty(&vma->file->path))
  729. name = qstrgetstr(&vma->file->path);
  730. cnt += snprintf(buf + cnt, size - cnt,
  731. start > (void *) 0xffffffff ? "%lx" : "%08x",
  732. start);
  733. cnt += snprintf(buf + cnt, size - cnt,
  734. end > (void *) 0xffffffff ? "-%lx" : "-%08x", end);
  735. cnt += snprintf(buf + cnt, size - cnt,
  736. " %c%c%cp %08x %02d:%02d %u %s\n",
  737. prot[0], prot[1], prot[2],
  738. vma->offset, dev_major, dev_minor, ino, name);
  739. } else {
  740. cnt += snprintf(buf + cnt, size - cnt,
  741. start > (void *) 0xffffffff ? "%lx" : "%08x",
  742. start);
  743. cnt += snprintf(buf + cnt, size - cnt,
  744. end > (void *) 0xffffffff ? "-%lx" : "-%08x", end);
  745. if (vma->comment[0])
  746. cnt += snprintf(buf + cnt, size - cnt,
  747. " %c%c%cp 00000000 00:00 0 [%s]\n",
  748. prot[0], prot[1], prot[2], vma->comment);
  749. else
  750. cnt += snprintf(buf + cnt, size - cnt,
  751. " %c%c%cp 00000000 00:00 0\n",
  752. prot[0], prot[1], prot[2]);
  753. }
  754. if (cnt >= size) {
  755. cnt = -EOVERFLOW;
  756. break;
  757. }
  758. }
  759. unlock(vma_list_lock);
  760. return cnt;
  761. }
  762. void unmap_all_vmas (void)
  763. {
  764. struct shim_thread * cur_thread = get_cur_thread();
  765. struct shim_vma * tmp, * n;
  766. void * start = NULL, * end = NULL;
  767. lock(vma_list_lock);
  768. list_for_each_entry_safe(tmp, n, &vma_list, list) {
  769. /* a adhoc vma can never be removed */
  770. if (tmp->flags & VMA_INTERNAL)
  771. continue;
  772. if (tmp->flags & VMA_UNMAPPED) {
  773. __remove_vma(tmp);
  774. continue;
  775. }
  776. if (cur_thread->stack &&
  777. test_vma_overlap(tmp, cur_thread->stack,
  778. cur_thread->stack_top - cur_thread->stack))
  779. continue;
  780. if (start == NULL)
  781. start = end = tmp->addr;
  782. if (end == tmp->addr) {
  783. end += tmp->length;
  784. __remove_vma(tmp);
  785. continue;
  786. }
  787. debug("removing vma %p - %p\n", start, end);
  788. DkVirtualMemoryFree(start, end - start);
  789. start = end = tmp->addr;
  790. end += tmp->length;
  791. __remove_vma(tmp);
  792. }
  793. if (start != NULL && start < end) {
  794. debug("removing vma %p - %p\n", start, end);
  795. DkVirtualMemoryFree(start, end - start);
  796. }
  797. unlock(vma_list_lock);
  798. }
  799. BEGIN_CP_FUNC(vma)
  800. {
  801. assert(size == sizeof(struct shim_vma));
  802. struct shim_vma * vma = (struct shim_vma *) obj;
  803. struct shim_vma * new_vma = NULL;
  804. PAL_FLG pal_prot = PAL_PROT(vma->prot, 0);
  805. ptr_t off = GET_FROM_CP_MAP(obj);
  806. if (!off) {
  807. off = ADD_CP_OFFSET(sizeof(struct shim_vma));
  808. ADD_TO_CP_MAP(obj, off);
  809. new_vma = (struct shim_vma *) (base + off);
  810. memcpy(new_vma, vma, sizeof(struct shim_vma));
  811. if (vma->file)
  812. DO_CP(handle, vma->file, &new_vma->file);
  813. REF_SET(new_vma->ref_count, 0);
  814. INIT_LIST_HEAD(&new_vma->list);
  815. void * need_mapped = vma->addr;
  816. #if MIGRATE_MORE_GIPC == 1
  817. if (store->use_gipc) {
  818. if (!NEED_MIGRATE_MEMORY_IF_GIPC(vma))
  819. goto no_mem;
  820. } else {
  821. if (!NEED_MIGRATE_MEMORY(vma))
  822. goto no_mem;
  823. }
  824. #else
  825. if (!NEED_MIGRATE_MEMORY(vma))
  826. goto no_mem;
  827. #endif
  828. void * send_addr = vma->addr;
  829. int send_size = vma->length;
  830. bool protected = false;
  831. if (vma->file) {
  832. int file_len = get_file_size(vma->file);
  833. if (file_len >= 0 &&
  834. vma->offset + vma->length > file_len)
  835. send_size = file_len > vma->offset ?
  836. file_len - vma->offset : 0;
  837. }
  838. if (!send_size)
  839. goto no_mem;
  840. if (store->use_gipc) {
  841. #if HASH_GIPC == 1
  842. if (!(pal_prot & PAL_PROT_READ)) {
  843. protected = true;
  844. DkVirtualMemoryProtect(send_addr,
  845. send_size,
  846. pal_prot|PAL_PROT_READ);
  847. }
  848. #endif /* HASH_GIPC == 1 */
  849. struct shim_gipc_entry * gipc;
  850. DO_CP_SIZE(gipc, send_addr, send_size, &gipc);
  851. gipc->mem.prot = pal_prot;
  852. } else {
  853. if (!(pal_prot & PROT_READ)) {
  854. protected = true;
  855. DkVirtualMemoryProtect(send_addr,
  856. send_size,
  857. pal_prot|PAL_PROT_READ);
  858. }
  859. struct shim_mem_entry * mem;
  860. DO_CP_SIZE(memory, send_addr, send_size, &mem);
  861. mem->prot = pal_prot;
  862. }
  863. need_mapped = vma->addr + vma->length;
  864. if (protected)
  865. DkVirtualMemoryProtect(send_addr, send_size, pal_prot);
  866. no_mem:
  867. ADD_CP_FUNC_ENTRY(off);
  868. ADD_CP_ENTRY(ADDR, need_mapped);
  869. } else {
  870. new_vma = (struct shim_vma *) (base + off);
  871. }
  872. if (objp)
  873. *objp = (void *) new_vma;
  874. }
  875. END_CP_FUNC(vma)
  876. DEFINE_PROFILE_CATAGORY(inside_rs_vma, resume_func);
  877. DEFINE_PROFILE_INTERVAL(vma_lookup_overlap, inside_rs_vma);
  878. DEFINE_PROFILE_INTERVAL(vma_add_bookkeep, inside_rs_vma);
  879. DEFINE_PROFILE_INTERVAL(vma_map_file, inside_rs_vma);
  880. DEFINE_PROFILE_INTERVAL(vma_map_anonymous, inside_rs_vma);
  881. BEGIN_RS_FUNC(vma)
  882. {
  883. struct shim_vma * vma = (void *) (base + GET_CP_FUNC_ENTRY());
  884. struct shim_vma * tmp, * prev = NULL;
  885. void * need_mapped = (void *) GET_CP_ENTRY(ADDR);
  886. int ret = 0;
  887. CP_REBASE(vma->file);
  888. CP_REBASE(vma->list);
  889. lock(vma_list_lock);
  890. BEGIN_PROFILE_INTERVAL();
  891. tmp = __lookup_overlap_vma(vma->addr, vma->length, &prev);
  892. SAVE_PROFILE_INTERVAL(vma_lookup_overlap);
  893. if (tmp) {
  894. if ((ret = __bkeep_munmap(vma->addr, vma->length, &vma->flags)) < 0)
  895. return ret;
  896. if (prev->list.next == &tmp->list &&
  897. tmp->addr < vma->addr)
  898. prev = tmp;
  899. }
  900. get_vma(vma);
  901. list_add(&vma->list, prev ? &prev->list : &vma_list);
  902. assert_vma();
  903. SAVE_PROFILE_INTERVAL(vma_add_bookkeep);
  904. unlock(vma_list_lock);
  905. if (!(vma->flags & VMA_UNMAPPED)) {
  906. if (vma->file) {
  907. struct shim_mount * fs = vma->file->fs;
  908. get_handle(vma->file);
  909. if (need_mapped < vma->addr + vma->length) {
  910. /* first try, use hstat to force it resumes pal handle */
  911. assert(vma->file->fs && vma->file->fs->fs_ops &&
  912. vma->file->fs->fs_ops->mmap);
  913. void * addr = need_mapped;
  914. int ret = fs->fs_ops->mmap(vma->file, &addr,
  915. vma->addr + vma->length -
  916. need_mapped,
  917. vma->prot,
  918. vma->flags,
  919. vma->offset +
  920. (need_mapped - vma->addr));
  921. if (ret < 0)
  922. return ret;
  923. if (!addr)
  924. return -ENOMEM;
  925. if (addr != need_mapped)
  926. return -EACCES;
  927. need_mapped += vma->length;
  928. SAVE_PROFILE_INTERVAL(vma_map_file);
  929. }
  930. }
  931. if (need_mapped < vma->addr + vma->length) {
  932. int pal_alloc_type = (vma->flags & MAP_32BIT) ? PAL_ALLOC_32BIT : 0;
  933. int pal_prot = vma->prot;
  934. if (DkVirtualMemoryAlloc(need_mapped,
  935. vma->addr + vma->length - need_mapped,
  936. pal_alloc_type, pal_prot)) {
  937. need_mapped += vma->length;
  938. SAVE_PROFILE_INTERVAL(vma_map_anonymous);
  939. }
  940. }
  941. if (need_mapped < vma->addr + vma->length)
  942. sys_printf("vma %p-%p cannot be allocated!\n", need_mapped,
  943. vma->addr + vma->length);
  944. }
  945. if (vma->file)
  946. get_handle(vma->file);
  947. if (vma->file)
  948. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d,path=%s,uri=%s",
  949. vma->addr, vma->addr + vma->length, vma->length,
  950. vma->prot, vma->flags, vma->offset,
  951. qstrgetstr(&vma->file->path), qstrgetstr(&vma->file->uri));
  952. else
  953. DEBUG_RS("%p-%p,size=%d,prot=%08x,flags=%08x,off=%d",
  954. vma->addr, vma->addr + vma->length, vma->length,
  955. vma->prot, vma->flags, vma->offset);
  956. }
  957. END_RS_FUNC(vma)
  958. BEGIN_CP_FUNC(all_vmas)
  959. {
  960. struct shim_vma * tmp, ** vmas;
  961. int nvmas = 0, cnt = 0;
  962. lock(vma_list_lock);
  963. __shrink_vmas();
  964. list_for_each_entry(tmp, &vma_list, list)
  965. if (!(tmp->flags & VMA_INTERNAL))
  966. nvmas++;
  967. if (!nvmas) {
  968. unlock(vma_list_lock);
  969. return 0;
  970. }
  971. vmas = __alloca(sizeof(struct shim_vam *) * nvmas);
  972. list_for_each_entry(tmp, &vma_list, list)
  973. if (!(tmp->flags & VMA_INTERNAL)) {
  974. get_vma(tmp);
  975. vmas[cnt++] = tmp;
  976. }
  977. unlock(vma_list_lock);
  978. for (cnt = 0 ; cnt < nvmas ; cnt++) {
  979. DO_CP(vma, vmas[cnt], NULL);
  980. put_vma(vmas[cnt]);
  981. }
  982. }
  983. END_CP_FUNC_NO_RS(all_vmas)
  984. void debug_print_vma_list (void)
  985. {
  986. sys_printf("vma bookkeeping:\n");
  987. struct shim_vma * vma;
  988. list_for_each_entry(vma, &vma_list, list) {
  989. const char * type = "", * name = "";
  990. if (vma->file) {
  991. if (!qstrempty(&vma->file->path)) {
  992. type = " path=";
  993. name = qstrgetstr(&vma->file->path);
  994. } else if (!qstrempty(&vma->file->uri)) {
  995. type = " uri=";
  996. name = qstrgetstr(&vma->file->uri);
  997. }
  998. }
  999. sys_printf("[%p-%p] prot=%08x flags=%08x%s%s offset=%d%s%s%s%s\n",
  1000. vma->addr, vma->addr + vma->length,
  1001. vma->prot,
  1002. vma->flags & ~(VMA_INTERNAL|VMA_UNMAPPED|VMA_TAINTED),
  1003. type, name,
  1004. vma->offset,
  1005. vma->flags & VMA_INTERNAL ? " (internal)" : "",
  1006. vma->flags & VMA_UNMAPPED ? " (unmapped)" : "",
  1007. vma->comment[0] ? " comment=" : "",
  1008. vma->comment[0] ? vma->comment : "");
  1009. }
  1010. }
  1011. void print_vma_hash (struct shim_vma * vma, void * addr, int len,
  1012. bool force_protect)
  1013. {
  1014. if (!addr)
  1015. addr = vma->addr;
  1016. if (!len)
  1017. len = vma->length - (addr - vma->addr);
  1018. if (addr < vma->addr || addr + len > vma->addr + vma->length)
  1019. return;
  1020. if (!(vma->prot & PROT_READ)) {
  1021. if (!force_protect)
  1022. return;
  1023. DkVirtualMemoryProtect(vma->addr, vma->length, PAL_PROT_READ);
  1024. }
  1025. for (unsigned long p = (unsigned long) addr ;
  1026. p < (unsigned long) addr + len ; p += allocsize) {
  1027. unsigned long hash = 0;
  1028. struct shim_md5_ctx ctx;
  1029. md5_init(&ctx);
  1030. md5_update(&ctx, (void *) p, allocsize);
  1031. md5_final(&ctx);
  1032. memcpy(&hash, ctx.digest, sizeof(unsigned long));
  1033. }
  1034. if (!(vma->prot & PROT_READ))
  1035. DkVirtualMemoryProtect(vma->addr, vma->length, vma->prot);
  1036. }