translate-all.c 56 KB

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