tb-maint.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * Translation Block Maintenance
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/interval-tree.h"
  21. #include "qemu/qtree.h"
  22. #include "exec/cputlb.h"
  23. #include "exec/log.h"
  24. #include "exec/exec-all.h"
  25. #include "exec/page-protection.h"
  26. #include "exec/tb-flush.h"
  27. #include "exec/translate-all.h"
  28. #include "system/tcg.h"
  29. #include "tcg/tcg.h"
  30. #include "tb-hash.h"
  31. #include "tb-context.h"
  32. #include "internal-common.h"
  33. #include "internal-target.h"
  34. /* List iterators for lists of tagged pointers in TranslationBlock. */
  35. #define TB_FOR_EACH_TAGGED(head, tb, n, field) \
  36. for (n = (head) & 1, tb = (TranslationBlock *)((head) & ~1); \
  37. tb; tb = (TranslationBlock *)tb->field[n], n = (uintptr_t)tb & 1, \
  38. tb = (TranslationBlock *)((uintptr_t)tb & ~1))
  39. #define TB_FOR_EACH_JMP(head_tb, tb, n) \
  40. TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
  41. static bool tb_cmp(const void *ap, const void *bp)
  42. {
  43. const TranslationBlock *a = ap;
  44. const TranslationBlock *b = bp;
  45. return ((tb_cflags(a) & CF_PCREL || a->pc == b->pc) &&
  46. a->cs_base == b->cs_base &&
  47. a->flags == b->flags &&
  48. (tb_cflags(a) & ~CF_INVALID) == (tb_cflags(b) & ~CF_INVALID) &&
  49. tb_page_addr0(a) == tb_page_addr0(b) &&
  50. tb_page_addr1(a) == tb_page_addr1(b));
  51. }
  52. void tb_htable_init(void)
  53. {
  54. unsigned int mode = QHT_MODE_AUTO_RESIZE;
  55. qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
  56. }
  57. typedef struct PageDesc PageDesc;
  58. #ifdef CONFIG_USER_ONLY
  59. /*
  60. * In user-mode page locks aren't used; mmap_lock is enough.
  61. */
  62. #define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
  63. static inline void tb_lock_pages(const TranslationBlock *tb) { }
  64. /*
  65. * For user-only, since we are protecting all of memory with a single lock,
  66. * and because the two pages of a TranslationBlock are always contiguous,
  67. * use a single data structure to record all TranslationBlocks.
  68. */
  69. static IntervalTreeRoot tb_root;
  70. static void tb_remove_all(void)
  71. {
  72. assert_memory_lock();
  73. memset(&tb_root, 0, sizeof(tb_root));
  74. }
  75. /* Call with mmap_lock held. */
  76. static void tb_record(TranslationBlock *tb)
  77. {
  78. vaddr addr;
  79. int flags;
  80. assert_memory_lock();
  81. tb->itree.last = tb->itree.start + tb->size - 1;
  82. /* translator_loop() must have made all TB pages non-writable */
  83. addr = tb_page_addr0(tb);
  84. flags = page_get_flags(addr);
  85. assert(!(flags & PAGE_WRITE));
  86. addr = tb_page_addr1(tb);
  87. if (addr != -1) {
  88. flags = page_get_flags(addr);
  89. assert(!(flags & PAGE_WRITE));
  90. }
  91. interval_tree_insert(&tb->itree, &tb_root);
  92. }
  93. /* Call with mmap_lock held. */
  94. static void tb_remove(TranslationBlock *tb)
  95. {
  96. assert_memory_lock();
  97. interval_tree_remove(&tb->itree, &tb_root);
  98. }
  99. /* TODO: For now, still shared with translate-all.c for system mode. */
  100. #define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \
  101. for (T = foreach_tb_first(start, last), \
  102. N = foreach_tb_next(T, start, last); \
  103. T != NULL; \
  104. T = N, N = foreach_tb_next(N, start, last))
  105. typedef TranslationBlock *PageForEachNext;
  106. static PageForEachNext foreach_tb_first(tb_page_addr_t start,
  107. tb_page_addr_t last)
  108. {
  109. IntervalTreeNode *n = interval_tree_iter_first(&tb_root, start, last);
  110. return n ? container_of(n, TranslationBlock, itree) : NULL;
  111. }
  112. static PageForEachNext foreach_tb_next(PageForEachNext tb,
  113. tb_page_addr_t start,
  114. tb_page_addr_t last)
  115. {
  116. IntervalTreeNode *n;
  117. if (tb) {
  118. n = interval_tree_iter_next(&tb->itree, start, last);
  119. if (n) {
  120. return container_of(n, TranslationBlock, itree);
  121. }
  122. }
  123. return NULL;
  124. }
  125. #else
  126. /*
  127. * In system mode we want L1_MAP to be based on ram offsets.
  128. */
  129. #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
  130. # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
  131. #else
  132. # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
  133. #endif
  134. /* Size of the L2 (and L3, etc) page tables. */
  135. #define V_L2_BITS 10
  136. #define V_L2_SIZE (1 << V_L2_BITS)
  137. /*
  138. * L1 Mapping properties
  139. */
  140. static int v_l1_size;
  141. static int v_l1_shift;
  142. static int v_l2_levels;
  143. /*
  144. * The bottom level has pointers to PageDesc, and is indexed by
  145. * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
  146. */
  147. #define V_L1_MIN_BITS 4
  148. #define V_L1_MAX_BITS (V_L2_BITS + 3)
  149. #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
  150. static void *l1_map[V_L1_MAX_SIZE];
  151. struct PageDesc {
  152. QemuSpin lock;
  153. /* list of TBs intersecting this ram page */
  154. uintptr_t first_tb;
  155. };
  156. void page_table_config_init(void)
  157. {
  158. uint32_t v_l1_bits;
  159. assert(TARGET_PAGE_BITS);
  160. /* The bits remaining after N lower levels of page tables. */
  161. v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
  162. if (v_l1_bits < V_L1_MIN_BITS) {
  163. v_l1_bits += V_L2_BITS;
  164. }
  165. v_l1_size = 1 << v_l1_bits;
  166. v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
  167. v_l2_levels = v_l1_shift / V_L2_BITS - 1;
  168. assert(v_l1_bits <= V_L1_MAX_BITS);
  169. assert(v_l1_shift % V_L2_BITS == 0);
  170. assert(v_l2_levels >= 0);
  171. }
  172. static PageDesc *page_find_alloc(tb_page_addr_t index, bool alloc)
  173. {
  174. PageDesc *pd;
  175. void **lp;
  176. /* Level 1. Always allocated. */
  177. lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
  178. /* Level 2..N-1. */
  179. for (int i = v_l2_levels; i > 0; i--) {
  180. void **p = qatomic_rcu_read(lp);
  181. if (p == NULL) {
  182. void *existing;
  183. if (!alloc) {
  184. return NULL;
  185. }
  186. p = g_new0(void *, V_L2_SIZE);
  187. existing = qatomic_cmpxchg(lp, NULL, p);
  188. if (unlikely(existing)) {
  189. g_free(p);
  190. p = existing;
  191. }
  192. }
  193. lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
  194. }
  195. pd = qatomic_rcu_read(lp);
  196. if (pd == NULL) {
  197. void *existing;
  198. if (!alloc) {
  199. return NULL;
  200. }
  201. pd = g_new0(PageDesc, V_L2_SIZE);
  202. for (int i = 0; i < V_L2_SIZE; i++) {
  203. qemu_spin_init(&pd[i].lock);
  204. }
  205. existing = qatomic_cmpxchg(lp, NULL, pd);
  206. if (unlikely(existing)) {
  207. for (int i = 0; i < V_L2_SIZE; i++) {
  208. qemu_spin_destroy(&pd[i].lock);
  209. }
  210. g_free(pd);
  211. pd = existing;
  212. }
  213. }
  214. return pd + (index & (V_L2_SIZE - 1));
  215. }
  216. static inline PageDesc *page_find(tb_page_addr_t index)
  217. {
  218. return page_find_alloc(index, false);
  219. }
  220. /**
  221. * struct page_entry - page descriptor entry
  222. * @pd: pointer to the &struct PageDesc of the page this entry represents
  223. * @index: page index of the page
  224. * @locked: whether the page is locked
  225. *
  226. * This struct helps us keep track of the locked state of a page, without
  227. * bloating &struct PageDesc.
  228. *
  229. * A page lock protects accesses to all fields of &struct PageDesc.
  230. *
  231. * See also: &struct page_collection.
  232. */
  233. struct page_entry {
  234. PageDesc *pd;
  235. tb_page_addr_t index;
  236. bool locked;
  237. };
  238. /**
  239. * struct page_collection - tracks a set of pages (i.e. &struct page_entry's)
  240. * @tree: Binary search tree (BST) of the pages, with key == page index
  241. * @max: Pointer to the page in @tree with the highest page index
  242. *
  243. * To avoid deadlock we lock pages in ascending order of page index.
  244. * When operating on a set of pages, we need to keep track of them so that
  245. * we can lock them in order and also unlock them later. For this we collect
  246. * pages (i.e. &struct page_entry's) in a binary search @tree. Given that the
  247. * @tree implementation we use does not provide an O(1) operation to obtain the
  248. * highest-ranked element, we use @max to keep track of the inserted page
  249. * with the highest index. This is valuable because if a page is not in
  250. * the tree and its index is higher than @max's, then we can lock it
  251. * without breaking the locking order rule.
  252. *
  253. * Note on naming: 'struct page_set' would be shorter, but we already have a few
  254. * page_set_*() helpers, so page_collection is used instead to avoid confusion.
  255. *
  256. * See also: page_collection_lock().
  257. */
  258. struct page_collection {
  259. QTree *tree;
  260. struct page_entry *max;
  261. };
  262. typedef int PageForEachNext;
  263. #define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
  264. TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)
  265. #ifdef CONFIG_DEBUG_TCG
  266. static __thread GHashTable *ht_pages_locked_debug;
  267. static void ht_pages_locked_debug_init(void)
  268. {
  269. if (ht_pages_locked_debug) {
  270. return;
  271. }
  272. ht_pages_locked_debug = g_hash_table_new(NULL, NULL);
  273. }
  274. static bool page_is_locked(const PageDesc *pd)
  275. {
  276. PageDesc *found;
  277. ht_pages_locked_debug_init();
  278. found = g_hash_table_lookup(ht_pages_locked_debug, pd);
  279. return !!found;
  280. }
  281. static void page_lock__debug(PageDesc *pd)
  282. {
  283. ht_pages_locked_debug_init();
  284. g_assert(!page_is_locked(pd));
  285. g_hash_table_insert(ht_pages_locked_debug, pd, pd);
  286. }
  287. static void page_unlock__debug(const PageDesc *pd)
  288. {
  289. bool removed;
  290. ht_pages_locked_debug_init();
  291. g_assert(page_is_locked(pd));
  292. removed = g_hash_table_remove(ht_pages_locked_debug, pd);
  293. g_assert(removed);
  294. }
  295. static void do_assert_page_locked(const PageDesc *pd,
  296. const char *file, int line)
  297. {
  298. if (unlikely(!page_is_locked(pd))) {
  299. error_report("assert_page_lock: PageDesc %p not locked @ %s:%d",
  300. pd, file, line);
  301. abort();
  302. }
  303. }
  304. #define assert_page_locked(pd) do_assert_page_locked(pd, __FILE__, __LINE__)
  305. void assert_no_pages_locked(void)
  306. {
  307. ht_pages_locked_debug_init();
  308. g_assert(g_hash_table_size(ht_pages_locked_debug) == 0);
  309. }
  310. #else /* !CONFIG_DEBUG_TCG */
  311. static inline void page_lock__debug(const PageDesc *pd) { }
  312. static inline void page_unlock__debug(const PageDesc *pd) { }
  313. static inline void assert_page_locked(const PageDesc *pd) { }
  314. #endif /* CONFIG_DEBUG_TCG */
  315. static void page_lock(PageDesc *pd)
  316. {
  317. page_lock__debug(pd);
  318. qemu_spin_lock(&pd->lock);
  319. }
  320. /* Like qemu_spin_trylock, returns false on success */
  321. static bool page_trylock(PageDesc *pd)
  322. {
  323. bool busy = qemu_spin_trylock(&pd->lock);
  324. if (!busy) {
  325. page_lock__debug(pd);
  326. }
  327. return busy;
  328. }
  329. static void page_unlock(PageDesc *pd)
  330. {
  331. qemu_spin_unlock(&pd->lock);
  332. page_unlock__debug(pd);
  333. }
  334. void tb_lock_page0(tb_page_addr_t paddr)
  335. {
  336. page_lock(page_find_alloc(paddr >> TARGET_PAGE_BITS, true));
  337. }
  338. void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
  339. {
  340. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  341. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  342. PageDesc *pd0, *pd1;
  343. if (pindex0 == pindex1) {
  344. /* Identical pages, and the first page is already locked. */
  345. return;
  346. }
  347. pd1 = page_find_alloc(pindex1, true);
  348. if (pindex0 < pindex1) {
  349. /* Correct locking order, we may block. */
  350. page_lock(pd1);
  351. return;
  352. }
  353. /* Incorrect locking order, we cannot block lest we deadlock. */
  354. if (!page_trylock(pd1)) {
  355. return;
  356. }
  357. /*
  358. * Drop the lock on page0 and get both page locks in the right order.
  359. * Restart translation via longjmp.
  360. */
  361. pd0 = page_find_alloc(pindex0, false);
  362. page_unlock(pd0);
  363. page_lock(pd1);
  364. page_lock(pd0);
  365. siglongjmp(tcg_ctx->jmp_trans, -3);
  366. }
  367. void tb_unlock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
  368. {
  369. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  370. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  371. if (pindex0 != pindex1) {
  372. page_unlock(page_find_alloc(pindex1, false));
  373. }
  374. }
  375. static void tb_lock_pages(TranslationBlock *tb)
  376. {
  377. tb_page_addr_t paddr0 = tb_page_addr0(tb);
  378. tb_page_addr_t paddr1 = tb_page_addr1(tb);
  379. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  380. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  381. if (unlikely(paddr0 == -1)) {
  382. return;
  383. }
  384. if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
  385. if (pindex0 < pindex1) {
  386. page_lock(page_find_alloc(pindex0, true));
  387. page_lock(page_find_alloc(pindex1, true));
  388. return;
  389. }
  390. page_lock(page_find_alloc(pindex1, true));
  391. }
  392. page_lock(page_find_alloc(pindex0, true));
  393. }
  394. void tb_unlock_pages(TranslationBlock *tb)
  395. {
  396. tb_page_addr_t paddr0 = tb_page_addr0(tb);
  397. tb_page_addr_t paddr1 = tb_page_addr1(tb);
  398. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  399. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  400. if (unlikely(paddr0 == -1)) {
  401. return;
  402. }
  403. if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
  404. page_unlock(page_find_alloc(pindex1, false));
  405. }
  406. page_unlock(page_find_alloc(pindex0, false));
  407. }
  408. static inline struct page_entry *
  409. page_entry_new(PageDesc *pd, tb_page_addr_t index)
  410. {
  411. struct page_entry *pe = g_malloc(sizeof(*pe));
  412. pe->index = index;
  413. pe->pd = pd;
  414. pe->locked = false;
  415. return pe;
  416. }
  417. static void page_entry_destroy(gpointer p)
  418. {
  419. struct page_entry *pe = p;
  420. g_assert(pe->locked);
  421. page_unlock(pe->pd);
  422. g_free(pe);
  423. }
  424. /* returns false on success */
  425. static bool page_entry_trylock(struct page_entry *pe)
  426. {
  427. bool busy = page_trylock(pe->pd);
  428. if (!busy) {
  429. g_assert(!pe->locked);
  430. pe->locked = true;
  431. }
  432. return busy;
  433. }
  434. static void do_page_entry_lock(struct page_entry *pe)
  435. {
  436. page_lock(pe->pd);
  437. g_assert(!pe->locked);
  438. pe->locked = true;
  439. }
  440. static gboolean page_entry_lock(gpointer key, gpointer value, gpointer data)
  441. {
  442. struct page_entry *pe = value;
  443. do_page_entry_lock(pe);
  444. return FALSE;
  445. }
  446. static gboolean page_entry_unlock(gpointer key, gpointer value, gpointer data)
  447. {
  448. struct page_entry *pe = value;
  449. if (pe->locked) {
  450. pe->locked = false;
  451. page_unlock(pe->pd);
  452. }
  453. return FALSE;
  454. }
  455. /*
  456. * Trylock a page, and if successful, add the page to a collection.
  457. * Returns true ("busy") if the page could not be locked; false otherwise.
  458. */
  459. static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)
  460. {
  461. tb_page_addr_t index = addr >> TARGET_PAGE_BITS;
  462. struct page_entry *pe;
  463. PageDesc *pd;
  464. pe = q_tree_lookup(set->tree, &index);
  465. if (pe) {
  466. return false;
  467. }
  468. pd = page_find(index);
  469. if (pd == NULL) {
  470. return false;
  471. }
  472. pe = page_entry_new(pd, index);
  473. q_tree_insert(set->tree, &pe->index, pe);
  474. /*
  475. * If this is either (1) the first insertion or (2) a page whose index
  476. * is higher than any other so far, just lock the page and move on.
  477. */
  478. if (set->max == NULL || pe->index > set->max->index) {
  479. set->max = pe;
  480. do_page_entry_lock(pe);
  481. return false;
  482. }
  483. /*
  484. * Try to acquire out-of-order lock; if busy, return busy so that we acquire
  485. * locks in order.
  486. */
  487. return page_entry_trylock(pe);
  488. }
  489. static gint tb_page_addr_cmp(gconstpointer ap, gconstpointer bp, gpointer udata)
  490. {
  491. tb_page_addr_t a = *(const tb_page_addr_t *)ap;
  492. tb_page_addr_t b = *(const tb_page_addr_t *)bp;
  493. if (a == b) {
  494. return 0;
  495. } else if (a < b) {
  496. return -1;
  497. }
  498. return 1;
  499. }
  500. /*
  501. * Lock a range of pages ([@start,@last]) as well as the pages of all
  502. * intersecting TBs.
  503. * Locking order: acquire locks in ascending order of page index.
  504. */
  505. static struct page_collection *page_collection_lock(tb_page_addr_t start,
  506. tb_page_addr_t last)
  507. {
  508. struct page_collection *set = g_malloc(sizeof(*set));
  509. tb_page_addr_t index;
  510. PageDesc *pd;
  511. start >>= TARGET_PAGE_BITS;
  512. last >>= TARGET_PAGE_BITS;
  513. g_assert(start <= last);
  514. set->tree = q_tree_new_full(tb_page_addr_cmp, NULL, NULL,
  515. page_entry_destroy);
  516. set->max = NULL;
  517. assert_no_pages_locked();
  518. retry:
  519. q_tree_foreach(set->tree, page_entry_lock, NULL);
  520. for (index = start; index <= last; index++) {
  521. TranslationBlock *tb;
  522. PageForEachNext n;
  523. pd = page_find(index);
  524. if (pd == NULL) {
  525. continue;
  526. }
  527. if (page_trylock_add(set, index << TARGET_PAGE_BITS)) {
  528. q_tree_foreach(set->tree, page_entry_unlock, NULL);
  529. goto retry;
  530. }
  531. assert_page_locked(pd);
  532. PAGE_FOR_EACH_TB(unused, unused, pd, tb, n) {
  533. if (page_trylock_add(set, tb_page_addr0(tb)) ||
  534. (tb_page_addr1(tb) != -1 &&
  535. page_trylock_add(set, tb_page_addr1(tb)))) {
  536. /* drop all locks, and reacquire in order */
  537. q_tree_foreach(set->tree, page_entry_unlock, NULL);
  538. goto retry;
  539. }
  540. }
  541. }
  542. return set;
  543. }
  544. static void page_collection_unlock(struct page_collection *set)
  545. {
  546. /* entries are unlocked and freed via page_entry_destroy */
  547. q_tree_destroy(set->tree);
  548. g_free(set);
  549. }
  550. /* Set to NULL all the 'first_tb' fields in all PageDescs. */
  551. static void tb_remove_all_1(int level, void **lp)
  552. {
  553. int i;
  554. if (*lp == NULL) {
  555. return;
  556. }
  557. if (level == 0) {
  558. PageDesc *pd = *lp;
  559. for (i = 0; i < V_L2_SIZE; ++i) {
  560. page_lock(&pd[i]);
  561. pd[i].first_tb = (uintptr_t)NULL;
  562. page_unlock(&pd[i]);
  563. }
  564. } else {
  565. void **pp = *lp;
  566. for (i = 0; i < V_L2_SIZE; ++i) {
  567. tb_remove_all_1(level - 1, pp + i);
  568. }
  569. }
  570. }
  571. static void tb_remove_all(void)
  572. {
  573. int i, l1_sz = v_l1_size;
  574. for (i = 0; i < l1_sz; i++) {
  575. tb_remove_all_1(v_l2_levels, l1_map + i);
  576. }
  577. }
  578. /*
  579. * Add the tb in the target page and protect it if necessary.
  580. * Called with @p->lock held.
  581. */
  582. static void tb_page_add(PageDesc *p, TranslationBlock *tb, unsigned int n)
  583. {
  584. bool page_already_protected;
  585. assert_page_locked(p);
  586. tb->page_next[n] = p->first_tb;
  587. page_already_protected = p->first_tb != 0;
  588. p->first_tb = (uintptr_t)tb | n;
  589. /*
  590. * If some code is already present, then the pages are already
  591. * protected. So we handle the case where only the first TB is
  592. * allocated in a physical page.
  593. */
  594. if (!page_already_protected) {
  595. tlb_protect_code(tb->page_addr[n] & TARGET_PAGE_MASK);
  596. }
  597. }
  598. static void tb_record(TranslationBlock *tb)
  599. {
  600. tb_page_addr_t paddr0 = tb_page_addr0(tb);
  601. tb_page_addr_t paddr1 = tb_page_addr1(tb);
  602. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  603. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  604. assert(paddr0 != -1);
  605. if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
  606. tb_page_add(page_find_alloc(pindex1, false), tb, 1);
  607. }
  608. tb_page_add(page_find_alloc(pindex0, false), tb, 0);
  609. }
  610. static void tb_page_remove(PageDesc *pd, TranslationBlock *tb)
  611. {
  612. TranslationBlock *tb1;
  613. uintptr_t *pprev;
  614. PageForEachNext n1;
  615. assert_page_locked(pd);
  616. pprev = &pd->first_tb;
  617. PAGE_FOR_EACH_TB(unused, unused, pd, tb1, n1) {
  618. if (tb1 == tb) {
  619. *pprev = tb1->page_next[n1];
  620. return;
  621. }
  622. pprev = &tb1->page_next[n1];
  623. }
  624. g_assert_not_reached();
  625. }
  626. static void tb_remove(TranslationBlock *tb)
  627. {
  628. tb_page_addr_t paddr0 = tb_page_addr0(tb);
  629. tb_page_addr_t paddr1 = tb_page_addr1(tb);
  630. tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
  631. tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
  632. assert(paddr0 != -1);
  633. if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
  634. tb_page_remove(page_find_alloc(pindex1, false), tb);
  635. }
  636. tb_page_remove(page_find_alloc(pindex0, false), tb);
  637. }
  638. #endif /* CONFIG_USER_ONLY */
  639. /* flush all the translation blocks */
  640. static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
  641. {
  642. bool did_flush = false;
  643. mmap_lock();
  644. /* If it is already been done on request of another CPU, just retry. */
  645. if (tb_ctx.tb_flush_count != tb_flush_count.host_int) {
  646. goto done;
  647. }
  648. did_flush = true;
  649. CPU_FOREACH(cpu) {
  650. tcg_flush_jmp_cache(cpu);
  651. }
  652. qht_reset_size(&tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
  653. tb_remove_all();
  654. tcg_region_reset_all();
  655. /* XXX: flush processor icache at this point if cache flush is expensive */
  656. qatomic_inc(&tb_ctx.tb_flush_count);
  657. done:
  658. mmap_unlock();
  659. if (did_flush) {
  660. qemu_plugin_flush_cb();
  661. }
  662. }
  663. void tb_flush(CPUState *cpu)
  664. {
  665. if (tcg_enabled()) {
  666. unsigned tb_flush_count = qatomic_read(&tb_ctx.tb_flush_count);
  667. if (cpu_in_serial_context(cpu)) {
  668. do_tb_flush(cpu, RUN_ON_CPU_HOST_INT(tb_flush_count));
  669. } else {
  670. async_safe_run_on_cpu(cpu, do_tb_flush,
  671. RUN_ON_CPU_HOST_INT(tb_flush_count));
  672. }
  673. }
  674. }
  675. /* remove @orig from its @n_orig-th jump list */
  676. static inline void tb_remove_from_jmp_list(TranslationBlock *orig, int n_orig)
  677. {
  678. uintptr_t ptr, ptr_locked;
  679. TranslationBlock *dest;
  680. TranslationBlock *tb;
  681. uintptr_t *pprev;
  682. int n;
  683. /* mark the LSB of jmp_dest[] so that no further jumps can be inserted */
  684. ptr = qatomic_or_fetch(&orig->jmp_dest[n_orig], 1);
  685. dest = (TranslationBlock *)(ptr & ~1);
  686. if (dest == NULL) {
  687. return;
  688. }
  689. qemu_spin_lock(&dest->jmp_lock);
  690. /*
  691. * While acquiring the lock, the jump might have been removed if the
  692. * destination TB was invalidated; check again.
  693. */
  694. ptr_locked = qatomic_read(&orig->jmp_dest[n_orig]);
  695. if (ptr_locked != ptr) {
  696. qemu_spin_unlock(&dest->jmp_lock);
  697. /*
  698. * The only possibility is that the jump was unlinked via
  699. * tb_jump_unlink(dest). Seeing here another destination would be a bug,
  700. * because we set the LSB above.
  701. */
  702. g_assert(ptr_locked == 1 && dest->cflags & CF_INVALID);
  703. return;
  704. }
  705. /*
  706. * We first acquired the lock, and since the destination pointer matches,
  707. * we know for sure that @orig is in the jmp list.
  708. */
  709. pprev = &dest->jmp_list_head;
  710. TB_FOR_EACH_JMP(dest, tb, n) {
  711. if (tb == orig && n == n_orig) {
  712. *pprev = tb->jmp_list_next[n];
  713. /* no need to set orig->jmp_dest[n]; setting the LSB was enough */
  714. qemu_spin_unlock(&dest->jmp_lock);
  715. return;
  716. }
  717. pprev = &tb->jmp_list_next[n];
  718. }
  719. g_assert_not_reached();
  720. }
  721. /*
  722. * Reset the jump entry 'n' of a TB so that it is not chained to another TB.
  723. */
  724. void tb_reset_jump(TranslationBlock *tb, int n)
  725. {
  726. uintptr_t addr = (uintptr_t)(tb->tc.ptr + tb->jmp_reset_offset[n]);
  727. tb_set_jmp_target(tb, n, addr);
  728. }
  729. /* remove any jumps to the TB */
  730. static inline void tb_jmp_unlink(TranslationBlock *dest)
  731. {
  732. TranslationBlock *tb;
  733. int n;
  734. qemu_spin_lock(&dest->jmp_lock);
  735. TB_FOR_EACH_JMP(dest, tb, n) {
  736. tb_reset_jump(tb, n);
  737. qatomic_and(&tb->jmp_dest[n], (uintptr_t)NULL | 1);
  738. /* No need to clear the list entry; setting the dest ptr is enough */
  739. }
  740. dest->jmp_list_head = (uintptr_t)NULL;
  741. qemu_spin_unlock(&dest->jmp_lock);
  742. }
  743. static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
  744. {
  745. CPUState *cpu;
  746. if (tb_cflags(tb) & CF_PCREL) {
  747. /* A TB may be at any virtual address */
  748. CPU_FOREACH(cpu) {
  749. tcg_flush_jmp_cache(cpu);
  750. }
  751. } else {
  752. uint32_t h = tb_jmp_cache_hash_func(tb->pc);
  753. CPU_FOREACH(cpu) {
  754. CPUJumpCache *jc = cpu->tb_jmp_cache;
  755. if (qatomic_read(&jc->array[h].tb) == tb) {
  756. qatomic_set(&jc->array[h].tb, NULL);
  757. }
  758. }
  759. }
  760. }
  761. /*
  762. * In user-mode, call with mmap_lock held.
  763. * In !user-mode, if @rm_from_page_list is set, call with the TB's pages'
  764. * locks held.
  765. */
  766. static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
  767. {
  768. uint32_t h;
  769. tb_page_addr_t phys_pc;
  770. uint32_t orig_cflags = tb_cflags(tb);
  771. assert_memory_lock();
  772. /* make sure no further incoming jumps will be chained to this TB */
  773. qemu_spin_lock(&tb->jmp_lock);
  774. qatomic_set(&tb->cflags, tb->cflags | CF_INVALID);
  775. qemu_spin_unlock(&tb->jmp_lock);
  776. /* remove the TB from the hash list */
  777. phys_pc = tb_page_addr0(tb);
  778. h = tb_hash_func(phys_pc, (orig_cflags & CF_PCREL ? 0 : tb->pc),
  779. tb->flags, tb->cs_base, orig_cflags);
  780. if (!qht_remove(&tb_ctx.htable, tb, h)) {
  781. return;
  782. }
  783. /* remove the TB from the page list */
  784. if (rm_from_page_list) {
  785. tb_remove(tb);
  786. }
  787. /* remove the TB from the hash list */
  788. tb_jmp_cache_inval_tb(tb);
  789. /* suppress this TB from the two jump lists */
  790. tb_remove_from_jmp_list(tb, 0);
  791. tb_remove_from_jmp_list(tb, 1);
  792. /* suppress any remaining jumps to this TB */
  793. tb_jmp_unlink(tb);
  794. qatomic_set(&tb_ctx.tb_phys_invalidate_count,
  795. tb_ctx.tb_phys_invalidate_count + 1);
  796. }
  797. static void tb_phys_invalidate__locked(TranslationBlock *tb)
  798. {
  799. qemu_thread_jit_write();
  800. do_tb_phys_invalidate(tb, true);
  801. qemu_thread_jit_execute();
  802. }
  803. /*
  804. * Invalidate one TB.
  805. * Called with mmap_lock held in user-mode.
  806. */
  807. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
  808. {
  809. if (page_addr == -1 && tb_page_addr0(tb) != -1) {
  810. tb_lock_pages(tb);
  811. do_tb_phys_invalidate(tb, true);
  812. tb_unlock_pages(tb);
  813. } else {
  814. do_tb_phys_invalidate(tb, false);
  815. }
  816. }
  817. /*
  818. * Add a new TB and link it to the physical page tables.
  819. * Called with mmap_lock held for user-mode emulation.
  820. *
  821. * Returns a pointer @tb, or a pointer to an existing TB that matches @tb.
  822. * Note that in !user-mode, another thread might have already added a TB
  823. * for the same block of guest code that @tb corresponds to. In that case,
  824. * the caller should discard the original @tb, and use instead the returned TB.
  825. */
  826. TranslationBlock *tb_link_page(TranslationBlock *tb)
  827. {
  828. void *existing_tb = NULL;
  829. uint32_t h;
  830. assert_memory_lock();
  831. tcg_debug_assert(!(tb->cflags & CF_INVALID));
  832. tb_record(tb);
  833. /* add in the hash table */
  834. h = tb_hash_func(tb_page_addr0(tb), (tb->cflags & CF_PCREL ? 0 : tb->pc),
  835. tb->flags, tb->cs_base, tb->cflags);
  836. qht_insert(&tb_ctx.htable, tb, h, &existing_tb);
  837. /* remove TB from the page(s) if we couldn't insert it */
  838. if (unlikely(existing_tb)) {
  839. tb_remove(tb);
  840. tb_unlock_pages(tb);
  841. return existing_tb;
  842. }
  843. tb_unlock_pages(tb);
  844. return tb;
  845. }
  846. #ifdef CONFIG_USER_ONLY
  847. /*
  848. * Invalidate all TBs which intersect with the target address range.
  849. * Called with mmap_lock held for user-mode emulation.
  850. * NOTE: this function must not be called while a TB is running.
  851. */
  852. void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
  853. {
  854. TranslationBlock *tb;
  855. PageForEachNext n;
  856. assert_memory_lock();
  857. PAGE_FOR_EACH_TB(start, last, unused, tb, n) {
  858. tb_phys_invalidate__locked(tb);
  859. }
  860. }
  861. /*
  862. * Invalidate all TBs which intersect with the target address page @addr.
  863. * Called with mmap_lock held for user-mode emulation
  864. * NOTE: this function must not be called while a TB is running.
  865. */
  866. static void tb_invalidate_phys_page(tb_page_addr_t addr)
  867. {
  868. tb_page_addr_t start, last;
  869. start = addr & TARGET_PAGE_MASK;
  870. last = addr | ~TARGET_PAGE_MASK;
  871. tb_invalidate_phys_range(start, last);
  872. }
  873. /*
  874. * Called with mmap_lock held. If pc is not 0 then it indicates the
  875. * host PC of the faulting store instruction that caused this invalidate.
  876. * Returns true if the caller needs to abort execution of the current
  877. * TB (because it was modified by this store and the guest CPU has
  878. * precise-SMC semantics).
  879. */
  880. bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
  881. {
  882. TranslationBlock *current_tb;
  883. bool current_tb_modified;
  884. TranslationBlock *tb;
  885. PageForEachNext n;
  886. tb_page_addr_t last;
  887. /*
  888. * Without precise smc semantics, or when outside of a TB,
  889. * we can skip to invalidate.
  890. */
  891. #ifndef TARGET_HAS_PRECISE_SMC
  892. pc = 0;
  893. #endif
  894. if (!pc) {
  895. tb_invalidate_phys_page(addr);
  896. return false;
  897. }
  898. assert_memory_lock();
  899. current_tb = tcg_tb_lookup(pc);
  900. last = addr | ~TARGET_PAGE_MASK;
  901. addr &= TARGET_PAGE_MASK;
  902. current_tb_modified = false;
  903. PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {
  904. if (current_tb == tb &&
  905. (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
  906. /*
  907. * If we are modifying the current TB, we must stop its
  908. * execution. We could be more precise by checking that
  909. * the modification is after the current PC, but it would
  910. * require a specialized function to partially restore
  911. * the CPU state.
  912. */
  913. current_tb_modified = true;
  914. cpu_restore_state_from_tb(current_cpu, current_tb, pc);
  915. }
  916. tb_phys_invalidate__locked(tb);
  917. }
  918. if (current_tb_modified) {
  919. /* Force execution of one insn next time. */
  920. CPUState *cpu = current_cpu;
  921. cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
  922. return true;
  923. }
  924. return false;
  925. }
  926. #else
  927. /*
  928. * @p must be non-NULL.
  929. * Call with all @pages locked.
  930. */
  931. static void
  932. tb_invalidate_phys_page_range__locked(struct page_collection *pages,
  933. PageDesc *p, tb_page_addr_t start,
  934. tb_page_addr_t last,
  935. uintptr_t retaddr)
  936. {
  937. TranslationBlock *tb;
  938. PageForEachNext n;
  939. #ifdef TARGET_HAS_PRECISE_SMC
  940. bool current_tb_modified = false;
  941. TranslationBlock *current_tb = retaddr ? tcg_tb_lookup(retaddr) : NULL;
  942. #endif /* TARGET_HAS_PRECISE_SMC */
  943. /* Range may not cross a page. */
  944. tcg_debug_assert(((start ^ last) & TARGET_PAGE_MASK) == 0);
  945. /*
  946. * We remove all the TBs in the range [start, last].
  947. * XXX: see if in some cases it could be faster to invalidate all the code
  948. */
  949. PAGE_FOR_EACH_TB(start, last, p, tb, n) {
  950. tb_page_addr_t tb_start, tb_last;
  951. /* NOTE: this is subtle as a TB may span two physical pages */
  952. tb_start = tb_page_addr0(tb);
  953. tb_last = tb_start + tb->size - 1;
  954. if (n == 0) {
  955. tb_last = MIN(tb_last, tb_start | ~TARGET_PAGE_MASK);
  956. } else {
  957. tb_start = tb_page_addr1(tb);
  958. tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK);
  959. }
  960. if (!(tb_last < start || tb_start > last)) {
  961. #ifdef TARGET_HAS_PRECISE_SMC
  962. if (current_tb == tb &&
  963. (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
  964. /*
  965. * If we are modifying the current TB, we must stop
  966. * its execution. We could be more precise by checking
  967. * that the modification is after the current PC, but it
  968. * would require a specialized function to partially
  969. * restore the CPU state.
  970. */
  971. current_tb_modified = true;
  972. cpu_restore_state_from_tb(current_cpu, current_tb, retaddr);
  973. }
  974. #endif /* TARGET_HAS_PRECISE_SMC */
  975. tb_phys_invalidate__locked(tb);
  976. }
  977. }
  978. /* if no code remaining, no need to continue to use slow writes */
  979. if (!p->first_tb) {
  980. tlb_unprotect_code(start);
  981. }
  982. #ifdef TARGET_HAS_PRECISE_SMC
  983. if (current_tb_modified) {
  984. page_collection_unlock(pages);
  985. /* Force execution of one insn next time. */
  986. current_cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
  987. mmap_unlock();
  988. cpu_loop_exit_noexc(current_cpu);
  989. }
  990. #endif
  991. }
  992. /*
  993. * Invalidate all TBs which intersect with the target physical address range
  994. * [start;last]. NOTE: start and end may refer to *different* physical pages.
  995. * 'is_cpu_write_access' should be true if called from a real cpu write
  996. * access: the virtual CPU will exit the current TB if code is modified inside
  997. * this TB.
  998. */
  999. void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
  1000. {
  1001. struct page_collection *pages;
  1002. tb_page_addr_t index, index_last;
  1003. pages = page_collection_lock(start, last);
  1004. index_last = last >> TARGET_PAGE_BITS;
  1005. for (index = start >> TARGET_PAGE_BITS; index <= index_last; index++) {
  1006. PageDesc *pd = page_find(index);
  1007. tb_page_addr_t page_start, page_last;
  1008. if (pd == NULL) {
  1009. continue;
  1010. }
  1011. assert_page_locked(pd);
  1012. page_start = index << TARGET_PAGE_BITS;
  1013. page_last = page_start | ~TARGET_PAGE_MASK;
  1014. page_last = MIN(page_last, last);
  1015. tb_invalidate_phys_page_range__locked(pages, pd,
  1016. page_start, page_last, 0);
  1017. }
  1018. page_collection_unlock(pages);
  1019. }
  1020. /*
  1021. * Call with all @pages in the range [@start, @start + len[ locked.
  1022. */
  1023. static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages,
  1024. tb_page_addr_t start,
  1025. unsigned len, uintptr_t ra)
  1026. {
  1027. PageDesc *p;
  1028. p = page_find(start >> TARGET_PAGE_BITS);
  1029. if (!p) {
  1030. return;
  1031. }
  1032. assert_page_locked(p);
  1033. tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1, ra);
  1034. }
  1035. /*
  1036. * len must be <= 8 and start must be a multiple of len.
  1037. * Called via softmmu_template.h when code areas are written to with
  1038. * iothread mutex not held.
  1039. */
  1040. void tb_invalidate_phys_range_fast(ram_addr_t ram_addr,
  1041. unsigned size,
  1042. uintptr_t retaddr)
  1043. {
  1044. struct page_collection *pages;
  1045. pages = page_collection_lock(ram_addr, ram_addr + size - 1);
  1046. tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr);
  1047. page_collection_unlock(pages);
  1048. }
  1049. #endif /* CONFIG_USER_ONLY */