blob: 4b6f9bb44362f36c058cf0ca2b3dd8c9bb59f064 [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{
190 unsigned long start, end;
191
192 start = (unsigned long)addr;
193 start &= ~(qemu_real_host_page_size - 1);
194
195 end = (unsigned long)addr + size;
196 end += qemu_real_host_page_size - 1;
197 end &= ~(qemu_real_host_page_size - 1);
198
199 mprotect((void *)start, end - start,
200 PROT_READ | PROT_WRITE | PROT_EXEC);
201}
202#endif
203
bellardb346ff42003-06-15 20:05:50 +0000204static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000205{
bellard83fb7ad2004-07-05 21:25:26 +0000206 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000207 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000208#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000209 {
210 SYSTEM_INFO system_info;
211 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000212
bellardd5a8f072004-09-29 21:15:28 +0000213 GetSystemInfo(&system_info);
214 qemu_real_host_page_size = system_info.dwPageSize;
bellardd5a8f072004-09-29 21:15:28 +0000215 }
bellard67b915a2004-03-31 23:37:16 +0000216#else
bellard83fb7ad2004-07-05 21:25:26 +0000217 qemu_real_host_page_size = getpagesize();
bellard67b915a2004-03-31 23:37:16 +0000218#endif
bellard83fb7ad2004-07-05 21:25:26 +0000219 if (qemu_host_page_size == 0)
220 qemu_host_page_size = qemu_real_host_page_size;
221 if (qemu_host_page_size < TARGET_PAGE_SIZE)
222 qemu_host_page_size = TARGET_PAGE_SIZE;
223 qemu_host_page_bits = 0;
224 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
225 qemu_host_page_bits++;
226 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000227 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
228 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000229
230#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
231 {
232 long long startaddr, endaddr;
233 FILE *f;
234 int n;
235
236 f = fopen("/proc/self/maps", "r");
237 if (f) {
238 do {
239 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
240 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000241 startaddr = MIN(startaddr,
242 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
243 endaddr = MIN(endaddr,
244 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
balrog50a95692007-12-12 01:16:23 +0000245 page_set_flags(TARGET_PAGE_ALIGN(startaddr),
246 TARGET_PAGE_ALIGN(endaddr),
247 PAGE_RESERVED);
248 }
249 } while (!feof(f));
250 fclose(f);
251 }
252 }
253#endif
bellard54936002003-05-13 00:25:15 +0000254}
255
aurel3200f82b82008-04-27 21:12:55 +0000256static inline PageDesc *page_find_alloc(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000257{
bellard54936002003-05-13 00:25:15 +0000258 PageDesc **lp, *p;
259
bellard54936002003-05-13 00:25:15 +0000260 lp = &l1_map[index >> L2_BITS];
261 p = *lp;
262 if (!p) {
263 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000264 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000265 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000266 *lp = p;
267 }
268 return p + (index & (L2_SIZE - 1));
269}
270
aurel3200f82b82008-04-27 21:12:55 +0000271static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000272{
bellard54936002003-05-13 00:25:15 +0000273 PageDesc *p;
274
bellard54936002003-05-13 00:25:15 +0000275 p = l1_map[index >> L2_BITS];
276 if (!p)
277 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000278 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000279}
280
bellard108c49b2005-07-24 12:55:09 +0000281static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000282{
bellard108c49b2005-07-24 12:55:09 +0000283 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000284 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000285
bellard108c49b2005-07-24 12:55:09 +0000286 p = (void **)l1_phys_map;
287#if TARGET_PHYS_ADDR_SPACE_BITS > 32
288
289#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
290#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
291#endif
292 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000293 p = *lp;
294 if (!p) {
295 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000296 if (!alloc)
297 return NULL;
298 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
299 memset(p, 0, sizeof(void *) * L1_SIZE);
300 *lp = p;
301 }
302#endif
303 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000304 pd = *lp;
305 if (!pd) {
306 int i;
bellard108c49b2005-07-24 12:55:09 +0000307 /* allocate if not found */
308 if (!alloc)
309 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000310 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
311 *lp = pd;
312 for (i = 0; i < L2_SIZE; i++)
313 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000314 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000315 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000316}
317
bellard108c49b2005-07-24 12:55:09 +0000318static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000319{
bellard108c49b2005-07-24 12:55:09 +0000320 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000321}
322
bellard9fa3e852004-01-04 18:06:42 +0000323#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000324static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000325static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000326 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000327#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000328
bellard26a5f132008-05-28 12:30:31 +0000329void code_gen_alloc(unsigned long tb_size)
330{
331 code_gen_buffer_size = tb_size;
332 if (code_gen_buffer_size == 0) {
333 /* XXX: needs ajustments */
334 code_gen_buffer_size = (int)(phys_ram_size / 4);
335 }
336 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
337 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
338 /* The code gen buffer location may have constraints depending on
339 the host cpu and OS */
340#if defined(__linux__)
341 {
342 int flags;
343 flags = MAP_PRIVATE | MAP_ANONYMOUS;
344#if defined(__x86_64__)
345 flags |= MAP_32BIT;
346 /* Cannot map more than that */
347 if (code_gen_buffer_size > (800 * 1024 * 1024))
348 code_gen_buffer_size = (800 * 1024 * 1024);
349#endif
350 code_gen_buffer = mmap(NULL, code_gen_buffer_size,
351 PROT_WRITE | PROT_READ | PROT_EXEC,
352 flags, -1, 0);
353 if (code_gen_buffer == MAP_FAILED) {
354 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
355 exit(1);
356 }
357 }
358#else
359 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
360 if (!code_gen_buffer) {
361 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
362 exit(1);
363 }
364 map_exec(code_gen_buffer, code_gen_buffer_size);
365#endif
366 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
367 code_gen_buffer_max_size = code_gen_buffer_size -
368 code_gen_max_block_size();
369 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
370 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
371}
372
373/* Must be called before using the QEMU cpus. 'tb_size' is the size
374 (in bytes) allocated to the translation buffer. Zero means default
375 size. */
376void cpu_exec_init_all(unsigned long tb_size)
377{
378 page_init();
379 cpu_gen_init();
380 code_gen_alloc(tb_size);
381 code_gen_ptr = code_gen_buffer;
382 io_mem_init();
383}
384
bellard6a00d602005-11-21 23:25:50 +0000385void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000386{
bellard6a00d602005-11-21 23:25:50 +0000387 CPUState **penv;
388 int cpu_index;
389
bellard6a00d602005-11-21 23:25:50 +0000390 env->next_cpu = NULL;
391 penv = &first_cpu;
392 cpu_index = 0;
393 while (*penv != NULL) {
394 penv = (CPUState **)&(*penv)->next_cpu;
395 cpu_index++;
396 }
397 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000398 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000399 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000400}
401
bellard9fa3e852004-01-04 18:06:42 +0000402static inline void invalidate_page_bitmap(PageDesc *p)
403{
404 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000405 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000406 p->code_bitmap = NULL;
407 }
408 p->code_write_count = 0;
409}
410
bellardfd6ce8f2003-05-14 19:00:11 +0000411/* set to NULL all the 'first_tb' fields in all PageDescs */
412static void page_flush_tb(void)
413{
414 int i, j;
415 PageDesc *p;
416
417 for(i = 0; i < L1_SIZE; i++) {
418 p = l1_map[i];
419 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000420 for(j = 0; j < L2_SIZE; j++) {
421 p->first_tb = NULL;
422 invalidate_page_bitmap(p);
423 p++;
424 }
bellardfd6ce8f2003-05-14 19:00:11 +0000425 }
426 }
427}
428
429/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000430/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000431void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000432{
bellard6a00d602005-11-21 23:25:50 +0000433 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000434#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000435 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
436 (unsigned long)(code_gen_ptr - code_gen_buffer),
437 nb_tbs, nb_tbs > 0 ?
438 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000439#endif
bellard26a5f132008-05-28 12:30:31 +0000440 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000441 cpu_abort(env1, "Internal error: code buffer overflow\n");
442
bellardfd6ce8f2003-05-14 19:00:11 +0000443 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000444
bellard6a00d602005-11-21 23:25:50 +0000445 for(env = first_cpu; env != NULL; env = env->next_cpu) {
446 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
447 }
bellard9fa3e852004-01-04 18:06:42 +0000448
bellard8a8a6082004-10-03 13:36:49 +0000449 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000450 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000451
bellardfd6ce8f2003-05-14 19:00:11 +0000452 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000453 /* XXX: flush processor icache at this point if cache flush is
454 expensive */
bellarde3db7222005-01-26 22:00:47 +0000455 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000456}
457
458#ifdef DEBUG_TB_CHECK
459
j_mayerbc98a7e2007-04-04 07:55:12 +0000460static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000461{
462 TranslationBlock *tb;
463 int i;
464 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000465 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
466 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000467 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
468 address >= tb->pc + tb->size)) {
469 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000470 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000471 }
472 }
473 }
474}
475
476/* verify that all the pages have correct rights for code */
477static void tb_page_check(void)
478{
479 TranslationBlock *tb;
480 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000481
pbrook99773bd2006-04-16 15:14:59 +0000482 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
483 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000484 flags1 = page_get_flags(tb->pc);
485 flags2 = page_get_flags(tb->pc + tb->size - 1);
486 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
487 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000488 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000489 }
490 }
491 }
492}
493
bellardd4e81642003-05-25 16:46:15 +0000494void tb_jmp_check(TranslationBlock *tb)
495{
496 TranslationBlock *tb1;
497 unsigned int n1;
498
499 /* suppress any remaining jumps to this TB */
500 tb1 = tb->jmp_first;
501 for(;;) {
502 n1 = (long)tb1 & 3;
503 tb1 = (TranslationBlock *)((long)tb1 & ~3);
504 if (n1 == 2)
505 break;
506 tb1 = tb1->jmp_next[n1];
507 }
508 /* check end of list */
509 if (tb1 != tb) {
510 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
511 }
512}
513
bellardfd6ce8f2003-05-14 19:00:11 +0000514#endif
515
516/* invalidate one TB */
517static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
518 int next_offset)
519{
520 TranslationBlock *tb1;
521 for(;;) {
522 tb1 = *ptb;
523 if (tb1 == tb) {
524 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
525 break;
526 }
527 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
528 }
529}
530
bellard9fa3e852004-01-04 18:06:42 +0000531static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
532{
533 TranslationBlock *tb1;
534 unsigned int n1;
535
536 for(;;) {
537 tb1 = *ptb;
538 n1 = (long)tb1 & 3;
539 tb1 = (TranslationBlock *)((long)tb1 & ~3);
540 if (tb1 == tb) {
541 *ptb = tb1->page_next[n1];
542 break;
543 }
544 ptb = &tb1->page_next[n1];
545 }
546}
547
bellardd4e81642003-05-25 16:46:15 +0000548static inline void tb_jmp_remove(TranslationBlock *tb, int n)
549{
550 TranslationBlock *tb1, **ptb;
551 unsigned int n1;
552
553 ptb = &tb->jmp_next[n];
554 tb1 = *ptb;
555 if (tb1) {
556 /* find tb(n) in circular list */
557 for(;;) {
558 tb1 = *ptb;
559 n1 = (long)tb1 & 3;
560 tb1 = (TranslationBlock *)((long)tb1 & ~3);
561 if (n1 == n && tb1 == tb)
562 break;
563 if (n1 == 2) {
564 ptb = &tb1->jmp_first;
565 } else {
566 ptb = &tb1->jmp_next[n1];
567 }
568 }
569 /* now we can suppress tb(n) from the list */
570 *ptb = tb->jmp_next[n];
571
572 tb->jmp_next[n] = NULL;
573 }
574}
575
576/* reset the jump entry 'n' of a TB so that it is not chained to
577 another TB */
578static inline void tb_reset_jump(TranslationBlock *tb, int n)
579{
580 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
581}
582
aurel3200f82b82008-04-27 21:12:55 +0000583static inline void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000584{
bellard6a00d602005-11-21 23:25:50 +0000585 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000586 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000587 unsigned int h, n1;
aurel3200f82b82008-04-27 21:12:55 +0000588 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000589 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000590
bellard9fa3e852004-01-04 18:06:42 +0000591 /* remove the TB from the hash list */
592 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
593 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000594 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000595 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000596
bellard9fa3e852004-01-04 18:06:42 +0000597 /* remove the TB from the page list */
598 if (tb->page_addr[0] != page_addr) {
599 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
600 tb_page_remove(&p->first_tb, tb);
601 invalidate_page_bitmap(p);
602 }
603 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
604 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
605 tb_page_remove(&p->first_tb, tb);
606 invalidate_page_bitmap(p);
607 }
608
bellard8a40a182005-11-20 10:35:40 +0000609 tb_invalidated_flag = 1;
610
611 /* remove the TB from the hash list */
612 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000613 for(env = first_cpu; env != NULL; env = env->next_cpu) {
614 if (env->tb_jmp_cache[h] == tb)
615 env->tb_jmp_cache[h] = NULL;
616 }
bellard8a40a182005-11-20 10:35:40 +0000617
618 /* suppress this TB from the two jump lists */
619 tb_jmp_remove(tb, 0);
620 tb_jmp_remove(tb, 1);
621
622 /* suppress any remaining jumps to this TB */
623 tb1 = tb->jmp_first;
624 for(;;) {
625 n1 = (long)tb1 & 3;
626 if (n1 == 2)
627 break;
628 tb1 = (TranslationBlock *)((long)tb1 & ~3);
629 tb2 = tb1->jmp_next[n1];
630 tb_reset_jump(tb1, n1);
631 tb1->jmp_next[n1] = NULL;
632 tb1 = tb2;
633 }
634 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
635
bellarde3db7222005-01-26 22:00:47 +0000636 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000637}
638
639static inline void set_bits(uint8_t *tab, int start, int len)
640{
641 int end, mask, end1;
642
643 end = start + len;
644 tab += start >> 3;
645 mask = 0xff << (start & 7);
646 if ((start & ~7) == (end & ~7)) {
647 if (start < end) {
648 mask &= ~(0xff << (end & 7));
649 *tab |= mask;
650 }
651 } else {
652 *tab++ |= mask;
653 start = (start + 8) & ~7;
654 end1 = end & ~7;
655 while (start < end1) {
656 *tab++ = 0xff;
657 start += 8;
658 }
659 if (start < end) {
660 mask = ~(0xff << (end & 7));
661 *tab |= mask;
662 }
663 }
664}
665
666static void build_page_bitmap(PageDesc *p)
667{
668 int n, tb_start, tb_end;
669 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000670
bellard59817cc2004-02-16 22:01:13 +0000671 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000672 if (!p->code_bitmap)
673 return;
674 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
675
676 tb = p->first_tb;
677 while (tb != NULL) {
678 n = (long)tb & 3;
679 tb = (TranslationBlock *)((long)tb & ~3);
680 /* NOTE: this is subtle as a TB may span two physical pages */
681 if (n == 0) {
682 /* NOTE: tb_end may be after the end of the page, but
683 it is not a problem */
684 tb_start = tb->pc & ~TARGET_PAGE_MASK;
685 tb_end = tb_start + tb->size;
686 if (tb_end > TARGET_PAGE_SIZE)
687 tb_end = TARGET_PAGE_SIZE;
688 } else {
689 tb_start = 0;
690 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
691 }
692 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
693 tb = tb->page_next[n];
694 }
695}
696
bellardd720b932004-04-25 17:57:43 +0000697#ifdef TARGET_HAS_PRECISE_SMC
698
ths5fafdf22007-09-16 21:08:06 +0000699static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000700 target_ulong pc, target_ulong cs_base, int flags,
701 int cflags)
702{
703 TranslationBlock *tb;
704 uint8_t *tc_ptr;
705 target_ulong phys_pc, phys_page2, virt_page2;
706 int code_gen_size;
707
bellardc27004e2005-01-03 23:35:10 +0000708 phys_pc = get_phys_addr_code(env, pc);
709 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000710 if (!tb) {
711 /* flush must be done */
712 tb_flush(env);
713 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000714 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000715 }
716 tc_ptr = code_gen_ptr;
717 tb->tc_ptr = tc_ptr;
718 tb->cs_base = cs_base;
719 tb->flags = flags;
720 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000721 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000722 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 +0000723
bellardd720b932004-04-25 17:57:43 +0000724 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000725 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000726 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000727 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000728 phys_page2 = get_phys_addr_code(env, virt_page2);
729 }
730 tb_link_phys(tb, phys_pc, phys_page2);
731}
732#endif
ths3b46e622007-09-17 08:09:54 +0000733
bellard9fa3e852004-01-04 18:06:42 +0000734/* invalidate all TBs which intersect with the target physical page
735 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000736 the same physical page. 'is_cpu_write_access' should be true if called
737 from a real cpu write access: the virtual CPU will exit the current
738 TB if code is modified inside this TB. */
aurel3200f82b82008-04-27 21:12:55 +0000739void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000740 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000741{
bellardd720b932004-04-25 17:57:43 +0000742 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000743 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000744 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000745 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000746 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000747 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000748
749 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000750 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000751 return;
ths5fafdf22007-09-16 21:08:06 +0000752 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000753 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
754 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000755 /* build code bitmap */
756 build_page_bitmap(p);
757 }
758
759 /* we remove all the TBs in the range [start, end[ */
760 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000761 current_tb_not_found = is_cpu_write_access;
762 current_tb_modified = 0;
763 current_tb = NULL; /* avoid warning */
764 current_pc = 0; /* avoid warning */
765 current_cs_base = 0; /* avoid warning */
766 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000767 tb = p->first_tb;
768 while (tb != NULL) {
769 n = (long)tb & 3;
770 tb = (TranslationBlock *)((long)tb & ~3);
771 tb_next = tb->page_next[n];
772 /* NOTE: this is subtle as a TB may span two physical pages */
773 if (n == 0) {
774 /* NOTE: tb_end may be after the end of the page, but
775 it is not a problem */
776 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
777 tb_end = tb_start + tb->size;
778 } else {
779 tb_start = tb->page_addr[1];
780 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
781 }
782 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000783#ifdef TARGET_HAS_PRECISE_SMC
784 if (current_tb_not_found) {
785 current_tb_not_found = 0;
786 current_tb = NULL;
787 if (env->mem_write_pc) {
788 /* now we have a real cpu fault */
789 current_tb = tb_find_pc(env->mem_write_pc);
790 }
791 }
792 if (current_tb == tb &&
793 !(current_tb->cflags & CF_SINGLE_INSN)) {
794 /* If we are modifying the current TB, we must stop
795 its execution. We could be more precise by checking
796 that the modification is after the current PC, but it
797 would require a specialized function to partially
798 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000799
bellardd720b932004-04-25 17:57:43 +0000800 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000801 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000802 env->mem_write_pc, NULL);
803#if defined(TARGET_I386)
804 current_flags = env->hflags;
805 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
806 current_cs_base = (target_ulong)env->segs[R_CS].base;
807 current_pc = current_cs_base + env->eip;
808#else
809#error unsupported CPU
810#endif
811 }
812#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000813 /* we need to do that to handle the case where a signal
814 occurs while doing tb_phys_invalidate() */
815 saved_tb = NULL;
816 if (env) {
817 saved_tb = env->current_tb;
818 env->current_tb = NULL;
819 }
bellard9fa3e852004-01-04 18:06:42 +0000820 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000821 if (env) {
822 env->current_tb = saved_tb;
823 if (env->interrupt_request && env->current_tb)
824 cpu_interrupt(env, env->interrupt_request);
825 }
bellard9fa3e852004-01-04 18:06:42 +0000826 }
827 tb = tb_next;
828 }
829#if !defined(CONFIG_USER_ONLY)
830 /* if no code remaining, no need to continue to use slow writes */
831 if (!p->first_tb) {
832 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000833 if (is_cpu_write_access) {
834 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
835 }
836 }
837#endif
838#ifdef TARGET_HAS_PRECISE_SMC
839 if (current_tb_modified) {
840 /* we generate a block containing just the instruction
841 modifying the memory. It will ensure that it cannot modify
842 itself */
bellardea1c1802004-06-14 18:56:36 +0000843 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000844 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000845 CF_SINGLE_INSN);
846 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000847 }
848#endif
849}
850
851/* len must be <= 8 and start must be a multiple of len */
aurel3200f82b82008-04-27 21:12:55 +0000852static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000853{
854 PageDesc *p;
855 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000856#if 0
bellarda4193c82004-06-03 14:01:43 +0000857 if (1) {
858 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000859 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
860 cpu_single_env->mem_write_vaddr, len,
861 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000862 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
863 }
bellard59817cc2004-02-16 22:01:13 +0000864 }
865#endif
bellard9fa3e852004-01-04 18:06:42 +0000866 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000867 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000868 return;
869 if (p->code_bitmap) {
870 offset = start & ~TARGET_PAGE_MASK;
871 b = p->code_bitmap[offset >> 3] >> (offset & 7);
872 if (b & ((1 << len) - 1))
873 goto do_invalidate;
874 } else {
875 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000876 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000877 }
878}
879
bellard9fa3e852004-01-04 18:06:42 +0000880#if !defined(CONFIG_SOFTMMU)
aurel3200f82b82008-04-27 21:12:55 +0000881static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +0000882 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000883{
bellardd720b932004-04-25 17:57:43 +0000884 int n, current_flags, current_tb_modified;
885 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000886 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000887 TranslationBlock *tb, *current_tb;
888#ifdef TARGET_HAS_PRECISE_SMC
889 CPUState *env = cpu_single_env;
890#endif
bellard9fa3e852004-01-04 18:06:42 +0000891
892 addr &= TARGET_PAGE_MASK;
893 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000894 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000895 return;
896 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000897 current_tb_modified = 0;
898 current_tb = NULL;
899 current_pc = 0; /* avoid warning */
900 current_cs_base = 0; /* avoid warning */
901 current_flags = 0; /* avoid warning */
902#ifdef TARGET_HAS_PRECISE_SMC
903 if (tb && pc != 0) {
904 current_tb = tb_find_pc(pc);
905 }
906#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000907 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000908 n = (long)tb & 3;
909 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000910#ifdef TARGET_HAS_PRECISE_SMC
911 if (current_tb == tb &&
912 !(current_tb->cflags & CF_SINGLE_INSN)) {
913 /* If we are modifying the current TB, we must stop
914 its execution. We could be more precise by checking
915 that the modification is after the current PC, but it
916 would require a specialized function to partially
917 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000918
bellardd720b932004-04-25 17:57:43 +0000919 current_tb_modified = 1;
920 cpu_restore_state(current_tb, env, pc, puc);
921#if defined(TARGET_I386)
922 current_flags = env->hflags;
923 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
924 current_cs_base = (target_ulong)env->segs[R_CS].base;
925 current_pc = current_cs_base + env->eip;
926#else
927#error unsupported CPU
928#endif
929 }
930#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000931 tb_phys_invalidate(tb, addr);
932 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000933 }
934 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000935#ifdef TARGET_HAS_PRECISE_SMC
936 if (current_tb_modified) {
937 /* we generate a block containing just the instruction
938 modifying the memory. It will ensure that it cannot modify
939 itself */
bellardea1c1802004-06-14 18:56:36 +0000940 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000941 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000942 CF_SINGLE_INSN);
943 cpu_resume_from_signal(env, puc);
944 }
945#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000946}
bellard9fa3e852004-01-04 18:06:42 +0000947#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000948
949/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000950static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000951 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000952{
953 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000954 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000955
bellard9fa3e852004-01-04 18:06:42 +0000956 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000957 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000958 tb->page_next[n] = p->first_tb;
959 last_first_tb = p->first_tb;
960 p->first_tb = (TranslationBlock *)((long)tb | n);
961 invalidate_page_bitmap(p);
962
bellard107db442004-06-22 18:48:46 +0000963#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000964
bellard9fa3e852004-01-04 18:06:42 +0000965#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000966 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000967 target_ulong addr;
968 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000969 int prot;
970
bellardfd6ce8f2003-05-14 19:00:11 +0000971 /* force the host page as non writable (writes will have a
972 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000973 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000974 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000975 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
976 addr += TARGET_PAGE_SIZE) {
977
978 p2 = page_find (addr >> TARGET_PAGE_BITS);
979 if (!p2)
980 continue;
981 prot |= p2->flags;
982 p2->flags &= ~PAGE_WRITE;
983 page_get_flags(addr);
984 }
ths5fafdf22007-09-16 21:08:06 +0000985 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000986 (prot & PAGE_BITS) & ~PAGE_WRITE);
987#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +0000988 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +0000989 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000990#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000991 }
bellard9fa3e852004-01-04 18:06:42 +0000992#else
993 /* if some code is already present, then the pages are already
994 protected. So we handle the case where only the first TB is
995 allocated in a physical page */
996 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000997 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000998 }
999#endif
bellardd720b932004-04-25 17:57:43 +00001000
1001#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001002}
1003
1004/* Allocate a new translation block. Flush the translation buffer if
1005 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001006TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001007{
1008 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001009
bellard26a5f132008-05-28 12:30:31 +00001010 if (nb_tbs >= code_gen_max_blocks ||
1011 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001012 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001013 tb = &tbs[nb_tbs++];
1014 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001015 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001016 return tb;
1017}
1018
bellard9fa3e852004-01-04 18:06:42 +00001019/* add a new TB and link it to the physical page tables. phys_page2 is
1020 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001021void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001022 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001023{
bellard9fa3e852004-01-04 18:06:42 +00001024 unsigned int h;
1025 TranslationBlock **ptb;
1026
1027 /* add in the physical hash table */
1028 h = tb_phys_hash_func(phys_pc);
1029 ptb = &tb_phys_hash[h];
1030 tb->phys_hash_next = *ptb;
1031 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001032
1033 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001034 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1035 if (phys_page2 != -1)
1036 tb_alloc_page(tb, 1, phys_page2);
1037 else
1038 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001039
bellardd4e81642003-05-25 16:46:15 +00001040 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1041 tb->jmp_next[0] = NULL;
1042 tb->jmp_next[1] = NULL;
1043
1044 /* init original jump addresses */
1045 if (tb->tb_next_offset[0] != 0xffff)
1046 tb_reset_jump(tb, 0);
1047 if (tb->tb_next_offset[1] != 0xffff)
1048 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001049
1050#ifdef DEBUG_TB_CHECK
1051 tb_page_check();
1052#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001053}
1054
bellarda513fe12003-05-27 23:29:48 +00001055/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1056 tb[1].tc_ptr. Return NULL if not found */
1057TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1058{
1059 int m_min, m_max, m;
1060 unsigned long v;
1061 TranslationBlock *tb;
1062
1063 if (nb_tbs <= 0)
1064 return NULL;
1065 if (tc_ptr < (unsigned long)code_gen_buffer ||
1066 tc_ptr >= (unsigned long)code_gen_ptr)
1067 return NULL;
1068 /* binary search (cf Knuth) */
1069 m_min = 0;
1070 m_max = nb_tbs - 1;
1071 while (m_min <= m_max) {
1072 m = (m_min + m_max) >> 1;
1073 tb = &tbs[m];
1074 v = (unsigned long)tb->tc_ptr;
1075 if (v == tc_ptr)
1076 return tb;
1077 else if (tc_ptr < v) {
1078 m_max = m - 1;
1079 } else {
1080 m_min = m + 1;
1081 }
ths5fafdf22007-09-16 21:08:06 +00001082 }
bellarda513fe12003-05-27 23:29:48 +00001083 return &tbs[m_max];
1084}
bellard75012672003-06-21 13:11:07 +00001085
bellardea041c02003-06-25 16:16:50 +00001086static void tb_reset_jump_recursive(TranslationBlock *tb);
1087
1088static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1089{
1090 TranslationBlock *tb1, *tb_next, **ptb;
1091 unsigned int n1;
1092
1093 tb1 = tb->jmp_next[n];
1094 if (tb1 != NULL) {
1095 /* find head of list */
1096 for(;;) {
1097 n1 = (long)tb1 & 3;
1098 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1099 if (n1 == 2)
1100 break;
1101 tb1 = tb1->jmp_next[n1];
1102 }
1103 /* we are now sure now that tb jumps to tb1 */
1104 tb_next = tb1;
1105
1106 /* remove tb from the jmp_first list */
1107 ptb = &tb_next->jmp_first;
1108 for(;;) {
1109 tb1 = *ptb;
1110 n1 = (long)tb1 & 3;
1111 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1112 if (n1 == n && tb1 == tb)
1113 break;
1114 ptb = &tb1->jmp_next[n1];
1115 }
1116 *ptb = tb->jmp_next[n];
1117 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001118
bellardea041c02003-06-25 16:16:50 +00001119 /* suppress the jump to next tb in generated code */
1120 tb_reset_jump(tb, n);
1121
bellard01243112004-01-04 15:48:17 +00001122 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001123 tb_reset_jump_recursive(tb_next);
1124 }
1125}
1126
1127static void tb_reset_jump_recursive(TranslationBlock *tb)
1128{
1129 tb_reset_jump_recursive2(tb, 0);
1130 tb_reset_jump_recursive2(tb, 1);
1131}
1132
bellard1fddef42005-04-17 19:16:13 +00001133#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001134static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1135{
j_mayer9b3c35e2007-04-07 11:21:28 +00001136 target_phys_addr_t addr;
1137 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001138 ram_addr_t ram_addr;
1139 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001140
pbrookc2f07f82006-04-08 17:14:56 +00001141 addr = cpu_get_phys_page_debug(env, pc);
1142 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1143 if (!p) {
1144 pd = IO_MEM_UNASSIGNED;
1145 } else {
1146 pd = p->phys_offset;
1147 }
1148 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001149 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001150}
bellardc27004e2005-01-03 23:35:10 +00001151#endif
bellardd720b932004-04-25 17:57:43 +00001152
pbrook6658ffb2007-03-16 23:58:11 +00001153/* Add a watchpoint. */
1154int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1155{
1156 int i;
1157
1158 for (i = 0; i < env->nb_watchpoints; i++) {
1159 if (addr == env->watchpoint[i].vaddr)
1160 return 0;
1161 }
1162 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1163 return -1;
1164
1165 i = env->nb_watchpoints++;
1166 env->watchpoint[i].vaddr = addr;
1167 tlb_flush_page(env, addr);
1168 /* FIXME: This flush is needed because of the hack to make memory ops
1169 terminate the TB. It can be removed once the proper IO trap and
1170 re-execute bits are in. */
1171 tb_flush(env);
1172 return i;
1173}
1174
1175/* Remove a watchpoint. */
1176int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1177{
1178 int i;
1179
1180 for (i = 0; i < env->nb_watchpoints; i++) {
1181 if (addr == env->watchpoint[i].vaddr) {
1182 env->nb_watchpoints--;
1183 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1184 tlb_flush_page(env, addr);
1185 return 0;
1186 }
1187 }
1188 return -1;
1189}
1190
edgar_igl7d03f822008-05-17 18:58:29 +00001191/* Remove all watchpoints. */
1192void cpu_watchpoint_remove_all(CPUState *env) {
1193 int i;
1194
1195 for (i = 0; i < env->nb_watchpoints; i++) {
1196 tlb_flush_page(env, env->watchpoint[i].vaddr);
1197 }
1198 env->nb_watchpoints = 0;
1199}
1200
bellardc33a3462003-07-29 20:50:33 +00001201/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1202 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001203int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001204{
bellard1fddef42005-04-17 19:16:13 +00001205#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001206 int i;
ths3b46e622007-09-17 08:09:54 +00001207
bellard4c3a88a2003-07-26 12:06:08 +00001208 for(i = 0; i < env->nb_breakpoints; i++) {
1209 if (env->breakpoints[i] == pc)
1210 return 0;
1211 }
1212
1213 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1214 return -1;
1215 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001216
bellardd720b932004-04-25 17:57:43 +00001217 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001218 return 0;
1219#else
1220 return -1;
1221#endif
1222}
1223
edgar_igl7d03f822008-05-17 18:58:29 +00001224/* remove all breakpoints */
1225void cpu_breakpoint_remove_all(CPUState *env) {
1226#if defined(TARGET_HAS_ICE)
1227 int i;
1228 for(i = 0; i < env->nb_breakpoints; i++) {
1229 breakpoint_invalidate(env, env->breakpoints[i]);
1230 }
1231 env->nb_breakpoints = 0;
1232#endif
1233}
1234
bellard4c3a88a2003-07-26 12:06:08 +00001235/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001236int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001237{
bellard1fddef42005-04-17 19:16:13 +00001238#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001239 int i;
1240 for(i = 0; i < env->nb_breakpoints; i++) {
1241 if (env->breakpoints[i] == pc)
1242 goto found;
1243 }
1244 return -1;
1245 found:
bellard4c3a88a2003-07-26 12:06:08 +00001246 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001247 if (i < env->nb_breakpoints)
1248 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001249
1250 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001251 return 0;
1252#else
1253 return -1;
1254#endif
1255}
1256
bellardc33a3462003-07-29 20:50:33 +00001257/* enable or disable single step mode. EXCP_DEBUG is returned by the
1258 CPU loop after each instruction */
1259void cpu_single_step(CPUState *env, int enabled)
1260{
bellard1fddef42005-04-17 19:16:13 +00001261#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001262 if (env->singlestep_enabled != enabled) {
1263 env->singlestep_enabled = enabled;
1264 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001265 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001266 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001267 }
1268#endif
1269}
1270
bellard34865132003-10-05 14:28:56 +00001271/* enable or disable low levels log */
1272void cpu_set_log(int log_flags)
1273{
1274 loglevel = log_flags;
1275 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001276 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001277 if (!logfile) {
1278 perror(logfilename);
1279 _exit(1);
1280 }
bellard9fa3e852004-01-04 18:06:42 +00001281#if !defined(CONFIG_SOFTMMU)
1282 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1283 {
1284 static uint8_t logfile_buf[4096];
1285 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1286 }
1287#else
bellard34865132003-10-05 14:28:56 +00001288 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001289#endif
pbrooke735b912007-06-30 13:53:24 +00001290 log_append = 1;
1291 }
1292 if (!loglevel && logfile) {
1293 fclose(logfile);
1294 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001295 }
1296}
1297
1298void cpu_set_log_filename(const char *filename)
1299{
1300 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001301 if (logfile) {
1302 fclose(logfile);
1303 logfile = NULL;
1304 }
1305 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001306}
bellardc33a3462003-07-29 20:50:33 +00001307
bellard01243112004-01-04 15:48:17 +00001308/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001309void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001310{
1311 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001312 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
bellard59817cc2004-02-16 22:01:13 +00001313
bellard68a79312003-06-30 13:12:32 +00001314 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001315 /* if the cpu is currently executing code, we must unlink it and
1316 all the potentially executing TB */
1317 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001318 if (tb && !testandset(&interrupt_lock)) {
1319 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001320 tb_reset_jump_recursive(tb);
aurel3215a51152008-03-28 22:29:15 +00001321 resetlock(&interrupt_lock);
bellardea041c02003-06-25 16:16:50 +00001322 }
1323}
1324
bellardb54ad042004-05-20 13:42:52 +00001325void cpu_reset_interrupt(CPUState *env, int mask)
1326{
1327 env->interrupt_request &= ~mask;
1328}
1329
bellardf193c792004-03-21 17:06:25 +00001330CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001331 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001332 "show generated host assembly code for each compiled TB" },
1333 { CPU_LOG_TB_IN_ASM, "in_asm",
1334 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001335 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001336 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001337 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001338 "show micro ops "
1339#ifdef TARGET_I386
1340 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001341#endif
blueswir1e01a1152008-03-14 17:37:11 +00001342 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001343 { CPU_LOG_INT, "int",
1344 "show interrupts/exceptions in short format" },
1345 { CPU_LOG_EXEC, "exec",
1346 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001347 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001348 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001349#ifdef TARGET_I386
1350 { CPU_LOG_PCALL, "pcall",
1351 "show protected mode far calls/returns/exceptions" },
1352#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001353#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001354 { CPU_LOG_IOPORT, "ioport",
1355 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001356#endif
bellardf193c792004-03-21 17:06:25 +00001357 { 0, NULL, NULL },
1358};
1359
1360static int cmp1(const char *s1, int n, const char *s2)
1361{
1362 if (strlen(s2) != n)
1363 return 0;
1364 return memcmp(s1, s2, n) == 0;
1365}
ths3b46e622007-09-17 08:09:54 +00001366
bellardf193c792004-03-21 17:06:25 +00001367/* takes a comma separated list of log masks. Return 0 if error. */
1368int cpu_str_to_log_mask(const char *str)
1369{
1370 CPULogItem *item;
1371 int mask;
1372 const char *p, *p1;
1373
1374 p = str;
1375 mask = 0;
1376 for(;;) {
1377 p1 = strchr(p, ',');
1378 if (!p1)
1379 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001380 if(cmp1(p,p1-p,"all")) {
1381 for(item = cpu_log_items; item->mask != 0; item++) {
1382 mask |= item->mask;
1383 }
1384 } else {
bellardf193c792004-03-21 17:06:25 +00001385 for(item = cpu_log_items; item->mask != 0; item++) {
1386 if (cmp1(p, p1 - p, item->name))
1387 goto found;
1388 }
1389 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001390 }
bellardf193c792004-03-21 17:06:25 +00001391 found:
1392 mask |= item->mask;
1393 if (*p1 != ',')
1394 break;
1395 p = p1 + 1;
1396 }
1397 return mask;
1398}
bellardea041c02003-06-25 16:16:50 +00001399
bellard75012672003-06-21 13:11:07 +00001400void cpu_abort(CPUState *env, const char *fmt, ...)
1401{
1402 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001403 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001404
1405 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001406 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001407 fprintf(stderr, "qemu: fatal: ");
1408 vfprintf(stderr, fmt, ap);
1409 fprintf(stderr, "\n");
1410#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001411 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1412#else
1413 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001414#endif
balrog924edca2007-06-10 14:07:13 +00001415 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001416 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001417 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001418 fprintf(logfile, "\n");
1419#ifdef TARGET_I386
1420 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1421#else
1422 cpu_dump_state(env, logfile, fprintf, 0);
1423#endif
balrog924edca2007-06-10 14:07:13 +00001424 fflush(logfile);
1425 fclose(logfile);
1426 }
pbrook493ae1f2007-11-23 16:53:59 +00001427 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001428 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001429 abort();
1430}
1431
thsc5be9f02007-02-28 20:20:53 +00001432CPUState *cpu_copy(CPUState *env)
1433{
ths01ba9812007-12-09 02:22:57 +00001434 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001435 /* preserve chaining and index */
1436 CPUState *next_cpu = new_env->next_cpu;
1437 int cpu_index = new_env->cpu_index;
1438 memcpy(new_env, env, sizeof(CPUState));
1439 new_env->next_cpu = next_cpu;
1440 new_env->cpu_index = cpu_index;
1441 return new_env;
1442}
1443
bellard01243112004-01-04 15:48:17 +00001444#if !defined(CONFIG_USER_ONLY)
1445
edgar_igl5c751e92008-05-06 08:44:21 +00001446static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1447{
1448 unsigned int i;
1449
1450 /* Discard jump cache entries for any tb which might potentially
1451 overlap the flushed page. */
1452 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1453 memset (&env->tb_jmp_cache[i], 0,
1454 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1455
1456 i = tb_jmp_cache_hash_page(addr);
1457 memset (&env->tb_jmp_cache[i], 0,
1458 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1459}
1460
bellardee8b7022004-02-03 23:35:10 +00001461/* NOTE: if flush_global is true, also flush global entries (not
1462 implemented yet) */
1463void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001464{
bellard33417e72003-08-10 21:47:01 +00001465 int i;
bellard01243112004-01-04 15:48:17 +00001466
bellard9fa3e852004-01-04 18:06:42 +00001467#if defined(DEBUG_TLB)
1468 printf("tlb_flush:\n");
1469#endif
bellard01243112004-01-04 15:48:17 +00001470 /* must reset current TB so that interrupts cannot modify the
1471 links while we are modifying them */
1472 env->current_tb = NULL;
1473
bellard33417e72003-08-10 21:47:01 +00001474 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001475 env->tlb_table[0][i].addr_read = -1;
1476 env->tlb_table[0][i].addr_write = -1;
1477 env->tlb_table[0][i].addr_code = -1;
1478 env->tlb_table[1][i].addr_read = -1;
1479 env->tlb_table[1][i].addr_write = -1;
1480 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001481#if (NB_MMU_MODES >= 3)
1482 env->tlb_table[2][i].addr_read = -1;
1483 env->tlb_table[2][i].addr_write = -1;
1484 env->tlb_table[2][i].addr_code = -1;
1485#if (NB_MMU_MODES == 4)
1486 env->tlb_table[3][i].addr_read = -1;
1487 env->tlb_table[3][i].addr_write = -1;
1488 env->tlb_table[3][i].addr_code = -1;
1489#endif
1490#endif
bellard33417e72003-08-10 21:47:01 +00001491 }
bellard9fa3e852004-01-04 18:06:42 +00001492
bellard8a40a182005-11-20 10:35:40 +00001493 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001494
1495#if !defined(CONFIG_SOFTMMU)
1496 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1497#endif
bellard0a962c02005-02-10 22:00:27 +00001498#ifdef USE_KQEMU
1499 if (env->kqemu_enabled) {
1500 kqemu_flush(env, flush_global);
1501 }
1502#endif
bellarde3db7222005-01-26 22:00:47 +00001503 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001504}
1505
bellard274da6b2004-05-20 21:56:27 +00001506static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001507{
ths5fafdf22007-09-16 21:08:06 +00001508 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001509 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001510 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001511 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001512 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001513 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1514 tlb_entry->addr_read = -1;
1515 tlb_entry->addr_write = -1;
1516 tlb_entry->addr_code = -1;
1517 }
bellard61382a52003-10-27 21:22:23 +00001518}
1519
bellard2e126692004-04-25 21:28:44 +00001520void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001521{
bellard8a40a182005-11-20 10:35:40 +00001522 int i;
bellard01243112004-01-04 15:48:17 +00001523
bellard9fa3e852004-01-04 18:06:42 +00001524#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001525 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001526#endif
bellard01243112004-01-04 15:48:17 +00001527 /* must reset current TB so that interrupts cannot modify the
1528 links while we are modifying them */
1529 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001530
bellard61382a52003-10-27 21:22:23 +00001531 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001532 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001533 tlb_flush_entry(&env->tlb_table[0][i], addr);
1534 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001535#if (NB_MMU_MODES >= 3)
1536 tlb_flush_entry(&env->tlb_table[2][i], addr);
1537#if (NB_MMU_MODES == 4)
1538 tlb_flush_entry(&env->tlb_table[3][i], addr);
1539#endif
1540#endif
bellard01243112004-01-04 15:48:17 +00001541
edgar_igl5c751e92008-05-06 08:44:21 +00001542 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001543
bellard01243112004-01-04 15:48:17 +00001544#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001545 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001546 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001547#endif
bellard0a962c02005-02-10 22:00:27 +00001548#ifdef USE_KQEMU
1549 if (env->kqemu_enabled) {
1550 kqemu_flush_page(env, addr);
1551 }
1552#endif
bellard9fa3e852004-01-04 18:06:42 +00001553}
1554
bellard9fa3e852004-01-04 18:06:42 +00001555/* update the TLBs so that writes to code in the virtual page 'addr'
1556 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001557static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001558{
ths5fafdf22007-09-16 21:08:06 +00001559 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001560 ram_addr + TARGET_PAGE_SIZE,
1561 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001562}
1563
bellard9fa3e852004-01-04 18:06:42 +00001564/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001565 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001566static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001567 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001568{
bellard3a7d9292005-08-21 09:26:42 +00001569 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001570}
1571
ths5fafdf22007-09-16 21:08:06 +00001572static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001573 unsigned long start, unsigned long length)
1574{
1575 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001576 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1577 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001578 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001579 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001580 }
1581 }
1582}
1583
bellard3a7d9292005-08-21 09:26:42 +00001584void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001585 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001586{
1587 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001588 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001589 int i, mask, len;
1590 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001591
1592 start &= TARGET_PAGE_MASK;
1593 end = TARGET_PAGE_ALIGN(end);
1594
1595 length = end - start;
1596 if (length == 0)
1597 return;
bellard0a962c02005-02-10 22:00:27 +00001598 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001599#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001600 /* XXX: should not depend on cpu context */
1601 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001602 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001603 ram_addr_t addr;
1604 addr = start;
1605 for(i = 0; i < len; i++) {
1606 kqemu_set_notdirty(env, addr);
1607 addr += TARGET_PAGE_SIZE;
1608 }
bellard3a7d9292005-08-21 09:26:42 +00001609 }
1610#endif
bellardf23db162005-08-21 19:12:28 +00001611 mask = ~dirty_flags;
1612 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1613 for(i = 0; i < len; i++)
1614 p[i] &= mask;
1615
bellard1ccde1c2004-02-06 19:46:14 +00001616 /* we modify the TLB cache so that the dirty bit will be set again
1617 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001618 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001619 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1620 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001621 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001622 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001623 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001624#if (NB_MMU_MODES >= 3)
1625 for(i = 0; i < CPU_TLB_SIZE; i++)
1626 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1627#if (NB_MMU_MODES == 4)
1628 for(i = 0; i < CPU_TLB_SIZE; i++)
1629 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1630#endif
1631#endif
bellard6a00d602005-11-21 23:25:50 +00001632 }
bellard59817cc2004-02-16 22:01:13 +00001633
1634#if !defined(CONFIG_SOFTMMU)
1635 /* XXX: this is expensive */
1636 {
1637 VirtPageDesc *p;
1638 int j;
1639 target_ulong addr;
1640
1641 for(i = 0; i < L1_SIZE; i++) {
1642 p = l1_virt_map[i];
1643 if (p) {
1644 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1645 for(j = 0; j < L2_SIZE; j++) {
1646 if (p->valid_tag == virt_valid_tag &&
1647 p->phys_addr >= start && p->phys_addr < end &&
1648 (p->prot & PROT_WRITE)) {
1649 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001650 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001651 p->prot & ~PROT_WRITE);
1652 }
1653 }
1654 addr += TARGET_PAGE_SIZE;
1655 p++;
1656 }
1657 }
1658 }
1659 }
1660#endif
bellard1ccde1c2004-02-06 19:46:14 +00001661}
1662
bellard3a7d9292005-08-21 09:26:42 +00001663static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1664{
1665 ram_addr_t ram_addr;
1666
bellard84b7b8e2005-11-28 21:19:04 +00001667 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001668 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001669 tlb_entry->addend - (unsigned long)phys_ram_base;
1670 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001671 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001672 }
1673 }
1674}
1675
1676/* update the TLB according to the current state of the dirty bits */
1677void cpu_tlb_update_dirty(CPUState *env)
1678{
1679 int i;
1680 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001681 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001682 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001683 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001684#if (NB_MMU_MODES >= 3)
1685 for(i = 0; i < CPU_TLB_SIZE; i++)
1686 tlb_update_dirty(&env->tlb_table[2][i]);
1687#if (NB_MMU_MODES == 4)
1688 for(i = 0; i < CPU_TLB_SIZE; i++)
1689 tlb_update_dirty(&env->tlb_table[3][i]);
1690#endif
1691#endif
bellard3a7d9292005-08-21 09:26:42 +00001692}
1693
ths5fafdf22007-09-16 21:08:06 +00001694static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001695 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001696{
1697 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001698 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1699 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001700 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001701 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001702 }
1703 }
1704}
1705
1706/* update the TLB corresponding to virtual page vaddr and phys addr
1707 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001708static inline void tlb_set_dirty(CPUState *env,
1709 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001710{
bellard1ccde1c2004-02-06 19:46:14 +00001711 int i;
1712
bellard1ccde1c2004-02-06 19:46:14 +00001713 addr &= TARGET_PAGE_MASK;
1714 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001715 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1716 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001717#if (NB_MMU_MODES >= 3)
1718 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1719#if (NB_MMU_MODES == 4)
1720 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1721#endif
1722#endif
bellard9fa3e852004-01-04 18:06:42 +00001723}
1724
bellard59817cc2004-02-16 22:01:13 +00001725/* add a new TLB entry. At most one entry for a given virtual address
1726 is permitted. Return 0 if OK or 2 if the page could not be mapped
1727 (can only happen in non SOFTMMU mode for I/O pages or pages
1728 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001729int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1730 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001731 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001732{
bellard92e873b2004-05-21 14:52:29 +00001733 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001734 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001735 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001736 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001737 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001738 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001739 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001740 int i;
bellard9fa3e852004-01-04 18:06:42 +00001741
bellard92e873b2004-05-21 14:52:29 +00001742 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001743 if (!p) {
1744 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001745 } else {
1746 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001747 }
1748#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001749 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1750 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001751#endif
1752
1753 ret = 0;
1754#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001755 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001756#endif
1757 {
bellard2a4188a2006-06-25 21:54:59 +00001758 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001759 /* IO memory case */
1760 address = vaddr | pd;
1761 addend = paddr;
1762 } else {
1763 /* standard memory */
1764 address = vaddr;
1765 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1766 }
pbrook6658ffb2007-03-16 23:58:11 +00001767
1768 /* Make accesses to pages with watchpoints go via the
1769 watchpoint trap routines. */
1770 for (i = 0; i < env->nb_watchpoints; i++) {
1771 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1772 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001773 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001774 address = vaddr | io_mem_watch;
1775 } else {
balrogd79acba2007-06-26 20:01:13 +00001776 env->watchpoint[i].addend = pd - paddr +
1777 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001778 /* TODO: Figure out how to make read watchpoints coexist
1779 with code. */
1780 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1781 }
1782 }
1783 }
balrogd79acba2007-06-26 20:01:13 +00001784
bellard90f18422005-07-24 10:17:31 +00001785 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001786 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001787 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001788 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001789 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001790 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001791 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001792 te->addr_read = -1;
1793 }
edgar_igl5c751e92008-05-06 08:44:21 +00001794
bellard84b7b8e2005-11-28 21:19:04 +00001795 if (prot & PAGE_EXEC) {
1796 te->addr_code = address;
1797 } else {
1798 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001799 }
bellard67b915a2004-03-31 23:37:16 +00001800 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001801 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001802 (pd & IO_MEM_ROMD)) {
1803 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001804 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001805 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001806 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001807 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001808 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001809 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001810 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001811 }
1812 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001813 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001814 }
1815 }
1816#if !defined(CONFIG_SOFTMMU)
1817 else {
1818 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1819 /* IO access: no mapping is done as it will be handled by the
1820 soft MMU */
1821 if (!(env->hflags & HF_SOFTMMU_MASK))
1822 ret = 2;
1823 } else {
1824 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001825
bellard59817cc2004-02-16 22:01:13 +00001826 if (vaddr >= MMAP_AREA_END) {
1827 ret = 2;
1828 } else {
1829 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001830 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001831#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001832 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001833#endif
ths5fafdf22007-09-16 21:08:06 +00001834 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001835 !cpu_physical_memory_is_dirty(pd))) {
1836 /* ROM: we do as if code was inside */
1837 /* if code is present, we only map as read only and save the
1838 original mapping */
1839 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001840
bellard90f18422005-07-24 10:17:31 +00001841 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001842 vp->phys_addr = pd;
1843 vp->prot = prot;
1844 vp->valid_tag = virt_valid_tag;
1845 prot &= ~PAGE_WRITE;
1846 }
bellard9fa3e852004-01-04 18:06:42 +00001847 }
ths5fafdf22007-09-16 21:08:06 +00001848 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001849 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1850 if (map_addr == MAP_FAILED) {
1851 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1852 paddr, vaddr);
1853 }
bellard9fa3e852004-01-04 18:06:42 +00001854 }
1855 }
1856 }
1857#endif
1858 return ret;
1859}
1860
1861/* called from signal handler: invalidate the code and unprotect the
1862 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001863int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001864{
1865#if !defined(CONFIG_SOFTMMU)
1866 VirtPageDesc *vp;
1867
1868#if defined(DEBUG_TLB)
1869 printf("page_unprotect: addr=0x%08x\n", addr);
1870#endif
1871 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001872
1873 /* if it is not mapped, no need to worry here */
1874 if (addr >= MMAP_AREA_END)
1875 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001876 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1877 if (!vp)
1878 return 0;
1879 /* NOTE: in this case, validate_tag is _not_ tested as it
1880 validates only the code TLB */
1881 if (vp->valid_tag != virt_valid_tag)
1882 return 0;
1883 if (!(vp->prot & PAGE_WRITE))
1884 return 0;
1885#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001886 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001887 addr, vp->phys_addr, vp->prot);
1888#endif
bellard59817cc2004-02-16 22:01:13 +00001889 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1890 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1891 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001892 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001893 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001894 /* flush the code inside */
1895 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001896 return 1;
1897#else
1898 return 0;
1899#endif
bellard33417e72003-08-10 21:47:01 +00001900}
1901
bellard01243112004-01-04 15:48:17 +00001902#else
1903
bellardee8b7022004-02-03 23:35:10 +00001904void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001905{
1906}
1907
bellard2e126692004-04-25 21:28:44 +00001908void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001909{
1910}
1911
ths5fafdf22007-09-16 21:08:06 +00001912int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1913 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001914 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001915{
bellard9fa3e852004-01-04 18:06:42 +00001916 return 0;
1917}
bellard33417e72003-08-10 21:47:01 +00001918
bellard9fa3e852004-01-04 18:06:42 +00001919/* dump memory mappings */
1920void page_dump(FILE *f)
1921{
1922 unsigned long start, end;
1923 int i, j, prot, prot1;
1924 PageDesc *p;
1925
1926 fprintf(f, "%-8s %-8s %-8s %s\n",
1927 "start", "end", "size", "prot");
1928 start = -1;
1929 end = -1;
1930 prot = 0;
1931 for(i = 0; i <= L1_SIZE; i++) {
1932 if (i < L1_SIZE)
1933 p = l1_map[i];
1934 else
1935 p = NULL;
1936 for(j = 0;j < L2_SIZE; j++) {
1937 if (!p)
1938 prot1 = 0;
1939 else
1940 prot1 = p[j].flags;
1941 if (prot1 != prot) {
1942 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1943 if (start != -1) {
1944 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001945 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001946 prot & PAGE_READ ? 'r' : '-',
1947 prot & PAGE_WRITE ? 'w' : '-',
1948 prot & PAGE_EXEC ? 'x' : '-');
1949 }
1950 if (prot1 != 0)
1951 start = end;
1952 else
1953 start = -1;
1954 prot = prot1;
1955 }
1956 if (!p)
1957 break;
1958 }
bellard33417e72003-08-10 21:47:01 +00001959 }
bellard33417e72003-08-10 21:47:01 +00001960}
1961
pbrook53a59602006-03-25 19:31:22 +00001962int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001963{
bellard9fa3e852004-01-04 18:06:42 +00001964 PageDesc *p;
1965
1966 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001967 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001968 return 0;
1969 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001970}
1971
bellard9fa3e852004-01-04 18:06:42 +00001972/* modify the flags of a page and invalidate the code if
1973 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1974 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001975void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001976{
1977 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001978 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001979
1980 start = start & TARGET_PAGE_MASK;
1981 end = TARGET_PAGE_ALIGN(end);
1982 if (flags & PAGE_WRITE)
1983 flags |= PAGE_WRITE_ORG;
1984 spin_lock(&tb_lock);
1985 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1986 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1987 /* if the write protection is set, then we invalidate the code
1988 inside */
ths5fafdf22007-09-16 21:08:06 +00001989 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001990 (flags & PAGE_WRITE) &&
1991 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001992 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001993 }
1994 p->flags = flags;
1995 }
1996 spin_unlock(&tb_lock);
1997}
1998
ths3d97b402007-11-02 19:02:07 +00001999int page_check_range(target_ulong start, target_ulong len, int flags)
2000{
2001 PageDesc *p;
2002 target_ulong end;
2003 target_ulong addr;
2004
2005 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2006 start = start & TARGET_PAGE_MASK;
2007
2008 if( end < start )
2009 /* we've wrapped around */
2010 return -1;
2011 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2012 p = page_find(addr >> TARGET_PAGE_BITS);
2013 if( !p )
2014 return -1;
2015 if( !(p->flags & PAGE_VALID) )
2016 return -1;
2017
bellarddae32702007-11-14 10:51:00 +00002018 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002019 return -1;
bellarddae32702007-11-14 10:51:00 +00002020 if (flags & PAGE_WRITE) {
2021 if (!(p->flags & PAGE_WRITE_ORG))
2022 return -1;
2023 /* unprotect the page if it was put read-only because it
2024 contains translated code */
2025 if (!(p->flags & PAGE_WRITE)) {
2026 if (!page_unprotect(addr, 0, NULL))
2027 return -1;
2028 }
2029 return 0;
2030 }
ths3d97b402007-11-02 19:02:07 +00002031 }
2032 return 0;
2033}
2034
bellard9fa3e852004-01-04 18:06:42 +00002035/* called from signal handler: invalidate the code and unprotect the
2036 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002037int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002038{
2039 unsigned int page_index, prot, pindex;
2040 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002041 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002042
bellard83fb7ad2004-07-05 21:25:26 +00002043 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002044 page_index = host_start >> TARGET_PAGE_BITS;
2045 p1 = page_find(page_index);
2046 if (!p1)
2047 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00002048 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002049 p = p1;
2050 prot = 0;
2051 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2052 prot |= p->flags;
2053 p++;
2054 }
2055 /* if the page was really writable, then we change its
2056 protection back to writable */
2057 if (prot & PAGE_WRITE_ORG) {
2058 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2059 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002060 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002061 (prot & PAGE_BITS) | PAGE_WRITE);
2062 p1[pindex].flags |= PAGE_WRITE;
2063 /* and since the content will be modified, we must invalidate
2064 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002065 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002066#ifdef DEBUG_TB_CHECK
2067 tb_invalidate_check(address);
2068#endif
2069 return 1;
2070 }
2071 }
2072 return 0;
2073}
2074
bellard6a00d602005-11-21 23:25:50 +00002075static inline void tlb_set_dirty(CPUState *env,
2076 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002077{
2078}
bellard9fa3e852004-01-04 18:06:42 +00002079#endif /* defined(CONFIG_USER_ONLY) */
2080
blueswir1db7b5422007-05-26 17:36:03 +00002081static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002082 ram_addr_t memory);
2083static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2084 ram_addr_t orig_memory);
blueswir1db7b5422007-05-26 17:36:03 +00002085#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2086 need_subpage) \
2087 do { \
2088 if (addr > start_addr) \
2089 start_addr2 = 0; \
2090 else { \
2091 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2092 if (start_addr2 > 0) \
2093 need_subpage = 1; \
2094 } \
2095 \
blueswir149e9fba2007-05-30 17:25:06 +00002096 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002097 end_addr2 = TARGET_PAGE_SIZE - 1; \
2098 else { \
2099 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2100 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2101 need_subpage = 1; \
2102 } \
2103 } while (0)
2104
bellard33417e72003-08-10 21:47:01 +00002105/* register physical memory. 'size' must be a multiple of the target
2106 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2107 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002108void cpu_register_physical_memory(target_phys_addr_t start_addr,
aurel3200f82b82008-04-27 21:12:55 +00002109 ram_addr_t size,
2110 ram_addr_t phys_offset)
bellard33417e72003-08-10 21:47:01 +00002111{
bellard108c49b2005-07-24 12:55:09 +00002112 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002113 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002114 CPUState *env;
aurel3200f82b82008-04-27 21:12:55 +00002115 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002116 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002117
bellard5fd386f2004-05-23 21:11:22 +00002118 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002119 end_addr = start_addr + (target_phys_addr_t)size;
2120 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002121 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2122 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
aurel3200f82b82008-04-27 21:12:55 +00002123 ram_addr_t orig_memory = p->phys_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002124 target_phys_addr_t start_addr2, end_addr2;
2125 int need_subpage = 0;
2126
2127 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2128 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002129 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002130 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2131 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2132 &p->phys_offset, orig_memory);
2133 } else {
2134 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2135 >> IO_MEM_SHIFT];
2136 }
2137 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2138 } else {
2139 p->phys_offset = phys_offset;
2140 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2141 (phys_offset & IO_MEM_ROMD))
2142 phys_offset += TARGET_PAGE_SIZE;
2143 }
2144 } else {
2145 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2146 p->phys_offset = phys_offset;
2147 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2148 (phys_offset & IO_MEM_ROMD))
2149 phys_offset += TARGET_PAGE_SIZE;
2150 else {
2151 target_phys_addr_t start_addr2, end_addr2;
2152 int need_subpage = 0;
2153
2154 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2155 end_addr2, need_subpage);
2156
blueswir14254fab2008-01-01 16:57:19 +00002157 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002158 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2159 &p->phys_offset, IO_MEM_UNASSIGNED);
2160 subpage_register(subpage, start_addr2, end_addr2,
2161 phys_offset);
2162 }
2163 }
2164 }
bellard33417e72003-08-10 21:47:01 +00002165 }
ths3b46e622007-09-17 08:09:54 +00002166
bellard9d420372006-06-25 22:25:22 +00002167 /* since each CPU stores ram addresses in its TLB cache, we must
2168 reset the modified entries */
2169 /* XXX: slow ! */
2170 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2171 tlb_flush(env, 1);
2172 }
bellard33417e72003-08-10 21:47:01 +00002173}
2174
bellardba863452006-09-24 18:41:10 +00002175/* XXX: temporary until new memory mapping API */
aurel3200f82b82008-04-27 21:12:55 +00002176ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002177{
2178 PhysPageDesc *p;
2179
2180 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2181 if (!p)
2182 return IO_MEM_UNASSIGNED;
2183 return p->phys_offset;
2184}
2185
bellarde9a1ab12007-02-08 23:08:38 +00002186/* XXX: better than nothing */
aurel3200f82b82008-04-27 21:12:55 +00002187ram_addr_t qemu_ram_alloc(ram_addr_t size)
bellarde9a1ab12007-02-08 23:08:38 +00002188{
2189 ram_addr_t addr;
balrog7fb4fdc2008-04-24 17:59:27 +00002190 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
bellarded441462008-05-23 11:56:45 +00002191 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 "\n",
2192 (uint64_t)size, (uint64_t)phys_ram_size);
bellarde9a1ab12007-02-08 23:08:38 +00002193 abort();
2194 }
2195 addr = phys_ram_alloc_offset;
2196 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2197 return addr;
2198}
2199
2200void qemu_ram_free(ram_addr_t addr)
2201{
2202}
2203
bellarda4193c82004-06-03 14:01:43 +00002204static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002205{
pbrook67d3b952006-12-18 05:03:52 +00002206#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002207 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002208#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002209#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002210 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002211#elif TARGET_CRIS
2212 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002213#endif
bellard33417e72003-08-10 21:47:01 +00002214 return 0;
2215}
2216
bellarda4193c82004-06-03 14:01:43 +00002217static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002218{
pbrook67d3b952006-12-18 05:03:52 +00002219#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002220 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002221#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002222#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002223 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002224#elif TARGET_CRIS
2225 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002226#endif
bellard33417e72003-08-10 21:47:01 +00002227}
2228
2229static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2230 unassigned_mem_readb,
2231 unassigned_mem_readb,
2232 unassigned_mem_readb,
2233};
2234
2235static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2236 unassigned_mem_writeb,
2237 unassigned_mem_writeb,
2238 unassigned_mem_writeb,
2239};
2240
bellarda4193c82004-06-03 14:01:43 +00002241static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002242{
bellard3a7d9292005-08-21 09:26:42 +00002243 unsigned long ram_addr;
2244 int dirty_flags;
2245 ram_addr = addr - (unsigned long)phys_ram_base;
2246 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2247 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2248#if !defined(CONFIG_USER_ONLY)
2249 tb_invalidate_phys_page_fast(ram_addr, 1);
2250 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2251#endif
2252 }
bellardc27004e2005-01-03 23:35:10 +00002253 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002254#ifdef USE_KQEMU
2255 if (cpu_single_env->kqemu_enabled &&
2256 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2257 kqemu_modify_page(cpu_single_env, ram_addr);
2258#endif
bellardf23db162005-08-21 19:12:28 +00002259 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2260 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2261 /* we remove the notdirty callback only if the code has been
2262 flushed */
2263 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002264 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002265}
2266
bellarda4193c82004-06-03 14:01:43 +00002267static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002268{
bellard3a7d9292005-08-21 09:26:42 +00002269 unsigned long ram_addr;
2270 int dirty_flags;
2271 ram_addr = addr - (unsigned long)phys_ram_base;
2272 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2273 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2274#if !defined(CONFIG_USER_ONLY)
2275 tb_invalidate_phys_page_fast(ram_addr, 2);
2276 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2277#endif
2278 }
bellardc27004e2005-01-03 23:35:10 +00002279 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002280#ifdef USE_KQEMU
2281 if (cpu_single_env->kqemu_enabled &&
2282 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2283 kqemu_modify_page(cpu_single_env, ram_addr);
2284#endif
bellardf23db162005-08-21 19:12:28 +00002285 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2286 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2287 /* we remove the notdirty callback only if the code has been
2288 flushed */
2289 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002290 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002291}
2292
bellarda4193c82004-06-03 14:01:43 +00002293static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002294{
bellard3a7d9292005-08-21 09:26:42 +00002295 unsigned long ram_addr;
2296 int dirty_flags;
2297 ram_addr = addr - (unsigned long)phys_ram_base;
2298 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2299 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2300#if !defined(CONFIG_USER_ONLY)
2301 tb_invalidate_phys_page_fast(ram_addr, 4);
2302 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2303#endif
2304 }
bellardc27004e2005-01-03 23:35:10 +00002305 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002306#ifdef USE_KQEMU
2307 if (cpu_single_env->kqemu_enabled &&
2308 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2309 kqemu_modify_page(cpu_single_env, ram_addr);
2310#endif
bellardf23db162005-08-21 19:12:28 +00002311 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2312 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2313 /* we remove the notdirty callback only if the code has been
2314 flushed */
2315 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002316 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002317}
2318
bellard3a7d9292005-08-21 09:26:42 +00002319static CPUReadMemoryFunc *error_mem_read[3] = {
2320 NULL, /* never used */
2321 NULL, /* never used */
2322 NULL, /* never used */
2323};
2324
bellard1ccde1c2004-02-06 19:46:14 +00002325static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2326 notdirty_mem_writeb,
2327 notdirty_mem_writew,
2328 notdirty_mem_writel,
2329};
2330
pbrook6658ffb2007-03-16 23:58:11 +00002331#if defined(CONFIG_SOFTMMU)
2332/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2333 so these check for a hit then pass through to the normal out-of-line
2334 phys routines. */
2335static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2336{
2337 return ldub_phys(addr);
2338}
2339
2340static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2341{
2342 return lduw_phys(addr);
2343}
2344
2345static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2346{
2347 return ldl_phys(addr);
2348}
2349
2350/* Generate a debug exception if a watchpoint has been hit.
2351 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002352 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002353static target_ulong check_watchpoint(target_phys_addr_t addr)
2354{
2355 CPUState *env = cpu_single_env;
2356 target_ulong watch;
2357 target_ulong retaddr;
2358 int i;
2359
2360 retaddr = addr;
2361 for (i = 0; i < env->nb_watchpoints; i++) {
2362 watch = env->watchpoint[i].vaddr;
2363 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002364 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002365 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2366 cpu_single_env->watchpoint_hit = i + 1;
2367 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2368 break;
2369 }
2370 }
2371 }
2372 return retaddr;
2373}
2374
2375static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2376 uint32_t val)
2377{
2378 addr = check_watchpoint(addr);
2379 stb_phys(addr, val);
2380}
2381
2382static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2383 uint32_t val)
2384{
2385 addr = check_watchpoint(addr);
2386 stw_phys(addr, val);
2387}
2388
2389static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2390 uint32_t val)
2391{
2392 addr = check_watchpoint(addr);
2393 stl_phys(addr, val);
2394}
2395
2396static CPUReadMemoryFunc *watch_mem_read[3] = {
2397 watch_mem_readb,
2398 watch_mem_readw,
2399 watch_mem_readl,
2400};
2401
2402static CPUWriteMemoryFunc *watch_mem_write[3] = {
2403 watch_mem_writeb,
2404 watch_mem_writew,
2405 watch_mem_writel,
2406};
2407#endif
2408
blueswir1db7b5422007-05-26 17:36:03 +00002409static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2410 unsigned int len)
2411{
blueswir1db7b5422007-05-26 17:36:03 +00002412 uint32_t ret;
2413 unsigned int idx;
2414
2415 idx = SUBPAGE_IDX(addr - mmio->base);
2416#if defined(DEBUG_SUBPAGE)
2417 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2418 mmio, len, addr, idx);
2419#endif
blueswir13ee89922008-01-02 19:45:26 +00002420 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002421
2422 return ret;
2423}
2424
2425static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2426 uint32_t value, unsigned int len)
2427{
blueswir1db7b5422007-05-26 17:36:03 +00002428 unsigned int idx;
2429
2430 idx = SUBPAGE_IDX(addr - mmio->base);
2431#if defined(DEBUG_SUBPAGE)
2432 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2433 mmio, len, addr, idx, value);
2434#endif
blueswir13ee89922008-01-02 19:45:26 +00002435 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002436}
2437
2438static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2439{
2440#if defined(DEBUG_SUBPAGE)
2441 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2442#endif
2443
2444 return subpage_readlen(opaque, addr, 0);
2445}
2446
2447static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2448 uint32_t value)
2449{
2450#if defined(DEBUG_SUBPAGE)
2451 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2452#endif
2453 subpage_writelen(opaque, addr, value, 0);
2454}
2455
2456static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2457{
2458#if defined(DEBUG_SUBPAGE)
2459 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2460#endif
2461
2462 return subpage_readlen(opaque, addr, 1);
2463}
2464
2465static void subpage_writew (void *opaque, target_phys_addr_t addr,
2466 uint32_t value)
2467{
2468#if defined(DEBUG_SUBPAGE)
2469 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2470#endif
2471 subpage_writelen(opaque, addr, value, 1);
2472}
2473
2474static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2475{
2476#if defined(DEBUG_SUBPAGE)
2477 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2478#endif
2479
2480 return subpage_readlen(opaque, addr, 2);
2481}
2482
2483static void subpage_writel (void *opaque,
2484 target_phys_addr_t addr, uint32_t value)
2485{
2486#if defined(DEBUG_SUBPAGE)
2487 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2488#endif
2489 subpage_writelen(opaque, addr, value, 2);
2490}
2491
2492static CPUReadMemoryFunc *subpage_read[] = {
2493 &subpage_readb,
2494 &subpage_readw,
2495 &subpage_readl,
2496};
2497
2498static CPUWriteMemoryFunc *subpage_write[] = {
2499 &subpage_writeb,
2500 &subpage_writew,
2501 &subpage_writel,
2502};
2503
2504static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002505 ram_addr_t memory)
blueswir1db7b5422007-05-26 17:36:03 +00002506{
2507 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002508 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002509
2510 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2511 return -1;
2512 idx = SUBPAGE_IDX(start);
2513 eidx = SUBPAGE_IDX(end);
2514#if defined(DEBUG_SUBPAGE)
2515 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2516 mmio, start, end, idx, eidx, memory);
2517#endif
2518 memory >>= IO_MEM_SHIFT;
2519 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002520 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002521 if (io_mem_read[memory][i]) {
2522 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2523 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2524 }
2525 if (io_mem_write[memory][i]) {
2526 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2527 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2528 }
blueswir14254fab2008-01-01 16:57:19 +00002529 }
blueswir1db7b5422007-05-26 17:36:03 +00002530 }
2531
2532 return 0;
2533}
2534
aurel3200f82b82008-04-27 21:12:55 +00002535static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2536 ram_addr_t orig_memory)
blueswir1db7b5422007-05-26 17:36:03 +00002537{
2538 subpage_t *mmio;
2539 int subpage_memory;
2540
2541 mmio = qemu_mallocz(sizeof(subpage_t));
2542 if (mmio != NULL) {
2543 mmio->base = base;
2544 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2545#if defined(DEBUG_SUBPAGE)
2546 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2547 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2548#endif
2549 *phys = subpage_memory | IO_MEM_SUBPAGE;
2550 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2551 }
2552
2553 return mmio;
2554}
2555
bellard33417e72003-08-10 21:47:01 +00002556static void io_mem_init(void)
2557{
bellard3a7d9292005-08-21 09:26:42 +00002558 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002559 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002560 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002561 io_mem_nb = 5;
2562
pbrook6658ffb2007-03-16 23:58:11 +00002563#if defined(CONFIG_SOFTMMU)
2564 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2565 watch_mem_write, NULL);
2566#endif
bellard1ccde1c2004-02-06 19:46:14 +00002567 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002568 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002569 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002570}
2571
2572/* mem_read and mem_write are arrays of functions containing the
2573 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002574 2). Functions can be omitted with a NULL function pointer. The
2575 registered functions may be modified dynamically later.
2576 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002577 modified. If it is zero, a new io zone is allocated. The return
2578 value can be used with cpu_register_physical_memory(). (-1) is
2579 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002580int cpu_register_io_memory(int io_index,
2581 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002582 CPUWriteMemoryFunc **mem_write,
2583 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002584{
blueswir14254fab2008-01-01 16:57:19 +00002585 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002586
2587 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002588 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002589 return -1;
2590 io_index = io_mem_nb++;
2591 } else {
2592 if (io_index >= IO_MEM_NB_ENTRIES)
2593 return -1;
2594 }
bellardb5ff1b32005-11-26 10:38:39 +00002595
bellard33417e72003-08-10 21:47:01 +00002596 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002597 if (!mem_read[i] || !mem_write[i])
2598 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002599 io_mem_read[io_index][i] = mem_read[i];
2600 io_mem_write[io_index][i] = mem_write[i];
2601 }
bellarda4193c82004-06-03 14:01:43 +00002602 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002603 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002604}
bellard61382a52003-10-27 21:22:23 +00002605
bellard8926b512004-10-10 15:14:20 +00002606CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2607{
2608 return io_mem_write[io_index >> IO_MEM_SHIFT];
2609}
2610
2611CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2612{
2613 return io_mem_read[io_index >> IO_MEM_SHIFT];
2614}
2615
bellard13eb76e2004-01-24 15:23:36 +00002616/* physical memory access (slow version, mainly for debug) */
2617#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002618void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002619 int len, int is_write)
2620{
2621 int l, flags;
2622 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002623 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002624
2625 while (len > 0) {
2626 page = addr & TARGET_PAGE_MASK;
2627 l = (page + TARGET_PAGE_SIZE) - addr;
2628 if (l > len)
2629 l = len;
2630 flags = page_get_flags(page);
2631 if (!(flags & PAGE_VALID))
2632 return;
2633 if (is_write) {
2634 if (!(flags & PAGE_WRITE))
2635 return;
bellard579a97f2007-11-11 14:26:47 +00002636 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002637 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00002638 /* FIXME - should this return an error rather than just fail? */
2639 return;
aurel3272fb7da2008-04-27 23:53:45 +00002640 memcpy(p, buf, l);
2641 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00002642 } else {
2643 if (!(flags & PAGE_READ))
2644 return;
bellard579a97f2007-11-11 14:26:47 +00002645 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002646 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00002647 /* FIXME - should this return an error rather than just fail? */
2648 return;
aurel3272fb7da2008-04-27 23:53:45 +00002649 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00002650 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002651 }
2652 len -= l;
2653 buf += l;
2654 addr += l;
2655 }
2656}
bellard8df1cd02005-01-28 22:37:22 +00002657
bellard13eb76e2004-01-24 15:23:36 +00002658#else
ths5fafdf22007-09-16 21:08:06 +00002659void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002660 int len, int is_write)
2661{
2662 int l, io_index;
2663 uint8_t *ptr;
2664 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002665 target_phys_addr_t page;
2666 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002667 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002668
bellard13eb76e2004-01-24 15:23:36 +00002669 while (len > 0) {
2670 page = addr & TARGET_PAGE_MASK;
2671 l = (page + TARGET_PAGE_SIZE) - addr;
2672 if (l > len)
2673 l = len;
bellard92e873b2004-05-21 14:52:29 +00002674 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002675 if (!p) {
2676 pd = IO_MEM_UNASSIGNED;
2677 } else {
2678 pd = p->phys_offset;
2679 }
ths3b46e622007-09-17 08:09:54 +00002680
bellard13eb76e2004-01-24 15:23:36 +00002681 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002682 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002683 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002684 /* XXX: could force cpu_single_env to NULL to avoid
2685 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002686 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002687 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002688 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002689 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002690 l = 4;
2691 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002692 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002693 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002694 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002695 l = 2;
2696 } else {
bellard1c213d12005-09-03 10:49:04 +00002697 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002698 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002699 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002700 l = 1;
2701 }
2702 } else {
bellardb448f2f2004-02-25 23:24:04 +00002703 unsigned long addr1;
2704 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002705 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002706 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002707 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002708 if (!cpu_physical_memory_is_dirty(addr1)) {
2709 /* invalidate code */
2710 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2711 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002712 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002713 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002714 }
bellard13eb76e2004-01-24 15:23:36 +00002715 }
2716 } else {
ths5fafdf22007-09-16 21:08:06 +00002717 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002718 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002719 /* I/O case */
2720 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2721 if (l >= 4 && ((addr & 3) == 0)) {
2722 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002723 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002724 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002725 l = 4;
2726 } else if (l >= 2 && ((addr & 1) == 0)) {
2727 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002728 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002729 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002730 l = 2;
2731 } else {
bellard1c213d12005-09-03 10:49:04 +00002732 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002733 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002734 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002735 l = 1;
2736 }
2737 } else {
2738 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002739 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002740 (addr & ~TARGET_PAGE_MASK);
2741 memcpy(buf, ptr, l);
2742 }
2743 }
2744 len -= l;
2745 buf += l;
2746 addr += l;
2747 }
2748}
bellard8df1cd02005-01-28 22:37:22 +00002749
bellardd0ecd2a2006-04-23 17:14:48 +00002750/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002751void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002752 const uint8_t *buf, int len)
2753{
2754 int l;
2755 uint8_t *ptr;
2756 target_phys_addr_t page;
2757 unsigned long pd;
2758 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002759
bellardd0ecd2a2006-04-23 17:14:48 +00002760 while (len > 0) {
2761 page = addr & TARGET_PAGE_MASK;
2762 l = (page + TARGET_PAGE_SIZE) - addr;
2763 if (l > len)
2764 l = len;
2765 p = phys_page_find(page >> TARGET_PAGE_BITS);
2766 if (!p) {
2767 pd = IO_MEM_UNASSIGNED;
2768 } else {
2769 pd = p->phys_offset;
2770 }
ths3b46e622007-09-17 08:09:54 +00002771
bellardd0ecd2a2006-04-23 17:14:48 +00002772 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002773 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2774 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002775 /* do nothing */
2776 } else {
2777 unsigned long addr1;
2778 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2779 /* ROM/RAM case */
2780 ptr = phys_ram_base + addr1;
2781 memcpy(ptr, buf, l);
2782 }
2783 len -= l;
2784 buf += l;
2785 addr += l;
2786 }
2787}
2788
2789
bellard8df1cd02005-01-28 22:37:22 +00002790/* warning: addr must be aligned */
2791uint32_t ldl_phys(target_phys_addr_t addr)
2792{
2793 int io_index;
2794 uint8_t *ptr;
2795 uint32_t val;
2796 unsigned long pd;
2797 PhysPageDesc *p;
2798
2799 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2800 if (!p) {
2801 pd = IO_MEM_UNASSIGNED;
2802 } else {
2803 pd = p->phys_offset;
2804 }
ths3b46e622007-09-17 08:09:54 +00002805
ths5fafdf22007-09-16 21:08:06 +00002806 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002807 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002808 /* I/O case */
2809 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2810 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2811 } else {
2812 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002813 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002814 (addr & ~TARGET_PAGE_MASK);
2815 val = ldl_p(ptr);
2816 }
2817 return val;
2818}
2819
bellard84b7b8e2005-11-28 21:19:04 +00002820/* warning: addr must be aligned */
2821uint64_t ldq_phys(target_phys_addr_t addr)
2822{
2823 int io_index;
2824 uint8_t *ptr;
2825 uint64_t val;
2826 unsigned long pd;
2827 PhysPageDesc *p;
2828
2829 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2830 if (!p) {
2831 pd = IO_MEM_UNASSIGNED;
2832 } else {
2833 pd = p->phys_offset;
2834 }
ths3b46e622007-09-17 08:09:54 +00002835
bellard2a4188a2006-06-25 21:54:59 +00002836 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2837 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002838 /* I/O case */
2839 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2840#ifdef TARGET_WORDS_BIGENDIAN
2841 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2842 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2843#else
2844 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2845 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2846#endif
2847 } else {
2848 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002849 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002850 (addr & ~TARGET_PAGE_MASK);
2851 val = ldq_p(ptr);
2852 }
2853 return val;
2854}
2855
bellardaab33092005-10-30 20:48:42 +00002856/* XXX: optimize */
2857uint32_t ldub_phys(target_phys_addr_t addr)
2858{
2859 uint8_t val;
2860 cpu_physical_memory_read(addr, &val, 1);
2861 return val;
2862}
2863
2864/* XXX: optimize */
2865uint32_t lduw_phys(target_phys_addr_t addr)
2866{
2867 uint16_t val;
2868 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2869 return tswap16(val);
2870}
2871
bellard8df1cd02005-01-28 22:37:22 +00002872/* warning: addr must be aligned. The ram page is not masked as dirty
2873 and the code inside is not invalidated. It is useful if the dirty
2874 bits are used to track modified PTEs */
2875void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2876{
2877 int io_index;
2878 uint8_t *ptr;
2879 unsigned long pd;
2880 PhysPageDesc *p;
2881
2882 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2883 if (!p) {
2884 pd = IO_MEM_UNASSIGNED;
2885 } else {
2886 pd = p->phys_offset;
2887 }
ths3b46e622007-09-17 08:09:54 +00002888
bellard3a7d9292005-08-21 09:26:42 +00002889 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002890 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2891 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2892 } else {
ths5fafdf22007-09-16 21:08:06 +00002893 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002894 (addr & ~TARGET_PAGE_MASK);
2895 stl_p(ptr, val);
2896 }
2897}
2898
j_mayerbc98a7e2007-04-04 07:55:12 +00002899void stq_phys_notdirty(target_phys_addr_t addr, uint64_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
j_mayerbc98a7e2007-04-04 07:55:12 +00002913 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2914 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2915#ifdef TARGET_WORDS_BIGENDIAN
2916 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2917 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2918#else
2919 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2920 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2921#endif
2922 } else {
ths5fafdf22007-09-16 21:08:06 +00002923 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002924 (addr & ~TARGET_PAGE_MASK);
2925 stq_p(ptr, val);
2926 }
2927}
2928
bellard8df1cd02005-01-28 22:37:22 +00002929/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002930void stl_phys(target_phys_addr_t addr, uint32_t val)
2931{
2932 int io_index;
2933 uint8_t *ptr;
2934 unsigned long pd;
2935 PhysPageDesc *p;
2936
2937 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2938 if (!p) {
2939 pd = IO_MEM_UNASSIGNED;
2940 } else {
2941 pd = p->phys_offset;
2942 }
ths3b46e622007-09-17 08:09:54 +00002943
bellard3a7d9292005-08-21 09:26:42 +00002944 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002945 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2946 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2947 } else {
2948 unsigned long addr1;
2949 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2950 /* RAM case */
2951 ptr = phys_ram_base + addr1;
2952 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002953 if (!cpu_physical_memory_is_dirty(addr1)) {
2954 /* invalidate code */
2955 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2956 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002957 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2958 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002959 }
bellard8df1cd02005-01-28 22:37:22 +00002960 }
2961}
2962
bellardaab33092005-10-30 20:48:42 +00002963/* XXX: optimize */
2964void stb_phys(target_phys_addr_t addr, uint32_t val)
2965{
2966 uint8_t v = val;
2967 cpu_physical_memory_write(addr, &v, 1);
2968}
2969
2970/* XXX: optimize */
2971void stw_phys(target_phys_addr_t addr, uint32_t val)
2972{
2973 uint16_t v = tswap16(val);
2974 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2975}
2976
2977/* XXX: optimize */
2978void stq_phys(target_phys_addr_t addr, uint64_t val)
2979{
2980 val = tswap64(val);
2981 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2982}
2983
bellard13eb76e2004-01-24 15:23:36 +00002984#endif
2985
2986/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002987int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002988 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002989{
2990 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002991 target_phys_addr_t phys_addr;
2992 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002993
2994 while (len > 0) {
2995 page = addr & TARGET_PAGE_MASK;
2996 phys_addr = cpu_get_phys_page_debug(env, page);
2997 /* if no physical page mapped, return an error */
2998 if (phys_addr == -1)
2999 return -1;
3000 l = (page + TARGET_PAGE_SIZE) - addr;
3001 if (l > len)
3002 l = len;
ths5fafdf22007-09-16 21:08:06 +00003003 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00003004 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00003005 len -= l;
3006 buf += l;
3007 addr += l;
3008 }
3009 return 0;
3010}
3011
bellarde3db7222005-01-26 22:00:47 +00003012void dump_exec_info(FILE *f,
3013 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3014{
3015 int i, target_code_size, max_target_code_size;
3016 int direct_jmp_count, direct_jmp2_count, cross_page;
3017 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003018
bellarde3db7222005-01-26 22:00:47 +00003019 target_code_size = 0;
3020 max_target_code_size = 0;
3021 cross_page = 0;
3022 direct_jmp_count = 0;
3023 direct_jmp2_count = 0;
3024 for(i = 0; i < nb_tbs; i++) {
3025 tb = &tbs[i];
3026 target_code_size += tb->size;
3027 if (tb->size > max_target_code_size)
3028 max_target_code_size = tb->size;
3029 if (tb->page_addr[1] != -1)
3030 cross_page++;
3031 if (tb->tb_next_offset[0] != 0xffff) {
3032 direct_jmp_count++;
3033 if (tb->tb_next_offset[1] != 0xffff) {
3034 direct_jmp2_count++;
3035 }
3036 }
3037 }
3038 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003039 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003040 cpu_fprintf(f, "gen code size %ld/%ld\n",
3041 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3042 cpu_fprintf(f, "TB count %d/%d\n",
3043 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003044 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003045 nb_tbs ? target_code_size / nb_tbs : 0,
3046 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003047 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003048 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3049 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003050 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3051 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003052 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3053 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003054 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003055 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3056 direct_jmp2_count,
3057 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003058 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003059 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3060 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3061 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003062 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003063}
3064
ths5fafdf22007-09-16 21:08:06 +00003065#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003066
3067#define MMUSUFFIX _cmmu
3068#define GETPC() NULL
3069#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003070#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003071
3072#define SHIFT 0
3073#include "softmmu_template.h"
3074
3075#define SHIFT 1
3076#include "softmmu_template.h"
3077
3078#define SHIFT 2
3079#include "softmmu_template.h"
3080
3081#define SHIFT 3
3082#include "softmmu_template.h"
3083
3084#undef env
3085
3086#endif