blob: 8015202a68dc870164b06b746259dd5c7da25d12 [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"
pbrook53a59602006-03-25 19:31:22 +000038#if defined(CONFIG_USER_ONLY)
39#include <qemu.h>
40#endif
bellard54936002003-05-13 00:25:15 +000041
bellardfd6ce8f2003-05-14 19:00:11 +000042//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000043//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000044//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000045//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000046
47/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000048//#define DEBUG_TB_CHECK
49//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000050
ths1196be32007-03-17 15:17:58 +000051//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000052//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000053
pbrook99773bd2006-04-16 15:14:59 +000054#if !defined(CONFIG_USER_ONLY)
55/* TB consistency checks only implemented for usermode emulation. */
56#undef DEBUG_TB_CHECK
57#endif
58
bellardfd6ce8f2003-05-14 19:00:11 +000059/* threshold to flush the translated code buffer */
blueswir1d07bde82007-12-11 19:35:45 +000060#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - code_gen_max_block_size())
bellardfd6ce8f2003-05-14 19:00:11 +000061
bellard9fa3e852004-01-04 18:06:42 +000062#define SMC_BITMAP_USE_THRESHOLD 10
63
64#define MMAP_AREA_START 0x00000000
65#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000066
bellard108c49b2005-07-24 12:55:09 +000067#if defined(TARGET_SPARC64)
68#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000069#elif defined(TARGET_SPARC)
70#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000071#elif defined(TARGET_ALPHA)
72#define TARGET_PHYS_ADDR_SPACE_BITS 42
73#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000074#elif defined(TARGET_PPC64)
75#define TARGET_PHYS_ADDR_SPACE_BITS 42
76#else
77/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
78#define TARGET_PHYS_ADDR_SPACE_BITS 32
79#endif
80
bellardfd6ce8f2003-05-14 19:00:11 +000081TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
bellard9fa3e852004-01-04 18:06:42 +000082TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000083int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000084/* any access to the tbs or the page table must use this lock */
85spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000086
bellardb8076a72005-04-07 22:20:31 +000087uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
bellardfd6ce8f2003-05-14 19:00:11 +000088uint8_t *code_gen_ptr;
89
bellard9fa3e852004-01-04 18:06:42 +000090int phys_ram_size;
91int phys_ram_fd;
92uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +000093uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +000094static ram_addr_t phys_ram_alloc_offset = 0;
bellard9fa3e852004-01-04 18:06:42 +000095
bellard6a00d602005-11-21 23:25:50 +000096CPUState *first_cpu;
97/* current CPU in the current thread. It is only valid inside
98 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +000099CPUState *cpu_single_env;
bellard6a00d602005-11-21 23:25:50 +0000100
bellard54936002003-05-13 00:25:15 +0000101typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000102 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000103 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000104 /* in order to optimize self modifying code, we count the number
105 of lookups we do to a given page to use a bitmap */
106 unsigned int code_write_count;
107 uint8_t *code_bitmap;
108#if defined(CONFIG_USER_ONLY)
109 unsigned long flags;
110#endif
bellard54936002003-05-13 00:25:15 +0000111} PageDesc;
112
bellard92e873b2004-05-21 14:52:29 +0000113typedef struct PhysPageDesc {
114 /* offset in host memory of the page + io_index in the low 12 bits */
bellarde04f40b2005-04-24 18:02:38 +0000115 uint32_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000116} PhysPageDesc;
117
bellard54936002003-05-13 00:25:15 +0000118#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000119#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
120/* XXX: this is a temporary hack for alpha target.
121 * In the future, this is to be replaced by a multi-level table
122 * to actually be able to handle the complete 64 bits address space.
123 */
124#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
125#else
bellard54936002003-05-13 00:25:15 +0000126#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000127#endif
bellard54936002003-05-13 00:25:15 +0000128
129#define L1_SIZE (1 << L1_BITS)
130#define L2_SIZE (1 << L2_BITS)
131
bellard33417e72003-08-10 21:47:01 +0000132static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000133
bellard83fb7ad2004-07-05 21:25:26 +0000134unsigned long qemu_real_host_page_size;
135unsigned long qemu_host_page_bits;
136unsigned long qemu_host_page_size;
137unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000138
bellard92e873b2004-05-21 14:52:29 +0000139/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000140static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000141PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000142
bellard33417e72003-08-10 21:47:01 +0000143/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000144CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
145CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000146void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000147static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000148#if defined(CONFIG_SOFTMMU)
149static int io_mem_watch;
150#endif
bellard33417e72003-08-10 21:47:01 +0000151
bellard34865132003-10-05 14:28:56 +0000152/* log support */
153char *logfilename = "/tmp/qemu.log";
154FILE *logfile;
155int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000156static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000157
bellarde3db7222005-01-26 22:00:47 +0000158/* statistics */
159static int tlb_flush_count;
160static int tb_flush_count;
161static int tb_phys_invalidate_count;
162
blueswir1db7b5422007-05-26 17:36:03 +0000163#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
164typedef struct subpage_t {
165 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000166 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
167 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
168 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000169} subpage_t;
170
bellardb346ff42003-06-15 20:05:50 +0000171static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000172{
bellard83fb7ad2004-07-05 21:25:26 +0000173 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000174 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000175#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000176 {
177 SYSTEM_INFO system_info;
178 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000179
bellardd5a8f072004-09-29 21:15:28 +0000180 GetSystemInfo(&system_info);
181 qemu_real_host_page_size = system_info.dwPageSize;
ths3b46e622007-09-17 08:09:54 +0000182
bellardd5a8f072004-09-29 21:15:28 +0000183 VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
184 PAGE_EXECUTE_READWRITE, &old_protect);
185 }
bellard67b915a2004-03-31 23:37:16 +0000186#else
bellard83fb7ad2004-07-05 21:25:26 +0000187 qemu_real_host_page_size = getpagesize();
bellardd5a8f072004-09-29 21:15:28 +0000188 {
189 unsigned long start, end;
190
191 start = (unsigned long)code_gen_buffer;
192 start &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000193
bellardd5a8f072004-09-29 21:15:28 +0000194 end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
195 end += qemu_real_host_page_size - 1;
196 end &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000197
ths5fafdf22007-09-16 21:08:06 +0000198 mprotect((void *)start, end - start,
bellardd5a8f072004-09-29 21:15:28 +0000199 PROT_READ | PROT_WRITE | PROT_EXEC);
200 }
bellard67b915a2004-03-31 23:37:16 +0000201#endif
bellardd5a8f072004-09-29 21:15:28 +0000202
bellard83fb7ad2004-07-05 21:25:26 +0000203 if (qemu_host_page_size == 0)
204 qemu_host_page_size = qemu_real_host_page_size;
205 if (qemu_host_page_size < TARGET_PAGE_SIZE)
206 qemu_host_page_size = TARGET_PAGE_SIZE;
207 qemu_host_page_bits = 0;
208 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
209 qemu_host_page_bits++;
210 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000211 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
212 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000213
214#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
215 {
216 long long startaddr, endaddr;
217 FILE *f;
218 int n;
219
220 f = fopen("/proc/self/maps", "r");
221 if (f) {
222 do {
223 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
224 if (n == 2) {
225 page_set_flags(TARGET_PAGE_ALIGN(startaddr),
226 TARGET_PAGE_ALIGN(endaddr),
227 PAGE_RESERVED);
228 }
229 } while (!feof(f));
230 fclose(f);
231 }
232 }
233#endif
bellard54936002003-05-13 00:25:15 +0000234}
235
bellardfd6ce8f2003-05-14 19:00:11 +0000236static inline PageDesc *page_find_alloc(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000237{
bellard54936002003-05-13 00:25:15 +0000238 PageDesc **lp, *p;
239
bellard54936002003-05-13 00:25:15 +0000240 lp = &l1_map[index >> L2_BITS];
241 p = *lp;
242 if (!p) {
243 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000244 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000245 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000246 *lp = p;
247 }
248 return p + (index & (L2_SIZE - 1));
249}
250
bellardfd6ce8f2003-05-14 19:00:11 +0000251static inline PageDesc *page_find(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000252{
bellard54936002003-05-13 00:25:15 +0000253 PageDesc *p;
254
bellard54936002003-05-13 00:25:15 +0000255 p = l1_map[index >> L2_BITS];
256 if (!p)
257 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000258 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000259}
260
bellard108c49b2005-07-24 12:55:09 +0000261static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000262{
bellard108c49b2005-07-24 12:55:09 +0000263 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000264 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000265
bellard108c49b2005-07-24 12:55:09 +0000266 p = (void **)l1_phys_map;
267#if TARGET_PHYS_ADDR_SPACE_BITS > 32
268
269#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
270#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
271#endif
272 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000273 p = *lp;
274 if (!p) {
275 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000276 if (!alloc)
277 return NULL;
278 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
279 memset(p, 0, sizeof(void *) * L1_SIZE);
280 *lp = p;
281 }
282#endif
283 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000284 pd = *lp;
285 if (!pd) {
286 int i;
bellard108c49b2005-07-24 12:55:09 +0000287 /* allocate if not found */
288 if (!alloc)
289 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000290 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
291 *lp = pd;
292 for (i = 0; i < L2_SIZE; i++)
293 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000294 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000295 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000296}
297
bellard108c49b2005-07-24 12:55:09 +0000298static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000299{
bellard108c49b2005-07-24 12:55:09 +0000300 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000301}
302
bellard9fa3e852004-01-04 18:06:42 +0000303#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000304static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000305static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000306 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000307#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000308
bellard6a00d602005-11-21 23:25:50 +0000309void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000310{
bellard6a00d602005-11-21 23:25:50 +0000311 CPUState **penv;
312 int cpu_index;
313
bellardfd6ce8f2003-05-14 19:00:11 +0000314 if (!code_gen_ptr) {
bellard57fec1f2008-02-01 10:50:11 +0000315 cpu_gen_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000316 code_gen_ptr = code_gen_buffer;
bellardb346ff42003-06-15 20:05:50 +0000317 page_init();
bellard33417e72003-08-10 21:47:01 +0000318 io_mem_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000319 }
bellard6a00d602005-11-21 23:25:50 +0000320 env->next_cpu = NULL;
321 penv = &first_cpu;
322 cpu_index = 0;
323 while (*penv != NULL) {
324 penv = (CPUState **)&(*penv)->next_cpu;
325 cpu_index++;
326 }
327 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000328 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000329 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000330}
331
bellard9fa3e852004-01-04 18:06:42 +0000332static inline void invalidate_page_bitmap(PageDesc *p)
333{
334 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000335 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000336 p->code_bitmap = NULL;
337 }
338 p->code_write_count = 0;
339}
340
bellardfd6ce8f2003-05-14 19:00:11 +0000341/* set to NULL all the 'first_tb' fields in all PageDescs */
342static void page_flush_tb(void)
343{
344 int i, j;
345 PageDesc *p;
346
347 for(i = 0; i < L1_SIZE; i++) {
348 p = l1_map[i];
349 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000350 for(j = 0; j < L2_SIZE; j++) {
351 p->first_tb = NULL;
352 invalidate_page_bitmap(p);
353 p++;
354 }
bellardfd6ce8f2003-05-14 19:00:11 +0000355 }
356 }
357}
358
359/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000360/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000361void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000362{
bellard6a00d602005-11-21 23:25:50 +0000363 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000364#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000365 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
366 (unsigned long)(code_gen_ptr - code_gen_buffer),
367 nb_tbs, nb_tbs > 0 ?
368 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000369#endif
pbrooka208e542008-03-31 17:07:36 +0000370 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > CODE_GEN_BUFFER_SIZE)
371 cpu_abort(env1, "Internal error: code buffer overflow\n");
372
bellardfd6ce8f2003-05-14 19:00:11 +0000373 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000374
bellard6a00d602005-11-21 23:25:50 +0000375 for(env = first_cpu; env != NULL; env = env->next_cpu) {
376 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
377 }
bellard9fa3e852004-01-04 18:06:42 +0000378
bellard8a8a6082004-10-03 13:36:49 +0000379 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000380 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000381
bellardfd6ce8f2003-05-14 19:00:11 +0000382 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000383 /* XXX: flush processor icache at this point if cache flush is
384 expensive */
bellarde3db7222005-01-26 22:00:47 +0000385 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000386}
387
388#ifdef DEBUG_TB_CHECK
389
j_mayerbc98a7e2007-04-04 07:55:12 +0000390static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000391{
392 TranslationBlock *tb;
393 int i;
394 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000395 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
396 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000397 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
398 address >= tb->pc + tb->size)) {
399 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000400 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000401 }
402 }
403 }
404}
405
406/* verify that all the pages have correct rights for code */
407static void tb_page_check(void)
408{
409 TranslationBlock *tb;
410 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000411
pbrook99773bd2006-04-16 15:14:59 +0000412 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
413 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000414 flags1 = page_get_flags(tb->pc);
415 flags2 = page_get_flags(tb->pc + tb->size - 1);
416 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
417 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000418 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000419 }
420 }
421 }
422}
423
bellardd4e81642003-05-25 16:46:15 +0000424void tb_jmp_check(TranslationBlock *tb)
425{
426 TranslationBlock *tb1;
427 unsigned int n1;
428
429 /* suppress any remaining jumps to this TB */
430 tb1 = tb->jmp_first;
431 for(;;) {
432 n1 = (long)tb1 & 3;
433 tb1 = (TranslationBlock *)((long)tb1 & ~3);
434 if (n1 == 2)
435 break;
436 tb1 = tb1->jmp_next[n1];
437 }
438 /* check end of list */
439 if (tb1 != tb) {
440 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
441 }
442}
443
bellardfd6ce8f2003-05-14 19:00:11 +0000444#endif
445
446/* invalidate one TB */
447static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
448 int next_offset)
449{
450 TranslationBlock *tb1;
451 for(;;) {
452 tb1 = *ptb;
453 if (tb1 == tb) {
454 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
455 break;
456 }
457 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
458 }
459}
460
bellard9fa3e852004-01-04 18:06:42 +0000461static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
462{
463 TranslationBlock *tb1;
464 unsigned int n1;
465
466 for(;;) {
467 tb1 = *ptb;
468 n1 = (long)tb1 & 3;
469 tb1 = (TranslationBlock *)((long)tb1 & ~3);
470 if (tb1 == tb) {
471 *ptb = tb1->page_next[n1];
472 break;
473 }
474 ptb = &tb1->page_next[n1];
475 }
476}
477
bellardd4e81642003-05-25 16:46:15 +0000478static inline void tb_jmp_remove(TranslationBlock *tb, int n)
479{
480 TranslationBlock *tb1, **ptb;
481 unsigned int n1;
482
483 ptb = &tb->jmp_next[n];
484 tb1 = *ptb;
485 if (tb1) {
486 /* find tb(n) in circular list */
487 for(;;) {
488 tb1 = *ptb;
489 n1 = (long)tb1 & 3;
490 tb1 = (TranslationBlock *)((long)tb1 & ~3);
491 if (n1 == n && tb1 == tb)
492 break;
493 if (n1 == 2) {
494 ptb = &tb1->jmp_first;
495 } else {
496 ptb = &tb1->jmp_next[n1];
497 }
498 }
499 /* now we can suppress tb(n) from the list */
500 *ptb = tb->jmp_next[n];
501
502 tb->jmp_next[n] = NULL;
503 }
504}
505
506/* reset the jump entry 'n' of a TB so that it is not chained to
507 another TB */
508static inline void tb_reset_jump(TranslationBlock *tb, int n)
509{
510 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
511}
512
bellard9fa3e852004-01-04 18:06:42 +0000513static inline void tb_phys_invalidate(TranslationBlock *tb, unsigned int page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000514{
bellard6a00d602005-11-21 23:25:50 +0000515 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000516 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000517 unsigned int h, n1;
bellard9fa3e852004-01-04 18:06:42 +0000518 target_ulong phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000519 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000520
bellard9fa3e852004-01-04 18:06:42 +0000521 /* remove the TB from the hash list */
522 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
523 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000524 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000525 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000526
bellard9fa3e852004-01-04 18:06:42 +0000527 /* remove the TB from the page list */
528 if (tb->page_addr[0] != page_addr) {
529 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
530 tb_page_remove(&p->first_tb, tb);
531 invalidate_page_bitmap(p);
532 }
533 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
534 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
535 tb_page_remove(&p->first_tb, tb);
536 invalidate_page_bitmap(p);
537 }
538
bellard8a40a182005-11-20 10:35:40 +0000539 tb_invalidated_flag = 1;
540
541 /* remove the TB from the hash list */
542 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000543 for(env = first_cpu; env != NULL; env = env->next_cpu) {
544 if (env->tb_jmp_cache[h] == tb)
545 env->tb_jmp_cache[h] = NULL;
546 }
bellard8a40a182005-11-20 10:35:40 +0000547
548 /* suppress this TB from the two jump lists */
549 tb_jmp_remove(tb, 0);
550 tb_jmp_remove(tb, 1);
551
552 /* suppress any remaining jumps to this TB */
553 tb1 = tb->jmp_first;
554 for(;;) {
555 n1 = (long)tb1 & 3;
556 if (n1 == 2)
557 break;
558 tb1 = (TranslationBlock *)((long)tb1 & ~3);
559 tb2 = tb1->jmp_next[n1];
560 tb_reset_jump(tb1, n1);
561 tb1->jmp_next[n1] = NULL;
562 tb1 = tb2;
563 }
564 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
565
bellarde3db7222005-01-26 22:00:47 +0000566 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000567}
568
569static inline void set_bits(uint8_t *tab, int start, int len)
570{
571 int end, mask, end1;
572
573 end = start + len;
574 tab += start >> 3;
575 mask = 0xff << (start & 7);
576 if ((start & ~7) == (end & ~7)) {
577 if (start < end) {
578 mask &= ~(0xff << (end & 7));
579 *tab |= mask;
580 }
581 } else {
582 *tab++ |= mask;
583 start = (start + 8) & ~7;
584 end1 = end & ~7;
585 while (start < end1) {
586 *tab++ = 0xff;
587 start += 8;
588 }
589 if (start < end) {
590 mask = ~(0xff << (end & 7));
591 *tab |= mask;
592 }
593 }
594}
595
596static void build_page_bitmap(PageDesc *p)
597{
598 int n, tb_start, tb_end;
599 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000600
bellard59817cc2004-02-16 22:01:13 +0000601 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000602 if (!p->code_bitmap)
603 return;
604 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
605
606 tb = p->first_tb;
607 while (tb != NULL) {
608 n = (long)tb & 3;
609 tb = (TranslationBlock *)((long)tb & ~3);
610 /* NOTE: this is subtle as a TB may span two physical pages */
611 if (n == 0) {
612 /* NOTE: tb_end may be after the end of the page, but
613 it is not a problem */
614 tb_start = tb->pc & ~TARGET_PAGE_MASK;
615 tb_end = tb_start + tb->size;
616 if (tb_end > TARGET_PAGE_SIZE)
617 tb_end = TARGET_PAGE_SIZE;
618 } else {
619 tb_start = 0;
620 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
621 }
622 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
623 tb = tb->page_next[n];
624 }
625}
626
bellardd720b932004-04-25 17:57:43 +0000627#ifdef TARGET_HAS_PRECISE_SMC
628
ths5fafdf22007-09-16 21:08:06 +0000629static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000630 target_ulong pc, target_ulong cs_base, int flags,
631 int cflags)
632{
633 TranslationBlock *tb;
634 uint8_t *tc_ptr;
635 target_ulong phys_pc, phys_page2, virt_page2;
636 int code_gen_size;
637
bellardc27004e2005-01-03 23:35:10 +0000638 phys_pc = get_phys_addr_code(env, pc);
639 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000640 if (!tb) {
641 /* flush must be done */
642 tb_flush(env);
643 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000644 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000645 }
646 tc_ptr = code_gen_ptr;
647 tb->tc_ptr = tc_ptr;
648 tb->cs_base = cs_base;
649 tb->flags = flags;
650 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000651 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000652 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 +0000653
bellardd720b932004-04-25 17:57:43 +0000654 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000655 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000656 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000657 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000658 phys_page2 = get_phys_addr_code(env, virt_page2);
659 }
660 tb_link_phys(tb, phys_pc, phys_page2);
661}
662#endif
ths3b46e622007-09-17 08:09:54 +0000663
bellard9fa3e852004-01-04 18:06:42 +0000664/* invalidate all TBs which intersect with the target physical page
665 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000666 the same physical page. 'is_cpu_write_access' should be true if called
667 from a real cpu write access: the virtual CPU will exit the current
668 TB if code is modified inside this TB. */
ths5fafdf22007-09-16 21:08:06 +0000669void tb_invalidate_phys_page_range(target_ulong start, target_ulong end,
bellardd720b932004-04-25 17:57:43 +0000670 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000671{
bellardd720b932004-04-25 17:57:43 +0000672 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000673 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000674 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000675 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000676 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000677 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000678
679 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000680 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000681 return;
ths5fafdf22007-09-16 21:08:06 +0000682 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000683 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
684 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000685 /* build code bitmap */
686 build_page_bitmap(p);
687 }
688
689 /* we remove all the TBs in the range [start, end[ */
690 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000691 current_tb_not_found = is_cpu_write_access;
692 current_tb_modified = 0;
693 current_tb = NULL; /* avoid warning */
694 current_pc = 0; /* avoid warning */
695 current_cs_base = 0; /* avoid warning */
696 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000697 tb = p->first_tb;
698 while (tb != NULL) {
699 n = (long)tb & 3;
700 tb = (TranslationBlock *)((long)tb & ~3);
701 tb_next = tb->page_next[n];
702 /* NOTE: this is subtle as a TB may span two physical pages */
703 if (n == 0) {
704 /* NOTE: tb_end may be after the end of the page, but
705 it is not a problem */
706 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
707 tb_end = tb_start + tb->size;
708 } else {
709 tb_start = tb->page_addr[1];
710 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
711 }
712 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000713#ifdef TARGET_HAS_PRECISE_SMC
714 if (current_tb_not_found) {
715 current_tb_not_found = 0;
716 current_tb = NULL;
717 if (env->mem_write_pc) {
718 /* now we have a real cpu fault */
719 current_tb = tb_find_pc(env->mem_write_pc);
720 }
721 }
722 if (current_tb == tb &&
723 !(current_tb->cflags & CF_SINGLE_INSN)) {
724 /* If we are modifying the current TB, we must stop
725 its execution. We could be more precise by checking
726 that the modification is after the current PC, but it
727 would require a specialized function to partially
728 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000729
bellardd720b932004-04-25 17:57:43 +0000730 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000731 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000732 env->mem_write_pc, NULL);
733#if defined(TARGET_I386)
734 current_flags = env->hflags;
735 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
736 current_cs_base = (target_ulong)env->segs[R_CS].base;
737 current_pc = current_cs_base + env->eip;
738#else
739#error unsupported CPU
740#endif
741 }
742#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000743 /* we need to do that to handle the case where a signal
744 occurs while doing tb_phys_invalidate() */
745 saved_tb = NULL;
746 if (env) {
747 saved_tb = env->current_tb;
748 env->current_tb = NULL;
749 }
bellard9fa3e852004-01-04 18:06:42 +0000750 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000751 if (env) {
752 env->current_tb = saved_tb;
753 if (env->interrupt_request && env->current_tb)
754 cpu_interrupt(env, env->interrupt_request);
755 }
bellard9fa3e852004-01-04 18:06:42 +0000756 }
757 tb = tb_next;
758 }
759#if !defined(CONFIG_USER_ONLY)
760 /* if no code remaining, no need to continue to use slow writes */
761 if (!p->first_tb) {
762 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000763 if (is_cpu_write_access) {
764 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
765 }
766 }
767#endif
768#ifdef TARGET_HAS_PRECISE_SMC
769 if (current_tb_modified) {
770 /* we generate a block containing just the instruction
771 modifying the memory. It will ensure that it cannot modify
772 itself */
bellardea1c1802004-06-14 18:56:36 +0000773 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000774 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000775 CF_SINGLE_INSN);
776 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000777 }
778#endif
779}
780
781/* len must be <= 8 and start must be a multiple of len */
bellardd720b932004-04-25 17:57:43 +0000782static inline void tb_invalidate_phys_page_fast(target_ulong start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000783{
784 PageDesc *p;
785 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000786#if 0
bellarda4193c82004-06-03 14:01:43 +0000787 if (1) {
788 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000789 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
790 cpu_single_env->mem_write_vaddr, len,
791 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000792 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
793 }
bellard59817cc2004-02-16 22:01:13 +0000794 }
795#endif
bellard9fa3e852004-01-04 18:06:42 +0000796 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000797 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000798 return;
799 if (p->code_bitmap) {
800 offset = start & ~TARGET_PAGE_MASK;
801 b = p->code_bitmap[offset >> 3] >> (offset & 7);
802 if (b & ((1 << len) - 1))
803 goto do_invalidate;
804 } else {
805 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000806 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000807 }
808}
809
bellard9fa3e852004-01-04 18:06:42 +0000810#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +0000811static void tb_invalidate_phys_page(target_ulong addr,
bellardd720b932004-04-25 17:57:43 +0000812 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000813{
bellardd720b932004-04-25 17:57:43 +0000814 int n, current_flags, current_tb_modified;
815 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000816 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000817 TranslationBlock *tb, *current_tb;
818#ifdef TARGET_HAS_PRECISE_SMC
819 CPUState *env = cpu_single_env;
820#endif
bellard9fa3e852004-01-04 18:06:42 +0000821
822 addr &= TARGET_PAGE_MASK;
823 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000824 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000825 return;
826 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000827 current_tb_modified = 0;
828 current_tb = NULL;
829 current_pc = 0; /* avoid warning */
830 current_cs_base = 0; /* avoid warning */
831 current_flags = 0; /* avoid warning */
832#ifdef TARGET_HAS_PRECISE_SMC
833 if (tb && pc != 0) {
834 current_tb = tb_find_pc(pc);
835 }
836#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000837 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000838 n = (long)tb & 3;
839 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000840#ifdef TARGET_HAS_PRECISE_SMC
841 if (current_tb == tb &&
842 !(current_tb->cflags & CF_SINGLE_INSN)) {
843 /* If we are modifying the current TB, we must stop
844 its execution. We could be more precise by checking
845 that the modification is after the current PC, but it
846 would require a specialized function to partially
847 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000848
bellardd720b932004-04-25 17:57:43 +0000849 current_tb_modified = 1;
850 cpu_restore_state(current_tb, env, pc, puc);
851#if defined(TARGET_I386)
852 current_flags = env->hflags;
853 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
854 current_cs_base = (target_ulong)env->segs[R_CS].base;
855 current_pc = current_cs_base + env->eip;
856#else
857#error unsupported CPU
858#endif
859 }
860#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000861 tb_phys_invalidate(tb, addr);
862 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000863 }
864 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000865#ifdef TARGET_HAS_PRECISE_SMC
866 if (current_tb_modified) {
867 /* we generate a block containing just the instruction
868 modifying the memory. It will ensure that it cannot modify
869 itself */
bellardea1c1802004-06-14 18:56:36 +0000870 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000871 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000872 CF_SINGLE_INSN);
873 cpu_resume_from_signal(env, puc);
874 }
875#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000876}
bellard9fa3e852004-01-04 18:06:42 +0000877#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000878
879/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000880static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000881 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000882{
883 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000884 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000885
bellard9fa3e852004-01-04 18:06:42 +0000886 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000887 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000888 tb->page_next[n] = p->first_tb;
889 last_first_tb = p->first_tb;
890 p->first_tb = (TranslationBlock *)((long)tb | n);
891 invalidate_page_bitmap(p);
892
bellard107db442004-06-22 18:48:46 +0000893#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000894
bellard9fa3e852004-01-04 18:06:42 +0000895#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000896 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000897 target_ulong addr;
898 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000899 int prot;
900
bellardfd6ce8f2003-05-14 19:00:11 +0000901 /* force the host page as non writable (writes will have a
902 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000903 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000904 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000905 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
906 addr += TARGET_PAGE_SIZE) {
907
908 p2 = page_find (addr >> TARGET_PAGE_BITS);
909 if (!p2)
910 continue;
911 prot |= p2->flags;
912 p2->flags &= ~PAGE_WRITE;
913 page_get_flags(addr);
914 }
ths5fafdf22007-09-16 21:08:06 +0000915 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000916 (prot & PAGE_BITS) & ~PAGE_WRITE);
917#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +0000918 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +0000919 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000920#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000921 }
bellard9fa3e852004-01-04 18:06:42 +0000922#else
923 /* if some code is already present, then the pages are already
924 protected. So we handle the case where only the first TB is
925 allocated in a physical page */
926 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000927 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000928 }
929#endif
bellardd720b932004-04-25 17:57:43 +0000930
931#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +0000932}
933
934/* Allocate a new translation block. Flush the translation buffer if
935 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +0000936TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +0000937{
938 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000939
ths5fafdf22007-09-16 21:08:06 +0000940 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
bellardfd6ce8f2003-05-14 19:00:11 +0000941 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
bellardd4e81642003-05-25 16:46:15 +0000942 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +0000943 tb = &tbs[nb_tbs++];
944 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +0000945 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +0000946 return tb;
947}
948
bellard9fa3e852004-01-04 18:06:42 +0000949/* add a new TB and link it to the physical page tables. phys_page2 is
950 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +0000951void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +0000952 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +0000953{
bellard9fa3e852004-01-04 18:06:42 +0000954 unsigned int h;
955 TranslationBlock **ptb;
956
957 /* add in the physical hash table */
958 h = tb_phys_hash_func(phys_pc);
959 ptb = &tb_phys_hash[h];
960 tb->phys_hash_next = *ptb;
961 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000962
963 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +0000964 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
965 if (phys_page2 != -1)
966 tb_alloc_page(tb, 1, phys_page2);
967 else
968 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +0000969
bellardd4e81642003-05-25 16:46:15 +0000970 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
971 tb->jmp_next[0] = NULL;
972 tb->jmp_next[1] = NULL;
973
974 /* init original jump addresses */
975 if (tb->tb_next_offset[0] != 0xffff)
976 tb_reset_jump(tb, 0);
977 if (tb->tb_next_offset[1] != 0xffff)
978 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +0000979
980#ifdef DEBUG_TB_CHECK
981 tb_page_check();
982#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000983}
984
bellarda513fe12003-05-27 23:29:48 +0000985/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
986 tb[1].tc_ptr. Return NULL if not found */
987TranslationBlock *tb_find_pc(unsigned long tc_ptr)
988{
989 int m_min, m_max, m;
990 unsigned long v;
991 TranslationBlock *tb;
992
993 if (nb_tbs <= 0)
994 return NULL;
995 if (tc_ptr < (unsigned long)code_gen_buffer ||
996 tc_ptr >= (unsigned long)code_gen_ptr)
997 return NULL;
998 /* binary search (cf Knuth) */
999 m_min = 0;
1000 m_max = nb_tbs - 1;
1001 while (m_min <= m_max) {
1002 m = (m_min + m_max) >> 1;
1003 tb = &tbs[m];
1004 v = (unsigned long)tb->tc_ptr;
1005 if (v == tc_ptr)
1006 return tb;
1007 else if (tc_ptr < v) {
1008 m_max = m - 1;
1009 } else {
1010 m_min = m + 1;
1011 }
ths5fafdf22007-09-16 21:08:06 +00001012 }
bellarda513fe12003-05-27 23:29:48 +00001013 return &tbs[m_max];
1014}
bellard75012672003-06-21 13:11:07 +00001015
bellardea041c02003-06-25 16:16:50 +00001016static void tb_reset_jump_recursive(TranslationBlock *tb);
1017
1018static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1019{
1020 TranslationBlock *tb1, *tb_next, **ptb;
1021 unsigned int n1;
1022
1023 tb1 = tb->jmp_next[n];
1024 if (tb1 != NULL) {
1025 /* find head of list */
1026 for(;;) {
1027 n1 = (long)tb1 & 3;
1028 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1029 if (n1 == 2)
1030 break;
1031 tb1 = tb1->jmp_next[n1];
1032 }
1033 /* we are now sure now that tb jumps to tb1 */
1034 tb_next = tb1;
1035
1036 /* remove tb from the jmp_first list */
1037 ptb = &tb_next->jmp_first;
1038 for(;;) {
1039 tb1 = *ptb;
1040 n1 = (long)tb1 & 3;
1041 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1042 if (n1 == n && tb1 == tb)
1043 break;
1044 ptb = &tb1->jmp_next[n1];
1045 }
1046 *ptb = tb->jmp_next[n];
1047 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001048
bellardea041c02003-06-25 16:16:50 +00001049 /* suppress the jump to next tb in generated code */
1050 tb_reset_jump(tb, n);
1051
bellard01243112004-01-04 15:48:17 +00001052 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001053 tb_reset_jump_recursive(tb_next);
1054 }
1055}
1056
1057static void tb_reset_jump_recursive(TranslationBlock *tb)
1058{
1059 tb_reset_jump_recursive2(tb, 0);
1060 tb_reset_jump_recursive2(tb, 1);
1061}
1062
bellard1fddef42005-04-17 19:16:13 +00001063#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001064static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1065{
j_mayer9b3c35e2007-04-07 11:21:28 +00001066 target_phys_addr_t addr;
1067 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001068 ram_addr_t ram_addr;
1069 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001070
pbrookc2f07f82006-04-08 17:14:56 +00001071 addr = cpu_get_phys_page_debug(env, pc);
1072 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1073 if (!p) {
1074 pd = IO_MEM_UNASSIGNED;
1075 } else {
1076 pd = p->phys_offset;
1077 }
1078 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001079 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001080}
bellardc27004e2005-01-03 23:35:10 +00001081#endif
bellardd720b932004-04-25 17:57:43 +00001082
pbrook6658ffb2007-03-16 23:58:11 +00001083/* Add a watchpoint. */
1084int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1085{
1086 int i;
1087
1088 for (i = 0; i < env->nb_watchpoints; i++) {
1089 if (addr == env->watchpoint[i].vaddr)
1090 return 0;
1091 }
1092 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1093 return -1;
1094
1095 i = env->nb_watchpoints++;
1096 env->watchpoint[i].vaddr = addr;
1097 tlb_flush_page(env, addr);
1098 /* FIXME: This flush is needed because of the hack to make memory ops
1099 terminate the TB. It can be removed once the proper IO trap and
1100 re-execute bits are in. */
1101 tb_flush(env);
1102 return i;
1103}
1104
1105/* Remove a watchpoint. */
1106int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1107{
1108 int i;
1109
1110 for (i = 0; i < env->nb_watchpoints; i++) {
1111 if (addr == env->watchpoint[i].vaddr) {
1112 env->nb_watchpoints--;
1113 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1114 tlb_flush_page(env, addr);
1115 return 0;
1116 }
1117 }
1118 return -1;
1119}
1120
bellardc33a3462003-07-29 20:50:33 +00001121/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1122 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001123int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001124{
bellard1fddef42005-04-17 19:16:13 +00001125#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001126 int i;
ths3b46e622007-09-17 08:09:54 +00001127
bellard4c3a88a2003-07-26 12:06:08 +00001128 for(i = 0; i < env->nb_breakpoints; i++) {
1129 if (env->breakpoints[i] == pc)
1130 return 0;
1131 }
1132
1133 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1134 return -1;
1135 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001136
bellardd720b932004-04-25 17:57:43 +00001137 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001138 return 0;
1139#else
1140 return -1;
1141#endif
1142}
1143
1144/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001145int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001146{
bellard1fddef42005-04-17 19:16:13 +00001147#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001148 int i;
1149 for(i = 0; i < env->nb_breakpoints; i++) {
1150 if (env->breakpoints[i] == pc)
1151 goto found;
1152 }
1153 return -1;
1154 found:
bellard4c3a88a2003-07-26 12:06:08 +00001155 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001156 if (i < env->nb_breakpoints)
1157 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001158
1159 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001160 return 0;
1161#else
1162 return -1;
1163#endif
1164}
1165
bellardc33a3462003-07-29 20:50:33 +00001166/* enable or disable single step mode. EXCP_DEBUG is returned by the
1167 CPU loop after each instruction */
1168void cpu_single_step(CPUState *env, int enabled)
1169{
bellard1fddef42005-04-17 19:16:13 +00001170#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001171 if (env->singlestep_enabled != enabled) {
1172 env->singlestep_enabled = enabled;
1173 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001174 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001175 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001176 }
1177#endif
1178}
1179
bellard34865132003-10-05 14:28:56 +00001180/* enable or disable low levels log */
1181void cpu_set_log(int log_flags)
1182{
1183 loglevel = log_flags;
1184 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001185 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001186 if (!logfile) {
1187 perror(logfilename);
1188 _exit(1);
1189 }
bellard9fa3e852004-01-04 18:06:42 +00001190#if !defined(CONFIG_SOFTMMU)
1191 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1192 {
1193 static uint8_t logfile_buf[4096];
1194 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1195 }
1196#else
bellard34865132003-10-05 14:28:56 +00001197 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001198#endif
pbrooke735b912007-06-30 13:53:24 +00001199 log_append = 1;
1200 }
1201 if (!loglevel && logfile) {
1202 fclose(logfile);
1203 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001204 }
1205}
1206
1207void cpu_set_log_filename(const char *filename)
1208{
1209 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001210 if (logfile) {
1211 fclose(logfile);
1212 logfile = NULL;
1213 }
1214 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001215}
bellardc33a3462003-07-29 20:50:33 +00001216
bellard01243112004-01-04 15:48:17 +00001217/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001218void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001219{
1220 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001221 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
bellard59817cc2004-02-16 22:01:13 +00001222
bellard68a79312003-06-30 13:12:32 +00001223 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001224 /* if the cpu is currently executing code, we must unlink it and
1225 all the potentially executing TB */
1226 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001227 if (tb && !testandset(&interrupt_lock)) {
1228 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001229 tb_reset_jump_recursive(tb);
aurel3215a51152008-03-28 22:29:15 +00001230 resetlock(&interrupt_lock);
bellardea041c02003-06-25 16:16:50 +00001231 }
1232}
1233
bellardb54ad042004-05-20 13:42:52 +00001234void cpu_reset_interrupt(CPUState *env, int mask)
1235{
1236 env->interrupt_request &= ~mask;
1237}
1238
bellardf193c792004-03-21 17:06:25 +00001239CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001240 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001241 "show generated host assembly code for each compiled TB" },
1242 { CPU_LOG_TB_IN_ASM, "in_asm",
1243 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001244 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001245 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001246 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001247 "show micro ops "
1248#ifdef TARGET_I386
1249 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001250#endif
blueswir1e01a1152008-03-14 17:37:11 +00001251 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001252 { CPU_LOG_INT, "int",
1253 "show interrupts/exceptions in short format" },
1254 { CPU_LOG_EXEC, "exec",
1255 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001256 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001257 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001258#ifdef TARGET_I386
1259 { CPU_LOG_PCALL, "pcall",
1260 "show protected mode far calls/returns/exceptions" },
1261#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001262#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001263 { CPU_LOG_IOPORT, "ioport",
1264 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001265#endif
bellardf193c792004-03-21 17:06:25 +00001266 { 0, NULL, NULL },
1267};
1268
1269static int cmp1(const char *s1, int n, const char *s2)
1270{
1271 if (strlen(s2) != n)
1272 return 0;
1273 return memcmp(s1, s2, n) == 0;
1274}
ths3b46e622007-09-17 08:09:54 +00001275
bellardf193c792004-03-21 17:06:25 +00001276/* takes a comma separated list of log masks. Return 0 if error. */
1277int cpu_str_to_log_mask(const char *str)
1278{
1279 CPULogItem *item;
1280 int mask;
1281 const char *p, *p1;
1282
1283 p = str;
1284 mask = 0;
1285 for(;;) {
1286 p1 = strchr(p, ',');
1287 if (!p1)
1288 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001289 if(cmp1(p,p1-p,"all")) {
1290 for(item = cpu_log_items; item->mask != 0; item++) {
1291 mask |= item->mask;
1292 }
1293 } else {
bellardf193c792004-03-21 17:06:25 +00001294 for(item = cpu_log_items; item->mask != 0; item++) {
1295 if (cmp1(p, p1 - p, item->name))
1296 goto found;
1297 }
1298 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001299 }
bellardf193c792004-03-21 17:06:25 +00001300 found:
1301 mask |= item->mask;
1302 if (*p1 != ',')
1303 break;
1304 p = p1 + 1;
1305 }
1306 return mask;
1307}
bellardea041c02003-06-25 16:16:50 +00001308
bellard75012672003-06-21 13:11:07 +00001309void cpu_abort(CPUState *env, const char *fmt, ...)
1310{
1311 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001312 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001313
1314 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001315 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001316 fprintf(stderr, "qemu: fatal: ");
1317 vfprintf(stderr, fmt, ap);
1318 fprintf(stderr, "\n");
1319#ifdef TARGET_I386
ths0573fbf2007-09-23 15:28:04 +00001320 if(env->intercept & INTERCEPT_SVM_MASK) {
1321 /* most probably the virtual machine should not
1322 be shut down but rather caught by the VMM */
1323 vmexit(SVM_EXIT_SHUTDOWN, 0);
1324 }
bellard7fe48482004-10-09 18:08:01 +00001325 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1326#else
1327 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001328#endif
balrog924edca2007-06-10 14:07:13 +00001329 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001330 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001331 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001332 fprintf(logfile, "\n");
1333#ifdef TARGET_I386
1334 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1335#else
1336 cpu_dump_state(env, logfile, fprintf, 0);
1337#endif
balrog924edca2007-06-10 14:07:13 +00001338 fflush(logfile);
1339 fclose(logfile);
1340 }
pbrook493ae1f2007-11-23 16:53:59 +00001341 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001342 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001343 abort();
1344}
1345
thsc5be9f02007-02-28 20:20:53 +00001346CPUState *cpu_copy(CPUState *env)
1347{
ths01ba9812007-12-09 02:22:57 +00001348 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001349 /* preserve chaining and index */
1350 CPUState *next_cpu = new_env->next_cpu;
1351 int cpu_index = new_env->cpu_index;
1352 memcpy(new_env, env, sizeof(CPUState));
1353 new_env->next_cpu = next_cpu;
1354 new_env->cpu_index = cpu_index;
1355 return new_env;
1356}
1357
bellard01243112004-01-04 15:48:17 +00001358#if !defined(CONFIG_USER_ONLY)
1359
bellardee8b7022004-02-03 23:35:10 +00001360/* NOTE: if flush_global is true, also flush global entries (not
1361 implemented yet) */
1362void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001363{
bellard33417e72003-08-10 21:47:01 +00001364 int i;
bellard01243112004-01-04 15:48:17 +00001365
bellard9fa3e852004-01-04 18:06:42 +00001366#if defined(DEBUG_TLB)
1367 printf("tlb_flush:\n");
1368#endif
bellard01243112004-01-04 15:48:17 +00001369 /* must reset current TB so that interrupts cannot modify the
1370 links while we are modifying them */
1371 env->current_tb = NULL;
1372
bellard33417e72003-08-10 21:47:01 +00001373 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001374 env->tlb_table[0][i].addr_read = -1;
1375 env->tlb_table[0][i].addr_write = -1;
1376 env->tlb_table[0][i].addr_code = -1;
1377 env->tlb_table[1][i].addr_read = -1;
1378 env->tlb_table[1][i].addr_write = -1;
1379 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001380#if (NB_MMU_MODES >= 3)
1381 env->tlb_table[2][i].addr_read = -1;
1382 env->tlb_table[2][i].addr_write = -1;
1383 env->tlb_table[2][i].addr_code = -1;
1384#if (NB_MMU_MODES == 4)
1385 env->tlb_table[3][i].addr_read = -1;
1386 env->tlb_table[3][i].addr_write = -1;
1387 env->tlb_table[3][i].addr_code = -1;
1388#endif
1389#endif
bellard33417e72003-08-10 21:47:01 +00001390 }
bellard9fa3e852004-01-04 18:06:42 +00001391
bellard8a40a182005-11-20 10:35:40 +00001392 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001393
1394#if !defined(CONFIG_SOFTMMU)
1395 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1396#endif
bellard0a962c02005-02-10 22:00:27 +00001397#ifdef USE_KQEMU
1398 if (env->kqemu_enabled) {
1399 kqemu_flush(env, flush_global);
1400 }
1401#endif
bellarde3db7222005-01-26 22:00:47 +00001402 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001403}
1404
bellard274da6b2004-05-20 21:56:27 +00001405static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001406{
ths5fafdf22007-09-16 21:08:06 +00001407 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001408 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001409 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001410 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001411 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001412 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1413 tlb_entry->addr_read = -1;
1414 tlb_entry->addr_write = -1;
1415 tlb_entry->addr_code = -1;
1416 }
bellard61382a52003-10-27 21:22:23 +00001417}
1418
bellard2e126692004-04-25 21:28:44 +00001419void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001420{
bellard8a40a182005-11-20 10:35:40 +00001421 int i;
bellard9fa3e852004-01-04 18:06:42 +00001422 TranslationBlock *tb;
bellard01243112004-01-04 15:48:17 +00001423
bellard9fa3e852004-01-04 18:06:42 +00001424#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001425 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001426#endif
bellard01243112004-01-04 15:48:17 +00001427 /* must reset current TB so that interrupts cannot modify the
1428 links while we are modifying them */
1429 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001430
bellard61382a52003-10-27 21:22:23 +00001431 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001432 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001433 tlb_flush_entry(&env->tlb_table[0][i], addr);
1434 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001435#if (NB_MMU_MODES >= 3)
1436 tlb_flush_entry(&env->tlb_table[2][i], addr);
1437#if (NB_MMU_MODES == 4)
1438 tlb_flush_entry(&env->tlb_table[3][i], addr);
1439#endif
1440#endif
bellard01243112004-01-04 15:48:17 +00001441
pbrookb362e5e2006-11-12 20:40:55 +00001442 /* Discard jump cache entries for any tb which might potentially
1443 overlap the flushed page. */
1444 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1445 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
1446
1447 i = tb_jmp_cache_hash_page(addr);
1448 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
bellard9fa3e852004-01-04 18:06:42 +00001449
bellard01243112004-01-04 15:48:17 +00001450#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001451 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001452 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001453#endif
bellard0a962c02005-02-10 22:00:27 +00001454#ifdef USE_KQEMU
1455 if (env->kqemu_enabled) {
1456 kqemu_flush_page(env, addr);
1457 }
1458#endif
bellard9fa3e852004-01-04 18:06:42 +00001459}
1460
bellard9fa3e852004-01-04 18:06:42 +00001461/* update the TLBs so that writes to code in the virtual page 'addr'
1462 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001463static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001464{
ths5fafdf22007-09-16 21:08:06 +00001465 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001466 ram_addr + TARGET_PAGE_SIZE,
1467 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001468}
1469
bellard9fa3e852004-01-04 18:06:42 +00001470/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001471 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001472static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001473 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001474{
bellard3a7d9292005-08-21 09:26:42 +00001475 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001476}
1477
ths5fafdf22007-09-16 21:08:06 +00001478static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001479 unsigned long start, unsigned long length)
1480{
1481 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001482 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1483 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001484 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001485 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001486 }
1487 }
1488}
1489
bellard3a7d9292005-08-21 09:26:42 +00001490void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001491 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001492{
1493 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001494 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001495 int i, mask, len;
1496 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001497
1498 start &= TARGET_PAGE_MASK;
1499 end = TARGET_PAGE_ALIGN(end);
1500
1501 length = end - start;
1502 if (length == 0)
1503 return;
bellard0a962c02005-02-10 22:00:27 +00001504 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001505#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001506 /* XXX: should not depend on cpu context */
1507 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001508 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001509 ram_addr_t addr;
1510 addr = start;
1511 for(i = 0; i < len; i++) {
1512 kqemu_set_notdirty(env, addr);
1513 addr += TARGET_PAGE_SIZE;
1514 }
bellard3a7d9292005-08-21 09:26:42 +00001515 }
1516#endif
bellardf23db162005-08-21 19:12:28 +00001517 mask = ~dirty_flags;
1518 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1519 for(i = 0; i < len; i++)
1520 p[i] &= mask;
1521
bellard1ccde1c2004-02-06 19:46:14 +00001522 /* we modify the TLB cache so that the dirty bit will be set again
1523 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001524 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001525 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1526 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001527 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001528 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001529 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001530#if (NB_MMU_MODES >= 3)
1531 for(i = 0; i < CPU_TLB_SIZE; i++)
1532 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1533#if (NB_MMU_MODES == 4)
1534 for(i = 0; i < CPU_TLB_SIZE; i++)
1535 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1536#endif
1537#endif
bellard6a00d602005-11-21 23:25:50 +00001538 }
bellard59817cc2004-02-16 22:01:13 +00001539
1540#if !defined(CONFIG_SOFTMMU)
1541 /* XXX: this is expensive */
1542 {
1543 VirtPageDesc *p;
1544 int j;
1545 target_ulong addr;
1546
1547 for(i = 0; i < L1_SIZE; i++) {
1548 p = l1_virt_map[i];
1549 if (p) {
1550 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1551 for(j = 0; j < L2_SIZE; j++) {
1552 if (p->valid_tag == virt_valid_tag &&
1553 p->phys_addr >= start && p->phys_addr < end &&
1554 (p->prot & PROT_WRITE)) {
1555 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001556 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001557 p->prot & ~PROT_WRITE);
1558 }
1559 }
1560 addr += TARGET_PAGE_SIZE;
1561 p++;
1562 }
1563 }
1564 }
1565 }
1566#endif
bellard1ccde1c2004-02-06 19:46:14 +00001567}
1568
bellard3a7d9292005-08-21 09:26:42 +00001569static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1570{
1571 ram_addr_t ram_addr;
1572
bellard84b7b8e2005-11-28 21:19:04 +00001573 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001574 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001575 tlb_entry->addend - (unsigned long)phys_ram_base;
1576 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001577 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001578 }
1579 }
1580}
1581
1582/* update the TLB according to the current state of the dirty bits */
1583void cpu_tlb_update_dirty(CPUState *env)
1584{
1585 int i;
1586 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001587 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001588 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001589 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001590#if (NB_MMU_MODES >= 3)
1591 for(i = 0; i < CPU_TLB_SIZE; i++)
1592 tlb_update_dirty(&env->tlb_table[2][i]);
1593#if (NB_MMU_MODES == 4)
1594 for(i = 0; i < CPU_TLB_SIZE; i++)
1595 tlb_update_dirty(&env->tlb_table[3][i]);
1596#endif
1597#endif
bellard3a7d9292005-08-21 09:26:42 +00001598}
1599
ths5fafdf22007-09-16 21:08:06 +00001600static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001601 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001602{
1603 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001604 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1605 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001606 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001607 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001608 }
1609 }
1610}
1611
1612/* update the TLB corresponding to virtual page vaddr and phys addr
1613 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001614static inline void tlb_set_dirty(CPUState *env,
1615 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001616{
bellard1ccde1c2004-02-06 19:46:14 +00001617 int i;
1618
bellard1ccde1c2004-02-06 19:46:14 +00001619 addr &= TARGET_PAGE_MASK;
1620 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001621 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1622 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001623#if (NB_MMU_MODES >= 3)
1624 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1625#if (NB_MMU_MODES == 4)
1626 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1627#endif
1628#endif
bellard9fa3e852004-01-04 18:06:42 +00001629}
1630
bellard59817cc2004-02-16 22:01:13 +00001631/* add a new TLB entry. At most one entry for a given virtual address
1632 is permitted. Return 0 if OK or 2 if the page could not be mapped
1633 (can only happen in non SOFTMMU mode for I/O pages or pages
1634 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001635int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1636 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001637 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001638{
bellard92e873b2004-05-21 14:52:29 +00001639 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001640 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001641 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001642 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001643 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001644 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001645 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001646 int i;
bellard9fa3e852004-01-04 18:06:42 +00001647
bellard92e873b2004-05-21 14:52:29 +00001648 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001649 if (!p) {
1650 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001651 } else {
1652 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001653 }
1654#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001655 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1656 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001657#endif
1658
1659 ret = 0;
1660#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001661 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001662#endif
1663 {
bellard2a4188a2006-06-25 21:54:59 +00001664 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001665 /* IO memory case */
1666 address = vaddr | pd;
1667 addend = paddr;
1668 } else {
1669 /* standard memory */
1670 address = vaddr;
1671 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1672 }
pbrook6658ffb2007-03-16 23:58:11 +00001673
1674 /* Make accesses to pages with watchpoints go via the
1675 watchpoint trap routines. */
1676 for (i = 0; i < env->nb_watchpoints; i++) {
1677 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1678 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001679 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001680 address = vaddr | io_mem_watch;
1681 } else {
balrogd79acba2007-06-26 20:01:13 +00001682 env->watchpoint[i].addend = pd - paddr +
1683 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001684 /* TODO: Figure out how to make read watchpoints coexist
1685 with code. */
1686 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1687 }
1688 }
1689 }
balrogd79acba2007-06-26 20:01:13 +00001690
bellard90f18422005-07-24 10:17:31 +00001691 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001692 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001693 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001694 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001695 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001696 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001697 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001698 te->addr_read = -1;
1699 }
1700 if (prot & PAGE_EXEC) {
1701 te->addr_code = address;
1702 } else {
1703 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001704 }
bellard67b915a2004-03-31 23:37:16 +00001705 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001706 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001707 (pd & IO_MEM_ROMD)) {
1708 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001709 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001710 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001711 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001712 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001713 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001714 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001715 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001716 }
1717 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001718 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001719 }
1720 }
1721#if !defined(CONFIG_SOFTMMU)
1722 else {
1723 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1724 /* IO access: no mapping is done as it will be handled by the
1725 soft MMU */
1726 if (!(env->hflags & HF_SOFTMMU_MASK))
1727 ret = 2;
1728 } else {
1729 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001730
bellard59817cc2004-02-16 22:01:13 +00001731 if (vaddr >= MMAP_AREA_END) {
1732 ret = 2;
1733 } else {
1734 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001735 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001736#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001737 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001738#endif
ths5fafdf22007-09-16 21:08:06 +00001739 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001740 !cpu_physical_memory_is_dirty(pd))) {
1741 /* ROM: we do as if code was inside */
1742 /* if code is present, we only map as read only and save the
1743 original mapping */
1744 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001745
bellard90f18422005-07-24 10:17:31 +00001746 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001747 vp->phys_addr = pd;
1748 vp->prot = prot;
1749 vp->valid_tag = virt_valid_tag;
1750 prot &= ~PAGE_WRITE;
1751 }
bellard9fa3e852004-01-04 18:06:42 +00001752 }
ths5fafdf22007-09-16 21:08:06 +00001753 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001754 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1755 if (map_addr == MAP_FAILED) {
1756 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1757 paddr, vaddr);
1758 }
bellard9fa3e852004-01-04 18:06:42 +00001759 }
1760 }
1761 }
1762#endif
1763 return ret;
1764}
1765
1766/* called from signal handler: invalidate the code and unprotect the
1767 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001768int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001769{
1770#if !defined(CONFIG_SOFTMMU)
1771 VirtPageDesc *vp;
1772
1773#if defined(DEBUG_TLB)
1774 printf("page_unprotect: addr=0x%08x\n", addr);
1775#endif
1776 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001777
1778 /* if it is not mapped, no need to worry here */
1779 if (addr >= MMAP_AREA_END)
1780 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001781 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1782 if (!vp)
1783 return 0;
1784 /* NOTE: in this case, validate_tag is _not_ tested as it
1785 validates only the code TLB */
1786 if (vp->valid_tag != virt_valid_tag)
1787 return 0;
1788 if (!(vp->prot & PAGE_WRITE))
1789 return 0;
1790#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001791 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001792 addr, vp->phys_addr, vp->prot);
1793#endif
bellard59817cc2004-02-16 22:01:13 +00001794 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1795 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1796 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001797 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001798 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001799 /* flush the code inside */
1800 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001801 return 1;
1802#else
1803 return 0;
1804#endif
bellard33417e72003-08-10 21:47:01 +00001805}
1806
bellard01243112004-01-04 15:48:17 +00001807#else
1808
bellardee8b7022004-02-03 23:35:10 +00001809void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001810{
1811}
1812
bellard2e126692004-04-25 21:28:44 +00001813void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001814{
1815}
1816
ths5fafdf22007-09-16 21:08:06 +00001817int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1818 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001819 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001820{
bellard9fa3e852004-01-04 18:06:42 +00001821 return 0;
1822}
bellard33417e72003-08-10 21:47:01 +00001823
bellard9fa3e852004-01-04 18:06:42 +00001824/* dump memory mappings */
1825void page_dump(FILE *f)
1826{
1827 unsigned long start, end;
1828 int i, j, prot, prot1;
1829 PageDesc *p;
1830
1831 fprintf(f, "%-8s %-8s %-8s %s\n",
1832 "start", "end", "size", "prot");
1833 start = -1;
1834 end = -1;
1835 prot = 0;
1836 for(i = 0; i <= L1_SIZE; i++) {
1837 if (i < L1_SIZE)
1838 p = l1_map[i];
1839 else
1840 p = NULL;
1841 for(j = 0;j < L2_SIZE; j++) {
1842 if (!p)
1843 prot1 = 0;
1844 else
1845 prot1 = p[j].flags;
1846 if (prot1 != prot) {
1847 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1848 if (start != -1) {
1849 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001850 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001851 prot & PAGE_READ ? 'r' : '-',
1852 prot & PAGE_WRITE ? 'w' : '-',
1853 prot & PAGE_EXEC ? 'x' : '-');
1854 }
1855 if (prot1 != 0)
1856 start = end;
1857 else
1858 start = -1;
1859 prot = prot1;
1860 }
1861 if (!p)
1862 break;
1863 }
bellard33417e72003-08-10 21:47:01 +00001864 }
bellard33417e72003-08-10 21:47:01 +00001865}
1866
pbrook53a59602006-03-25 19:31:22 +00001867int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001868{
bellard9fa3e852004-01-04 18:06:42 +00001869 PageDesc *p;
1870
1871 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001872 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001873 return 0;
1874 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001875}
1876
bellard9fa3e852004-01-04 18:06:42 +00001877/* modify the flags of a page and invalidate the code if
1878 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1879 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001880void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001881{
1882 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001883 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001884
1885 start = start & TARGET_PAGE_MASK;
1886 end = TARGET_PAGE_ALIGN(end);
1887 if (flags & PAGE_WRITE)
1888 flags |= PAGE_WRITE_ORG;
1889 spin_lock(&tb_lock);
1890 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1891 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1892 /* if the write protection is set, then we invalidate the code
1893 inside */
ths5fafdf22007-09-16 21:08:06 +00001894 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001895 (flags & PAGE_WRITE) &&
1896 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001897 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001898 }
1899 p->flags = flags;
1900 }
1901 spin_unlock(&tb_lock);
1902}
1903
ths3d97b402007-11-02 19:02:07 +00001904int page_check_range(target_ulong start, target_ulong len, int flags)
1905{
1906 PageDesc *p;
1907 target_ulong end;
1908 target_ulong addr;
1909
1910 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
1911 start = start & TARGET_PAGE_MASK;
1912
1913 if( end < start )
1914 /* we've wrapped around */
1915 return -1;
1916 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1917 p = page_find(addr >> TARGET_PAGE_BITS);
1918 if( !p )
1919 return -1;
1920 if( !(p->flags & PAGE_VALID) )
1921 return -1;
1922
bellarddae32702007-11-14 10:51:00 +00001923 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00001924 return -1;
bellarddae32702007-11-14 10:51:00 +00001925 if (flags & PAGE_WRITE) {
1926 if (!(p->flags & PAGE_WRITE_ORG))
1927 return -1;
1928 /* unprotect the page if it was put read-only because it
1929 contains translated code */
1930 if (!(p->flags & PAGE_WRITE)) {
1931 if (!page_unprotect(addr, 0, NULL))
1932 return -1;
1933 }
1934 return 0;
1935 }
ths3d97b402007-11-02 19:02:07 +00001936 }
1937 return 0;
1938}
1939
bellard9fa3e852004-01-04 18:06:42 +00001940/* called from signal handler: invalidate the code and unprotect the
1941 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001942int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001943{
1944 unsigned int page_index, prot, pindex;
1945 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00001946 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001947
bellard83fb7ad2004-07-05 21:25:26 +00001948 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00001949 page_index = host_start >> TARGET_PAGE_BITS;
1950 p1 = page_find(page_index);
1951 if (!p1)
1952 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00001953 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00001954 p = p1;
1955 prot = 0;
1956 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
1957 prot |= p->flags;
1958 p++;
1959 }
1960 /* if the page was really writable, then we change its
1961 protection back to writable */
1962 if (prot & PAGE_WRITE_ORG) {
1963 pindex = (address - host_start) >> TARGET_PAGE_BITS;
1964 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00001965 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00001966 (prot & PAGE_BITS) | PAGE_WRITE);
1967 p1[pindex].flags |= PAGE_WRITE;
1968 /* and since the content will be modified, we must invalidate
1969 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00001970 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001971#ifdef DEBUG_TB_CHECK
1972 tb_invalidate_check(address);
1973#endif
1974 return 1;
1975 }
1976 }
1977 return 0;
1978}
1979
bellard6a00d602005-11-21 23:25:50 +00001980static inline void tlb_set_dirty(CPUState *env,
1981 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001982{
1983}
bellard9fa3e852004-01-04 18:06:42 +00001984#endif /* defined(CONFIG_USER_ONLY) */
1985
blueswir1db7b5422007-05-26 17:36:03 +00001986static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1987 int memory);
1988static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
1989 int orig_memory);
1990#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
1991 need_subpage) \
1992 do { \
1993 if (addr > start_addr) \
1994 start_addr2 = 0; \
1995 else { \
1996 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
1997 if (start_addr2 > 0) \
1998 need_subpage = 1; \
1999 } \
2000 \
blueswir149e9fba2007-05-30 17:25:06 +00002001 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002002 end_addr2 = TARGET_PAGE_SIZE - 1; \
2003 else { \
2004 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2005 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2006 need_subpage = 1; \
2007 } \
2008 } while (0)
2009
bellard33417e72003-08-10 21:47:01 +00002010/* register physical memory. 'size' must be a multiple of the target
2011 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2012 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002013void cpu_register_physical_memory(target_phys_addr_t start_addr,
bellard2e126692004-04-25 21:28:44 +00002014 unsigned long size,
2015 unsigned long phys_offset)
bellard33417e72003-08-10 21:47:01 +00002016{
bellard108c49b2005-07-24 12:55:09 +00002017 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002018 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002019 CPUState *env;
blueswir1db7b5422007-05-26 17:36:03 +00002020 unsigned long orig_size = size;
2021 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002022
bellard5fd386f2004-05-23 21:11:22 +00002023 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002024 end_addr = start_addr + (target_phys_addr_t)size;
2025 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002026 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2027 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2028 unsigned long orig_memory = p->phys_offset;
2029 target_phys_addr_t start_addr2, end_addr2;
2030 int need_subpage = 0;
2031
2032 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2033 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002034 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002035 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2036 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2037 &p->phys_offset, orig_memory);
2038 } else {
2039 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2040 >> IO_MEM_SHIFT];
2041 }
2042 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2043 } else {
2044 p->phys_offset = phys_offset;
2045 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2046 (phys_offset & IO_MEM_ROMD))
2047 phys_offset += TARGET_PAGE_SIZE;
2048 }
2049 } else {
2050 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2051 p->phys_offset = phys_offset;
2052 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2053 (phys_offset & IO_MEM_ROMD))
2054 phys_offset += TARGET_PAGE_SIZE;
2055 else {
2056 target_phys_addr_t start_addr2, end_addr2;
2057 int need_subpage = 0;
2058
2059 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2060 end_addr2, need_subpage);
2061
blueswir14254fab2008-01-01 16:57:19 +00002062 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002063 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2064 &p->phys_offset, IO_MEM_UNASSIGNED);
2065 subpage_register(subpage, start_addr2, end_addr2,
2066 phys_offset);
2067 }
2068 }
2069 }
bellard33417e72003-08-10 21:47:01 +00002070 }
ths3b46e622007-09-17 08:09:54 +00002071
bellard9d420372006-06-25 22:25:22 +00002072 /* since each CPU stores ram addresses in its TLB cache, we must
2073 reset the modified entries */
2074 /* XXX: slow ! */
2075 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2076 tlb_flush(env, 1);
2077 }
bellard33417e72003-08-10 21:47:01 +00002078}
2079
bellardba863452006-09-24 18:41:10 +00002080/* XXX: temporary until new memory mapping API */
2081uint32_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2082{
2083 PhysPageDesc *p;
2084
2085 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2086 if (!p)
2087 return IO_MEM_UNASSIGNED;
2088 return p->phys_offset;
2089}
2090
bellarde9a1ab12007-02-08 23:08:38 +00002091/* XXX: better than nothing */
2092ram_addr_t qemu_ram_alloc(unsigned int size)
2093{
2094 ram_addr_t addr;
2095 if ((phys_ram_alloc_offset + size) >= phys_ram_size) {
ths5fafdf22007-09-16 21:08:06 +00002096 fprintf(stderr, "Not enough memory (requested_size = %u, max memory = %d)\n",
bellarde9a1ab12007-02-08 23:08:38 +00002097 size, phys_ram_size);
2098 abort();
2099 }
2100 addr = phys_ram_alloc_offset;
2101 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2102 return addr;
2103}
2104
2105void qemu_ram_free(ram_addr_t addr)
2106{
2107}
2108
bellarda4193c82004-06-03 14:01:43 +00002109static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002110{
pbrook67d3b952006-12-18 05:03:52 +00002111#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002112 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002113#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002114#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002115 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002116#elif TARGET_CRIS
2117 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002118#endif
bellard33417e72003-08-10 21:47:01 +00002119 return 0;
2120}
2121
bellarda4193c82004-06-03 14:01:43 +00002122static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002123{
pbrook67d3b952006-12-18 05:03:52 +00002124#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002125 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002126#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002127#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002128 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002129#elif TARGET_CRIS
2130 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002131#endif
bellard33417e72003-08-10 21:47:01 +00002132}
2133
2134static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2135 unassigned_mem_readb,
2136 unassigned_mem_readb,
2137 unassigned_mem_readb,
2138};
2139
2140static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2141 unassigned_mem_writeb,
2142 unassigned_mem_writeb,
2143 unassigned_mem_writeb,
2144};
2145
bellarda4193c82004-06-03 14:01:43 +00002146static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002147{
bellard3a7d9292005-08-21 09:26:42 +00002148 unsigned long ram_addr;
2149 int dirty_flags;
2150 ram_addr = addr - (unsigned long)phys_ram_base;
2151 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2152 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2153#if !defined(CONFIG_USER_ONLY)
2154 tb_invalidate_phys_page_fast(ram_addr, 1);
2155 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2156#endif
2157 }
bellardc27004e2005-01-03 23:35:10 +00002158 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002159#ifdef USE_KQEMU
2160 if (cpu_single_env->kqemu_enabled &&
2161 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2162 kqemu_modify_page(cpu_single_env, ram_addr);
2163#endif
bellardf23db162005-08-21 19:12:28 +00002164 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2165 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2166 /* we remove the notdirty callback only if the code has been
2167 flushed */
2168 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002169 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002170}
2171
bellarda4193c82004-06-03 14:01:43 +00002172static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002173{
bellard3a7d9292005-08-21 09:26:42 +00002174 unsigned long ram_addr;
2175 int dirty_flags;
2176 ram_addr = addr - (unsigned long)phys_ram_base;
2177 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2178 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2179#if !defined(CONFIG_USER_ONLY)
2180 tb_invalidate_phys_page_fast(ram_addr, 2);
2181 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2182#endif
2183 }
bellardc27004e2005-01-03 23:35:10 +00002184 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002185#ifdef USE_KQEMU
2186 if (cpu_single_env->kqemu_enabled &&
2187 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2188 kqemu_modify_page(cpu_single_env, ram_addr);
2189#endif
bellardf23db162005-08-21 19:12:28 +00002190 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2191 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2192 /* we remove the notdirty callback only if the code has been
2193 flushed */
2194 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002195 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002196}
2197
bellarda4193c82004-06-03 14:01:43 +00002198static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002199{
bellard3a7d9292005-08-21 09:26:42 +00002200 unsigned long ram_addr;
2201 int dirty_flags;
2202 ram_addr = addr - (unsigned long)phys_ram_base;
2203 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2204 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2205#if !defined(CONFIG_USER_ONLY)
2206 tb_invalidate_phys_page_fast(ram_addr, 4);
2207 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2208#endif
2209 }
bellardc27004e2005-01-03 23:35:10 +00002210 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002211#ifdef USE_KQEMU
2212 if (cpu_single_env->kqemu_enabled &&
2213 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2214 kqemu_modify_page(cpu_single_env, ram_addr);
2215#endif
bellardf23db162005-08-21 19:12:28 +00002216 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2217 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2218 /* we remove the notdirty callback only if the code has been
2219 flushed */
2220 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002221 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002222}
2223
bellard3a7d9292005-08-21 09:26:42 +00002224static CPUReadMemoryFunc *error_mem_read[3] = {
2225 NULL, /* never used */
2226 NULL, /* never used */
2227 NULL, /* never used */
2228};
2229
bellard1ccde1c2004-02-06 19:46:14 +00002230static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2231 notdirty_mem_writeb,
2232 notdirty_mem_writew,
2233 notdirty_mem_writel,
2234};
2235
pbrook6658ffb2007-03-16 23:58:11 +00002236#if defined(CONFIG_SOFTMMU)
2237/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2238 so these check for a hit then pass through to the normal out-of-line
2239 phys routines. */
2240static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2241{
2242 return ldub_phys(addr);
2243}
2244
2245static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2246{
2247 return lduw_phys(addr);
2248}
2249
2250static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2251{
2252 return ldl_phys(addr);
2253}
2254
2255/* Generate a debug exception if a watchpoint has been hit.
2256 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002257 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002258static target_ulong check_watchpoint(target_phys_addr_t addr)
2259{
2260 CPUState *env = cpu_single_env;
2261 target_ulong watch;
2262 target_ulong retaddr;
2263 int i;
2264
2265 retaddr = addr;
2266 for (i = 0; i < env->nb_watchpoints; i++) {
2267 watch = env->watchpoint[i].vaddr;
2268 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002269 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002270 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2271 cpu_single_env->watchpoint_hit = i + 1;
2272 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2273 break;
2274 }
2275 }
2276 }
2277 return retaddr;
2278}
2279
2280static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2281 uint32_t val)
2282{
2283 addr = check_watchpoint(addr);
2284 stb_phys(addr, val);
2285}
2286
2287static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2288 uint32_t val)
2289{
2290 addr = check_watchpoint(addr);
2291 stw_phys(addr, val);
2292}
2293
2294static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2295 uint32_t val)
2296{
2297 addr = check_watchpoint(addr);
2298 stl_phys(addr, val);
2299}
2300
2301static CPUReadMemoryFunc *watch_mem_read[3] = {
2302 watch_mem_readb,
2303 watch_mem_readw,
2304 watch_mem_readl,
2305};
2306
2307static CPUWriteMemoryFunc *watch_mem_write[3] = {
2308 watch_mem_writeb,
2309 watch_mem_writew,
2310 watch_mem_writel,
2311};
2312#endif
2313
blueswir1db7b5422007-05-26 17:36:03 +00002314static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2315 unsigned int len)
2316{
blueswir1db7b5422007-05-26 17:36:03 +00002317 uint32_t ret;
2318 unsigned int idx;
2319
2320 idx = SUBPAGE_IDX(addr - mmio->base);
2321#if defined(DEBUG_SUBPAGE)
2322 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2323 mmio, len, addr, idx);
2324#endif
blueswir13ee89922008-01-02 19:45:26 +00002325 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002326
2327 return ret;
2328}
2329
2330static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2331 uint32_t value, unsigned int len)
2332{
blueswir1db7b5422007-05-26 17:36:03 +00002333 unsigned int idx;
2334
2335 idx = SUBPAGE_IDX(addr - mmio->base);
2336#if defined(DEBUG_SUBPAGE)
2337 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2338 mmio, len, addr, idx, value);
2339#endif
blueswir13ee89922008-01-02 19:45:26 +00002340 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002341}
2342
2343static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2344{
2345#if defined(DEBUG_SUBPAGE)
2346 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2347#endif
2348
2349 return subpage_readlen(opaque, addr, 0);
2350}
2351
2352static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2353 uint32_t value)
2354{
2355#if defined(DEBUG_SUBPAGE)
2356 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2357#endif
2358 subpage_writelen(opaque, addr, value, 0);
2359}
2360
2361static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2362{
2363#if defined(DEBUG_SUBPAGE)
2364 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2365#endif
2366
2367 return subpage_readlen(opaque, addr, 1);
2368}
2369
2370static void subpage_writew (void *opaque, target_phys_addr_t addr,
2371 uint32_t value)
2372{
2373#if defined(DEBUG_SUBPAGE)
2374 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2375#endif
2376 subpage_writelen(opaque, addr, value, 1);
2377}
2378
2379static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2380{
2381#if defined(DEBUG_SUBPAGE)
2382 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2383#endif
2384
2385 return subpage_readlen(opaque, addr, 2);
2386}
2387
2388static void subpage_writel (void *opaque,
2389 target_phys_addr_t addr, uint32_t value)
2390{
2391#if defined(DEBUG_SUBPAGE)
2392 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2393#endif
2394 subpage_writelen(opaque, addr, value, 2);
2395}
2396
2397static CPUReadMemoryFunc *subpage_read[] = {
2398 &subpage_readb,
2399 &subpage_readw,
2400 &subpage_readl,
2401};
2402
2403static CPUWriteMemoryFunc *subpage_write[] = {
2404 &subpage_writeb,
2405 &subpage_writew,
2406 &subpage_writel,
2407};
2408
2409static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2410 int memory)
2411{
2412 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002413 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002414
2415 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2416 return -1;
2417 idx = SUBPAGE_IDX(start);
2418 eidx = SUBPAGE_IDX(end);
2419#if defined(DEBUG_SUBPAGE)
2420 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2421 mmio, start, end, idx, eidx, memory);
2422#endif
2423 memory >>= IO_MEM_SHIFT;
2424 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002425 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002426 if (io_mem_read[memory][i]) {
2427 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2428 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2429 }
2430 if (io_mem_write[memory][i]) {
2431 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2432 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2433 }
blueswir14254fab2008-01-01 16:57:19 +00002434 }
blueswir1db7b5422007-05-26 17:36:03 +00002435 }
2436
2437 return 0;
2438}
2439
2440static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
2441 int orig_memory)
2442{
2443 subpage_t *mmio;
2444 int subpage_memory;
2445
2446 mmio = qemu_mallocz(sizeof(subpage_t));
2447 if (mmio != NULL) {
2448 mmio->base = base;
2449 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2450#if defined(DEBUG_SUBPAGE)
2451 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2452 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2453#endif
2454 *phys = subpage_memory | IO_MEM_SUBPAGE;
2455 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2456 }
2457
2458 return mmio;
2459}
2460
bellard33417e72003-08-10 21:47:01 +00002461static void io_mem_init(void)
2462{
bellard3a7d9292005-08-21 09:26:42 +00002463 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002464 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002465 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002466 io_mem_nb = 5;
2467
pbrook6658ffb2007-03-16 23:58:11 +00002468#if defined(CONFIG_SOFTMMU)
2469 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2470 watch_mem_write, NULL);
2471#endif
bellard1ccde1c2004-02-06 19:46:14 +00002472 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002473 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002474 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002475}
2476
2477/* mem_read and mem_write are arrays of functions containing the
2478 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002479 2). Functions can be omitted with a NULL function pointer. The
2480 registered functions may be modified dynamically later.
2481 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002482 modified. If it is zero, a new io zone is allocated. The return
2483 value can be used with cpu_register_physical_memory(). (-1) is
2484 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002485int cpu_register_io_memory(int io_index,
2486 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002487 CPUWriteMemoryFunc **mem_write,
2488 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002489{
blueswir14254fab2008-01-01 16:57:19 +00002490 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002491
2492 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002493 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002494 return -1;
2495 io_index = io_mem_nb++;
2496 } else {
2497 if (io_index >= IO_MEM_NB_ENTRIES)
2498 return -1;
2499 }
bellardb5ff1b32005-11-26 10:38:39 +00002500
bellard33417e72003-08-10 21:47:01 +00002501 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002502 if (!mem_read[i] || !mem_write[i])
2503 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002504 io_mem_read[io_index][i] = mem_read[i];
2505 io_mem_write[io_index][i] = mem_write[i];
2506 }
bellarda4193c82004-06-03 14:01:43 +00002507 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002508 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002509}
bellard61382a52003-10-27 21:22:23 +00002510
bellard8926b512004-10-10 15:14:20 +00002511CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2512{
2513 return io_mem_write[io_index >> IO_MEM_SHIFT];
2514}
2515
2516CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2517{
2518 return io_mem_read[io_index >> IO_MEM_SHIFT];
2519}
2520
bellard13eb76e2004-01-24 15:23:36 +00002521/* physical memory access (slow version, mainly for debug) */
2522#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002523void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002524 int len, int is_write)
2525{
2526 int l, flags;
2527 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002528 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002529
2530 while (len > 0) {
2531 page = addr & TARGET_PAGE_MASK;
2532 l = (page + TARGET_PAGE_SIZE) - addr;
2533 if (l > len)
2534 l = len;
2535 flags = page_get_flags(page);
2536 if (!(flags & PAGE_VALID))
2537 return;
2538 if (is_write) {
2539 if (!(flags & PAGE_WRITE))
2540 return;
bellard579a97f2007-11-11 14:26:47 +00002541 /* XXX: this code should not depend on lock_user */
2542 if (!(p = lock_user(VERIFY_WRITE, addr, len, 0)))
2543 /* FIXME - should this return an error rather than just fail? */
2544 return;
pbrook53a59602006-03-25 19:31:22 +00002545 memcpy(p, buf, len);
2546 unlock_user(p, addr, len);
bellard13eb76e2004-01-24 15:23:36 +00002547 } else {
2548 if (!(flags & PAGE_READ))
2549 return;
bellard579a97f2007-11-11 14:26:47 +00002550 /* XXX: this code should not depend on lock_user */
2551 if (!(p = lock_user(VERIFY_READ, addr, len, 1)))
2552 /* FIXME - should this return an error rather than just fail? */
2553 return;
pbrook53a59602006-03-25 19:31:22 +00002554 memcpy(buf, p, len);
2555 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002556 }
2557 len -= l;
2558 buf += l;
2559 addr += l;
2560 }
2561}
bellard8df1cd02005-01-28 22:37:22 +00002562
bellard13eb76e2004-01-24 15:23:36 +00002563#else
ths5fafdf22007-09-16 21:08:06 +00002564void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002565 int len, int is_write)
2566{
2567 int l, io_index;
2568 uint8_t *ptr;
2569 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002570 target_phys_addr_t page;
2571 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002572 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002573
bellard13eb76e2004-01-24 15:23:36 +00002574 while (len > 0) {
2575 page = addr & TARGET_PAGE_MASK;
2576 l = (page + TARGET_PAGE_SIZE) - addr;
2577 if (l > len)
2578 l = len;
bellard92e873b2004-05-21 14:52:29 +00002579 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002580 if (!p) {
2581 pd = IO_MEM_UNASSIGNED;
2582 } else {
2583 pd = p->phys_offset;
2584 }
ths3b46e622007-09-17 08:09:54 +00002585
bellard13eb76e2004-01-24 15:23:36 +00002586 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002587 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002588 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002589 /* XXX: could force cpu_single_env to NULL to avoid
2590 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002591 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002592 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002593 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002594 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002595 l = 4;
2596 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002597 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002598 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002599 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002600 l = 2;
2601 } else {
bellard1c213d12005-09-03 10:49:04 +00002602 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002603 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002604 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002605 l = 1;
2606 }
2607 } else {
bellardb448f2f2004-02-25 23:24:04 +00002608 unsigned long addr1;
2609 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002610 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002611 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002612 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002613 if (!cpu_physical_memory_is_dirty(addr1)) {
2614 /* invalidate code */
2615 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2616 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002617 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002618 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002619 }
bellard13eb76e2004-01-24 15:23:36 +00002620 }
2621 } else {
ths5fafdf22007-09-16 21:08:06 +00002622 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002623 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002624 /* I/O case */
2625 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2626 if (l >= 4 && ((addr & 3) == 0)) {
2627 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002628 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002629 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002630 l = 4;
2631 } else if (l >= 2 && ((addr & 1) == 0)) {
2632 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002633 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002634 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002635 l = 2;
2636 } else {
bellard1c213d12005-09-03 10:49:04 +00002637 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002638 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002639 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002640 l = 1;
2641 }
2642 } else {
2643 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002644 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002645 (addr & ~TARGET_PAGE_MASK);
2646 memcpy(buf, ptr, l);
2647 }
2648 }
2649 len -= l;
2650 buf += l;
2651 addr += l;
2652 }
2653}
bellard8df1cd02005-01-28 22:37:22 +00002654
bellardd0ecd2a2006-04-23 17:14:48 +00002655/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002656void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002657 const uint8_t *buf, int len)
2658{
2659 int l;
2660 uint8_t *ptr;
2661 target_phys_addr_t page;
2662 unsigned long pd;
2663 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002664
bellardd0ecd2a2006-04-23 17:14:48 +00002665 while (len > 0) {
2666 page = addr & TARGET_PAGE_MASK;
2667 l = (page + TARGET_PAGE_SIZE) - addr;
2668 if (l > len)
2669 l = len;
2670 p = phys_page_find(page >> TARGET_PAGE_BITS);
2671 if (!p) {
2672 pd = IO_MEM_UNASSIGNED;
2673 } else {
2674 pd = p->phys_offset;
2675 }
ths3b46e622007-09-17 08:09:54 +00002676
bellardd0ecd2a2006-04-23 17:14:48 +00002677 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002678 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2679 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002680 /* do nothing */
2681 } else {
2682 unsigned long addr1;
2683 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2684 /* ROM/RAM case */
2685 ptr = phys_ram_base + addr1;
2686 memcpy(ptr, buf, l);
2687 }
2688 len -= l;
2689 buf += l;
2690 addr += l;
2691 }
2692}
2693
2694
bellard8df1cd02005-01-28 22:37:22 +00002695/* warning: addr must be aligned */
2696uint32_t ldl_phys(target_phys_addr_t addr)
2697{
2698 int io_index;
2699 uint8_t *ptr;
2700 uint32_t val;
2701 unsigned long pd;
2702 PhysPageDesc *p;
2703
2704 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2705 if (!p) {
2706 pd = IO_MEM_UNASSIGNED;
2707 } else {
2708 pd = p->phys_offset;
2709 }
ths3b46e622007-09-17 08:09:54 +00002710
ths5fafdf22007-09-16 21:08:06 +00002711 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002712 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002713 /* I/O case */
2714 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2715 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2716 } else {
2717 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002718 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002719 (addr & ~TARGET_PAGE_MASK);
2720 val = ldl_p(ptr);
2721 }
2722 return val;
2723}
2724
bellard84b7b8e2005-11-28 21:19:04 +00002725/* warning: addr must be aligned */
2726uint64_t ldq_phys(target_phys_addr_t addr)
2727{
2728 int io_index;
2729 uint8_t *ptr;
2730 uint64_t val;
2731 unsigned long pd;
2732 PhysPageDesc *p;
2733
2734 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2735 if (!p) {
2736 pd = IO_MEM_UNASSIGNED;
2737 } else {
2738 pd = p->phys_offset;
2739 }
ths3b46e622007-09-17 08:09:54 +00002740
bellard2a4188a2006-06-25 21:54:59 +00002741 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2742 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002743 /* I/O case */
2744 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2745#ifdef TARGET_WORDS_BIGENDIAN
2746 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2747 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2748#else
2749 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2750 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2751#endif
2752 } else {
2753 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002754 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002755 (addr & ~TARGET_PAGE_MASK);
2756 val = ldq_p(ptr);
2757 }
2758 return val;
2759}
2760
bellardaab33092005-10-30 20:48:42 +00002761/* XXX: optimize */
2762uint32_t ldub_phys(target_phys_addr_t addr)
2763{
2764 uint8_t val;
2765 cpu_physical_memory_read(addr, &val, 1);
2766 return val;
2767}
2768
2769/* XXX: optimize */
2770uint32_t lduw_phys(target_phys_addr_t addr)
2771{
2772 uint16_t val;
2773 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2774 return tswap16(val);
2775}
2776
bellard8df1cd02005-01-28 22:37:22 +00002777/* warning: addr must be aligned. The ram page is not masked as dirty
2778 and the code inside is not invalidated. It is useful if the dirty
2779 bits are used to track modified PTEs */
2780void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2781{
2782 int io_index;
2783 uint8_t *ptr;
2784 unsigned long pd;
2785 PhysPageDesc *p;
2786
2787 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2788 if (!p) {
2789 pd = IO_MEM_UNASSIGNED;
2790 } else {
2791 pd = p->phys_offset;
2792 }
ths3b46e622007-09-17 08:09:54 +00002793
bellard3a7d9292005-08-21 09:26:42 +00002794 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002795 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2796 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2797 } else {
ths5fafdf22007-09-16 21:08:06 +00002798 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002799 (addr & ~TARGET_PAGE_MASK);
2800 stl_p(ptr, val);
2801 }
2802}
2803
j_mayerbc98a7e2007-04-04 07:55:12 +00002804void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2805{
2806 int io_index;
2807 uint8_t *ptr;
2808 unsigned long pd;
2809 PhysPageDesc *p;
2810
2811 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2812 if (!p) {
2813 pd = IO_MEM_UNASSIGNED;
2814 } else {
2815 pd = p->phys_offset;
2816 }
ths3b46e622007-09-17 08:09:54 +00002817
j_mayerbc98a7e2007-04-04 07:55:12 +00002818 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2819 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2820#ifdef TARGET_WORDS_BIGENDIAN
2821 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2822 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2823#else
2824 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2825 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2826#endif
2827 } else {
ths5fafdf22007-09-16 21:08:06 +00002828 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002829 (addr & ~TARGET_PAGE_MASK);
2830 stq_p(ptr, val);
2831 }
2832}
2833
bellard8df1cd02005-01-28 22:37:22 +00002834/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002835void stl_phys(target_phys_addr_t addr, uint32_t val)
2836{
2837 int io_index;
2838 uint8_t *ptr;
2839 unsigned long pd;
2840 PhysPageDesc *p;
2841
2842 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2843 if (!p) {
2844 pd = IO_MEM_UNASSIGNED;
2845 } else {
2846 pd = p->phys_offset;
2847 }
ths3b46e622007-09-17 08:09:54 +00002848
bellard3a7d9292005-08-21 09:26:42 +00002849 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002850 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2851 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2852 } else {
2853 unsigned long addr1;
2854 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2855 /* RAM case */
2856 ptr = phys_ram_base + addr1;
2857 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002858 if (!cpu_physical_memory_is_dirty(addr1)) {
2859 /* invalidate code */
2860 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2861 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002862 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2863 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002864 }
bellard8df1cd02005-01-28 22:37:22 +00002865 }
2866}
2867
bellardaab33092005-10-30 20:48:42 +00002868/* XXX: optimize */
2869void stb_phys(target_phys_addr_t addr, uint32_t val)
2870{
2871 uint8_t v = val;
2872 cpu_physical_memory_write(addr, &v, 1);
2873}
2874
2875/* XXX: optimize */
2876void stw_phys(target_phys_addr_t addr, uint32_t val)
2877{
2878 uint16_t v = tswap16(val);
2879 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2880}
2881
2882/* XXX: optimize */
2883void stq_phys(target_phys_addr_t addr, uint64_t val)
2884{
2885 val = tswap64(val);
2886 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2887}
2888
bellard13eb76e2004-01-24 15:23:36 +00002889#endif
2890
2891/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002892int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002893 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002894{
2895 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002896 target_phys_addr_t phys_addr;
2897 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002898
2899 while (len > 0) {
2900 page = addr & TARGET_PAGE_MASK;
2901 phys_addr = cpu_get_phys_page_debug(env, page);
2902 /* if no physical page mapped, return an error */
2903 if (phys_addr == -1)
2904 return -1;
2905 l = (page + TARGET_PAGE_SIZE) - addr;
2906 if (l > len)
2907 l = len;
ths5fafdf22007-09-16 21:08:06 +00002908 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00002909 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002910 len -= l;
2911 buf += l;
2912 addr += l;
2913 }
2914 return 0;
2915}
2916
bellarde3db7222005-01-26 22:00:47 +00002917void dump_exec_info(FILE *f,
2918 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
2919{
2920 int i, target_code_size, max_target_code_size;
2921 int direct_jmp_count, direct_jmp2_count, cross_page;
2922 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00002923
bellarde3db7222005-01-26 22:00:47 +00002924 target_code_size = 0;
2925 max_target_code_size = 0;
2926 cross_page = 0;
2927 direct_jmp_count = 0;
2928 direct_jmp2_count = 0;
2929 for(i = 0; i < nb_tbs; i++) {
2930 tb = &tbs[i];
2931 target_code_size += tb->size;
2932 if (tb->size > max_target_code_size)
2933 max_target_code_size = tb->size;
2934 if (tb->page_addr[1] != -1)
2935 cross_page++;
2936 if (tb->tb_next_offset[0] != 0xffff) {
2937 direct_jmp_count++;
2938 if (tb->tb_next_offset[1] != 0xffff) {
2939 direct_jmp2_count++;
2940 }
2941 }
2942 }
2943 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00002944 cpu_fprintf(f, "Translation buffer state:\n");
bellarde3db7222005-01-26 22:00:47 +00002945 cpu_fprintf(f, "TB count %d\n", nb_tbs);
ths5fafdf22007-09-16 21:08:06 +00002946 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00002947 nb_tbs ? target_code_size / nb_tbs : 0,
2948 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00002949 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00002950 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
2951 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00002952 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
2953 cross_page,
bellarde3db7222005-01-26 22:00:47 +00002954 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
2955 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00002956 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00002957 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
2958 direct_jmp2_count,
2959 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00002960 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00002961 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
2962 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
2963 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellard57fec1f2008-02-01 10:50:11 +00002964#ifdef CONFIG_PROFILER
2965 {
2966 int64_t tot;
2967 tot = dyngen_interm_time + dyngen_code_time;
2968 cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2969 tot, tot / 2.4e9);
2970 cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2971 dyngen_tb_count,
2972 dyngen_tb_count1 - dyngen_tb_count,
2973 dyngen_tb_count1 ? (double)(dyngen_tb_count1 - dyngen_tb_count) / dyngen_tb_count1 * 100.0 : 0);
2974 cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n",
2975 dyngen_tb_count ? (double)dyngen_op_count / dyngen_tb_count : 0, dyngen_op_count_max);
2976 cpu_fprintf(f, "old ops/total ops %0.1f%%\n",
2977 dyngen_op_count ? (double)dyngen_old_op_count / dyngen_op_count * 100.0 : 0);
2978 cpu_fprintf(f, "deleted ops/TB %0.2f\n",
2979 dyngen_tb_count ?
2980 (double)dyngen_tcg_del_op_count / dyngen_tb_count : 0);
2981 cpu_fprintf(f, "cycles/op %0.1f\n",
2982 dyngen_op_count ? (double)tot / dyngen_op_count : 0);
2983 cpu_fprintf(f, "cycles/in byte %0.1f\n",
2984 dyngen_code_in_len ? (double)tot / dyngen_code_in_len : 0);
2985 cpu_fprintf(f, "cycles/out byte %0.1f\n",
2986 dyngen_code_out_len ? (double)tot / dyngen_code_out_len : 0);
2987 if (tot == 0)
2988 tot = 1;
2989 cpu_fprintf(f, " gen_interm time %0.1f%%\n",
2990 (double)dyngen_interm_time / tot * 100.0);
2991 cpu_fprintf(f, " gen_code time %0.1f%%\n",
2992 (double)dyngen_code_time / tot * 100.0);
2993 cpu_fprintf(f, "cpu_restore count %" PRId64 "\n",
2994 dyngen_restore_count);
2995 cpu_fprintf(f, " avg cycles %0.1f\n",
2996 dyngen_restore_count ? (double)dyngen_restore_time / dyngen_restore_count : 0);
2997 {
2998 extern void dump_op_count(void);
2999 dump_op_count();
3000 }
3001 }
3002#endif
bellarde3db7222005-01-26 22:00:47 +00003003}
3004
ths5fafdf22007-09-16 21:08:06 +00003005#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003006
3007#define MMUSUFFIX _cmmu
3008#define GETPC() NULL
3009#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003010#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003011
3012#define SHIFT 0
3013#include "softmmu_template.h"
3014
3015#define SHIFT 1
3016#include "softmmu_template.h"
3017
3018#define SHIFT 2
3019#include "softmmu_template.h"
3020
3021#define SHIFT 3
3022#include "softmmu_template.h"
3023
3024#undef env
3025
3026#endif