blob: eb3c8abf5dd28e3ab7d7ddcbb090a3719e29a43f [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"
pbrook53a59602006-03-25 19:31:22 +000039#if defined(CONFIG_USER_ONLY)
40#include <qemu.h>
41#endif
bellard54936002003-05-13 00:25:15 +000042
bellardfd6ce8f2003-05-14 19:00:11 +000043//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000044//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000045//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000046//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000047
48/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000049//#define DEBUG_TB_CHECK
50//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000051
ths1196be32007-03-17 15:17:58 +000052//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000053//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000054
pbrook99773bd2006-04-16 15:14:59 +000055#if !defined(CONFIG_USER_ONLY)
56/* TB consistency checks only implemented for usermode emulation. */
57#undef DEBUG_TB_CHECK
58#endif
59
bellardfd6ce8f2003-05-14 19:00:11 +000060/* threshold to flush the translated code buffer */
blueswir1d07bde82007-12-11 19:35:45 +000061#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - code_gen_max_block_size())
bellardfd6ce8f2003-05-14 19:00:11 +000062
bellard9fa3e852004-01-04 18:06:42 +000063#define SMC_BITMAP_USE_THRESHOLD 10
64
65#define MMAP_AREA_START 0x00000000
66#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000067
bellard108c49b2005-07-24 12:55:09 +000068#if defined(TARGET_SPARC64)
69#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000070#elif defined(TARGET_SPARC)
71#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000072#elif defined(TARGET_ALPHA)
73#define TARGET_PHYS_ADDR_SPACE_BITS 42
74#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000075#elif defined(TARGET_PPC64)
76#define TARGET_PHYS_ADDR_SPACE_BITS 42
aurel3200f82b82008-04-27 21:12:55 +000077#elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
78#define TARGET_PHYS_ADDR_SPACE_BITS 42
79#elif defined(TARGET_I386) && !defined(USE_KQEMU)
80#define TARGET_PHYS_ADDR_SPACE_BITS 36
bellard108c49b2005-07-24 12:55:09 +000081#else
82/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
83#define TARGET_PHYS_ADDR_SPACE_BITS 32
84#endif
85
bellardfd6ce8f2003-05-14 19:00:11 +000086TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
bellard9fa3e852004-01-04 18:06:42 +000087TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000088int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000089/* any access to the tbs or the page table must use this lock */
90spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000091
bellardb8076a72005-04-07 22:20:31 +000092uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
bellardfd6ce8f2003-05-14 19:00:11 +000093uint8_t *code_gen_ptr;
94
aurel3200f82b82008-04-27 21:12:55 +000095ram_addr_t phys_ram_size;
bellard9fa3e852004-01-04 18:06:42 +000096int phys_ram_fd;
97uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +000098uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +000099static ram_addr_t phys_ram_alloc_offset = 0;
bellard9fa3e852004-01-04 18:06:42 +0000100
bellard6a00d602005-11-21 23:25:50 +0000101CPUState *first_cpu;
102/* current CPU in the current thread. It is only valid inside
103 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000104CPUState *cpu_single_env;
bellard6a00d602005-11-21 23:25:50 +0000105
bellard54936002003-05-13 00:25:15 +0000106typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000107 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000108 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000109 /* in order to optimize self modifying code, we count the number
110 of lookups we do to a given page to use a bitmap */
111 unsigned int code_write_count;
112 uint8_t *code_bitmap;
113#if defined(CONFIG_USER_ONLY)
114 unsigned long flags;
115#endif
bellard54936002003-05-13 00:25:15 +0000116} PageDesc;
117
bellard92e873b2004-05-21 14:52:29 +0000118typedef struct PhysPageDesc {
119 /* offset in host memory of the page + io_index in the low 12 bits */
aurel3200f82b82008-04-27 21:12:55 +0000120 ram_addr_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000121} PhysPageDesc;
122
bellard54936002003-05-13 00:25:15 +0000123#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000124#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
125/* XXX: this is a temporary hack for alpha target.
126 * In the future, this is to be replaced by a multi-level table
127 * to actually be able to handle the complete 64 bits address space.
128 */
129#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
130#else
aurel3203875442008-04-22 20:45:18 +0000131#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000132#endif
bellard54936002003-05-13 00:25:15 +0000133
134#define L1_SIZE (1 << L1_BITS)
135#define L2_SIZE (1 << L2_BITS)
136
bellard33417e72003-08-10 21:47:01 +0000137static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000138
bellard83fb7ad2004-07-05 21:25:26 +0000139unsigned long qemu_real_host_page_size;
140unsigned long qemu_host_page_bits;
141unsigned long qemu_host_page_size;
142unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000143
bellard92e873b2004-05-21 14:52:29 +0000144/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000145static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000146PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000147
bellard33417e72003-08-10 21:47:01 +0000148/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000149CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
150CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000151void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000152static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000153#if defined(CONFIG_SOFTMMU)
154static int io_mem_watch;
155#endif
bellard33417e72003-08-10 21:47:01 +0000156
bellard34865132003-10-05 14:28:56 +0000157/* log support */
158char *logfilename = "/tmp/qemu.log";
159FILE *logfile;
160int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000161static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000162
bellarde3db7222005-01-26 22:00:47 +0000163/* statistics */
164static int tlb_flush_count;
165static int tb_flush_count;
166static int tb_phys_invalidate_count;
167
blueswir1db7b5422007-05-26 17:36:03 +0000168#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
169typedef struct subpage_t {
170 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000171 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
172 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
173 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000174} subpage_t;
175
bellardb346ff42003-06-15 20:05:50 +0000176static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000177{
bellard83fb7ad2004-07-05 21:25:26 +0000178 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000179 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000180#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000181 {
182 SYSTEM_INFO system_info;
183 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000184
bellardd5a8f072004-09-29 21:15:28 +0000185 GetSystemInfo(&system_info);
186 qemu_real_host_page_size = system_info.dwPageSize;
ths3b46e622007-09-17 08:09:54 +0000187
bellardd5a8f072004-09-29 21:15:28 +0000188 VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
189 PAGE_EXECUTE_READWRITE, &old_protect);
190 }
bellard67b915a2004-03-31 23:37:16 +0000191#else
bellard83fb7ad2004-07-05 21:25:26 +0000192 qemu_real_host_page_size = getpagesize();
bellardd5a8f072004-09-29 21:15:28 +0000193 {
194 unsigned long start, end;
195
196 start = (unsigned long)code_gen_buffer;
197 start &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000198
bellardd5a8f072004-09-29 21:15:28 +0000199 end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
200 end += qemu_real_host_page_size - 1;
201 end &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000202
ths5fafdf22007-09-16 21:08:06 +0000203 mprotect((void *)start, end - start,
bellardd5a8f072004-09-29 21:15:28 +0000204 PROT_READ | PROT_WRITE | PROT_EXEC);
205 }
bellard67b915a2004-03-31 23:37:16 +0000206#endif
bellardd5a8f072004-09-29 21:15:28 +0000207
bellard83fb7ad2004-07-05 21:25:26 +0000208 if (qemu_host_page_size == 0)
209 qemu_host_page_size = qemu_real_host_page_size;
210 if (qemu_host_page_size < TARGET_PAGE_SIZE)
211 qemu_host_page_size = TARGET_PAGE_SIZE;
212 qemu_host_page_bits = 0;
213 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
214 qemu_host_page_bits++;
215 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000216 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
217 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000218
219#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
220 {
221 long long startaddr, endaddr;
222 FILE *f;
223 int n;
224
225 f = fopen("/proc/self/maps", "r");
226 if (f) {
227 do {
228 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
229 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000230 startaddr = MIN(startaddr,
231 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
232 endaddr = MIN(endaddr,
233 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
balrog50a95692007-12-12 01:16:23 +0000234 page_set_flags(TARGET_PAGE_ALIGN(startaddr),
235 TARGET_PAGE_ALIGN(endaddr),
236 PAGE_RESERVED);
237 }
238 } while (!feof(f));
239 fclose(f);
240 }
241 }
242#endif
bellard54936002003-05-13 00:25:15 +0000243}
244
aurel3200f82b82008-04-27 21:12:55 +0000245static inline PageDesc *page_find_alloc(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000246{
bellard54936002003-05-13 00:25:15 +0000247 PageDesc **lp, *p;
248
bellard54936002003-05-13 00:25:15 +0000249 lp = &l1_map[index >> L2_BITS];
250 p = *lp;
251 if (!p) {
252 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000253 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000254 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000255 *lp = p;
256 }
257 return p + (index & (L2_SIZE - 1));
258}
259
aurel3200f82b82008-04-27 21:12:55 +0000260static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000261{
bellard54936002003-05-13 00:25:15 +0000262 PageDesc *p;
263
bellard54936002003-05-13 00:25:15 +0000264 p = l1_map[index >> L2_BITS];
265 if (!p)
266 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000267 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000268}
269
bellard108c49b2005-07-24 12:55:09 +0000270static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000271{
bellard108c49b2005-07-24 12:55:09 +0000272 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000273 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000274
bellard108c49b2005-07-24 12:55:09 +0000275 p = (void **)l1_phys_map;
276#if TARGET_PHYS_ADDR_SPACE_BITS > 32
277
278#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
279#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
280#endif
281 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000282 p = *lp;
283 if (!p) {
284 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000285 if (!alloc)
286 return NULL;
287 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
288 memset(p, 0, sizeof(void *) * L1_SIZE);
289 *lp = p;
290 }
291#endif
292 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000293 pd = *lp;
294 if (!pd) {
295 int i;
bellard108c49b2005-07-24 12:55:09 +0000296 /* allocate if not found */
297 if (!alloc)
298 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000299 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
300 *lp = pd;
301 for (i = 0; i < L2_SIZE; i++)
302 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000303 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000304 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000305}
306
bellard108c49b2005-07-24 12:55:09 +0000307static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000308{
bellard108c49b2005-07-24 12:55:09 +0000309 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000310}
311
bellard9fa3e852004-01-04 18:06:42 +0000312#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000313static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000314static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000315 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000316#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000317
bellard6a00d602005-11-21 23:25:50 +0000318void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000319{
bellard6a00d602005-11-21 23:25:50 +0000320 CPUState **penv;
321 int cpu_index;
322
bellardfd6ce8f2003-05-14 19:00:11 +0000323 if (!code_gen_ptr) {
bellard57fec1f2008-02-01 10:50:11 +0000324 cpu_gen_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000325 code_gen_ptr = code_gen_buffer;
bellardb346ff42003-06-15 20:05:50 +0000326 page_init();
bellard33417e72003-08-10 21:47:01 +0000327 io_mem_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000328 }
bellard6a00d602005-11-21 23:25:50 +0000329 env->next_cpu = NULL;
330 penv = &first_cpu;
331 cpu_index = 0;
332 while (*penv != NULL) {
333 penv = (CPUState **)&(*penv)->next_cpu;
334 cpu_index++;
335 }
336 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000337 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000338 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000339}
340
bellard9fa3e852004-01-04 18:06:42 +0000341static inline void invalidate_page_bitmap(PageDesc *p)
342{
343 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000344 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000345 p->code_bitmap = NULL;
346 }
347 p->code_write_count = 0;
348}
349
bellardfd6ce8f2003-05-14 19:00:11 +0000350/* set to NULL all the 'first_tb' fields in all PageDescs */
351static void page_flush_tb(void)
352{
353 int i, j;
354 PageDesc *p;
355
356 for(i = 0; i < L1_SIZE; i++) {
357 p = l1_map[i];
358 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000359 for(j = 0; j < L2_SIZE; j++) {
360 p->first_tb = NULL;
361 invalidate_page_bitmap(p);
362 p++;
363 }
bellardfd6ce8f2003-05-14 19:00:11 +0000364 }
365 }
366}
367
368/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000369/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000370void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000371{
bellard6a00d602005-11-21 23:25:50 +0000372 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000373#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000374 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
375 (unsigned long)(code_gen_ptr - code_gen_buffer),
376 nb_tbs, nb_tbs > 0 ?
377 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000378#endif
pbrooka208e542008-03-31 17:07:36 +0000379 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > CODE_GEN_BUFFER_SIZE)
380 cpu_abort(env1, "Internal error: code buffer overflow\n");
381
bellardfd6ce8f2003-05-14 19:00:11 +0000382 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000383
bellard6a00d602005-11-21 23:25:50 +0000384 for(env = first_cpu; env != NULL; env = env->next_cpu) {
385 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
386 }
bellard9fa3e852004-01-04 18:06:42 +0000387
bellard8a8a6082004-10-03 13:36:49 +0000388 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000389 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000390
bellardfd6ce8f2003-05-14 19:00:11 +0000391 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000392 /* XXX: flush processor icache at this point if cache flush is
393 expensive */
bellarde3db7222005-01-26 22:00:47 +0000394 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000395}
396
397#ifdef DEBUG_TB_CHECK
398
j_mayerbc98a7e2007-04-04 07:55:12 +0000399static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000400{
401 TranslationBlock *tb;
402 int i;
403 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000404 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
405 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000406 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
407 address >= tb->pc + tb->size)) {
408 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000409 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000410 }
411 }
412 }
413}
414
415/* verify that all the pages have correct rights for code */
416static void tb_page_check(void)
417{
418 TranslationBlock *tb;
419 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000420
pbrook99773bd2006-04-16 15:14:59 +0000421 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
422 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000423 flags1 = page_get_flags(tb->pc);
424 flags2 = page_get_flags(tb->pc + tb->size - 1);
425 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
426 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000427 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000428 }
429 }
430 }
431}
432
bellardd4e81642003-05-25 16:46:15 +0000433void tb_jmp_check(TranslationBlock *tb)
434{
435 TranslationBlock *tb1;
436 unsigned int n1;
437
438 /* suppress any remaining jumps to this TB */
439 tb1 = tb->jmp_first;
440 for(;;) {
441 n1 = (long)tb1 & 3;
442 tb1 = (TranslationBlock *)((long)tb1 & ~3);
443 if (n1 == 2)
444 break;
445 tb1 = tb1->jmp_next[n1];
446 }
447 /* check end of list */
448 if (tb1 != tb) {
449 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
450 }
451}
452
bellardfd6ce8f2003-05-14 19:00:11 +0000453#endif
454
455/* invalidate one TB */
456static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
457 int next_offset)
458{
459 TranslationBlock *tb1;
460 for(;;) {
461 tb1 = *ptb;
462 if (tb1 == tb) {
463 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
464 break;
465 }
466 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
467 }
468}
469
bellard9fa3e852004-01-04 18:06:42 +0000470static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
471{
472 TranslationBlock *tb1;
473 unsigned int n1;
474
475 for(;;) {
476 tb1 = *ptb;
477 n1 = (long)tb1 & 3;
478 tb1 = (TranslationBlock *)((long)tb1 & ~3);
479 if (tb1 == tb) {
480 *ptb = tb1->page_next[n1];
481 break;
482 }
483 ptb = &tb1->page_next[n1];
484 }
485}
486
bellardd4e81642003-05-25 16:46:15 +0000487static inline void tb_jmp_remove(TranslationBlock *tb, int n)
488{
489 TranslationBlock *tb1, **ptb;
490 unsigned int n1;
491
492 ptb = &tb->jmp_next[n];
493 tb1 = *ptb;
494 if (tb1) {
495 /* find tb(n) in circular list */
496 for(;;) {
497 tb1 = *ptb;
498 n1 = (long)tb1 & 3;
499 tb1 = (TranslationBlock *)((long)tb1 & ~3);
500 if (n1 == n && tb1 == tb)
501 break;
502 if (n1 == 2) {
503 ptb = &tb1->jmp_first;
504 } else {
505 ptb = &tb1->jmp_next[n1];
506 }
507 }
508 /* now we can suppress tb(n) from the list */
509 *ptb = tb->jmp_next[n];
510
511 tb->jmp_next[n] = NULL;
512 }
513}
514
515/* reset the jump entry 'n' of a TB so that it is not chained to
516 another TB */
517static inline void tb_reset_jump(TranslationBlock *tb, int n)
518{
519 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
520}
521
aurel3200f82b82008-04-27 21:12:55 +0000522static inline void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000523{
bellard6a00d602005-11-21 23:25:50 +0000524 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000525 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000526 unsigned int h, n1;
aurel3200f82b82008-04-27 21:12:55 +0000527 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000528 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000529
bellard9fa3e852004-01-04 18:06:42 +0000530 /* remove the TB from the hash list */
531 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
532 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000533 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000534 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000535
bellard9fa3e852004-01-04 18:06:42 +0000536 /* remove the TB from the page list */
537 if (tb->page_addr[0] != page_addr) {
538 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
539 tb_page_remove(&p->first_tb, tb);
540 invalidate_page_bitmap(p);
541 }
542 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
543 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
544 tb_page_remove(&p->first_tb, tb);
545 invalidate_page_bitmap(p);
546 }
547
bellard8a40a182005-11-20 10:35:40 +0000548 tb_invalidated_flag = 1;
549
550 /* remove the TB from the hash list */
551 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000552 for(env = first_cpu; env != NULL; env = env->next_cpu) {
553 if (env->tb_jmp_cache[h] == tb)
554 env->tb_jmp_cache[h] = NULL;
555 }
bellard8a40a182005-11-20 10:35:40 +0000556
557 /* suppress this TB from the two jump lists */
558 tb_jmp_remove(tb, 0);
559 tb_jmp_remove(tb, 1);
560
561 /* suppress any remaining jumps to this TB */
562 tb1 = tb->jmp_first;
563 for(;;) {
564 n1 = (long)tb1 & 3;
565 if (n1 == 2)
566 break;
567 tb1 = (TranslationBlock *)((long)tb1 & ~3);
568 tb2 = tb1->jmp_next[n1];
569 tb_reset_jump(tb1, n1);
570 tb1->jmp_next[n1] = NULL;
571 tb1 = tb2;
572 }
573 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
574
bellarde3db7222005-01-26 22:00:47 +0000575 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000576}
577
578static inline void set_bits(uint8_t *tab, int start, int len)
579{
580 int end, mask, end1;
581
582 end = start + len;
583 tab += start >> 3;
584 mask = 0xff << (start & 7);
585 if ((start & ~7) == (end & ~7)) {
586 if (start < end) {
587 mask &= ~(0xff << (end & 7));
588 *tab |= mask;
589 }
590 } else {
591 *tab++ |= mask;
592 start = (start + 8) & ~7;
593 end1 = end & ~7;
594 while (start < end1) {
595 *tab++ = 0xff;
596 start += 8;
597 }
598 if (start < end) {
599 mask = ~(0xff << (end & 7));
600 *tab |= mask;
601 }
602 }
603}
604
605static void build_page_bitmap(PageDesc *p)
606{
607 int n, tb_start, tb_end;
608 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000609
bellard59817cc2004-02-16 22:01:13 +0000610 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000611 if (!p->code_bitmap)
612 return;
613 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
614
615 tb = p->first_tb;
616 while (tb != NULL) {
617 n = (long)tb & 3;
618 tb = (TranslationBlock *)((long)tb & ~3);
619 /* NOTE: this is subtle as a TB may span two physical pages */
620 if (n == 0) {
621 /* NOTE: tb_end may be after the end of the page, but
622 it is not a problem */
623 tb_start = tb->pc & ~TARGET_PAGE_MASK;
624 tb_end = tb_start + tb->size;
625 if (tb_end > TARGET_PAGE_SIZE)
626 tb_end = TARGET_PAGE_SIZE;
627 } else {
628 tb_start = 0;
629 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
630 }
631 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
632 tb = tb->page_next[n];
633 }
634}
635
bellardd720b932004-04-25 17:57:43 +0000636#ifdef TARGET_HAS_PRECISE_SMC
637
ths5fafdf22007-09-16 21:08:06 +0000638static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000639 target_ulong pc, target_ulong cs_base, int flags,
640 int cflags)
641{
642 TranslationBlock *tb;
643 uint8_t *tc_ptr;
644 target_ulong phys_pc, phys_page2, virt_page2;
645 int code_gen_size;
646
bellardc27004e2005-01-03 23:35:10 +0000647 phys_pc = get_phys_addr_code(env, pc);
648 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000649 if (!tb) {
650 /* flush must be done */
651 tb_flush(env);
652 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000653 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000654 }
655 tc_ptr = code_gen_ptr;
656 tb->tc_ptr = tc_ptr;
657 tb->cs_base = cs_base;
658 tb->flags = flags;
659 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000660 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000661 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 +0000662
bellardd720b932004-04-25 17:57:43 +0000663 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000664 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000665 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000666 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000667 phys_page2 = get_phys_addr_code(env, virt_page2);
668 }
669 tb_link_phys(tb, phys_pc, phys_page2);
670}
671#endif
ths3b46e622007-09-17 08:09:54 +0000672
bellard9fa3e852004-01-04 18:06:42 +0000673/* invalidate all TBs which intersect with the target physical page
674 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000675 the same physical page. 'is_cpu_write_access' should be true if called
676 from a real cpu write access: the virtual CPU will exit the current
677 TB if code is modified inside this TB. */
aurel3200f82b82008-04-27 21:12:55 +0000678void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000679 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000680{
bellardd720b932004-04-25 17:57:43 +0000681 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000682 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000683 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000684 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000685 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000686 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000687
688 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000689 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000690 return;
ths5fafdf22007-09-16 21:08:06 +0000691 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000692 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
693 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000694 /* build code bitmap */
695 build_page_bitmap(p);
696 }
697
698 /* we remove all the TBs in the range [start, end[ */
699 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000700 current_tb_not_found = is_cpu_write_access;
701 current_tb_modified = 0;
702 current_tb = NULL; /* avoid warning */
703 current_pc = 0; /* avoid warning */
704 current_cs_base = 0; /* avoid warning */
705 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000706 tb = p->first_tb;
707 while (tb != NULL) {
708 n = (long)tb & 3;
709 tb = (TranslationBlock *)((long)tb & ~3);
710 tb_next = tb->page_next[n];
711 /* NOTE: this is subtle as a TB may span two physical pages */
712 if (n == 0) {
713 /* NOTE: tb_end may be after the end of the page, but
714 it is not a problem */
715 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
716 tb_end = tb_start + tb->size;
717 } else {
718 tb_start = tb->page_addr[1];
719 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
720 }
721 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000722#ifdef TARGET_HAS_PRECISE_SMC
723 if (current_tb_not_found) {
724 current_tb_not_found = 0;
725 current_tb = NULL;
726 if (env->mem_write_pc) {
727 /* now we have a real cpu fault */
728 current_tb = tb_find_pc(env->mem_write_pc);
729 }
730 }
731 if (current_tb == tb &&
732 !(current_tb->cflags & CF_SINGLE_INSN)) {
733 /* If we are modifying the current TB, we must stop
734 its execution. We could be more precise by checking
735 that the modification is after the current PC, but it
736 would require a specialized function to partially
737 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000738
bellardd720b932004-04-25 17:57:43 +0000739 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000740 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000741 env->mem_write_pc, NULL);
742#if defined(TARGET_I386)
743 current_flags = env->hflags;
744 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
745 current_cs_base = (target_ulong)env->segs[R_CS].base;
746 current_pc = current_cs_base + env->eip;
747#else
748#error unsupported CPU
749#endif
750 }
751#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000752 /* we need to do that to handle the case where a signal
753 occurs while doing tb_phys_invalidate() */
754 saved_tb = NULL;
755 if (env) {
756 saved_tb = env->current_tb;
757 env->current_tb = NULL;
758 }
bellard9fa3e852004-01-04 18:06:42 +0000759 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000760 if (env) {
761 env->current_tb = saved_tb;
762 if (env->interrupt_request && env->current_tb)
763 cpu_interrupt(env, env->interrupt_request);
764 }
bellard9fa3e852004-01-04 18:06:42 +0000765 }
766 tb = tb_next;
767 }
768#if !defined(CONFIG_USER_ONLY)
769 /* if no code remaining, no need to continue to use slow writes */
770 if (!p->first_tb) {
771 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000772 if (is_cpu_write_access) {
773 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
774 }
775 }
776#endif
777#ifdef TARGET_HAS_PRECISE_SMC
778 if (current_tb_modified) {
779 /* we generate a block containing just the instruction
780 modifying the memory. It will ensure that it cannot modify
781 itself */
bellardea1c1802004-06-14 18:56:36 +0000782 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000783 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000784 CF_SINGLE_INSN);
785 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000786 }
787#endif
788}
789
790/* len must be <= 8 and start must be a multiple of len */
aurel3200f82b82008-04-27 21:12:55 +0000791static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000792{
793 PageDesc *p;
794 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000795#if 0
bellarda4193c82004-06-03 14:01:43 +0000796 if (1) {
797 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000798 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
799 cpu_single_env->mem_write_vaddr, len,
800 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000801 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
802 }
bellard59817cc2004-02-16 22:01:13 +0000803 }
804#endif
bellard9fa3e852004-01-04 18:06:42 +0000805 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000806 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000807 return;
808 if (p->code_bitmap) {
809 offset = start & ~TARGET_PAGE_MASK;
810 b = p->code_bitmap[offset >> 3] >> (offset & 7);
811 if (b & ((1 << len) - 1))
812 goto do_invalidate;
813 } else {
814 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000815 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000816 }
817}
818
bellard9fa3e852004-01-04 18:06:42 +0000819#if !defined(CONFIG_SOFTMMU)
aurel3200f82b82008-04-27 21:12:55 +0000820static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +0000821 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000822{
bellardd720b932004-04-25 17:57:43 +0000823 int n, current_flags, current_tb_modified;
824 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000825 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000826 TranslationBlock *tb, *current_tb;
827#ifdef TARGET_HAS_PRECISE_SMC
828 CPUState *env = cpu_single_env;
829#endif
bellard9fa3e852004-01-04 18:06:42 +0000830
831 addr &= TARGET_PAGE_MASK;
832 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000833 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000834 return;
835 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000836 current_tb_modified = 0;
837 current_tb = NULL;
838 current_pc = 0; /* avoid warning */
839 current_cs_base = 0; /* avoid warning */
840 current_flags = 0; /* avoid warning */
841#ifdef TARGET_HAS_PRECISE_SMC
842 if (tb && pc != 0) {
843 current_tb = tb_find_pc(pc);
844 }
845#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000846 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000847 n = (long)tb & 3;
848 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000849#ifdef TARGET_HAS_PRECISE_SMC
850 if (current_tb == tb &&
851 !(current_tb->cflags & CF_SINGLE_INSN)) {
852 /* If we are modifying the current TB, we must stop
853 its execution. We could be more precise by checking
854 that the modification is after the current PC, but it
855 would require a specialized function to partially
856 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000857
bellardd720b932004-04-25 17:57:43 +0000858 current_tb_modified = 1;
859 cpu_restore_state(current_tb, env, pc, puc);
860#if defined(TARGET_I386)
861 current_flags = env->hflags;
862 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
863 current_cs_base = (target_ulong)env->segs[R_CS].base;
864 current_pc = current_cs_base + env->eip;
865#else
866#error unsupported CPU
867#endif
868 }
869#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000870 tb_phys_invalidate(tb, addr);
871 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000872 }
873 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000874#ifdef TARGET_HAS_PRECISE_SMC
875 if (current_tb_modified) {
876 /* we generate a block containing just the instruction
877 modifying the memory. It will ensure that it cannot modify
878 itself */
bellardea1c1802004-06-14 18:56:36 +0000879 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000880 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000881 CF_SINGLE_INSN);
882 cpu_resume_from_signal(env, puc);
883 }
884#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000885}
bellard9fa3e852004-01-04 18:06:42 +0000886#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000887
888/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000889static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000890 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000891{
892 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000893 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000894
bellard9fa3e852004-01-04 18:06:42 +0000895 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000896 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000897 tb->page_next[n] = p->first_tb;
898 last_first_tb = p->first_tb;
899 p->first_tb = (TranslationBlock *)((long)tb | n);
900 invalidate_page_bitmap(p);
901
bellard107db442004-06-22 18:48:46 +0000902#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000903
bellard9fa3e852004-01-04 18:06:42 +0000904#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000905 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000906 target_ulong addr;
907 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000908 int prot;
909
bellardfd6ce8f2003-05-14 19:00:11 +0000910 /* force the host page as non writable (writes will have a
911 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000912 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000913 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000914 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
915 addr += TARGET_PAGE_SIZE) {
916
917 p2 = page_find (addr >> TARGET_PAGE_BITS);
918 if (!p2)
919 continue;
920 prot |= p2->flags;
921 p2->flags &= ~PAGE_WRITE;
922 page_get_flags(addr);
923 }
ths5fafdf22007-09-16 21:08:06 +0000924 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000925 (prot & PAGE_BITS) & ~PAGE_WRITE);
926#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +0000927 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +0000928 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000929#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000930 }
bellard9fa3e852004-01-04 18:06:42 +0000931#else
932 /* if some code is already present, then the pages are already
933 protected. So we handle the case where only the first TB is
934 allocated in a physical page */
935 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000936 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000937 }
938#endif
bellardd720b932004-04-25 17:57:43 +0000939
940#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +0000941}
942
943/* Allocate a new translation block. Flush the translation buffer if
944 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +0000945TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +0000946{
947 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000948
ths5fafdf22007-09-16 21:08:06 +0000949 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
bellardfd6ce8f2003-05-14 19:00:11 +0000950 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
bellardd4e81642003-05-25 16:46:15 +0000951 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +0000952 tb = &tbs[nb_tbs++];
953 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +0000954 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +0000955 return tb;
956}
957
bellard9fa3e852004-01-04 18:06:42 +0000958/* add a new TB and link it to the physical page tables. phys_page2 is
959 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +0000960void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +0000961 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +0000962{
bellard9fa3e852004-01-04 18:06:42 +0000963 unsigned int h;
964 TranslationBlock **ptb;
965
966 /* add in the physical hash table */
967 h = tb_phys_hash_func(phys_pc);
968 ptb = &tb_phys_hash[h];
969 tb->phys_hash_next = *ptb;
970 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000971
972 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +0000973 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
974 if (phys_page2 != -1)
975 tb_alloc_page(tb, 1, phys_page2);
976 else
977 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +0000978
bellardd4e81642003-05-25 16:46:15 +0000979 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
980 tb->jmp_next[0] = NULL;
981 tb->jmp_next[1] = NULL;
982
983 /* init original jump addresses */
984 if (tb->tb_next_offset[0] != 0xffff)
985 tb_reset_jump(tb, 0);
986 if (tb->tb_next_offset[1] != 0xffff)
987 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +0000988
989#ifdef DEBUG_TB_CHECK
990 tb_page_check();
991#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000992}
993
bellarda513fe12003-05-27 23:29:48 +0000994/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
995 tb[1].tc_ptr. Return NULL if not found */
996TranslationBlock *tb_find_pc(unsigned long tc_ptr)
997{
998 int m_min, m_max, m;
999 unsigned long v;
1000 TranslationBlock *tb;
1001
1002 if (nb_tbs <= 0)
1003 return NULL;
1004 if (tc_ptr < (unsigned long)code_gen_buffer ||
1005 tc_ptr >= (unsigned long)code_gen_ptr)
1006 return NULL;
1007 /* binary search (cf Knuth) */
1008 m_min = 0;
1009 m_max = nb_tbs - 1;
1010 while (m_min <= m_max) {
1011 m = (m_min + m_max) >> 1;
1012 tb = &tbs[m];
1013 v = (unsigned long)tb->tc_ptr;
1014 if (v == tc_ptr)
1015 return tb;
1016 else if (tc_ptr < v) {
1017 m_max = m - 1;
1018 } else {
1019 m_min = m + 1;
1020 }
ths5fafdf22007-09-16 21:08:06 +00001021 }
bellarda513fe12003-05-27 23:29:48 +00001022 return &tbs[m_max];
1023}
bellard75012672003-06-21 13:11:07 +00001024
bellardea041c02003-06-25 16:16:50 +00001025static void tb_reset_jump_recursive(TranslationBlock *tb);
1026
1027static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1028{
1029 TranslationBlock *tb1, *tb_next, **ptb;
1030 unsigned int n1;
1031
1032 tb1 = tb->jmp_next[n];
1033 if (tb1 != NULL) {
1034 /* find head of list */
1035 for(;;) {
1036 n1 = (long)tb1 & 3;
1037 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1038 if (n1 == 2)
1039 break;
1040 tb1 = tb1->jmp_next[n1];
1041 }
1042 /* we are now sure now that tb jumps to tb1 */
1043 tb_next = tb1;
1044
1045 /* remove tb from the jmp_first list */
1046 ptb = &tb_next->jmp_first;
1047 for(;;) {
1048 tb1 = *ptb;
1049 n1 = (long)tb1 & 3;
1050 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1051 if (n1 == n && tb1 == tb)
1052 break;
1053 ptb = &tb1->jmp_next[n1];
1054 }
1055 *ptb = tb->jmp_next[n];
1056 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001057
bellardea041c02003-06-25 16:16:50 +00001058 /* suppress the jump to next tb in generated code */
1059 tb_reset_jump(tb, n);
1060
bellard01243112004-01-04 15:48:17 +00001061 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001062 tb_reset_jump_recursive(tb_next);
1063 }
1064}
1065
1066static void tb_reset_jump_recursive(TranslationBlock *tb)
1067{
1068 tb_reset_jump_recursive2(tb, 0);
1069 tb_reset_jump_recursive2(tb, 1);
1070}
1071
bellard1fddef42005-04-17 19:16:13 +00001072#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001073static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1074{
j_mayer9b3c35e2007-04-07 11:21:28 +00001075 target_phys_addr_t addr;
1076 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001077 ram_addr_t ram_addr;
1078 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001079
pbrookc2f07f82006-04-08 17:14:56 +00001080 addr = cpu_get_phys_page_debug(env, pc);
1081 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1082 if (!p) {
1083 pd = IO_MEM_UNASSIGNED;
1084 } else {
1085 pd = p->phys_offset;
1086 }
1087 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001088 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001089}
bellardc27004e2005-01-03 23:35:10 +00001090#endif
bellardd720b932004-04-25 17:57:43 +00001091
pbrook6658ffb2007-03-16 23:58:11 +00001092/* Add a watchpoint. */
1093int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1094{
1095 int i;
1096
1097 for (i = 0; i < env->nb_watchpoints; i++) {
1098 if (addr == env->watchpoint[i].vaddr)
1099 return 0;
1100 }
1101 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1102 return -1;
1103
1104 i = env->nb_watchpoints++;
1105 env->watchpoint[i].vaddr = addr;
1106 tlb_flush_page(env, addr);
1107 /* FIXME: This flush is needed because of the hack to make memory ops
1108 terminate the TB. It can be removed once the proper IO trap and
1109 re-execute bits are in. */
1110 tb_flush(env);
1111 return i;
1112}
1113
1114/* Remove a watchpoint. */
1115int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1116{
1117 int i;
1118
1119 for (i = 0; i < env->nb_watchpoints; i++) {
1120 if (addr == env->watchpoint[i].vaddr) {
1121 env->nb_watchpoints--;
1122 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1123 tlb_flush_page(env, addr);
1124 return 0;
1125 }
1126 }
1127 return -1;
1128}
1129
bellardc33a3462003-07-29 20:50:33 +00001130/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1131 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001132int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001133{
bellard1fddef42005-04-17 19:16:13 +00001134#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001135 int i;
ths3b46e622007-09-17 08:09:54 +00001136
bellard4c3a88a2003-07-26 12:06:08 +00001137 for(i = 0; i < env->nb_breakpoints; i++) {
1138 if (env->breakpoints[i] == pc)
1139 return 0;
1140 }
1141
1142 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1143 return -1;
1144 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001145
bellardd720b932004-04-25 17:57:43 +00001146 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001147 return 0;
1148#else
1149 return -1;
1150#endif
1151}
1152
1153/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001154int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001155{
bellard1fddef42005-04-17 19:16:13 +00001156#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001157 int i;
1158 for(i = 0; i < env->nb_breakpoints; i++) {
1159 if (env->breakpoints[i] == pc)
1160 goto found;
1161 }
1162 return -1;
1163 found:
bellard4c3a88a2003-07-26 12:06:08 +00001164 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001165 if (i < env->nb_breakpoints)
1166 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001167
1168 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001169 return 0;
1170#else
1171 return -1;
1172#endif
1173}
1174
bellardc33a3462003-07-29 20:50:33 +00001175/* enable or disable single step mode. EXCP_DEBUG is returned by the
1176 CPU loop after each instruction */
1177void cpu_single_step(CPUState *env, int enabled)
1178{
bellard1fddef42005-04-17 19:16:13 +00001179#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001180 if (env->singlestep_enabled != enabled) {
1181 env->singlestep_enabled = enabled;
1182 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001183 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001184 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001185 }
1186#endif
1187}
1188
bellard34865132003-10-05 14:28:56 +00001189/* enable or disable low levels log */
1190void cpu_set_log(int log_flags)
1191{
1192 loglevel = log_flags;
1193 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001194 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001195 if (!logfile) {
1196 perror(logfilename);
1197 _exit(1);
1198 }
bellard9fa3e852004-01-04 18:06:42 +00001199#if !defined(CONFIG_SOFTMMU)
1200 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1201 {
1202 static uint8_t logfile_buf[4096];
1203 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1204 }
1205#else
bellard34865132003-10-05 14:28:56 +00001206 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001207#endif
pbrooke735b912007-06-30 13:53:24 +00001208 log_append = 1;
1209 }
1210 if (!loglevel && logfile) {
1211 fclose(logfile);
1212 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001213 }
1214}
1215
1216void cpu_set_log_filename(const char *filename)
1217{
1218 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001219 if (logfile) {
1220 fclose(logfile);
1221 logfile = NULL;
1222 }
1223 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001224}
bellardc33a3462003-07-29 20:50:33 +00001225
bellard01243112004-01-04 15:48:17 +00001226/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001227void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001228{
1229 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001230 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
bellard59817cc2004-02-16 22:01:13 +00001231
bellard68a79312003-06-30 13:12:32 +00001232 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001233 /* if the cpu is currently executing code, we must unlink it and
1234 all the potentially executing TB */
1235 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001236 if (tb && !testandset(&interrupt_lock)) {
1237 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001238 tb_reset_jump_recursive(tb);
aurel3215a51152008-03-28 22:29:15 +00001239 resetlock(&interrupt_lock);
bellardea041c02003-06-25 16:16:50 +00001240 }
1241}
1242
bellardb54ad042004-05-20 13:42:52 +00001243void cpu_reset_interrupt(CPUState *env, int mask)
1244{
1245 env->interrupt_request &= ~mask;
1246}
1247
bellardf193c792004-03-21 17:06:25 +00001248CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001249 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001250 "show generated host assembly code for each compiled TB" },
1251 { CPU_LOG_TB_IN_ASM, "in_asm",
1252 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001253 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001254 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001255 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001256 "show micro ops "
1257#ifdef TARGET_I386
1258 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001259#endif
blueswir1e01a1152008-03-14 17:37:11 +00001260 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001261 { CPU_LOG_INT, "int",
1262 "show interrupts/exceptions in short format" },
1263 { CPU_LOG_EXEC, "exec",
1264 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001265 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001266 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001267#ifdef TARGET_I386
1268 { CPU_LOG_PCALL, "pcall",
1269 "show protected mode far calls/returns/exceptions" },
1270#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001271#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001272 { CPU_LOG_IOPORT, "ioport",
1273 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001274#endif
bellardf193c792004-03-21 17:06:25 +00001275 { 0, NULL, NULL },
1276};
1277
1278static int cmp1(const char *s1, int n, const char *s2)
1279{
1280 if (strlen(s2) != n)
1281 return 0;
1282 return memcmp(s1, s2, n) == 0;
1283}
ths3b46e622007-09-17 08:09:54 +00001284
bellardf193c792004-03-21 17:06:25 +00001285/* takes a comma separated list of log masks. Return 0 if error. */
1286int cpu_str_to_log_mask(const char *str)
1287{
1288 CPULogItem *item;
1289 int mask;
1290 const char *p, *p1;
1291
1292 p = str;
1293 mask = 0;
1294 for(;;) {
1295 p1 = strchr(p, ',');
1296 if (!p1)
1297 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001298 if(cmp1(p,p1-p,"all")) {
1299 for(item = cpu_log_items; item->mask != 0; item++) {
1300 mask |= item->mask;
1301 }
1302 } else {
bellardf193c792004-03-21 17:06:25 +00001303 for(item = cpu_log_items; item->mask != 0; item++) {
1304 if (cmp1(p, p1 - p, item->name))
1305 goto found;
1306 }
1307 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001308 }
bellardf193c792004-03-21 17:06:25 +00001309 found:
1310 mask |= item->mask;
1311 if (*p1 != ',')
1312 break;
1313 p = p1 + 1;
1314 }
1315 return mask;
1316}
bellardea041c02003-06-25 16:16:50 +00001317
bellard75012672003-06-21 13:11:07 +00001318void cpu_abort(CPUState *env, const char *fmt, ...)
1319{
1320 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001321 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001322
1323 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001324 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001325 fprintf(stderr, "qemu: fatal: ");
1326 vfprintf(stderr, fmt, ap);
1327 fprintf(stderr, "\n");
1328#ifdef TARGET_I386
ths0573fbf2007-09-23 15:28:04 +00001329 if(env->intercept & INTERCEPT_SVM_MASK) {
1330 /* most probably the virtual machine should not
1331 be shut down but rather caught by the VMM */
1332 vmexit(SVM_EXIT_SHUTDOWN, 0);
1333 }
bellard7fe48482004-10-09 18:08:01 +00001334 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1335#else
1336 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001337#endif
balrog924edca2007-06-10 14:07:13 +00001338 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001339 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001340 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001341 fprintf(logfile, "\n");
1342#ifdef TARGET_I386
1343 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1344#else
1345 cpu_dump_state(env, logfile, fprintf, 0);
1346#endif
balrog924edca2007-06-10 14:07:13 +00001347 fflush(logfile);
1348 fclose(logfile);
1349 }
pbrook493ae1f2007-11-23 16:53:59 +00001350 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001351 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001352 abort();
1353}
1354
thsc5be9f02007-02-28 20:20:53 +00001355CPUState *cpu_copy(CPUState *env)
1356{
ths01ba9812007-12-09 02:22:57 +00001357 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001358 /* preserve chaining and index */
1359 CPUState *next_cpu = new_env->next_cpu;
1360 int cpu_index = new_env->cpu_index;
1361 memcpy(new_env, env, sizeof(CPUState));
1362 new_env->next_cpu = next_cpu;
1363 new_env->cpu_index = cpu_index;
1364 return new_env;
1365}
1366
bellard01243112004-01-04 15:48:17 +00001367#if !defined(CONFIG_USER_ONLY)
1368
bellardee8b7022004-02-03 23:35:10 +00001369/* NOTE: if flush_global is true, also flush global entries (not
1370 implemented yet) */
1371void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001372{
bellard33417e72003-08-10 21:47:01 +00001373 int i;
bellard01243112004-01-04 15:48:17 +00001374
bellard9fa3e852004-01-04 18:06:42 +00001375#if defined(DEBUG_TLB)
1376 printf("tlb_flush:\n");
1377#endif
bellard01243112004-01-04 15:48:17 +00001378 /* must reset current TB so that interrupts cannot modify the
1379 links while we are modifying them */
1380 env->current_tb = NULL;
1381
bellard33417e72003-08-10 21:47:01 +00001382 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001383 env->tlb_table[0][i].addr_read = -1;
1384 env->tlb_table[0][i].addr_write = -1;
1385 env->tlb_table[0][i].addr_code = -1;
1386 env->tlb_table[1][i].addr_read = -1;
1387 env->tlb_table[1][i].addr_write = -1;
1388 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001389#if (NB_MMU_MODES >= 3)
1390 env->tlb_table[2][i].addr_read = -1;
1391 env->tlb_table[2][i].addr_write = -1;
1392 env->tlb_table[2][i].addr_code = -1;
1393#if (NB_MMU_MODES == 4)
1394 env->tlb_table[3][i].addr_read = -1;
1395 env->tlb_table[3][i].addr_write = -1;
1396 env->tlb_table[3][i].addr_code = -1;
1397#endif
1398#endif
bellard33417e72003-08-10 21:47:01 +00001399 }
bellard9fa3e852004-01-04 18:06:42 +00001400
bellard8a40a182005-11-20 10:35:40 +00001401 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001402
1403#if !defined(CONFIG_SOFTMMU)
1404 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1405#endif
bellard0a962c02005-02-10 22:00:27 +00001406#ifdef USE_KQEMU
1407 if (env->kqemu_enabled) {
1408 kqemu_flush(env, flush_global);
1409 }
1410#endif
bellarde3db7222005-01-26 22:00:47 +00001411 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001412}
1413
bellard274da6b2004-05-20 21:56:27 +00001414static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001415{
ths5fafdf22007-09-16 21:08:06 +00001416 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001417 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001418 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001419 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001420 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001421 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1422 tlb_entry->addr_read = -1;
1423 tlb_entry->addr_write = -1;
1424 tlb_entry->addr_code = -1;
1425 }
bellard61382a52003-10-27 21:22:23 +00001426}
1427
bellard2e126692004-04-25 21:28:44 +00001428void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001429{
bellard8a40a182005-11-20 10:35:40 +00001430 int i;
bellard9fa3e852004-01-04 18:06:42 +00001431 TranslationBlock *tb;
bellard01243112004-01-04 15:48:17 +00001432
bellard9fa3e852004-01-04 18:06:42 +00001433#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001434 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001435#endif
bellard01243112004-01-04 15:48:17 +00001436 /* must reset current TB so that interrupts cannot modify the
1437 links while we are modifying them */
1438 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001439
bellard61382a52003-10-27 21:22:23 +00001440 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001441 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001442 tlb_flush_entry(&env->tlb_table[0][i], addr);
1443 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001444#if (NB_MMU_MODES >= 3)
1445 tlb_flush_entry(&env->tlb_table[2][i], addr);
1446#if (NB_MMU_MODES == 4)
1447 tlb_flush_entry(&env->tlb_table[3][i], addr);
1448#endif
1449#endif
bellard01243112004-01-04 15:48:17 +00001450
pbrookb362e5e2006-11-12 20:40:55 +00001451 /* Discard jump cache entries for any tb which might potentially
1452 overlap the flushed page. */
1453 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1454 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
1455
1456 i = tb_jmp_cache_hash_page(addr);
1457 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
bellard9fa3e852004-01-04 18:06:42 +00001458
bellard01243112004-01-04 15:48:17 +00001459#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001460 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001461 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001462#endif
bellard0a962c02005-02-10 22:00:27 +00001463#ifdef USE_KQEMU
1464 if (env->kqemu_enabled) {
1465 kqemu_flush_page(env, addr);
1466 }
1467#endif
bellard9fa3e852004-01-04 18:06:42 +00001468}
1469
bellard9fa3e852004-01-04 18:06:42 +00001470/* update the TLBs so that writes to code in the virtual page 'addr'
1471 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001472static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001473{
ths5fafdf22007-09-16 21:08:06 +00001474 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001475 ram_addr + TARGET_PAGE_SIZE,
1476 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001477}
1478
bellard9fa3e852004-01-04 18:06:42 +00001479/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001480 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001481static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001482 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001483{
bellard3a7d9292005-08-21 09:26:42 +00001484 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001485}
1486
ths5fafdf22007-09-16 21:08:06 +00001487static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001488 unsigned long start, unsigned long length)
1489{
1490 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001491 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1492 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001493 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001494 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001495 }
1496 }
1497}
1498
bellard3a7d9292005-08-21 09:26:42 +00001499void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001500 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001501{
1502 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001503 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001504 int i, mask, len;
1505 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001506
1507 start &= TARGET_PAGE_MASK;
1508 end = TARGET_PAGE_ALIGN(end);
1509
1510 length = end - start;
1511 if (length == 0)
1512 return;
bellard0a962c02005-02-10 22:00:27 +00001513 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001514#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001515 /* XXX: should not depend on cpu context */
1516 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001517 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001518 ram_addr_t addr;
1519 addr = start;
1520 for(i = 0; i < len; i++) {
1521 kqemu_set_notdirty(env, addr);
1522 addr += TARGET_PAGE_SIZE;
1523 }
bellard3a7d9292005-08-21 09:26:42 +00001524 }
1525#endif
bellardf23db162005-08-21 19:12:28 +00001526 mask = ~dirty_flags;
1527 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1528 for(i = 0; i < len; i++)
1529 p[i] &= mask;
1530
bellard1ccde1c2004-02-06 19:46:14 +00001531 /* we modify the TLB cache so that the dirty bit will be set again
1532 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001533 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001534 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1535 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001536 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001537 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001538 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001539#if (NB_MMU_MODES >= 3)
1540 for(i = 0; i < CPU_TLB_SIZE; i++)
1541 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1542#if (NB_MMU_MODES == 4)
1543 for(i = 0; i < CPU_TLB_SIZE; i++)
1544 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1545#endif
1546#endif
bellard6a00d602005-11-21 23:25:50 +00001547 }
bellard59817cc2004-02-16 22:01:13 +00001548
1549#if !defined(CONFIG_SOFTMMU)
1550 /* XXX: this is expensive */
1551 {
1552 VirtPageDesc *p;
1553 int j;
1554 target_ulong addr;
1555
1556 for(i = 0; i < L1_SIZE; i++) {
1557 p = l1_virt_map[i];
1558 if (p) {
1559 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1560 for(j = 0; j < L2_SIZE; j++) {
1561 if (p->valid_tag == virt_valid_tag &&
1562 p->phys_addr >= start && p->phys_addr < end &&
1563 (p->prot & PROT_WRITE)) {
1564 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001565 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001566 p->prot & ~PROT_WRITE);
1567 }
1568 }
1569 addr += TARGET_PAGE_SIZE;
1570 p++;
1571 }
1572 }
1573 }
1574 }
1575#endif
bellard1ccde1c2004-02-06 19:46:14 +00001576}
1577
bellard3a7d9292005-08-21 09:26:42 +00001578static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1579{
1580 ram_addr_t ram_addr;
1581
bellard84b7b8e2005-11-28 21:19:04 +00001582 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001583 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001584 tlb_entry->addend - (unsigned long)phys_ram_base;
1585 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001586 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001587 }
1588 }
1589}
1590
1591/* update the TLB according to the current state of the dirty bits */
1592void cpu_tlb_update_dirty(CPUState *env)
1593{
1594 int i;
1595 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001596 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001597 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001598 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001599#if (NB_MMU_MODES >= 3)
1600 for(i = 0; i < CPU_TLB_SIZE; i++)
1601 tlb_update_dirty(&env->tlb_table[2][i]);
1602#if (NB_MMU_MODES == 4)
1603 for(i = 0; i < CPU_TLB_SIZE; i++)
1604 tlb_update_dirty(&env->tlb_table[3][i]);
1605#endif
1606#endif
bellard3a7d9292005-08-21 09:26:42 +00001607}
1608
ths5fafdf22007-09-16 21:08:06 +00001609static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001610 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001611{
1612 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001613 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1614 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001615 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001616 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001617 }
1618 }
1619}
1620
1621/* update the TLB corresponding to virtual page vaddr and phys addr
1622 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001623static inline void tlb_set_dirty(CPUState *env,
1624 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001625{
bellard1ccde1c2004-02-06 19:46:14 +00001626 int i;
1627
bellard1ccde1c2004-02-06 19:46:14 +00001628 addr &= TARGET_PAGE_MASK;
1629 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001630 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1631 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001632#if (NB_MMU_MODES >= 3)
1633 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1634#if (NB_MMU_MODES == 4)
1635 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1636#endif
1637#endif
bellard9fa3e852004-01-04 18:06:42 +00001638}
1639
bellard59817cc2004-02-16 22:01:13 +00001640/* add a new TLB entry. At most one entry for a given virtual address
1641 is permitted. Return 0 if OK or 2 if the page could not be mapped
1642 (can only happen in non SOFTMMU mode for I/O pages or pages
1643 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001644int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1645 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001646 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001647{
bellard92e873b2004-05-21 14:52:29 +00001648 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001649 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001650 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001651 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001652 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001653 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001654 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001655 int i;
bellard9fa3e852004-01-04 18:06:42 +00001656
bellard92e873b2004-05-21 14:52:29 +00001657 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001658 if (!p) {
1659 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001660 } else {
1661 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001662 }
1663#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001664 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1665 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001666#endif
1667
1668 ret = 0;
1669#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001670 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001671#endif
1672 {
bellard2a4188a2006-06-25 21:54:59 +00001673 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001674 /* IO memory case */
1675 address = vaddr | pd;
1676 addend = paddr;
1677 } else {
1678 /* standard memory */
1679 address = vaddr;
1680 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1681 }
pbrook6658ffb2007-03-16 23:58:11 +00001682
1683 /* Make accesses to pages with watchpoints go via the
1684 watchpoint trap routines. */
1685 for (i = 0; i < env->nb_watchpoints; i++) {
1686 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1687 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001688 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001689 address = vaddr | io_mem_watch;
1690 } else {
balrogd79acba2007-06-26 20:01:13 +00001691 env->watchpoint[i].addend = pd - paddr +
1692 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001693 /* TODO: Figure out how to make read watchpoints coexist
1694 with code. */
1695 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1696 }
1697 }
1698 }
balrogd79acba2007-06-26 20:01:13 +00001699
bellard90f18422005-07-24 10:17:31 +00001700 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001701 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001702 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001703 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001704 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001705 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001706 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001707 te->addr_read = -1;
1708 }
1709 if (prot & PAGE_EXEC) {
1710 te->addr_code = address;
1711 } else {
1712 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001713 }
bellard67b915a2004-03-31 23:37:16 +00001714 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001715 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001716 (pd & IO_MEM_ROMD)) {
1717 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001718 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001719 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001720 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001721 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001722 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001723 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001724 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001725 }
1726 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001727 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001728 }
1729 }
1730#if !defined(CONFIG_SOFTMMU)
1731 else {
1732 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1733 /* IO access: no mapping is done as it will be handled by the
1734 soft MMU */
1735 if (!(env->hflags & HF_SOFTMMU_MASK))
1736 ret = 2;
1737 } else {
1738 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001739
bellard59817cc2004-02-16 22:01:13 +00001740 if (vaddr >= MMAP_AREA_END) {
1741 ret = 2;
1742 } else {
1743 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001744 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001745#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001746 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001747#endif
ths5fafdf22007-09-16 21:08:06 +00001748 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001749 !cpu_physical_memory_is_dirty(pd))) {
1750 /* ROM: we do as if code was inside */
1751 /* if code is present, we only map as read only and save the
1752 original mapping */
1753 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001754
bellard90f18422005-07-24 10:17:31 +00001755 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001756 vp->phys_addr = pd;
1757 vp->prot = prot;
1758 vp->valid_tag = virt_valid_tag;
1759 prot &= ~PAGE_WRITE;
1760 }
bellard9fa3e852004-01-04 18:06:42 +00001761 }
ths5fafdf22007-09-16 21:08:06 +00001762 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001763 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1764 if (map_addr == MAP_FAILED) {
1765 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1766 paddr, vaddr);
1767 }
bellard9fa3e852004-01-04 18:06:42 +00001768 }
1769 }
1770 }
1771#endif
1772 return ret;
1773}
1774
1775/* called from signal handler: invalidate the code and unprotect the
1776 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001777int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001778{
1779#if !defined(CONFIG_SOFTMMU)
1780 VirtPageDesc *vp;
1781
1782#if defined(DEBUG_TLB)
1783 printf("page_unprotect: addr=0x%08x\n", addr);
1784#endif
1785 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001786
1787 /* if it is not mapped, no need to worry here */
1788 if (addr >= MMAP_AREA_END)
1789 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001790 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1791 if (!vp)
1792 return 0;
1793 /* NOTE: in this case, validate_tag is _not_ tested as it
1794 validates only the code TLB */
1795 if (vp->valid_tag != virt_valid_tag)
1796 return 0;
1797 if (!(vp->prot & PAGE_WRITE))
1798 return 0;
1799#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001800 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001801 addr, vp->phys_addr, vp->prot);
1802#endif
bellard59817cc2004-02-16 22:01:13 +00001803 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1804 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1805 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001806 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001807 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001808 /* flush the code inside */
1809 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001810 return 1;
1811#else
1812 return 0;
1813#endif
bellard33417e72003-08-10 21:47:01 +00001814}
1815
bellard01243112004-01-04 15:48:17 +00001816#else
1817
bellardee8b7022004-02-03 23:35:10 +00001818void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001819{
1820}
1821
bellard2e126692004-04-25 21:28:44 +00001822void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001823{
1824}
1825
ths5fafdf22007-09-16 21:08:06 +00001826int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1827 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001828 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001829{
bellard9fa3e852004-01-04 18:06:42 +00001830 return 0;
1831}
bellard33417e72003-08-10 21:47:01 +00001832
bellard9fa3e852004-01-04 18:06:42 +00001833/* dump memory mappings */
1834void page_dump(FILE *f)
1835{
1836 unsigned long start, end;
1837 int i, j, prot, prot1;
1838 PageDesc *p;
1839
1840 fprintf(f, "%-8s %-8s %-8s %s\n",
1841 "start", "end", "size", "prot");
1842 start = -1;
1843 end = -1;
1844 prot = 0;
1845 for(i = 0; i <= L1_SIZE; i++) {
1846 if (i < L1_SIZE)
1847 p = l1_map[i];
1848 else
1849 p = NULL;
1850 for(j = 0;j < L2_SIZE; j++) {
1851 if (!p)
1852 prot1 = 0;
1853 else
1854 prot1 = p[j].flags;
1855 if (prot1 != prot) {
1856 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1857 if (start != -1) {
1858 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001859 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001860 prot & PAGE_READ ? 'r' : '-',
1861 prot & PAGE_WRITE ? 'w' : '-',
1862 prot & PAGE_EXEC ? 'x' : '-');
1863 }
1864 if (prot1 != 0)
1865 start = end;
1866 else
1867 start = -1;
1868 prot = prot1;
1869 }
1870 if (!p)
1871 break;
1872 }
bellard33417e72003-08-10 21:47:01 +00001873 }
bellard33417e72003-08-10 21:47:01 +00001874}
1875
pbrook53a59602006-03-25 19:31:22 +00001876int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001877{
bellard9fa3e852004-01-04 18:06:42 +00001878 PageDesc *p;
1879
1880 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001881 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001882 return 0;
1883 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001884}
1885
bellard9fa3e852004-01-04 18:06:42 +00001886/* modify the flags of a page and invalidate the code if
1887 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1888 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001889void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001890{
1891 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001892 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001893
1894 start = start & TARGET_PAGE_MASK;
1895 end = TARGET_PAGE_ALIGN(end);
1896 if (flags & PAGE_WRITE)
1897 flags |= PAGE_WRITE_ORG;
1898 spin_lock(&tb_lock);
1899 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1900 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1901 /* if the write protection is set, then we invalidate the code
1902 inside */
ths5fafdf22007-09-16 21:08:06 +00001903 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001904 (flags & PAGE_WRITE) &&
1905 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001906 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001907 }
1908 p->flags = flags;
1909 }
1910 spin_unlock(&tb_lock);
1911}
1912
ths3d97b402007-11-02 19:02:07 +00001913int page_check_range(target_ulong start, target_ulong len, int flags)
1914{
1915 PageDesc *p;
1916 target_ulong end;
1917 target_ulong addr;
1918
1919 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
1920 start = start & TARGET_PAGE_MASK;
1921
1922 if( end < start )
1923 /* we've wrapped around */
1924 return -1;
1925 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1926 p = page_find(addr >> TARGET_PAGE_BITS);
1927 if( !p )
1928 return -1;
1929 if( !(p->flags & PAGE_VALID) )
1930 return -1;
1931
bellarddae32702007-11-14 10:51:00 +00001932 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00001933 return -1;
bellarddae32702007-11-14 10:51:00 +00001934 if (flags & PAGE_WRITE) {
1935 if (!(p->flags & PAGE_WRITE_ORG))
1936 return -1;
1937 /* unprotect the page if it was put read-only because it
1938 contains translated code */
1939 if (!(p->flags & PAGE_WRITE)) {
1940 if (!page_unprotect(addr, 0, NULL))
1941 return -1;
1942 }
1943 return 0;
1944 }
ths3d97b402007-11-02 19:02:07 +00001945 }
1946 return 0;
1947}
1948
bellard9fa3e852004-01-04 18:06:42 +00001949/* called from signal handler: invalidate the code and unprotect the
1950 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001951int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001952{
1953 unsigned int page_index, prot, pindex;
1954 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00001955 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001956
bellard83fb7ad2004-07-05 21:25:26 +00001957 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00001958 page_index = host_start >> TARGET_PAGE_BITS;
1959 p1 = page_find(page_index);
1960 if (!p1)
1961 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00001962 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00001963 p = p1;
1964 prot = 0;
1965 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
1966 prot |= p->flags;
1967 p++;
1968 }
1969 /* if the page was really writable, then we change its
1970 protection back to writable */
1971 if (prot & PAGE_WRITE_ORG) {
1972 pindex = (address - host_start) >> TARGET_PAGE_BITS;
1973 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00001974 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00001975 (prot & PAGE_BITS) | PAGE_WRITE);
1976 p1[pindex].flags |= PAGE_WRITE;
1977 /* and since the content will be modified, we must invalidate
1978 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00001979 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001980#ifdef DEBUG_TB_CHECK
1981 tb_invalidate_check(address);
1982#endif
1983 return 1;
1984 }
1985 }
1986 return 0;
1987}
1988
bellard6a00d602005-11-21 23:25:50 +00001989static inline void tlb_set_dirty(CPUState *env,
1990 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001991{
1992}
bellard9fa3e852004-01-04 18:06:42 +00001993#endif /* defined(CONFIG_USER_ONLY) */
1994
blueswir1db7b5422007-05-26 17:36:03 +00001995static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00001996 ram_addr_t memory);
1997static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
1998 ram_addr_t orig_memory);
blueswir1db7b5422007-05-26 17:36:03 +00001999#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2000 need_subpage) \
2001 do { \
2002 if (addr > start_addr) \
2003 start_addr2 = 0; \
2004 else { \
2005 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2006 if (start_addr2 > 0) \
2007 need_subpage = 1; \
2008 } \
2009 \
blueswir149e9fba2007-05-30 17:25:06 +00002010 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002011 end_addr2 = TARGET_PAGE_SIZE - 1; \
2012 else { \
2013 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2014 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2015 need_subpage = 1; \
2016 } \
2017 } while (0)
2018
bellard33417e72003-08-10 21:47:01 +00002019/* register physical memory. 'size' must be a multiple of the target
2020 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2021 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002022void cpu_register_physical_memory(target_phys_addr_t start_addr,
aurel3200f82b82008-04-27 21:12:55 +00002023 ram_addr_t size,
2024 ram_addr_t phys_offset)
bellard33417e72003-08-10 21:47:01 +00002025{
bellard108c49b2005-07-24 12:55:09 +00002026 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002027 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002028 CPUState *env;
aurel3200f82b82008-04-27 21:12:55 +00002029 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002030 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002031
bellard5fd386f2004-05-23 21:11:22 +00002032 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002033 end_addr = start_addr + (target_phys_addr_t)size;
2034 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002035 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2036 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
aurel3200f82b82008-04-27 21:12:55 +00002037 ram_addr_t orig_memory = p->phys_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002038 target_phys_addr_t start_addr2, end_addr2;
2039 int need_subpage = 0;
2040
2041 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2042 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002043 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002044 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2045 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2046 &p->phys_offset, orig_memory);
2047 } else {
2048 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2049 >> IO_MEM_SHIFT];
2050 }
2051 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2052 } else {
2053 p->phys_offset = phys_offset;
2054 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2055 (phys_offset & IO_MEM_ROMD))
2056 phys_offset += TARGET_PAGE_SIZE;
2057 }
2058 } else {
2059 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2060 p->phys_offset = phys_offset;
2061 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2062 (phys_offset & IO_MEM_ROMD))
2063 phys_offset += TARGET_PAGE_SIZE;
2064 else {
2065 target_phys_addr_t start_addr2, end_addr2;
2066 int need_subpage = 0;
2067
2068 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2069 end_addr2, need_subpage);
2070
blueswir14254fab2008-01-01 16:57:19 +00002071 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002072 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2073 &p->phys_offset, IO_MEM_UNASSIGNED);
2074 subpage_register(subpage, start_addr2, end_addr2,
2075 phys_offset);
2076 }
2077 }
2078 }
bellard33417e72003-08-10 21:47:01 +00002079 }
ths3b46e622007-09-17 08:09:54 +00002080
bellard9d420372006-06-25 22:25:22 +00002081 /* since each CPU stores ram addresses in its TLB cache, we must
2082 reset the modified entries */
2083 /* XXX: slow ! */
2084 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2085 tlb_flush(env, 1);
2086 }
bellard33417e72003-08-10 21:47:01 +00002087}
2088
bellardba863452006-09-24 18:41:10 +00002089/* XXX: temporary until new memory mapping API */
aurel3200f82b82008-04-27 21:12:55 +00002090ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002091{
2092 PhysPageDesc *p;
2093
2094 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2095 if (!p)
2096 return IO_MEM_UNASSIGNED;
2097 return p->phys_offset;
2098}
2099
bellarde9a1ab12007-02-08 23:08:38 +00002100/* XXX: better than nothing */
aurel3200f82b82008-04-27 21:12:55 +00002101ram_addr_t qemu_ram_alloc(ram_addr_t size)
bellarde9a1ab12007-02-08 23:08:38 +00002102{
2103 ram_addr_t addr;
balrog7fb4fdc2008-04-24 17:59:27 +00002104 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
aurel3200f82b82008-04-27 21:12:55 +00002105 fprintf(stderr, "Not enough memory (requested_size = %lu, max memory = %ld)\n",
aurel3203875442008-04-22 20:45:18 +00002106 size, phys_ram_size);
bellarde9a1ab12007-02-08 23:08:38 +00002107 abort();
2108 }
2109 addr = phys_ram_alloc_offset;
2110 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2111 return addr;
2112}
2113
2114void qemu_ram_free(ram_addr_t addr)
2115{
2116}
2117
bellarda4193c82004-06-03 14:01:43 +00002118static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002119{
pbrook67d3b952006-12-18 05:03:52 +00002120#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002121 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002122#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002123#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002124 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002125#elif TARGET_CRIS
2126 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002127#endif
bellard33417e72003-08-10 21:47:01 +00002128 return 0;
2129}
2130
bellarda4193c82004-06-03 14:01:43 +00002131static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002132{
pbrook67d3b952006-12-18 05:03:52 +00002133#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002134 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002135#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002136#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002137 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002138#elif TARGET_CRIS
2139 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002140#endif
bellard33417e72003-08-10 21:47:01 +00002141}
2142
2143static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2144 unassigned_mem_readb,
2145 unassigned_mem_readb,
2146 unassigned_mem_readb,
2147};
2148
2149static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2150 unassigned_mem_writeb,
2151 unassigned_mem_writeb,
2152 unassigned_mem_writeb,
2153};
2154
bellarda4193c82004-06-03 14:01:43 +00002155static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002156{
bellard3a7d9292005-08-21 09:26:42 +00002157 unsigned long ram_addr;
2158 int dirty_flags;
2159 ram_addr = addr - (unsigned long)phys_ram_base;
2160 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2161 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2162#if !defined(CONFIG_USER_ONLY)
2163 tb_invalidate_phys_page_fast(ram_addr, 1);
2164 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2165#endif
2166 }
bellardc27004e2005-01-03 23:35:10 +00002167 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002168#ifdef USE_KQEMU
2169 if (cpu_single_env->kqemu_enabled &&
2170 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2171 kqemu_modify_page(cpu_single_env, ram_addr);
2172#endif
bellardf23db162005-08-21 19:12:28 +00002173 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2174 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2175 /* we remove the notdirty callback only if the code has been
2176 flushed */
2177 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002178 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002179}
2180
bellarda4193c82004-06-03 14:01:43 +00002181static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002182{
bellard3a7d9292005-08-21 09:26:42 +00002183 unsigned long ram_addr;
2184 int dirty_flags;
2185 ram_addr = addr - (unsigned long)phys_ram_base;
2186 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2187 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2188#if !defined(CONFIG_USER_ONLY)
2189 tb_invalidate_phys_page_fast(ram_addr, 2);
2190 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2191#endif
2192 }
bellardc27004e2005-01-03 23:35:10 +00002193 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002194#ifdef USE_KQEMU
2195 if (cpu_single_env->kqemu_enabled &&
2196 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2197 kqemu_modify_page(cpu_single_env, ram_addr);
2198#endif
bellardf23db162005-08-21 19:12:28 +00002199 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2200 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2201 /* we remove the notdirty callback only if the code has been
2202 flushed */
2203 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002204 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002205}
2206
bellarda4193c82004-06-03 14:01:43 +00002207static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002208{
bellard3a7d9292005-08-21 09:26:42 +00002209 unsigned long ram_addr;
2210 int dirty_flags;
2211 ram_addr = addr - (unsigned long)phys_ram_base;
2212 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2213 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2214#if !defined(CONFIG_USER_ONLY)
2215 tb_invalidate_phys_page_fast(ram_addr, 4);
2216 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2217#endif
2218 }
bellardc27004e2005-01-03 23:35:10 +00002219 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002220#ifdef USE_KQEMU
2221 if (cpu_single_env->kqemu_enabled &&
2222 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2223 kqemu_modify_page(cpu_single_env, ram_addr);
2224#endif
bellardf23db162005-08-21 19:12:28 +00002225 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2226 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2227 /* we remove the notdirty callback only if the code has been
2228 flushed */
2229 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002230 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002231}
2232
bellard3a7d9292005-08-21 09:26:42 +00002233static CPUReadMemoryFunc *error_mem_read[3] = {
2234 NULL, /* never used */
2235 NULL, /* never used */
2236 NULL, /* never used */
2237};
2238
bellard1ccde1c2004-02-06 19:46:14 +00002239static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2240 notdirty_mem_writeb,
2241 notdirty_mem_writew,
2242 notdirty_mem_writel,
2243};
2244
pbrook6658ffb2007-03-16 23:58:11 +00002245#if defined(CONFIG_SOFTMMU)
2246/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2247 so these check for a hit then pass through to the normal out-of-line
2248 phys routines. */
2249static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2250{
2251 return ldub_phys(addr);
2252}
2253
2254static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2255{
2256 return lduw_phys(addr);
2257}
2258
2259static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2260{
2261 return ldl_phys(addr);
2262}
2263
2264/* Generate a debug exception if a watchpoint has been hit.
2265 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002266 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002267static target_ulong check_watchpoint(target_phys_addr_t addr)
2268{
2269 CPUState *env = cpu_single_env;
2270 target_ulong watch;
2271 target_ulong retaddr;
2272 int i;
2273
2274 retaddr = addr;
2275 for (i = 0; i < env->nb_watchpoints; i++) {
2276 watch = env->watchpoint[i].vaddr;
2277 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002278 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002279 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2280 cpu_single_env->watchpoint_hit = i + 1;
2281 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2282 break;
2283 }
2284 }
2285 }
2286 return retaddr;
2287}
2288
2289static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2290 uint32_t val)
2291{
2292 addr = check_watchpoint(addr);
2293 stb_phys(addr, val);
2294}
2295
2296static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2297 uint32_t val)
2298{
2299 addr = check_watchpoint(addr);
2300 stw_phys(addr, val);
2301}
2302
2303static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2304 uint32_t val)
2305{
2306 addr = check_watchpoint(addr);
2307 stl_phys(addr, val);
2308}
2309
2310static CPUReadMemoryFunc *watch_mem_read[3] = {
2311 watch_mem_readb,
2312 watch_mem_readw,
2313 watch_mem_readl,
2314};
2315
2316static CPUWriteMemoryFunc *watch_mem_write[3] = {
2317 watch_mem_writeb,
2318 watch_mem_writew,
2319 watch_mem_writel,
2320};
2321#endif
2322
blueswir1db7b5422007-05-26 17:36:03 +00002323static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2324 unsigned int len)
2325{
blueswir1db7b5422007-05-26 17:36:03 +00002326 uint32_t ret;
2327 unsigned int idx;
2328
2329 idx = SUBPAGE_IDX(addr - mmio->base);
2330#if defined(DEBUG_SUBPAGE)
2331 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2332 mmio, len, addr, idx);
2333#endif
blueswir13ee89922008-01-02 19:45:26 +00002334 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002335
2336 return ret;
2337}
2338
2339static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2340 uint32_t value, unsigned int len)
2341{
blueswir1db7b5422007-05-26 17:36:03 +00002342 unsigned int idx;
2343
2344 idx = SUBPAGE_IDX(addr - mmio->base);
2345#if defined(DEBUG_SUBPAGE)
2346 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2347 mmio, len, addr, idx, value);
2348#endif
blueswir13ee89922008-01-02 19:45:26 +00002349 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002350}
2351
2352static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2353{
2354#if defined(DEBUG_SUBPAGE)
2355 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2356#endif
2357
2358 return subpage_readlen(opaque, addr, 0);
2359}
2360
2361static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2362 uint32_t value)
2363{
2364#if defined(DEBUG_SUBPAGE)
2365 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2366#endif
2367 subpage_writelen(opaque, addr, value, 0);
2368}
2369
2370static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2371{
2372#if defined(DEBUG_SUBPAGE)
2373 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2374#endif
2375
2376 return subpage_readlen(opaque, addr, 1);
2377}
2378
2379static void subpage_writew (void *opaque, target_phys_addr_t addr,
2380 uint32_t value)
2381{
2382#if defined(DEBUG_SUBPAGE)
2383 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2384#endif
2385 subpage_writelen(opaque, addr, value, 1);
2386}
2387
2388static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2389{
2390#if defined(DEBUG_SUBPAGE)
2391 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2392#endif
2393
2394 return subpage_readlen(opaque, addr, 2);
2395}
2396
2397static void subpage_writel (void *opaque,
2398 target_phys_addr_t addr, uint32_t value)
2399{
2400#if defined(DEBUG_SUBPAGE)
2401 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2402#endif
2403 subpage_writelen(opaque, addr, value, 2);
2404}
2405
2406static CPUReadMemoryFunc *subpage_read[] = {
2407 &subpage_readb,
2408 &subpage_readw,
2409 &subpage_readl,
2410};
2411
2412static CPUWriteMemoryFunc *subpage_write[] = {
2413 &subpage_writeb,
2414 &subpage_writew,
2415 &subpage_writel,
2416};
2417
2418static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002419 ram_addr_t memory)
blueswir1db7b5422007-05-26 17:36:03 +00002420{
2421 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002422 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002423
2424 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2425 return -1;
2426 idx = SUBPAGE_IDX(start);
2427 eidx = SUBPAGE_IDX(end);
2428#if defined(DEBUG_SUBPAGE)
2429 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2430 mmio, start, end, idx, eidx, memory);
2431#endif
2432 memory >>= IO_MEM_SHIFT;
2433 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002434 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002435 if (io_mem_read[memory][i]) {
2436 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2437 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2438 }
2439 if (io_mem_write[memory][i]) {
2440 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2441 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2442 }
blueswir14254fab2008-01-01 16:57:19 +00002443 }
blueswir1db7b5422007-05-26 17:36:03 +00002444 }
2445
2446 return 0;
2447}
2448
aurel3200f82b82008-04-27 21:12:55 +00002449static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2450 ram_addr_t orig_memory)
blueswir1db7b5422007-05-26 17:36:03 +00002451{
2452 subpage_t *mmio;
2453 int subpage_memory;
2454
2455 mmio = qemu_mallocz(sizeof(subpage_t));
2456 if (mmio != NULL) {
2457 mmio->base = base;
2458 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2459#if defined(DEBUG_SUBPAGE)
2460 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2461 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2462#endif
2463 *phys = subpage_memory | IO_MEM_SUBPAGE;
2464 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2465 }
2466
2467 return mmio;
2468}
2469
bellard33417e72003-08-10 21:47:01 +00002470static void io_mem_init(void)
2471{
bellard3a7d9292005-08-21 09:26:42 +00002472 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002473 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002474 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002475 io_mem_nb = 5;
2476
pbrook6658ffb2007-03-16 23:58:11 +00002477#if defined(CONFIG_SOFTMMU)
2478 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2479 watch_mem_write, NULL);
2480#endif
bellard1ccde1c2004-02-06 19:46:14 +00002481 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002482 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002483 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002484}
2485
2486/* mem_read and mem_write are arrays of functions containing the
2487 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002488 2). Functions can be omitted with a NULL function pointer. The
2489 registered functions may be modified dynamically later.
2490 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002491 modified. If it is zero, a new io zone is allocated. The return
2492 value can be used with cpu_register_physical_memory(). (-1) is
2493 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002494int cpu_register_io_memory(int io_index,
2495 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002496 CPUWriteMemoryFunc **mem_write,
2497 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002498{
blueswir14254fab2008-01-01 16:57:19 +00002499 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002500
2501 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002502 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002503 return -1;
2504 io_index = io_mem_nb++;
2505 } else {
2506 if (io_index >= IO_MEM_NB_ENTRIES)
2507 return -1;
2508 }
bellardb5ff1b32005-11-26 10:38:39 +00002509
bellard33417e72003-08-10 21:47:01 +00002510 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002511 if (!mem_read[i] || !mem_write[i])
2512 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002513 io_mem_read[io_index][i] = mem_read[i];
2514 io_mem_write[io_index][i] = mem_write[i];
2515 }
bellarda4193c82004-06-03 14:01:43 +00002516 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002517 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002518}
bellard61382a52003-10-27 21:22:23 +00002519
bellard8926b512004-10-10 15:14:20 +00002520CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2521{
2522 return io_mem_write[io_index >> IO_MEM_SHIFT];
2523}
2524
2525CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2526{
2527 return io_mem_read[io_index >> IO_MEM_SHIFT];
2528}
2529
bellard13eb76e2004-01-24 15:23:36 +00002530/* physical memory access (slow version, mainly for debug) */
2531#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002532void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002533 int len, int is_write)
2534{
2535 int l, flags;
2536 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002537 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002538
2539 while (len > 0) {
2540 page = addr & TARGET_PAGE_MASK;
2541 l = (page + TARGET_PAGE_SIZE) - addr;
2542 if (l > len)
2543 l = len;
2544 flags = page_get_flags(page);
2545 if (!(flags & PAGE_VALID))
2546 return;
2547 if (is_write) {
2548 if (!(flags & PAGE_WRITE))
2549 return;
bellard579a97f2007-11-11 14:26:47 +00002550 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002551 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00002552 /* FIXME - should this return an error rather than just fail? */
2553 return;
aurel3272fb7da2008-04-27 23:53:45 +00002554 memcpy(p, buf, l);
2555 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00002556 } else {
2557 if (!(flags & PAGE_READ))
2558 return;
bellard579a97f2007-11-11 14:26:47 +00002559 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002560 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00002561 /* FIXME - should this return an error rather than just fail? */
2562 return;
aurel3272fb7da2008-04-27 23:53:45 +00002563 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00002564 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002565 }
2566 len -= l;
2567 buf += l;
2568 addr += l;
2569 }
2570}
bellard8df1cd02005-01-28 22:37:22 +00002571
bellard13eb76e2004-01-24 15:23:36 +00002572#else
ths5fafdf22007-09-16 21:08:06 +00002573void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002574 int len, int is_write)
2575{
2576 int l, io_index;
2577 uint8_t *ptr;
2578 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002579 target_phys_addr_t page;
2580 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002581 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002582
bellard13eb76e2004-01-24 15:23:36 +00002583 while (len > 0) {
2584 page = addr & TARGET_PAGE_MASK;
2585 l = (page + TARGET_PAGE_SIZE) - addr;
2586 if (l > len)
2587 l = len;
bellard92e873b2004-05-21 14:52:29 +00002588 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002589 if (!p) {
2590 pd = IO_MEM_UNASSIGNED;
2591 } else {
2592 pd = p->phys_offset;
2593 }
ths3b46e622007-09-17 08:09:54 +00002594
bellard13eb76e2004-01-24 15:23:36 +00002595 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002596 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002597 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002598 /* XXX: could force cpu_single_env to NULL to avoid
2599 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002600 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002601 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002602 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002603 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002604 l = 4;
2605 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002606 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002607 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002608 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002609 l = 2;
2610 } else {
bellard1c213d12005-09-03 10:49:04 +00002611 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002612 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002613 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002614 l = 1;
2615 }
2616 } else {
bellardb448f2f2004-02-25 23:24:04 +00002617 unsigned long addr1;
2618 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002619 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002620 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002621 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002622 if (!cpu_physical_memory_is_dirty(addr1)) {
2623 /* invalidate code */
2624 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2625 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002626 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002627 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002628 }
bellard13eb76e2004-01-24 15:23:36 +00002629 }
2630 } else {
ths5fafdf22007-09-16 21:08:06 +00002631 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002632 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002633 /* I/O case */
2634 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2635 if (l >= 4 && ((addr & 3) == 0)) {
2636 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002637 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002638 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002639 l = 4;
2640 } else if (l >= 2 && ((addr & 1) == 0)) {
2641 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002642 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002643 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002644 l = 2;
2645 } else {
bellard1c213d12005-09-03 10:49:04 +00002646 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002647 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002648 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002649 l = 1;
2650 }
2651 } else {
2652 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002653 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002654 (addr & ~TARGET_PAGE_MASK);
2655 memcpy(buf, ptr, l);
2656 }
2657 }
2658 len -= l;
2659 buf += l;
2660 addr += l;
2661 }
2662}
bellard8df1cd02005-01-28 22:37:22 +00002663
bellardd0ecd2a2006-04-23 17:14:48 +00002664/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002665void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002666 const uint8_t *buf, int len)
2667{
2668 int l;
2669 uint8_t *ptr;
2670 target_phys_addr_t page;
2671 unsigned long pd;
2672 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002673
bellardd0ecd2a2006-04-23 17:14:48 +00002674 while (len > 0) {
2675 page = addr & TARGET_PAGE_MASK;
2676 l = (page + TARGET_PAGE_SIZE) - addr;
2677 if (l > len)
2678 l = len;
2679 p = phys_page_find(page >> TARGET_PAGE_BITS);
2680 if (!p) {
2681 pd = IO_MEM_UNASSIGNED;
2682 } else {
2683 pd = p->phys_offset;
2684 }
ths3b46e622007-09-17 08:09:54 +00002685
bellardd0ecd2a2006-04-23 17:14:48 +00002686 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002687 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2688 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002689 /* do nothing */
2690 } else {
2691 unsigned long addr1;
2692 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2693 /* ROM/RAM case */
2694 ptr = phys_ram_base + addr1;
2695 memcpy(ptr, buf, l);
2696 }
2697 len -= l;
2698 buf += l;
2699 addr += l;
2700 }
2701}
2702
2703
bellard8df1cd02005-01-28 22:37:22 +00002704/* warning: addr must be aligned */
2705uint32_t ldl_phys(target_phys_addr_t addr)
2706{
2707 int io_index;
2708 uint8_t *ptr;
2709 uint32_t val;
2710 unsigned long pd;
2711 PhysPageDesc *p;
2712
2713 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2714 if (!p) {
2715 pd = IO_MEM_UNASSIGNED;
2716 } else {
2717 pd = p->phys_offset;
2718 }
ths3b46e622007-09-17 08:09:54 +00002719
ths5fafdf22007-09-16 21:08:06 +00002720 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002721 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002722 /* I/O case */
2723 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2724 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2725 } else {
2726 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002727 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002728 (addr & ~TARGET_PAGE_MASK);
2729 val = ldl_p(ptr);
2730 }
2731 return val;
2732}
2733
bellard84b7b8e2005-11-28 21:19:04 +00002734/* warning: addr must be aligned */
2735uint64_t ldq_phys(target_phys_addr_t addr)
2736{
2737 int io_index;
2738 uint8_t *ptr;
2739 uint64_t val;
2740 unsigned long pd;
2741 PhysPageDesc *p;
2742
2743 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2744 if (!p) {
2745 pd = IO_MEM_UNASSIGNED;
2746 } else {
2747 pd = p->phys_offset;
2748 }
ths3b46e622007-09-17 08:09:54 +00002749
bellard2a4188a2006-06-25 21:54:59 +00002750 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2751 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002752 /* I/O case */
2753 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2754#ifdef TARGET_WORDS_BIGENDIAN
2755 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2756 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2757#else
2758 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2759 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2760#endif
2761 } else {
2762 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002763 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002764 (addr & ~TARGET_PAGE_MASK);
2765 val = ldq_p(ptr);
2766 }
2767 return val;
2768}
2769
bellardaab33092005-10-30 20:48:42 +00002770/* XXX: optimize */
2771uint32_t ldub_phys(target_phys_addr_t addr)
2772{
2773 uint8_t val;
2774 cpu_physical_memory_read(addr, &val, 1);
2775 return val;
2776}
2777
2778/* XXX: optimize */
2779uint32_t lduw_phys(target_phys_addr_t addr)
2780{
2781 uint16_t val;
2782 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2783 return tswap16(val);
2784}
2785
bellard8df1cd02005-01-28 22:37:22 +00002786/* warning: addr must be aligned. The ram page is not masked as dirty
2787 and the code inside is not invalidated. It is useful if the dirty
2788 bits are used to track modified PTEs */
2789void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2790{
2791 int io_index;
2792 uint8_t *ptr;
2793 unsigned long pd;
2794 PhysPageDesc *p;
2795
2796 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2797 if (!p) {
2798 pd = IO_MEM_UNASSIGNED;
2799 } else {
2800 pd = p->phys_offset;
2801 }
ths3b46e622007-09-17 08:09:54 +00002802
bellard3a7d9292005-08-21 09:26:42 +00002803 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002804 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2805 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2806 } else {
ths5fafdf22007-09-16 21:08:06 +00002807 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002808 (addr & ~TARGET_PAGE_MASK);
2809 stl_p(ptr, val);
2810 }
2811}
2812
j_mayerbc98a7e2007-04-04 07:55:12 +00002813void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2814{
2815 int io_index;
2816 uint8_t *ptr;
2817 unsigned long pd;
2818 PhysPageDesc *p;
2819
2820 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2821 if (!p) {
2822 pd = IO_MEM_UNASSIGNED;
2823 } else {
2824 pd = p->phys_offset;
2825 }
ths3b46e622007-09-17 08:09:54 +00002826
j_mayerbc98a7e2007-04-04 07:55:12 +00002827 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2828 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2829#ifdef TARGET_WORDS_BIGENDIAN
2830 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2831 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2832#else
2833 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2834 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2835#endif
2836 } else {
ths5fafdf22007-09-16 21:08:06 +00002837 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002838 (addr & ~TARGET_PAGE_MASK);
2839 stq_p(ptr, val);
2840 }
2841}
2842
bellard8df1cd02005-01-28 22:37:22 +00002843/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002844void stl_phys(target_phys_addr_t addr, uint32_t val)
2845{
2846 int io_index;
2847 uint8_t *ptr;
2848 unsigned long pd;
2849 PhysPageDesc *p;
2850
2851 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2852 if (!p) {
2853 pd = IO_MEM_UNASSIGNED;
2854 } else {
2855 pd = p->phys_offset;
2856 }
ths3b46e622007-09-17 08:09:54 +00002857
bellard3a7d9292005-08-21 09:26:42 +00002858 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002859 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2860 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2861 } else {
2862 unsigned long addr1;
2863 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2864 /* RAM case */
2865 ptr = phys_ram_base + addr1;
2866 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002867 if (!cpu_physical_memory_is_dirty(addr1)) {
2868 /* invalidate code */
2869 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2870 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002871 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2872 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002873 }
bellard8df1cd02005-01-28 22:37:22 +00002874 }
2875}
2876
bellardaab33092005-10-30 20:48:42 +00002877/* XXX: optimize */
2878void stb_phys(target_phys_addr_t addr, uint32_t val)
2879{
2880 uint8_t v = val;
2881 cpu_physical_memory_write(addr, &v, 1);
2882}
2883
2884/* XXX: optimize */
2885void stw_phys(target_phys_addr_t addr, uint32_t val)
2886{
2887 uint16_t v = tswap16(val);
2888 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2889}
2890
2891/* XXX: optimize */
2892void stq_phys(target_phys_addr_t addr, uint64_t val)
2893{
2894 val = tswap64(val);
2895 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2896}
2897
bellard13eb76e2004-01-24 15:23:36 +00002898#endif
2899
2900/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002901int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002902 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002903{
2904 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002905 target_phys_addr_t phys_addr;
2906 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002907
2908 while (len > 0) {
2909 page = addr & TARGET_PAGE_MASK;
2910 phys_addr = cpu_get_phys_page_debug(env, page);
2911 /* if no physical page mapped, return an error */
2912 if (phys_addr == -1)
2913 return -1;
2914 l = (page + TARGET_PAGE_SIZE) - addr;
2915 if (l > len)
2916 l = len;
ths5fafdf22007-09-16 21:08:06 +00002917 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00002918 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002919 len -= l;
2920 buf += l;
2921 addr += l;
2922 }
2923 return 0;
2924}
2925
bellarde3db7222005-01-26 22:00:47 +00002926void dump_exec_info(FILE *f,
2927 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
2928{
2929 int i, target_code_size, max_target_code_size;
2930 int direct_jmp_count, direct_jmp2_count, cross_page;
2931 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00002932
bellarde3db7222005-01-26 22:00:47 +00002933 target_code_size = 0;
2934 max_target_code_size = 0;
2935 cross_page = 0;
2936 direct_jmp_count = 0;
2937 direct_jmp2_count = 0;
2938 for(i = 0; i < nb_tbs; i++) {
2939 tb = &tbs[i];
2940 target_code_size += tb->size;
2941 if (tb->size > max_target_code_size)
2942 max_target_code_size = tb->size;
2943 if (tb->page_addr[1] != -1)
2944 cross_page++;
2945 if (tb->tb_next_offset[0] != 0xffff) {
2946 direct_jmp_count++;
2947 if (tb->tb_next_offset[1] != 0xffff) {
2948 direct_jmp2_count++;
2949 }
2950 }
2951 }
2952 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00002953 cpu_fprintf(f, "Translation buffer state:\n");
bellarde3db7222005-01-26 22:00:47 +00002954 cpu_fprintf(f, "TB count %d\n", nb_tbs);
ths5fafdf22007-09-16 21:08:06 +00002955 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00002956 nb_tbs ? target_code_size / nb_tbs : 0,
2957 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00002958 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00002959 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
2960 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00002961 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
2962 cross_page,
bellarde3db7222005-01-26 22:00:47 +00002963 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
2964 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00002965 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00002966 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
2967 direct_jmp2_count,
2968 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00002969 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00002970 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
2971 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
2972 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellard57fec1f2008-02-01 10:50:11 +00002973#ifdef CONFIG_PROFILER
2974 {
2975 int64_t tot;
2976 tot = dyngen_interm_time + dyngen_code_time;
2977 cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2978 tot, tot / 2.4e9);
2979 cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2980 dyngen_tb_count,
2981 dyngen_tb_count1 - dyngen_tb_count,
2982 dyngen_tb_count1 ? (double)(dyngen_tb_count1 - dyngen_tb_count) / dyngen_tb_count1 * 100.0 : 0);
2983 cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n",
2984 dyngen_tb_count ? (double)dyngen_op_count / dyngen_tb_count : 0, dyngen_op_count_max);
2985 cpu_fprintf(f, "old ops/total ops %0.1f%%\n",
2986 dyngen_op_count ? (double)dyngen_old_op_count / dyngen_op_count * 100.0 : 0);
2987 cpu_fprintf(f, "deleted ops/TB %0.2f\n",
2988 dyngen_tb_count ?
2989 (double)dyngen_tcg_del_op_count / dyngen_tb_count : 0);
2990 cpu_fprintf(f, "cycles/op %0.1f\n",
2991 dyngen_op_count ? (double)tot / dyngen_op_count : 0);
2992 cpu_fprintf(f, "cycles/in byte %0.1f\n",
2993 dyngen_code_in_len ? (double)tot / dyngen_code_in_len : 0);
2994 cpu_fprintf(f, "cycles/out byte %0.1f\n",
2995 dyngen_code_out_len ? (double)tot / dyngen_code_out_len : 0);
2996 if (tot == 0)
2997 tot = 1;
2998 cpu_fprintf(f, " gen_interm time %0.1f%%\n",
2999 (double)dyngen_interm_time / tot * 100.0);
3000 cpu_fprintf(f, " gen_code time %0.1f%%\n",
3001 (double)dyngen_code_time / tot * 100.0);
3002 cpu_fprintf(f, "cpu_restore count %" PRId64 "\n",
3003 dyngen_restore_count);
3004 cpu_fprintf(f, " avg cycles %0.1f\n",
3005 dyngen_restore_count ? (double)dyngen_restore_time / dyngen_restore_count : 0);
3006 {
3007 extern void dump_op_count(void);
3008 dump_op_count();
3009 }
3010 }
3011#endif
bellarde3db7222005-01-26 22:00:47 +00003012}
3013
ths5fafdf22007-09-16 21:08:06 +00003014#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003015
3016#define MMUSUFFIX _cmmu
3017#define GETPC() NULL
3018#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003019#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003020
3021#define SHIFT 0
3022#include "softmmu_template.h"
3023
3024#define SHIFT 1
3025#include "softmmu_template.h"
3026
3027#define SHIFT 2
3028#include "softmmu_template.h"
3029
3030#define SHIFT 3
3031#include "softmmu_template.h"
3032
3033#undef env
3034
3035#endif