blob: 9ad2edcf3b57899798bf9a1ff066c5ed36fd1ed0 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
bellardfd6ce8f2003-05-14 19:00:11 +00002 * virtual page mapping and translated block handling
bellard54936002003-05-13 00:25:15 +00003 *
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
bellard67b915a2004-03-31 23:37:16 +000020#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000021#ifdef _WIN32
22#include <windows.h>
23#else
bellarda98d49b2004-11-14 16:22:05 +000024#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000025#include <sys/mman.h>
26#endif
bellard54936002003-05-13 00:25:15 +000027#include <stdlib.h>
28#include <stdio.h>
29#include <stdarg.h>
30#include <string.h>
31#include <errno.h>
32#include <unistd.h>
33#include <inttypes.h>
34
bellard6180a182003-09-30 21:04:53 +000035#include "cpu.h"
36#include "exec-all.h"
pbrook53a59602006-03-25 19:31:22 +000037#if defined(CONFIG_USER_ONLY)
38#include <qemu.h>
39#endif
bellard54936002003-05-13 00:25:15 +000040
bellardfd6ce8f2003-05-14 19:00:11 +000041//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000042//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000043//#define DEBUG_TLB
bellardfd6ce8f2003-05-14 19:00:11 +000044
45/* make various TB consistency checks */
46//#define DEBUG_TB_CHECK
bellard98857882004-01-18 21:52:14 +000047//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000048
49/* threshold to flush the translated code buffer */
50#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
51
bellard9fa3e852004-01-04 18:06:42 +000052#define SMC_BITMAP_USE_THRESHOLD 10
53
54#define MMAP_AREA_START 0x00000000
55#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000056
bellard108c49b2005-07-24 12:55:09 +000057#if defined(TARGET_SPARC64)
58#define TARGET_PHYS_ADDR_SPACE_BITS 41
59#elif defined(TARGET_PPC64)
60#define TARGET_PHYS_ADDR_SPACE_BITS 42
61#else
62/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
63#define TARGET_PHYS_ADDR_SPACE_BITS 32
64#endif
65
bellardfd6ce8f2003-05-14 19:00:11 +000066TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
bellard9fa3e852004-01-04 18:06:42 +000067TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000068int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000069/* any access to the tbs or the page table must use this lock */
70spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000071
bellardb8076a72005-04-07 22:20:31 +000072uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
bellardfd6ce8f2003-05-14 19:00:11 +000073uint8_t *code_gen_ptr;
74
bellard9fa3e852004-01-04 18:06:42 +000075int phys_ram_size;
76int phys_ram_fd;
77uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +000078uint8_t *phys_ram_dirty;
bellard9fa3e852004-01-04 18:06:42 +000079
bellard6a00d602005-11-21 23:25:50 +000080CPUState *first_cpu;
81/* current CPU in the current thread. It is only valid inside
82 cpu_exec() */
83CPUState *cpu_single_env;
84
bellard54936002003-05-13 00:25:15 +000085typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +000086 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +000087 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +000088 /* in order to optimize self modifying code, we count the number
89 of lookups we do to a given page to use a bitmap */
90 unsigned int code_write_count;
91 uint8_t *code_bitmap;
92#if defined(CONFIG_USER_ONLY)
93 unsigned long flags;
94#endif
bellard54936002003-05-13 00:25:15 +000095} PageDesc;
96
bellard92e873b2004-05-21 14:52:29 +000097typedef struct PhysPageDesc {
98 /* offset in host memory of the page + io_index in the low 12 bits */
bellarde04f40b2005-04-24 18:02:38 +000099 uint32_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000100} PhysPageDesc;
101
bellard54936002003-05-13 00:25:15 +0000102#define L2_BITS 10
103#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
104
105#define L1_SIZE (1 << L1_BITS)
106#define L2_SIZE (1 << L2_BITS)
107
bellard33417e72003-08-10 21:47:01 +0000108static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000109
bellard83fb7ad2004-07-05 21:25:26 +0000110unsigned long qemu_real_host_page_size;
111unsigned long qemu_host_page_bits;
112unsigned long qemu_host_page_size;
113unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000114
bellard92e873b2004-05-21 14:52:29 +0000115/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000116static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000117PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000118
bellard33417e72003-08-10 21:47:01 +0000119/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000120CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
121CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000122void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000123static int io_mem_nb;
124
bellard34865132003-10-05 14:28:56 +0000125/* log support */
126char *logfilename = "/tmp/qemu.log";
127FILE *logfile;
128int loglevel;
129
bellarde3db7222005-01-26 22:00:47 +0000130/* statistics */
131static int tlb_flush_count;
132static int tb_flush_count;
133static int tb_phys_invalidate_count;
134
bellardb346ff42003-06-15 20:05:50 +0000135static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000136{
bellard83fb7ad2004-07-05 21:25:26 +0000137 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000138 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000139#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000140 {
141 SYSTEM_INFO system_info;
142 DWORD old_protect;
143
144 GetSystemInfo(&system_info);
145 qemu_real_host_page_size = system_info.dwPageSize;
146
147 VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
148 PAGE_EXECUTE_READWRITE, &old_protect);
149 }
bellard67b915a2004-03-31 23:37:16 +0000150#else
bellard83fb7ad2004-07-05 21:25:26 +0000151 qemu_real_host_page_size = getpagesize();
bellardd5a8f072004-09-29 21:15:28 +0000152 {
153 unsigned long start, end;
154
155 start = (unsigned long)code_gen_buffer;
156 start &= ~(qemu_real_host_page_size - 1);
157
158 end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
159 end += qemu_real_host_page_size - 1;
160 end &= ~(qemu_real_host_page_size - 1);
161
162 mprotect((void *)start, end - start,
163 PROT_READ | PROT_WRITE | PROT_EXEC);
164 }
bellard67b915a2004-03-31 23:37:16 +0000165#endif
bellardd5a8f072004-09-29 21:15:28 +0000166
bellard83fb7ad2004-07-05 21:25:26 +0000167 if (qemu_host_page_size == 0)
168 qemu_host_page_size = qemu_real_host_page_size;
169 if (qemu_host_page_size < TARGET_PAGE_SIZE)
170 qemu_host_page_size = TARGET_PAGE_SIZE;
171 qemu_host_page_bits = 0;
172 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
173 qemu_host_page_bits++;
174 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000175 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
176 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
bellard54936002003-05-13 00:25:15 +0000177}
178
bellardfd6ce8f2003-05-14 19:00:11 +0000179static inline PageDesc *page_find_alloc(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000180{
bellard54936002003-05-13 00:25:15 +0000181 PageDesc **lp, *p;
182
bellard54936002003-05-13 00:25:15 +0000183 lp = &l1_map[index >> L2_BITS];
184 p = *lp;
185 if (!p) {
186 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000187 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000188 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000189 *lp = p;
190 }
191 return p + (index & (L2_SIZE - 1));
192}
193
bellardfd6ce8f2003-05-14 19:00:11 +0000194static inline PageDesc *page_find(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000195{
bellard54936002003-05-13 00:25:15 +0000196 PageDesc *p;
197
bellard54936002003-05-13 00:25:15 +0000198 p = l1_map[index >> L2_BITS];
199 if (!p)
200 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000201 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000202}
203
bellard108c49b2005-07-24 12:55:09 +0000204static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000205{
bellard108c49b2005-07-24 12:55:09 +0000206 void **lp, **p;
bellard92e873b2004-05-21 14:52:29 +0000207
bellard108c49b2005-07-24 12:55:09 +0000208 p = (void **)l1_phys_map;
209#if TARGET_PHYS_ADDR_SPACE_BITS > 32
210
211#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
212#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
213#endif
214 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000215 p = *lp;
216 if (!p) {
217 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000218 if (!alloc)
219 return NULL;
220 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
221 memset(p, 0, sizeof(void *) * L1_SIZE);
222 *lp = p;
223 }
224#endif
225 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
226 p = *lp;
227 if (!p) {
228 /* allocate if not found */
229 if (!alloc)
230 return NULL;
bellard0a962c02005-02-10 22:00:27 +0000231 p = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
bellard92e873b2004-05-21 14:52:29 +0000232 memset(p, 0, sizeof(PhysPageDesc) * L2_SIZE);
233 *lp = p;
234 }
bellard108c49b2005-07-24 12:55:09 +0000235 return ((PhysPageDesc *)p) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000236}
237
bellard108c49b2005-07-24 12:55:09 +0000238static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000239{
bellard108c49b2005-07-24 12:55:09 +0000240 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000241}
242
bellard9fa3e852004-01-04 18:06:42 +0000243#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000244static void tlb_protect_code(ram_addr_t ram_addr);
bellard3a7d9292005-08-21 09:26:42 +0000245static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
246 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000247#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000248
bellard6a00d602005-11-21 23:25:50 +0000249void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000250{
bellard6a00d602005-11-21 23:25:50 +0000251 CPUState **penv;
252 int cpu_index;
253
bellardfd6ce8f2003-05-14 19:00:11 +0000254 if (!code_gen_ptr) {
255 code_gen_ptr = code_gen_buffer;
bellardb346ff42003-06-15 20:05:50 +0000256 page_init();
bellard33417e72003-08-10 21:47:01 +0000257 io_mem_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000258 }
bellard6a00d602005-11-21 23:25:50 +0000259 env->next_cpu = NULL;
260 penv = &first_cpu;
261 cpu_index = 0;
262 while (*penv != NULL) {
263 penv = (CPUState **)&(*penv)->next_cpu;
264 cpu_index++;
265 }
266 env->cpu_index = cpu_index;
267 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000268}
269
bellard9fa3e852004-01-04 18:06:42 +0000270static inline void invalidate_page_bitmap(PageDesc *p)
271{
272 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000273 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000274 p->code_bitmap = NULL;
275 }
276 p->code_write_count = 0;
277}
278
bellardfd6ce8f2003-05-14 19:00:11 +0000279/* set to NULL all the 'first_tb' fields in all PageDescs */
280static void page_flush_tb(void)
281{
282 int i, j;
283 PageDesc *p;
284
285 for(i = 0; i < L1_SIZE; i++) {
286 p = l1_map[i];
287 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000288 for(j = 0; j < L2_SIZE; j++) {
289 p->first_tb = NULL;
290 invalidate_page_bitmap(p);
291 p++;
292 }
bellardfd6ce8f2003-05-14 19:00:11 +0000293 }
294 }
295}
296
297/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000298/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000299void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000300{
bellard6a00d602005-11-21 23:25:50 +0000301 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000302#if defined(DEBUG_FLUSH)
bellardfd6ce8f2003-05-14 19:00:11 +0000303 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
304 code_gen_ptr - code_gen_buffer,
305 nb_tbs,
bellard01243112004-01-04 15:48:17 +0000306 nb_tbs > 0 ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000307#endif
308 nb_tbs = 0;
bellard6a00d602005-11-21 23:25:50 +0000309
310 for(env = first_cpu; env != NULL; env = env->next_cpu) {
311 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
312 }
bellard9fa3e852004-01-04 18:06:42 +0000313
bellard8a8a6082004-10-03 13:36:49 +0000314 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000315 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000316
bellardfd6ce8f2003-05-14 19:00:11 +0000317 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000318 /* XXX: flush processor icache at this point if cache flush is
319 expensive */
bellarde3db7222005-01-26 22:00:47 +0000320 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000321}
322
323#ifdef DEBUG_TB_CHECK
324
325static void tb_invalidate_check(unsigned long address)
326{
327 TranslationBlock *tb;
328 int i;
329 address &= TARGET_PAGE_MASK;
330 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
331 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
332 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
333 address >= tb->pc + tb->size)) {
334 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
335 address, tb->pc, tb->size);
336 }
337 }
338 }
339}
340
341/* verify that all the pages have correct rights for code */
342static void tb_page_check(void)
343{
344 TranslationBlock *tb;
345 int i, flags1, flags2;
346
347 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
348 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
349 flags1 = page_get_flags(tb->pc);
350 flags2 = page_get_flags(tb->pc + tb->size - 1);
351 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
352 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
353 tb->pc, tb->size, flags1, flags2);
354 }
355 }
356 }
357}
358
bellardd4e81642003-05-25 16:46:15 +0000359void tb_jmp_check(TranslationBlock *tb)
360{
361 TranslationBlock *tb1;
362 unsigned int n1;
363
364 /* suppress any remaining jumps to this TB */
365 tb1 = tb->jmp_first;
366 for(;;) {
367 n1 = (long)tb1 & 3;
368 tb1 = (TranslationBlock *)((long)tb1 & ~3);
369 if (n1 == 2)
370 break;
371 tb1 = tb1->jmp_next[n1];
372 }
373 /* check end of list */
374 if (tb1 != tb) {
375 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
376 }
377}
378
bellardfd6ce8f2003-05-14 19:00:11 +0000379#endif
380
381/* invalidate one TB */
382static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
383 int next_offset)
384{
385 TranslationBlock *tb1;
386 for(;;) {
387 tb1 = *ptb;
388 if (tb1 == tb) {
389 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
390 break;
391 }
392 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
393 }
394}
395
bellard9fa3e852004-01-04 18:06:42 +0000396static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
397{
398 TranslationBlock *tb1;
399 unsigned int n1;
400
401 for(;;) {
402 tb1 = *ptb;
403 n1 = (long)tb1 & 3;
404 tb1 = (TranslationBlock *)((long)tb1 & ~3);
405 if (tb1 == tb) {
406 *ptb = tb1->page_next[n1];
407 break;
408 }
409 ptb = &tb1->page_next[n1];
410 }
411}
412
bellardd4e81642003-05-25 16:46:15 +0000413static inline void tb_jmp_remove(TranslationBlock *tb, int n)
414{
415 TranslationBlock *tb1, **ptb;
416 unsigned int n1;
417
418 ptb = &tb->jmp_next[n];
419 tb1 = *ptb;
420 if (tb1) {
421 /* find tb(n) in circular list */
422 for(;;) {
423 tb1 = *ptb;
424 n1 = (long)tb1 & 3;
425 tb1 = (TranslationBlock *)((long)tb1 & ~3);
426 if (n1 == n && tb1 == tb)
427 break;
428 if (n1 == 2) {
429 ptb = &tb1->jmp_first;
430 } else {
431 ptb = &tb1->jmp_next[n1];
432 }
433 }
434 /* now we can suppress tb(n) from the list */
435 *ptb = tb->jmp_next[n];
436
437 tb->jmp_next[n] = NULL;
438 }
439}
440
441/* reset the jump entry 'n' of a TB so that it is not chained to
442 another TB */
443static inline void tb_reset_jump(TranslationBlock *tb, int n)
444{
445 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
446}
447
bellard9fa3e852004-01-04 18:06:42 +0000448static inline void tb_phys_invalidate(TranslationBlock *tb, unsigned int page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000449{
bellard6a00d602005-11-21 23:25:50 +0000450 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000451 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000452 unsigned int h, n1;
bellard9fa3e852004-01-04 18:06:42 +0000453 target_ulong phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000454 TranslationBlock *tb1, *tb2;
bellard9fa3e852004-01-04 18:06:42 +0000455
456 /* remove the TB from the hash list */
457 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
458 h = tb_phys_hash_func(phys_pc);
459 tb_remove(&tb_phys_hash[h], tb,
460 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000461
bellard9fa3e852004-01-04 18:06:42 +0000462 /* remove the TB from the page list */
463 if (tb->page_addr[0] != page_addr) {
464 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
465 tb_page_remove(&p->first_tb, tb);
466 invalidate_page_bitmap(p);
467 }
468 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
469 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
470 tb_page_remove(&p->first_tb, tb);
471 invalidate_page_bitmap(p);
472 }
473
bellard8a40a182005-11-20 10:35:40 +0000474 tb_invalidated_flag = 1;
475
476 /* remove the TB from the hash list */
477 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000478 for(env = first_cpu; env != NULL; env = env->next_cpu) {
479 if (env->tb_jmp_cache[h] == tb)
480 env->tb_jmp_cache[h] = NULL;
481 }
bellard8a40a182005-11-20 10:35:40 +0000482
483 /* suppress this TB from the two jump lists */
484 tb_jmp_remove(tb, 0);
485 tb_jmp_remove(tb, 1);
486
487 /* suppress any remaining jumps to this TB */
488 tb1 = tb->jmp_first;
489 for(;;) {
490 n1 = (long)tb1 & 3;
491 if (n1 == 2)
492 break;
493 tb1 = (TranslationBlock *)((long)tb1 & ~3);
494 tb2 = tb1->jmp_next[n1];
495 tb_reset_jump(tb1, n1);
496 tb1->jmp_next[n1] = NULL;
497 tb1 = tb2;
498 }
499 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
500
bellarde3db7222005-01-26 22:00:47 +0000501 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000502}
503
504static inline void set_bits(uint8_t *tab, int start, int len)
505{
506 int end, mask, end1;
507
508 end = start + len;
509 tab += start >> 3;
510 mask = 0xff << (start & 7);
511 if ((start & ~7) == (end & ~7)) {
512 if (start < end) {
513 mask &= ~(0xff << (end & 7));
514 *tab |= mask;
515 }
516 } else {
517 *tab++ |= mask;
518 start = (start + 8) & ~7;
519 end1 = end & ~7;
520 while (start < end1) {
521 *tab++ = 0xff;
522 start += 8;
523 }
524 if (start < end) {
525 mask = ~(0xff << (end & 7));
526 *tab |= mask;
527 }
528 }
529}
530
531static void build_page_bitmap(PageDesc *p)
532{
533 int n, tb_start, tb_end;
534 TranslationBlock *tb;
535
bellard59817cc2004-02-16 22:01:13 +0000536 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000537 if (!p->code_bitmap)
538 return;
539 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
540
541 tb = p->first_tb;
542 while (tb != NULL) {
543 n = (long)tb & 3;
544 tb = (TranslationBlock *)((long)tb & ~3);
545 /* NOTE: this is subtle as a TB may span two physical pages */
546 if (n == 0) {
547 /* NOTE: tb_end may be after the end of the page, but
548 it is not a problem */
549 tb_start = tb->pc & ~TARGET_PAGE_MASK;
550 tb_end = tb_start + tb->size;
551 if (tb_end > TARGET_PAGE_SIZE)
552 tb_end = TARGET_PAGE_SIZE;
553 } else {
554 tb_start = 0;
555 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
556 }
557 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
558 tb = tb->page_next[n];
559 }
560}
561
bellardd720b932004-04-25 17:57:43 +0000562#ifdef TARGET_HAS_PRECISE_SMC
563
564static void tb_gen_code(CPUState *env,
565 target_ulong pc, target_ulong cs_base, int flags,
566 int cflags)
567{
568 TranslationBlock *tb;
569 uint8_t *tc_ptr;
570 target_ulong phys_pc, phys_page2, virt_page2;
571 int code_gen_size;
572
bellardc27004e2005-01-03 23:35:10 +0000573 phys_pc = get_phys_addr_code(env, pc);
574 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000575 if (!tb) {
576 /* flush must be done */
577 tb_flush(env);
578 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000579 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000580 }
581 tc_ptr = code_gen_ptr;
582 tb->tc_ptr = tc_ptr;
583 tb->cs_base = cs_base;
584 tb->flags = flags;
585 tb->cflags = cflags;
586 cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
587 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
588
589 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000590 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000591 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000592 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000593 phys_page2 = get_phys_addr_code(env, virt_page2);
594 }
595 tb_link_phys(tb, phys_pc, phys_page2);
596}
597#endif
598
bellard9fa3e852004-01-04 18:06:42 +0000599/* invalidate all TBs which intersect with the target physical page
600 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000601 the same physical page. 'is_cpu_write_access' should be true if called
602 from a real cpu write access: the virtual CPU will exit the current
603 TB if code is modified inside this TB. */
604void tb_invalidate_phys_page_range(target_ulong start, target_ulong end,
605 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000606{
bellardd720b932004-04-25 17:57:43 +0000607 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000608 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000609 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000610 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000611 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000612 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000613
614 p = page_find(start >> TARGET_PAGE_BITS);
615 if (!p)
616 return;
617 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000618 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
619 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000620 /* build code bitmap */
621 build_page_bitmap(p);
622 }
623
624 /* we remove all the TBs in the range [start, end[ */
625 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000626 current_tb_not_found = is_cpu_write_access;
627 current_tb_modified = 0;
628 current_tb = NULL; /* avoid warning */
629 current_pc = 0; /* avoid warning */
630 current_cs_base = 0; /* avoid warning */
631 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000632 tb = p->first_tb;
633 while (tb != NULL) {
634 n = (long)tb & 3;
635 tb = (TranslationBlock *)((long)tb & ~3);
636 tb_next = tb->page_next[n];
637 /* NOTE: this is subtle as a TB may span two physical pages */
638 if (n == 0) {
639 /* NOTE: tb_end may be after the end of the page, but
640 it is not a problem */
641 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
642 tb_end = tb_start + tb->size;
643 } else {
644 tb_start = tb->page_addr[1];
645 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
646 }
647 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000648#ifdef TARGET_HAS_PRECISE_SMC
649 if (current_tb_not_found) {
650 current_tb_not_found = 0;
651 current_tb = NULL;
652 if (env->mem_write_pc) {
653 /* now we have a real cpu fault */
654 current_tb = tb_find_pc(env->mem_write_pc);
655 }
656 }
657 if (current_tb == tb &&
658 !(current_tb->cflags & CF_SINGLE_INSN)) {
659 /* If we are modifying the current TB, we must stop
660 its execution. We could be more precise by checking
661 that the modification is after the current PC, but it
662 would require a specialized function to partially
663 restore the CPU state */
664
665 current_tb_modified = 1;
666 cpu_restore_state(current_tb, env,
667 env->mem_write_pc, NULL);
668#if defined(TARGET_I386)
669 current_flags = env->hflags;
670 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
671 current_cs_base = (target_ulong)env->segs[R_CS].base;
672 current_pc = current_cs_base + env->eip;
673#else
674#error unsupported CPU
675#endif
676 }
677#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000678 /* we need to do that to handle the case where a signal
679 occurs while doing tb_phys_invalidate() */
680 saved_tb = NULL;
681 if (env) {
682 saved_tb = env->current_tb;
683 env->current_tb = NULL;
684 }
bellard9fa3e852004-01-04 18:06:42 +0000685 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000686 if (env) {
687 env->current_tb = saved_tb;
688 if (env->interrupt_request && env->current_tb)
689 cpu_interrupt(env, env->interrupt_request);
690 }
bellard9fa3e852004-01-04 18:06:42 +0000691 }
692 tb = tb_next;
693 }
694#if !defined(CONFIG_USER_ONLY)
695 /* if no code remaining, no need to continue to use slow writes */
696 if (!p->first_tb) {
697 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000698 if (is_cpu_write_access) {
699 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
700 }
701 }
702#endif
703#ifdef TARGET_HAS_PRECISE_SMC
704 if (current_tb_modified) {
705 /* we generate a block containing just the instruction
706 modifying the memory. It will ensure that it cannot modify
707 itself */
bellardea1c1802004-06-14 18:56:36 +0000708 env->current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000709 tb_gen_code(env, current_pc, current_cs_base, current_flags,
710 CF_SINGLE_INSN);
711 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000712 }
713#endif
714}
715
716/* len must be <= 8 and start must be a multiple of len */
bellardd720b932004-04-25 17:57:43 +0000717static inline void tb_invalidate_phys_page_fast(target_ulong start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000718{
719 PageDesc *p;
720 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000721#if 0
bellarda4193c82004-06-03 14:01:43 +0000722 if (1) {
723 if (loglevel) {
724 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
725 cpu_single_env->mem_write_vaddr, len,
726 cpu_single_env->eip,
727 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
728 }
bellard59817cc2004-02-16 22:01:13 +0000729 }
730#endif
bellard9fa3e852004-01-04 18:06:42 +0000731 p = page_find(start >> TARGET_PAGE_BITS);
732 if (!p)
733 return;
734 if (p->code_bitmap) {
735 offset = start & ~TARGET_PAGE_MASK;
736 b = p->code_bitmap[offset >> 3] >> (offset & 7);
737 if (b & ((1 << len) - 1))
738 goto do_invalidate;
739 } else {
740 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000741 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000742 }
743}
744
bellard9fa3e852004-01-04 18:06:42 +0000745#if !defined(CONFIG_SOFTMMU)
bellardd720b932004-04-25 17:57:43 +0000746static void tb_invalidate_phys_page(target_ulong addr,
747 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000748{
bellardd720b932004-04-25 17:57:43 +0000749 int n, current_flags, current_tb_modified;
750 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000751 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000752 TranslationBlock *tb, *current_tb;
753#ifdef TARGET_HAS_PRECISE_SMC
754 CPUState *env = cpu_single_env;
755#endif
bellard9fa3e852004-01-04 18:06:42 +0000756
757 addr &= TARGET_PAGE_MASK;
758 p = page_find(addr >> TARGET_PAGE_BITS);
759 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000760 return;
761 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000762 current_tb_modified = 0;
763 current_tb = NULL;
764 current_pc = 0; /* avoid warning */
765 current_cs_base = 0; /* avoid warning */
766 current_flags = 0; /* avoid warning */
767#ifdef TARGET_HAS_PRECISE_SMC
768 if (tb && pc != 0) {
769 current_tb = tb_find_pc(pc);
770 }
771#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000772 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000773 n = (long)tb & 3;
774 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000775#ifdef TARGET_HAS_PRECISE_SMC
776 if (current_tb == tb &&
777 !(current_tb->cflags & CF_SINGLE_INSN)) {
778 /* If we are modifying the current TB, we must stop
779 its execution. We could be more precise by checking
780 that the modification is after the current PC, but it
781 would require a specialized function to partially
782 restore the CPU state */
783
784 current_tb_modified = 1;
785 cpu_restore_state(current_tb, env, pc, puc);
786#if defined(TARGET_I386)
787 current_flags = env->hflags;
788 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
789 current_cs_base = (target_ulong)env->segs[R_CS].base;
790 current_pc = current_cs_base + env->eip;
791#else
792#error unsupported CPU
793#endif
794 }
795#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000796 tb_phys_invalidate(tb, addr);
797 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000798 }
799 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000800#ifdef TARGET_HAS_PRECISE_SMC
801 if (current_tb_modified) {
802 /* we generate a block containing just the instruction
803 modifying the memory. It will ensure that it cannot modify
804 itself */
bellardea1c1802004-06-14 18:56:36 +0000805 env->current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000806 tb_gen_code(env, current_pc, current_cs_base, current_flags,
807 CF_SINGLE_INSN);
808 cpu_resume_from_signal(env, puc);
809 }
810#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000811}
bellard9fa3e852004-01-04 18:06:42 +0000812#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000813
814/* add the tb in the target page and protect it if necessary */
bellard9fa3e852004-01-04 18:06:42 +0000815static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000816 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000817{
818 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000819 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000820
bellard9fa3e852004-01-04 18:06:42 +0000821 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000822 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000823 tb->page_next[n] = p->first_tb;
824 last_first_tb = p->first_tb;
825 p->first_tb = (TranslationBlock *)((long)tb | n);
826 invalidate_page_bitmap(p);
827
bellard107db442004-06-22 18:48:46 +0000828#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000829
bellard9fa3e852004-01-04 18:06:42 +0000830#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000831 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000832 target_ulong addr;
833 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000834 int prot;
835
bellardfd6ce8f2003-05-14 19:00:11 +0000836 /* force the host page as non writable (writes will have a
837 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000838 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000839 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000840 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
841 addr += TARGET_PAGE_SIZE) {
842
843 p2 = page_find (addr >> TARGET_PAGE_BITS);
844 if (!p2)
845 continue;
846 prot |= p2->flags;
847 p2->flags &= ~PAGE_WRITE;
848 page_get_flags(addr);
849 }
850 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000851 (prot & PAGE_BITS) & ~PAGE_WRITE);
852#ifdef DEBUG_TB_INVALIDATE
853 printf("protecting code page: 0x%08lx\n",
pbrook53a59602006-03-25 19:31:22 +0000854 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000855#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000856 }
bellard9fa3e852004-01-04 18:06:42 +0000857#else
858 /* if some code is already present, then the pages are already
859 protected. So we handle the case where only the first TB is
860 allocated in a physical page */
861 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000862 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000863 }
864#endif
bellardd720b932004-04-25 17:57:43 +0000865
866#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +0000867}
868
869/* Allocate a new translation block. Flush the translation buffer if
870 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +0000871TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +0000872{
873 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000874
875 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
876 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
bellardd4e81642003-05-25 16:46:15 +0000877 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +0000878 tb = &tbs[nb_tbs++];
879 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +0000880 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +0000881 return tb;
882}
883
bellard9fa3e852004-01-04 18:06:42 +0000884/* add a new TB and link it to the physical page tables. phys_page2 is
885 (-1) to indicate that only one page contains the TB. */
886void tb_link_phys(TranslationBlock *tb,
887 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +0000888{
bellard9fa3e852004-01-04 18:06:42 +0000889 unsigned int h;
890 TranslationBlock **ptb;
891
892 /* add in the physical hash table */
893 h = tb_phys_hash_func(phys_pc);
894 ptb = &tb_phys_hash[h];
895 tb->phys_hash_next = *ptb;
896 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000897
898 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +0000899 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
900 if (phys_page2 != -1)
901 tb_alloc_page(tb, 1, phys_page2);
902 else
903 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +0000904
bellardd4e81642003-05-25 16:46:15 +0000905 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
906 tb->jmp_next[0] = NULL;
907 tb->jmp_next[1] = NULL;
bellardb448f2f2004-02-25 23:24:04 +0000908#ifdef USE_CODE_COPY
909 tb->cflags &= ~CF_FP_USED;
910 if (tb->cflags & CF_TB_FP_USED)
911 tb->cflags |= CF_FP_USED;
912#endif
bellardd4e81642003-05-25 16:46:15 +0000913
914 /* init original jump addresses */
915 if (tb->tb_next_offset[0] != 0xffff)
916 tb_reset_jump(tb, 0);
917 if (tb->tb_next_offset[1] != 0xffff)
918 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +0000919
920#ifdef DEBUG_TB_CHECK
921 tb_page_check();
922#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000923}
924
bellarda513fe12003-05-27 23:29:48 +0000925/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
926 tb[1].tc_ptr. Return NULL if not found */
927TranslationBlock *tb_find_pc(unsigned long tc_ptr)
928{
929 int m_min, m_max, m;
930 unsigned long v;
931 TranslationBlock *tb;
932
933 if (nb_tbs <= 0)
934 return NULL;
935 if (tc_ptr < (unsigned long)code_gen_buffer ||
936 tc_ptr >= (unsigned long)code_gen_ptr)
937 return NULL;
938 /* binary search (cf Knuth) */
939 m_min = 0;
940 m_max = nb_tbs - 1;
941 while (m_min <= m_max) {
942 m = (m_min + m_max) >> 1;
943 tb = &tbs[m];
944 v = (unsigned long)tb->tc_ptr;
945 if (v == tc_ptr)
946 return tb;
947 else if (tc_ptr < v) {
948 m_max = m - 1;
949 } else {
950 m_min = m + 1;
951 }
952 }
953 return &tbs[m_max];
954}
bellard75012672003-06-21 13:11:07 +0000955
bellardea041c02003-06-25 16:16:50 +0000956static void tb_reset_jump_recursive(TranslationBlock *tb);
957
958static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
959{
960 TranslationBlock *tb1, *tb_next, **ptb;
961 unsigned int n1;
962
963 tb1 = tb->jmp_next[n];
964 if (tb1 != NULL) {
965 /* find head of list */
966 for(;;) {
967 n1 = (long)tb1 & 3;
968 tb1 = (TranslationBlock *)((long)tb1 & ~3);
969 if (n1 == 2)
970 break;
971 tb1 = tb1->jmp_next[n1];
972 }
973 /* we are now sure now that tb jumps to tb1 */
974 tb_next = tb1;
975
976 /* remove tb from the jmp_first list */
977 ptb = &tb_next->jmp_first;
978 for(;;) {
979 tb1 = *ptb;
980 n1 = (long)tb1 & 3;
981 tb1 = (TranslationBlock *)((long)tb1 & ~3);
982 if (n1 == n && tb1 == tb)
983 break;
984 ptb = &tb1->jmp_next[n1];
985 }
986 *ptb = tb->jmp_next[n];
987 tb->jmp_next[n] = NULL;
988
989 /* suppress the jump to next tb in generated code */
990 tb_reset_jump(tb, n);
991
bellard01243112004-01-04 15:48:17 +0000992 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +0000993 tb_reset_jump_recursive(tb_next);
994 }
995}
996
997static void tb_reset_jump_recursive(TranslationBlock *tb)
998{
999 tb_reset_jump_recursive2(tb, 0);
1000 tb_reset_jump_recursive2(tb, 1);
1001}
1002
bellard1fddef42005-04-17 19:16:13 +00001003#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001004static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1005{
1006 target_ulong phys_addr;
1007
1008 phys_addr = cpu_get_phys_page_debug(env, pc);
1009 tb_invalidate_phys_page_range(phys_addr, phys_addr + 1, 0);
1010}
bellardc27004e2005-01-03 23:35:10 +00001011#endif
bellardd720b932004-04-25 17:57:43 +00001012
bellardc33a3462003-07-29 20:50:33 +00001013/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1014 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001015int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001016{
bellard1fddef42005-04-17 19:16:13 +00001017#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001018 int i;
bellardd720b932004-04-25 17:57:43 +00001019
bellard4c3a88a2003-07-26 12:06:08 +00001020 for(i = 0; i < env->nb_breakpoints; i++) {
1021 if (env->breakpoints[i] == pc)
1022 return 0;
1023 }
1024
1025 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1026 return -1;
1027 env->breakpoints[env->nb_breakpoints++] = pc;
bellardd720b932004-04-25 17:57:43 +00001028
1029 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001030 return 0;
1031#else
1032 return -1;
1033#endif
1034}
1035
1036/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001037int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001038{
bellard1fddef42005-04-17 19:16:13 +00001039#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001040 int i;
1041 for(i = 0; i < env->nb_breakpoints; i++) {
1042 if (env->breakpoints[i] == pc)
1043 goto found;
1044 }
1045 return -1;
1046 found:
bellard4c3a88a2003-07-26 12:06:08 +00001047 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001048 if (i < env->nb_breakpoints)
1049 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001050
1051 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001052 return 0;
1053#else
1054 return -1;
1055#endif
1056}
1057
bellardc33a3462003-07-29 20:50:33 +00001058/* enable or disable single step mode. EXCP_DEBUG is returned by the
1059 CPU loop after each instruction */
1060void cpu_single_step(CPUState *env, int enabled)
1061{
bellard1fddef42005-04-17 19:16:13 +00001062#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001063 if (env->singlestep_enabled != enabled) {
1064 env->singlestep_enabled = enabled;
1065 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001066 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001067 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001068 }
1069#endif
1070}
1071
bellard34865132003-10-05 14:28:56 +00001072/* enable or disable low levels log */
1073void cpu_set_log(int log_flags)
1074{
1075 loglevel = log_flags;
1076 if (loglevel && !logfile) {
1077 logfile = fopen(logfilename, "w");
1078 if (!logfile) {
1079 perror(logfilename);
1080 _exit(1);
1081 }
bellard9fa3e852004-01-04 18:06:42 +00001082#if !defined(CONFIG_SOFTMMU)
1083 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1084 {
1085 static uint8_t logfile_buf[4096];
1086 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1087 }
1088#else
bellard34865132003-10-05 14:28:56 +00001089 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001090#endif
bellard34865132003-10-05 14:28:56 +00001091 }
1092}
1093
1094void cpu_set_log_filename(const char *filename)
1095{
1096 logfilename = strdup(filename);
1097}
bellardc33a3462003-07-29 20:50:33 +00001098
bellard01243112004-01-04 15:48:17 +00001099/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001100void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001101{
1102 TranslationBlock *tb;
bellardee8b7022004-02-03 23:35:10 +00001103 static int interrupt_lock;
bellard59817cc2004-02-16 22:01:13 +00001104
bellard68a79312003-06-30 13:12:32 +00001105 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001106 /* if the cpu is currently executing code, we must unlink it and
1107 all the potentially executing TB */
1108 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001109 if (tb && !testandset(&interrupt_lock)) {
1110 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001111 tb_reset_jump_recursive(tb);
bellardee8b7022004-02-03 23:35:10 +00001112 interrupt_lock = 0;
bellardea041c02003-06-25 16:16:50 +00001113 }
1114}
1115
bellardb54ad042004-05-20 13:42:52 +00001116void cpu_reset_interrupt(CPUState *env, int mask)
1117{
1118 env->interrupt_request &= ~mask;
1119}
1120
bellardf193c792004-03-21 17:06:25 +00001121CPULogItem cpu_log_items[] = {
1122 { CPU_LOG_TB_OUT_ASM, "out_asm",
1123 "show generated host assembly code for each compiled TB" },
1124 { CPU_LOG_TB_IN_ASM, "in_asm",
1125 "show target assembly code for each compiled TB" },
1126 { CPU_LOG_TB_OP, "op",
1127 "show micro ops for each compiled TB (only usable if 'in_asm' used)" },
1128#ifdef TARGET_I386
1129 { CPU_LOG_TB_OP_OPT, "op_opt",
1130 "show micro ops after optimization for each compiled TB" },
1131#endif
1132 { CPU_LOG_INT, "int",
1133 "show interrupts/exceptions in short format" },
1134 { CPU_LOG_EXEC, "exec",
1135 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001136 { CPU_LOG_TB_CPU, "cpu",
1137 "show CPU state before bloc translation" },
bellardf193c792004-03-21 17:06:25 +00001138#ifdef TARGET_I386
1139 { CPU_LOG_PCALL, "pcall",
1140 "show protected mode far calls/returns/exceptions" },
1141#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001142#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001143 { CPU_LOG_IOPORT, "ioport",
1144 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001145#endif
bellardf193c792004-03-21 17:06:25 +00001146 { 0, NULL, NULL },
1147};
1148
1149static int cmp1(const char *s1, int n, const char *s2)
1150{
1151 if (strlen(s2) != n)
1152 return 0;
1153 return memcmp(s1, s2, n) == 0;
1154}
1155
1156/* takes a comma separated list of log masks. Return 0 if error. */
1157int cpu_str_to_log_mask(const char *str)
1158{
1159 CPULogItem *item;
1160 int mask;
1161 const char *p, *p1;
1162
1163 p = str;
1164 mask = 0;
1165 for(;;) {
1166 p1 = strchr(p, ',');
1167 if (!p1)
1168 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001169 if(cmp1(p,p1-p,"all")) {
1170 for(item = cpu_log_items; item->mask != 0; item++) {
1171 mask |= item->mask;
1172 }
1173 } else {
bellardf193c792004-03-21 17:06:25 +00001174 for(item = cpu_log_items; item->mask != 0; item++) {
1175 if (cmp1(p, p1 - p, item->name))
1176 goto found;
1177 }
1178 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001179 }
bellardf193c792004-03-21 17:06:25 +00001180 found:
1181 mask |= item->mask;
1182 if (*p1 != ',')
1183 break;
1184 p = p1 + 1;
1185 }
1186 return mask;
1187}
bellardea041c02003-06-25 16:16:50 +00001188
bellard75012672003-06-21 13:11:07 +00001189void cpu_abort(CPUState *env, const char *fmt, ...)
1190{
1191 va_list ap;
1192
1193 va_start(ap, fmt);
1194 fprintf(stderr, "qemu: fatal: ");
1195 vfprintf(stderr, fmt, ap);
1196 fprintf(stderr, "\n");
1197#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001198 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1199#else
1200 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001201#endif
1202 va_end(ap);
1203 abort();
1204}
1205
bellard01243112004-01-04 15:48:17 +00001206#if !defined(CONFIG_USER_ONLY)
1207
bellardee8b7022004-02-03 23:35:10 +00001208/* NOTE: if flush_global is true, also flush global entries (not
1209 implemented yet) */
1210void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001211{
bellard33417e72003-08-10 21:47:01 +00001212 int i;
bellard01243112004-01-04 15:48:17 +00001213
bellard9fa3e852004-01-04 18:06:42 +00001214#if defined(DEBUG_TLB)
1215 printf("tlb_flush:\n");
1216#endif
bellard01243112004-01-04 15:48:17 +00001217 /* must reset current TB so that interrupts cannot modify the
1218 links while we are modifying them */
1219 env->current_tb = NULL;
1220
bellard33417e72003-08-10 21:47:01 +00001221 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001222 env->tlb_table[0][i].addr_read = -1;
1223 env->tlb_table[0][i].addr_write = -1;
1224 env->tlb_table[0][i].addr_code = -1;
1225 env->tlb_table[1][i].addr_read = -1;
1226 env->tlb_table[1][i].addr_write = -1;
1227 env->tlb_table[1][i].addr_code = -1;
bellard33417e72003-08-10 21:47:01 +00001228 }
bellard9fa3e852004-01-04 18:06:42 +00001229
bellard8a40a182005-11-20 10:35:40 +00001230 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001231
1232#if !defined(CONFIG_SOFTMMU)
1233 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1234#endif
bellard0a962c02005-02-10 22:00:27 +00001235#ifdef USE_KQEMU
1236 if (env->kqemu_enabled) {
1237 kqemu_flush(env, flush_global);
1238 }
1239#endif
bellarde3db7222005-01-26 22:00:47 +00001240 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001241}
1242
bellard274da6b2004-05-20 21:56:27 +00001243static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001244{
bellard84b7b8e2005-11-28 21:19:04 +00001245 if (addr == (tlb_entry->addr_read &
1246 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1247 addr == (tlb_entry->addr_write &
1248 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1249 addr == (tlb_entry->addr_code &
1250 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1251 tlb_entry->addr_read = -1;
1252 tlb_entry->addr_write = -1;
1253 tlb_entry->addr_code = -1;
1254 }
bellard61382a52003-10-27 21:22:23 +00001255}
1256
bellard2e126692004-04-25 21:28:44 +00001257void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001258{
bellard8a40a182005-11-20 10:35:40 +00001259 int i;
bellard9fa3e852004-01-04 18:06:42 +00001260 TranslationBlock *tb;
bellard01243112004-01-04 15:48:17 +00001261
bellard9fa3e852004-01-04 18:06:42 +00001262#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001263 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001264#endif
bellard01243112004-01-04 15:48:17 +00001265 /* must reset current TB so that interrupts cannot modify the
1266 links while we are modifying them */
1267 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001268
bellard61382a52003-10-27 21:22:23 +00001269 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001270 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001271 tlb_flush_entry(&env->tlb_table[0][i], addr);
1272 tlb_flush_entry(&env->tlb_table[1][i], addr);
bellard01243112004-01-04 15:48:17 +00001273
bellard8a40a182005-11-20 10:35:40 +00001274 for(i = 0; i < TB_JMP_CACHE_SIZE; i++) {
1275 tb = env->tb_jmp_cache[i];
1276 if (tb &&
1277 ((tb->pc & TARGET_PAGE_MASK) == addr ||
1278 ((tb->pc + tb->size - 1) & TARGET_PAGE_MASK) == addr)) {
1279 env->tb_jmp_cache[i] = NULL;
bellard9fa3e852004-01-04 18:06:42 +00001280 }
1281 }
1282
bellard01243112004-01-04 15:48:17 +00001283#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001284 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001285 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001286#endif
bellard0a962c02005-02-10 22:00:27 +00001287#ifdef USE_KQEMU
1288 if (env->kqemu_enabled) {
1289 kqemu_flush_page(env, addr);
1290 }
1291#endif
bellard9fa3e852004-01-04 18:06:42 +00001292}
1293
bellard9fa3e852004-01-04 18:06:42 +00001294/* update the TLBs so that writes to code in the virtual page 'addr'
1295 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001296static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001297{
bellard6a00d602005-11-21 23:25:50 +00001298 cpu_physical_memory_reset_dirty(ram_addr,
1299 ram_addr + TARGET_PAGE_SIZE,
1300 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001301}
1302
bellard9fa3e852004-01-04 18:06:42 +00001303/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001304 tested for self modifying code */
1305static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
1306 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001307{
bellard3a7d9292005-08-21 09:26:42 +00001308 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001309}
1310
1311static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
1312 unsigned long start, unsigned long length)
1313{
1314 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001315 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1316 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001317 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001318 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001319 }
1320 }
1321}
1322
bellard3a7d9292005-08-21 09:26:42 +00001323void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001324 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001325{
1326 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001327 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001328 int i, mask, len;
1329 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001330
1331 start &= TARGET_PAGE_MASK;
1332 end = TARGET_PAGE_ALIGN(end);
1333
1334 length = end - start;
1335 if (length == 0)
1336 return;
bellard0a962c02005-02-10 22:00:27 +00001337 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001338#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001339 /* XXX: should not depend on cpu context */
1340 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001341 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001342 ram_addr_t addr;
1343 addr = start;
1344 for(i = 0; i < len; i++) {
1345 kqemu_set_notdirty(env, addr);
1346 addr += TARGET_PAGE_SIZE;
1347 }
bellard3a7d9292005-08-21 09:26:42 +00001348 }
1349#endif
bellardf23db162005-08-21 19:12:28 +00001350 mask = ~dirty_flags;
1351 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1352 for(i = 0; i < len; i++)
1353 p[i] &= mask;
1354
bellard1ccde1c2004-02-06 19:46:14 +00001355 /* we modify the TLB cache so that the dirty bit will be set again
1356 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001357 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001358 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1359 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001360 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001361 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001362 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001363 }
bellard59817cc2004-02-16 22:01:13 +00001364
1365#if !defined(CONFIG_SOFTMMU)
1366 /* XXX: this is expensive */
1367 {
1368 VirtPageDesc *p;
1369 int j;
1370 target_ulong addr;
1371
1372 for(i = 0; i < L1_SIZE; i++) {
1373 p = l1_virt_map[i];
1374 if (p) {
1375 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1376 for(j = 0; j < L2_SIZE; j++) {
1377 if (p->valid_tag == virt_valid_tag &&
1378 p->phys_addr >= start && p->phys_addr < end &&
1379 (p->prot & PROT_WRITE)) {
1380 if (addr < MMAP_AREA_END) {
1381 mprotect((void *)addr, TARGET_PAGE_SIZE,
1382 p->prot & ~PROT_WRITE);
1383 }
1384 }
1385 addr += TARGET_PAGE_SIZE;
1386 p++;
1387 }
1388 }
1389 }
1390 }
1391#endif
bellard1ccde1c2004-02-06 19:46:14 +00001392}
1393
bellard3a7d9292005-08-21 09:26:42 +00001394static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1395{
1396 ram_addr_t ram_addr;
1397
bellard84b7b8e2005-11-28 21:19:04 +00001398 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1399 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001400 tlb_entry->addend - (unsigned long)phys_ram_base;
1401 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001402 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001403 }
1404 }
1405}
1406
1407/* update the TLB according to the current state of the dirty bits */
1408void cpu_tlb_update_dirty(CPUState *env)
1409{
1410 int i;
1411 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001412 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001413 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001414 tlb_update_dirty(&env->tlb_table[1][i]);
bellard3a7d9292005-08-21 09:26:42 +00001415}
1416
bellard1ccde1c2004-02-06 19:46:14 +00001417static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001418 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001419{
1420 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001421 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1422 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001423 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001424 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001425 }
1426 }
1427}
1428
1429/* update the TLB corresponding to virtual page vaddr and phys addr
1430 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001431static inline void tlb_set_dirty(CPUState *env,
1432 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001433{
bellard1ccde1c2004-02-06 19:46:14 +00001434 int i;
1435
bellard1ccde1c2004-02-06 19:46:14 +00001436 addr &= TARGET_PAGE_MASK;
1437 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001438 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1439 tlb_set_dirty1(&env->tlb_table[1][i], addr);
bellard9fa3e852004-01-04 18:06:42 +00001440}
1441
bellard59817cc2004-02-16 22:01:13 +00001442/* add a new TLB entry. At most one entry for a given virtual address
1443 is permitted. Return 0 if OK or 2 if the page could not be mapped
1444 (can only happen in non SOFTMMU mode for I/O pages or pages
1445 conflicting with the host address space). */
bellard84b7b8e2005-11-28 21:19:04 +00001446int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1447 target_phys_addr_t paddr, int prot,
1448 int is_user, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001449{
bellard92e873b2004-05-21 14:52:29 +00001450 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001451 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001452 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001453 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001454 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001455 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001456 CPUTLBEntry *te;
bellard9fa3e852004-01-04 18:06:42 +00001457
bellard92e873b2004-05-21 14:52:29 +00001458 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001459 if (!p) {
1460 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001461 } else {
1462 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001463 }
1464#if defined(DEBUG_TLB)
bellard3a7d9292005-08-21 09:26:42 +00001465 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x u=%d smmu=%d pd=0x%08lx\n",
bellard84b7b8e2005-11-28 21:19:04 +00001466 vaddr, (int)paddr, prot, is_user, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001467#endif
1468
1469 ret = 0;
1470#if !defined(CONFIG_SOFTMMU)
1471 if (is_softmmu)
1472#endif
1473 {
1474 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1475 /* IO memory case */
1476 address = vaddr | pd;
1477 addend = paddr;
1478 } else {
1479 /* standard memory */
1480 address = vaddr;
1481 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1482 }
1483
bellard90f18422005-07-24 10:17:31 +00001484 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001485 addend -= vaddr;
bellard84b7b8e2005-11-28 21:19:04 +00001486 te = &env->tlb_table[is_user][index];
1487 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001488 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001489 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001490 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001491 te->addr_read = -1;
1492 }
1493 if (prot & PAGE_EXEC) {
1494 te->addr_code = address;
1495 } else {
1496 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001497 }
bellard67b915a2004-03-31 23:37:16 +00001498 if (prot & PAGE_WRITE) {
bellard9fa3e852004-01-04 18:06:42 +00001499 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM) {
1500 /* ROM: access is ignored (same as unassigned) */
bellard84b7b8e2005-11-28 21:19:04 +00001501 te->addr_write = vaddr | IO_MEM_ROM;
bellard3a7d9292005-08-21 09:26:42 +00001502 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001503 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001504 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001505 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001506 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001507 }
1508 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001509 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001510 }
1511 }
1512#if !defined(CONFIG_SOFTMMU)
1513 else {
1514 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1515 /* IO access: no mapping is done as it will be handled by the
1516 soft MMU */
1517 if (!(env->hflags & HF_SOFTMMU_MASK))
1518 ret = 2;
1519 } else {
1520 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001521
bellard59817cc2004-02-16 22:01:13 +00001522 if (vaddr >= MMAP_AREA_END) {
1523 ret = 2;
1524 } else {
1525 if (prot & PROT_WRITE) {
1526 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001527#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001528 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001529#endif
bellard59817cc2004-02-16 22:01:13 +00001530 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
1531 !cpu_physical_memory_is_dirty(pd))) {
1532 /* ROM: we do as if code was inside */
1533 /* if code is present, we only map as read only and save the
1534 original mapping */
1535 VirtPageDesc *vp;
1536
bellard90f18422005-07-24 10:17:31 +00001537 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001538 vp->phys_addr = pd;
1539 vp->prot = prot;
1540 vp->valid_tag = virt_valid_tag;
1541 prot &= ~PAGE_WRITE;
1542 }
bellard9fa3e852004-01-04 18:06:42 +00001543 }
bellard59817cc2004-02-16 22:01:13 +00001544 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
1545 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1546 if (map_addr == MAP_FAILED) {
1547 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1548 paddr, vaddr);
1549 }
bellard9fa3e852004-01-04 18:06:42 +00001550 }
1551 }
1552 }
1553#endif
1554 return ret;
1555}
1556
1557/* called from signal handler: invalidate the code and unprotect the
1558 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001559int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001560{
1561#if !defined(CONFIG_SOFTMMU)
1562 VirtPageDesc *vp;
1563
1564#if defined(DEBUG_TLB)
1565 printf("page_unprotect: addr=0x%08x\n", addr);
1566#endif
1567 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001568
1569 /* if it is not mapped, no need to worry here */
1570 if (addr >= MMAP_AREA_END)
1571 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001572 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1573 if (!vp)
1574 return 0;
1575 /* NOTE: in this case, validate_tag is _not_ tested as it
1576 validates only the code TLB */
1577 if (vp->valid_tag != virt_valid_tag)
1578 return 0;
1579 if (!(vp->prot & PAGE_WRITE))
1580 return 0;
1581#if defined(DEBUG_TLB)
1582 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
1583 addr, vp->phys_addr, vp->prot);
1584#endif
bellard59817cc2004-02-16 22:01:13 +00001585 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1586 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1587 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001588 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001589 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001590 /* flush the code inside */
1591 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001592 return 1;
1593#else
1594 return 0;
1595#endif
bellard33417e72003-08-10 21:47:01 +00001596}
1597
bellard01243112004-01-04 15:48:17 +00001598#else
1599
bellardee8b7022004-02-03 23:35:10 +00001600void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001601{
1602}
1603
bellard2e126692004-04-25 21:28:44 +00001604void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001605{
1606}
1607
bellard84b7b8e2005-11-28 21:19:04 +00001608int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1609 target_phys_addr_t paddr, int prot,
1610 int is_user, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001611{
bellard9fa3e852004-01-04 18:06:42 +00001612 return 0;
1613}
bellard33417e72003-08-10 21:47:01 +00001614
bellard9fa3e852004-01-04 18:06:42 +00001615/* dump memory mappings */
1616void page_dump(FILE *f)
1617{
1618 unsigned long start, end;
1619 int i, j, prot, prot1;
1620 PageDesc *p;
1621
1622 fprintf(f, "%-8s %-8s %-8s %s\n",
1623 "start", "end", "size", "prot");
1624 start = -1;
1625 end = -1;
1626 prot = 0;
1627 for(i = 0; i <= L1_SIZE; i++) {
1628 if (i < L1_SIZE)
1629 p = l1_map[i];
1630 else
1631 p = NULL;
1632 for(j = 0;j < L2_SIZE; j++) {
1633 if (!p)
1634 prot1 = 0;
1635 else
1636 prot1 = p[j].flags;
1637 if (prot1 != prot) {
1638 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1639 if (start != -1) {
1640 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
1641 start, end, end - start,
1642 prot & PAGE_READ ? 'r' : '-',
1643 prot & PAGE_WRITE ? 'w' : '-',
1644 prot & PAGE_EXEC ? 'x' : '-');
1645 }
1646 if (prot1 != 0)
1647 start = end;
1648 else
1649 start = -1;
1650 prot = prot1;
1651 }
1652 if (!p)
1653 break;
1654 }
bellard33417e72003-08-10 21:47:01 +00001655 }
bellard33417e72003-08-10 21:47:01 +00001656}
1657
pbrook53a59602006-03-25 19:31:22 +00001658int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001659{
bellard9fa3e852004-01-04 18:06:42 +00001660 PageDesc *p;
1661
1662 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001663 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001664 return 0;
1665 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001666}
1667
bellard9fa3e852004-01-04 18:06:42 +00001668/* modify the flags of a page and invalidate the code if
1669 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1670 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001671void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001672{
1673 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001674 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001675
1676 start = start & TARGET_PAGE_MASK;
1677 end = TARGET_PAGE_ALIGN(end);
1678 if (flags & PAGE_WRITE)
1679 flags |= PAGE_WRITE_ORG;
1680 spin_lock(&tb_lock);
1681 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1682 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1683 /* if the write protection is set, then we invalidate the code
1684 inside */
1685 if (!(p->flags & PAGE_WRITE) &&
1686 (flags & PAGE_WRITE) &&
1687 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001688 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001689 }
1690 p->flags = flags;
1691 }
1692 spin_unlock(&tb_lock);
1693}
1694
1695/* called from signal handler: invalidate the code and unprotect the
1696 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001697int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001698{
1699 unsigned int page_index, prot, pindex;
1700 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00001701 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001702
bellard83fb7ad2004-07-05 21:25:26 +00001703 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00001704 page_index = host_start >> TARGET_PAGE_BITS;
1705 p1 = page_find(page_index);
1706 if (!p1)
1707 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00001708 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00001709 p = p1;
1710 prot = 0;
1711 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
1712 prot |= p->flags;
1713 p++;
1714 }
1715 /* if the page was really writable, then we change its
1716 protection back to writable */
1717 if (prot & PAGE_WRITE_ORG) {
1718 pindex = (address - host_start) >> TARGET_PAGE_BITS;
1719 if (!(p1[pindex].flags & PAGE_WRITE)) {
pbrook53a59602006-03-25 19:31:22 +00001720 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00001721 (prot & PAGE_BITS) | PAGE_WRITE);
1722 p1[pindex].flags |= PAGE_WRITE;
1723 /* and since the content will be modified, we must invalidate
1724 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00001725 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001726#ifdef DEBUG_TB_CHECK
1727 tb_invalidate_check(address);
1728#endif
1729 return 1;
1730 }
1731 }
1732 return 0;
1733}
1734
1735/* call this function when system calls directly modify a memory area */
pbrook53a59602006-03-25 19:31:22 +00001736/* ??? This should be redundant now we have lock_user. */
1737void page_unprotect_range(target_ulong data, target_ulong data_size)
bellard9fa3e852004-01-04 18:06:42 +00001738{
pbrook53a59602006-03-25 19:31:22 +00001739 target_ulong start, end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001740
pbrook53a59602006-03-25 19:31:22 +00001741 start = data;
bellard9fa3e852004-01-04 18:06:42 +00001742 end = start + data_size;
1743 start &= TARGET_PAGE_MASK;
1744 end = TARGET_PAGE_ALIGN(end);
1745 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
bellardd720b932004-04-25 17:57:43 +00001746 page_unprotect(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001747 }
1748}
1749
bellard6a00d602005-11-21 23:25:50 +00001750static inline void tlb_set_dirty(CPUState *env,
1751 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001752{
1753}
bellard9fa3e852004-01-04 18:06:42 +00001754#endif /* defined(CONFIG_USER_ONLY) */
1755
bellard33417e72003-08-10 21:47:01 +00001756/* register physical memory. 'size' must be a multiple of the target
1757 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
1758 io memory page */
bellard2e126692004-04-25 21:28:44 +00001759void cpu_register_physical_memory(target_phys_addr_t start_addr,
1760 unsigned long size,
1761 unsigned long phys_offset)
bellard33417e72003-08-10 21:47:01 +00001762{
bellard108c49b2005-07-24 12:55:09 +00001763 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00001764 PhysPageDesc *p;
bellard33417e72003-08-10 21:47:01 +00001765
bellard5fd386f2004-05-23 21:11:22 +00001766 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001767 end_addr = start_addr + size;
bellard5fd386f2004-05-23 21:11:22 +00001768 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
bellard108c49b2005-07-24 12:55:09 +00001769 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
bellard9fa3e852004-01-04 18:06:42 +00001770 p->phys_offset = phys_offset;
1771 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM)
bellard33417e72003-08-10 21:47:01 +00001772 phys_offset += TARGET_PAGE_SIZE;
1773 }
1774}
1775
bellarda4193c82004-06-03 14:01:43 +00001776static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00001777{
1778 return 0;
1779}
1780
bellarda4193c82004-06-03 14:01:43 +00001781static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00001782{
1783}
1784
1785static CPUReadMemoryFunc *unassigned_mem_read[3] = {
1786 unassigned_mem_readb,
1787 unassigned_mem_readb,
1788 unassigned_mem_readb,
1789};
1790
1791static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
1792 unassigned_mem_writeb,
1793 unassigned_mem_writeb,
1794 unassigned_mem_writeb,
1795};
1796
bellarda4193c82004-06-03 14:01:43 +00001797static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00001798{
bellard3a7d9292005-08-21 09:26:42 +00001799 unsigned long ram_addr;
1800 int dirty_flags;
1801 ram_addr = addr - (unsigned long)phys_ram_base;
1802 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1803 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1804#if !defined(CONFIG_USER_ONLY)
1805 tb_invalidate_phys_page_fast(ram_addr, 1);
1806 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1807#endif
1808 }
bellardc27004e2005-01-03 23:35:10 +00001809 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00001810#ifdef USE_KQEMU
1811 if (cpu_single_env->kqemu_enabled &&
1812 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
1813 kqemu_modify_page(cpu_single_env, ram_addr);
1814#endif
bellardf23db162005-08-21 19:12:28 +00001815 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1816 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
1817 /* we remove the notdirty callback only if the code has been
1818 flushed */
1819 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00001820 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00001821}
1822
bellarda4193c82004-06-03 14:01:43 +00001823static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00001824{
bellard3a7d9292005-08-21 09:26:42 +00001825 unsigned long ram_addr;
1826 int dirty_flags;
1827 ram_addr = addr - (unsigned long)phys_ram_base;
1828 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1829 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1830#if !defined(CONFIG_USER_ONLY)
1831 tb_invalidate_phys_page_fast(ram_addr, 2);
1832 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1833#endif
1834 }
bellardc27004e2005-01-03 23:35:10 +00001835 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00001836#ifdef USE_KQEMU
1837 if (cpu_single_env->kqemu_enabled &&
1838 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
1839 kqemu_modify_page(cpu_single_env, ram_addr);
1840#endif
bellardf23db162005-08-21 19:12:28 +00001841 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1842 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
1843 /* we remove the notdirty callback only if the code has been
1844 flushed */
1845 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00001846 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00001847}
1848
bellarda4193c82004-06-03 14:01:43 +00001849static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00001850{
bellard3a7d9292005-08-21 09:26:42 +00001851 unsigned long ram_addr;
1852 int dirty_flags;
1853 ram_addr = addr - (unsigned long)phys_ram_base;
1854 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1855 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1856#if !defined(CONFIG_USER_ONLY)
1857 tb_invalidate_phys_page_fast(ram_addr, 4);
1858 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
1859#endif
1860 }
bellardc27004e2005-01-03 23:35:10 +00001861 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00001862#ifdef USE_KQEMU
1863 if (cpu_single_env->kqemu_enabled &&
1864 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
1865 kqemu_modify_page(cpu_single_env, ram_addr);
1866#endif
bellardf23db162005-08-21 19:12:28 +00001867 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1868 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
1869 /* we remove the notdirty callback only if the code has been
1870 flushed */
1871 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00001872 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00001873}
1874
bellard3a7d9292005-08-21 09:26:42 +00001875static CPUReadMemoryFunc *error_mem_read[3] = {
1876 NULL, /* never used */
1877 NULL, /* never used */
1878 NULL, /* never used */
1879};
1880
bellard1ccde1c2004-02-06 19:46:14 +00001881static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
1882 notdirty_mem_writeb,
1883 notdirty_mem_writew,
1884 notdirty_mem_writel,
1885};
1886
bellard33417e72003-08-10 21:47:01 +00001887static void io_mem_init(void)
1888{
bellard3a7d9292005-08-21 09:26:42 +00001889 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00001890 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00001891 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00001892 io_mem_nb = 5;
1893
1894 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00001895 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00001896 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001897}
1898
1899/* mem_read and mem_write are arrays of functions containing the
1900 function to access byte (index 0), word (index 1) and dword (index
1901 2). All functions must be supplied. If io_index is non zero, the
1902 corresponding io zone is modified. If it is zero, a new io zone is
1903 allocated. The return value can be used with
1904 cpu_register_physical_memory(). (-1) is returned if error. */
1905int cpu_register_io_memory(int io_index,
1906 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00001907 CPUWriteMemoryFunc **mem_write,
1908 void *opaque)
bellard33417e72003-08-10 21:47:01 +00001909{
1910 int i;
1911
1912 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00001913 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00001914 return -1;
1915 io_index = io_mem_nb++;
1916 } else {
1917 if (io_index >= IO_MEM_NB_ENTRIES)
1918 return -1;
1919 }
bellardb5ff1b32005-11-26 10:38:39 +00001920
bellard33417e72003-08-10 21:47:01 +00001921 for(i = 0;i < 3; i++) {
1922 io_mem_read[io_index][i] = mem_read[i];
1923 io_mem_write[io_index][i] = mem_write[i];
1924 }
bellarda4193c82004-06-03 14:01:43 +00001925 io_mem_opaque[io_index] = opaque;
bellard33417e72003-08-10 21:47:01 +00001926 return io_index << IO_MEM_SHIFT;
1927}
bellard61382a52003-10-27 21:22:23 +00001928
bellard8926b512004-10-10 15:14:20 +00001929CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
1930{
1931 return io_mem_write[io_index >> IO_MEM_SHIFT];
1932}
1933
1934CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
1935{
1936 return io_mem_read[io_index >> IO_MEM_SHIFT];
1937}
1938
bellard13eb76e2004-01-24 15:23:36 +00001939/* physical memory access (slow version, mainly for debug) */
1940#if defined(CONFIG_USER_ONLY)
bellard2e126692004-04-25 21:28:44 +00001941void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00001942 int len, int is_write)
1943{
1944 int l, flags;
1945 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00001946 void * p;
bellard13eb76e2004-01-24 15:23:36 +00001947
1948 while (len > 0) {
1949 page = addr & TARGET_PAGE_MASK;
1950 l = (page + TARGET_PAGE_SIZE) - addr;
1951 if (l > len)
1952 l = len;
1953 flags = page_get_flags(page);
1954 if (!(flags & PAGE_VALID))
1955 return;
1956 if (is_write) {
1957 if (!(flags & PAGE_WRITE))
1958 return;
pbrook53a59602006-03-25 19:31:22 +00001959 p = lock_user(addr, len, 0);
1960 memcpy(p, buf, len);
1961 unlock_user(p, addr, len);
bellard13eb76e2004-01-24 15:23:36 +00001962 } else {
1963 if (!(flags & PAGE_READ))
1964 return;
pbrook53a59602006-03-25 19:31:22 +00001965 p = lock_user(addr, len, 1);
1966 memcpy(buf, p, len);
1967 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00001968 }
1969 len -= l;
1970 buf += l;
1971 addr += l;
1972 }
1973}
bellard8df1cd02005-01-28 22:37:22 +00001974
bellard13eb76e2004-01-24 15:23:36 +00001975#else
bellard2e126692004-04-25 21:28:44 +00001976void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00001977 int len, int is_write)
1978{
1979 int l, io_index;
1980 uint8_t *ptr;
1981 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00001982 target_phys_addr_t page;
1983 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00001984 PhysPageDesc *p;
bellard13eb76e2004-01-24 15:23:36 +00001985
1986 while (len > 0) {
1987 page = addr & TARGET_PAGE_MASK;
1988 l = (page + TARGET_PAGE_SIZE) - addr;
1989 if (l > len)
1990 l = len;
bellard92e873b2004-05-21 14:52:29 +00001991 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00001992 if (!p) {
1993 pd = IO_MEM_UNASSIGNED;
1994 } else {
1995 pd = p->phys_offset;
1996 }
1997
1998 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00001999 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002000 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002001 /* XXX: could force cpu_single_env to NULL to avoid
2002 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002003 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002004 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002005 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002006 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002007 l = 4;
2008 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002009 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002010 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002011 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002012 l = 2;
2013 } else {
bellard1c213d12005-09-03 10:49:04 +00002014 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002015 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002016 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002017 l = 1;
2018 }
2019 } else {
bellardb448f2f2004-02-25 23:24:04 +00002020 unsigned long addr1;
2021 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002022 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002023 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002024 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002025 if (!cpu_physical_memory_is_dirty(addr1)) {
2026 /* invalidate code */
2027 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2028 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002029 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2030 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002031 }
bellard13eb76e2004-01-24 15:23:36 +00002032 }
2033 } else {
bellard3a7d9292005-08-21 09:26:42 +00002034 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
bellard13eb76e2004-01-24 15:23:36 +00002035 /* I/O case */
2036 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2037 if (l >= 4 && ((addr & 3) == 0)) {
2038 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002039 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002040 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002041 l = 4;
2042 } else if (l >= 2 && ((addr & 1) == 0)) {
2043 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002044 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002045 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002046 l = 2;
2047 } else {
bellard1c213d12005-09-03 10:49:04 +00002048 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002049 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002050 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002051 l = 1;
2052 }
2053 } else {
2054 /* RAM case */
2055 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2056 (addr & ~TARGET_PAGE_MASK);
2057 memcpy(buf, ptr, l);
2058 }
2059 }
2060 len -= l;
2061 buf += l;
2062 addr += l;
2063 }
2064}
bellard8df1cd02005-01-28 22:37:22 +00002065
2066/* warning: addr must be aligned */
2067uint32_t ldl_phys(target_phys_addr_t addr)
2068{
2069 int io_index;
2070 uint8_t *ptr;
2071 uint32_t val;
2072 unsigned long pd;
2073 PhysPageDesc *p;
2074
2075 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2076 if (!p) {
2077 pd = IO_MEM_UNASSIGNED;
2078 } else {
2079 pd = p->phys_offset;
2080 }
2081
bellard3a7d9292005-08-21 09:26:42 +00002082 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
bellard8df1cd02005-01-28 22:37:22 +00002083 /* I/O case */
2084 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2085 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2086 } else {
2087 /* RAM case */
2088 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2089 (addr & ~TARGET_PAGE_MASK);
2090 val = ldl_p(ptr);
2091 }
2092 return val;
2093}
2094
bellard84b7b8e2005-11-28 21:19:04 +00002095/* warning: addr must be aligned */
2096uint64_t ldq_phys(target_phys_addr_t addr)
2097{
2098 int io_index;
2099 uint8_t *ptr;
2100 uint64_t val;
2101 unsigned long pd;
2102 PhysPageDesc *p;
2103
2104 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2105 if (!p) {
2106 pd = IO_MEM_UNASSIGNED;
2107 } else {
2108 pd = p->phys_offset;
2109 }
2110
2111 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
2112 /* I/O case */
2113 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2114#ifdef TARGET_WORDS_BIGENDIAN
2115 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2116 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2117#else
2118 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2119 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2120#endif
2121 } else {
2122 /* RAM case */
2123 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2124 (addr & ~TARGET_PAGE_MASK);
2125 val = ldq_p(ptr);
2126 }
2127 return val;
2128}
2129
bellardaab33092005-10-30 20:48:42 +00002130/* XXX: optimize */
2131uint32_t ldub_phys(target_phys_addr_t addr)
2132{
2133 uint8_t val;
2134 cpu_physical_memory_read(addr, &val, 1);
2135 return val;
2136}
2137
2138/* XXX: optimize */
2139uint32_t lduw_phys(target_phys_addr_t addr)
2140{
2141 uint16_t val;
2142 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2143 return tswap16(val);
2144}
2145
bellard8df1cd02005-01-28 22:37:22 +00002146/* warning: addr must be aligned. The ram page is not masked as dirty
2147 and the code inside is not invalidated. It is useful if the dirty
2148 bits are used to track modified PTEs */
2149void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2150{
2151 int io_index;
2152 uint8_t *ptr;
2153 unsigned long pd;
2154 PhysPageDesc *p;
2155
2156 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2157 if (!p) {
2158 pd = IO_MEM_UNASSIGNED;
2159 } else {
2160 pd = p->phys_offset;
2161 }
2162
bellard3a7d9292005-08-21 09:26:42 +00002163 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002164 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2165 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2166 } else {
2167 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2168 (addr & ~TARGET_PAGE_MASK);
2169 stl_p(ptr, val);
2170 }
2171}
2172
2173/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002174void stl_phys(target_phys_addr_t addr, uint32_t val)
2175{
2176 int io_index;
2177 uint8_t *ptr;
2178 unsigned long pd;
2179 PhysPageDesc *p;
2180
2181 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2182 if (!p) {
2183 pd = IO_MEM_UNASSIGNED;
2184 } else {
2185 pd = p->phys_offset;
2186 }
2187
bellard3a7d9292005-08-21 09:26:42 +00002188 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002189 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2190 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2191 } else {
2192 unsigned long addr1;
2193 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2194 /* RAM case */
2195 ptr = phys_ram_base + addr1;
2196 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002197 if (!cpu_physical_memory_is_dirty(addr1)) {
2198 /* invalidate code */
2199 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2200 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002201 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2202 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002203 }
bellard8df1cd02005-01-28 22:37:22 +00002204 }
2205}
2206
bellardaab33092005-10-30 20:48:42 +00002207/* XXX: optimize */
2208void stb_phys(target_phys_addr_t addr, uint32_t val)
2209{
2210 uint8_t v = val;
2211 cpu_physical_memory_write(addr, &v, 1);
2212}
2213
2214/* XXX: optimize */
2215void stw_phys(target_phys_addr_t addr, uint32_t val)
2216{
2217 uint16_t v = tswap16(val);
2218 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2219}
2220
2221/* XXX: optimize */
2222void stq_phys(target_phys_addr_t addr, uint64_t val)
2223{
2224 val = tswap64(val);
2225 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2226}
2227
bellard13eb76e2004-01-24 15:23:36 +00002228#endif
2229
2230/* virtual memory access for debug */
bellardb448f2f2004-02-25 23:24:04 +00002231int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
2232 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002233{
2234 int l;
2235 target_ulong page, phys_addr;
2236
2237 while (len > 0) {
2238 page = addr & TARGET_PAGE_MASK;
2239 phys_addr = cpu_get_phys_page_debug(env, page);
2240 /* if no physical page mapped, return an error */
2241 if (phys_addr == -1)
2242 return -1;
2243 l = (page + TARGET_PAGE_SIZE) - addr;
2244 if (l > len)
2245 l = len;
bellardb448f2f2004-02-25 23:24:04 +00002246 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
2247 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002248 len -= l;
2249 buf += l;
2250 addr += l;
2251 }
2252 return 0;
2253}
2254
bellarde3db7222005-01-26 22:00:47 +00002255void dump_exec_info(FILE *f,
2256 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
2257{
2258 int i, target_code_size, max_target_code_size;
2259 int direct_jmp_count, direct_jmp2_count, cross_page;
2260 TranslationBlock *tb;
2261
2262 target_code_size = 0;
2263 max_target_code_size = 0;
2264 cross_page = 0;
2265 direct_jmp_count = 0;
2266 direct_jmp2_count = 0;
2267 for(i = 0; i < nb_tbs; i++) {
2268 tb = &tbs[i];
2269 target_code_size += tb->size;
2270 if (tb->size > max_target_code_size)
2271 max_target_code_size = tb->size;
2272 if (tb->page_addr[1] != -1)
2273 cross_page++;
2274 if (tb->tb_next_offset[0] != 0xffff) {
2275 direct_jmp_count++;
2276 if (tb->tb_next_offset[1] != 0xffff) {
2277 direct_jmp2_count++;
2278 }
2279 }
2280 }
2281 /* XXX: avoid using doubles ? */
2282 cpu_fprintf(f, "TB count %d\n", nb_tbs);
2283 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
2284 nb_tbs ? target_code_size / nb_tbs : 0,
2285 max_target_code_size);
2286 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
2287 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
2288 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
2289 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
2290 cross_page,
2291 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
2292 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
2293 direct_jmp_count,
2294 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
2295 direct_jmp2_count,
2296 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
2297 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
2298 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
2299 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
2300}
2301
bellard61382a52003-10-27 21:22:23 +00002302#if !defined(CONFIG_USER_ONLY)
2303
2304#define MMUSUFFIX _cmmu
2305#define GETPC() NULL
2306#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00002307#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00002308
2309#define SHIFT 0
2310#include "softmmu_template.h"
2311
2312#define SHIFT 1
2313#include "softmmu_template.h"
2314
2315#define SHIFT 2
2316#include "softmmu_template.h"
2317
2318#define SHIFT 3
2319#include "softmmu_template.h"
2320
2321#undef env
2322
2323#endif