blob: 7a889013e0c813ada36b5fea58d25052200c1f71 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
bellardfd6ce8f2003-05-14 19:00:11 +00002 * virtual page mapping and translated block handling
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
bellard67b915a2004-03-31 23:37:16 +000020#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000021#ifdef _WIN32
ths4fddf622007-12-17 04:42:29 +000022#define WIN32_LEAN_AND_MEAN
bellardd5a8f072004-09-29 21:15:28 +000023#include <windows.h>
24#else
bellarda98d49b2004-11-14 16:22:05 +000025#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000026#include <sys/mman.h>
27#endif
bellard54936002003-05-13 00:25:15 +000028#include <stdlib.h>
29#include <stdio.h>
30#include <stdarg.h>
31#include <string.h>
32#include <errno.h>
33#include <unistd.h>
34#include <inttypes.h>
35
bellard6180a182003-09-30 21:04:53 +000036#include "cpu.h"
37#include "exec-all.h"
aurel32ca10f862008-04-11 21:35:42 +000038#include "qemu-common.h"
bellardb67d9a52008-05-23 09:57:34 +000039#include "tcg.h"
pbrook53a59602006-03-25 19:31:22 +000040#if defined(CONFIG_USER_ONLY)
41#include <qemu.h>
42#endif
bellard54936002003-05-13 00:25:15 +000043
bellardfd6ce8f2003-05-14 19:00:11 +000044//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000045//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000046//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000047//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000048
49/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000050//#define DEBUG_TB_CHECK
51//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000052
ths1196be32007-03-17 15:17:58 +000053//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000054//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000055
pbrook99773bd2006-04-16 15:14:59 +000056#if !defined(CONFIG_USER_ONLY)
57/* TB consistency checks only implemented for usermode emulation. */
58#undef DEBUG_TB_CHECK
59#endif
60
bellard9fa3e852004-01-04 18:06:42 +000061#define SMC_BITMAP_USE_THRESHOLD 10
62
63#define MMAP_AREA_START 0x00000000
64#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000065
bellard108c49b2005-07-24 12:55:09 +000066#if defined(TARGET_SPARC64)
67#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000068#elif defined(TARGET_SPARC)
69#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000070#elif defined(TARGET_ALPHA)
71#define TARGET_PHYS_ADDR_SPACE_BITS 42
72#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000073#elif defined(TARGET_PPC64)
74#define TARGET_PHYS_ADDR_SPACE_BITS 42
aurel3200f82b82008-04-27 21:12:55 +000075#elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
76#define TARGET_PHYS_ADDR_SPACE_BITS 42
77#elif defined(TARGET_I386) && !defined(USE_KQEMU)
78#define TARGET_PHYS_ADDR_SPACE_BITS 36
bellard108c49b2005-07-24 12:55:09 +000079#else
80/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
81#define TARGET_PHYS_ADDR_SPACE_BITS 32
82#endif
83
pbrookfab94c02008-05-24 13:56:15 +000084TranslationBlock *tbs;
bellard26a5f132008-05-28 12:30:31 +000085int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000086TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000087int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000088/* any access to the tbs or the page table must use this lock */
89spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000090
bellard7cb69ca2008-05-10 10:55:51 +000091uint8_t code_gen_prologue[1024] __attribute__((aligned (32)));
bellard26a5f132008-05-28 12:30:31 +000092uint8_t *code_gen_buffer;
93unsigned long code_gen_buffer_size;
94/* threshold to flush the translated code buffer */
95unsigned long code_gen_buffer_max_size;
bellardfd6ce8f2003-05-14 19:00:11 +000096uint8_t *code_gen_ptr;
97
aurel3200f82b82008-04-27 21:12:55 +000098ram_addr_t phys_ram_size;
bellard9fa3e852004-01-04 18:06:42 +000099int phys_ram_fd;
100uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +0000101uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +0000102static ram_addr_t phys_ram_alloc_offset = 0;
bellard9fa3e852004-01-04 18:06:42 +0000103
bellard6a00d602005-11-21 23:25:50 +0000104CPUState *first_cpu;
105/* current CPU in the current thread. It is only valid inside
106 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000107CPUState *cpu_single_env;
bellard6a00d602005-11-21 23:25:50 +0000108
bellard54936002003-05-13 00:25:15 +0000109typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000110 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000111 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000112 /* in order to optimize self modifying code, we count the number
113 of lookups we do to a given page to use a bitmap */
114 unsigned int code_write_count;
115 uint8_t *code_bitmap;
116#if defined(CONFIG_USER_ONLY)
117 unsigned long flags;
118#endif
bellard54936002003-05-13 00:25:15 +0000119} PageDesc;
120
bellard92e873b2004-05-21 14:52:29 +0000121typedef struct PhysPageDesc {
122 /* offset in host memory of the page + io_index in the low 12 bits */
aurel3200f82b82008-04-27 21:12:55 +0000123 ram_addr_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000124} PhysPageDesc;
125
bellard54936002003-05-13 00:25:15 +0000126#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000127#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
128/* XXX: this is a temporary hack for alpha target.
129 * In the future, this is to be replaced by a multi-level table
130 * to actually be able to handle the complete 64 bits address space.
131 */
132#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
133#else
aurel3203875442008-04-22 20:45:18 +0000134#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000135#endif
bellard54936002003-05-13 00:25:15 +0000136
137#define L1_SIZE (1 << L1_BITS)
138#define L2_SIZE (1 << L2_BITS)
139
bellard33417e72003-08-10 21:47:01 +0000140static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000141
bellard83fb7ad2004-07-05 21:25:26 +0000142unsigned long qemu_real_host_page_size;
143unsigned long qemu_host_page_bits;
144unsigned long qemu_host_page_size;
145unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000146
bellard92e873b2004-05-21 14:52:29 +0000147/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000148static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000149PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000150
bellard33417e72003-08-10 21:47:01 +0000151/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000152CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
153CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000154void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000155static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000156#if defined(CONFIG_SOFTMMU)
157static int io_mem_watch;
158#endif
bellard33417e72003-08-10 21:47:01 +0000159
bellard34865132003-10-05 14:28:56 +0000160/* log support */
161char *logfilename = "/tmp/qemu.log";
162FILE *logfile;
163int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000164static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000165
bellarde3db7222005-01-26 22:00:47 +0000166/* statistics */
167static int tlb_flush_count;
168static int tb_flush_count;
169static int tb_phys_invalidate_count;
170
blueswir1db7b5422007-05-26 17:36:03 +0000171#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
172typedef struct subpage_t {
173 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000174 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
175 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
176 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000177} subpage_t;
178
bellard7cb69ca2008-05-10 10:55:51 +0000179#ifdef _WIN32
180static void map_exec(void *addr, long size)
181{
182 DWORD old_protect;
183 VirtualProtect(addr, size,
184 PAGE_EXECUTE_READWRITE, &old_protect);
185
186}
187#else
188static void map_exec(void *addr, long size)
189{
bellard43694152008-05-29 09:35:57 +0000190 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000191
bellard43694152008-05-29 09:35:57 +0000192 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000193 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000194 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000195
196 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000197 end += page_size - 1;
198 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000199
200 mprotect((void *)start, end - start,
201 PROT_READ | PROT_WRITE | PROT_EXEC);
202}
203#endif
204
bellardb346ff42003-06-15 20:05:50 +0000205static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000206{
bellard83fb7ad2004-07-05 21:25:26 +0000207 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000208 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000209#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000210 {
211 SYSTEM_INFO system_info;
212 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000213
bellardd5a8f072004-09-29 21:15:28 +0000214 GetSystemInfo(&system_info);
215 qemu_real_host_page_size = system_info.dwPageSize;
bellardd5a8f072004-09-29 21:15:28 +0000216 }
bellard67b915a2004-03-31 23:37:16 +0000217#else
bellard83fb7ad2004-07-05 21:25:26 +0000218 qemu_real_host_page_size = getpagesize();
bellard67b915a2004-03-31 23:37:16 +0000219#endif
bellard83fb7ad2004-07-05 21:25:26 +0000220 if (qemu_host_page_size == 0)
221 qemu_host_page_size = qemu_real_host_page_size;
222 if (qemu_host_page_size < TARGET_PAGE_SIZE)
223 qemu_host_page_size = TARGET_PAGE_SIZE;
224 qemu_host_page_bits = 0;
225 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
226 qemu_host_page_bits++;
227 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000228 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
229 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000230
231#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
232 {
233 long long startaddr, endaddr;
234 FILE *f;
235 int n;
236
237 f = fopen("/proc/self/maps", "r");
238 if (f) {
239 do {
240 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
241 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000242 startaddr = MIN(startaddr,
243 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
244 endaddr = MIN(endaddr,
245 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
pbrookb5fc9092008-05-29 13:56:10 +0000246 page_set_flags(startaddr & TARGET_PAGE_MASK,
balrog50a95692007-12-12 01:16:23 +0000247 TARGET_PAGE_ALIGN(endaddr),
248 PAGE_RESERVED);
249 }
250 } while (!feof(f));
251 fclose(f);
252 }
253 }
254#endif
bellard54936002003-05-13 00:25:15 +0000255}
256
aurel3200f82b82008-04-27 21:12:55 +0000257static inline PageDesc *page_find_alloc(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000258{
bellard54936002003-05-13 00:25:15 +0000259 PageDesc **lp, *p;
260
bellard54936002003-05-13 00:25:15 +0000261 lp = &l1_map[index >> L2_BITS];
262 p = *lp;
263 if (!p) {
264 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000265 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000266 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000267 *lp = p;
268 }
269 return p + (index & (L2_SIZE - 1));
270}
271
aurel3200f82b82008-04-27 21:12:55 +0000272static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000273{
bellard54936002003-05-13 00:25:15 +0000274 PageDesc *p;
275
bellard54936002003-05-13 00:25:15 +0000276 p = l1_map[index >> L2_BITS];
277 if (!p)
278 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000279 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000280}
281
bellard108c49b2005-07-24 12:55:09 +0000282static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000283{
bellard108c49b2005-07-24 12:55:09 +0000284 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000285 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000286
bellard108c49b2005-07-24 12:55:09 +0000287 p = (void **)l1_phys_map;
288#if TARGET_PHYS_ADDR_SPACE_BITS > 32
289
290#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
291#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
292#endif
293 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000294 p = *lp;
295 if (!p) {
296 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000297 if (!alloc)
298 return NULL;
299 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
300 memset(p, 0, sizeof(void *) * L1_SIZE);
301 *lp = p;
302 }
303#endif
304 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000305 pd = *lp;
306 if (!pd) {
307 int i;
bellard108c49b2005-07-24 12:55:09 +0000308 /* allocate if not found */
309 if (!alloc)
310 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000311 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
312 *lp = pd;
313 for (i = 0; i < L2_SIZE; i++)
314 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000315 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000316 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000317}
318
bellard108c49b2005-07-24 12:55:09 +0000319static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000320{
bellard108c49b2005-07-24 12:55:09 +0000321 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000322}
323
bellard9fa3e852004-01-04 18:06:42 +0000324#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000325static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000326static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000327 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000328#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000329
bellard43694152008-05-29 09:35:57 +0000330#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
331
332#if defined(CONFIG_USER_ONLY)
333/* Currently it is not recommanded to allocate big chunks of data in
334 user mode. It will change when a dedicated libc will be used */
335#define USE_STATIC_CODE_GEN_BUFFER
336#endif
337
338#ifdef USE_STATIC_CODE_GEN_BUFFER
339static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
340#endif
341
bellard26a5f132008-05-28 12:30:31 +0000342void code_gen_alloc(unsigned long tb_size)
343{
bellard43694152008-05-29 09:35:57 +0000344#ifdef USE_STATIC_CODE_GEN_BUFFER
345 code_gen_buffer = static_code_gen_buffer;
346 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
347 map_exec(code_gen_buffer, code_gen_buffer_size);
348#else
bellard26a5f132008-05-28 12:30:31 +0000349 code_gen_buffer_size = tb_size;
350 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000351#if defined(CONFIG_USER_ONLY)
352 /* in user mode, phys_ram_size is not meaningful */
353 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
354#else
bellard26a5f132008-05-28 12:30:31 +0000355 /* XXX: needs ajustments */
356 code_gen_buffer_size = (int)(phys_ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000357#endif
bellard26a5f132008-05-28 12:30:31 +0000358 }
359 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
360 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
361 /* The code gen buffer location may have constraints depending on
362 the host cpu and OS */
363#if defined(__linux__)
364 {
365 int flags;
366 flags = MAP_PRIVATE | MAP_ANONYMOUS;
367#if defined(__x86_64__)
368 flags |= MAP_32BIT;
369 /* Cannot map more than that */
370 if (code_gen_buffer_size > (800 * 1024 * 1024))
371 code_gen_buffer_size = (800 * 1024 * 1024);
372#endif
373 code_gen_buffer = mmap(NULL, code_gen_buffer_size,
374 PROT_WRITE | PROT_READ | PROT_EXEC,
375 flags, -1, 0);
376 if (code_gen_buffer == MAP_FAILED) {
377 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
378 exit(1);
379 }
380 }
381#else
382 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
383 if (!code_gen_buffer) {
384 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
385 exit(1);
386 }
387 map_exec(code_gen_buffer, code_gen_buffer_size);
388#endif
bellard43694152008-05-29 09:35:57 +0000389#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000390 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
391 code_gen_buffer_max_size = code_gen_buffer_size -
392 code_gen_max_block_size();
393 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
394 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
395}
396
397/* Must be called before using the QEMU cpus. 'tb_size' is the size
398 (in bytes) allocated to the translation buffer. Zero means default
399 size. */
400void cpu_exec_init_all(unsigned long tb_size)
401{
bellard26a5f132008-05-28 12:30:31 +0000402 cpu_gen_init();
403 code_gen_alloc(tb_size);
404 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000405 page_init();
bellard26a5f132008-05-28 12:30:31 +0000406 io_mem_init();
407}
408
bellard6a00d602005-11-21 23:25:50 +0000409void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000410{
bellard6a00d602005-11-21 23:25:50 +0000411 CPUState **penv;
412 int cpu_index;
413
bellard6a00d602005-11-21 23:25:50 +0000414 env->next_cpu = NULL;
415 penv = &first_cpu;
416 cpu_index = 0;
417 while (*penv != NULL) {
418 penv = (CPUState **)&(*penv)->next_cpu;
419 cpu_index++;
420 }
421 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000422 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000423 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000424}
425
bellard9fa3e852004-01-04 18:06:42 +0000426static inline void invalidate_page_bitmap(PageDesc *p)
427{
428 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000429 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000430 p->code_bitmap = NULL;
431 }
432 p->code_write_count = 0;
433}
434
bellardfd6ce8f2003-05-14 19:00:11 +0000435/* set to NULL all the 'first_tb' fields in all PageDescs */
436static void page_flush_tb(void)
437{
438 int i, j;
439 PageDesc *p;
440
441 for(i = 0; i < L1_SIZE; i++) {
442 p = l1_map[i];
443 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000444 for(j = 0; j < L2_SIZE; j++) {
445 p->first_tb = NULL;
446 invalidate_page_bitmap(p);
447 p++;
448 }
bellardfd6ce8f2003-05-14 19:00:11 +0000449 }
450 }
451}
452
453/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000454/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000455void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000456{
bellard6a00d602005-11-21 23:25:50 +0000457 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000458#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000459 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
460 (unsigned long)(code_gen_ptr - code_gen_buffer),
461 nb_tbs, nb_tbs > 0 ?
462 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000463#endif
bellard26a5f132008-05-28 12:30:31 +0000464 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000465 cpu_abort(env1, "Internal error: code buffer overflow\n");
466
bellardfd6ce8f2003-05-14 19:00:11 +0000467 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000468
bellard6a00d602005-11-21 23:25:50 +0000469 for(env = first_cpu; env != NULL; env = env->next_cpu) {
470 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
471 }
bellard9fa3e852004-01-04 18:06:42 +0000472
bellard8a8a6082004-10-03 13:36:49 +0000473 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000474 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000475
bellardfd6ce8f2003-05-14 19:00:11 +0000476 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000477 /* XXX: flush processor icache at this point if cache flush is
478 expensive */
bellarde3db7222005-01-26 22:00:47 +0000479 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000480}
481
482#ifdef DEBUG_TB_CHECK
483
j_mayerbc98a7e2007-04-04 07:55:12 +0000484static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000485{
486 TranslationBlock *tb;
487 int i;
488 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000489 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
490 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000491 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
492 address >= tb->pc + tb->size)) {
493 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000494 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000495 }
496 }
497 }
498}
499
500/* verify that all the pages have correct rights for code */
501static void tb_page_check(void)
502{
503 TranslationBlock *tb;
504 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000505
pbrook99773bd2006-04-16 15:14:59 +0000506 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
507 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000508 flags1 = page_get_flags(tb->pc);
509 flags2 = page_get_flags(tb->pc + tb->size - 1);
510 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
511 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000512 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000513 }
514 }
515 }
516}
517
bellardd4e81642003-05-25 16:46:15 +0000518void tb_jmp_check(TranslationBlock *tb)
519{
520 TranslationBlock *tb1;
521 unsigned int n1;
522
523 /* suppress any remaining jumps to this TB */
524 tb1 = tb->jmp_first;
525 for(;;) {
526 n1 = (long)tb1 & 3;
527 tb1 = (TranslationBlock *)((long)tb1 & ~3);
528 if (n1 == 2)
529 break;
530 tb1 = tb1->jmp_next[n1];
531 }
532 /* check end of list */
533 if (tb1 != tb) {
534 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
535 }
536}
537
bellardfd6ce8f2003-05-14 19:00:11 +0000538#endif
539
540/* invalidate one TB */
541static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
542 int next_offset)
543{
544 TranslationBlock *tb1;
545 for(;;) {
546 tb1 = *ptb;
547 if (tb1 == tb) {
548 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
549 break;
550 }
551 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
552 }
553}
554
bellard9fa3e852004-01-04 18:06:42 +0000555static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
556{
557 TranslationBlock *tb1;
558 unsigned int n1;
559
560 for(;;) {
561 tb1 = *ptb;
562 n1 = (long)tb1 & 3;
563 tb1 = (TranslationBlock *)((long)tb1 & ~3);
564 if (tb1 == tb) {
565 *ptb = tb1->page_next[n1];
566 break;
567 }
568 ptb = &tb1->page_next[n1];
569 }
570}
571
bellardd4e81642003-05-25 16:46:15 +0000572static inline void tb_jmp_remove(TranslationBlock *tb, int n)
573{
574 TranslationBlock *tb1, **ptb;
575 unsigned int n1;
576
577 ptb = &tb->jmp_next[n];
578 tb1 = *ptb;
579 if (tb1) {
580 /* find tb(n) in circular list */
581 for(;;) {
582 tb1 = *ptb;
583 n1 = (long)tb1 & 3;
584 tb1 = (TranslationBlock *)((long)tb1 & ~3);
585 if (n1 == n && tb1 == tb)
586 break;
587 if (n1 == 2) {
588 ptb = &tb1->jmp_first;
589 } else {
590 ptb = &tb1->jmp_next[n1];
591 }
592 }
593 /* now we can suppress tb(n) from the list */
594 *ptb = tb->jmp_next[n];
595
596 tb->jmp_next[n] = NULL;
597 }
598}
599
600/* reset the jump entry 'n' of a TB so that it is not chained to
601 another TB */
602static inline void tb_reset_jump(TranslationBlock *tb, int n)
603{
604 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
605}
606
aurel3200f82b82008-04-27 21:12:55 +0000607static inline void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000608{
bellard6a00d602005-11-21 23:25:50 +0000609 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000610 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000611 unsigned int h, n1;
aurel3200f82b82008-04-27 21:12:55 +0000612 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000613 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000614
bellard9fa3e852004-01-04 18:06:42 +0000615 /* remove the TB from the hash list */
616 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
617 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000618 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000619 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000620
bellard9fa3e852004-01-04 18:06:42 +0000621 /* remove the TB from the page list */
622 if (tb->page_addr[0] != page_addr) {
623 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
624 tb_page_remove(&p->first_tb, tb);
625 invalidate_page_bitmap(p);
626 }
627 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
628 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
629 tb_page_remove(&p->first_tb, tb);
630 invalidate_page_bitmap(p);
631 }
632
bellard8a40a182005-11-20 10:35:40 +0000633 tb_invalidated_flag = 1;
634
635 /* remove the TB from the hash list */
636 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000637 for(env = first_cpu; env != NULL; env = env->next_cpu) {
638 if (env->tb_jmp_cache[h] == tb)
639 env->tb_jmp_cache[h] = NULL;
640 }
bellard8a40a182005-11-20 10:35:40 +0000641
642 /* suppress this TB from the two jump lists */
643 tb_jmp_remove(tb, 0);
644 tb_jmp_remove(tb, 1);
645
646 /* suppress any remaining jumps to this TB */
647 tb1 = tb->jmp_first;
648 for(;;) {
649 n1 = (long)tb1 & 3;
650 if (n1 == 2)
651 break;
652 tb1 = (TranslationBlock *)((long)tb1 & ~3);
653 tb2 = tb1->jmp_next[n1];
654 tb_reset_jump(tb1, n1);
655 tb1->jmp_next[n1] = NULL;
656 tb1 = tb2;
657 }
658 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
659
bellarde3db7222005-01-26 22:00:47 +0000660 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000661}
662
663static inline void set_bits(uint8_t *tab, int start, int len)
664{
665 int end, mask, end1;
666
667 end = start + len;
668 tab += start >> 3;
669 mask = 0xff << (start & 7);
670 if ((start & ~7) == (end & ~7)) {
671 if (start < end) {
672 mask &= ~(0xff << (end & 7));
673 *tab |= mask;
674 }
675 } else {
676 *tab++ |= mask;
677 start = (start + 8) & ~7;
678 end1 = end & ~7;
679 while (start < end1) {
680 *tab++ = 0xff;
681 start += 8;
682 }
683 if (start < end) {
684 mask = ~(0xff << (end & 7));
685 *tab |= mask;
686 }
687 }
688}
689
690static void build_page_bitmap(PageDesc *p)
691{
692 int n, tb_start, tb_end;
693 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000694
bellard59817cc2004-02-16 22:01:13 +0000695 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000696 if (!p->code_bitmap)
697 return;
698 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
699
700 tb = p->first_tb;
701 while (tb != NULL) {
702 n = (long)tb & 3;
703 tb = (TranslationBlock *)((long)tb & ~3);
704 /* NOTE: this is subtle as a TB may span two physical pages */
705 if (n == 0) {
706 /* NOTE: tb_end may be after the end of the page, but
707 it is not a problem */
708 tb_start = tb->pc & ~TARGET_PAGE_MASK;
709 tb_end = tb_start + tb->size;
710 if (tb_end > TARGET_PAGE_SIZE)
711 tb_end = TARGET_PAGE_SIZE;
712 } else {
713 tb_start = 0;
714 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
715 }
716 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
717 tb = tb->page_next[n];
718 }
719}
720
bellardd720b932004-04-25 17:57:43 +0000721#ifdef TARGET_HAS_PRECISE_SMC
722
ths5fafdf22007-09-16 21:08:06 +0000723static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000724 target_ulong pc, target_ulong cs_base, int flags,
725 int cflags)
726{
727 TranslationBlock *tb;
728 uint8_t *tc_ptr;
729 target_ulong phys_pc, phys_page2, virt_page2;
730 int code_gen_size;
731
bellardc27004e2005-01-03 23:35:10 +0000732 phys_pc = get_phys_addr_code(env, pc);
733 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000734 if (!tb) {
735 /* flush must be done */
736 tb_flush(env);
737 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000738 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000739 }
740 tc_ptr = code_gen_ptr;
741 tb->tc_ptr = tc_ptr;
742 tb->cs_base = cs_base;
743 tb->flags = flags;
744 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000745 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000746 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
ths3b46e622007-09-17 08:09:54 +0000747
bellardd720b932004-04-25 17:57:43 +0000748 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000749 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000750 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000751 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000752 phys_page2 = get_phys_addr_code(env, virt_page2);
753 }
754 tb_link_phys(tb, phys_pc, phys_page2);
755}
756#endif
ths3b46e622007-09-17 08:09:54 +0000757
bellard9fa3e852004-01-04 18:06:42 +0000758/* invalidate all TBs which intersect with the target physical page
759 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000760 the same physical page. 'is_cpu_write_access' should be true if called
761 from a real cpu write access: the virtual CPU will exit the current
762 TB if code is modified inside this TB. */
aurel3200f82b82008-04-27 21:12:55 +0000763void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000764 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000765{
bellardd720b932004-04-25 17:57:43 +0000766 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000767 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000768 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000769 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000770 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000771 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000772
773 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000774 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000775 return;
ths5fafdf22007-09-16 21:08:06 +0000776 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000777 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
778 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000779 /* build code bitmap */
780 build_page_bitmap(p);
781 }
782
783 /* we remove all the TBs in the range [start, end[ */
784 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000785 current_tb_not_found = is_cpu_write_access;
786 current_tb_modified = 0;
787 current_tb = NULL; /* avoid warning */
788 current_pc = 0; /* avoid warning */
789 current_cs_base = 0; /* avoid warning */
790 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000791 tb = p->first_tb;
792 while (tb != NULL) {
793 n = (long)tb & 3;
794 tb = (TranslationBlock *)((long)tb & ~3);
795 tb_next = tb->page_next[n];
796 /* NOTE: this is subtle as a TB may span two physical pages */
797 if (n == 0) {
798 /* NOTE: tb_end may be after the end of the page, but
799 it is not a problem */
800 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
801 tb_end = tb_start + tb->size;
802 } else {
803 tb_start = tb->page_addr[1];
804 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
805 }
806 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000807#ifdef TARGET_HAS_PRECISE_SMC
808 if (current_tb_not_found) {
809 current_tb_not_found = 0;
810 current_tb = NULL;
811 if (env->mem_write_pc) {
812 /* now we have a real cpu fault */
813 current_tb = tb_find_pc(env->mem_write_pc);
814 }
815 }
816 if (current_tb == tb &&
817 !(current_tb->cflags & CF_SINGLE_INSN)) {
818 /* If we are modifying the current TB, we must stop
819 its execution. We could be more precise by checking
820 that the modification is after the current PC, but it
821 would require a specialized function to partially
822 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000823
bellardd720b932004-04-25 17:57:43 +0000824 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000825 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000826 env->mem_write_pc, NULL);
827#if defined(TARGET_I386)
828 current_flags = env->hflags;
829 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
830 current_cs_base = (target_ulong)env->segs[R_CS].base;
831 current_pc = current_cs_base + env->eip;
832#else
833#error unsupported CPU
834#endif
835 }
836#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000837 /* we need to do that to handle the case where a signal
838 occurs while doing tb_phys_invalidate() */
839 saved_tb = NULL;
840 if (env) {
841 saved_tb = env->current_tb;
842 env->current_tb = NULL;
843 }
bellard9fa3e852004-01-04 18:06:42 +0000844 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000845 if (env) {
846 env->current_tb = saved_tb;
847 if (env->interrupt_request && env->current_tb)
848 cpu_interrupt(env, env->interrupt_request);
849 }
bellard9fa3e852004-01-04 18:06:42 +0000850 }
851 tb = tb_next;
852 }
853#if !defined(CONFIG_USER_ONLY)
854 /* if no code remaining, no need to continue to use slow writes */
855 if (!p->first_tb) {
856 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000857 if (is_cpu_write_access) {
858 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
859 }
860 }
861#endif
862#ifdef TARGET_HAS_PRECISE_SMC
863 if (current_tb_modified) {
864 /* we generate a block containing just the instruction
865 modifying the memory. It will ensure that it cannot modify
866 itself */
bellardea1c1802004-06-14 18:56:36 +0000867 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000868 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000869 CF_SINGLE_INSN);
870 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000871 }
872#endif
873}
874
875/* len must be <= 8 and start must be a multiple of len */
aurel3200f82b82008-04-27 21:12:55 +0000876static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000877{
878 PageDesc *p;
879 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000880#if 0
bellarda4193c82004-06-03 14:01:43 +0000881 if (1) {
882 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000883 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
884 cpu_single_env->mem_write_vaddr, len,
885 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000886 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
887 }
bellard59817cc2004-02-16 22:01:13 +0000888 }
889#endif
bellard9fa3e852004-01-04 18:06:42 +0000890 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000891 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000892 return;
893 if (p->code_bitmap) {
894 offset = start & ~TARGET_PAGE_MASK;
895 b = p->code_bitmap[offset >> 3] >> (offset & 7);
896 if (b & ((1 << len) - 1))
897 goto do_invalidate;
898 } else {
899 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000900 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000901 }
902}
903
bellard9fa3e852004-01-04 18:06:42 +0000904#if !defined(CONFIG_SOFTMMU)
aurel3200f82b82008-04-27 21:12:55 +0000905static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +0000906 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000907{
bellardd720b932004-04-25 17:57:43 +0000908 int n, current_flags, current_tb_modified;
909 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000910 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000911 TranslationBlock *tb, *current_tb;
912#ifdef TARGET_HAS_PRECISE_SMC
913 CPUState *env = cpu_single_env;
914#endif
bellard9fa3e852004-01-04 18:06:42 +0000915
916 addr &= TARGET_PAGE_MASK;
917 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000918 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000919 return;
920 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000921 current_tb_modified = 0;
922 current_tb = NULL;
923 current_pc = 0; /* avoid warning */
924 current_cs_base = 0; /* avoid warning */
925 current_flags = 0; /* avoid warning */
926#ifdef TARGET_HAS_PRECISE_SMC
927 if (tb && pc != 0) {
928 current_tb = tb_find_pc(pc);
929 }
930#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000931 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000932 n = (long)tb & 3;
933 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000934#ifdef TARGET_HAS_PRECISE_SMC
935 if (current_tb == tb &&
936 !(current_tb->cflags & CF_SINGLE_INSN)) {
937 /* If we are modifying the current TB, we must stop
938 its execution. We could be more precise by checking
939 that the modification is after the current PC, but it
940 would require a specialized function to partially
941 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000942
bellardd720b932004-04-25 17:57:43 +0000943 current_tb_modified = 1;
944 cpu_restore_state(current_tb, env, pc, puc);
945#if defined(TARGET_I386)
946 current_flags = env->hflags;
947 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
948 current_cs_base = (target_ulong)env->segs[R_CS].base;
949 current_pc = current_cs_base + env->eip;
950#else
951#error unsupported CPU
952#endif
953 }
954#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000955 tb_phys_invalidate(tb, addr);
956 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000957 }
958 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000959#ifdef TARGET_HAS_PRECISE_SMC
960 if (current_tb_modified) {
961 /* we generate a block containing just the instruction
962 modifying the memory. It will ensure that it cannot modify
963 itself */
bellardea1c1802004-06-14 18:56:36 +0000964 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000965 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000966 CF_SINGLE_INSN);
967 cpu_resume_from_signal(env, puc);
968 }
969#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000970}
bellard9fa3e852004-01-04 18:06:42 +0000971#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000972
973/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000974static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000975 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000976{
977 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000978 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000979
bellard9fa3e852004-01-04 18:06:42 +0000980 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000981 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000982 tb->page_next[n] = p->first_tb;
983 last_first_tb = p->first_tb;
984 p->first_tb = (TranslationBlock *)((long)tb | n);
985 invalidate_page_bitmap(p);
986
bellard107db442004-06-22 18:48:46 +0000987#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000988
bellard9fa3e852004-01-04 18:06:42 +0000989#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000990 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000991 target_ulong addr;
992 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000993 int prot;
994
bellardfd6ce8f2003-05-14 19:00:11 +0000995 /* force the host page as non writable (writes will have a
996 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000997 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000998 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000999 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1000 addr += TARGET_PAGE_SIZE) {
1001
1002 p2 = page_find (addr >> TARGET_PAGE_BITS);
1003 if (!p2)
1004 continue;
1005 prot |= p2->flags;
1006 p2->flags &= ~PAGE_WRITE;
1007 page_get_flags(addr);
1008 }
ths5fafdf22007-09-16 21:08:06 +00001009 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001010 (prot & PAGE_BITS) & ~PAGE_WRITE);
1011#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001012 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001013 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001014#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001015 }
bellard9fa3e852004-01-04 18:06:42 +00001016#else
1017 /* if some code is already present, then the pages are already
1018 protected. So we handle the case where only the first TB is
1019 allocated in a physical page */
1020 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001021 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001022 }
1023#endif
bellardd720b932004-04-25 17:57:43 +00001024
1025#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001026}
1027
1028/* Allocate a new translation block. Flush the translation buffer if
1029 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001030TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001031{
1032 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001033
bellard26a5f132008-05-28 12:30:31 +00001034 if (nb_tbs >= code_gen_max_blocks ||
1035 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001036 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001037 tb = &tbs[nb_tbs++];
1038 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001039 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001040 return tb;
1041}
1042
bellard9fa3e852004-01-04 18:06:42 +00001043/* add a new TB and link it to the physical page tables. phys_page2 is
1044 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001045void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001046 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001047{
bellard9fa3e852004-01-04 18:06:42 +00001048 unsigned int h;
1049 TranslationBlock **ptb;
1050
1051 /* add in the physical hash table */
1052 h = tb_phys_hash_func(phys_pc);
1053 ptb = &tb_phys_hash[h];
1054 tb->phys_hash_next = *ptb;
1055 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001056
1057 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001058 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1059 if (phys_page2 != -1)
1060 tb_alloc_page(tb, 1, phys_page2);
1061 else
1062 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001063
bellardd4e81642003-05-25 16:46:15 +00001064 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1065 tb->jmp_next[0] = NULL;
1066 tb->jmp_next[1] = NULL;
1067
1068 /* init original jump addresses */
1069 if (tb->tb_next_offset[0] != 0xffff)
1070 tb_reset_jump(tb, 0);
1071 if (tb->tb_next_offset[1] != 0xffff)
1072 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001073
1074#ifdef DEBUG_TB_CHECK
1075 tb_page_check();
1076#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001077}
1078
bellarda513fe12003-05-27 23:29:48 +00001079/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1080 tb[1].tc_ptr. Return NULL if not found */
1081TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1082{
1083 int m_min, m_max, m;
1084 unsigned long v;
1085 TranslationBlock *tb;
1086
1087 if (nb_tbs <= 0)
1088 return NULL;
1089 if (tc_ptr < (unsigned long)code_gen_buffer ||
1090 tc_ptr >= (unsigned long)code_gen_ptr)
1091 return NULL;
1092 /* binary search (cf Knuth) */
1093 m_min = 0;
1094 m_max = nb_tbs - 1;
1095 while (m_min <= m_max) {
1096 m = (m_min + m_max) >> 1;
1097 tb = &tbs[m];
1098 v = (unsigned long)tb->tc_ptr;
1099 if (v == tc_ptr)
1100 return tb;
1101 else if (tc_ptr < v) {
1102 m_max = m - 1;
1103 } else {
1104 m_min = m + 1;
1105 }
ths5fafdf22007-09-16 21:08:06 +00001106 }
bellarda513fe12003-05-27 23:29:48 +00001107 return &tbs[m_max];
1108}
bellard75012672003-06-21 13:11:07 +00001109
bellardea041c02003-06-25 16:16:50 +00001110static void tb_reset_jump_recursive(TranslationBlock *tb);
1111
1112static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1113{
1114 TranslationBlock *tb1, *tb_next, **ptb;
1115 unsigned int n1;
1116
1117 tb1 = tb->jmp_next[n];
1118 if (tb1 != NULL) {
1119 /* find head of list */
1120 for(;;) {
1121 n1 = (long)tb1 & 3;
1122 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1123 if (n1 == 2)
1124 break;
1125 tb1 = tb1->jmp_next[n1];
1126 }
1127 /* we are now sure now that tb jumps to tb1 */
1128 tb_next = tb1;
1129
1130 /* remove tb from the jmp_first list */
1131 ptb = &tb_next->jmp_first;
1132 for(;;) {
1133 tb1 = *ptb;
1134 n1 = (long)tb1 & 3;
1135 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1136 if (n1 == n && tb1 == tb)
1137 break;
1138 ptb = &tb1->jmp_next[n1];
1139 }
1140 *ptb = tb->jmp_next[n];
1141 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001142
bellardea041c02003-06-25 16:16:50 +00001143 /* suppress the jump to next tb in generated code */
1144 tb_reset_jump(tb, n);
1145
bellard01243112004-01-04 15:48:17 +00001146 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001147 tb_reset_jump_recursive(tb_next);
1148 }
1149}
1150
1151static void tb_reset_jump_recursive(TranslationBlock *tb)
1152{
1153 tb_reset_jump_recursive2(tb, 0);
1154 tb_reset_jump_recursive2(tb, 1);
1155}
1156
bellard1fddef42005-04-17 19:16:13 +00001157#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001158static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1159{
j_mayer9b3c35e2007-04-07 11:21:28 +00001160 target_phys_addr_t addr;
1161 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001162 ram_addr_t ram_addr;
1163 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001164
pbrookc2f07f82006-04-08 17:14:56 +00001165 addr = cpu_get_phys_page_debug(env, pc);
1166 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1167 if (!p) {
1168 pd = IO_MEM_UNASSIGNED;
1169 } else {
1170 pd = p->phys_offset;
1171 }
1172 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001173 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001174}
bellardc27004e2005-01-03 23:35:10 +00001175#endif
bellardd720b932004-04-25 17:57:43 +00001176
pbrook6658ffb2007-03-16 23:58:11 +00001177/* Add a watchpoint. */
1178int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1179{
1180 int i;
1181
1182 for (i = 0; i < env->nb_watchpoints; i++) {
1183 if (addr == env->watchpoint[i].vaddr)
1184 return 0;
1185 }
1186 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1187 return -1;
1188
1189 i = env->nb_watchpoints++;
1190 env->watchpoint[i].vaddr = addr;
1191 tlb_flush_page(env, addr);
1192 /* FIXME: This flush is needed because of the hack to make memory ops
1193 terminate the TB. It can be removed once the proper IO trap and
1194 re-execute bits are in. */
1195 tb_flush(env);
1196 return i;
1197}
1198
1199/* Remove a watchpoint. */
1200int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1201{
1202 int i;
1203
1204 for (i = 0; i < env->nb_watchpoints; i++) {
1205 if (addr == env->watchpoint[i].vaddr) {
1206 env->nb_watchpoints--;
1207 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1208 tlb_flush_page(env, addr);
1209 return 0;
1210 }
1211 }
1212 return -1;
1213}
1214
edgar_igl7d03f822008-05-17 18:58:29 +00001215/* Remove all watchpoints. */
1216void cpu_watchpoint_remove_all(CPUState *env) {
1217 int i;
1218
1219 for (i = 0; i < env->nb_watchpoints; i++) {
1220 tlb_flush_page(env, env->watchpoint[i].vaddr);
1221 }
1222 env->nb_watchpoints = 0;
1223}
1224
bellardc33a3462003-07-29 20:50:33 +00001225/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1226 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001227int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001228{
bellard1fddef42005-04-17 19:16:13 +00001229#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001230 int i;
ths3b46e622007-09-17 08:09:54 +00001231
bellard4c3a88a2003-07-26 12:06:08 +00001232 for(i = 0; i < env->nb_breakpoints; i++) {
1233 if (env->breakpoints[i] == pc)
1234 return 0;
1235 }
1236
1237 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1238 return -1;
1239 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001240
bellardd720b932004-04-25 17:57:43 +00001241 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001242 return 0;
1243#else
1244 return -1;
1245#endif
1246}
1247
edgar_igl7d03f822008-05-17 18:58:29 +00001248/* remove all breakpoints */
1249void cpu_breakpoint_remove_all(CPUState *env) {
1250#if defined(TARGET_HAS_ICE)
1251 int i;
1252 for(i = 0; i < env->nb_breakpoints; i++) {
1253 breakpoint_invalidate(env, env->breakpoints[i]);
1254 }
1255 env->nb_breakpoints = 0;
1256#endif
1257}
1258
bellard4c3a88a2003-07-26 12:06:08 +00001259/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001260int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001261{
bellard1fddef42005-04-17 19:16:13 +00001262#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001263 int i;
1264 for(i = 0; i < env->nb_breakpoints; i++) {
1265 if (env->breakpoints[i] == pc)
1266 goto found;
1267 }
1268 return -1;
1269 found:
bellard4c3a88a2003-07-26 12:06:08 +00001270 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001271 if (i < env->nb_breakpoints)
1272 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001273
1274 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001275 return 0;
1276#else
1277 return -1;
1278#endif
1279}
1280
bellardc33a3462003-07-29 20:50:33 +00001281/* enable or disable single step mode. EXCP_DEBUG is returned by the
1282 CPU loop after each instruction */
1283void cpu_single_step(CPUState *env, int enabled)
1284{
bellard1fddef42005-04-17 19:16:13 +00001285#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001286 if (env->singlestep_enabled != enabled) {
1287 env->singlestep_enabled = enabled;
1288 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001289 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001290 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001291 }
1292#endif
1293}
1294
bellard34865132003-10-05 14:28:56 +00001295/* enable or disable low levels log */
1296void cpu_set_log(int log_flags)
1297{
1298 loglevel = log_flags;
1299 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001300 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001301 if (!logfile) {
1302 perror(logfilename);
1303 _exit(1);
1304 }
bellard9fa3e852004-01-04 18:06:42 +00001305#if !defined(CONFIG_SOFTMMU)
1306 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1307 {
1308 static uint8_t logfile_buf[4096];
1309 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1310 }
1311#else
bellard34865132003-10-05 14:28:56 +00001312 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001313#endif
pbrooke735b912007-06-30 13:53:24 +00001314 log_append = 1;
1315 }
1316 if (!loglevel && logfile) {
1317 fclose(logfile);
1318 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001319 }
1320}
1321
1322void cpu_set_log_filename(const char *filename)
1323{
1324 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001325 if (logfile) {
1326 fclose(logfile);
1327 logfile = NULL;
1328 }
1329 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001330}
bellardc33a3462003-07-29 20:50:33 +00001331
bellard01243112004-01-04 15:48:17 +00001332/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001333void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001334{
1335 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001336 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
bellard59817cc2004-02-16 22:01:13 +00001337
bellard68a79312003-06-30 13:12:32 +00001338 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001339 /* if the cpu is currently executing code, we must unlink it and
1340 all the potentially executing TB */
1341 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001342 if (tb && !testandset(&interrupt_lock)) {
1343 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001344 tb_reset_jump_recursive(tb);
aurel3215a51152008-03-28 22:29:15 +00001345 resetlock(&interrupt_lock);
bellardea041c02003-06-25 16:16:50 +00001346 }
1347}
1348
bellardb54ad042004-05-20 13:42:52 +00001349void cpu_reset_interrupt(CPUState *env, int mask)
1350{
1351 env->interrupt_request &= ~mask;
1352}
1353
bellardf193c792004-03-21 17:06:25 +00001354CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001355 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001356 "show generated host assembly code for each compiled TB" },
1357 { CPU_LOG_TB_IN_ASM, "in_asm",
1358 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001359 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001360 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001361 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001362 "show micro ops "
1363#ifdef TARGET_I386
1364 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001365#endif
blueswir1e01a1152008-03-14 17:37:11 +00001366 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001367 { CPU_LOG_INT, "int",
1368 "show interrupts/exceptions in short format" },
1369 { CPU_LOG_EXEC, "exec",
1370 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001371 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001372 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001373#ifdef TARGET_I386
1374 { CPU_LOG_PCALL, "pcall",
1375 "show protected mode far calls/returns/exceptions" },
1376#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001377#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001378 { CPU_LOG_IOPORT, "ioport",
1379 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001380#endif
bellardf193c792004-03-21 17:06:25 +00001381 { 0, NULL, NULL },
1382};
1383
1384static int cmp1(const char *s1, int n, const char *s2)
1385{
1386 if (strlen(s2) != n)
1387 return 0;
1388 return memcmp(s1, s2, n) == 0;
1389}
ths3b46e622007-09-17 08:09:54 +00001390
bellardf193c792004-03-21 17:06:25 +00001391/* takes a comma separated list of log masks. Return 0 if error. */
1392int cpu_str_to_log_mask(const char *str)
1393{
1394 CPULogItem *item;
1395 int mask;
1396 const char *p, *p1;
1397
1398 p = str;
1399 mask = 0;
1400 for(;;) {
1401 p1 = strchr(p, ',');
1402 if (!p1)
1403 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001404 if(cmp1(p,p1-p,"all")) {
1405 for(item = cpu_log_items; item->mask != 0; item++) {
1406 mask |= item->mask;
1407 }
1408 } else {
bellardf193c792004-03-21 17:06:25 +00001409 for(item = cpu_log_items; item->mask != 0; item++) {
1410 if (cmp1(p, p1 - p, item->name))
1411 goto found;
1412 }
1413 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001414 }
bellardf193c792004-03-21 17:06:25 +00001415 found:
1416 mask |= item->mask;
1417 if (*p1 != ',')
1418 break;
1419 p = p1 + 1;
1420 }
1421 return mask;
1422}
bellardea041c02003-06-25 16:16:50 +00001423
bellard75012672003-06-21 13:11:07 +00001424void cpu_abort(CPUState *env, const char *fmt, ...)
1425{
1426 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001427 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001428
1429 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001430 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001431 fprintf(stderr, "qemu: fatal: ");
1432 vfprintf(stderr, fmt, ap);
1433 fprintf(stderr, "\n");
1434#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001435 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1436#else
1437 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001438#endif
balrog924edca2007-06-10 14:07:13 +00001439 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001440 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001441 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001442 fprintf(logfile, "\n");
1443#ifdef TARGET_I386
1444 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1445#else
1446 cpu_dump_state(env, logfile, fprintf, 0);
1447#endif
balrog924edca2007-06-10 14:07:13 +00001448 fflush(logfile);
1449 fclose(logfile);
1450 }
pbrook493ae1f2007-11-23 16:53:59 +00001451 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001452 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001453 abort();
1454}
1455
thsc5be9f02007-02-28 20:20:53 +00001456CPUState *cpu_copy(CPUState *env)
1457{
ths01ba9812007-12-09 02:22:57 +00001458 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001459 /* preserve chaining and index */
1460 CPUState *next_cpu = new_env->next_cpu;
1461 int cpu_index = new_env->cpu_index;
1462 memcpy(new_env, env, sizeof(CPUState));
1463 new_env->next_cpu = next_cpu;
1464 new_env->cpu_index = cpu_index;
1465 return new_env;
1466}
1467
bellard01243112004-01-04 15:48:17 +00001468#if !defined(CONFIG_USER_ONLY)
1469
edgar_igl5c751e92008-05-06 08:44:21 +00001470static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1471{
1472 unsigned int i;
1473
1474 /* Discard jump cache entries for any tb which might potentially
1475 overlap the flushed page. */
1476 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1477 memset (&env->tb_jmp_cache[i], 0,
1478 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1479
1480 i = tb_jmp_cache_hash_page(addr);
1481 memset (&env->tb_jmp_cache[i], 0,
1482 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1483}
1484
bellardee8b7022004-02-03 23:35:10 +00001485/* NOTE: if flush_global is true, also flush global entries (not
1486 implemented yet) */
1487void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001488{
bellard33417e72003-08-10 21:47:01 +00001489 int i;
bellard01243112004-01-04 15:48:17 +00001490
bellard9fa3e852004-01-04 18:06:42 +00001491#if defined(DEBUG_TLB)
1492 printf("tlb_flush:\n");
1493#endif
bellard01243112004-01-04 15:48:17 +00001494 /* must reset current TB so that interrupts cannot modify the
1495 links while we are modifying them */
1496 env->current_tb = NULL;
1497
bellard33417e72003-08-10 21:47:01 +00001498 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001499 env->tlb_table[0][i].addr_read = -1;
1500 env->tlb_table[0][i].addr_write = -1;
1501 env->tlb_table[0][i].addr_code = -1;
1502 env->tlb_table[1][i].addr_read = -1;
1503 env->tlb_table[1][i].addr_write = -1;
1504 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001505#if (NB_MMU_MODES >= 3)
1506 env->tlb_table[2][i].addr_read = -1;
1507 env->tlb_table[2][i].addr_write = -1;
1508 env->tlb_table[2][i].addr_code = -1;
1509#if (NB_MMU_MODES == 4)
1510 env->tlb_table[3][i].addr_read = -1;
1511 env->tlb_table[3][i].addr_write = -1;
1512 env->tlb_table[3][i].addr_code = -1;
1513#endif
1514#endif
bellard33417e72003-08-10 21:47:01 +00001515 }
bellard9fa3e852004-01-04 18:06:42 +00001516
bellard8a40a182005-11-20 10:35:40 +00001517 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001518
1519#if !defined(CONFIG_SOFTMMU)
1520 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1521#endif
bellard0a962c02005-02-10 22:00:27 +00001522#ifdef USE_KQEMU
1523 if (env->kqemu_enabled) {
1524 kqemu_flush(env, flush_global);
1525 }
1526#endif
bellarde3db7222005-01-26 22:00:47 +00001527 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001528}
1529
bellard274da6b2004-05-20 21:56:27 +00001530static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001531{
ths5fafdf22007-09-16 21:08:06 +00001532 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001533 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001534 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001535 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001536 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001537 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1538 tlb_entry->addr_read = -1;
1539 tlb_entry->addr_write = -1;
1540 tlb_entry->addr_code = -1;
1541 }
bellard61382a52003-10-27 21:22:23 +00001542}
1543
bellard2e126692004-04-25 21:28:44 +00001544void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001545{
bellard8a40a182005-11-20 10:35:40 +00001546 int i;
bellard01243112004-01-04 15:48:17 +00001547
bellard9fa3e852004-01-04 18:06:42 +00001548#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001549 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001550#endif
bellard01243112004-01-04 15:48:17 +00001551 /* must reset current TB so that interrupts cannot modify the
1552 links while we are modifying them */
1553 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001554
bellard61382a52003-10-27 21:22:23 +00001555 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001556 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001557 tlb_flush_entry(&env->tlb_table[0][i], addr);
1558 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001559#if (NB_MMU_MODES >= 3)
1560 tlb_flush_entry(&env->tlb_table[2][i], addr);
1561#if (NB_MMU_MODES == 4)
1562 tlb_flush_entry(&env->tlb_table[3][i], addr);
1563#endif
1564#endif
bellard01243112004-01-04 15:48:17 +00001565
edgar_igl5c751e92008-05-06 08:44:21 +00001566 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001567
bellard01243112004-01-04 15:48:17 +00001568#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001569 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001570 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001571#endif
bellard0a962c02005-02-10 22:00:27 +00001572#ifdef USE_KQEMU
1573 if (env->kqemu_enabled) {
1574 kqemu_flush_page(env, addr);
1575 }
1576#endif
bellard9fa3e852004-01-04 18:06:42 +00001577}
1578
bellard9fa3e852004-01-04 18:06:42 +00001579/* update the TLBs so that writes to code in the virtual page 'addr'
1580 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001581static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001582{
ths5fafdf22007-09-16 21:08:06 +00001583 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001584 ram_addr + TARGET_PAGE_SIZE,
1585 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001586}
1587
bellard9fa3e852004-01-04 18:06:42 +00001588/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001589 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001590static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001591 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001592{
bellard3a7d9292005-08-21 09:26:42 +00001593 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001594}
1595
ths5fafdf22007-09-16 21:08:06 +00001596static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001597 unsigned long start, unsigned long length)
1598{
1599 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001600 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1601 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001602 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001603 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001604 }
1605 }
1606}
1607
bellard3a7d9292005-08-21 09:26:42 +00001608void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001609 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001610{
1611 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001612 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001613 int i, mask, len;
1614 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001615
1616 start &= TARGET_PAGE_MASK;
1617 end = TARGET_PAGE_ALIGN(end);
1618
1619 length = end - start;
1620 if (length == 0)
1621 return;
bellard0a962c02005-02-10 22:00:27 +00001622 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001623#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001624 /* XXX: should not depend on cpu context */
1625 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001626 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001627 ram_addr_t addr;
1628 addr = start;
1629 for(i = 0; i < len; i++) {
1630 kqemu_set_notdirty(env, addr);
1631 addr += TARGET_PAGE_SIZE;
1632 }
bellard3a7d9292005-08-21 09:26:42 +00001633 }
1634#endif
bellardf23db162005-08-21 19:12:28 +00001635 mask = ~dirty_flags;
1636 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1637 for(i = 0; i < len; i++)
1638 p[i] &= mask;
1639
bellard1ccde1c2004-02-06 19:46:14 +00001640 /* we modify the TLB cache so that the dirty bit will be set again
1641 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001642 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001643 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1644 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001645 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001646 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001647 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001648#if (NB_MMU_MODES >= 3)
1649 for(i = 0; i < CPU_TLB_SIZE; i++)
1650 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1651#if (NB_MMU_MODES == 4)
1652 for(i = 0; i < CPU_TLB_SIZE; i++)
1653 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1654#endif
1655#endif
bellard6a00d602005-11-21 23:25:50 +00001656 }
bellard59817cc2004-02-16 22:01:13 +00001657
1658#if !defined(CONFIG_SOFTMMU)
1659 /* XXX: this is expensive */
1660 {
1661 VirtPageDesc *p;
1662 int j;
1663 target_ulong addr;
1664
1665 for(i = 0; i < L1_SIZE; i++) {
1666 p = l1_virt_map[i];
1667 if (p) {
1668 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1669 for(j = 0; j < L2_SIZE; j++) {
1670 if (p->valid_tag == virt_valid_tag &&
1671 p->phys_addr >= start && p->phys_addr < end &&
1672 (p->prot & PROT_WRITE)) {
1673 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001674 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001675 p->prot & ~PROT_WRITE);
1676 }
1677 }
1678 addr += TARGET_PAGE_SIZE;
1679 p++;
1680 }
1681 }
1682 }
1683 }
1684#endif
bellard1ccde1c2004-02-06 19:46:14 +00001685}
1686
bellard3a7d9292005-08-21 09:26:42 +00001687static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1688{
1689 ram_addr_t ram_addr;
1690
bellard84b7b8e2005-11-28 21:19:04 +00001691 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001692 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001693 tlb_entry->addend - (unsigned long)phys_ram_base;
1694 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001695 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001696 }
1697 }
1698}
1699
1700/* update the TLB according to the current state of the dirty bits */
1701void cpu_tlb_update_dirty(CPUState *env)
1702{
1703 int i;
1704 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001705 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001706 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001707 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001708#if (NB_MMU_MODES >= 3)
1709 for(i = 0; i < CPU_TLB_SIZE; i++)
1710 tlb_update_dirty(&env->tlb_table[2][i]);
1711#if (NB_MMU_MODES == 4)
1712 for(i = 0; i < CPU_TLB_SIZE; i++)
1713 tlb_update_dirty(&env->tlb_table[3][i]);
1714#endif
1715#endif
bellard3a7d9292005-08-21 09:26:42 +00001716}
1717
ths5fafdf22007-09-16 21:08:06 +00001718static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001719 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001720{
1721 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001722 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1723 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001724 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001725 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001726 }
1727 }
1728}
1729
1730/* update the TLB corresponding to virtual page vaddr and phys addr
1731 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001732static inline void tlb_set_dirty(CPUState *env,
1733 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001734{
bellard1ccde1c2004-02-06 19:46:14 +00001735 int i;
1736
bellard1ccde1c2004-02-06 19:46:14 +00001737 addr &= TARGET_PAGE_MASK;
1738 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001739 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1740 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001741#if (NB_MMU_MODES >= 3)
1742 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1743#if (NB_MMU_MODES == 4)
1744 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1745#endif
1746#endif
bellard9fa3e852004-01-04 18:06:42 +00001747}
1748
bellard59817cc2004-02-16 22:01:13 +00001749/* add a new TLB entry. At most one entry for a given virtual address
1750 is permitted. Return 0 if OK or 2 if the page could not be mapped
1751 (can only happen in non SOFTMMU mode for I/O pages or pages
1752 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001753int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1754 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001755 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001756{
bellard92e873b2004-05-21 14:52:29 +00001757 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001758 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001759 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001760 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001761 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001762 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001763 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001764 int i;
bellard9fa3e852004-01-04 18:06:42 +00001765
bellard92e873b2004-05-21 14:52:29 +00001766 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001767 if (!p) {
1768 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001769 } else {
1770 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001771 }
1772#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001773 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1774 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001775#endif
1776
1777 ret = 0;
1778#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001779 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001780#endif
1781 {
bellard2a4188a2006-06-25 21:54:59 +00001782 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001783 /* IO memory case */
1784 address = vaddr | pd;
1785 addend = paddr;
1786 } else {
1787 /* standard memory */
1788 address = vaddr;
1789 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1790 }
pbrook6658ffb2007-03-16 23:58:11 +00001791
1792 /* Make accesses to pages with watchpoints go via the
1793 watchpoint trap routines. */
1794 for (i = 0; i < env->nb_watchpoints; i++) {
1795 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1796 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001797 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001798 address = vaddr | io_mem_watch;
1799 } else {
balrogd79acba2007-06-26 20:01:13 +00001800 env->watchpoint[i].addend = pd - paddr +
1801 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001802 /* TODO: Figure out how to make read watchpoints coexist
1803 with code. */
1804 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1805 }
1806 }
1807 }
balrogd79acba2007-06-26 20:01:13 +00001808
bellard90f18422005-07-24 10:17:31 +00001809 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001810 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001811 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001812 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001813 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001814 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001815 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001816 te->addr_read = -1;
1817 }
edgar_igl5c751e92008-05-06 08:44:21 +00001818
bellard84b7b8e2005-11-28 21:19:04 +00001819 if (prot & PAGE_EXEC) {
1820 te->addr_code = address;
1821 } else {
1822 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001823 }
bellard67b915a2004-03-31 23:37:16 +00001824 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001825 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001826 (pd & IO_MEM_ROMD)) {
1827 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001828 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001829 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001830 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001831 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001832 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001833 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001834 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001835 }
1836 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001837 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001838 }
1839 }
1840#if !defined(CONFIG_SOFTMMU)
1841 else {
1842 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1843 /* IO access: no mapping is done as it will be handled by the
1844 soft MMU */
1845 if (!(env->hflags & HF_SOFTMMU_MASK))
1846 ret = 2;
1847 } else {
1848 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001849
bellard59817cc2004-02-16 22:01:13 +00001850 if (vaddr >= MMAP_AREA_END) {
1851 ret = 2;
1852 } else {
1853 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001854 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001855#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001856 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001857#endif
ths5fafdf22007-09-16 21:08:06 +00001858 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001859 !cpu_physical_memory_is_dirty(pd))) {
1860 /* ROM: we do as if code was inside */
1861 /* if code is present, we only map as read only and save the
1862 original mapping */
1863 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001864
bellard90f18422005-07-24 10:17:31 +00001865 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001866 vp->phys_addr = pd;
1867 vp->prot = prot;
1868 vp->valid_tag = virt_valid_tag;
1869 prot &= ~PAGE_WRITE;
1870 }
bellard9fa3e852004-01-04 18:06:42 +00001871 }
ths5fafdf22007-09-16 21:08:06 +00001872 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001873 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1874 if (map_addr == MAP_FAILED) {
1875 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1876 paddr, vaddr);
1877 }
bellard9fa3e852004-01-04 18:06:42 +00001878 }
1879 }
1880 }
1881#endif
1882 return ret;
1883}
1884
1885/* called from signal handler: invalidate the code and unprotect the
1886 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001887int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001888{
1889#if !defined(CONFIG_SOFTMMU)
1890 VirtPageDesc *vp;
1891
1892#if defined(DEBUG_TLB)
1893 printf("page_unprotect: addr=0x%08x\n", addr);
1894#endif
1895 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001896
1897 /* if it is not mapped, no need to worry here */
1898 if (addr >= MMAP_AREA_END)
1899 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001900 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1901 if (!vp)
1902 return 0;
1903 /* NOTE: in this case, validate_tag is _not_ tested as it
1904 validates only the code TLB */
1905 if (vp->valid_tag != virt_valid_tag)
1906 return 0;
1907 if (!(vp->prot & PAGE_WRITE))
1908 return 0;
1909#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001910 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001911 addr, vp->phys_addr, vp->prot);
1912#endif
bellard59817cc2004-02-16 22:01:13 +00001913 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1914 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1915 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001916 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001917 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001918 /* flush the code inside */
1919 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001920 return 1;
1921#else
1922 return 0;
1923#endif
bellard33417e72003-08-10 21:47:01 +00001924}
1925
bellard01243112004-01-04 15:48:17 +00001926#else
1927
bellardee8b7022004-02-03 23:35:10 +00001928void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001929{
1930}
1931
bellard2e126692004-04-25 21:28:44 +00001932void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001933{
1934}
1935
ths5fafdf22007-09-16 21:08:06 +00001936int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1937 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001938 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001939{
bellard9fa3e852004-01-04 18:06:42 +00001940 return 0;
1941}
bellard33417e72003-08-10 21:47:01 +00001942
bellard9fa3e852004-01-04 18:06:42 +00001943/* dump memory mappings */
1944void page_dump(FILE *f)
1945{
1946 unsigned long start, end;
1947 int i, j, prot, prot1;
1948 PageDesc *p;
1949
1950 fprintf(f, "%-8s %-8s %-8s %s\n",
1951 "start", "end", "size", "prot");
1952 start = -1;
1953 end = -1;
1954 prot = 0;
1955 for(i = 0; i <= L1_SIZE; i++) {
1956 if (i < L1_SIZE)
1957 p = l1_map[i];
1958 else
1959 p = NULL;
1960 for(j = 0;j < L2_SIZE; j++) {
1961 if (!p)
1962 prot1 = 0;
1963 else
1964 prot1 = p[j].flags;
1965 if (prot1 != prot) {
1966 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1967 if (start != -1) {
1968 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001969 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001970 prot & PAGE_READ ? 'r' : '-',
1971 prot & PAGE_WRITE ? 'w' : '-',
1972 prot & PAGE_EXEC ? 'x' : '-');
1973 }
1974 if (prot1 != 0)
1975 start = end;
1976 else
1977 start = -1;
1978 prot = prot1;
1979 }
1980 if (!p)
1981 break;
1982 }
bellard33417e72003-08-10 21:47:01 +00001983 }
bellard33417e72003-08-10 21:47:01 +00001984}
1985
pbrook53a59602006-03-25 19:31:22 +00001986int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001987{
bellard9fa3e852004-01-04 18:06:42 +00001988 PageDesc *p;
1989
1990 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001991 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001992 return 0;
1993 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001994}
1995
bellard9fa3e852004-01-04 18:06:42 +00001996/* modify the flags of a page and invalidate the code if
1997 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1998 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001999void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002000{
2001 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002002 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00002003
2004 start = start & TARGET_PAGE_MASK;
2005 end = TARGET_PAGE_ALIGN(end);
2006 if (flags & PAGE_WRITE)
2007 flags |= PAGE_WRITE_ORG;
2008 spin_lock(&tb_lock);
2009 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2010 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
2011 /* if the write protection is set, then we invalidate the code
2012 inside */
ths5fafdf22007-09-16 21:08:06 +00002013 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002014 (flags & PAGE_WRITE) &&
2015 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002016 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002017 }
2018 p->flags = flags;
2019 }
2020 spin_unlock(&tb_lock);
2021}
2022
ths3d97b402007-11-02 19:02:07 +00002023int page_check_range(target_ulong start, target_ulong len, int flags)
2024{
2025 PageDesc *p;
2026 target_ulong end;
2027 target_ulong addr;
2028
2029 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2030 start = start & TARGET_PAGE_MASK;
2031
2032 if( end < start )
2033 /* we've wrapped around */
2034 return -1;
2035 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2036 p = page_find(addr >> TARGET_PAGE_BITS);
2037 if( !p )
2038 return -1;
2039 if( !(p->flags & PAGE_VALID) )
2040 return -1;
2041
bellarddae32702007-11-14 10:51:00 +00002042 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002043 return -1;
bellarddae32702007-11-14 10:51:00 +00002044 if (flags & PAGE_WRITE) {
2045 if (!(p->flags & PAGE_WRITE_ORG))
2046 return -1;
2047 /* unprotect the page if it was put read-only because it
2048 contains translated code */
2049 if (!(p->flags & PAGE_WRITE)) {
2050 if (!page_unprotect(addr, 0, NULL))
2051 return -1;
2052 }
2053 return 0;
2054 }
ths3d97b402007-11-02 19:02:07 +00002055 }
2056 return 0;
2057}
2058
bellard9fa3e852004-01-04 18:06:42 +00002059/* called from signal handler: invalidate the code and unprotect the
2060 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002061int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002062{
2063 unsigned int page_index, prot, pindex;
2064 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002065 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002066
bellard83fb7ad2004-07-05 21:25:26 +00002067 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002068 page_index = host_start >> TARGET_PAGE_BITS;
2069 p1 = page_find(page_index);
2070 if (!p1)
2071 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00002072 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002073 p = p1;
2074 prot = 0;
2075 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2076 prot |= p->flags;
2077 p++;
2078 }
2079 /* if the page was really writable, then we change its
2080 protection back to writable */
2081 if (prot & PAGE_WRITE_ORG) {
2082 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2083 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002084 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002085 (prot & PAGE_BITS) | PAGE_WRITE);
2086 p1[pindex].flags |= PAGE_WRITE;
2087 /* and since the content will be modified, we must invalidate
2088 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002089 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002090#ifdef DEBUG_TB_CHECK
2091 tb_invalidate_check(address);
2092#endif
2093 return 1;
2094 }
2095 }
2096 return 0;
2097}
2098
bellard6a00d602005-11-21 23:25:50 +00002099static inline void tlb_set_dirty(CPUState *env,
2100 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002101{
2102}
bellard9fa3e852004-01-04 18:06:42 +00002103#endif /* defined(CONFIG_USER_ONLY) */
2104
blueswir1db7b5422007-05-26 17:36:03 +00002105static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002106 ram_addr_t memory);
2107static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2108 ram_addr_t orig_memory);
blueswir1db7b5422007-05-26 17:36:03 +00002109#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2110 need_subpage) \
2111 do { \
2112 if (addr > start_addr) \
2113 start_addr2 = 0; \
2114 else { \
2115 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2116 if (start_addr2 > 0) \
2117 need_subpage = 1; \
2118 } \
2119 \
blueswir149e9fba2007-05-30 17:25:06 +00002120 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002121 end_addr2 = TARGET_PAGE_SIZE - 1; \
2122 else { \
2123 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2124 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2125 need_subpage = 1; \
2126 } \
2127 } while (0)
2128
bellard33417e72003-08-10 21:47:01 +00002129/* register physical memory. 'size' must be a multiple of the target
2130 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2131 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002132void cpu_register_physical_memory(target_phys_addr_t start_addr,
aurel3200f82b82008-04-27 21:12:55 +00002133 ram_addr_t size,
2134 ram_addr_t phys_offset)
bellard33417e72003-08-10 21:47:01 +00002135{
bellard108c49b2005-07-24 12:55:09 +00002136 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002137 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002138 CPUState *env;
aurel3200f82b82008-04-27 21:12:55 +00002139 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002140 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002141
bellard5fd386f2004-05-23 21:11:22 +00002142 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002143 end_addr = start_addr + (target_phys_addr_t)size;
2144 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002145 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2146 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
aurel3200f82b82008-04-27 21:12:55 +00002147 ram_addr_t orig_memory = p->phys_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002148 target_phys_addr_t start_addr2, end_addr2;
2149 int need_subpage = 0;
2150
2151 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2152 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002153 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002154 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2155 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2156 &p->phys_offset, orig_memory);
2157 } else {
2158 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2159 >> IO_MEM_SHIFT];
2160 }
2161 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2162 } else {
2163 p->phys_offset = phys_offset;
2164 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2165 (phys_offset & IO_MEM_ROMD))
2166 phys_offset += TARGET_PAGE_SIZE;
2167 }
2168 } else {
2169 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2170 p->phys_offset = phys_offset;
2171 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2172 (phys_offset & IO_MEM_ROMD))
2173 phys_offset += TARGET_PAGE_SIZE;
2174 else {
2175 target_phys_addr_t start_addr2, end_addr2;
2176 int need_subpage = 0;
2177
2178 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2179 end_addr2, need_subpage);
2180
blueswir14254fab2008-01-01 16:57:19 +00002181 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002182 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2183 &p->phys_offset, IO_MEM_UNASSIGNED);
2184 subpage_register(subpage, start_addr2, end_addr2,
2185 phys_offset);
2186 }
2187 }
2188 }
bellard33417e72003-08-10 21:47:01 +00002189 }
ths3b46e622007-09-17 08:09:54 +00002190
bellard9d420372006-06-25 22:25:22 +00002191 /* since each CPU stores ram addresses in its TLB cache, we must
2192 reset the modified entries */
2193 /* XXX: slow ! */
2194 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2195 tlb_flush(env, 1);
2196 }
bellard33417e72003-08-10 21:47:01 +00002197}
2198
bellardba863452006-09-24 18:41:10 +00002199/* XXX: temporary until new memory mapping API */
aurel3200f82b82008-04-27 21:12:55 +00002200ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002201{
2202 PhysPageDesc *p;
2203
2204 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2205 if (!p)
2206 return IO_MEM_UNASSIGNED;
2207 return p->phys_offset;
2208}
2209
bellarde9a1ab12007-02-08 23:08:38 +00002210/* XXX: better than nothing */
aurel3200f82b82008-04-27 21:12:55 +00002211ram_addr_t qemu_ram_alloc(ram_addr_t size)
bellarde9a1ab12007-02-08 23:08:38 +00002212{
2213 ram_addr_t addr;
balrog7fb4fdc2008-04-24 17:59:27 +00002214 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
bellarded441462008-05-23 11:56:45 +00002215 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 "\n",
2216 (uint64_t)size, (uint64_t)phys_ram_size);
bellarde9a1ab12007-02-08 23:08:38 +00002217 abort();
2218 }
2219 addr = phys_ram_alloc_offset;
2220 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2221 return addr;
2222}
2223
2224void qemu_ram_free(ram_addr_t addr)
2225{
2226}
2227
bellarda4193c82004-06-03 14:01:43 +00002228static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002229{
pbrook67d3b952006-12-18 05:03:52 +00002230#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002231 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002232#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002233#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002234 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002235#elif TARGET_CRIS
2236 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002237#endif
bellard33417e72003-08-10 21:47:01 +00002238 return 0;
2239}
2240
bellarda4193c82004-06-03 14:01:43 +00002241static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002242{
pbrook67d3b952006-12-18 05:03:52 +00002243#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002244 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002245#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002246#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002247 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002248#elif TARGET_CRIS
2249 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002250#endif
bellard33417e72003-08-10 21:47:01 +00002251}
2252
2253static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2254 unassigned_mem_readb,
2255 unassigned_mem_readb,
2256 unassigned_mem_readb,
2257};
2258
2259static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2260 unassigned_mem_writeb,
2261 unassigned_mem_writeb,
2262 unassigned_mem_writeb,
2263};
2264
bellarda4193c82004-06-03 14:01:43 +00002265static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002266{
bellard3a7d9292005-08-21 09:26:42 +00002267 unsigned long ram_addr;
2268 int dirty_flags;
2269 ram_addr = addr - (unsigned long)phys_ram_base;
2270 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2271 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2272#if !defined(CONFIG_USER_ONLY)
2273 tb_invalidate_phys_page_fast(ram_addr, 1);
2274 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2275#endif
2276 }
bellardc27004e2005-01-03 23:35:10 +00002277 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002278#ifdef USE_KQEMU
2279 if (cpu_single_env->kqemu_enabled &&
2280 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2281 kqemu_modify_page(cpu_single_env, ram_addr);
2282#endif
bellardf23db162005-08-21 19:12:28 +00002283 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2284 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2285 /* we remove the notdirty callback only if the code has been
2286 flushed */
2287 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002288 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002289}
2290
bellarda4193c82004-06-03 14:01:43 +00002291static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002292{
bellard3a7d9292005-08-21 09:26:42 +00002293 unsigned long ram_addr;
2294 int dirty_flags;
2295 ram_addr = addr - (unsigned long)phys_ram_base;
2296 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2297 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2298#if !defined(CONFIG_USER_ONLY)
2299 tb_invalidate_phys_page_fast(ram_addr, 2);
2300 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2301#endif
2302 }
bellardc27004e2005-01-03 23:35:10 +00002303 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002304#ifdef USE_KQEMU
2305 if (cpu_single_env->kqemu_enabled &&
2306 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2307 kqemu_modify_page(cpu_single_env, ram_addr);
2308#endif
bellardf23db162005-08-21 19:12:28 +00002309 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2310 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2311 /* we remove the notdirty callback only if the code has been
2312 flushed */
2313 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002314 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002315}
2316
bellarda4193c82004-06-03 14:01:43 +00002317static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002318{
bellard3a7d9292005-08-21 09:26:42 +00002319 unsigned long ram_addr;
2320 int dirty_flags;
2321 ram_addr = addr - (unsigned long)phys_ram_base;
2322 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2323 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2324#if !defined(CONFIG_USER_ONLY)
2325 tb_invalidate_phys_page_fast(ram_addr, 4);
2326 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2327#endif
2328 }
bellardc27004e2005-01-03 23:35:10 +00002329 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002330#ifdef USE_KQEMU
2331 if (cpu_single_env->kqemu_enabled &&
2332 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2333 kqemu_modify_page(cpu_single_env, ram_addr);
2334#endif
bellardf23db162005-08-21 19:12:28 +00002335 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2336 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2337 /* we remove the notdirty callback only if the code has been
2338 flushed */
2339 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002340 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002341}
2342
bellard3a7d9292005-08-21 09:26:42 +00002343static CPUReadMemoryFunc *error_mem_read[3] = {
2344 NULL, /* never used */
2345 NULL, /* never used */
2346 NULL, /* never used */
2347};
2348
bellard1ccde1c2004-02-06 19:46:14 +00002349static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2350 notdirty_mem_writeb,
2351 notdirty_mem_writew,
2352 notdirty_mem_writel,
2353};
2354
pbrook6658ffb2007-03-16 23:58:11 +00002355#if defined(CONFIG_SOFTMMU)
2356/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2357 so these check for a hit then pass through to the normal out-of-line
2358 phys routines. */
2359static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2360{
2361 return ldub_phys(addr);
2362}
2363
2364static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2365{
2366 return lduw_phys(addr);
2367}
2368
2369static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2370{
2371 return ldl_phys(addr);
2372}
2373
2374/* Generate a debug exception if a watchpoint has been hit.
2375 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002376 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002377static target_ulong check_watchpoint(target_phys_addr_t addr)
2378{
2379 CPUState *env = cpu_single_env;
2380 target_ulong watch;
2381 target_ulong retaddr;
2382 int i;
2383
2384 retaddr = addr;
2385 for (i = 0; i < env->nb_watchpoints; i++) {
2386 watch = env->watchpoint[i].vaddr;
2387 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002388 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002389 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2390 cpu_single_env->watchpoint_hit = i + 1;
2391 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2392 break;
2393 }
2394 }
2395 }
2396 return retaddr;
2397}
2398
2399static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2400 uint32_t val)
2401{
2402 addr = check_watchpoint(addr);
2403 stb_phys(addr, val);
2404}
2405
2406static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2407 uint32_t val)
2408{
2409 addr = check_watchpoint(addr);
2410 stw_phys(addr, val);
2411}
2412
2413static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2414 uint32_t val)
2415{
2416 addr = check_watchpoint(addr);
2417 stl_phys(addr, val);
2418}
2419
2420static CPUReadMemoryFunc *watch_mem_read[3] = {
2421 watch_mem_readb,
2422 watch_mem_readw,
2423 watch_mem_readl,
2424};
2425
2426static CPUWriteMemoryFunc *watch_mem_write[3] = {
2427 watch_mem_writeb,
2428 watch_mem_writew,
2429 watch_mem_writel,
2430};
2431#endif
2432
blueswir1db7b5422007-05-26 17:36:03 +00002433static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2434 unsigned int len)
2435{
blueswir1db7b5422007-05-26 17:36:03 +00002436 uint32_t ret;
2437 unsigned int idx;
2438
2439 idx = SUBPAGE_IDX(addr - mmio->base);
2440#if defined(DEBUG_SUBPAGE)
2441 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2442 mmio, len, addr, idx);
2443#endif
blueswir13ee89922008-01-02 19:45:26 +00002444 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002445
2446 return ret;
2447}
2448
2449static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2450 uint32_t value, unsigned int len)
2451{
blueswir1db7b5422007-05-26 17:36:03 +00002452 unsigned int idx;
2453
2454 idx = SUBPAGE_IDX(addr - mmio->base);
2455#if defined(DEBUG_SUBPAGE)
2456 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2457 mmio, len, addr, idx, value);
2458#endif
blueswir13ee89922008-01-02 19:45:26 +00002459 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002460}
2461
2462static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2463{
2464#if defined(DEBUG_SUBPAGE)
2465 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2466#endif
2467
2468 return subpage_readlen(opaque, addr, 0);
2469}
2470
2471static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2472 uint32_t value)
2473{
2474#if defined(DEBUG_SUBPAGE)
2475 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2476#endif
2477 subpage_writelen(opaque, addr, value, 0);
2478}
2479
2480static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2481{
2482#if defined(DEBUG_SUBPAGE)
2483 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2484#endif
2485
2486 return subpage_readlen(opaque, addr, 1);
2487}
2488
2489static void subpage_writew (void *opaque, target_phys_addr_t addr,
2490 uint32_t value)
2491{
2492#if defined(DEBUG_SUBPAGE)
2493 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2494#endif
2495 subpage_writelen(opaque, addr, value, 1);
2496}
2497
2498static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2499{
2500#if defined(DEBUG_SUBPAGE)
2501 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2502#endif
2503
2504 return subpage_readlen(opaque, addr, 2);
2505}
2506
2507static void subpage_writel (void *opaque,
2508 target_phys_addr_t addr, uint32_t value)
2509{
2510#if defined(DEBUG_SUBPAGE)
2511 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2512#endif
2513 subpage_writelen(opaque, addr, value, 2);
2514}
2515
2516static CPUReadMemoryFunc *subpage_read[] = {
2517 &subpage_readb,
2518 &subpage_readw,
2519 &subpage_readl,
2520};
2521
2522static CPUWriteMemoryFunc *subpage_write[] = {
2523 &subpage_writeb,
2524 &subpage_writew,
2525 &subpage_writel,
2526};
2527
2528static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002529 ram_addr_t memory)
blueswir1db7b5422007-05-26 17:36:03 +00002530{
2531 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002532 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002533
2534 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2535 return -1;
2536 idx = SUBPAGE_IDX(start);
2537 eidx = SUBPAGE_IDX(end);
2538#if defined(DEBUG_SUBPAGE)
2539 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2540 mmio, start, end, idx, eidx, memory);
2541#endif
2542 memory >>= IO_MEM_SHIFT;
2543 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002544 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002545 if (io_mem_read[memory][i]) {
2546 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2547 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2548 }
2549 if (io_mem_write[memory][i]) {
2550 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2551 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2552 }
blueswir14254fab2008-01-01 16:57:19 +00002553 }
blueswir1db7b5422007-05-26 17:36:03 +00002554 }
2555
2556 return 0;
2557}
2558
aurel3200f82b82008-04-27 21:12:55 +00002559static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2560 ram_addr_t orig_memory)
blueswir1db7b5422007-05-26 17:36:03 +00002561{
2562 subpage_t *mmio;
2563 int subpage_memory;
2564
2565 mmio = qemu_mallocz(sizeof(subpage_t));
2566 if (mmio != NULL) {
2567 mmio->base = base;
2568 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2569#if defined(DEBUG_SUBPAGE)
2570 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2571 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2572#endif
2573 *phys = subpage_memory | IO_MEM_SUBPAGE;
2574 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2575 }
2576
2577 return mmio;
2578}
2579
bellard33417e72003-08-10 21:47:01 +00002580static void io_mem_init(void)
2581{
bellard3a7d9292005-08-21 09:26:42 +00002582 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002583 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002584 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002585 io_mem_nb = 5;
2586
pbrook6658ffb2007-03-16 23:58:11 +00002587#if defined(CONFIG_SOFTMMU)
2588 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2589 watch_mem_write, NULL);
2590#endif
bellard1ccde1c2004-02-06 19:46:14 +00002591 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002592 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002593 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002594}
2595
2596/* mem_read and mem_write are arrays of functions containing the
2597 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002598 2). Functions can be omitted with a NULL function pointer. The
2599 registered functions may be modified dynamically later.
2600 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002601 modified. If it is zero, a new io zone is allocated. The return
2602 value can be used with cpu_register_physical_memory(). (-1) is
2603 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002604int cpu_register_io_memory(int io_index,
2605 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002606 CPUWriteMemoryFunc **mem_write,
2607 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002608{
blueswir14254fab2008-01-01 16:57:19 +00002609 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002610
2611 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002612 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002613 return -1;
2614 io_index = io_mem_nb++;
2615 } else {
2616 if (io_index >= IO_MEM_NB_ENTRIES)
2617 return -1;
2618 }
bellardb5ff1b32005-11-26 10:38:39 +00002619
bellard33417e72003-08-10 21:47:01 +00002620 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002621 if (!mem_read[i] || !mem_write[i])
2622 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002623 io_mem_read[io_index][i] = mem_read[i];
2624 io_mem_write[io_index][i] = mem_write[i];
2625 }
bellarda4193c82004-06-03 14:01:43 +00002626 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002627 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002628}
bellard61382a52003-10-27 21:22:23 +00002629
bellard8926b512004-10-10 15:14:20 +00002630CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2631{
2632 return io_mem_write[io_index >> IO_MEM_SHIFT];
2633}
2634
2635CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2636{
2637 return io_mem_read[io_index >> IO_MEM_SHIFT];
2638}
2639
bellard13eb76e2004-01-24 15:23:36 +00002640/* physical memory access (slow version, mainly for debug) */
2641#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002642void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002643 int len, int is_write)
2644{
2645 int l, flags;
2646 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002647 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002648
2649 while (len > 0) {
2650 page = addr & TARGET_PAGE_MASK;
2651 l = (page + TARGET_PAGE_SIZE) - addr;
2652 if (l > len)
2653 l = len;
2654 flags = page_get_flags(page);
2655 if (!(flags & PAGE_VALID))
2656 return;
2657 if (is_write) {
2658 if (!(flags & PAGE_WRITE))
2659 return;
bellard579a97f2007-11-11 14:26:47 +00002660 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002661 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00002662 /* FIXME - should this return an error rather than just fail? */
2663 return;
aurel3272fb7da2008-04-27 23:53:45 +00002664 memcpy(p, buf, l);
2665 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00002666 } else {
2667 if (!(flags & PAGE_READ))
2668 return;
bellard579a97f2007-11-11 14:26:47 +00002669 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002670 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00002671 /* FIXME - should this return an error rather than just fail? */
2672 return;
aurel3272fb7da2008-04-27 23:53:45 +00002673 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00002674 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002675 }
2676 len -= l;
2677 buf += l;
2678 addr += l;
2679 }
2680}
bellard8df1cd02005-01-28 22:37:22 +00002681
bellard13eb76e2004-01-24 15:23:36 +00002682#else
ths5fafdf22007-09-16 21:08:06 +00002683void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002684 int len, int is_write)
2685{
2686 int l, io_index;
2687 uint8_t *ptr;
2688 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002689 target_phys_addr_t page;
2690 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002691 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002692
bellard13eb76e2004-01-24 15:23:36 +00002693 while (len > 0) {
2694 page = addr & TARGET_PAGE_MASK;
2695 l = (page + TARGET_PAGE_SIZE) - addr;
2696 if (l > len)
2697 l = len;
bellard92e873b2004-05-21 14:52:29 +00002698 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002699 if (!p) {
2700 pd = IO_MEM_UNASSIGNED;
2701 } else {
2702 pd = p->phys_offset;
2703 }
ths3b46e622007-09-17 08:09:54 +00002704
bellard13eb76e2004-01-24 15:23:36 +00002705 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002706 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002707 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002708 /* XXX: could force cpu_single_env to NULL to avoid
2709 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002710 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002711 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002712 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002713 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002714 l = 4;
2715 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002716 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002717 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002718 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002719 l = 2;
2720 } else {
bellard1c213d12005-09-03 10:49:04 +00002721 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002722 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002723 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002724 l = 1;
2725 }
2726 } else {
bellardb448f2f2004-02-25 23:24:04 +00002727 unsigned long addr1;
2728 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002729 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002730 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002731 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002732 if (!cpu_physical_memory_is_dirty(addr1)) {
2733 /* invalidate code */
2734 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2735 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002736 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002737 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002738 }
bellard13eb76e2004-01-24 15:23:36 +00002739 }
2740 } else {
ths5fafdf22007-09-16 21:08:06 +00002741 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002742 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002743 /* I/O case */
2744 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2745 if (l >= 4 && ((addr & 3) == 0)) {
2746 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002747 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002748 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002749 l = 4;
2750 } else if (l >= 2 && ((addr & 1) == 0)) {
2751 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002752 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002753 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002754 l = 2;
2755 } else {
bellard1c213d12005-09-03 10:49:04 +00002756 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002757 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002758 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002759 l = 1;
2760 }
2761 } else {
2762 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002763 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002764 (addr & ~TARGET_PAGE_MASK);
2765 memcpy(buf, ptr, l);
2766 }
2767 }
2768 len -= l;
2769 buf += l;
2770 addr += l;
2771 }
2772}
bellard8df1cd02005-01-28 22:37:22 +00002773
bellardd0ecd2a2006-04-23 17:14:48 +00002774/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002775void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002776 const uint8_t *buf, int len)
2777{
2778 int l;
2779 uint8_t *ptr;
2780 target_phys_addr_t page;
2781 unsigned long pd;
2782 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002783
bellardd0ecd2a2006-04-23 17:14:48 +00002784 while (len > 0) {
2785 page = addr & TARGET_PAGE_MASK;
2786 l = (page + TARGET_PAGE_SIZE) - addr;
2787 if (l > len)
2788 l = len;
2789 p = phys_page_find(page >> TARGET_PAGE_BITS);
2790 if (!p) {
2791 pd = IO_MEM_UNASSIGNED;
2792 } else {
2793 pd = p->phys_offset;
2794 }
ths3b46e622007-09-17 08:09:54 +00002795
bellardd0ecd2a2006-04-23 17:14:48 +00002796 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002797 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2798 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002799 /* do nothing */
2800 } else {
2801 unsigned long addr1;
2802 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2803 /* ROM/RAM case */
2804 ptr = phys_ram_base + addr1;
2805 memcpy(ptr, buf, l);
2806 }
2807 len -= l;
2808 buf += l;
2809 addr += l;
2810 }
2811}
2812
2813
bellard8df1cd02005-01-28 22:37:22 +00002814/* warning: addr must be aligned */
2815uint32_t ldl_phys(target_phys_addr_t addr)
2816{
2817 int io_index;
2818 uint8_t *ptr;
2819 uint32_t val;
2820 unsigned long pd;
2821 PhysPageDesc *p;
2822
2823 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2824 if (!p) {
2825 pd = IO_MEM_UNASSIGNED;
2826 } else {
2827 pd = p->phys_offset;
2828 }
ths3b46e622007-09-17 08:09:54 +00002829
ths5fafdf22007-09-16 21:08:06 +00002830 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002831 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002832 /* I/O case */
2833 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2834 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2835 } else {
2836 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002837 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002838 (addr & ~TARGET_PAGE_MASK);
2839 val = ldl_p(ptr);
2840 }
2841 return val;
2842}
2843
bellard84b7b8e2005-11-28 21:19:04 +00002844/* warning: addr must be aligned */
2845uint64_t ldq_phys(target_phys_addr_t addr)
2846{
2847 int io_index;
2848 uint8_t *ptr;
2849 uint64_t val;
2850 unsigned long pd;
2851 PhysPageDesc *p;
2852
2853 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2854 if (!p) {
2855 pd = IO_MEM_UNASSIGNED;
2856 } else {
2857 pd = p->phys_offset;
2858 }
ths3b46e622007-09-17 08:09:54 +00002859
bellard2a4188a2006-06-25 21:54:59 +00002860 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2861 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002862 /* I/O case */
2863 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2864#ifdef TARGET_WORDS_BIGENDIAN
2865 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2866 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2867#else
2868 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2869 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2870#endif
2871 } else {
2872 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002873 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002874 (addr & ~TARGET_PAGE_MASK);
2875 val = ldq_p(ptr);
2876 }
2877 return val;
2878}
2879
bellardaab33092005-10-30 20:48:42 +00002880/* XXX: optimize */
2881uint32_t ldub_phys(target_phys_addr_t addr)
2882{
2883 uint8_t val;
2884 cpu_physical_memory_read(addr, &val, 1);
2885 return val;
2886}
2887
2888/* XXX: optimize */
2889uint32_t lduw_phys(target_phys_addr_t addr)
2890{
2891 uint16_t val;
2892 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2893 return tswap16(val);
2894}
2895
bellard8df1cd02005-01-28 22:37:22 +00002896/* warning: addr must be aligned. The ram page is not masked as dirty
2897 and the code inside is not invalidated. It is useful if the dirty
2898 bits are used to track modified PTEs */
2899void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2900{
2901 int io_index;
2902 uint8_t *ptr;
2903 unsigned long pd;
2904 PhysPageDesc *p;
2905
2906 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2907 if (!p) {
2908 pd = IO_MEM_UNASSIGNED;
2909 } else {
2910 pd = p->phys_offset;
2911 }
ths3b46e622007-09-17 08:09:54 +00002912
bellard3a7d9292005-08-21 09:26:42 +00002913 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002914 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2915 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2916 } else {
ths5fafdf22007-09-16 21:08:06 +00002917 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002918 (addr & ~TARGET_PAGE_MASK);
2919 stl_p(ptr, val);
2920 }
2921}
2922
j_mayerbc98a7e2007-04-04 07:55:12 +00002923void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2924{
2925 int io_index;
2926 uint8_t *ptr;
2927 unsigned long pd;
2928 PhysPageDesc *p;
2929
2930 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2931 if (!p) {
2932 pd = IO_MEM_UNASSIGNED;
2933 } else {
2934 pd = p->phys_offset;
2935 }
ths3b46e622007-09-17 08:09:54 +00002936
j_mayerbc98a7e2007-04-04 07:55:12 +00002937 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2938 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2939#ifdef TARGET_WORDS_BIGENDIAN
2940 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2941 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2942#else
2943 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2944 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2945#endif
2946 } else {
ths5fafdf22007-09-16 21:08:06 +00002947 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002948 (addr & ~TARGET_PAGE_MASK);
2949 stq_p(ptr, val);
2950 }
2951}
2952
bellard8df1cd02005-01-28 22:37:22 +00002953/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002954void stl_phys(target_phys_addr_t addr, uint32_t val)
2955{
2956 int io_index;
2957 uint8_t *ptr;
2958 unsigned long pd;
2959 PhysPageDesc *p;
2960
2961 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2962 if (!p) {
2963 pd = IO_MEM_UNASSIGNED;
2964 } else {
2965 pd = p->phys_offset;
2966 }
ths3b46e622007-09-17 08:09:54 +00002967
bellard3a7d9292005-08-21 09:26:42 +00002968 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002969 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2970 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2971 } else {
2972 unsigned long addr1;
2973 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2974 /* RAM case */
2975 ptr = phys_ram_base + addr1;
2976 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002977 if (!cpu_physical_memory_is_dirty(addr1)) {
2978 /* invalidate code */
2979 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2980 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002981 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2982 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002983 }
bellard8df1cd02005-01-28 22:37:22 +00002984 }
2985}
2986
bellardaab33092005-10-30 20:48:42 +00002987/* XXX: optimize */
2988void stb_phys(target_phys_addr_t addr, uint32_t val)
2989{
2990 uint8_t v = val;
2991 cpu_physical_memory_write(addr, &v, 1);
2992}
2993
2994/* XXX: optimize */
2995void stw_phys(target_phys_addr_t addr, uint32_t val)
2996{
2997 uint16_t v = tswap16(val);
2998 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2999}
3000
3001/* XXX: optimize */
3002void stq_phys(target_phys_addr_t addr, uint64_t val)
3003{
3004 val = tswap64(val);
3005 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3006}
3007
bellard13eb76e2004-01-24 15:23:36 +00003008#endif
3009
3010/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00003011int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00003012 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003013{
3014 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00003015 target_phys_addr_t phys_addr;
3016 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00003017
3018 while (len > 0) {
3019 page = addr & TARGET_PAGE_MASK;
3020 phys_addr = cpu_get_phys_page_debug(env, page);
3021 /* if no physical page mapped, return an error */
3022 if (phys_addr == -1)
3023 return -1;
3024 l = (page + TARGET_PAGE_SIZE) - addr;
3025 if (l > len)
3026 l = len;
ths5fafdf22007-09-16 21:08:06 +00003027 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00003028 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00003029 len -= l;
3030 buf += l;
3031 addr += l;
3032 }
3033 return 0;
3034}
3035
bellarde3db7222005-01-26 22:00:47 +00003036void dump_exec_info(FILE *f,
3037 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3038{
3039 int i, target_code_size, max_target_code_size;
3040 int direct_jmp_count, direct_jmp2_count, cross_page;
3041 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003042
bellarde3db7222005-01-26 22:00:47 +00003043 target_code_size = 0;
3044 max_target_code_size = 0;
3045 cross_page = 0;
3046 direct_jmp_count = 0;
3047 direct_jmp2_count = 0;
3048 for(i = 0; i < nb_tbs; i++) {
3049 tb = &tbs[i];
3050 target_code_size += tb->size;
3051 if (tb->size > max_target_code_size)
3052 max_target_code_size = tb->size;
3053 if (tb->page_addr[1] != -1)
3054 cross_page++;
3055 if (tb->tb_next_offset[0] != 0xffff) {
3056 direct_jmp_count++;
3057 if (tb->tb_next_offset[1] != 0xffff) {
3058 direct_jmp2_count++;
3059 }
3060 }
3061 }
3062 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003063 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003064 cpu_fprintf(f, "gen code size %ld/%ld\n",
3065 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3066 cpu_fprintf(f, "TB count %d/%d\n",
3067 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003068 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003069 nb_tbs ? target_code_size / nb_tbs : 0,
3070 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003071 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003072 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3073 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003074 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3075 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003076 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3077 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003078 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003079 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3080 direct_jmp2_count,
3081 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003082 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003083 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3084 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3085 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003086 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003087}
3088
ths5fafdf22007-09-16 21:08:06 +00003089#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003090
3091#define MMUSUFFIX _cmmu
3092#define GETPC() NULL
3093#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003094#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003095
3096#define SHIFT 0
3097#include "softmmu_template.h"
3098
3099#define SHIFT 1
3100#include "softmmu_template.h"
3101
3102#define SHIFT 2
3103#include "softmmu_template.h"
3104
3105#define SHIFT 3
3106#include "softmmu_template.h"
3107
3108#undef env
3109
3110#endif