Merge commit 'b947c12c0bb217fe09968e652873e0d22b269d68' into upstream-merge
[qemu-kvm.git] / exec.c
blob9f748bc583b3df27c48676dfad60b9bbd9434181
1 /*
2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
20 #ifdef _WIN32
21 #include <windows.h>
22 #else
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #endif
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "cache-utils.h"
32 #if !defined(TARGET_IA64)
33 #include "tcg.h"
34 #endif
35 #include "qemu-kvm.h"
37 #include "hw/hw.h"
38 #include "hw/qdev.h"
39 #include "osdep.h"
40 #include "kvm.h"
41 #include "qemu-timer.h"
42 #if defined(CONFIG_USER_ONLY)
43 #include <qemu.h>
44 #include <signal.h>
45 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
46 #include <sys/param.h>
47 #if __FreeBSD_version >= 700104
48 #define HAVE_KINFO_GETVMMAP
49 #define sigqueue sigqueue_freebsd /* avoid redefinition */
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <machine/profile.h>
53 #define _KERNEL
54 #include <sys/user.h>
55 #undef _KERNEL
56 #undef sigqueue
57 #include <libutil.h>
58 #endif
59 #endif
60 #endif
62 //#define DEBUG_TB_INVALIDATE
63 //#define DEBUG_FLUSH
64 //#define DEBUG_TLB
65 //#define DEBUG_UNASSIGNED
67 /* make various TB consistency checks */
68 //#define DEBUG_TB_CHECK
69 //#define DEBUG_TLB_CHECK
71 //#define DEBUG_IOPORT
72 //#define DEBUG_SUBPAGE
74 #if !defined(CONFIG_USER_ONLY)
75 /* TB consistency checks only implemented for usermode emulation. */
76 #undef DEBUG_TB_CHECK
77 #endif
79 #define SMC_BITMAP_USE_THRESHOLD 10
81 static TranslationBlock *tbs;
82 static int code_gen_max_blocks;
83 TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
84 static int nb_tbs;
85 /* any access to the tbs or the page table must use this lock */
86 spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
88 #if defined(__arm__) || defined(__sparc_v9__)
89 /* The prologue must be reachable with a direct jump. ARM and Sparc64
90 have limited branch ranges (possibly also PPC) so place it in a
91 section close to code segment. */
92 #define code_gen_section \
93 __attribute__((__section__(".gen_code"))) \
94 __attribute__((aligned (32)))
95 #elif defined(_WIN32)
96 /* Maximum alignment for Win32 is 16. */
97 #define code_gen_section \
98 __attribute__((aligned (16)))
99 #else
100 #define code_gen_section \
101 __attribute__((aligned (32)))
102 #endif
104 uint8_t code_gen_prologue[1024] code_gen_section;
105 static uint8_t *code_gen_buffer;
106 static unsigned long code_gen_buffer_size;
107 /* threshold to flush the translated code buffer */
108 static unsigned long code_gen_buffer_max_size;
109 static uint8_t *code_gen_ptr;
111 #if !defined(CONFIG_USER_ONLY)
112 int phys_ram_fd;
113 static int in_migration;
115 RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list) };
116 #endif
118 CPUState *first_cpu;
119 /* current CPU in the current thread. It is only valid inside
120 cpu_exec() */
121 CPUState *cpu_single_env;
122 /* 0 = Do not count executed instructions.
123 1 = Precise instruction counting.
124 2 = Adaptive rate instruction counting. */
125 int use_icount = 0;
126 /* Current instruction counter. While executing translated code this may
127 include some instructions that have not yet been executed. */
128 int64_t qemu_icount;
130 typedef struct PageDesc {
131 /* list of TBs intersecting this ram page */
132 TranslationBlock *first_tb;
133 /* in order to optimize self modifying code, we count the number
134 of lookups we do to a given page to use a bitmap */
135 unsigned int code_write_count;
136 uint8_t *code_bitmap;
137 #if defined(CONFIG_USER_ONLY)
138 unsigned long flags;
139 #endif
140 } PageDesc;
142 /* In system mode we want L1_MAP to be based on ram offsets,
143 while in user mode we want it to be based on virtual addresses. */
144 #if !defined(CONFIG_USER_ONLY)
145 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
146 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
147 #else
148 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
149 #endif
150 #else
151 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
152 #endif
154 /* Size of the L2 (and L3, etc) page tables. */
155 #define L2_BITS 10
156 #define L2_SIZE (1 << L2_BITS)
158 /* The bits remaining after N lower levels of page tables. */
159 #define P_L1_BITS_REM \
160 ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
161 #define V_L1_BITS_REM \
162 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
164 /* Size of the L1 page table. Avoid silly small sizes. */
165 #if P_L1_BITS_REM < 4
166 #define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
167 #else
168 #define P_L1_BITS P_L1_BITS_REM
169 #endif
171 #if V_L1_BITS_REM < 4
172 #define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
173 #else
174 #define V_L1_BITS V_L1_BITS_REM
175 #endif
177 #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
178 #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
180 #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
181 #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
183 unsigned long qemu_real_host_page_size;
184 unsigned long qemu_host_page_bits;
185 unsigned long qemu_host_page_size;
186 unsigned long qemu_host_page_mask;
188 /* This is a multi-level map on the virtual address space.
189 The bottom level has pointers to PageDesc. */
190 static void *l1_map[V_L1_SIZE];
192 #if !defined(CONFIG_USER_ONLY)
193 typedef struct PhysPageDesc {
194 /* offset in host memory of the page + io_index in the low bits */
195 ram_addr_t phys_offset;
196 ram_addr_t region_offset;
197 } PhysPageDesc;
199 /* This is a multi-level map on the physical address space.
200 The bottom level has pointers to PhysPageDesc. */
201 static void *l1_phys_map[P_L1_SIZE];
203 static void io_mem_init(void);
205 /* io memory support */
206 CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
207 CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
208 void *io_mem_opaque[IO_MEM_NB_ENTRIES];
209 static char io_mem_used[IO_MEM_NB_ENTRIES];
210 static int io_mem_watch;
211 #endif
213 /* log support */
214 #ifdef WIN32
215 static const char *logfilename = "qemu.log";
216 #else
217 static const char *logfilename = "/tmp/qemu.log";
218 #endif
219 FILE *logfile;
220 int loglevel;
221 static int log_append = 0;
223 /* statistics */
224 #if !defined(CONFIG_USER_ONLY)
225 static int tlb_flush_count;
226 #endif
227 static int tb_flush_count;
228 static int tb_phys_invalidate_count;
230 #ifdef _WIN32
231 static void map_exec(void *addr, long size)
233 DWORD old_protect;
234 VirtualProtect(addr, size,
235 PAGE_EXECUTE_READWRITE, &old_protect);
238 #else
239 static void map_exec(void *addr, long size)
241 unsigned long start, end, page_size;
243 page_size = getpagesize();
244 start = (unsigned long)addr;
245 start &= ~(page_size - 1);
247 end = (unsigned long)addr + size;
248 end += page_size - 1;
249 end &= ~(page_size - 1);
251 mprotect((void *)start, end - start,
252 PROT_READ | PROT_WRITE | PROT_EXEC);
254 #endif
256 static void page_init(void)
258 /* NOTE: we can always suppose that qemu_host_page_size >=
259 TARGET_PAGE_SIZE */
260 #ifdef _WIN32
262 SYSTEM_INFO system_info;
264 GetSystemInfo(&system_info);
265 qemu_real_host_page_size = system_info.dwPageSize;
267 #else
268 qemu_real_host_page_size = getpagesize();
269 #endif
270 if (qemu_host_page_size == 0)
271 qemu_host_page_size = qemu_real_host_page_size;
272 if (qemu_host_page_size < TARGET_PAGE_SIZE)
273 qemu_host_page_size = TARGET_PAGE_SIZE;
274 qemu_host_page_bits = 0;
275 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
276 qemu_host_page_bits++;
277 qemu_host_page_mask = ~(qemu_host_page_size - 1);
279 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
281 #ifdef HAVE_KINFO_GETVMMAP
282 struct kinfo_vmentry *freep;
283 int i, cnt;
285 freep = kinfo_getvmmap(getpid(), &cnt);
286 if (freep) {
287 mmap_lock();
288 for (i = 0; i < cnt; i++) {
289 unsigned long startaddr, endaddr;
291 startaddr = freep[i].kve_start;
292 endaddr = freep[i].kve_end;
293 if (h2g_valid(startaddr)) {
294 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
296 if (h2g_valid(endaddr)) {
297 endaddr = h2g(endaddr);
298 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
299 } else {
300 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
301 endaddr = ~0ul;
302 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
303 #endif
307 free(freep);
308 mmap_unlock();
310 #else
311 FILE *f;
313 last_brk = (unsigned long)sbrk(0);
315 f = fopen("/compat/linux/proc/self/maps", "r");
316 if (f) {
317 mmap_lock();
319 do {
320 unsigned long startaddr, endaddr;
321 int n;
323 n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
325 if (n == 2 && h2g_valid(startaddr)) {
326 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
328 if (h2g_valid(endaddr)) {
329 endaddr = h2g(endaddr);
330 } else {
331 endaddr = ~0ul;
333 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
335 } while (!feof(f));
337 fclose(f);
338 mmap_unlock();
340 #endif
342 #endif
345 static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
347 PageDesc *pd;
348 void **lp;
349 int i;
351 #if defined(CONFIG_USER_ONLY)
352 /* We can't use qemu_malloc because it may recurse into a locked mutex. */
353 # define ALLOC(P, SIZE) \
354 do { \
355 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
356 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
357 } while (0)
358 #else
359 # define ALLOC(P, SIZE) \
360 do { P = qemu_mallocz(SIZE); } while (0)
361 #endif
363 /* Level 1. Always allocated. */
364 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
366 /* Level 2..N-1. */
367 for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
368 void **p = *lp;
370 if (p == NULL) {
371 if (!alloc) {
372 return NULL;
374 ALLOC(p, sizeof(void *) * L2_SIZE);
375 *lp = p;
378 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
381 pd = *lp;
382 if (pd == NULL) {
383 if (!alloc) {
384 return NULL;
386 ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
387 *lp = pd;
390 #undef ALLOC
392 return pd + (index & (L2_SIZE - 1));
395 static inline PageDesc *page_find(tb_page_addr_t index)
397 return page_find_alloc(index, 0);
400 #if !defined(CONFIG_USER_ONLY)
401 static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
403 PhysPageDesc *pd;
404 void **lp;
405 int i;
407 /* Level 1. Always allocated. */
408 lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
410 /* Level 2..N-1. */
411 for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
412 void **p = *lp;
413 if (p == NULL) {
414 if (!alloc) {
415 return NULL;
417 *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE);
419 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
422 pd = *lp;
423 if (pd == NULL) {
424 int i;
426 if (!alloc) {
427 return NULL;
430 *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE);
432 for (i = 0; i < L2_SIZE; i++) {
433 pd[i].phys_offset = IO_MEM_UNASSIGNED;
434 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
438 return pd + (index & (L2_SIZE - 1));
441 static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
443 return phys_page_find_alloc(index, 0);
446 static void tlb_protect_code(ram_addr_t ram_addr);
447 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
448 target_ulong vaddr);
449 #define mmap_lock() do { } while(0)
450 #define mmap_unlock() do { } while(0)
451 #endif
453 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
455 #if defined(CONFIG_USER_ONLY)
456 /* Currently it is not recommended to allocate big chunks of data in
457 user mode. It will change when a dedicated libc will be used */
458 #define USE_STATIC_CODE_GEN_BUFFER
459 #endif
461 #ifdef USE_STATIC_CODE_GEN_BUFFER
462 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
463 __attribute__((aligned (CODE_GEN_ALIGN)));
464 #endif
466 static void code_gen_alloc(unsigned long tb_size)
468 if (kvm_enabled())
469 return;
471 #ifdef USE_STATIC_CODE_GEN_BUFFER
472 code_gen_buffer = static_code_gen_buffer;
473 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
474 map_exec(code_gen_buffer, code_gen_buffer_size);
475 #else
476 code_gen_buffer_size = tb_size;
477 if (code_gen_buffer_size == 0) {
478 #if defined(CONFIG_USER_ONLY)
479 /* in user mode, phys_ram_size is not meaningful */
480 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
481 #else
482 /* XXX: needs adjustments */
483 code_gen_buffer_size = (unsigned long)(ram_size / 4);
484 #endif
486 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
487 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
488 /* The code gen buffer location may have constraints depending on
489 the host cpu and OS */
490 #if defined(__linux__)
492 int flags;
493 void *start = NULL;
495 flags = MAP_PRIVATE | MAP_ANONYMOUS;
496 #if defined(__x86_64__)
497 flags |= MAP_32BIT;
498 /* Cannot map more than that */
499 if (code_gen_buffer_size > (800 * 1024 * 1024))
500 code_gen_buffer_size = (800 * 1024 * 1024);
501 #elif defined(__sparc_v9__)
502 // Map the buffer below 2G, so we can use direct calls and branches
503 flags |= MAP_FIXED;
504 start = (void *) 0x60000000UL;
505 if (code_gen_buffer_size > (512 * 1024 * 1024))
506 code_gen_buffer_size = (512 * 1024 * 1024);
507 #elif defined(__arm__)
508 /* Map the buffer below 32M, so we can use direct calls and branches */
509 flags |= MAP_FIXED;
510 start = (void *) 0x01000000UL;
511 if (code_gen_buffer_size > 16 * 1024 * 1024)
512 code_gen_buffer_size = 16 * 1024 * 1024;
513 #elif defined(__s390x__)
514 /* Map the buffer so that we can use direct calls and branches. */
515 /* We have a +- 4GB range on the branches; leave some slop. */
516 if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) {
517 code_gen_buffer_size = 3ul * 1024 * 1024 * 1024;
519 start = (void *)0x90000000UL;
520 #endif
521 code_gen_buffer = mmap(start, code_gen_buffer_size,
522 PROT_WRITE | PROT_READ | PROT_EXEC,
523 flags, -1, 0);
524 if (code_gen_buffer == MAP_FAILED) {
525 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
526 exit(1);
529 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
530 || defined(__DragonFly__) || defined(__OpenBSD__)
532 int flags;
533 void *addr = NULL;
534 flags = MAP_PRIVATE | MAP_ANONYMOUS;
535 #if defined(__x86_64__)
536 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
537 * 0x40000000 is free */
538 flags |= MAP_FIXED;
539 addr = (void *)0x40000000;
540 /* Cannot map more than that */
541 if (code_gen_buffer_size > (800 * 1024 * 1024))
542 code_gen_buffer_size = (800 * 1024 * 1024);
543 #elif defined(__sparc_v9__)
544 // Map the buffer below 2G, so we can use direct calls and branches
545 flags |= MAP_FIXED;
546 addr = (void *) 0x60000000UL;
547 if (code_gen_buffer_size > (512 * 1024 * 1024)) {
548 code_gen_buffer_size = (512 * 1024 * 1024);
550 #endif
551 code_gen_buffer = mmap(addr, code_gen_buffer_size,
552 PROT_WRITE | PROT_READ | PROT_EXEC,
553 flags, -1, 0);
554 if (code_gen_buffer == MAP_FAILED) {
555 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
556 exit(1);
559 #else
560 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
561 map_exec(code_gen_buffer, code_gen_buffer_size);
562 #endif
563 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
564 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
565 code_gen_buffer_max_size = code_gen_buffer_size -
566 (TCG_MAX_OP_SIZE * OPC_MAX_SIZE);
567 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
568 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
571 /* Must be called before using the QEMU cpus. 'tb_size' is the size
572 (in bytes) allocated to the translation buffer. Zero means default
573 size. */
574 void cpu_exec_init_all(unsigned long tb_size)
576 cpu_gen_init();
577 code_gen_alloc(tb_size);
578 code_gen_ptr = code_gen_buffer;
579 page_init();
580 #if !defined(CONFIG_USER_ONLY)
581 io_mem_init();
582 #endif
583 #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
584 /* There's no guest base to take into account, so go ahead and
585 initialize the prologue now. */
586 tcg_prologue_init(&tcg_ctx);
587 #endif
590 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
592 static int cpu_common_post_load(void *opaque, int version_id)
594 CPUState *env = opaque;
596 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
597 version_id is increased. */
598 env->interrupt_request &= ~0x01;
599 tlb_flush(env, 1);
601 return 0;
604 static const VMStateDescription vmstate_cpu_common = {
605 .name = "cpu_common",
606 .version_id = 1,
607 .minimum_version_id = 1,
608 .minimum_version_id_old = 1,
609 .post_load = cpu_common_post_load,
610 .fields = (VMStateField []) {
611 VMSTATE_UINT32(halted, CPUState),
612 VMSTATE_UINT32(interrupt_request, CPUState),
613 VMSTATE_END_OF_LIST()
616 #endif
618 CPUState *qemu_get_cpu(int cpu)
620 CPUState *env = first_cpu;
622 while (env) {
623 if (env->cpu_index == cpu)
624 break;
625 env = env->next_cpu;
628 return env;
631 void cpu_exec_init(CPUState *env)
633 CPUState **penv;
634 int cpu_index;
636 #if defined(CONFIG_USER_ONLY)
637 cpu_list_lock();
638 #endif
639 env->next_cpu = NULL;
640 penv = &first_cpu;
641 cpu_index = 0;
642 while (*penv != NULL) {
643 penv = &(*penv)->next_cpu;
644 cpu_index++;
646 env->cpu_index = cpu_index;
647 env->numa_node = 0;
648 QTAILQ_INIT(&env->breakpoints);
649 QTAILQ_INIT(&env->watchpoints);
650 #ifdef __WIN32
651 env->thread_id = GetCurrentProcessId();
652 #else
653 env->thread_id = getpid();
654 #endif
655 *penv = env;
656 #if defined(CONFIG_USER_ONLY)
657 cpu_list_unlock();
658 #endif
659 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
660 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
661 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
662 cpu_save, cpu_load, env);
663 #endif
666 static inline void invalidate_page_bitmap(PageDesc *p)
668 if (p->code_bitmap) {
669 qemu_free(p->code_bitmap);
670 p->code_bitmap = NULL;
672 p->code_write_count = 0;
675 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
677 static void page_flush_tb_1 (int level, void **lp)
679 int i;
681 if (*lp == NULL) {
682 return;
684 if (level == 0) {
685 PageDesc *pd = *lp;
686 for (i = 0; i < L2_SIZE; ++i) {
687 pd[i].first_tb = NULL;
688 invalidate_page_bitmap(pd + i);
690 } else {
691 void **pp = *lp;
692 for (i = 0; i < L2_SIZE; ++i) {
693 page_flush_tb_1 (level - 1, pp + i);
698 static void page_flush_tb(void)
700 int i;
701 for (i = 0; i < V_L1_SIZE; i++) {
702 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
706 /* flush all the translation blocks */
707 /* XXX: tb_flush is currently not thread safe */
708 void tb_flush(CPUState *env1)
710 CPUState *env;
711 #if defined(DEBUG_FLUSH)
712 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
713 (unsigned long)(code_gen_ptr - code_gen_buffer),
714 nb_tbs, nb_tbs > 0 ?
715 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
716 #endif
717 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
718 cpu_abort(env1, "Internal error: code buffer overflow\n");
720 nb_tbs = 0;
722 for(env = first_cpu; env != NULL; env = env->next_cpu) {
723 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
726 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
727 page_flush_tb();
729 code_gen_ptr = code_gen_buffer;
730 /* XXX: flush processor icache at this point if cache flush is
731 expensive */
732 tb_flush_count++;
735 #ifdef DEBUG_TB_CHECK
737 static void tb_invalidate_check(target_ulong address)
739 TranslationBlock *tb;
740 int i;
741 address &= TARGET_PAGE_MASK;
742 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
743 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
744 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
745 address >= tb->pc + tb->size)) {
746 printf("ERROR invalidate: address=" TARGET_FMT_lx
747 " PC=%08lx size=%04x\n",
748 address, (long)tb->pc, tb->size);
754 /* verify that all the pages have correct rights for code */
755 static void tb_page_check(void)
757 TranslationBlock *tb;
758 int i, flags1, flags2;
760 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
761 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
762 flags1 = page_get_flags(tb->pc);
763 flags2 = page_get_flags(tb->pc + tb->size - 1);
764 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
765 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
766 (long)tb->pc, tb->size, flags1, flags2);
772 #endif
774 /* invalidate one TB */
775 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
776 int next_offset)
778 TranslationBlock *tb1;
779 for(;;) {
780 tb1 = *ptb;
781 if (tb1 == tb) {
782 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
783 break;
785 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
789 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
791 TranslationBlock *tb1;
792 unsigned int n1;
794 for(;;) {
795 tb1 = *ptb;
796 n1 = (long)tb1 & 3;
797 tb1 = (TranslationBlock *)((long)tb1 & ~3);
798 if (tb1 == tb) {
799 *ptb = tb1->page_next[n1];
800 break;
802 ptb = &tb1->page_next[n1];
806 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
808 TranslationBlock *tb1, **ptb;
809 unsigned int n1;
811 ptb = &tb->jmp_next[n];
812 tb1 = *ptb;
813 if (tb1) {
814 /* find tb(n) in circular list */
815 for(;;) {
816 tb1 = *ptb;
817 n1 = (long)tb1 & 3;
818 tb1 = (TranslationBlock *)((long)tb1 & ~3);
819 if (n1 == n && tb1 == tb)
820 break;
821 if (n1 == 2) {
822 ptb = &tb1->jmp_first;
823 } else {
824 ptb = &tb1->jmp_next[n1];
827 /* now we can suppress tb(n) from the list */
828 *ptb = tb->jmp_next[n];
830 tb->jmp_next[n] = NULL;
834 /* reset the jump entry 'n' of a TB so that it is not chained to
835 another TB */
836 static inline void tb_reset_jump(TranslationBlock *tb, int n)
838 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
841 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
843 CPUState *env;
844 PageDesc *p;
845 unsigned int h, n1;
846 tb_page_addr_t phys_pc;
847 TranslationBlock *tb1, *tb2;
849 /* remove the TB from the hash list */
850 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
851 h = tb_phys_hash_func(phys_pc);
852 tb_remove(&tb_phys_hash[h], tb,
853 offsetof(TranslationBlock, phys_hash_next));
855 /* remove the TB from the page list */
856 if (tb->page_addr[0] != page_addr) {
857 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
858 tb_page_remove(&p->first_tb, tb);
859 invalidate_page_bitmap(p);
861 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
862 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
863 tb_page_remove(&p->first_tb, tb);
864 invalidate_page_bitmap(p);
867 tb_invalidated_flag = 1;
869 /* remove the TB from the hash list */
870 h = tb_jmp_cache_hash_func(tb->pc);
871 for(env = first_cpu; env != NULL; env = env->next_cpu) {
872 if (env->tb_jmp_cache[h] == tb)
873 env->tb_jmp_cache[h] = NULL;
876 /* suppress this TB from the two jump lists */
877 tb_jmp_remove(tb, 0);
878 tb_jmp_remove(tb, 1);
880 /* suppress any remaining jumps to this TB */
881 tb1 = tb->jmp_first;
882 for(;;) {
883 n1 = (long)tb1 & 3;
884 if (n1 == 2)
885 break;
886 tb1 = (TranslationBlock *)((long)tb1 & ~3);
887 tb2 = tb1->jmp_next[n1];
888 tb_reset_jump(tb1, n1);
889 tb1->jmp_next[n1] = NULL;
890 tb1 = tb2;
892 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
894 tb_phys_invalidate_count++;
897 static inline void set_bits(uint8_t *tab, int start, int len)
899 int end, mask, end1;
901 end = start + len;
902 tab += start >> 3;
903 mask = 0xff << (start & 7);
904 if ((start & ~7) == (end & ~7)) {
905 if (start < end) {
906 mask &= ~(0xff << (end & 7));
907 *tab |= mask;
909 } else {
910 *tab++ |= mask;
911 start = (start + 8) & ~7;
912 end1 = end & ~7;
913 while (start < end1) {
914 *tab++ = 0xff;
915 start += 8;
917 if (start < end) {
918 mask = ~(0xff << (end & 7));
919 *tab |= mask;
924 static void build_page_bitmap(PageDesc *p)
926 int n, tb_start, tb_end;
927 TranslationBlock *tb;
929 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
931 tb = p->first_tb;
932 while (tb != NULL) {
933 n = (long)tb & 3;
934 tb = (TranslationBlock *)((long)tb & ~3);
935 /* NOTE: this is subtle as a TB may span two physical pages */
936 if (n == 0) {
937 /* NOTE: tb_end may be after the end of the page, but
938 it is not a problem */
939 tb_start = tb->pc & ~TARGET_PAGE_MASK;
940 tb_end = tb_start + tb->size;
941 if (tb_end > TARGET_PAGE_SIZE)
942 tb_end = TARGET_PAGE_SIZE;
943 } else {
944 tb_start = 0;
945 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
947 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
948 tb = tb->page_next[n];
952 TranslationBlock *tb_gen_code(CPUState *env,
953 target_ulong pc, target_ulong cs_base,
954 int flags, int cflags)
956 TranslationBlock *tb;
957 uint8_t *tc_ptr;
958 tb_page_addr_t phys_pc, phys_page2;
959 target_ulong virt_page2;
960 int code_gen_size;
962 phys_pc = get_page_addr_code(env, pc);
963 tb = tb_alloc(pc);
964 if (!tb) {
965 /* flush must be done */
966 tb_flush(env);
967 /* cannot fail at this point */
968 tb = tb_alloc(pc);
969 /* Don't forget to invalidate previous TB info. */
970 tb_invalidated_flag = 1;
972 tc_ptr = code_gen_ptr;
973 tb->tc_ptr = tc_ptr;
974 tb->cs_base = cs_base;
975 tb->flags = flags;
976 tb->cflags = cflags;
977 cpu_gen_code(env, tb, &code_gen_size);
978 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
980 /* check next page if needed */
981 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
982 phys_page2 = -1;
983 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
984 phys_page2 = get_page_addr_code(env, virt_page2);
986 tb_link_page(tb, phys_pc, phys_page2);
987 return tb;
990 /* invalidate all TBs which intersect with the target physical page
991 starting in range [start;end[. NOTE: start and end must refer to
992 the same physical page. 'is_cpu_write_access' should be true if called
993 from a real cpu write access: the virtual CPU will exit the current
994 TB if code is modified inside this TB. */
995 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
996 int is_cpu_write_access)
998 TranslationBlock *tb, *tb_next, *saved_tb;
999 CPUState *env = cpu_single_env;
1000 tb_page_addr_t tb_start, tb_end;
1001 PageDesc *p;
1002 int n;
1003 #ifdef TARGET_HAS_PRECISE_SMC
1004 int current_tb_not_found = is_cpu_write_access;
1005 TranslationBlock *current_tb = NULL;
1006 int current_tb_modified = 0;
1007 target_ulong current_pc = 0;
1008 target_ulong current_cs_base = 0;
1009 int current_flags = 0;
1010 #endif /* TARGET_HAS_PRECISE_SMC */
1012 p = page_find(start >> TARGET_PAGE_BITS);
1013 if (!p)
1014 return;
1015 if (!p->code_bitmap &&
1016 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1017 is_cpu_write_access) {
1018 /* build code bitmap */
1019 build_page_bitmap(p);
1022 /* we remove all the TBs in the range [start, end[ */
1023 /* XXX: see if in some cases it could be faster to invalidate all the code */
1024 tb = p->first_tb;
1025 while (tb != NULL) {
1026 n = (long)tb & 3;
1027 tb = (TranslationBlock *)((long)tb & ~3);
1028 tb_next = tb->page_next[n];
1029 /* NOTE: this is subtle as a TB may span two physical pages */
1030 if (n == 0) {
1031 /* NOTE: tb_end may be after the end of the page, but
1032 it is not a problem */
1033 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1034 tb_end = tb_start + tb->size;
1035 } else {
1036 tb_start = tb->page_addr[1];
1037 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1039 if (!(tb_end <= start || tb_start >= end)) {
1040 #ifdef TARGET_HAS_PRECISE_SMC
1041 if (current_tb_not_found) {
1042 current_tb_not_found = 0;
1043 current_tb = NULL;
1044 if (env->mem_io_pc) {
1045 /* now we have a real cpu fault */
1046 current_tb = tb_find_pc(env->mem_io_pc);
1049 if (current_tb == tb &&
1050 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1051 /* If we are modifying the current TB, we must stop
1052 its execution. We could be more precise by checking
1053 that the modification is after the current PC, but it
1054 would require a specialized function to partially
1055 restore the CPU state */
1057 current_tb_modified = 1;
1058 cpu_restore_state(current_tb, env,
1059 env->mem_io_pc, NULL);
1060 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1061 &current_flags);
1063 #endif /* TARGET_HAS_PRECISE_SMC */
1064 /* we need to do that to handle the case where a signal
1065 occurs while doing tb_phys_invalidate() */
1066 saved_tb = NULL;
1067 if (env) {
1068 saved_tb = env->current_tb;
1069 env->current_tb = NULL;
1071 tb_phys_invalidate(tb, -1);
1072 if (env) {
1073 env->current_tb = saved_tb;
1074 if (env->interrupt_request && env->current_tb)
1075 cpu_interrupt(env, env->interrupt_request);
1078 tb = tb_next;
1080 #if !defined(CONFIG_USER_ONLY)
1081 /* if no code remaining, no need to continue to use slow writes */
1082 if (!p->first_tb) {
1083 invalidate_page_bitmap(p);
1084 if (is_cpu_write_access) {
1085 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
1088 #endif
1089 #ifdef TARGET_HAS_PRECISE_SMC
1090 if (current_tb_modified) {
1091 /* we generate a block containing just the instruction
1092 modifying the memory. It will ensure that it cannot modify
1093 itself */
1094 env->current_tb = NULL;
1095 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1096 cpu_resume_from_signal(env, NULL);
1098 #endif
1101 /* len must be <= 8 and start must be a multiple of len */
1102 static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
1104 PageDesc *p;
1105 int offset, b;
1106 #if 0
1107 if (1) {
1108 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1109 cpu_single_env->mem_io_vaddr, len,
1110 cpu_single_env->eip,
1111 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
1113 #endif
1114 p = page_find(start >> TARGET_PAGE_BITS);
1115 if (!p)
1116 return;
1117 if (p->code_bitmap) {
1118 offset = start & ~TARGET_PAGE_MASK;
1119 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1120 if (b & ((1 << len) - 1))
1121 goto do_invalidate;
1122 } else {
1123 do_invalidate:
1124 tb_invalidate_phys_page_range(start, start + len, 1);
1128 #if !defined(CONFIG_SOFTMMU)
1129 static void tb_invalidate_phys_page(tb_page_addr_t addr,
1130 unsigned long pc, void *puc)
1132 TranslationBlock *tb;
1133 PageDesc *p;
1134 int n;
1135 #ifdef TARGET_HAS_PRECISE_SMC
1136 TranslationBlock *current_tb = NULL;
1137 CPUState *env = cpu_single_env;
1138 int current_tb_modified = 0;
1139 target_ulong current_pc = 0;
1140 target_ulong current_cs_base = 0;
1141 int current_flags = 0;
1142 #endif
1144 addr &= TARGET_PAGE_MASK;
1145 p = page_find(addr >> TARGET_PAGE_BITS);
1146 if (!p)
1147 return;
1148 tb = p->first_tb;
1149 #ifdef TARGET_HAS_PRECISE_SMC
1150 if (tb && pc != 0) {
1151 current_tb = tb_find_pc(pc);
1153 #endif
1154 while (tb != NULL) {
1155 n = (long)tb & 3;
1156 tb = (TranslationBlock *)((long)tb & ~3);
1157 #ifdef TARGET_HAS_PRECISE_SMC
1158 if (current_tb == tb &&
1159 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1160 /* If we are modifying the current TB, we must stop
1161 its execution. We could be more precise by checking
1162 that the modification is after the current PC, but it
1163 would require a specialized function to partially
1164 restore the CPU state */
1166 current_tb_modified = 1;
1167 cpu_restore_state(current_tb, env, pc, puc);
1168 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1169 &current_flags);
1171 #endif /* TARGET_HAS_PRECISE_SMC */
1172 tb_phys_invalidate(tb, addr);
1173 tb = tb->page_next[n];
1175 p->first_tb = NULL;
1176 #ifdef TARGET_HAS_PRECISE_SMC
1177 if (current_tb_modified) {
1178 /* we generate a block containing just the instruction
1179 modifying the memory. It will ensure that it cannot modify
1180 itself */
1181 env->current_tb = NULL;
1182 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1183 cpu_resume_from_signal(env, puc);
1185 #endif
1187 #endif
1189 /* add the tb in the target page and protect it if necessary */
1190 static inline void tb_alloc_page(TranslationBlock *tb,
1191 unsigned int n, tb_page_addr_t page_addr)
1193 PageDesc *p;
1194 TranslationBlock *last_first_tb;
1196 tb->page_addr[n] = page_addr;
1197 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
1198 tb->page_next[n] = p->first_tb;
1199 last_first_tb = p->first_tb;
1200 p->first_tb = (TranslationBlock *)((long)tb | n);
1201 invalidate_page_bitmap(p);
1203 #if defined(TARGET_HAS_SMC) || 1
1205 #if defined(CONFIG_USER_ONLY)
1206 if (p->flags & PAGE_WRITE) {
1207 target_ulong addr;
1208 PageDesc *p2;
1209 int prot;
1211 /* force the host page as non writable (writes will have a
1212 page fault + mprotect overhead) */
1213 page_addr &= qemu_host_page_mask;
1214 prot = 0;
1215 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1216 addr += TARGET_PAGE_SIZE) {
1218 p2 = page_find (addr >> TARGET_PAGE_BITS);
1219 if (!p2)
1220 continue;
1221 prot |= p2->flags;
1222 p2->flags &= ~PAGE_WRITE;
1224 mprotect(g2h(page_addr), qemu_host_page_size,
1225 (prot & PAGE_BITS) & ~PAGE_WRITE);
1226 #ifdef DEBUG_TB_INVALIDATE
1227 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1228 page_addr);
1229 #endif
1231 #else
1232 /* if some code is already present, then the pages are already
1233 protected. So we handle the case where only the first TB is
1234 allocated in a physical page */
1235 if (!last_first_tb) {
1236 tlb_protect_code(page_addr);
1238 #endif
1240 #endif /* TARGET_HAS_SMC */
1243 /* Allocate a new translation block. Flush the translation buffer if
1244 too many translation blocks or too much generated code. */
1245 TranslationBlock *tb_alloc(target_ulong pc)
1247 TranslationBlock *tb;
1249 if (nb_tbs >= code_gen_max_blocks ||
1250 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
1251 return NULL;
1252 tb = &tbs[nb_tbs++];
1253 tb->pc = pc;
1254 tb->cflags = 0;
1255 return tb;
1258 void tb_free(TranslationBlock *tb)
1260 /* In practice this is mostly used for single use temporary TB
1261 Ignore the hard cases and just back up if this TB happens to
1262 be the last one generated. */
1263 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1264 code_gen_ptr = tb->tc_ptr;
1265 nb_tbs--;
1269 /* add a new TB and link it to the physical page tables. phys_page2 is
1270 (-1) to indicate that only one page contains the TB. */
1271 void tb_link_page(TranslationBlock *tb,
1272 tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
1274 unsigned int h;
1275 TranslationBlock **ptb;
1277 /* Grab the mmap lock to stop another thread invalidating this TB
1278 before we are done. */
1279 mmap_lock();
1280 /* add in the physical hash table */
1281 h = tb_phys_hash_func(phys_pc);
1282 ptb = &tb_phys_hash[h];
1283 tb->phys_hash_next = *ptb;
1284 *ptb = tb;
1286 /* add in the page list */
1287 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1288 if (phys_page2 != -1)
1289 tb_alloc_page(tb, 1, phys_page2);
1290 else
1291 tb->page_addr[1] = -1;
1293 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1294 tb->jmp_next[0] = NULL;
1295 tb->jmp_next[1] = NULL;
1297 /* init original jump addresses */
1298 if (tb->tb_next_offset[0] != 0xffff)
1299 tb_reset_jump(tb, 0);
1300 if (tb->tb_next_offset[1] != 0xffff)
1301 tb_reset_jump(tb, 1);
1303 #ifdef DEBUG_TB_CHECK
1304 tb_page_check();
1305 #endif
1306 mmap_unlock();
1309 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1310 tb[1].tc_ptr. Return NULL if not found */
1311 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1313 int m_min, m_max, m;
1314 unsigned long v;
1315 TranslationBlock *tb;
1317 if (nb_tbs <= 0)
1318 return NULL;
1319 if (tc_ptr < (unsigned long)code_gen_buffer ||
1320 tc_ptr >= (unsigned long)code_gen_ptr)
1321 return NULL;
1322 /* binary search (cf Knuth) */
1323 m_min = 0;
1324 m_max = nb_tbs - 1;
1325 while (m_min <= m_max) {
1326 m = (m_min + m_max) >> 1;
1327 tb = &tbs[m];
1328 v = (unsigned long)tb->tc_ptr;
1329 if (v == tc_ptr)
1330 return tb;
1331 else if (tc_ptr < v) {
1332 m_max = m - 1;
1333 } else {
1334 m_min = m + 1;
1337 return &tbs[m_max];
1340 static void tb_reset_jump_recursive(TranslationBlock *tb);
1342 static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1344 TranslationBlock *tb1, *tb_next, **ptb;
1345 unsigned int n1;
1347 tb1 = tb->jmp_next[n];
1348 if (tb1 != NULL) {
1349 /* find head of list */
1350 for(;;) {
1351 n1 = (long)tb1 & 3;
1352 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1353 if (n1 == 2)
1354 break;
1355 tb1 = tb1->jmp_next[n1];
1357 /* we are now sure now that tb jumps to tb1 */
1358 tb_next = tb1;
1360 /* remove tb from the jmp_first list */
1361 ptb = &tb_next->jmp_first;
1362 for(;;) {
1363 tb1 = *ptb;
1364 n1 = (long)tb1 & 3;
1365 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1366 if (n1 == n && tb1 == tb)
1367 break;
1368 ptb = &tb1->jmp_next[n1];
1370 *ptb = tb->jmp_next[n];
1371 tb->jmp_next[n] = NULL;
1373 /* suppress the jump to next tb in generated code */
1374 tb_reset_jump(tb, n);
1376 /* suppress jumps in the tb on which we could have jumped */
1377 tb_reset_jump_recursive(tb_next);
1381 static void tb_reset_jump_recursive(TranslationBlock *tb)
1383 tb_reset_jump_recursive2(tb, 0);
1384 tb_reset_jump_recursive2(tb, 1);
1387 #if defined(TARGET_HAS_ICE)
1388 #if defined(CONFIG_USER_ONLY)
1389 static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1391 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1393 #else
1394 static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1396 target_phys_addr_t addr;
1397 target_ulong pd;
1398 ram_addr_t ram_addr;
1399 PhysPageDesc *p;
1401 addr = cpu_get_phys_page_debug(env, pc);
1402 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1403 if (!p) {
1404 pd = IO_MEM_UNASSIGNED;
1405 } else {
1406 pd = p->phys_offset;
1408 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
1409 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1411 #endif
1412 #endif /* TARGET_HAS_ICE */
1414 #if defined(CONFIG_USER_ONLY)
1415 void cpu_watchpoint_remove_all(CPUState *env, int mask)
1420 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1421 int flags, CPUWatchpoint **watchpoint)
1423 return -ENOSYS;
1425 #else
1426 /* Add a watchpoint. */
1427 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1428 int flags, CPUWatchpoint **watchpoint)
1430 target_ulong len_mask = ~(len - 1);
1431 CPUWatchpoint *wp;
1433 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1434 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1435 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1436 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1437 return -EINVAL;
1439 wp = qemu_malloc(sizeof(*wp));
1441 wp->vaddr = addr;
1442 wp->len_mask = len_mask;
1443 wp->flags = flags;
1445 /* keep all GDB-injected watchpoints in front */
1446 if (flags & BP_GDB)
1447 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
1448 else
1449 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
1451 tlb_flush_page(env, addr);
1453 if (watchpoint)
1454 *watchpoint = wp;
1455 return 0;
1458 /* Remove a specific watchpoint. */
1459 int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1460 int flags)
1462 target_ulong len_mask = ~(len - 1);
1463 CPUWatchpoint *wp;
1465 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1466 if (addr == wp->vaddr && len_mask == wp->len_mask
1467 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
1468 cpu_watchpoint_remove_by_ref(env, wp);
1469 return 0;
1472 return -ENOENT;
1475 /* Remove a specific watchpoint by reference. */
1476 void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1478 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
1480 tlb_flush_page(env, watchpoint->vaddr);
1482 qemu_free(watchpoint);
1485 /* Remove all matching watchpoints. */
1486 void cpu_watchpoint_remove_all(CPUState *env, int mask)
1488 CPUWatchpoint *wp, *next;
1490 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
1491 if (wp->flags & mask)
1492 cpu_watchpoint_remove_by_ref(env, wp);
1495 #endif
1497 /* Add a breakpoint. */
1498 int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1499 CPUBreakpoint **breakpoint)
1501 #if defined(TARGET_HAS_ICE)
1502 CPUBreakpoint *bp;
1504 bp = qemu_malloc(sizeof(*bp));
1506 bp->pc = pc;
1507 bp->flags = flags;
1509 /* keep all GDB-injected breakpoints in front */
1510 if (flags & BP_GDB)
1511 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
1512 else
1513 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
1515 breakpoint_invalidate(env, pc);
1517 if (breakpoint)
1518 *breakpoint = bp;
1519 return 0;
1520 #else
1521 return -ENOSYS;
1522 #endif
1525 /* Remove a specific breakpoint. */
1526 int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1528 #if defined(TARGET_HAS_ICE)
1529 CPUBreakpoint *bp;
1531 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1532 if (bp->pc == pc && bp->flags == flags) {
1533 cpu_breakpoint_remove_by_ref(env, bp);
1534 return 0;
1537 return -ENOENT;
1538 #else
1539 return -ENOSYS;
1540 #endif
1543 /* Remove a specific breakpoint by reference. */
1544 void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
1546 #if defined(TARGET_HAS_ICE)
1547 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
1549 breakpoint_invalidate(env, breakpoint->pc);
1551 qemu_free(breakpoint);
1552 #endif
1555 /* Remove all matching breakpoints. */
1556 void cpu_breakpoint_remove_all(CPUState *env, int mask)
1558 #if defined(TARGET_HAS_ICE)
1559 CPUBreakpoint *bp, *next;
1561 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
1562 if (bp->flags & mask)
1563 cpu_breakpoint_remove_by_ref(env, bp);
1565 #endif
1568 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1569 CPU loop after each instruction */
1570 void cpu_single_step(CPUState *env, int enabled)
1572 #if defined(TARGET_HAS_ICE)
1573 if (env->singlestep_enabled != enabled) {
1574 env->singlestep_enabled = enabled;
1575 if (kvm_enabled())
1576 kvm_update_guest_debug(env, 0);
1577 else {
1578 /* must flush all the translated code to avoid inconsistencies */
1579 /* XXX: only flush what is necessary */
1580 tb_flush(env);
1583 #endif
1586 /* enable or disable low levels log */
1587 void cpu_set_log(int log_flags)
1589 loglevel = log_flags;
1590 if (loglevel && !logfile) {
1591 logfile = fopen(logfilename, log_append ? "a" : "w");
1592 if (!logfile) {
1593 perror(logfilename);
1594 _exit(1);
1596 #if !defined(CONFIG_SOFTMMU)
1597 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1599 static char logfile_buf[4096];
1600 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1602 #elif !defined(_WIN32)
1603 /* Win32 doesn't support line-buffering and requires size >= 2 */
1604 setvbuf(logfile, NULL, _IOLBF, 0);
1605 #endif
1606 log_append = 1;
1608 if (!loglevel && logfile) {
1609 fclose(logfile);
1610 logfile = NULL;
1614 void cpu_set_log_filename(const char *filename)
1616 logfilename = strdup(filename);
1617 if (logfile) {
1618 fclose(logfile);
1619 logfile = NULL;
1621 cpu_set_log(loglevel);
1624 static void cpu_unlink_tb(CPUState *env)
1626 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1627 problem and hope the cpu will stop of its own accord. For userspace
1628 emulation this often isn't actually as bad as it sounds. Often
1629 signals are used primarily to interrupt blocking syscalls. */
1630 TranslationBlock *tb;
1631 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
1633 spin_lock(&interrupt_lock);
1634 tb = env->current_tb;
1635 /* if the cpu is currently executing code, we must unlink it and
1636 all the potentially executing TB */
1637 if (tb) {
1638 env->current_tb = NULL;
1639 tb_reset_jump_recursive(tb);
1641 spin_unlock(&interrupt_lock);
1644 /* mask must never be zero, except for A20 change call */
1645 void cpu_interrupt(CPUState *env, int mask)
1647 int old_mask;
1649 old_mask = env->interrupt_request;
1650 env->interrupt_request |= mask;
1651 if (kvm_enabled() && !kvm_irqchip_in_kernel())
1652 kvm_update_interrupt_request(env);
1654 #ifndef CONFIG_USER_ONLY
1656 * If called from iothread context, wake the target cpu in
1657 * case its halted.
1659 if (!qemu_cpu_self(env)) {
1660 qemu_cpu_kick(env);
1661 return;
1663 #endif
1665 if (use_icount) {
1666 env->icount_decr.u16.high = 0xffff;
1667 #ifndef CONFIG_USER_ONLY
1668 if (!can_do_io(env)
1669 && (mask & ~old_mask) != 0) {
1670 cpu_abort(env, "Raised interrupt while not in I/O function");
1672 #endif
1673 } else {
1674 cpu_unlink_tb(env);
1678 void cpu_reset_interrupt(CPUState *env, int mask)
1680 env->interrupt_request &= ~mask;
1683 void cpu_exit(CPUState *env)
1685 env->exit_request = 1;
1686 cpu_unlink_tb(env);
1689 const CPULogItem cpu_log_items[] = {
1690 { CPU_LOG_TB_OUT_ASM, "out_asm",
1691 "show generated host assembly code for each compiled TB" },
1692 { CPU_LOG_TB_IN_ASM, "in_asm",
1693 "show target assembly code for each compiled TB" },
1694 { CPU_LOG_TB_OP, "op",
1695 "show micro ops for each compiled TB" },
1696 { CPU_LOG_TB_OP_OPT, "op_opt",
1697 "show micro ops "
1698 #ifdef TARGET_I386
1699 "before eflags optimization and "
1700 #endif
1701 "after liveness analysis" },
1702 { CPU_LOG_INT, "int",
1703 "show interrupts/exceptions in short format" },
1704 { CPU_LOG_EXEC, "exec",
1705 "show trace before each executed TB (lots of logs)" },
1706 { CPU_LOG_TB_CPU, "cpu",
1707 "show CPU state before block translation" },
1708 #ifdef TARGET_I386
1709 { CPU_LOG_PCALL, "pcall",
1710 "show protected mode far calls/returns/exceptions" },
1711 { CPU_LOG_RESET, "cpu_reset",
1712 "show CPU state before CPU resets" },
1713 #endif
1714 #ifdef DEBUG_IOPORT
1715 { CPU_LOG_IOPORT, "ioport",
1716 "show all i/o ports accesses" },
1717 #endif
1718 { 0, NULL, NULL },
1721 #ifndef CONFIG_USER_ONLY
1722 static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
1723 = QLIST_HEAD_INITIALIZER(memory_client_list);
1725 static void cpu_notify_set_memory(target_phys_addr_t start_addr,
1726 ram_addr_t size,
1727 ram_addr_t phys_offset)
1729 CPUPhysMemoryClient *client;
1730 QLIST_FOREACH(client, &memory_client_list, list) {
1731 client->set_memory(client, start_addr, size, phys_offset);
1735 static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
1736 target_phys_addr_t end)
1738 CPUPhysMemoryClient *client;
1739 QLIST_FOREACH(client, &memory_client_list, list) {
1740 int r = client->sync_dirty_bitmap(client, start, end);
1741 if (r < 0)
1742 return r;
1744 return 0;
1747 static int cpu_notify_migration_log(int enable)
1749 CPUPhysMemoryClient *client;
1750 QLIST_FOREACH(client, &memory_client_list, list) {
1751 int r = client->migration_log(client, enable);
1752 if (r < 0)
1753 return r;
1755 return 0;
1758 static void phys_page_for_each_1(CPUPhysMemoryClient *client,
1759 int level, void **lp)
1761 int i;
1763 if (*lp == NULL) {
1764 return;
1766 if (level == 0) {
1767 PhysPageDesc *pd = *lp;
1768 for (i = 0; i < L2_SIZE; ++i) {
1769 if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
1770 client->set_memory(client, pd[i].region_offset,
1771 TARGET_PAGE_SIZE, pd[i].phys_offset);
1774 } else {
1775 void **pp = *lp;
1776 for (i = 0; i < L2_SIZE; ++i) {
1777 phys_page_for_each_1(client, level - 1, pp + i);
1782 static void phys_page_for_each(CPUPhysMemoryClient *client)
1784 int i;
1785 for (i = 0; i < P_L1_SIZE; ++i) {
1786 phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
1787 l1_phys_map + 1);
1791 void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
1793 QLIST_INSERT_HEAD(&memory_client_list, client, list);
1794 phys_page_for_each(client);
1797 void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
1799 QLIST_REMOVE(client, list);
1801 #endif
1803 static int cmp1(const char *s1, int n, const char *s2)
1805 if (strlen(s2) != n)
1806 return 0;
1807 return memcmp(s1, s2, n) == 0;
1810 /* takes a comma separated list of log masks. Return 0 if error. */
1811 int cpu_str_to_log_mask(const char *str)
1813 const CPULogItem *item;
1814 int mask;
1815 const char *p, *p1;
1817 p = str;
1818 mask = 0;
1819 for(;;) {
1820 p1 = strchr(p, ',');
1821 if (!p1)
1822 p1 = p + strlen(p);
1823 if(cmp1(p,p1-p,"all")) {
1824 for(item = cpu_log_items; item->mask != 0; item++) {
1825 mask |= item->mask;
1827 } else {
1828 for(item = cpu_log_items; item->mask != 0; item++) {
1829 if (cmp1(p, p1 - p, item->name))
1830 goto found;
1832 return 0;
1834 found:
1835 mask |= item->mask;
1836 if (*p1 != ',')
1837 break;
1838 p = p1 + 1;
1840 return mask;
1843 void cpu_abort(CPUState *env, const char *fmt, ...)
1845 va_list ap;
1846 va_list ap2;
1848 va_start(ap, fmt);
1849 va_copy(ap2, ap);
1850 fprintf(stderr, "qemu: fatal: ");
1851 vfprintf(stderr, fmt, ap);
1852 fprintf(stderr, "\n");
1853 #ifdef TARGET_I386
1854 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1855 #else
1856 cpu_dump_state(env, stderr, fprintf, 0);
1857 #endif
1858 if (qemu_log_enabled()) {
1859 qemu_log("qemu: fatal: ");
1860 qemu_log_vprintf(fmt, ap2);
1861 qemu_log("\n");
1862 #ifdef TARGET_I386
1863 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
1864 #else
1865 log_cpu_state(env, 0);
1866 #endif
1867 qemu_log_flush();
1868 qemu_log_close();
1870 va_end(ap2);
1871 va_end(ap);
1872 #if defined(CONFIG_USER_ONLY)
1874 struct sigaction act;
1875 sigfillset(&act.sa_mask);
1876 act.sa_handler = SIG_DFL;
1877 sigaction(SIGABRT, &act, NULL);
1879 #endif
1880 abort();
1883 CPUState *cpu_copy(CPUState *env)
1885 CPUState *new_env = cpu_init(env->cpu_model_str);
1886 CPUState *next_cpu = new_env->next_cpu;
1887 int cpu_index = new_env->cpu_index;
1888 #if defined(TARGET_HAS_ICE)
1889 CPUBreakpoint *bp;
1890 CPUWatchpoint *wp;
1891 #endif
1893 memcpy(new_env, env, sizeof(CPUState));
1895 /* Preserve chaining and index. */
1896 new_env->next_cpu = next_cpu;
1897 new_env->cpu_index = cpu_index;
1899 /* Clone all break/watchpoints.
1900 Note: Once we support ptrace with hw-debug register access, make sure
1901 BP_CPU break/watchpoints are handled correctly on clone. */
1902 QTAILQ_INIT(&env->breakpoints);
1903 QTAILQ_INIT(&env->watchpoints);
1904 #if defined(TARGET_HAS_ICE)
1905 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1906 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1908 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1909 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1910 wp->flags, NULL);
1912 #endif
1914 return new_env;
1917 #if !defined(CONFIG_USER_ONLY)
1919 static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1921 unsigned int i;
1923 /* Discard jump cache entries for any tb which might potentially
1924 overlap the flushed page. */
1925 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1926 memset (&env->tb_jmp_cache[i], 0,
1927 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1929 i = tb_jmp_cache_hash_page(addr);
1930 memset (&env->tb_jmp_cache[i], 0,
1931 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1934 static CPUTLBEntry s_cputlb_empty_entry = {
1935 .addr_read = -1,
1936 .addr_write = -1,
1937 .addr_code = -1,
1938 .addend = -1,
1941 /* NOTE: if flush_global is true, also flush global entries (not
1942 implemented yet) */
1943 void tlb_flush(CPUState *env, int flush_global)
1945 int i;
1947 #if defined(DEBUG_TLB)
1948 printf("tlb_flush:\n");
1949 #endif
1950 /* must reset current TB so that interrupts cannot modify the
1951 links while we are modifying them */
1952 env->current_tb = NULL;
1954 for(i = 0; i < CPU_TLB_SIZE; i++) {
1955 int mmu_idx;
1956 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1957 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
1961 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
1963 env->tlb_flush_addr = -1;
1964 env->tlb_flush_mask = 0;
1965 tlb_flush_count++;
1968 static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
1970 if (addr == (tlb_entry->addr_read &
1971 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1972 addr == (tlb_entry->addr_write &
1973 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1974 addr == (tlb_entry->addr_code &
1975 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1976 *tlb_entry = s_cputlb_empty_entry;
1980 void tlb_flush_page(CPUState *env, target_ulong addr)
1982 int i;
1983 int mmu_idx;
1985 #if defined(DEBUG_TLB)
1986 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
1987 #endif
1988 /* Check if we need to flush due to large pages. */
1989 if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
1990 #if defined(DEBUG_TLB)
1991 printf("tlb_flush_page: forced full flush ("
1992 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
1993 env->tlb_flush_addr, env->tlb_flush_mask);
1994 #endif
1995 tlb_flush(env, 1);
1996 return;
1998 /* must reset current TB so that interrupts cannot modify the
1999 links while we are modifying them */
2000 env->current_tb = NULL;
2002 addr &= TARGET_PAGE_MASK;
2003 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2004 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2005 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
2007 tlb_flush_jmp_cache(env, addr);
2010 /* update the TLBs so that writes to code in the virtual page 'addr'
2011 can be detected */
2012 static void tlb_protect_code(ram_addr_t ram_addr)
2014 cpu_physical_memory_reset_dirty(ram_addr,
2015 ram_addr + TARGET_PAGE_SIZE,
2016 CODE_DIRTY_FLAG);
2019 /* update the TLB so that writes in physical page 'phys_addr' are no longer
2020 tested for self modifying code */
2021 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
2022 target_ulong vaddr)
2024 cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG);
2027 static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
2028 unsigned long start, unsigned long length)
2030 unsigned long addr;
2031 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
2032 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
2033 if ((addr - start) < length) {
2034 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
2039 /* Note: start and end must be within the same ram block. */
2040 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
2041 int dirty_flags)
2043 CPUState *env;
2044 unsigned long length, start1;
2045 int i;
2047 start &= TARGET_PAGE_MASK;
2048 end = TARGET_PAGE_ALIGN(end);
2050 length = end - start;
2051 if (length == 0)
2052 return;
2053 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
2055 /* we modify the TLB cache so that the dirty bit will be set again
2056 when accessing the range */
2057 start1 = (unsigned long)qemu_safe_ram_ptr(start);
2058 /* Chek that we don't span multiple blocks - this breaks the
2059 address comparisons below. */
2060 if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
2061 != (end - 1) - start) {
2062 abort();
2065 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2066 int mmu_idx;
2067 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2068 for(i = 0; i < CPU_TLB_SIZE; i++)
2069 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
2070 start1, length);
2075 int cpu_physical_memory_set_dirty_tracking(int enable)
2077 int ret = 0;
2078 in_migration = enable;
2079 ret = cpu_notify_migration_log(!!enable);
2080 return ret;
2083 int cpu_physical_memory_get_dirty_tracking(void)
2085 return in_migration;
2088 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
2089 target_phys_addr_t end_addr)
2091 int ret;
2093 ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
2094 return ret;
2097 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
2099 ram_addr_t ram_addr;
2100 void *p;
2102 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
2103 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
2104 + tlb_entry->addend);
2105 ram_addr = qemu_ram_addr_from_host_nofail(p);
2106 if (!cpu_physical_memory_is_dirty(ram_addr)) {
2107 tlb_entry->addr_write |= TLB_NOTDIRTY;
2112 /* update the TLB according to the current state of the dirty bits */
2113 void cpu_tlb_update_dirty(CPUState *env)
2115 int i;
2116 int mmu_idx;
2117 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2118 for(i = 0; i < CPU_TLB_SIZE; i++)
2119 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
2123 static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
2125 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
2126 tlb_entry->addr_write = vaddr;
2129 /* update the TLB corresponding to virtual page vaddr
2130 so that it is no longer dirty */
2131 static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
2133 int i;
2134 int mmu_idx;
2136 vaddr &= TARGET_PAGE_MASK;
2137 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2138 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2139 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
2142 /* Our TLB does not support large pages, so remember the area covered by
2143 large pages and trigger a full TLB flush if these are invalidated. */
2144 static void tlb_add_large_page(CPUState *env, target_ulong vaddr,
2145 target_ulong size)
2147 target_ulong mask = ~(size - 1);
2149 if (env->tlb_flush_addr == (target_ulong)-1) {
2150 env->tlb_flush_addr = vaddr & mask;
2151 env->tlb_flush_mask = mask;
2152 return;
2154 /* Extend the existing region to include the new page.
2155 This is a compromise between unnecessary flushes and the cost
2156 of maintaining a full variable size TLB. */
2157 mask &= env->tlb_flush_mask;
2158 while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
2159 mask <<= 1;
2161 env->tlb_flush_addr &= mask;
2162 env->tlb_flush_mask = mask;
2165 /* Add a new TLB entry. At most one entry for a given virtual address
2166 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
2167 supplied size is only used by tlb_flush_page. */
2168 void tlb_set_page(CPUState *env, target_ulong vaddr,
2169 target_phys_addr_t paddr, int prot,
2170 int mmu_idx, target_ulong size)
2172 PhysPageDesc *p;
2173 unsigned long pd;
2174 unsigned int index;
2175 target_ulong address;
2176 target_ulong code_address;
2177 unsigned long addend;
2178 CPUTLBEntry *te;
2179 CPUWatchpoint *wp;
2180 target_phys_addr_t iotlb;
2182 assert(size >= TARGET_PAGE_SIZE);
2183 if (size != TARGET_PAGE_SIZE) {
2184 tlb_add_large_page(env, vaddr, size);
2186 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
2187 if (!p) {
2188 pd = IO_MEM_UNASSIGNED;
2189 } else {
2190 pd = p->phys_offset;
2192 #if defined(DEBUG_TLB)
2193 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
2194 " prot=%x idx=%d pd=0x%08lx\n",
2195 vaddr, paddr, prot, mmu_idx, pd);
2196 #endif
2198 address = vaddr;
2199 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2200 /* IO memory case (romd handled later) */
2201 address |= TLB_MMIO;
2203 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
2204 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2205 /* Normal RAM. */
2206 iotlb = pd & TARGET_PAGE_MASK;
2207 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2208 iotlb |= IO_MEM_NOTDIRTY;
2209 else
2210 iotlb |= IO_MEM_ROM;
2211 } else {
2212 /* IO handlers are currently passed a physical address.
2213 It would be nice to pass an offset from the base address
2214 of that region. This would avoid having to special case RAM,
2215 and avoid full address decoding in every device.
2216 We can't use the high bits of pd for this because
2217 IO_MEM_ROMD uses these as a ram address. */
2218 iotlb = (pd & ~TARGET_PAGE_MASK);
2219 if (p) {
2220 iotlb += p->region_offset;
2221 } else {
2222 iotlb += paddr;
2226 code_address = address;
2227 /* Make accesses to pages with watchpoints go via the
2228 watchpoint trap routines. */
2229 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
2230 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
2231 /* Avoid trapping reads of pages with a write breakpoint. */
2232 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
2233 iotlb = io_mem_watch + paddr;
2234 address |= TLB_MMIO;
2235 break;
2240 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2241 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2242 te = &env->tlb_table[mmu_idx][index];
2243 te->addend = addend - vaddr;
2244 if (prot & PAGE_READ) {
2245 te->addr_read = address;
2246 } else {
2247 te->addr_read = -1;
2250 if (prot & PAGE_EXEC) {
2251 te->addr_code = code_address;
2252 } else {
2253 te->addr_code = -1;
2255 if (prot & PAGE_WRITE) {
2256 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2257 (pd & IO_MEM_ROMD)) {
2258 /* Write access calls the I/O callback. */
2259 te->addr_write = address | TLB_MMIO;
2260 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2261 !cpu_physical_memory_is_dirty(pd)) {
2262 te->addr_write = address | TLB_NOTDIRTY;
2263 } else {
2264 te->addr_write = address;
2266 } else {
2267 te->addr_write = -1;
2271 #else
2273 void tlb_flush(CPUState *env, int flush_global)
2277 void tlb_flush_page(CPUState *env, target_ulong addr)
2282 * Walks guest process memory "regions" one by one
2283 * and calls callback function 'fn' for each region.
2286 struct walk_memory_regions_data
2288 walk_memory_regions_fn fn;
2289 void *priv;
2290 unsigned long start;
2291 int prot;
2294 static int walk_memory_regions_end(struct walk_memory_regions_data *data,
2295 abi_ulong end, int new_prot)
2297 if (data->start != -1ul) {
2298 int rc = data->fn(data->priv, data->start, end, data->prot);
2299 if (rc != 0) {
2300 return rc;
2304 data->start = (new_prot ? end : -1ul);
2305 data->prot = new_prot;
2307 return 0;
2310 static int walk_memory_regions_1(struct walk_memory_regions_data *data,
2311 abi_ulong base, int level, void **lp)
2313 abi_ulong pa;
2314 int i, rc;
2316 if (*lp == NULL) {
2317 return walk_memory_regions_end(data, base, 0);
2320 if (level == 0) {
2321 PageDesc *pd = *lp;
2322 for (i = 0; i < L2_SIZE; ++i) {
2323 int prot = pd[i].flags;
2325 pa = base | (i << TARGET_PAGE_BITS);
2326 if (prot != data->prot) {
2327 rc = walk_memory_regions_end(data, pa, prot);
2328 if (rc != 0) {
2329 return rc;
2333 } else {
2334 void **pp = *lp;
2335 for (i = 0; i < L2_SIZE; ++i) {
2336 pa = base | ((abi_ulong)i <<
2337 (TARGET_PAGE_BITS + L2_BITS * level));
2338 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2339 if (rc != 0) {
2340 return rc;
2345 return 0;
2348 int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2350 struct walk_memory_regions_data data;
2351 unsigned long i;
2353 data.fn = fn;
2354 data.priv = priv;
2355 data.start = -1ul;
2356 data.prot = 0;
2358 for (i = 0; i < V_L1_SIZE; i++) {
2359 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
2360 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
2361 if (rc != 0) {
2362 return rc;
2366 return walk_memory_regions_end(&data, 0, 0);
2369 static int dump_region(void *priv, abi_ulong start,
2370 abi_ulong end, unsigned long prot)
2372 FILE *f = (FILE *)priv;
2374 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
2375 " "TARGET_ABI_FMT_lx" %c%c%c\n",
2376 start, end, end - start,
2377 ((prot & PAGE_READ) ? 'r' : '-'),
2378 ((prot & PAGE_WRITE) ? 'w' : '-'),
2379 ((prot & PAGE_EXEC) ? 'x' : '-'));
2381 return (0);
2384 /* dump memory mappings */
2385 void page_dump(FILE *f)
2387 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2388 "start", "end", "size", "prot");
2389 walk_memory_regions(f, dump_region);
2392 int page_get_flags(target_ulong address)
2394 PageDesc *p;
2396 p = page_find(address >> TARGET_PAGE_BITS);
2397 if (!p)
2398 return 0;
2399 return p->flags;
2402 /* Modify the flags of a page and invalidate the code if necessary.
2403 The flag PAGE_WRITE_ORG is positioned automatically depending
2404 on PAGE_WRITE. The mmap_lock should already be held. */
2405 void page_set_flags(target_ulong start, target_ulong end, int flags)
2407 target_ulong addr, len;
2409 /* This function should never be called with addresses outside the
2410 guest address space. If this assert fires, it probably indicates
2411 a missing call to h2g_valid. */
2412 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2413 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2414 #endif
2415 assert(start < end);
2417 start = start & TARGET_PAGE_MASK;
2418 end = TARGET_PAGE_ALIGN(end);
2420 if (flags & PAGE_WRITE) {
2421 flags |= PAGE_WRITE_ORG;
2424 for (addr = start, len = end - start;
2425 len != 0;
2426 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2427 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2429 /* If the write protection bit is set, then we invalidate
2430 the code inside. */
2431 if (!(p->flags & PAGE_WRITE) &&
2432 (flags & PAGE_WRITE) &&
2433 p->first_tb) {
2434 tb_invalidate_phys_page(addr, 0, NULL);
2436 p->flags = flags;
2440 int page_check_range(target_ulong start, target_ulong len, int flags)
2442 PageDesc *p;
2443 target_ulong end;
2444 target_ulong addr;
2446 /* This function should never be called with addresses outside the
2447 guest address space. If this assert fires, it probably indicates
2448 a missing call to h2g_valid. */
2449 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2450 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2451 #endif
2453 if (len == 0) {
2454 return 0;
2456 if (start + len - 1 < start) {
2457 /* We've wrapped around. */
2458 return -1;
2461 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2462 start = start & TARGET_PAGE_MASK;
2464 for (addr = start, len = end - start;
2465 len != 0;
2466 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2467 p = page_find(addr >> TARGET_PAGE_BITS);
2468 if( !p )
2469 return -1;
2470 if( !(p->flags & PAGE_VALID) )
2471 return -1;
2473 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
2474 return -1;
2475 if (flags & PAGE_WRITE) {
2476 if (!(p->flags & PAGE_WRITE_ORG))
2477 return -1;
2478 /* unprotect the page if it was put read-only because it
2479 contains translated code */
2480 if (!(p->flags & PAGE_WRITE)) {
2481 if (!page_unprotect(addr, 0, NULL))
2482 return -1;
2484 return 0;
2487 return 0;
2490 /* called from signal handler: invalidate the code and unprotect the
2491 page. Return TRUE if the fault was successfully handled. */
2492 int page_unprotect(target_ulong address, unsigned long pc, void *puc)
2494 unsigned int prot;
2495 PageDesc *p;
2496 target_ulong host_start, host_end, addr;
2498 /* Technically this isn't safe inside a signal handler. However we
2499 know this only ever happens in a synchronous SEGV handler, so in
2500 practice it seems to be ok. */
2501 mmap_lock();
2503 p = page_find(address >> TARGET_PAGE_BITS);
2504 if (!p) {
2505 mmap_unlock();
2506 return 0;
2509 /* if the page was really writable, then we change its
2510 protection back to writable */
2511 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2512 host_start = address & qemu_host_page_mask;
2513 host_end = host_start + qemu_host_page_size;
2515 prot = 0;
2516 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2517 p = page_find(addr >> TARGET_PAGE_BITS);
2518 p->flags |= PAGE_WRITE;
2519 prot |= p->flags;
2521 /* and since the content will be modified, we must invalidate
2522 the corresponding translated code. */
2523 tb_invalidate_phys_page(addr, pc, puc);
2524 #ifdef DEBUG_TB_CHECK
2525 tb_invalidate_check(addr);
2526 #endif
2528 mprotect((void *)g2h(host_start), qemu_host_page_size,
2529 prot & PAGE_BITS);
2531 mmap_unlock();
2532 return 1;
2534 mmap_unlock();
2535 return 0;
2538 static inline void tlb_set_dirty(CPUState *env,
2539 unsigned long addr, target_ulong vaddr)
2542 #endif /* defined(CONFIG_USER_ONLY) */
2544 #if !defined(CONFIG_USER_ONLY)
2546 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2547 typedef struct subpage_t {
2548 target_phys_addr_t base;
2549 ram_addr_t sub_io_index[TARGET_PAGE_SIZE];
2550 ram_addr_t region_offset[TARGET_PAGE_SIZE];
2551 } subpage_t;
2553 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2554 ram_addr_t memory, ram_addr_t region_offset);
2555 static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2556 ram_addr_t orig_memory,
2557 ram_addr_t region_offset);
2558 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2559 need_subpage) \
2560 do { \
2561 if (addr > start_addr) \
2562 start_addr2 = 0; \
2563 else { \
2564 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2565 if (start_addr2 > 0) \
2566 need_subpage = 1; \
2569 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2570 end_addr2 = TARGET_PAGE_SIZE - 1; \
2571 else { \
2572 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2573 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2574 need_subpage = 1; \
2576 } while (0)
2578 /* register physical memory.
2579 For RAM, 'size' must be a multiple of the target page size.
2580 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2581 io memory page. The address used when calling the IO function is
2582 the offset from the start of the region, plus region_offset. Both
2583 start_addr and region_offset are rounded down to a page boundary
2584 before calculating this offset. This should not be a problem unless
2585 the low bits of start_addr and region_offset differ. */
2586 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2587 ram_addr_t size,
2588 ram_addr_t phys_offset,
2589 ram_addr_t region_offset)
2591 target_phys_addr_t addr, end_addr;
2592 PhysPageDesc *p;
2593 CPUState *env;
2594 ram_addr_t orig_size = size;
2595 subpage_t *subpage;
2597 cpu_notify_set_memory(start_addr, size, phys_offset);
2599 if (phys_offset == IO_MEM_UNASSIGNED) {
2600 region_offset = start_addr;
2602 region_offset &= TARGET_PAGE_MASK;
2603 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
2604 end_addr = start_addr + (target_phys_addr_t)size;
2605 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
2606 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2607 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2608 ram_addr_t orig_memory = p->phys_offset;
2609 target_phys_addr_t start_addr2, end_addr2;
2610 int need_subpage = 0;
2612 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2613 need_subpage);
2614 if (need_subpage) {
2615 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2616 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2617 &p->phys_offset, orig_memory,
2618 p->region_offset);
2619 } else {
2620 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2621 >> IO_MEM_SHIFT];
2623 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2624 region_offset);
2625 p->region_offset = 0;
2626 } else {
2627 p->phys_offset = phys_offset;
2628 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2629 (phys_offset & IO_MEM_ROMD))
2630 phys_offset += TARGET_PAGE_SIZE;
2632 } else {
2633 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2634 p->phys_offset = phys_offset;
2635 p->region_offset = region_offset;
2636 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2637 (phys_offset & IO_MEM_ROMD)) {
2638 phys_offset += TARGET_PAGE_SIZE;
2639 } else {
2640 target_phys_addr_t start_addr2, end_addr2;
2641 int need_subpage = 0;
2643 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2644 end_addr2, need_subpage);
2646 if (need_subpage) {
2647 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2648 &p->phys_offset, IO_MEM_UNASSIGNED,
2649 addr & TARGET_PAGE_MASK);
2650 subpage_register(subpage, start_addr2, end_addr2,
2651 phys_offset, region_offset);
2652 p->region_offset = 0;
2656 region_offset += TARGET_PAGE_SIZE;
2659 /* since each CPU stores ram addresses in its TLB cache, we must
2660 reset the modified entries */
2661 /* XXX: slow ! */
2662 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2663 tlb_flush(env, 1);
2667 /* XXX: temporary until new memory mapping API */
2668 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2670 PhysPageDesc *p;
2672 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2673 if (!p)
2674 return IO_MEM_UNASSIGNED;
2675 return p->phys_offset;
2678 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2680 if (kvm_enabled())
2681 kvm_coalesce_mmio_region(addr, size);
2684 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2686 if (kvm_enabled())
2687 kvm_uncoalesce_mmio_region(addr, size);
2690 void qemu_flush_coalesced_mmio_buffer(void)
2692 if (kvm_enabled())
2693 kvm_flush_coalesced_mmio_buffer();
2696 #if defined(__linux__) && !defined(TARGET_S390X)
2698 #include <sys/vfs.h>
2700 #define HUGETLBFS_MAGIC 0x958458f6
2702 static long gethugepagesize(const char *path)
2704 struct statfs fs;
2705 int ret;
2707 do {
2708 ret = statfs(path, &fs);
2709 } while (ret != 0 && errno == EINTR);
2711 if (ret != 0) {
2712 perror(path);
2713 return 0;
2716 if (fs.f_type != HUGETLBFS_MAGIC)
2717 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
2719 return fs.f_bsize;
2722 static void *file_ram_alloc(RAMBlock *block,
2723 ram_addr_t memory,
2724 const char *path)
2726 char *filename;
2727 void *area;
2728 int fd;
2729 #ifdef MAP_POPULATE
2730 int flags;
2731 #endif
2732 unsigned long hpagesize;
2734 hpagesize = gethugepagesize(path);
2735 if (!hpagesize) {
2736 return NULL;
2739 if (memory < hpagesize) {
2740 return NULL;
2743 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2744 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2745 return NULL;
2748 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
2749 return NULL;
2752 fd = mkstemp(filename);
2753 if (fd < 0) {
2754 perror("unable to create backing store for hugepages");
2755 free(filename);
2756 return NULL;
2758 unlink(filename);
2759 free(filename);
2761 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2764 * ftruncate is not supported by hugetlbfs in older
2765 * hosts, so don't bother bailing out on errors.
2766 * If anything goes wrong with it under other filesystems,
2767 * mmap will fail.
2769 if (ftruncate(fd, memory))
2770 perror("ftruncate");
2772 #ifdef MAP_POPULATE
2773 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2774 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2775 * to sidestep this quirk.
2777 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2778 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2779 #else
2780 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2781 #endif
2782 if (area == MAP_FAILED) {
2783 perror("file_ram_alloc: can't mmap RAM pages");
2784 close(fd);
2785 return (NULL);
2787 block->fd = fd;
2788 return area;
2790 #endif
2792 static ram_addr_t find_ram_offset(ram_addr_t size)
2794 RAMBlock *block, *next_block;
2795 ram_addr_t offset = 0, mingap = ULONG_MAX;
2797 if (QLIST_EMPTY(&ram_list.blocks))
2798 return 0;
2800 QLIST_FOREACH(block, &ram_list.blocks, next) {
2801 ram_addr_t end, next = ULONG_MAX;
2803 end = block->offset + block->length;
2805 QLIST_FOREACH(next_block, &ram_list.blocks, next) {
2806 if (next_block->offset >= end) {
2807 next = MIN(next, next_block->offset);
2810 if (next - end >= size && next - end < mingap) {
2811 offset = end;
2812 mingap = next - end;
2815 return offset;
2818 static ram_addr_t last_ram_offset(void)
2820 RAMBlock *block;
2821 ram_addr_t last = 0;
2823 QLIST_FOREACH(block, &ram_list.blocks, next)
2824 last = MAX(last, block->offset + block->length);
2826 return last;
2829 ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
2830 ram_addr_t size, void *host)
2832 RAMBlock *new_block, *block;
2834 size = TARGET_PAGE_ALIGN(size);
2835 new_block = qemu_mallocz(sizeof(*new_block));
2837 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
2838 char *id = dev->parent_bus->info->get_dev_path(dev);
2839 if (id) {
2840 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
2841 qemu_free(id);
2844 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
2846 QLIST_FOREACH(block, &ram_list.blocks, next) {
2847 if (!strcmp(block->idstr, new_block->idstr)) {
2848 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
2849 new_block->idstr);
2850 abort();
2854 if (host) {
2855 new_block->host = host;
2856 } else {
2857 if (mem_path) {
2858 #if defined (__linux__) && !defined(TARGET_S390X)
2859 new_block->host = file_ram_alloc(new_block, size, mem_path);
2860 if (!new_block->host) {
2861 new_block->host = qemu_vmalloc(size);
2862 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
2864 #else
2865 fprintf(stderr, "-mem-path option unsupported\n");
2866 exit(1);
2867 #endif
2868 } else {
2869 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
2870 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2871 new_block->host = mmap((void*)0x1000000, size,
2872 PROT_EXEC|PROT_READ|PROT_WRITE,
2873 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
2874 #else
2875 new_block->host = qemu_vmalloc(size);
2876 #endif
2877 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
2881 new_block->offset = find_ram_offset(size);
2882 new_block->length = size;
2884 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
2886 ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
2887 last_ram_offset() >> TARGET_PAGE_BITS);
2888 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
2889 0xff, size >> TARGET_PAGE_BITS);
2891 if (kvm_enabled())
2892 kvm_setup_guest_memory(new_block->host, size);
2894 return new_block->offset;
2897 void qemu_ram_unmap(ram_addr_t addr)
2899 RAMBlock *block;
2901 QLIST_FOREACH(block, &ram_list.blocks, next) {
2902 if (addr == block->offset) {
2903 QLIST_REMOVE(block, next);
2904 qemu_free(block);
2905 return;
2910 ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
2912 return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
2915 void qemu_ram_free(ram_addr_t addr)
2917 RAMBlock *block;
2919 QLIST_FOREACH(block, &ram_list.blocks, next) {
2920 if (addr == block->offset) {
2921 QLIST_REMOVE(block, next);
2922 if (mem_path) {
2923 #if defined (__linux__) && !defined(TARGET_S390X)
2924 if (block->fd) {
2925 munmap(block->host, block->length);
2926 close(block->fd);
2927 } else {
2928 qemu_vfree(block->host);
2930 #endif
2931 } else {
2932 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
2933 munmap(block->host, block->length);
2934 #else
2935 qemu_vfree(block->host);
2936 #endif
2938 qemu_free(block);
2939 return;
2945 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2946 With the exception of the softmmu code in this file, this should
2947 only be used for local memory (e.g. video ram) that the device owns,
2948 and knows it isn't going to access beyond the end of the block.
2950 It should not be used for general purpose DMA.
2951 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2953 void *qemu_get_ram_ptr(ram_addr_t addr)
2955 RAMBlock *block;
2957 QLIST_FOREACH(block, &ram_list.blocks, next) {
2958 if (addr - block->offset < block->length) {
2959 QLIST_REMOVE(block, next);
2960 QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
2961 return block->host + (addr - block->offset);
2965 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2966 abort();
2968 return NULL;
2971 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2972 * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
2974 void *qemu_safe_ram_ptr(ram_addr_t addr)
2976 RAMBlock *block;
2978 QLIST_FOREACH(block, &ram_list.blocks, next) {
2979 if (addr - block->offset < block->length) {
2980 return block->host + (addr - block->offset);
2984 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2985 abort();
2987 return NULL;
2990 int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
2992 RAMBlock *block;
2993 uint8_t *host = ptr;
2995 QLIST_FOREACH(block, &ram_list.blocks, next) {
2996 if (host - block->host < block->length) {
2997 *ram_addr = block->offset + (host - block->host);
2998 return 0;
3001 return -1;
3004 /* Some of the softmmu routines need to translate from a host pointer
3005 (typically a TLB entry) back to a ram offset. */
3006 ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
3008 ram_addr_t ram_addr;
3010 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
3011 fprintf(stderr, "Bad ram pointer %p\n", ptr);
3012 abort();
3014 return ram_addr;
3017 static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
3019 #ifdef DEBUG_UNASSIGNED
3020 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3021 #endif
3022 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3023 do_unassigned_access(addr, 0, 0, 0, 1);
3024 #endif
3025 return 0;
3028 static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
3030 #ifdef DEBUG_UNASSIGNED
3031 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3032 #endif
3033 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3034 do_unassigned_access(addr, 0, 0, 0, 2);
3035 #endif
3036 return 0;
3039 static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
3041 #ifdef DEBUG_UNASSIGNED
3042 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3043 #endif
3044 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3045 do_unassigned_access(addr, 0, 0, 0, 4);
3046 #endif
3047 return 0;
3050 static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
3052 #ifdef DEBUG_UNASSIGNED
3053 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3054 #endif
3055 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3056 do_unassigned_access(addr, 1, 0, 0, 1);
3057 #endif
3060 static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
3062 #ifdef DEBUG_UNASSIGNED
3063 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3064 #endif
3065 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3066 do_unassigned_access(addr, 1, 0, 0, 2);
3067 #endif
3070 static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
3072 #ifdef DEBUG_UNASSIGNED
3073 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3074 #endif
3075 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3076 do_unassigned_access(addr, 1, 0, 0, 4);
3077 #endif
3080 static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
3081 unassigned_mem_readb,
3082 unassigned_mem_readw,
3083 unassigned_mem_readl,
3086 static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
3087 unassigned_mem_writeb,
3088 unassigned_mem_writew,
3089 unassigned_mem_writel,
3092 static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
3093 uint32_t val)
3095 int dirty_flags;
3096 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3097 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3098 #if !defined(CONFIG_USER_ONLY)
3099 tb_invalidate_phys_page_fast(ram_addr, 1);
3100 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3101 #endif
3103 stb_p(qemu_get_ram_ptr(ram_addr), val);
3104 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
3105 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
3106 /* we remove the notdirty callback only if the code has been
3107 flushed */
3108 if (dirty_flags == 0xff)
3109 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
3112 static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
3113 uint32_t val)
3115 int dirty_flags;
3116 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3117 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3118 #if !defined(CONFIG_USER_ONLY)
3119 tb_invalidate_phys_page_fast(ram_addr, 2);
3120 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3121 #endif
3123 stw_p(qemu_get_ram_ptr(ram_addr), val);
3124 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
3125 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
3126 /* we remove the notdirty callback only if the code has been
3127 flushed */
3128 if (dirty_flags == 0xff)
3129 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
3132 static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
3133 uint32_t val)
3135 int dirty_flags;
3136 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3137 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3138 #if !defined(CONFIG_USER_ONLY)
3139 tb_invalidate_phys_page_fast(ram_addr, 4);
3140 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3141 #endif
3143 stl_p(qemu_get_ram_ptr(ram_addr), val);
3144 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
3145 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
3146 /* we remove the notdirty callback only if the code has been
3147 flushed */
3148 if (dirty_flags == 0xff)
3149 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
3152 static CPUReadMemoryFunc * const error_mem_read[3] = {
3153 NULL, /* never used */
3154 NULL, /* never used */
3155 NULL, /* never used */
3158 static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
3159 notdirty_mem_writeb,
3160 notdirty_mem_writew,
3161 notdirty_mem_writel,
3164 /* Generate a debug exception if a watchpoint has been hit. */
3165 static void check_watchpoint(int offset, int len_mask, int flags)
3167 CPUState *env = cpu_single_env;
3168 target_ulong pc, cs_base;
3169 TranslationBlock *tb;
3170 target_ulong vaddr;
3171 CPUWatchpoint *wp;
3172 int cpu_flags;
3174 if (env->watchpoint_hit) {
3175 /* We re-entered the check after replacing the TB. Now raise
3176 * the debug interrupt so that is will trigger after the
3177 * current instruction. */
3178 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
3179 return;
3181 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
3182 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
3183 if ((vaddr == (wp->vaddr & len_mask) ||
3184 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
3185 wp->flags |= BP_WATCHPOINT_HIT;
3186 if (!env->watchpoint_hit) {
3187 env->watchpoint_hit = wp;
3188 tb = tb_find_pc(env->mem_io_pc);
3189 if (!tb) {
3190 cpu_abort(env, "check_watchpoint: could not find TB for "
3191 "pc=%p", (void *)env->mem_io_pc);
3193 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
3194 tb_phys_invalidate(tb, -1);
3195 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
3196 env->exception_index = EXCP_DEBUG;
3197 } else {
3198 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
3199 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
3201 cpu_resume_from_signal(env, NULL);
3203 } else {
3204 wp->flags &= ~BP_WATCHPOINT_HIT;
3209 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
3210 so these check for a hit then pass through to the normal out-of-line
3211 phys routines. */
3212 static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
3214 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
3215 return ldub_phys(addr);
3218 static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
3220 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
3221 return lduw_phys(addr);
3224 static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
3226 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
3227 return ldl_phys(addr);
3230 static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
3231 uint32_t val)
3233 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
3234 stb_phys(addr, val);
3237 static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
3238 uint32_t val)
3240 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
3241 stw_phys(addr, val);
3244 static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
3245 uint32_t val)
3247 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
3248 stl_phys(addr, val);
3251 static CPUReadMemoryFunc * const watch_mem_read[3] = {
3252 watch_mem_readb,
3253 watch_mem_readw,
3254 watch_mem_readl,
3257 static CPUWriteMemoryFunc * const watch_mem_write[3] = {
3258 watch_mem_writeb,
3259 watch_mem_writew,
3260 watch_mem_writel,
3263 static inline uint32_t subpage_readlen (subpage_t *mmio,
3264 target_phys_addr_t addr,
3265 unsigned int len)
3267 unsigned int idx = SUBPAGE_IDX(addr);
3268 #if defined(DEBUG_SUBPAGE)
3269 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3270 mmio, len, addr, idx);
3271 #endif
3273 addr += mmio->region_offset[idx];
3274 idx = mmio->sub_io_index[idx];
3275 return io_mem_read[idx][len](io_mem_opaque[idx], addr);
3278 static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
3279 uint32_t value, unsigned int len)
3281 unsigned int idx = SUBPAGE_IDX(addr);
3282 #if defined(DEBUG_SUBPAGE)
3283 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n",
3284 __func__, mmio, len, addr, idx, value);
3285 #endif
3287 addr += mmio->region_offset[idx];
3288 idx = mmio->sub_io_index[idx];
3289 io_mem_write[idx][len](io_mem_opaque[idx], addr, value);
3292 static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
3294 return subpage_readlen(opaque, addr, 0);
3297 static void subpage_writeb (void *opaque, target_phys_addr_t addr,
3298 uint32_t value)
3300 subpage_writelen(opaque, addr, value, 0);
3303 static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
3305 return subpage_readlen(opaque, addr, 1);
3308 static void subpage_writew (void *opaque, target_phys_addr_t addr,
3309 uint32_t value)
3311 subpage_writelen(opaque, addr, value, 1);
3314 static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
3316 return subpage_readlen(opaque, addr, 2);
3319 static void subpage_writel (void *opaque, target_phys_addr_t addr,
3320 uint32_t value)
3322 subpage_writelen(opaque, addr, value, 2);
3325 static CPUReadMemoryFunc * const subpage_read[] = {
3326 &subpage_readb,
3327 &subpage_readw,
3328 &subpage_readl,
3331 static CPUWriteMemoryFunc * const subpage_write[] = {
3332 &subpage_writeb,
3333 &subpage_writew,
3334 &subpage_writel,
3337 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3338 ram_addr_t memory, ram_addr_t region_offset)
3340 int idx, eidx;
3342 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3343 return -1;
3344 idx = SUBPAGE_IDX(start);
3345 eidx = SUBPAGE_IDX(end);
3346 #if defined(DEBUG_SUBPAGE)
3347 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
3348 mmio, start, end, idx, eidx, memory);
3349 #endif
3350 if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
3351 memory = IO_MEM_UNASSIGNED;
3352 memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3353 for (; idx <= eidx; idx++) {
3354 mmio->sub_io_index[idx] = memory;
3355 mmio->region_offset[idx] = region_offset;
3358 return 0;
3361 static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3362 ram_addr_t orig_memory,
3363 ram_addr_t region_offset)
3365 subpage_t *mmio;
3366 int subpage_memory;
3368 mmio = qemu_mallocz(sizeof(subpage_t));
3370 mmio->base = base;
3371 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio,
3372 DEVICE_NATIVE_ENDIAN);
3373 #if defined(DEBUG_SUBPAGE)
3374 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3375 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
3376 #endif
3377 *phys = subpage_memory | IO_MEM_SUBPAGE;
3378 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset);
3380 return mmio;
3383 static int get_free_io_mem_idx(void)
3385 int i;
3387 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3388 if (!io_mem_used[i]) {
3389 io_mem_used[i] = 1;
3390 return i;
3392 fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
3393 return -1;
3397 * Usually, devices operate in little endian mode. There are devices out
3398 * there that operate in big endian too. Each device gets byte swapped
3399 * mmio if plugged onto a CPU that does the other endianness.
3401 * CPU Device swap?
3403 * little little no
3404 * little big yes
3405 * big little yes
3406 * big big no
3409 typedef struct SwapEndianContainer {
3410 CPUReadMemoryFunc *read[3];
3411 CPUWriteMemoryFunc *write[3];
3412 void *opaque;
3413 } SwapEndianContainer;
3415 static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr)
3417 uint32_t val;
3418 SwapEndianContainer *c = opaque;
3419 val = c->read[0](c->opaque, addr);
3420 return val;
3423 static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr)
3425 uint32_t val;
3426 SwapEndianContainer *c = opaque;
3427 val = bswap16(c->read[1](c->opaque, addr));
3428 return val;
3431 static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr)
3433 uint32_t val;
3434 SwapEndianContainer *c = opaque;
3435 val = bswap32(c->read[2](c->opaque, addr));
3436 return val;
3439 static CPUReadMemoryFunc * const swapendian_readfn[3]={
3440 swapendian_mem_readb,
3441 swapendian_mem_readw,
3442 swapendian_mem_readl
3445 static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr,
3446 uint32_t val)
3448 SwapEndianContainer *c = opaque;
3449 c->write[0](c->opaque, addr, val);
3452 static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr,
3453 uint32_t val)
3455 SwapEndianContainer *c = opaque;
3456 c->write[1](c->opaque, addr, bswap16(val));
3459 static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr,
3460 uint32_t val)
3462 SwapEndianContainer *c = opaque;
3463 c->write[2](c->opaque, addr, bswap32(val));
3466 static CPUWriteMemoryFunc * const swapendian_writefn[3]={
3467 swapendian_mem_writeb,
3468 swapendian_mem_writew,
3469 swapendian_mem_writel
3472 static void swapendian_init(int io_index)
3474 SwapEndianContainer *c = qemu_malloc(sizeof(SwapEndianContainer));
3475 int i;
3477 /* Swap mmio for big endian targets */
3478 c->opaque = io_mem_opaque[io_index];
3479 for (i = 0; i < 3; i++) {
3480 c->read[i] = io_mem_read[io_index][i];
3481 c->write[i] = io_mem_write[io_index][i];
3483 io_mem_read[io_index][i] = swapendian_readfn[i];
3484 io_mem_write[io_index][i] = swapendian_writefn[i];
3486 io_mem_opaque[io_index] = c;
3489 static void swapendian_del(int io_index)
3491 if (io_mem_read[io_index][0] == swapendian_readfn[0]) {
3492 qemu_free(io_mem_opaque[io_index]);
3496 /* mem_read and mem_write are arrays of functions containing the
3497 function to access byte (index 0), word (index 1) and dword (index
3498 2). Functions can be omitted with a NULL function pointer.
3499 If io_index is non zero, the corresponding io zone is
3500 modified. If it is zero, a new io zone is allocated. The return
3501 value can be used with cpu_register_physical_memory(). (-1) is
3502 returned if error. */
3503 static int cpu_register_io_memory_fixed(int io_index,
3504 CPUReadMemoryFunc * const *mem_read,
3505 CPUWriteMemoryFunc * const *mem_write,
3506 void *opaque, enum device_endian endian)
3508 int i;
3510 if (io_index <= 0) {
3511 io_index = get_free_io_mem_idx();
3512 if (io_index == -1)
3513 return io_index;
3514 } else {
3515 io_index >>= IO_MEM_SHIFT;
3516 if (io_index >= IO_MEM_NB_ENTRIES)
3517 return -1;
3520 for (i = 0; i < 3; ++i) {
3521 io_mem_read[io_index][i]
3522 = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]);
3524 for (i = 0; i < 3; ++i) {
3525 io_mem_write[io_index][i]
3526 = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]);
3528 io_mem_opaque[io_index] = opaque;
3530 switch (endian) {
3531 case DEVICE_BIG_ENDIAN:
3532 #ifndef TARGET_WORDS_BIGENDIAN
3533 swapendian_init(io_index);
3534 #endif
3535 break;
3536 case DEVICE_LITTLE_ENDIAN:
3537 #ifdef TARGET_WORDS_BIGENDIAN
3538 swapendian_init(io_index);
3539 #endif
3540 break;
3541 case DEVICE_NATIVE_ENDIAN:
3542 default:
3543 break;
3546 return (io_index << IO_MEM_SHIFT);
3549 int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
3550 CPUWriteMemoryFunc * const *mem_write,
3551 void *opaque, enum device_endian endian)
3553 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian);
3556 void cpu_unregister_io_memory(int io_table_address)
3558 int i;
3559 int io_index = io_table_address >> IO_MEM_SHIFT;
3561 swapendian_del(io_index);
3563 for (i=0;i < 3; i++) {
3564 io_mem_read[io_index][i] = unassigned_mem_read[i];
3565 io_mem_write[io_index][i] = unassigned_mem_write[i];
3567 io_mem_opaque[io_index] = NULL;
3568 io_mem_used[io_index] = 0;
3571 static void io_mem_init(void)
3573 int i;
3575 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read,
3576 unassigned_mem_write, NULL,
3577 DEVICE_NATIVE_ENDIAN);
3578 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read,
3579 unassigned_mem_write, NULL,
3580 DEVICE_NATIVE_ENDIAN);
3581 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read,
3582 notdirty_mem_write, NULL,
3583 DEVICE_NATIVE_ENDIAN);
3584 for (i=0; i<5; i++)
3585 io_mem_used[i] = 1;
3587 io_mem_watch = cpu_register_io_memory(watch_mem_read,
3588 watch_mem_write, NULL,
3589 DEVICE_NATIVE_ENDIAN);
3592 #endif /* !defined(CONFIG_USER_ONLY) */
3594 /* physical memory access (slow version, mainly for debug) */
3595 #if defined(CONFIG_USER_ONLY)
3596 int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3597 uint8_t *buf, int len, int is_write)
3599 int l, flags;
3600 target_ulong page;
3601 void * p;
3603 while (len > 0) {
3604 page = addr & TARGET_PAGE_MASK;
3605 l = (page + TARGET_PAGE_SIZE) - addr;
3606 if (l > len)
3607 l = len;
3608 flags = page_get_flags(page);
3609 if (!(flags & PAGE_VALID))
3610 return -1;
3611 if (is_write) {
3612 if (!(flags & PAGE_WRITE))
3613 return -1;
3614 /* XXX: this code should not depend on lock_user */
3615 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
3616 return -1;
3617 memcpy(p, buf, l);
3618 unlock_user(p, addr, l);
3619 } else {
3620 if (!(flags & PAGE_READ))
3621 return -1;
3622 /* XXX: this code should not depend on lock_user */
3623 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
3624 return -1;
3625 memcpy(buf, p, l);
3626 unlock_user(p, addr, 0);
3628 len -= l;
3629 buf += l;
3630 addr += l;
3632 return 0;
3635 #else
3636 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
3637 int len, int is_write)
3639 int l, io_index;
3640 uint8_t *ptr;
3641 uint32_t val;
3642 target_phys_addr_t page;
3643 unsigned long pd;
3644 PhysPageDesc *p;
3646 while (len > 0) {
3647 page = addr & TARGET_PAGE_MASK;
3648 l = (page + TARGET_PAGE_SIZE) - addr;
3649 if (l > len)
3650 l = len;
3651 p = phys_page_find(page >> TARGET_PAGE_BITS);
3652 if (!p) {
3653 pd = IO_MEM_UNASSIGNED;
3654 } else {
3655 pd = p->phys_offset;
3658 if (is_write) {
3659 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3660 target_phys_addr_t addr1 = addr;
3661 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3662 if (p)
3663 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3664 /* XXX: could force cpu_single_env to NULL to avoid
3665 potential bugs */
3666 if (l >= 4 && ((addr1 & 3) == 0)) {
3667 /* 32 bit write access */
3668 val = ldl_p(buf);
3669 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
3670 l = 4;
3671 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3672 /* 16 bit write access */
3673 val = lduw_p(buf);
3674 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
3675 l = 2;
3676 } else {
3677 /* 8 bit write access */
3678 val = ldub_p(buf);
3679 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
3680 l = 1;
3682 } else {
3683 unsigned long addr1;
3684 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3685 /* RAM case */
3686 ptr = qemu_get_ram_ptr(addr1);
3687 memcpy(ptr, buf, l);
3688 if (!cpu_physical_memory_is_dirty(addr1)) {
3689 /* invalidate code */
3690 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3691 /* set dirty bit */
3692 cpu_physical_memory_set_dirty_flags(
3693 addr1, (0xff & ~CODE_DIRTY_FLAG));
3695 /* qemu doesn't execute guest code directly, but kvm does
3696 therefore flush instruction caches */
3697 if (kvm_enabled())
3698 flush_icache_range((unsigned long)ptr,
3699 ((unsigned long)ptr)+l);
3701 } else {
3702 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3703 !(pd & IO_MEM_ROMD)) {
3704 target_phys_addr_t addr1 = addr;
3705 /* I/O case */
3706 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3707 if (p)
3708 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3709 if (l >= 4 && ((addr1 & 3) == 0)) {
3710 /* 32 bit read access */
3711 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
3712 stl_p(buf, val);
3713 l = 4;
3714 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3715 /* 16 bit read access */
3716 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
3717 stw_p(buf, val);
3718 l = 2;
3719 } else {
3720 /* 8 bit read access */
3721 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
3722 stb_p(buf, val);
3723 l = 1;
3725 } else {
3726 /* RAM case */
3727 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3728 (addr & ~TARGET_PAGE_MASK);
3729 memcpy(buf, ptr, l);
3732 len -= l;
3733 buf += l;
3734 addr += l;
3738 /* used for ROM loading : can write in RAM and ROM */
3739 void cpu_physical_memory_write_rom(target_phys_addr_t addr,
3740 const uint8_t *buf, int len)
3742 int l;
3743 uint8_t *ptr;
3744 target_phys_addr_t page;
3745 unsigned long pd;
3746 PhysPageDesc *p;
3748 while (len > 0) {
3749 page = addr & TARGET_PAGE_MASK;
3750 l = (page + TARGET_PAGE_SIZE) - addr;
3751 if (l > len)
3752 l = len;
3753 p = phys_page_find(page >> TARGET_PAGE_BITS);
3754 if (!p) {
3755 pd = IO_MEM_UNASSIGNED;
3756 } else {
3757 pd = p->phys_offset;
3760 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
3761 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3762 !(pd & IO_MEM_ROMD)) {
3763 /* do nothing */
3764 } else {
3765 unsigned long addr1;
3766 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3767 /* ROM/RAM case */
3768 ptr = qemu_get_ram_ptr(addr1);
3769 memcpy(ptr, buf, l);
3771 len -= l;
3772 buf += l;
3773 addr += l;
3777 typedef struct {
3778 void *buffer;
3779 target_phys_addr_t addr;
3780 target_phys_addr_t len;
3781 } BounceBuffer;
3783 static BounceBuffer bounce;
3785 typedef struct MapClient {
3786 void *opaque;
3787 void (*callback)(void *opaque);
3788 QLIST_ENTRY(MapClient) link;
3789 } MapClient;
3791 static QLIST_HEAD(map_client_list, MapClient) map_client_list
3792 = QLIST_HEAD_INITIALIZER(map_client_list);
3794 void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3796 MapClient *client = qemu_malloc(sizeof(*client));
3798 client->opaque = opaque;
3799 client->callback = callback;
3800 QLIST_INSERT_HEAD(&map_client_list, client, link);
3801 return client;
3804 void cpu_unregister_map_client(void *_client)
3806 MapClient *client = (MapClient *)_client;
3808 QLIST_REMOVE(client, link);
3809 qemu_free(client);
3812 static void cpu_notify_map_clients(void)
3814 MapClient *client;
3816 while (!QLIST_EMPTY(&map_client_list)) {
3817 client = QLIST_FIRST(&map_client_list);
3818 client->callback(client->opaque);
3819 cpu_unregister_map_client(client);
3823 /* Map a physical memory region into a host virtual address.
3824 * May map a subset of the requested range, given by and returned in *plen.
3825 * May return NULL if resources needed to perform the mapping are exhausted.
3826 * Use only for reads OR writes - not for read-modify-write operations.
3827 * Use cpu_register_map_client() to know when retrying the map operation is
3828 * likely to succeed.
3830 void *cpu_physical_memory_map(target_phys_addr_t addr,
3831 target_phys_addr_t *plen,
3832 int is_write)
3834 target_phys_addr_t len = *plen;
3835 target_phys_addr_t done = 0;
3836 int l;
3837 uint8_t *ret = NULL;
3838 uint8_t *ptr;
3839 target_phys_addr_t page;
3840 unsigned long pd;
3841 PhysPageDesc *p;
3842 unsigned long addr1;
3844 while (len > 0) {
3845 page = addr & TARGET_PAGE_MASK;
3846 l = (page + TARGET_PAGE_SIZE) - addr;
3847 if (l > len)
3848 l = len;
3849 p = phys_page_find(page >> TARGET_PAGE_BITS);
3850 if (!p) {
3851 pd = IO_MEM_UNASSIGNED;
3852 } else {
3853 pd = p->phys_offset;
3856 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3857 if (done || bounce.buffer) {
3858 break;
3860 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3861 bounce.addr = addr;
3862 bounce.len = l;
3863 if (!is_write) {
3864 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3866 ptr = bounce.buffer;
3867 } else {
3868 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3869 ptr = qemu_get_ram_ptr(addr1);
3871 if (!done) {
3872 ret = ptr;
3873 } else if (ret + done != ptr) {
3874 break;
3877 len -= l;
3878 addr += l;
3879 done += l;
3881 *plen = done;
3882 return ret;
3885 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3886 * Will also mark the memory as dirty if is_write == 1. access_len gives
3887 * the amount of memory that was actually read or written by the caller.
3889 void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3890 int is_write, target_phys_addr_t access_len)
3892 unsigned long flush_len = (unsigned long)access_len;
3894 if (buffer != bounce.buffer) {
3895 if (is_write) {
3896 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
3897 while (access_len) {
3898 unsigned l;
3899 l = TARGET_PAGE_SIZE;
3900 if (l > access_len)
3901 l = access_len;
3902 if (!cpu_physical_memory_is_dirty(addr1)) {
3903 /* invalidate code */
3904 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3905 /* set dirty bit */
3906 cpu_physical_memory_set_dirty_flags(
3907 addr1, (0xff & ~CODE_DIRTY_FLAG));
3909 addr1 += l;
3910 access_len -= l;
3912 dma_flush_range((unsigned long)buffer,
3913 (unsigned long)buffer + flush_len);
3915 return;
3917 if (is_write) {
3918 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3920 qemu_vfree(bounce.buffer);
3921 bounce.buffer = NULL;
3922 cpu_notify_map_clients();
3925 /* warning: addr must be aligned */
3926 uint32_t ldl_phys(target_phys_addr_t addr)
3928 int io_index;
3929 uint8_t *ptr;
3930 uint32_t val;
3931 unsigned long pd;
3932 PhysPageDesc *p;
3934 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3935 if (!p) {
3936 pd = IO_MEM_UNASSIGNED;
3937 } else {
3938 pd = p->phys_offset;
3941 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3942 !(pd & IO_MEM_ROMD)) {
3943 /* I/O case */
3944 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3945 if (p)
3946 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3947 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3948 } else {
3949 /* RAM case */
3950 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3951 (addr & ~TARGET_PAGE_MASK);
3952 val = ldl_p(ptr);
3954 return val;
3957 /* warning: addr must be aligned */
3958 uint64_t ldq_phys(target_phys_addr_t addr)
3960 int io_index;
3961 uint8_t *ptr;
3962 uint64_t val;
3963 unsigned long pd;
3964 PhysPageDesc *p;
3966 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3967 if (!p) {
3968 pd = IO_MEM_UNASSIGNED;
3969 } else {
3970 pd = p->phys_offset;
3973 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3974 !(pd & IO_MEM_ROMD)) {
3975 /* I/O case */
3976 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3977 if (p)
3978 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3979 #ifdef TARGET_WORDS_BIGENDIAN
3980 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3981 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3982 #else
3983 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3984 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3985 #endif
3986 } else {
3987 /* RAM case */
3988 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3989 (addr & ~TARGET_PAGE_MASK);
3990 val = ldq_p(ptr);
3992 return val;
3995 /* XXX: optimize */
3996 uint32_t ldub_phys(target_phys_addr_t addr)
3998 uint8_t val;
3999 cpu_physical_memory_read(addr, &val, 1);
4000 return val;
4003 /* warning: addr must be aligned */
4004 uint32_t lduw_phys(target_phys_addr_t addr)
4006 int io_index;
4007 uint8_t *ptr;
4008 uint64_t val;
4009 unsigned long pd;
4010 PhysPageDesc *p;
4012 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4013 if (!p) {
4014 pd = IO_MEM_UNASSIGNED;
4015 } else {
4016 pd = p->phys_offset;
4019 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
4020 !(pd & IO_MEM_ROMD)) {
4021 /* I/O case */
4022 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4023 if (p)
4024 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4025 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
4026 } else {
4027 /* RAM case */
4028 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
4029 (addr & ~TARGET_PAGE_MASK);
4030 val = lduw_p(ptr);
4032 return val;
4035 /* warning: addr must be aligned. The ram page is not masked as dirty
4036 and the code inside is not invalidated. It is useful if the dirty
4037 bits are used to track modified PTEs */
4038 void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
4040 int io_index;
4041 uint8_t *ptr;
4042 unsigned long pd;
4043 PhysPageDesc *p;
4045 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4046 if (!p) {
4047 pd = IO_MEM_UNASSIGNED;
4048 } else {
4049 pd = p->phys_offset;
4052 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4053 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4054 if (p)
4055 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4056 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4057 } else {
4058 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4059 ptr = qemu_get_ram_ptr(addr1);
4060 stl_p(ptr, val);
4062 if (unlikely(in_migration)) {
4063 if (!cpu_physical_memory_is_dirty(addr1)) {
4064 /* invalidate code */
4065 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4066 /* set dirty bit */
4067 cpu_physical_memory_set_dirty_flags(
4068 addr1, (0xff & ~CODE_DIRTY_FLAG));
4074 void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
4076 int io_index;
4077 uint8_t *ptr;
4078 unsigned long pd;
4079 PhysPageDesc *p;
4081 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4082 if (!p) {
4083 pd = IO_MEM_UNASSIGNED;
4084 } else {
4085 pd = p->phys_offset;
4088 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4089 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4090 if (p)
4091 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4092 #ifdef TARGET_WORDS_BIGENDIAN
4093 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
4094 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
4095 #else
4096 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4097 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
4098 #endif
4099 } else {
4100 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
4101 (addr & ~TARGET_PAGE_MASK);
4102 stq_p(ptr, val);
4106 /* warning: addr must be aligned */
4107 void stl_phys(target_phys_addr_t addr, uint32_t val)
4109 int io_index;
4110 uint8_t *ptr;
4111 unsigned long pd;
4112 PhysPageDesc *p;
4114 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4115 if (!p) {
4116 pd = IO_MEM_UNASSIGNED;
4117 } else {
4118 pd = p->phys_offset;
4121 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4122 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4123 if (p)
4124 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4125 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4126 } else {
4127 unsigned long addr1;
4128 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4129 /* RAM case */
4130 ptr = qemu_get_ram_ptr(addr1);
4131 stl_p(ptr, val);
4132 if (!cpu_physical_memory_is_dirty(addr1)) {
4133 /* invalidate code */
4134 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4135 /* set dirty bit */
4136 cpu_physical_memory_set_dirty_flags(addr1,
4137 (0xff & ~CODE_DIRTY_FLAG));
4142 /* XXX: optimize */
4143 void stb_phys(target_phys_addr_t addr, uint32_t val)
4145 uint8_t v = val;
4146 cpu_physical_memory_write(addr, &v, 1);
4149 /* warning: addr must be aligned */
4150 void stw_phys(target_phys_addr_t addr, uint32_t val)
4152 int io_index;
4153 uint8_t *ptr;
4154 unsigned long pd;
4155 PhysPageDesc *p;
4157 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4158 if (!p) {
4159 pd = IO_MEM_UNASSIGNED;
4160 } else {
4161 pd = p->phys_offset;
4164 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4165 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4166 if (p)
4167 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4168 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
4169 } else {
4170 unsigned long addr1;
4171 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4172 /* RAM case */
4173 ptr = qemu_get_ram_ptr(addr1);
4174 stw_p(ptr, val);
4175 if (!cpu_physical_memory_is_dirty(addr1)) {
4176 /* invalidate code */
4177 tb_invalidate_phys_page_range(addr1, addr1 + 2, 0);
4178 /* set dirty bit */
4179 cpu_physical_memory_set_dirty_flags(addr1,
4180 (0xff & ~CODE_DIRTY_FLAG));
4185 /* XXX: optimize */
4186 void stq_phys(target_phys_addr_t addr, uint64_t val)
4188 val = tswap64(val);
4189 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
4192 /* virtual memory access for debug (includes writing to ROM) */
4193 int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
4194 uint8_t *buf, int len, int is_write)
4196 int l;
4197 target_phys_addr_t phys_addr;
4198 target_ulong page;
4200 while (len > 0) {
4201 page = addr & TARGET_PAGE_MASK;
4202 phys_addr = cpu_get_phys_page_debug(env, page);
4203 /* if no physical page mapped, return an error */
4204 if (phys_addr == -1)
4205 return -1;
4206 l = (page + TARGET_PAGE_SIZE) - addr;
4207 if (l > len)
4208 l = len;
4209 phys_addr += (addr & ~TARGET_PAGE_MASK);
4210 if (is_write)
4211 cpu_physical_memory_write_rom(phys_addr, buf, l);
4212 else
4213 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
4214 len -= l;
4215 buf += l;
4216 addr += l;
4218 return 0;
4220 #endif
4222 /* in deterministic execution mode, instructions doing device I/Os
4223 must be at the end of the TB */
4224 void cpu_io_recompile(CPUState *env, void *retaddr)
4226 TranslationBlock *tb;
4227 uint32_t n, cflags;
4228 target_ulong pc, cs_base;
4229 uint64_t flags;
4231 tb = tb_find_pc((unsigned long)retaddr);
4232 if (!tb) {
4233 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
4234 retaddr);
4236 n = env->icount_decr.u16.low + tb->icount;
4237 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
4238 /* Calculate how many instructions had been executed before the fault
4239 occurred. */
4240 n = n - env->icount_decr.u16.low;
4241 /* Generate a new TB ending on the I/O insn. */
4242 n++;
4243 /* On MIPS and SH, delay slot instructions can only be restarted if
4244 they were already the first instruction in the TB. If this is not
4245 the first instruction in a TB then re-execute the preceding
4246 branch. */
4247 #if defined(TARGET_MIPS)
4248 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
4249 env->active_tc.PC -= 4;
4250 env->icount_decr.u16.low++;
4251 env->hflags &= ~MIPS_HFLAG_BMASK;
4253 #elif defined(TARGET_SH4)
4254 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
4255 && n > 1) {
4256 env->pc -= 2;
4257 env->icount_decr.u16.low++;
4258 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
4260 #endif
4261 /* This should never happen. */
4262 if (n > CF_COUNT_MASK)
4263 cpu_abort(env, "TB too big during recompile");
4265 cflags = n | CF_LAST_IO;
4266 pc = tb->pc;
4267 cs_base = tb->cs_base;
4268 flags = tb->flags;
4269 tb_phys_invalidate(tb, -1);
4270 /* FIXME: In theory this could raise an exception. In practice
4271 we have already translated the block once so it's probably ok. */
4272 tb_gen_code(env, pc, cs_base, flags, cflags);
4273 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
4274 the first in the TB) then we end up generating a whole new TB and
4275 repeating the fault, which is horribly inefficient.
4276 Better would be to execute just this insn uncached, or generate a
4277 second new TB. */
4278 cpu_resume_from_signal(env, NULL);
4281 #if !defined(CONFIG_USER_ONLY)
4283 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
4285 int i, target_code_size, max_target_code_size;
4286 int direct_jmp_count, direct_jmp2_count, cross_page;
4287 TranslationBlock *tb;
4289 target_code_size = 0;
4290 max_target_code_size = 0;
4291 cross_page = 0;
4292 direct_jmp_count = 0;
4293 direct_jmp2_count = 0;
4294 for(i = 0; i < nb_tbs; i++) {
4295 tb = &tbs[i];
4296 target_code_size += tb->size;
4297 if (tb->size > max_target_code_size)
4298 max_target_code_size = tb->size;
4299 if (tb->page_addr[1] != -1)
4300 cross_page++;
4301 if (tb->tb_next_offset[0] != 0xffff) {
4302 direct_jmp_count++;
4303 if (tb->tb_next_offset[1] != 0xffff) {
4304 direct_jmp2_count++;
4308 /* XXX: avoid using doubles ? */
4309 cpu_fprintf(f, "Translation buffer state:\n");
4310 cpu_fprintf(f, "gen code size %td/%ld\n",
4311 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
4312 cpu_fprintf(f, "TB count %d/%d\n",
4313 nb_tbs, code_gen_max_blocks);
4314 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
4315 nb_tbs ? target_code_size / nb_tbs : 0,
4316 max_target_code_size);
4317 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
4318 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
4319 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
4320 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
4321 cross_page,
4322 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
4323 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
4324 direct_jmp_count,
4325 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
4326 direct_jmp2_count,
4327 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
4328 cpu_fprintf(f, "\nStatistics:\n");
4329 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
4330 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
4331 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
4332 #ifdef CONFIG_PROFILER
4333 tcg_dump_info(f, cpu_fprintf);
4334 #endif
4337 #define MMUSUFFIX _cmmu
4338 #define GETPC() NULL
4339 #define env cpu_single_env
4340 #define SOFTMMU_CODE_ACCESS
4342 #define SHIFT 0
4343 #include "softmmu_template.h"
4345 #define SHIFT 1
4346 #include "softmmu_template.h"
4348 #define SHIFT 2
4349 #include "softmmu_template.h"
4351 #define SHIFT 3
4352 #include "softmmu_template.h"
4354 #undef env
4356 #endif