translate-all.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /*
  2. * Host code generation
  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 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. #ifdef _WIN32
  20. #include <windows.h>
  21. #else
  22. #include <sys/types.h>
  23. #include <sys/mman.h>
  24. #endif
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <inttypes.h>
  30. #include "config.h"
  31. #include "qemu-common.h"
  32. #define NO_CPU_IO_DEFS
  33. #include "cpu.h"
  34. #include "disas/disas.h"
  35. #include "tcg.h"
  36. #include "qemu/timer.h"
  37. #include "exec/memory.h"
  38. #include "exec/address-spaces.h"
  39. #if defined(CONFIG_USER_ONLY)
  40. #include "qemu.h"
  41. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  42. #include <sys/param.h>
  43. #if __FreeBSD_version >= 700104
  44. #define HAVE_KINFO_GETVMMAP
  45. #define sigqueue sigqueue_freebsd /* avoid redefinition */
  46. #include <sys/time.h>
  47. #include <sys/proc.h>
  48. #include <machine/profile.h>
  49. #define _KERNEL
  50. #include <sys/user.h>
  51. #undef _KERNEL
  52. #undef sigqueue
  53. #include <libutil.h>
  54. #endif
  55. #endif
  56. #endif
  57. #include "exec/cputlb.h"
  58. #include "translate-all.h"
  59. //#define DEBUG_TB_INVALIDATE
  60. //#define DEBUG_FLUSH
  61. /* make various TB consistency checks */
  62. //#define DEBUG_TB_CHECK
  63. #if !defined(CONFIG_USER_ONLY)
  64. /* TB consistency checks only implemented for usermode emulation. */
  65. #undef DEBUG_TB_CHECK
  66. #endif
  67. #define SMC_BITMAP_USE_THRESHOLD 10
  68. /* Code generation and translation blocks */
  69. static TranslationBlock *tbs;
  70. static int code_gen_max_blocks;
  71. TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  72. static int nb_tbs;
  73. /* any access to the tbs or the page table must use this lock */
  74. spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
  75. uint8_t *code_gen_prologue;
  76. static uint8_t *code_gen_buffer;
  77. static size_t code_gen_buffer_size;
  78. /* threshold to flush the translated code buffer */
  79. static size_t code_gen_buffer_max_size;
  80. static uint8_t *code_gen_ptr;
  81. typedef struct PageDesc {
  82. /* list of TBs intersecting this ram page */
  83. TranslationBlock *first_tb;
  84. /* in order to optimize self modifying code, we count the number
  85. of lookups we do to a given page to use a bitmap */
  86. unsigned int code_write_count;
  87. uint8_t *code_bitmap;
  88. #if defined(CONFIG_USER_ONLY)
  89. unsigned long flags;
  90. #endif
  91. } PageDesc;
  92. /* In system mode we want L1_MAP to be based on ram offsets,
  93. while in user mode we want it to be based on virtual addresses. */
  94. #if !defined(CONFIG_USER_ONLY)
  95. #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
  96. # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
  97. #else
  98. # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
  99. #endif
  100. #else
  101. # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
  102. #endif
  103. /* The bits remaining after N lower levels of page tables. */
  104. #define V_L1_BITS_REM \
  105. ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
  106. #if V_L1_BITS_REM < 4
  107. #define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
  108. #else
  109. #define V_L1_BITS V_L1_BITS_REM
  110. #endif
  111. #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
  112. #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
  113. uintptr_t qemu_real_host_page_size;
  114. uintptr_t qemu_host_page_size;
  115. uintptr_t qemu_host_page_mask;
  116. /* This is a multi-level map on the virtual address space.
  117. The bottom level has pointers to PageDesc. */
  118. static void *l1_map[V_L1_SIZE];
  119. /* statistics */
  120. static int tb_flush_count;
  121. static int tb_phys_invalidate_count;
  122. /* code generation context */
  123. TCGContext tcg_ctx;
  124. static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
  125. tb_page_addr_t phys_page2);
  126. static TranslationBlock *tb_find_pc(uintptr_t tc_ptr);
  127. void cpu_gen_init(void)
  128. {
  129. tcg_context_init(&tcg_ctx);
  130. }
  131. /* return non zero if the very first instruction is invalid so that
  132. the virtual CPU can trigger an exception.
  133. '*gen_code_size_ptr' contains the size of the generated code (host
  134. code).
  135. */
  136. int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr)
  137. {
  138. TCGContext *s = &tcg_ctx;
  139. uint8_t *gen_code_buf;
  140. int gen_code_size;
  141. #ifdef CONFIG_PROFILER
  142. int64_t ti;
  143. #endif
  144. #ifdef CONFIG_PROFILER
  145. s->tb_count1++; /* includes aborted translations because of
  146. exceptions */
  147. ti = profile_getclock();
  148. #endif
  149. tcg_func_start(s);
  150. gen_intermediate_code(env, tb);
  151. /* generate machine code */
  152. gen_code_buf = tb->tc_ptr;
  153. tb->tb_next_offset[0] = 0xffff;
  154. tb->tb_next_offset[1] = 0xffff;
  155. s->tb_next_offset = tb->tb_next_offset;
  156. #ifdef USE_DIRECT_JUMP
  157. s->tb_jmp_offset = tb->tb_jmp_offset;
  158. s->tb_next = NULL;
  159. #else
  160. s->tb_jmp_offset = NULL;
  161. s->tb_next = tb->tb_next;
  162. #endif
  163. #ifdef CONFIG_PROFILER
  164. s->tb_count++;
  165. s->interm_time += profile_getclock() - ti;
  166. s->code_time -= profile_getclock();
  167. #endif
  168. gen_code_size = tcg_gen_code(s, gen_code_buf);
  169. *gen_code_size_ptr = gen_code_size;
  170. #ifdef CONFIG_PROFILER
  171. s->code_time += profile_getclock();
  172. s->code_in_len += tb->size;
  173. s->code_out_len += gen_code_size;
  174. #endif
  175. #ifdef DEBUG_DISAS
  176. if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
  177. qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
  178. log_disas(tb->tc_ptr, *gen_code_size_ptr);
  179. qemu_log("\n");
  180. qemu_log_flush();
  181. }
  182. #endif
  183. return 0;
  184. }
  185. /* The cpu state corresponding to 'searched_pc' is restored.
  186. */
  187. static int cpu_restore_state_from_tb(TranslationBlock *tb, CPUArchState *env,
  188. uintptr_t searched_pc)
  189. {
  190. TCGContext *s = &tcg_ctx;
  191. int j;
  192. uintptr_t tc_ptr;
  193. #ifdef CONFIG_PROFILER
  194. int64_t ti;
  195. #endif
  196. #ifdef CONFIG_PROFILER
  197. ti = profile_getclock();
  198. #endif
  199. tcg_func_start(s);
  200. gen_intermediate_code_pc(env, tb);
  201. if (use_icount) {
  202. /* Reset the cycle counter to the start of the block. */
  203. env->icount_decr.u16.low += tb->icount;
  204. /* Clear the IO flag. */
  205. env->can_do_io = 0;
  206. }
  207. /* find opc index corresponding to search_pc */
  208. tc_ptr = (uintptr_t)tb->tc_ptr;
  209. if (searched_pc < tc_ptr)
  210. return -1;
  211. s->tb_next_offset = tb->tb_next_offset;
  212. #ifdef USE_DIRECT_JUMP
  213. s->tb_jmp_offset = tb->tb_jmp_offset;
  214. s->tb_next = NULL;
  215. #else
  216. s->tb_jmp_offset = NULL;
  217. s->tb_next = tb->tb_next;
  218. #endif
  219. j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
  220. if (j < 0)
  221. return -1;
  222. /* now find start of instruction before */
  223. while (s->gen_opc_instr_start[j] == 0) {
  224. j--;
  225. }
  226. env->icount_decr.u16.low -= s->gen_opc_icount[j];
  227. restore_state_to_opc(env, tb, j);
  228. #ifdef CONFIG_PROFILER
  229. s->restore_time += profile_getclock() - ti;
  230. s->restore_count++;
  231. #endif
  232. return 0;
  233. }
  234. bool cpu_restore_state(CPUArchState *env, uintptr_t retaddr)
  235. {
  236. TranslationBlock *tb;
  237. tb = tb_find_pc(retaddr);
  238. if (tb) {
  239. cpu_restore_state_from_tb(tb, env, retaddr);
  240. return true;
  241. }
  242. return false;
  243. }
  244. #ifdef _WIN32
  245. static inline void map_exec(void *addr, long size)
  246. {
  247. DWORD old_protect;
  248. VirtualProtect(addr, size,
  249. PAGE_EXECUTE_READWRITE, &old_protect);
  250. }
  251. #else
  252. static inline void map_exec(void *addr, long size)
  253. {
  254. unsigned long start, end, page_size;
  255. page_size = getpagesize();
  256. start = (unsigned long)addr;
  257. start &= ~(page_size - 1);
  258. end = (unsigned long)addr + size;
  259. end += page_size - 1;
  260. end &= ~(page_size - 1);
  261. mprotect((void *)start, end - start,
  262. PROT_READ | PROT_WRITE | PROT_EXEC);
  263. }
  264. #endif
  265. static void page_init(void)
  266. {
  267. /* NOTE: we can always suppose that qemu_host_page_size >=
  268. TARGET_PAGE_SIZE */
  269. #ifdef _WIN32
  270. {
  271. SYSTEM_INFO system_info;
  272. GetSystemInfo(&system_info);
  273. qemu_real_host_page_size = system_info.dwPageSize;
  274. }
  275. #else
  276. qemu_real_host_page_size = getpagesize();
  277. #endif
  278. if (qemu_host_page_size == 0) {
  279. qemu_host_page_size = qemu_real_host_page_size;
  280. }
  281. if (qemu_host_page_size < TARGET_PAGE_SIZE) {
  282. qemu_host_page_size = TARGET_PAGE_SIZE;
  283. }
  284. qemu_host_page_mask = ~(qemu_host_page_size - 1);
  285. #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
  286. {
  287. #ifdef HAVE_KINFO_GETVMMAP
  288. struct kinfo_vmentry *freep;
  289. int i, cnt;
  290. freep = kinfo_getvmmap(getpid(), &cnt);
  291. if (freep) {
  292. mmap_lock();
  293. for (i = 0; i < cnt; i++) {
  294. unsigned long startaddr, endaddr;
  295. startaddr = freep[i].kve_start;
  296. endaddr = freep[i].kve_end;
  297. if (h2g_valid(startaddr)) {
  298. startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
  299. if (h2g_valid(endaddr)) {
  300. endaddr = h2g(endaddr);
  301. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  302. } else {
  303. #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
  304. endaddr = ~0ul;
  305. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  306. #endif
  307. }
  308. }
  309. }
  310. free(freep);
  311. mmap_unlock();
  312. }
  313. #else
  314. FILE *f;
  315. last_brk = (unsigned long)sbrk(0);
  316. f = fopen("/compat/linux/proc/self/maps", "r");
  317. if (f) {
  318. mmap_lock();
  319. do {
  320. unsigned long startaddr, endaddr;
  321. int n;
  322. n = fscanf(f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
  323. if (n == 2 && h2g_valid(startaddr)) {
  324. startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
  325. if (h2g_valid(endaddr)) {
  326. endaddr = h2g(endaddr);
  327. } else {
  328. endaddr = ~0ul;
  329. }
  330. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  331. }
  332. } while (!feof(f));
  333. fclose(f);
  334. mmap_unlock();
  335. }
  336. #endif
  337. }
  338. #endif
  339. }
  340. static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
  341. {
  342. PageDesc *pd;
  343. void **lp;
  344. int i;
  345. #if defined(CONFIG_USER_ONLY)
  346. /* We can't use g_malloc because it may recurse into a locked mutex. */
  347. # define ALLOC(P, SIZE) \
  348. do { \
  349. P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
  350. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
  351. } while (0)
  352. #else
  353. # define ALLOC(P, SIZE) \
  354. do { P = g_malloc0(SIZE); } while (0)
  355. #endif
  356. /* Level 1. Always allocated. */
  357. lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
  358. /* Level 2..N-1. */
  359. for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
  360. void **p = *lp;
  361. if (p == NULL) {
  362. if (!alloc) {
  363. return NULL;
  364. }
  365. ALLOC(p, sizeof(void *) * L2_SIZE);
  366. *lp = p;
  367. }
  368. lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
  369. }
  370. pd = *lp;
  371. if (pd == NULL) {
  372. if (!alloc) {
  373. return NULL;
  374. }
  375. ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
  376. *lp = pd;
  377. }
  378. #undef ALLOC
  379. return pd + (index & (L2_SIZE - 1));
  380. }
  381. static inline PageDesc *page_find(tb_page_addr_t index)
  382. {
  383. return page_find_alloc(index, 0);
  384. }
  385. #if !defined(CONFIG_USER_ONLY)
  386. #define mmap_lock() do { } while (0)
  387. #define mmap_unlock() do { } while (0)
  388. #endif
  389. #if defined(CONFIG_USER_ONLY)
  390. /* Currently it is not recommended to allocate big chunks of data in
  391. user mode. It will change when a dedicated libc will be used. */
  392. /* ??? 64-bit hosts ought to have no problem mmaping data outside the
  393. region in which the guest needs to run. Revisit this. */
  394. #define USE_STATIC_CODE_GEN_BUFFER
  395. #endif
  396. /* ??? Should configure for this, not list operating systems here. */
  397. #if (defined(__linux__) \
  398. || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
  399. || defined(__DragonFly__) || defined(__OpenBSD__) \
  400. || defined(__NetBSD__))
  401. # define USE_MMAP
  402. #endif
  403. /* Minimum size of the code gen buffer. This number is randomly chosen,
  404. but not so small that we can't have a fair number of TB's live. */
  405. #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024)
  406. /* Maximum size of the code gen buffer we'd like to use. Unless otherwise
  407. indicated, this is constrained by the range of direct branches on the
  408. host cpu, as used by the TCG implementation of goto_tb. */
  409. #if defined(__x86_64__)
  410. # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
  411. #elif defined(__sparc__)
  412. # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
  413. #elif defined(__arm__)
  414. # define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024)
  415. #elif defined(__s390x__)
  416. /* We have a +- 4GB range on the branches; leave some slop. */
  417. # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
  418. #else
  419. # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
  420. #endif
  421. #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024)
  422. #define DEFAULT_CODE_GEN_BUFFER_SIZE \
  423. (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
  424. ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
  425. static inline size_t size_code_gen_buffer(size_t tb_size)
  426. {
  427. /* Size the buffer. */
  428. if (tb_size == 0) {
  429. #ifdef USE_STATIC_CODE_GEN_BUFFER
  430. tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
  431. #else
  432. /* ??? Needs adjustments. */
  433. /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
  434. static buffer, we could size this on RESERVED_VA, on the text
  435. segment size of the executable, or continue to use the default. */
  436. tb_size = (unsigned long)(ram_size / 4);
  437. #endif
  438. }
  439. if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
  440. tb_size = MIN_CODE_GEN_BUFFER_SIZE;
  441. }
  442. if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
  443. tb_size = MAX_CODE_GEN_BUFFER_SIZE;
  444. }
  445. code_gen_buffer_size = tb_size;
  446. return tb_size;
  447. }
  448. #ifdef USE_STATIC_CODE_GEN_BUFFER
  449. static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
  450. __attribute__((aligned(CODE_GEN_ALIGN)));
  451. static inline void *alloc_code_gen_buffer(void)
  452. {
  453. map_exec(static_code_gen_buffer, code_gen_buffer_size);
  454. return static_code_gen_buffer;
  455. }
  456. #elif defined(USE_MMAP)
  457. static inline void *alloc_code_gen_buffer(void)
  458. {
  459. int flags = MAP_PRIVATE | MAP_ANONYMOUS;
  460. uintptr_t start = 0;
  461. void *buf;
  462. /* Constrain the position of the buffer based on the host cpu.
  463. Note that these addresses are chosen in concert with the
  464. addresses assigned in the relevant linker script file. */
  465. # if defined(__PIE__) || defined(__PIC__)
  466. /* Don't bother setting a preferred location if we're building
  467. a position-independent executable. We're more likely to get
  468. an address near the main executable if we let the kernel
  469. choose the address. */
  470. # elif defined(__x86_64__) && defined(MAP_32BIT)
  471. /* Force the memory down into low memory with the executable.
  472. Leave the choice of exact location with the kernel. */
  473. flags |= MAP_32BIT;
  474. /* Cannot expect to map more than 800MB in low memory. */
  475. if (code_gen_buffer_size > 800u * 1024 * 1024) {
  476. code_gen_buffer_size = 800u * 1024 * 1024;
  477. }
  478. # elif defined(__sparc__)
  479. start = 0x40000000ul;
  480. # elif defined(__s390x__)
  481. start = 0x90000000ul;
  482. # endif
  483. buf = mmap((void *)start, code_gen_buffer_size,
  484. PROT_WRITE | PROT_READ | PROT_EXEC, flags, -1, 0);
  485. return buf == MAP_FAILED ? NULL : buf;
  486. }
  487. #else
  488. static inline void *alloc_code_gen_buffer(void)
  489. {
  490. void *buf = g_malloc(code_gen_buffer_size);
  491. if (buf) {
  492. map_exec(buf, code_gen_buffer_size);
  493. }
  494. return buf;
  495. }
  496. #endif /* USE_STATIC_CODE_GEN_BUFFER, USE_MMAP */
  497. static inline void code_gen_alloc(size_t tb_size)
  498. {
  499. code_gen_buffer_size = size_code_gen_buffer(tb_size);
  500. code_gen_buffer = alloc_code_gen_buffer();
  501. if (code_gen_buffer == NULL) {
  502. fprintf(stderr, "Could not allocate dynamic translator buffer\n");
  503. exit(1);
  504. }
  505. qemu_madvise(code_gen_buffer, code_gen_buffer_size, QEMU_MADV_HUGEPAGE);
  506. /* Steal room for the prologue at the end of the buffer. This ensures
  507. (via the MAX_CODE_GEN_BUFFER_SIZE limits above) that direct branches
  508. from TB's to the prologue are going to be in range. It also means
  509. that we don't need to mark (additional) portions of the data segment
  510. as executable. */
  511. code_gen_prologue = code_gen_buffer + code_gen_buffer_size - 1024;
  512. code_gen_buffer_size -= 1024;
  513. code_gen_buffer_max_size = code_gen_buffer_size -
  514. (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
  515. code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
  516. tbs = g_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
  517. }
  518. /* Must be called before using the QEMU cpus. 'tb_size' is the size
  519. (in bytes) allocated to the translation buffer. Zero means default
  520. size. */
  521. void tcg_exec_init(unsigned long tb_size)
  522. {
  523. cpu_gen_init();
  524. code_gen_alloc(tb_size);
  525. code_gen_ptr = code_gen_buffer;
  526. tcg_register_jit(code_gen_buffer, code_gen_buffer_size);
  527. page_init();
  528. #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
  529. /* There's no guest base to take into account, so go ahead and
  530. initialize the prologue now. */
  531. tcg_prologue_init(&tcg_ctx);
  532. #endif
  533. }
  534. bool tcg_enabled(void)
  535. {
  536. return code_gen_buffer != NULL;
  537. }
  538. /* Allocate a new translation block. Flush the translation buffer if
  539. too many translation blocks or too much generated code. */
  540. static TranslationBlock *tb_alloc(target_ulong pc)
  541. {
  542. TranslationBlock *tb;
  543. if (nb_tbs >= code_gen_max_blocks ||
  544. (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) {
  545. return NULL;
  546. }
  547. tb = &tbs[nb_tbs++];
  548. tb->pc = pc;
  549. tb->cflags = 0;
  550. return tb;
  551. }
  552. void tb_free(TranslationBlock *tb)
  553. {
  554. /* In practice this is mostly used for single use temporary TB
  555. Ignore the hard cases and just back up if this TB happens to
  556. be the last one generated. */
  557. if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
  558. code_gen_ptr = tb->tc_ptr;
  559. nb_tbs--;
  560. }
  561. }
  562. static inline void invalidate_page_bitmap(PageDesc *p)
  563. {
  564. if (p->code_bitmap) {
  565. g_free(p->code_bitmap);
  566. p->code_bitmap = NULL;
  567. }
  568. p->code_write_count = 0;
  569. }
  570. /* Set to NULL all the 'first_tb' fields in all PageDescs. */
  571. static void page_flush_tb_1(int level, void **lp)
  572. {
  573. int i;
  574. if (*lp == NULL) {
  575. return;
  576. }
  577. if (level == 0) {
  578. PageDesc *pd = *lp;
  579. for (i = 0; i < L2_SIZE; ++i) {
  580. pd[i].first_tb = NULL;
  581. invalidate_page_bitmap(pd + i);
  582. }
  583. } else {
  584. void **pp = *lp;
  585. for (i = 0; i < L2_SIZE; ++i) {
  586. page_flush_tb_1(level - 1, pp + i);
  587. }
  588. }
  589. }
  590. static void page_flush_tb(void)
  591. {
  592. int i;
  593. for (i = 0; i < V_L1_SIZE; i++) {
  594. page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
  595. }
  596. }
  597. /* flush all the translation blocks */
  598. /* XXX: tb_flush is currently not thread safe */
  599. void tb_flush(CPUArchState *env1)
  600. {
  601. CPUArchState *env;
  602. #if defined(DEBUG_FLUSH)
  603. printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
  604. (unsigned long)(code_gen_ptr - code_gen_buffer),
  605. nb_tbs, nb_tbs > 0 ?
  606. ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
  607. #endif
  608. if ((unsigned long)(code_gen_ptr - code_gen_buffer)
  609. > code_gen_buffer_size) {
  610. cpu_abort(env1, "Internal error: code buffer overflow\n");
  611. }
  612. nb_tbs = 0;
  613. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  614. memset(env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof(void *));
  615. }
  616. memset(tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof(void *));
  617. page_flush_tb();
  618. code_gen_ptr = code_gen_buffer;
  619. /* XXX: flush processor icache at this point if cache flush is
  620. expensive */
  621. tb_flush_count++;
  622. }
  623. #ifdef DEBUG_TB_CHECK
  624. static void tb_invalidate_check(target_ulong address)
  625. {
  626. TranslationBlock *tb;
  627. int i;
  628. address &= TARGET_PAGE_MASK;
  629. for (i = 0; i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  630. for (tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  631. if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
  632. address >= tb->pc + tb->size)) {
  633. printf("ERROR invalidate: address=" TARGET_FMT_lx
  634. " PC=%08lx size=%04x\n",
  635. address, (long)tb->pc, tb->size);
  636. }
  637. }
  638. }
  639. }
  640. /* verify that all the pages have correct rights for code */
  641. static void tb_page_check(void)
  642. {
  643. TranslationBlock *tb;
  644. int i, flags1, flags2;
  645. for (i = 0; i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  646. for (tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  647. flags1 = page_get_flags(tb->pc);
  648. flags2 = page_get_flags(tb->pc + tb->size - 1);
  649. if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
  650. printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
  651. (long)tb->pc, tb->size, flags1, flags2);
  652. }
  653. }
  654. }
  655. }
  656. #endif
  657. static inline void tb_hash_remove(TranslationBlock **ptb, TranslationBlock *tb)
  658. {
  659. TranslationBlock *tb1;
  660. for (;;) {
  661. tb1 = *ptb;
  662. if (tb1 == tb) {
  663. *ptb = tb1->phys_hash_next;
  664. break;
  665. }
  666. ptb = &tb1->phys_hash_next;
  667. }
  668. }
  669. static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
  670. {
  671. TranslationBlock *tb1;
  672. unsigned int n1;
  673. for (;;) {
  674. tb1 = *ptb;
  675. n1 = (uintptr_t)tb1 & 3;
  676. tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
  677. if (tb1 == tb) {
  678. *ptb = tb1->page_next[n1];
  679. break;
  680. }
  681. ptb = &tb1->page_next[n1];
  682. }
  683. }
  684. static inline void tb_jmp_remove(TranslationBlock *tb, int n)
  685. {
  686. TranslationBlock *tb1, **ptb;
  687. unsigned int n1;
  688. ptb = &tb->jmp_next[n];
  689. tb1 = *ptb;
  690. if (tb1) {
  691. /* find tb(n) in circular list */
  692. for (;;) {
  693. tb1 = *ptb;
  694. n1 = (uintptr_t)tb1 & 3;
  695. tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
  696. if (n1 == n && tb1 == tb) {
  697. break;
  698. }
  699. if (n1 == 2) {
  700. ptb = &tb1->jmp_first;
  701. } else {
  702. ptb = &tb1->jmp_next[n1];
  703. }
  704. }
  705. /* now we can suppress tb(n) from the list */
  706. *ptb = tb->jmp_next[n];
  707. tb->jmp_next[n] = NULL;
  708. }
  709. }
  710. /* reset the jump entry 'n' of a TB so that it is not chained to
  711. another TB */
  712. static inline void tb_reset_jump(TranslationBlock *tb, int n)
  713. {
  714. tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
  715. }
  716. /* invalidate one TB */
  717. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
  718. {
  719. CPUArchState *env;
  720. PageDesc *p;
  721. unsigned int h, n1;
  722. tb_page_addr_t phys_pc;
  723. TranslationBlock *tb1, *tb2;
  724. /* remove the TB from the hash list */
  725. phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  726. h = tb_phys_hash_func(phys_pc);
  727. tb_hash_remove(&tb_phys_hash[h], tb);
  728. /* remove the TB from the page list */
  729. if (tb->page_addr[0] != page_addr) {
  730. p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
  731. tb_page_remove(&p->first_tb, tb);
  732. invalidate_page_bitmap(p);
  733. }
  734. if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
  735. p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
  736. tb_page_remove(&p->first_tb, tb);
  737. invalidate_page_bitmap(p);
  738. }
  739. tb_invalidated_flag = 1;
  740. /* remove the TB from the hash list */
  741. h = tb_jmp_cache_hash_func(tb->pc);
  742. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  743. if (env->tb_jmp_cache[h] == tb) {
  744. env->tb_jmp_cache[h] = NULL;
  745. }
  746. }
  747. /* suppress this TB from the two jump lists */
  748. tb_jmp_remove(tb, 0);
  749. tb_jmp_remove(tb, 1);
  750. /* suppress any remaining jumps to this TB */
  751. tb1 = tb->jmp_first;
  752. for (;;) {
  753. n1 = (uintptr_t)tb1 & 3;
  754. if (n1 == 2) {
  755. break;
  756. }
  757. tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
  758. tb2 = tb1->jmp_next[n1];
  759. tb_reset_jump(tb1, n1);
  760. tb1->jmp_next[n1] = NULL;
  761. tb1 = tb2;
  762. }
  763. tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2); /* fail safe */
  764. tb_phys_invalidate_count++;
  765. }
  766. static inline void set_bits(uint8_t *tab, int start, int len)
  767. {
  768. int end, mask, end1;
  769. end = start + len;
  770. tab += start >> 3;
  771. mask = 0xff << (start & 7);
  772. if ((start & ~7) == (end & ~7)) {
  773. if (start < end) {
  774. mask &= ~(0xff << (end & 7));
  775. *tab |= mask;
  776. }
  777. } else {
  778. *tab++ |= mask;
  779. start = (start + 8) & ~7;
  780. end1 = end & ~7;
  781. while (start < end1) {
  782. *tab++ = 0xff;
  783. start += 8;
  784. }
  785. if (start < end) {
  786. mask = ~(0xff << (end & 7));
  787. *tab |= mask;
  788. }
  789. }
  790. }
  791. static void build_page_bitmap(PageDesc *p)
  792. {
  793. int n, tb_start, tb_end;
  794. TranslationBlock *tb;
  795. p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
  796. tb = p->first_tb;
  797. while (tb != NULL) {
  798. n = (uintptr_t)tb & 3;
  799. tb = (TranslationBlock *)((uintptr_t)tb & ~3);
  800. /* NOTE: this is subtle as a TB may span two physical pages */
  801. if (n == 0) {
  802. /* NOTE: tb_end may be after the end of the page, but
  803. it is not a problem */
  804. tb_start = tb->pc & ~TARGET_PAGE_MASK;
  805. tb_end = tb_start + tb->size;
  806. if (tb_end > TARGET_PAGE_SIZE) {
  807. tb_end = TARGET_PAGE_SIZE;
  808. }
  809. } else {
  810. tb_start = 0;
  811. tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  812. }
  813. set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
  814. tb = tb->page_next[n];
  815. }
  816. }
  817. TranslationBlock *tb_gen_code(CPUArchState *env,
  818. target_ulong pc, target_ulong cs_base,
  819. int flags, int cflags)
  820. {
  821. TranslationBlock *tb;
  822. uint8_t *tc_ptr;
  823. tb_page_addr_t phys_pc, phys_page2;
  824. target_ulong virt_page2;
  825. int code_gen_size;
  826. phys_pc = get_page_addr_code(env, pc);
  827. tb = tb_alloc(pc);
  828. if (!tb) {
  829. /* flush must be done */
  830. tb_flush(env);
  831. /* cannot fail at this point */
  832. tb = tb_alloc(pc);
  833. /* Don't forget to invalidate previous TB info. */
  834. tb_invalidated_flag = 1;
  835. }
  836. tc_ptr = code_gen_ptr;
  837. tb->tc_ptr = tc_ptr;
  838. tb->cs_base = cs_base;
  839. tb->flags = flags;
  840. tb->cflags = cflags;
  841. cpu_gen_code(env, tb, &code_gen_size);
  842. code_gen_ptr = (void *)(((uintptr_t)code_gen_ptr + code_gen_size +
  843. CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
  844. /* check next page if needed */
  845. virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
  846. phys_page2 = -1;
  847. if ((pc & TARGET_PAGE_MASK) != virt_page2) {
  848. phys_page2 = get_page_addr_code(env, virt_page2);
  849. }
  850. tb_link_page(tb, phys_pc, phys_page2);
  851. return tb;
  852. }
  853. /*
  854. * Invalidate all TBs which intersect with the target physical address range
  855. * [start;end[. NOTE: start and end may refer to *different* physical pages.
  856. * 'is_cpu_write_access' should be true if called from a real cpu write
  857. * access: the virtual CPU will exit the current TB if code is modified inside
  858. * this TB.
  859. */
  860. void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
  861. int is_cpu_write_access)
  862. {
  863. while (start < end) {
  864. tb_invalidate_phys_page_range(start, end, is_cpu_write_access);
  865. start &= TARGET_PAGE_MASK;
  866. start += TARGET_PAGE_SIZE;
  867. }
  868. }
  869. /*
  870. * Invalidate all TBs which intersect with the target physical address range
  871. * [start;end[. NOTE: start and end must refer to the *same* physical page.
  872. * 'is_cpu_write_access' should be true if called from a real cpu write
  873. * access: the virtual CPU will exit the current TB if code is modified inside
  874. * this TB.
  875. */
  876. void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
  877. int is_cpu_write_access)
  878. {
  879. TranslationBlock *tb, *tb_next, *saved_tb;
  880. CPUArchState *env = cpu_single_env;
  881. tb_page_addr_t tb_start, tb_end;
  882. PageDesc *p;
  883. int n;
  884. #ifdef TARGET_HAS_PRECISE_SMC
  885. int current_tb_not_found = is_cpu_write_access;
  886. TranslationBlock *current_tb = NULL;
  887. int current_tb_modified = 0;
  888. target_ulong current_pc = 0;
  889. target_ulong current_cs_base = 0;
  890. int current_flags = 0;
  891. #endif /* TARGET_HAS_PRECISE_SMC */
  892. p = page_find(start >> TARGET_PAGE_BITS);
  893. if (!p) {
  894. return;
  895. }
  896. if (!p->code_bitmap &&
  897. ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
  898. is_cpu_write_access) {
  899. /* build code bitmap */
  900. build_page_bitmap(p);
  901. }
  902. /* we remove all the TBs in the range [start, end[ */
  903. /* XXX: see if in some cases it could be faster to invalidate all
  904. the code */
  905. tb = p->first_tb;
  906. while (tb != NULL) {
  907. n = (uintptr_t)tb & 3;
  908. tb = (TranslationBlock *)((uintptr_t)tb & ~3);
  909. tb_next = tb->page_next[n];
  910. /* NOTE: this is subtle as a TB may span two physical pages */
  911. if (n == 0) {
  912. /* NOTE: tb_end may be after the end of the page, but
  913. it is not a problem */
  914. tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  915. tb_end = tb_start + tb->size;
  916. } else {
  917. tb_start = tb->page_addr[1];
  918. tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  919. }
  920. if (!(tb_end <= start || tb_start >= end)) {
  921. #ifdef TARGET_HAS_PRECISE_SMC
  922. if (current_tb_not_found) {
  923. current_tb_not_found = 0;
  924. current_tb = NULL;
  925. if (env->mem_io_pc) {
  926. /* now we have a real cpu fault */
  927. current_tb = tb_find_pc(env->mem_io_pc);
  928. }
  929. }
  930. if (current_tb == tb &&
  931. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  932. /* If we are modifying the current TB, we must stop
  933. its execution. We could be more precise by checking
  934. that the modification is after the current PC, but it
  935. would require a specialized function to partially
  936. restore the CPU state */
  937. current_tb_modified = 1;
  938. cpu_restore_state_from_tb(current_tb, env, env->mem_io_pc);
  939. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  940. &current_flags);
  941. }
  942. #endif /* TARGET_HAS_PRECISE_SMC */
  943. /* we need to do that to handle the case where a signal
  944. occurs while doing tb_phys_invalidate() */
  945. saved_tb = NULL;
  946. if (env) {
  947. saved_tb = env->current_tb;
  948. env->current_tb = NULL;
  949. }
  950. tb_phys_invalidate(tb, -1);
  951. if (env) {
  952. env->current_tb = saved_tb;
  953. if (env->interrupt_request && env->current_tb) {
  954. cpu_interrupt(env, env->interrupt_request);
  955. }
  956. }
  957. }
  958. tb = tb_next;
  959. }
  960. #if !defined(CONFIG_USER_ONLY)
  961. /* if no code remaining, no need to continue to use slow writes */
  962. if (!p->first_tb) {
  963. invalidate_page_bitmap(p);
  964. if (is_cpu_write_access) {
  965. tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
  966. }
  967. }
  968. #endif
  969. #ifdef TARGET_HAS_PRECISE_SMC
  970. if (current_tb_modified) {
  971. /* we generate a block containing just the instruction
  972. modifying the memory. It will ensure that it cannot modify
  973. itself */
  974. env->current_tb = NULL;
  975. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  976. cpu_resume_from_signal(env, NULL);
  977. }
  978. #endif
  979. }
  980. /* len must be <= 8 and start must be a multiple of len */
  981. void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
  982. {
  983. PageDesc *p;
  984. int offset, b;
  985. #if 0
  986. if (1) {
  987. qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
  988. cpu_single_env->mem_io_vaddr, len,
  989. cpu_single_env->eip,
  990. cpu_single_env->eip +
  991. (intptr_t)cpu_single_env->segs[R_CS].base);
  992. }
  993. #endif
  994. p = page_find(start >> TARGET_PAGE_BITS);
  995. if (!p) {
  996. return;
  997. }
  998. if (p->code_bitmap) {
  999. offset = start & ~TARGET_PAGE_MASK;
  1000. b = p->code_bitmap[offset >> 3] >> (offset & 7);
  1001. if (b & ((1 << len) - 1)) {
  1002. goto do_invalidate;
  1003. }
  1004. } else {
  1005. do_invalidate:
  1006. tb_invalidate_phys_page_range(start, start + len, 1);
  1007. }
  1008. }
  1009. #if !defined(CONFIG_SOFTMMU)
  1010. static void tb_invalidate_phys_page(tb_page_addr_t addr,
  1011. uintptr_t pc, void *puc)
  1012. {
  1013. TranslationBlock *tb;
  1014. PageDesc *p;
  1015. int n;
  1016. #ifdef TARGET_HAS_PRECISE_SMC
  1017. TranslationBlock *current_tb = NULL;
  1018. CPUArchState *env = cpu_single_env;
  1019. int current_tb_modified = 0;
  1020. target_ulong current_pc = 0;
  1021. target_ulong current_cs_base = 0;
  1022. int current_flags = 0;
  1023. #endif
  1024. addr &= TARGET_PAGE_MASK;
  1025. p = page_find(addr >> TARGET_PAGE_BITS);
  1026. if (!p) {
  1027. return;
  1028. }
  1029. tb = p->first_tb;
  1030. #ifdef TARGET_HAS_PRECISE_SMC
  1031. if (tb && pc != 0) {
  1032. current_tb = tb_find_pc(pc);
  1033. }
  1034. #endif
  1035. while (tb != NULL) {
  1036. n = (uintptr_t)tb & 3;
  1037. tb = (TranslationBlock *)((uintptr_t)tb & ~3);
  1038. #ifdef TARGET_HAS_PRECISE_SMC
  1039. if (current_tb == tb &&
  1040. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  1041. /* If we are modifying the current TB, we must stop
  1042. its execution. We could be more precise by checking
  1043. that the modification is after the current PC, but it
  1044. would require a specialized function to partially
  1045. restore the CPU state */
  1046. current_tb_modified = 1;
  1047. cpu_restore_state_from_tb(current_tb, env, pc);
  1048. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  1049. &current_flags);
  1050. }
  1051. #endif /* TARGET_HAS_PRECISE_SMC */
  1052. tb_phys_invalidate(tb, addr);
  1053. tb = tb->page_next[n];
  1054. }
  1055. p->first_tb = NULL;
  1056. #ifdef TARGET_HAS_PRECISE_SMC
  1057. if (current_tb_modified) {
  1058. /* we generate a block containing just the instruction
  1059. modifying the memory. It will ensure that it cannot modify
  1060. itself */
  1061. env->current_tb = NULL;
  1062. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  1063. cpu_resume_from_signal(env, puc);
  1064. }
  1065. #endif
  1066. }
  1067. #endif
  1068. /* add the tb in the target page and protect it if necessary */
  1069. static inline void tb_alloc_page(TranslationBlock *tb,
  1070. unsigned int n, tb_page_addr_t page_addr)
  1071. {
  1072. PageDesc *p;
  1073. #ifndef CONFIG_USER_ONLY
  1074. bool page_already_protected;
  1075. #endif
  1076. tb->page_addr[n] = page_addr;
  1077. p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
  1078. tb->page_next[n] = p->first_tb;
  1079. #ifndef CONFIG_USER_ONLY
  1080. page_already_protected = p->first_tb != NULL;
  1081. #endif
  1082. p->first_tb = (TranslationBlock *)((uintptr_t)tb | n);
  1083. invalidate_page_bitmap(p);
  1084. #if defined(TARGET_HAS_SMC) || 1
  1085. #if defined(CONFIG_USER_ONLY)
  1086. if (p->flags & PAGE_WRITE) {
  1087. target_ulong addr;
  1088. PageDesc *p2;
  1089. int prot;
  1090. /* force the host page as non writable (writes will have a
  1091. page fault + mprotect overhead) */
  1092. page_addr &= qemu_host_page_mask;
  1093. prot = 0;
  1094. for (addr = page_addr; addr < page_addr + qemu_host_page_size;
  1095. addr += TARGET_PAGE_SIZE) {
  1096. p2 = page_find(addr >> TARGET_PAGE_BITS);
  1097. if (!p2) {
  1098. continue;
  1099. }
  1100. prot |= p2->flags;
  1101. p2->flags &= ~PAGE_WRITE;
  1102. }
  1103. mprotect(g2h(page_addr), qemu_host_page_size,
  1104. (prot & PAGE_BITS) & ~PAGE_WRITE);
  1105. #ifdef DEBUG_TB_INVALIDATE
  1106. printf("protecting code page: 0x" TARGET_FMT_lx "\n",
  1107. page_addr);
  1108. #endif
  1109. }
  1110. #else
  1111. /* if some code is already present, then the pages are already
  1112. protected. So we handle the case where only the first TB is
  1113. allocated in a physical page */
  1114. if (!page_already_protected) {
  1115. tlb_protect_code(page_addr);
  1116. }
  1117. #endif
  1118. #endif /* TARGET_HAS_SMC */
  1119. }
  1120. /* add a new TB and link it to the physical page tables. phys_page2 is
  1121. (-1) to indicate that only one page contains the TB. */
  1122. static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
  1123. tb_page_addr_t phys_page2)
  1124. {
  1125. unsigned int h;
  1126. TranslationBlock **ptb;
  1127. /* Grab the mmap lock to stop another thread invalidating this TB
  1128. before we are done. */
  1129. mmap_lock();
  1130. /* add in the physical hash table */
  1131. h = tb_phys_hash_func(phys_pc);
  1132. ptb = &tb_phys_hash[h];
  1133. tb->phys_hash_next = *ptb;
  1134. *ptb = tb;
  1135. /* add in the page list */
  1136. tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
  1137. if (phys_page2 != -1) {
  1138. tb_alloc_page(tb, 1, phys_page2);
  1139. } else {
  1140. tb->page_addr[1] = -1;
  1141. }
  1142. tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2);
  1143. tb->jmp_next[0] = NULL;
  1144. tb->jmp_next[1] = NULL;
  1145. /* init original jump addresses */
  1146. if (tb->tb_next_offset[0] != 0xffff) {
  1147. tb_reset_jump(tb, 0);
  1148. }
  1149. if (tb->tb_next_offset[1] != 0xffff) {
  1150. tb_reset_jump(tb, 1);
  1151. }
  1152. #ifdef DEBUG_TB_CHECK
  1153. tb_page_check();
  1154. #endif
  1155. mmap_unlock();
  1156. }
  1157. #if defined(CONFIG_QEMU_LDST_OPTIMIZATION) && defined(CONFIG_SOFTMMU)
  1158. /* check whether the given addr is in TCG generated code buffer or not */
  1159. bool is_tcg_gen_code(uintptr_t tc_ptr)
  1160. {
  1161. /* This can be called during code generation, code_gen_buffer_size
  1162. is used instead of code_gen_ptr for upper boundary checking */
  1163. return (tc_ptr >= (uintptr_t)code_gen_buffer &&
  1164. tc_ptr < (uintptr_t)(code_gen_buffer + code_gen_buffer_size));
  1165. }
  1166. #endif
  1167. /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
  1168. tb[1].tc_ptr. Return NULL if not found */
  1169. static TranslationBlock *tb_find_pc(uintptr_t tc_ptr)
  1170. {
  1171. int m_min, m_max, m;
  1172. uintptr_t v;
  1173. TranslationBlock *tb;
  1174. if (nb_tbs <= 0) {
  1175. return NULL;
  1176. }
  1177. if (tc_ptr < (uintptr_t)code_gen_buffer ||
  1178. tc_ptr >= (uintptr_t)code_gen_ptr) {
  1179. return NULL;
  1180. }
  1181. /* binary search (cf Knuth) */
  1182. m_min = 0;
  1183. m_max = nb_tbs - 1;
  1184. while (m_min <= m_max) {
  1185. m = (m_min + m_max) >> 1;
  1186. tb = &tbs[m];
  1187. v = (uintptr_t)tb->tc_ptr;
  1188. if (v == tc_ptr) {
  1189. return tb;
  1190. } else if (tc_ptr < v) {
  1191. m_max = m - 1;
  1192. } else {
  1193. m_min = m + 1;
  1194. }
  1195. }
  1196. return &tbs[m_max];
  1197. }
  1198. #if defined(TARGET_HAS_ICE) && !defined(CONFIG_USER_ONLY)
  1199. void tb_invalidate_phys_addr(hwaddr addr)
  1200. {
  1201. ram_addr_t ram_addr;
  1202. MemoryRegionSection *section;
  1203. section = phys_page_find(address_space_memory.dispatch,
  1204. addr >> TARGET_PAGE_BITS);
  1205. if (!(memory_region_is_ram(section->mr)
  1206. || (section->mr->rom_device && section->mr->readable))) {
  1207. return;
  1208. }
  1209. ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
  1210. + memory_region_section_addr(section, addr);
  1211. tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
  1212. }
  1213. #endif /* TARGET_HAS_ICE && !defined(CONFIG_USER_ONLY) */
  1214. void tb_check_watchpoint(CPUArchState *env)
  1215. {
  1216. TranslationBlock *tb;
  1217. tb = tb_find_pc(env->mem_io_pc);
  1218. if (!tb) {
  1219. cpu_abort(env, "check_watchpoint: could not find TB for pc=%p",
  1220. (void *)env->mem_io_pc);
  1221. }
  1222. cpu_restore_state_from_tb(tb, env, env->mem_io_pc);
  1223. tb_phys_invalidate(tb, -1);
  1224. }
  1225. #ifndef CONFIG_USER_ONLY
  1226. /* mask must never be zero, except for A20 change call */
  1227. static void tcg_handle_interrupt(CPUArchState *env, int mask)
  1228. {
  1229. CPUState *cpu = ENV_GET_CPU(env);
  1230. int old_mask;
  1231. old_mask = env->interrupt_request;
  1232. env->interrupt_request |= mask;
  1233. /*
  1234. * If called from iothread context, wake the target cpu in
  1235. * case its halted.
  1236. */
  1237. if (!qemu_cpu_is_self(cpu)) {
  1238. qemu_cpu_kick(cpu);
  1239. return;
  1240. }
  1241. if (use_icount) {
  1242. env->icount_decr.u16.high = 0xffff;
  1243. if (!can_do_io(env)
  1244. && (mask & ~old_mask) != 0) {
  1245. cpu_abort(env, "Raised interrupt while not in I/O function");
  1246. }
  1247. } else {
  1248. env->tcg_exit_req = 1;
  1249. }
  1250. }
  1251. CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
  1252. /* in deterministic execution mode, instructions doing device I/Os
  1253. must be at the end of the TB */
  1254. void cpu_io_recompile(CPUArchState *env, uintptr_t retaddr)
  1255. {
  1256. TranslationBlock *tb;
  1257. uint32_t n, cflags;
  1258. target_ulong pc, cs_base;
  1259. uint64_t flags;
  1260. tb = tb_find_pc(retaddr);
  1261. if (!tb) {
  1262. cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
  1263. (void *)retaddr);
  1264. }
  1265. n = env->icount_decr.u16.low + tb->icount;
  1266. cpu_restore_state_from_tb(tb, env, retaddr);
  1267. /* Calculate how many instructions had been executed before the fault
  1268. occurred. */
  1269. n = n - env->icount_decr.u16.low;
  1270. /* Generate a new TB ending on the I/O insn. */
  1271. n++;
  1272. /* On MIPS and SH, delay slot instructions can only be restarted if
  1273. they were already the first instruction in the TB. If this is not
  1274. the first instruction in a TB then re-execute the preceding
  1275. branch. */
  1276. #if defined(TARGET_MIPS)
  1277. if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
  1278. env->active_tc.PC -= 4;
  1279. env->icount_decr.u16.low++;
  1280. env->hflags &= ~MIPS_HFLAG_BMASK;
  1281. }
  1282. #elif defined(TARGET_SH4)
  1283. if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
  1284. && n > 1) {
  1285. env->pc -= 2;
  1286. env->icount_decr.u16.low++;
  1287. env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
  1288. }
  1289. #endif
  1290. /* This should never happen. */
  1291. if (n > CF_COUNT_MASK) {
  1292. cpu_abort(env, "TB too big during recompile");
  1293. }
  1294. cflags = n | CF_LAST_IO;
  1295. pc = tb->pc;
  1296. cs_base = tb->cs_base;
  1297. flags = tb->flags;
  1298. tb_phys_invalidate(tb, -1);
  1299. /* FIXME: In theory this could raise an exception. In practice
  1300. we have already translated the block once so it's probably ok. */
  1301. tb_gen_code(env, pc, cs_base, flags, cflags);
  1302. /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
  1303. the first in the TB) then we end up generating a whole new TB and
  1304. repeating the fault, which is horribly inefficient.
  1305. Better would be to execute just this insn uncached, or generate a
  1306. second new TB. */
  1307. cpu_resume_from_signal(env, NULL);
  1308. }
  1309. void tb_flush_jmp_cache(CPUArchState *env, target_ulong addr)
  1310. {
  1311. unsigned int i;
  1312. /* Discard jump cache entries for any tb which might potentially
  1313. overlap the flushed page. */
  1314. i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
  1315. memset(&env->tb_jmp_cache[i], 0,
  1316. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1317. i = tb_jmp_cache_hash_page(addr);
  1318. memset(&env->tb_jmp_cache[i], 0,
  1319. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1320. }
  1321. void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
  1322. {
  1323. int i, target_code_size, max_target_code_size;
  1324. int direct_jmp_count, direct_jmp2_count, cross_page;
  1325. TranslationBlock *tb;
  1326. target_code_size = 0;
  1327. max_target_code_size = 0;
  1328. cross_page = 0;
  1329. direct_jmp_count = 0;
  1330. direct_jmp2_count = 0;
  1331. for (i = 0; i < nb_tbs; i++) {
  1332. tb = &tbs[i];
  1333. target_code_size += tb->size;
  1334. if (tb->size > max_target_code_size) {
  1335. max_target_code_size = tb->size;
  1336. }
  1337. if (tb->page_addr[1] != -1) {
  1338. cross_page++;
  1339. }
  1340. if (tb->tb_next_offset[0] != 0xffff) {
  1341. direct_jmp_count++;
  1342. if (tb->tb_next_offset[1] != 0xffff) {
  1343. direct_jmp2_count++;
  1344. }
  1345. }
  1346. }
  1347. /* XXX: avoid using doubles ? */
  1348. cpu_fprintf(f, "Translation buffer state:\n");
  1349. cpu_fprintf(f, "gen code size %td/%zd\n",
  1350. code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
  1351. cpu_fprintf(f, "TB count %d/%d\n",
  1352. nb_tbs, code_gen_max_blocks);
  1353. cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
  1354. nb_tbs ? target_code_size / nb_tbs : 0,
  1355. max_target_code_size);
  1356. cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
  1357. nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
  1358. target_code_size ? (double) (code_gen_ptr - code_gen_buffer)
  1359. / target_code_size : 0);
  1360. cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
  1361. cross_page,
  1362. nb_tbs ? (cross_page * 100) / nb_tbs : 0);
  1363. cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
  1364. direct_jmp_count,
  1365. nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
  1366. direct_jmp2_count,
  1367. nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
  1368. cpu_fprintf(f, "\nStatistics:\n");
  1369. cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
  1370. cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
  1371. cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
  1372. tcg_dump_info(f, cpu_fprintf);
  1373. }
  1374. #else /* CONFIG_USER_ONLY */
  1375. void cpu_interrupt(CPUArchState *env, int mask)
  1376. {
  1377. env->interrupt_request |= mask;
  1378. env->tcg_exit_req = 1;
  1379. }
  1380. /*
  1381. * Walks guest process memory "regions" one by one
  1382. * and calls callback function 'fn' for each region.
  1383. */
  1384. struct walk_memory_regions_data {
  1385. walk_memory_regions_fn fn;
  1386. void *priv;
  1387. uintptr_t start;
  1388. int prot;
  1389. };
  1390. static int walk_memory_regions_end(struct walk_memory_regions_data *data,
  1391. abi_ulong end, int new_prot)
  1392. {
  1393. if (data->start != -1ul) {
  1394. int rc = data->fn(data->priv, data->start, end, data->prot);
  1395. if (rc != 0) {
  1396. return rc;
  1397. }
  1398. }
  1399. data->start = (new_prot ? end : -1ul);
  1400. data->prot = new_prot;
  1401. return 0;
  1402. }
  1403. static int walk_memory_regions_1(struct walk_memory_regions_data *data,
  1404. abi_ulong base, int level, void **lp)
  1405. {
  1406. abi_ulong pa;
  1407. int i, rc;
  1408. if (*lp == NULL) {
  1409. return walk_memory_regions_end(data, base, 0);
  1410. }
  1411. if (level == 0) {
  1412. PageDesc *pd = *lp;
  1413. for (i = 0; i < L2_SIZE; ++i) {
  1414. int prot = pd[i].flags;
  1415. pa = base | (i << TARGET_PAGE_BITS);
  1416. if (prot != data->prot) {
  1417. rc = walk_memory_regions_end(data, pa, prot);
  1418. if (rc != 0) {
  1419. return rc;
  1420. }
  1421. }
  1422. }
  1423. } else {
  1424. void **pp = *lp;
  1425. for (i = 0; i < L2_SIZE; ++i) {
  1426. pa = base | ((abi_ulong)i <<
  1427. (TARGET_PAGE_BITS + L2_BITS * level));
  1428. rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
  1429. if (rc != 0) {
  1430. return rc;
  1431. }
  1432. }
  1433. }
  1434. return 0;
  1435. }
  1436. int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
  1437. {
  1438. struct walk_memory_regions_data data;
  1439. uintptr_t i;
  1440. data.fn = fn;
  1441. data.priv = priv;
  1442. data.start = -1ul;
  1443. data.prot = 0;
  1444. for (i = 0; i < V_L1_SIZE; i++) {
  1445. int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
  1446. V_L1_SHIFT / L2_BITS - 1, l1_map + i);
  1447. if (rc != 0) {
  1448. return rc;
  1449. }
  1450. }
  1451. return walk_memory_regions_end(&data, 0, 0);
  1452. }
  1453. static int dump_region(void *priv, abi_ulong start,
  1454. abi_ulong end, unsigned long prot)
  1455. {
  1456. FILE *f = (FILE *)priv;
  1457. (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
  1458. " "TARGET_ABI_FMT_lx" %c%c%c\n",
  1459. start, end, end - start,
  1460. ((prot & PAGE_READ) ? 'r' : '-'),
  1461. ((prot & PAGE_WRITE) ? 'w' : '-'),
  1462. ((prot & PAGE_EXEC) ? 'x' : '-'));
  1463. return 0;
  1464. }
  1465. /* dump memory mappings */
  1466. void page_dump(FILE *f)
  1467. {
  1468. (void) fprintf(f, "%-8s %-8s %-8s %s\n",
  1469. "start", "end", "size", "prot");
  1470. walk_memory_regions(f, dump_region);
  1471. }
  1472. int page_get_flags(target_ulong address)
  1473. {
  1474. PageDesc *p;
  1475. p = page_find(address >> TARGET_PAGE_BITS);
  1476. if (!p) {
  1477. return 0;
  1478. }
  1479. return p->flags;
  1480. }
  1481. /* Modify the flags of a page and invalidate the code if necessary.
  1482. The flag PAGE_WRITE_ORG is positioned automatically depending
  1483. on PAGE_WRITE. The mmap_lock should already be held. */
  1484. void page_set_flags(target_ulong start, target_ulong end, int flags)
  1485. {
  1486. target_ulong addr, len;
  1487. /* This function should never be called with addresses outside the
  1488. guest address space. If this assert fires, it probably indicates
  1489. a missing call to h2g_valid. */
  1490. #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
  1491. assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
  1492. #endif
  1493. assert(start < end);
  1494. start = start & TARGET_PAGE_MASK;
  1495. end = TARGET_PAGE_ALIGN(end);
  1496. if (flags & PAGE_WRITE) {
  1497. flags |= PAGE_WRITE_ORG;
  1498. }
  1499. for (addr = start, len = end - start;
  1500. len != 0;
  1501. len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
  1502. PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
  1503. /* If the write protection bit is set, then we invalidate
  1504. the code inside. */
  1505. if (!(p->flags & PAGE_WRITE) &&
  1506. (flags & PAGE_WRITE) &&
  1507. p->first_tb) {
  1508. tb_invalidate_phys_page(addr, 0, NULL);
  1509. }
  1510. p->flags = flags;
  1511. }
  1512. }
  1513. int page_check_range(target_ulong start, target_ulong len, int flags)
  1514. {
  1515. PageDesc *p;
  1516. target_ulong end;
  1517. target_ulong addr;
  1518. /* This function should never be called with addresses outside the
  1519. guest address space. If this assert fires, it probably indicates
  1520. a missing call to h2g_valid. */
  1521. #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
  1522. assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
  1523. #endif
  1524. if (len == 0) {
  1525. return 0;
  1526. }
  1527. if (start + len - 1 < start) {
  1528. /* We've wrapped around. */
  1529. return -1;
  1530. }
  1531. /* must do before we loose bits in the next step */
  1532. end = TARGET_PAGE_ALIGN(start + len);
  1533. start = start & TARGET_PAGE_MASK;
  1534. for (addr = start, len = end - start;
  1535. len != 0;
  1536. len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
  1537. p = page_find(addr >> TARGET_PAGE_BITS);
  1538. if (!p) {
  1539. return -1;
  1540. }
  1541. if (!(p->flags & PAGE_VALID)) {
  1542. return -1;
  1543. }
  1544. if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) {
  1545. return -1;
  1546. }
  1547. if (flags & PAGE_WRITE) {
  1548. if (!(p->flags & PAGE_WRITE_ORG)) {
  1549. return -1;
  1550. }
  1551. /* unprotect the page if it was put read-only because it
  1552. contains translated code */
  1553. if (!(p->flags & PAGE_WRITE)) {
  1554. if (!page_unprotect(addr, 0, NULL)) {
  1555. return -1;
  1556. }
  1557. }
  1558. return 0;
  1559. }
  1560. }
  1561. return 0;
  1562. }
  1563. /* called from signal handler: invalidate the code and unprotect the
  1564. page. Return TRUE if the fault was successfully handled. */
  1565. int page_unprotect(target_ulong address, uintptr_t pc, void *puc)
  1566. {
  1567. unsigned int prot;
  1568. PageDesc *p;
  1569. target_ulong host_start, host_end, addr;
  1570. /* Technically this isn't safe inside a signal handler. However we
  1571. know this only ever happens in a synchronous SEGV handler, so in
  1572. practice it seems to be ok. */
  1573. mmap_lock();
  1574. p = page_find(address >> TARGET_PAGE_BITS);
  1575. if (!p) {
  1576. mmap_unlock();
  1577. return 0;
  1578. }
  1579. /* if the page was really writable, then we change its
  1580. protection back to writable */
  1581. if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
  1582. host_start = address & qemu_host_page_mask;
  1583. host_end = host_start + qemu_host_page_size;
  1584. prot = 0;
  1585. for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
  1586. p = page_find(addr >> TARGET_PAGE_BITS);
  1587. p->flags |= PAGE_WRITE;
  1588. prot |= p->flags;
  1589. /* and since the content will be modified, we must invalidate
  1590. the corresponding translated code. */
  1591. tb_invalidate_phys_page(addr, pc, puc);
  1592. #ifdef DEBUG_TB_CHECK
  1593. tb_invalidate_check(addr);
  1594. #endif
  1595. }
  1596. mprotect((void *)g2h(host_start), qemu_host_page_size,
  1597. prot & PAGE_BITS);
  1598. mmap_unlock();
  1599. return 1;
  1600. }
  1601. mmap_unlock();
  1602. return 0;
  1603. }
  1604. #endif /* CONFIG_USER_ONLY */