blob: fd08ec92782b3c6fa5fc1f3c3ccb737dfb18a35e [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
22#include <windows.h>
23#else
bellarda98d49b2004-11-14 16:22:05 +000024#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000025#include <sys/mman.h>
26#endif
bellard54936002003-05-13 00:25:15 +000027#include <stdlib.h>
28#include <stdio.h>
29#include <stdarg.h>
30#include <string.h>
31#include <errno.h>
32#include <unistd.h>
33#include <inttypes.h>
34
bellard6180a182003-09-30 21:04:53 +000035#include "cpu.h"
36#include "exec-all.h"
pbrook53a59602006-03-25 19:31:22 +000037#if defined(CONFIG_USER_ONLY)
38#include <qemu.h>
39#endif
bellard54936002003-05-13 00:25:15 +000040
bellardfd6ce8f2003-05-14 19:00:11 +000041//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000042//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000043//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000044//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000045
46/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000047//#define DEBUG_TB_CHECK
48//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000049
ths1196be32007-03-17 15:17:58 +000050//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000051//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000052
pbrook99773bd2006-04-16 15:14:59 +000053#if !defined(CONFIG_USER_ONLY)
54/* TB consistency checks only implemented for usermode emulation. */
55#undef DEBUG_TB_CHECK
56#endif
57
bellardfd6ce8f2003-05-14 19:00:11 +000058/* threshold to flush the translated code buffer */
blueswir1d07bde82007-12-11 19:35:45 +000059#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - code_gen_max_block_size())
bellardfd6ce8f2003-05-14 19:00:11 +000060
bellard9fa3e852004-01-04 18:06:42 +000061#define SMC_BITMAP_USE_THRESHOLD 10
62
63#define MMAP_AREA_START 0x00000000
64#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000065
bellard108c49b2005-07-24 12:55:09 +000066#if defined(TARGET_SPARC64)
67#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000068#elif defined(TARGET_SPARC)
69#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000070#elif defined(TARGET_ALPHA)
71#define TARGET_PHYS_ADDR_SPACE_BITS 42
72#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000073#elif defined(TARGET_PPC64)
74#define TARGET_PHYS_ADDR_SPACE_BITS 42
75#else
76/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
77#define TARGET_PHYS_ADDR_SPACE_BITS 32
78#endif
79
bellardfd6ce8f2003-05-14 19:00:11 +000080TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
bellard9fa3e852004-01-04 18:06:42 +000081TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000082int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000083/* any access to the tbs or the page table must use this lock */
84spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000085
bellardb8076a72005-04-07 22:20:31 +000086uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
bellardfd6ce8f2003-05-14 19:00:11 +000087uint8_t *code_gen_ptr;
88
bellard9fa3e852004-01-04 18:06:42 +000089int phys_ram_size;
90int phys_ram_fd;
91uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +000092uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +000093static ram_addr_t phys_ram_alloc_offset = 0;
bellard9fa3e852004-01-04 18:06:42 +000094
bellard6a00d602005-11-21 23:25:50 +000095CPUState *first_cpu;
96/* current CPU in the current thread. It is only valid inside
97 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +000098CPUState *cpu_single_env;
bellard6a00d602005-11-21 23:25:50 +000099
bellard54936002003-05-13 00:25:15 +0000100typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000101 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000102 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000103 /* in order to optimize self modifying code, we count the number
104 of lookups we do to a given page to use a bitmap */
105 unsigned int code_write_count;
106 uint8_t *code_bitmap;
107#if defined(CONFIG_USER_ONLY)
108 unsigned long flags;
109#endif
bellard54936002003-05-13 00:25:15 +0000110} PageDesc;
111
bellard92e873b2004-05-21 14:52:29 +0000112typedef struct PhysPageDesc {
113 /* offset in host memory of the page + io_index in the low 12 bits */
bellarde04f40b2005-04-24 18:02:38 +0000114 uint32_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000115} PhysPageDesc;
116
bellard54936002003-05-13 00:25:15 +0000117#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000118#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
119/* XXX: this is a temporary hack for alpha target.
120 * In the future, this is to be replaced by a multi-level table
121 * to actually be able to handle the complete 64 bits address space.
122 */
123#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
124#else
bellard54936002003-05-13 00:25:15 +0000125#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000126#endif
bellard54936002003-05-13 00:25:15 +0000127
128#define L1_SIZE (1 << L1_BITS)
129#define L2_SIZE (1 << L2_BITS)
130
bellard33417e72003-08-10 21:47:01 +0000131static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000132
bellard83fb7ad2004-07-05 21:25:26 +0000133unsigned long qemu_real_host_page_size;
134unsigned long qemu_host_page_bits;
135unsigned long qemu_host_page_size;
136unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000137
bellard92e873b2004-05-21 14:52:29 +0000138/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000139static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000140PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000141
bellard33417e72003-08-10 21:47:01 +0000142/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000143CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
144CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000145void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000146static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000147#if defined(CONFIG_SOFTMMU)
148static int io_mem_watch;
149#endif
bellard33417e72003-08-10 21:47:01 +0000150
bellard34865132003-10-05 14:28:56 +0000151/* log support */
152char *logfilename = "/tmp/qemu.log";
153FILE *logfile;
154int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000155static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000156
bellarde3db7222005-01-26 22:00:47 +0000157/* statistics */
158static int tlb_flush_count;
159static int tb_flush_count;
160static int tb_phys_invalidate_count;
161
blueswir1db7b5422007-05-26 17:36:03 +0000162#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
163typedef struct subpage_t {
164 target_phys_addr_t base;
165 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE];
166 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE];
167 void *opaque[TARGET_PAGE_SIZE];
168} subpage_t;
169
bellardb346ff42003-06-15 20:05:50 +0000170static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000171{
bellard83fb7ad2004-07-05 21:25:26 +0000172 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000173 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000174#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000175 {
176 SYSTEM_INFO system_info;
177 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000178
bellardd5a8f072004-09-29 21:15:28 +0000179 GetSystemInfo(&system_info);
180 qemu_real_host_page_size = system_info.dwPageSize;
ths3b46e622007-09-17 08:09:54 +0000181
bellardd5a8f072004-09-29 21:15:28 +0000182 VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
183 PAGE_EXECUTE_READWRITE, &old_protect);
184 }
bellard67b915a2004-03-31 23:37:16 +0000185#else
bellard83fb7ad2004-07-05 21:25:26 +0000186 qemu_real_host_page_size = getpagesize();
bellardd5a8f072004-09-29 21:15:28 +0000187 {
188 unsigned long start, end;
189
190 start = (unsigned long)code_gen_buffer;
191 start &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000192
bellardd5a8f072004-09-29 21:15:28 +0000193 end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
194 end += qemu_real_host_page_size - 1;
195 end &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000196
ths5fafdf22007-09-16 21:08:06 +0000197 mprotect((void *)start, end - start,
bellardd5a8f072004-09-29 21:15:28 +0000198 PROT_READ | PROT_WRITE | PROT_EXEC);
199 }
bellard67b915a2004-03-31 23:37:16 +0000200#endif
bellardd5a8f072004-09-29 21:15:28 +0000201
bellard83fb7ad2004-07-05 21:25:26 +0000202 if (qemu_host_page_size == 0)
203 qemu_host_page_size = qemu_real_host_page_size;
204 if (qemu_host_page_size < TARGET_PAGE_SIZE)
205 qemu_host_page_size = TARGET_PAGE_SIZE;
206 qemu_host_page_bits = 0;
207 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
208 qemu_host_page_bits++;
209 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000210 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
211 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
bellard54936002003-05-13 00:25:15 +0000212}
213
bellardfd6ce8f2003-05-14 19:00:11 +0000214static inline PageDesc *page_find_alloc(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000215{
bellard54936002003-05-13 00:25:15 +0000216 PageDesc **lp, *p;
217
bellard54936002003-05-13 00:25:15 +0000218 lp = &l1_map[index >> L2_BITS];
219 p = *lp;
220 if (!p) {
221 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000222 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000223 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000224 *lp = p;
225 }
226 return p + (index & (L2_SIZE - 1));
227}
228
bellardfd6ce8f2003-05-14 19:00:11 +0000229static inline PageDesc *page_find(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000230{
bellard54936002003-05-13 00:25:15 +0000231 PageDesc *p;
232
bellard54936002003-05-13 00:25:15 +0000233 p = l1_map[index >> L2_BITS];
234 if (!p)
235 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000236 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000237}
238
bellard108c49b2005-07-24 12:55:09 +0000239static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000240{
bellard108c49b2005-07-24 12:55:09 +0000241 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000242 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000243
bellard108c49b2005-07-24 12:55:09 +0000244 p = (void **)l1_phys_map;
245#if TARGET_PHYS_ADDR_SPACE_BITS > 32
246
247#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
248#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
249#endif
250 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000251 p = *lp;
252 if (!p) {
253 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000254 if (!alloc)
255 return NULL;
256 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
257 memset(p, 0, sizeof(void *) * L1_SIZE);
258 *lp = p;
259 }
260#endif
261 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000262 pd = *lp;
263 if (!pd) {
264 int i;
bellard108c49b2005-07-24 12:55:09 +0000265 /* allocate if not found */
266 if (!alloc)
267 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000268 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
269 *lp = pd;
270 for (i = 0; i < L2_SIZE; i++)
271 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000272 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000273 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000274}
275
bellard108c49b2005-07-24 12:55:09 +0000276static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000277{
bellard108c49b2005-07-24 12:55:09 +0000278 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000279}
280
bellard9fa3e852004-01-04 18:06:42 +0000281#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000282static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000283static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000284 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000285#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000286
bellard6a00d602005-11-21 23:25:50 +0000287void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000288{
bellard6a00d602005-11-21 23:25:50 +0000289 CPUState **penv;
290 int cpu_index;
291
bellardfd6ce8f2003-05-14 19:00:11 +0000292 if (!code_gen_ptr) {
293 code_gen_ptr = code_gen_buffer;
bellardb346ff42003-06-15 20:05:50 +0000294 page_init();
bellard33417e72003-08-10 21:47:01 +0000295 io_mem_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000296 }
bellard6a00d602005-11-21 23:25:50 +0000297 env->next_cpu = NULL;
298 penv = &first_cpu;
299 cpu_index = 0;
300 while (*penv != NULL) {
301 penv = (CPUState **)&(*penv)->next_cpu;
302 cpu_index++;
303 }
304 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000305 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000306 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000307}
308
bellard9fa3e852004-01-04 18:06:42 +0000309static inline void invalidate_page_bitmap(PageDesc *p)
310{
311 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000312 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000313 p->code_bitmap = NULL;
314 }
315 p->code_write_count = 0;
316}
317
bellardfd6ce8f2003-05-14 19:00:11 +0000318/* set to NULL all the 'first_tb' fields in all PageDescs */
319static void page_flush_tb(void)
320{
321 int i, j;
322 PageDesc *p;
323
324 for(i = 0; i < L1_SIZE; i++) {
325 p = l1_map[i];
326 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000327 for(j = 0; j < L2_SIZE; j++) {
328 p->first_tb = NULL;
329 invalidate_page_bitmap(p);
330 p++;
331 }
bellardfd6ce8f2003-05-14 19:00:11 +0000332 }
333 }
334}
335
336/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000337/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000338void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000339{
bellard6a00d602005-11-21 23:25:50 +0000340 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000341#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000342 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
343 (unsigned long)(code_gen_ptr - code_gen_buffer),
344 nb_tbs, nb_tbs > 0 ?
345 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000346#endif
347 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000348
bellard6a00d602005-11-21 23:25:50 +0000349 for(env = first_cpu; env != NULL; env = env->next_cpu) {
350 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
351 }
bellard9fa3e852004-01-04 18:06:42 +0000352
bellard8a8a6082004-10-03 13:36:49 +0000353 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000354 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000355
bellardfd6ce8f2003-05-14 19:00:11 +0000356 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000357 /* XXX: flush processor icache at this point if cache flush is
358 expensive */
bellarde3db7222005-01-26 22:00:47 +0000359 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000360}
361
362#ifdef DEBUG_TB_CHECK
363
j_mayerbc98a7e2007-04-04 07:55:12 +0000364static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000365{
366 TranslationBlock *tb;
367 int i;
368 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000369 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
370 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000371 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
372 address >= tb->pc + tb->size)) {
373 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000374 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000375 }
376 }
377 }
378}
379
380/* verify that all the pages have correct rights for code */
381static void tb_page_check(void)
382{
383 TranslationBlock *tb;
384 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000385
pbrook99773bd2006-04-16 15:14:59 +0000386 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
387 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000388 flags1 = page_get_flags(tb->pc);
389 flags2 = page_get_flags(tb->pc + tb->size - 1);
390 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
391 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000392 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000393 }
394 }
395 }
396}
397
bellardd4e81642003-05-25 16:46:15 +0000398void tb_jmp_check(TranslationBlock *tb)
399{
400 TranslationBlock *tb1;
401 unsigned int n1;
402
403 /* suppress any remaining jumps to this TB */
404 tb1 = tb->jmp_first;
405 for(;;) {
406 n1 = (long)tb1 & 3;
407 tb1 = (TranslationBlock *)((long)tb1 & ~3);
408 if (n1 == 2)
409 break;
410 tb1 = tb1->jmp_next[n1];
411 }
412 /* check end of list */
413 if (tb1 != tb) {
414 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
415 }
416}
417
bellardfd6ce8f2003-05-14 19:00:11 +0000418#endif
419
420/* invalidate one TB */
421static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
422 int next_offset)
423{
424 TranslationBlock *tb1;
425 for(;;) {
426 tb1 = *ptb;
427 if (tb1 == tb) {
428 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
429 break;
430 }
431 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
432 }
433}
434
bellard9fa3e852004-01-04 18:06:42 +0000435static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
436{
437 TranslationBlock *tb1;
438 unsigned int n1;
439
440 for(;;) {
441 tb1 = *ptb;
442 n1 = (long)tb1 & 3;
443 tb1 = (TranslationBlock *)((long)tb1 & ~3);
444 if (tb1 == tb) {
445 *ptb = tb1->page_next[n1];
446 break;
447 }
448 ptb = &tb1->page_next[n1];
449 }
450}
451
bellardd4e81642003-05-25 16:46:15 +0000452static inline void tb_jmp_remove(TranslationBlock *tb, int n)
453{
454 TranslationBlock *tb1, **ptb;
455 unsigned int n1;
456
457 ptb = &tb->jmp_next[n];
458 tb1 = *ptb;
459 if (tb1) {
460 /* find tb(n) in circular list */
461 for(;;) {
462 tb1 = *ptb;
463 n1 = (long)tb1 & 3;
464 tb1 = (TranslationBlock *)((long)tb1 & ~3);
465 if (n1 == n && tb1 == tb)
466 break;
467 if (n1 == 2) {
468 ptb = &tb1->jmp_first;
469 } else {
470 ptb = &tb1->jmp_next[n1];
471 }
472 }
473 /* now we can suppress tb(n) from the list */
474 *ptb = tb->jmp_next[n];
475
476 tb->jmp_next[n] = NULL;
477 }
478}
479
480/* reset the jump entry 'n' of a TB so that it is not chained to
481 another TB */
482static inline void tb_reset_jump(TranslationBlock *tb, int n)
483{
484 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
485}
486
bellard9fa3e852004-01-04 18:06:42 +0000487static inline void tb_phys_invalidate(TranslationBlock *tb, unsigned int page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000488{
bellard6a00d602005-11-21 23:25:50 +0000489 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000490 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000491 unsigned int h, n1;
bellard9fa3e852004-01-04 18:06:42 +0000492 target_ulong phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000493 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000494
bellard9fa3e852004-01-04 18:06:42 +0000495 /* remove the TB from the hash list */
496 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
497 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000498 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000499 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000500
bellard9fa3e852004-01-04 18:06:42 +0000501 /* remove the TB from the page list */
502 if (tb->page_addr[0] != page_addr) {
503 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
504 tb_page_remove(&p->first_tb, tb);
505 invalidate_page_bitmap(p);
506 }
507 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
508 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
509 tb_page_remove(&p->first_tb, tb);
510 invalidate_page_bitmap(p);
511 }
512
bellard8a40a182005-11-20 10:35:40 +0000513 tb_invalidated_flag = 1;
514
515 /* remove the TB from the hash list */
516 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000517 for(env = first_cpu; env != NULL; env = env->next_cpu) {
518 if (env->tb_jmp_cache[h] == tb)
519 env->tb_jmp_cache[h] = NULL;
520 }
bellard8a40a182005-11-20 10:35:40 +0000521
522 /* suppress this TB from the two jump lists */
523 tb_jmp_remove(tb, 0);
524 tb_jmp_remove(tb, 1);
525
526 /* suppress any remaining jumps to this TB */
527 tb1 = tb->jmp_first;
528 for(;;) {
529 n1 = (long)tb1 & 3;
530 if (n1 == 2)
531 break;
532 tb1 = (TranslationBlock *)((long)tb1 & ~3);
533 tb2 = tb1->jmp_next[n1];
534 tb_reset_jump(tb1, n1);
535 tb1->jmp_next[n1] = NULL;
536 tb1 = tb2;
537 }
538 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
539
bellarde3db7222005-01-26 22:00:47 +0000540 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000541}
542
543static inline void set_bits(uint8_t *tab, int start, int len)
544{
545 int end, mask, end1;
546
547 end = start + len;
548 tab += start >> 3;
549 mask = 0xff << (start & 7);
550 if ((start & ~7) == (end & ~7)) {
551 if (start < end) {
552 mask &= ~(0xff << (end & 7));
553 *tab |= mask;
554 }
555 } else {
556 *tab++ |= mask;
557 start = (start + 8) & ~7;
558 end1 = end & ~7;
559 while (start < end1) {
560 *tab++ = 0xff;
561 start += 8;
562 }
563 if (start < end) {
564 mask = ~(0xff << (end & 7));
565 *tab |= mask;
566 }
567 }
568}
569
570static void build_page_bitmap(PageDesc *p)
571{
572 int n, tb_start, tb_end;
573 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000574
bellard59817cc2004-02-16 22:01:13 +0000575 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000576 if (!p->code_bitmap)
577 return;
578 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
579
580 tb = p->first_tb;
581 while (tb != NULL) {
582 n = (long)tb & 3;
583 tb = (TranslationBlock *)((long)tb & ~3);
584 /* NOTE: this is subtle as a TB may span two physical pages */
585 if (n == 0) {
586 /* NOTE: tb_end may be after the end of the page, but
587 it is not a problem */
588 tb_start = tb->pc & ~TARGET_PAGE_MASK;
589 tb_end = tb_start + tb->size;
590 if (tb_end > TARGET_PAGE_SIZE)
591 tb_end = TARGET_PAGE_SIZE;
592 } else {
593 tb_start = 0;
594 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
595 }
596 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
597 tb = tb->page_next[n];
598 }
599}
600
bellardd720b932004-04-25 17:57:43 +0000601#ifdef TARGET_HAS_PRECISE_SMC
602
ths5fafdf22007-09-16 21:08:06 +0000603static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000604 target_ulong pc, target_ulong cs_base, int flags,
605 int cflags)
606{
607 TranslationBlock *tb;
608 uint8_t *tc_ptr;
609 target_ulong phys_pc, phys_page2, virt_page2;
610 int code_gen_size;
611
bellardc27004e2005-01-03 23:35:10 +0000612 phys_pc = get_phys_addr_code(env, pc);
613 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000614 if (!tb) {
615 /* flush must be done */
616 tb_flush(env);
617 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000618 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000619 }
620 tc_ptr = code_gen_ptr;
621 tb->tc_ptr = tc_ptr;
622 tb->cs_base = cs_base;
623 tb->flags = flags;
624 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000625 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000626 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 +0000627
bellardd720b932004-04-25 17:57:43 +0000628 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000629 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000630 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000631 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000632 phys_page2 = get_phys_addr_code(env, virt_page2);
633 }
634 tb_link_phys(tb, phys_pc, phys_page2);
635}
636#endif
ths3b46e622007-09-17 08:09:54 +0000637
bellard9fa3e852004-01-04 18:06:42 +0000638/* invalidate all TBs which intersect with the target physical page
639 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000640 the same physical page. 'is_cpu_write_access' should be true if called
641 from a real cpu write access: the virtual CPU will exit the current
642 TB if code is modified inside this TB. */
ths5fafdf22007-09-16 21:08:06 +0000643void tb_invalidate_phys_page_range(target_ulong start, target_ulong end,
bellardd720b932004-04-25 17:57:43 +0000644 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000645{
bellardd720b932004-04-25 17:57:43 +0000646 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000647 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000648 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000649 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000650 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000651 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000652
653 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000654 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000655 return;
ths5fafdf22007-09-16 21:08:06 +0000656 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000657 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
658 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000659 /* build code bitmap */
660 build_page_bitmap(p);
661 }
662
663 /* we remove all the TBs in the range [start, end[ */
664 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000665 current_tb_not_found = is_cpu_write_access;
666 current_tb_modified = 0;
667 current_tb = NULL; /* avoid warning */
668 current_pc = 0; /* avoid warning */
669 current_cs_base = 0; /* avoid warning */
670 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000671 tb = p->first_tb;
672 while (tb != NULL) {
673 n = (long)tb & 3;
674 tb = (TranslationBlock *)((long)tb & ~3);
675 tb_next = tb->page_next[n];
676 /* NOTE: this is subtle as a TB may span two physical pages */
677 if (n == 0) {
678 /* NOTE: tb_end may be after the end of the page, but
679 it is not a problem */
680 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
681 tb_end = tb_start + tb->size;
682 } else {
683 tb_start = tb->page_addr[1];
684 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
685 }
686 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000687#ifdef TARGET_HAS_PRECISE_SMC
688 if (current_tb_not_found) {
689 current_tb_not_found = 0;
690 current_tb = NULL;
691 if (env->mem_write_pc) {
692 /* now we have a real cpu fault */
693 current_tb = tb_find_pc(env->mem_write_pc);
694 }
695 }
696 if (current_tb == tb &&
697 !(current_tb->cflags & CF_SINGLE_INSN)) {
698 /* If we are modifying the current TB, we must stop
699 its execution. We could be more precise by checking
700 that the modification is after the current PC, but it
701 would require a specialized function to partially
702 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000703
bellardd720b932004-04-25 17:57:43 +0000704 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000705 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000706 env->mem_write_pc, NULL);
707#if defined(TARGET_I386)
708 current_flags = env->hflags;
709 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
710 current_cs_base = (target_ulong)env->segs[R_CS].base;
711 current_pc = current_cs_base + env->eip;
712#else
713#error unsupported CPU
714#endif
715 }
716#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000717 /* we need to do that to handle the case where a signal
718 occurs while doing tb_phys_invalidate() */
719 saved_tb = NULL;
720 if (env) {
721 saved_tb = env->current_tb;
722 env->current_tb = NULL;
723 }
bellard9fa3e852004-01-04 18:06:42 +0000724 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000725 if (env) {
726 env->current_tb = saved_tb;
727 if (env->interrupt_request && env->current_tb)
728 cpu_interrupt(env, env->interrupt_request);
729 }
bellard9fa3e852004-01-04 18:06:42 +0000730 }
731 tb = tb_next;
732 }
733#if !defined(CONFIG_USER_ONLY)
734 /* if no code remaining, no need to continue to use slow writes */
735 if (!p->first_tb) {
736 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000737 if (is_cpu_write_access) {
738 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
739 }
740 }
741#endif
742#ifdef TARGET_HAS_PRECISE_SMC
743 if (current_tb_modified) {
744 /* we generate a block containing just the instruction
745 modifying the memory. It will ensure that it cannot modify
746 itself */
bellardea1c1802004-06-14 18:56:36 +0000747 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000748 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000749 CF_SINGLE_INSN);
750 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000751 }
752#endif
753}
754
755/* len must be <= 8 and start must be a multiple of len */
bellardd720b932004-04-25 17:57:43 +0000756static inline void tb_invalidate_phys_page_fast(target_ulong start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000757{
758 PageDesc *p;
759 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000760#if 0
bellarda4193c82004-06-03 14:01:43 +0000761 if (1) {
762 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000763 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
764 cpu_single_env->mem_write_vaddr, len,
765 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000766 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
767 }
bellard59817cc2004-02-16 22:01:13 +0000768 }
769#endif
bellard9fa3e852004-01-04 18:06:42 +0000770 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000771 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000772 return;
773 if (p->code_bitmap) {
774 offset = start & ~TARGET_PAGE_MASK;
775 b = p->code_bitmap[offset >> 3] >> (offset & 7);
776 if (b & ((1 << len) - 1))
777 goto do_invalidate;
778 } else {
779 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000780 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000781 }
782}
783
bellard9fa3e852004-01-04 18:06:42 +0000784#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +0000785static void tb_invalidate_phys_page(target_ulong addr,
bellardd720b932004-04-25 17:57:43 +0000786 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000787{
bellardd720b932004-04-25 17:57:43 +0000788 int n, current_flags, current_tb_modified;
789 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000790 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000791 TranslationBlock *tb, *current_tb;
792#ifdef TARGET_HAS_PRECISE_SMC
793 CPUState *env = cpu_single_env;
794#endif
bellard9fa3e852004-01-04 18:06:42 +0000795
796 addr &= TARGET_PAGE_MASK;
797 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000798 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000799 return;
800 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000801 current_tb_modified = 0;
802 current_tb = NULL;
803 current_pc = 0; /* avoid warning */
804 current_cs_base = 0; /* avoid warning */
805 current_flags = 0; /* avoid warning */
806#ifdef TARGET_HAS_PRECISE_SMC
807 if (tb && pc != 0) {
808 current_tb = tb_find_pc(pc);
809 }
810#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000811 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000812 n = (long)tb & 3;
813 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000814#ifdef TARGET_HAS_PRECISE_SMC
815 if (current_tb == tb &&
816 !(current_tb->cflags & CF_SINGLE_INSN)) {
817 /* If we are modifying the current TB, we must stop
818 its execution. We could be more precise by checking
819 that the modification is after the current PC, but it
820 would require a specialized function to partially
821 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000822
bellardd720b932004-04-25 17:57:43 +0000823 current_tb_modified = 1;
824 cpu_restore_state(current_tb, env, pc, puc);
825#if defined(TARGET_I386)
826 current_flags = env->hflags;
827 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
828 current_cs_base = (target_ulong)env->segs[R_CS].base;
829 current_pc = current_cs_base + env->eip;
830#else
831#error unsupported CPU
832#endif
833 }
834#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000835 tb_phys_invalidate(tb, addr);
836 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000837 }
838 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000839#ifdef TARGET_HAS_PRECISE_SMC
840 if (current_tb_modified) {
841 /* we generate a block containing just the instruction
842 modifying the memory. It will ensure that it cannot modify
843 itself */
bellardea1c1802004-06-14 18:56:36 +0000844 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000845 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000846 CF_SINGLE_INSN);
847 cpu_resume_from_signal(env, puc);
848 }
849#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000850}
bellard9fa3e852004-01-04 18:06:42 +0000851#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000852
853/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000854static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000855 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000856{
857 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000858 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000859
bellard9fa3e852004-01-04 18:06:42 +0000860 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000861 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000862 tb->page_next[n] = p->first_tb;
863 last_first_tb = p->first_tb;
864 p->first_tb = (TranslationBlock *)((long)tb | n);
865 invalidate_page_bitmap(p);
866
bellard107db442004-06-22 18:48:46 +0000867#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000868
bellard9fa3e852004-01-04 18:06:42 +0000869#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000870 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000871 target_ulong addr;
872 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000873 int prot;
874
bellardfd6ce8f2003-05-14 19:00:11 +0000875 /* force the host page as non writable (writes will have a
876 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000877 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000878 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000879 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
880 addr += TARGET_PAGE_SIZE) {
881
882 p2 = page_find (addr >> TARGET_PAGE_BITS);
883 if (!p2)
884 continue;
885 prot |= p2->flags;
886 p2->flags &= ~PAGE_WRITE;
887 page_get_flags(addr);
888 }
ths5fafdf22007-09-16 21:08:06 +0000889 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000890 (prot & PAGE_BITS) & ~PAGE_WRITE);
891#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +0000892 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +0000893 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000894#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000895 }
bellard9fa3e852004-01-04 18:06:42 +0000896#else
897 /* if some code is already present, then the pages are already
898 protected. So we handle the case where only the first TB is
899 allocated in a physical page */
900 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000901 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000902 }
903#endif
bellardd720b932004-04-25 17:57:43 +0000904
905#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +0000906}
907
908/* Allocate a new translation block. Flush the translation buffer if
909 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +0000910TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +0000911{
912 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000913
ths5fafdf22007-09-16 21:08:06 +0000914 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
bellardfd6ce8f2003-05-14 19:00:11 +0000915 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
bellardd4e81642003-05-25 16:46:15 +0000916 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +0000917 tb = &tbs[nb_tbs++];
918 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +0000919 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +0000920 return tb;
921}
922
bellard9fa3e852004-01-04 18:06:42 +0000923/* add a new TB and link it to the physical page tables. phys_page2 is
924 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +0000925void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +0000926 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +0000927{
bellard9fa3e852004-01-04 18:06:42 +0000928 unsigned int h;
929 TranslationBlock **ptb;
930
931 /* add in the physical hash table */
932 h = tb_phys_hash_func(phys_pc);
933 ptb = &tb_phys_hash[h];
934 tb->phys_hash_next = *ptb;
935 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000936
937 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +0000938 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
939 if (phys_page2 != -1)
940 tb_alloc_page(tb, 1, phys_page2);
941 else
942 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +0000943
bellardd4e81642003-05-25 16:46:15 +0000944 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
945 tb->jmp_next[0] = NULL;
946 tb->jmp_next[1] = NULL;
947
948 /* init original jump addresses */
949 if (tb->tb_next_offset[0] != 0xffff)
950 tb_reset_jump(tb, 0);
951 if (tb->tb_next_offset[1] != 0xffff)
952 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +0000953
954#ifdef DEBUG_TB_CHECK
955 tb_page_check();
956#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000957}
958
bellarda513fe12003-05-27 23:29:48 +0000959/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
960 tb[1].tc_ptr. Return NULL if not found */
961TranslationBlock *tb_find_pc(unsigned long tc_ptr)
962{
963 int m_min, m_max, m;
964 unsigned long v;
965 TranslationBlock *tb;
966
967 if (nb_tbs <= 0)
968 return NULL;
969 if (tc_ptr < (unsigned long)code_gen_buffer ||
970 tc_ptr >= (unsigned long)code_gen_ptr)
971 return NULL;
972 /* binary search (cf Knuth) */
973 m_min = 0;
974 m_max = nb_tbs - 1;
975 while (m_min <= m_max) {
976 m = (m_min + m_max) >> 1;
977 tb = &tbs[m];
978 v = (unsigned long)tb->tc_ptr;
979 if (v == tc_ptr)
980 return tb;
981 else if (tc_ptr < v) {
982 m_max = m - 1;
983 } else {
984 m_min = m + 1;
985 }
ths5fafdf22007-09-16 21:08:06 +0000986 }
bellarda513fe12003-05-27 23:29:48 +0000987 return &tbs[m_max];
988}
bellard75012672003-06-21 13:11:07 +0000989
bellardea041c02003-06-25 16:16:50 +0000990static void tb_reset_jump_recursive(TranslationBlock *tb);
991
992static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
993{
994 TranslationBlock *tb1, *tb_next, **ptb;
995 unsigned int n1;
996
997 tb1 = tb->jmp_next[n];
998 if (tb1 != NULL) {
999 /* find head of list */
1000 for(;;) {
1001 n1 = (long)tb1 & 3;
1002 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1003 if (n1 == 2)
1004 break;
1005 tb1 = tb1->jmp_next[n1];
1006 }
1007 /* we are now sure now that tb jumps to tb1 */
1008 tb_next = tb1;
1009
1010 /* remove tb from the jmp_first list */
1011 ptb = &tb_next->jmp_first;
1012 for(;;) {
1013 tb1 = *ptb;
1014 n1 = (long)tb1 & 3;
1015 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1016 if (n1 == n && tb1 == tb)
1017 break;
1018 ptb = &tb1->jmp_next[n1];
1019 }
1020 *ptb = tb->jmp_next[n];
1021 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001022
bellardea041c02003-06-25 16:16:50 +00001023 /* suppress the jump to next tb in generated code */
1024 tb_reset_jump(tb, n);
1025
bellard01243112004-01-04 15:48:17 +00001026 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001027 tb_reset_jump_recursive(tb_next);
1028 }
1029}
1030
1031static void tb_reset_jump_recursive(TranslationBlock *tb)
1032{
1033 tb_reset_jump_recursive2(tb, 0);
1034 tb_reset_jump_recursive2(tb, 1);
1035}
1036
bellard1fddef42005-04-17 19:16:13 +00001037#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001038static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1039{
j_mayer9b3c35e2007-04-07 11:21:28 +00001040 target_phys_addr_t addr;
1041 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001042 ram_addr_t ram_addr;
1043 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001044
pbrookc2f07f82006-04-08 17:14:56 +00001045 addr = cpu_get_phys_page_debug(env, pc);
1046 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1047 if (!p) {
1048 pd = IO_MEM_UNASSIGNED;
1049 } else {
1050 pd = p->phys_offset;
1051 }
1052 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001053 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001054}
bellardc27004e2005-01-03 23:35:10 +00001055#endif
bellardd720b932004-04-25 17:57:43 +00001056
pbrook6658ffb2007-03-16 23:58:11 +00001057/* Add a watchpoint. */
1058int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1059{
1060 int i;
1061
1062 for (i = 0; i < env->nb_watchpoints; i++) {
1063 if (addr == env->watchpoint[i].vaddr)
1064 return 0;
1065 }
1066 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1067 return -1;
1068
1069 i = env->nb_watchpoints++;
1070 env->watchpoint[i].vaddr = addr;
1071 tlb_flush_page(env, addr);
1072 /* FIXME: This flush is needed because of the hack to make memory ops
1073 terminate the TB. It can be removed once the proper IO trap and
1074 re-execute bits are in. */
1075 tb_flush(env);
1076 return i;
1077}
1078
1079/* Remove a watchpoint. */
1080int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1081{
1082 int i;
1083
1084 for (i = 0; i < env->nb_watchpoints; i++) {
1085 if (addr == env->watchpoint[i].vaddr) {
1086 env->nb_watchpoints--;
1087 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1088 tlb_flush_page(env, addr);
1089 return 0;
1090 }
1091 }
1092 return -1;
1093}
1094
bellardc33a3462003-07-29 20:50:33 +00001095/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1096 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001097int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001098{
bellard1fddef42005-04-17 19:16:13 +00001099#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001100 int i;
ths3b46e622007-09-17 08:09:54 +00001101
bellard4c3a88a2003-07-26 12:06:08 +00001102 for(i = 0; i < env->nb_breakpoints; i++) {
1103 if (env->breakpoints[i] == pc)
1104 return 0;
1105 }
1106
1107 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1108 return -1;
1109 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001110
bellardd720b932004-04-25 17:57:43 +00001111 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001112 return 0;
1113#else
1114 return -1;
1115#endif
1116}
1117
1118/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001119int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001120{
bellard1fddef42005-04-17 19:16:13 +00001121#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001122 int i;
1123 for(i = 0; i < env->nb_breakpoints; i++) {
1124 if (env->breakpoints[i] == pc)
1125 goto found;
1126 }
1127 return -1;
1128 found:
bellard4c3a88a2003-07-26 12:06:08 +00001129 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001130 if (i < env->nb_breakpoints)
1131 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001132
1133 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001134 return 0;
1135#else
1136 return -1;
1137#endif
1138}
1139
bellardc33a3462003-07-29 20:50:33 +00001140/* enable or disable single step mode. EXCP_DEBUG is returned by the
1141 CPU loop after each instruction */
1142void cpu_single_step(CPUState *env, int enabled)
1143{
bellard1fddef42005-04-17 19:16:13 +00001144#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001145 if (env->singlestep_enabled != enabled) {
1146 env->singlestep_enabled = enabled;
1147 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001148 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001149 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001150 }
1151#endif
1152}
1153
bellard34865132003-10-05 14:28:56 +00001154/* enable or disable low levels log */
1155void cpu_set_log(int log_flags)
1156{
1157 loglevel = log_flags;
1158 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001159 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001160 if (!logfile) {
1161 perror(logfilename);
1162 _exit(1);
1163 }
bellard9fa3e852004-01-04 18:06:42 +00001164#if !defined(CONFIG_SOFTMMU)
1165 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1166 {
1167 static uint8_t logfile_buf[4096];
1168 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1169 }
1170#else
bellard34865132003-10-05 14:28:56 +00001171 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001172#endif
pbrooke735b912007-06-30 13:53:24 +00001173 log_append = 1;
1174 }
1175 if (!loglevel && logfile) {
1176 fclose(logfile);
1177 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001178 }
1179}
1180
1181void cpu_set_log_filename(const char *filename)
1182{
1183 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001184 if (logfile) {
1185 fclose(logfile);
1186 logfile = NULL;
1187 }
1188 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001189}
bellardc33a3462003-07-29 20:50:33 +00001190
bellard01243112004-01-04 15:48:17 +00001191/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001192void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001193{
1194 TranslationBlock *tb;
bellardee8b7022004-02-03 23:35:10 +00001195 static int interrupt_lock;
bellard59817cc2004-02-16 22:01:13 +00001196
bellard68a79312003-06-30 13:12:32 +00001197 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001198 /* if the cpu is currently executing code, we must unlink it and
1199 all the potentially executing TB */
1200 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001201 if (tb && !testandset(&interrupt_lock)) {
1202 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001203 tb_reset_jump_recursive(tb);
bellardee8b7022004-02-03 23:35:10 +00001204 interrupt_lock = 0;
bellardea041c02003-06-25 16:16:50 +00001205 }
1206}
1207
bellardb54ad042004-05-20 13:42:52 +00001208void cpu_reset_interrupt(CPUState *env, int mask)
1209{
1210 env->interrupt_request &= ~mask;
1211}
1212
bellardf193c792004-03-21 17:06:25 +00001213CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001214 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001215 "show generated host assembly code for each compiled TB" },
1216 { CPU_LOG_TB_IN_ASM, "in_asm",
1217 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001218 { CPU_LOG_TB_OP, "op",
bellardf193c792004-03-21 17:06:25 +00001219 "show micro ops for each compiled TB (only usable if 'in_asm' used)" },
1220#ifdef TARGET_I386
1221 { CPU_LOG_TB_OP_OPT, "op_opt",
1222 "show micro ops after optimization for each compiled TB" },
1223#endif
1224 { CPU_LOG_INT, "int",
1225 "show interrupts/exceptions in short format" },
1226 { CPU_LOG_EXEC, "exec",
1227 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001228 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001229 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001230#ifdef TARGET_I386
1231 { CPU_LOG_PCALL, "pcall",
1232 "show protected mode far calls/returns/exceptions" },
1233#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001234#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001235 { CPU_LOG_IOPORT, "ioport",
1236 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001237#endif
bellardf193c792004-03-21 17:06:25 +00001238 { 0, NULL, NULL },
1239};
1240
1241static int cmp1(const char *s1, int n, const char *s2)
1242{
1243 if (strlen(s2) != n)
1244 return 0;
1245 return memcmp(s1, s2, n) == 0;
1246}
ths3b46e622007-09-17 08:09:54 +00001247
bellardf193c792004-03-21 17:06:25 +00001248/* takes a comma separated list of log masks. Return 0 if error. */
1249int cpu_str_to_log_mask(const char *str)
1250{
1251 CPULogItem *item;
1252 int mask;
1253 const char *p, *p1;
1254
1255 p = str;
1256 mask = 0;
1257 for(;;) {
1258 p1 = strchr(p, ',');
1259 if (!p1)
1260 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001261 if(cmp1(p,p1-p,"all")) {
1262 for(item = cpu_log_items; item->mask != 0; item++) {
1263 mask |= item->mask;
1264 }
1265 } else {
bellardf193c792004-03-21 17:06:25 +00001266 for(item = cpu_log_items; item->mask != 0; item++) {
1267 if (cmp1(p, p1 - p, item->name))
1268 goto found;
1269 }
1270 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001271 }
bellardf193c792004-03-21 17:06:25 +00001272 found:
1273 mask |= item->mask;
1274 if (*p1 != ',')
1275 break;
1276 p = p1 + 1;
1277 }
1278 return mask;
1279}
bellardea041c02003-06-25 16:16:50 +00001280
bellard75012672003-06-21 13:11:07 +00001281void cpu_abort(CPUState *env, const char *fmt, ...)
1282{
1283 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001284 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001285
1286 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001287 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001288 fprintf(stderr, "qemu: fatal: ");
1289 vfprintf(stderr, fmt, ap);
1290 fprintf(stderr, "\n");
1291#ifdef TARGET_I386
ths0573fbf2007-09-23 15:28:04 +00001292 if(env->intercept & INTERCEPT_SVM_MASK) {
1293 /* most probably the virtual machine should not
1294 be shut down but rather caught by the VMM */
1295 vmexit(SVM_EXIT_SHUTDOWN, 0);
1296 }
bellard7fe48482004-10-09 18:08:01 +00001297 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1298#else
1299 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001300#endif
balrog924edca2007-06-10 14:07:13 +00001301 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001302 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001303 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001304 fprintf(logfile, "\n");
1305#ifdef TARGET_I386
1306 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1307#else
1308 cpu_dump_state(env, logfile, fprintf, 0);
1309#endif
balrog924edca2007-06-10 14:07:13 +00001310 fflush(logfile);
1311 fclose(logfile);
1312 }
pbrook493ae1f2007-11-23 16:53:59 +00001313 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001314 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001315 abort();
1316}
1317
thsc5be9f02007-02-28 20:20:53 +00001318CPUState *cpu_copy(CPUState *env)
1319{
ths01ba9812007-12-09 02:22:57 +00001320 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001321 /* preserve chaining and index */
1322 CPUState *next_cpu = new_env->next_cpu;
1323 int cpu_index = new_env->cpu_index;
1324 memcpy(new_env, env, sizeof(CPUState));
1325 new_env->next_cpu = next_cpu;
1326 new_env->cpu_index = cpu_index;
1327 return new_env;
1328}
1329
bellard01243112004-01-04 15:48:17 +00001330#if !defined(CONFIG_USER_ONLY)
1331
bellardee8b7022004-02-03 23:35:10 +00001332/* NOTE: if flush_global is true, also flush global entries (not
1333 implemented yet) */
1334void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001335{
bellard33417e72003-08-10 21:47:01 +00001336 int i;
bellard01243112004-01-04 15:48:17 +00001337
bellard9fa3e852004-01-04 18:06:42 +00001338#if defined(DEBUG_TLB)
1339 printf("tlb_flush:\n");
1340#endif
bellard01243112004-01-04 15:48:17 +00001341 /* must reset current TB so that interrupts cannot modify the
1342 links while we are modifying them */
1343 env->current_tb = NULL;
1344
bellard33417e72003-08-10 21:47:01 +00001345 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001346 env->tlb_table[0][i].addr_read = -1;
1347 env->tlb_table[0][i].addr_write = -1;
1348 env->tlb_table[0][i].addr_code = -1;
1349 env->tlb_table[1][i].addr_read = -1;
1350 env->tlb_table[1][i].addr_write = -1;
1351 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001352#if (NB_MMU_MODES >= 3)
1353 env->tlb_table[2][i].addr_read = -1;
1354 env->tlb_table[2][i].addr_write = -1;
1355 env->tlb_table[2][i].addr_code = -1;
1356#if (NB_MMU_MODES == 4)
1357 env->tlb_table[3][i].addr_read = -1;
1358 env->tlb_table[3][i].addr_write = -1;
1359 env->tlb_table[3][i].addr_code = -1;
1360#endif
1361#endif
bellard33417e72003-08-10 21:47:01 +00001362 }
bellard9fa3e852004-01-04 18:06:42 +00001363
bellard8a40a182005-11-20 10:35:40 +00001364 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001365
1366#if !defined(CONFIG_SOFTMMU)
1367 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1368#endif
bellard0a962c02005-02-10 22:00:27 +00001369#ifdef USE_KQEMU
1370 if (env->kqemu_enabled) {
1371 kqemu_flush(env, flush_global);
1372 }
1373#endif
bellarde3db7222005-01-26 22:00:47 +00001374 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001375}
1376
bellard274da6b2004-05-20 21:56:27 +00001377static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001378{
ths5fafdf22007-09-16 21:08:06 +00001379 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001380 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001381 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001382 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001383 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001384 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1385 tlb_entry->addr_read = -1;
1386 tlb_entry->addr_write = -1;
1387 tlb_entry->addr_code = -1;
1388 }
bellard61382a52003-10-27 21:22:23 +00001389}
1390
bellard2e126692004-04-25 21:28:44 +00001391void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001392{
bellard8a40a182005-11-20 10:35:40 +00001393 int i;
bellard9fa3e852004-01-04 18:06:42 +00001394 TranslationBlock *tb;
bellard01243112004-01-04 15:48:17 +00001395
bellard9fa3e852004-01-04 18:06:42 +00001396#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001397 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001398#endif
bellard01243112004-01-04 15:48:17 +00001399 /* must reset current TB so that interrupts cannot modify the
1400 links while we are modifying them */
1401 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001402
bellard61382a52003-10-27 21:22:23 +00001403 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001404 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001405 tlb_flush_entry(&env->tlb_table[0][i], addr);
1406 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001407#if (NB_MMU_MODES >= 3)
1408 tlb_flush_entry(&env->tlb_table[2][i], addr);
1409#if (NB_MMU_MODES == 4)
1410 tlb_flush_entry(&env->tlb_table[3][i], addr);
1411#endif
1412#endif
bellard01243112004-01-04 15:48:17 +00001413
pbrookb362e5e2006-11-12 20:40:55 +00001414 /* Discard jump cache entries for any tb which might potentially
1415 overlap the flushed page. */
1416 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1417 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
1418
1419 i = tb_jmp_cache_hash_page(addr);
1420 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
bellard9fa3e852004-01-04 18:06:42 +00001421
bellard01243112004-01-04 15:48:17 +00001422#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001423 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001424 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001425#endif
bellard0a962c02005-02-10 22:00:27 +00001426#ifdef USE_KQEMU
1427 if (env->kqemu_enabled) {
1428 kqemu_flush_page(env, addr);
1429 }
1430#endif
bellard9fa3e852004-01-04 18:06:42 +00001431}
1432
bellard9fa3e852004-01-04 18:06:42 +00001433/* update the TLBs so that writes to code in the virtual page 'addr'
1434 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001435static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001436{
ths5fafdf22007-09-16 21:08:06 +00001437 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001438 ram_addr + TARGET_PAGE_SIZE,
1439 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001440}
1441
bellard9fa3e852004-01-04 18:06:42 +00001442/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001443 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001444static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001445 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001446{
bellard3a7d9292005-08-21 09:26:42 +00001447 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001448}
1449
ths5fafdf22007-09-16 21:08:06 +00001450static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001451 unsigned long start, unsigned long length)
1452{
1453 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001454 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1455 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001456 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001457 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001458 }
1459 }
1460}
1461
bellard3a7d9292005-08-21 09:26:42 +00001462void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001463 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001464{
1465 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001466 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001467 int i, mask, len;
1468 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001469
1470 start &= TARGET_PAGE_MASK;
1471 end = TARGET_PAGE_ALIGN(end);
1472
1473 length = end - start;
1474 if (length == 0)
1475 return;
bellard0a962c02005-02-10 22:00:27 +00001476 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001477#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001478 /* XXX: should not depend on cpu context */
1479 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001480 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001481 ram_addr_t addr;
1482 addr = start;
1483 for(i = 0; i < len; i++) {
1484 kqemu_set_notdirty(env, addr);
1485 addr += TARGET_PAGE_SIZE;
1486 }
bellard3a7d9292005-08-21 09:26:42 +00001487 }
1488#endif
bellardf23db162005-08-21 19:12:28 +00001489 mask = ~dirty_flags;
1490 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1491 for(i = 0; i < len; i++)
1492 p[i] &= mask;
1493
bellard1ccde1c2004-02-06 19:46:14 +00001494 /* we modify the TLB cache so that the dirty bit will be set again
1495 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001496 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001497 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1498 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001499 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001500 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001501 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001502#if (NB_MMU_MODES >= 3)
1503 for(i = 0; i < CPU_TLB_SIZE; i++)
1504 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1505#if (NB_MMU_MODES == 4)
1506 for(i = 0; i < CPU_TLB_SIZE; i++)
1507 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1508#endif
1509#endif
bellard6a00d602005-11-21 23:25:50 +00001510 }
bellard59817cc2004-02-16 22:01:13 +00001511
1512#if !defined(CONFIG_SOFTMMU)
1513 /* XXX: this is expensive */
1514 {
1515 VirtPageDesc *p;
1516 int j;
1517 target_ulong addr;
1518
1519 for(i = 0; i < L1_SIZE; i++) {
1520 p = l1_virt_map[i];
1521 if (p) {
1522 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1523 for(j = 0; j < L2_SIZE; j++) {
1524 if (p->valid_tag == virt_valid_tag &&
1525 p->phys_addr >= start && p->phys_addr < end &&
1526 (p->prot & PROT_WRITE)) {
1527 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001528 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001529 p->prot & ~PROT_WRITE);
1530 }
1531 }
1532 addr += TARGET_PAGE_SIZE;
1533 p++;
1534 }
1535 }
1536 }
1537 }
1538#endif
bellard1ccde1c2004-02-06 19:46:14 +00001539}
1540
bellard3a7d9292005-08-21 09:26:42 +00001541static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1542{
1543 ram_addr_t ram_addr;
1544
bellard84b7b8e2005-11-28 21:19:04 +00001545 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001546 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001547 tlb_entry->addend - (unsigned long)phys_ram_base;
1548 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001549 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001550 }
1551 }
1552}
1553
1554/* update the TLB according to the current state of the dirty bits */
1555void cpu_tlb_update_dirty(CPUState *env)
1556{
1557 int i;
1558 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001559 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001560 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001561 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001562#if (NB_MMU_MODES >= 3)
1563 for(i = 0; i < CPU_TLB_SIZE; i++)
1564 tlb_update_dirty(&env->tlb_table[2][i]);
1565#if (NB_MMU_MODES == 4)
1566 for(i = 0; i < CPU_TLB_SIZE; i++)
1567 tlb_update_dirty(&env->tlb_table[3][i]);
1568#endif
1569#endif
bellard3a7d9292005-08-21 09:26:42 +00001570}
1571
ths5fafdf22007-09-16 21:08:06 +00001572static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001573 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001574{
1575 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001576 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1577 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001578 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001579 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001580 }
1581 }
1582}
1583
1584/* update the TLB corresponding to virtual page vaddr and phys addr
1585 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001586static inline void tlb_set_dirty(CPUState *env,
1587 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001588{
bellard1ccde1c2004-02-06 19:46:14 +00001589 int i;
1590
bellard1ccde1c2004-02-06 19:46:14 +00001591 addr &= TARGET_PAGE_MASK;
1592 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001593 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1594 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001595#if (NB_MMU_MODES >= 3)
1596 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1597#if (NB_MMU_MODES == 4)
1598 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1599#endif
1600#endif
bellard9fa3e852004-01-04 18:06:42 +00001601}
1602
bellard59817cc2004-02-16 22:01:13 +00001603/* add a new TLB entry. At most one entry for a given virtual address
1604 is permitted. Return 0 if OK or 2 if the page could not be mapped
1605 (can only happen in non SOFTMMU mode for I/O pages or pages
1606 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001607int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1608 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001609 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001610{
bellard92e873b2004-05-21 14:52:29 +00001611 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001612 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001613 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001614 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001615 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001616 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001617 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001618 int i;
bellard9fa3e852004-01-04 18:06:42 +00001619
bellard92e873b2004-05-21 14:52:29 +00001620 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001621 if (!p) {
1622 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001623 } else {
1624 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001625 }
1626#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001627 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1628 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001629#endif
1630
1631 ret = 0;
1632#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001633 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001634#endif
1635 {
bellard2a4188a2006-06-25 21:54:59 +00001636 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001637 /* IO memory case */
1638 address = vaddr | pd;
1639 addend = paddr;
1640 } else {
1641 /* standard memory */
1642 address = vaddr;
1643 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1644 }
pbrook6658ffb2007-03-16 23:58:11 +00001645
1646 /* Make accesses to pages with watchpoints go via the
1647 watchpoint trap routines. */
1648 for (i = 0; i < env->nb_watchpoints; i++) {
1649 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1650 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001651 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001652 address = vaddr | io_mem_watch;
1653 } else {
balrogd79acba2007-06-26 20:01:13 +00001654 env->watchpoint[i].addend = pd - paddr +
1655 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001656 /* TODO: Figure out how to make read watchpoints coexist
1657 with code. */
1658 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1659 }
1660 }
1661 }
balrogd79acba2007-06-26 20:01:13 +00001662
bellard90f18422005-07-24 10:17:31 +00001663 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001664 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001665 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001666 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001667 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001668 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001669 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001670 te->addr_read = -1;
1671 }
1672 if (prot & PAGE_EXEC) {
1673 te->addr_code = address;
1674 } else {
1675 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001676 }
bellard67b915a2004-03-31 23:37:16 +00001677 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001678 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001679 (pd & IO_MEM_ROMD)) {
1680 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001681 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001682 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001683 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001684 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001685 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001686 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001687 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001688 }
1689 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001690 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001691 }
1692 }
1693#if !defined(CONFIG_SOFTMMU)
1694 else {
1695 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1696 /* IO access: no mapping is done as it will be handled by the
1697 soft MMU */
1698 if (!(env->hflags & HF_SOFTMMU_MASK))
1699 ret = 2;
1700 } else {
1701 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001702
bellard59817cc2004-02-16 22:01:13 +00001703 if (vaddr >= MMAP_AREA_END) {
1704 ret = 2;
1705 } else {
1706 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001707 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001708#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001709 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001710#endif
ths5fafdf22007-09-16 21:08:06 +00001711 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001712 !cpu_physical_memory_is_dirty(pd))) {
1713 /* ROM: we do as if code was inside */
1714 /* if code is present, we only map as read only and save the
1715 original mapping */
1716 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001717
bellard90f18422005-07-24 10:17:31 +00001718 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001719 vp->phys_addr = pd;
1720 vp->prot = prot;
1721 vp->valid_tag = virt_valid_tag;
1722 prot &= ~PAGE_WRITE;
1723 }
bellard9fa3e852004-01-04 18:06:42 +00001724 }
ths5fafdf22007-09-16 21:08:06 +00001725 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001726 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1727 if (map_addr == MAP_FAILED) {
1728 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1729 paddr, vaddr);
1730 }
bellard9fa3e852004-01-04 18:06:42 +00001731 }
1732 }
1733 }
1734#endif
1735 return ret;
1736}
1737
1738/* called from signal handler: invalidate the code and unprotect the
1739 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001740int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001741{
1742#if !defined(CONFIG_SOFTMMU)
1743 VirtPageDesc *vp;
1744
1745#if defined(DEBUG_TLB)
1746 printf("page_unprotect: addr=0x%08x\n", addr);
1747#endif
1748 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001749
1750 /* if it is not mapped, no need to worry here */
1751 if (addr >= MMAP_AREA_END)
1752 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001753 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1754 if (!vp)
1755 return 0;
1756 /* NOTE: in this case, validate_tag is _not_ tested as it
1757 validates only the code TLB */
1758 if (vp->valid_tag != virt_valid_tag)
1759 return 0;
1760 if (!(vp->prot & PAGE_WRITE))
1761 return 0;
1762#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001763 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001764 addr, vp->phys_addr, vp->prot);
1765#endif
bellard59817cc2004-02-16 22:01:13 +00001766 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1767 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1768 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001769 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001770 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001771 /* flush the code inside */
1772 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001773 return 1;
1774#else
1775 return 0;
1776#endif
bellard33417e72003-08-10 21:47:01 +00001777}
1778
bellard01243112004-01-04 15:48:17 +00001779#else
1780
bellardee8b7022004-02-03 23:35:10 +00001781void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001782{
1783}
1784
bellard2e126692004-04-25 21:28:44 +00001785void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001786{
1787}
1788
ths5fafdf22007-09-16 21:08:06 +00001789int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1790 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001791 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001792{
bellard9fa3e852004-01-04 18:06:42 +00001793 return 0;
1794}
bellard33417e72003-08-10 21:47:01 +00001795
bellard9fa3e852004-01-04 18:06:42 +00001796/* dump memory mappings */
1797void page_dump(FILE *f)
1798{
1799 unsigned long start, end;
1800 int i, j, prot, prot1;
1801 PageDesc *p;
1802
1803 fprintf(f, "%-8s %-8s %-8s %s\n",
1804 "start", "end", "size", "prot");
1805 start = -1;
1806 end = -1;
1807 prot = 0;
1808 for(i = 0; i <= L1_SIZE; i++) {
1809 if (i < L1_SIZE)
1810 p = l1_map[i];
1811 else
1812 p = NULL;
1813 for(j = 0;j < L2_SIZE; j++) {
1814 if (!p)
1815 prot1 = 0;
1816 else
1817 prot1 = p[j].flags;
1818 if (prot1 != prot) {
1819 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1820 if (start != -1) {
1821 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001822 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001823 prot & PAGE_READ ? 'r' : '-',
1824 prot & PAGE_WRITE ? 'w' : '-',
1825 prot & PAGE_EXEC ? 'x' : '-');
1826 }
1827 if (prot1 != 0)
1828 start = end;
1829 else
1830 start = -1;
1831 prot = prot1;
1832 }
1833 if (!p)
1834 break;
1835 }
bellard33417e72003-08-10 21:47:01 +00001836 }
bellard33417e72003-08-10 21:47:01 +00001837}
1838
pbrook53a59602006-03-25 19:31:22 +00001839int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001840{
bellard9fa3e852004-01-04 18:06:42 +00001841 PageDesc *p;
1842
1843 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001844 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001845 return 0;
1846 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001847}
1848
bellard9fa3e852004-01-04 18:06:42 +00001849/* modify the flags of a page and invalidate the code if
1850 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1851 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001852void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001853{
1854 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001855 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001856
1857 start = start & TARGET_PAGE_MASK;
1858 end = TARGET_PAGE_ALIGN(end);
1859 if (flags & PAGE_WRITE)
1860 flags |= PAGE_WRITE_ORG;
1861 spin_lock(&tb_lock);
1862 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1863 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1864 /* if the write protection is set, then we invalidate the code
1865 inside */
ths5fafdf22007-09-16 21:08:06 +00001866 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001867 (flags & PAGE_WRITE) &&
1868 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001869 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001870 }
1871 p->flags = flags;
1872 }
1873 spin_unlock(&tb_lock);
1874}
1875
ths3d97b402007-11-02 19:02:07 +00001876int page_check_range(target_ulong start, target_ulong len, int flags)
1877{
1878 PageDesc *p;
1879 target_ulong end;
1880 target_ulong addr;
1881
1882 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
1883 start = start & TARGET_PAGE_MASK;
1884
1885 if( end < start )
1886 /* we've wrapped around */
1887 return -1;
1888 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1889 p = page_find(addr >> TARGET_PAGE_BITS);
1890 if( !p )
1891 return -1;
1892 if( !(p->flags & PAGE_VALID) )
1893 return -1;
1894
bellarddae32702007-11-14 10:51:00 +00001895 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00001896 return -1;
bellarddae32702007-11-14 10:51:00 +00001897 if (flags & PAGE_WRITE) {
1898 if (!(p->flags & PAGE_WRITE_ORG))
1899 return -1;
1900 /* unprotect the page if it was put read-only because it
1901 contains translated code */
1902 if (!(p->flags & PAGE_WRITE)) {
1903 if (!page_unprotect(addr, 0, NULL))
1904 return -1;
1905 }
1906 return 0;
1907 }
ths3d97b402007-11-02 19:02:07 +00001908 }
1909 return 0;
1910}
1911
bellard9fa3e852004-01-04 18:06:42 +00001912/* called from signal handler: invalidate the code and unprotect the
1913 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001914int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001915{
1916 unsigned int page_index, prot, pindex;
1917 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00001918 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001919
bellard83fb7ad2004-07-05 21:25:26 +00001920 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00001921 page_index = host_start >> TARGET_PAGE_BITS;
1922 p1 = page_find(page_index);
1923 if (!p1)
1924 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00001925 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00001926 p = p1;
1927 prot = 0;
1928 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
1929 prot |= p->flags;
1930 p++;
1931 }
1932 /* if the page was really writable, then we change its
1933 protection back to writable */
1934 if (prot & PAGE_WRITE_ORG) {
1935 pindex = (address - host_start) >> TARGET_PAGE_BITS;
1936 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00001937 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00001938 (prot & PAGE_BITS) | PAGE_WRITE);
1939 p1[pindex].flags |= PAGE_WRITE;
1940 /* and since the content will be modified, we must invalidate
1941 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00001942 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001943#ifdef DEBUG_TB_CHECK
1944 tb_invalidate_check(address);
1945#endif
1946 return 1;
1947 }
1948 }
1949 return 0;
1950}
1951
bellard6a00d602005-11-21 23:25:50 +00001952static inline void tlb_set_dirty(CPUState *env,
1953 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001954{
1955}
bellard9fa3e852004-01-04 18:06:42 +00001956#endif /* defined(CONFIG_USER_ONLY) */
1957
blueswir1db7b5422007-05-26 17:36:03 +00001958static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1959 int memory);
1960static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
1961 int orig_memory);
1962#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
1963 need_subpage) \
1964 do { \
1965 if (addr > start_addr) \
1966 start_addr2 = 0; \
1967 else { \
1968 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
1969 if (start_addr2 > 0) \
1970 need_subpage = 1; \
1971 } \
1972 \
blueswir149e9fba2007-05-30 17:25:06 +00001973 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00001974 end_addr2 = TARGET_PAGE_SIZE - 1; \
1975 else { \
1976 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
1977 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
1978 need_subpage = 1; \
1979 } \
1980 } while (0)
1981
bellard33417e72003-08-10 21:47:01 +00001982/* register physical memory. 'size' must be a multiple of the target
1983 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
1984 io memory page */
ths5fafdf22007-09-16 21:08:06 +00001985void cpu_register_physical_memory(target_phys_addr_t start_addr,
bellard2e126692004-04-25 21:28:44 +00001986 unsigned long size,
1987 unsigned long phys_offset)
bellard33417e72003-08-10 21:47:01 +00001988{
bellard108c49b2005-07-24 12:55:09 +00001989 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00001990 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00001991 CPUState *env;
blueswir1db7b5422007-05-26 17:36:03 +00001992 unsigned long orig_size = size;
1993 void *subpage;
bellard33417e72003-08-10 21:47:01 +00001994
bellard5fd386f2004-05-23 21:11:22 +00001995 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00001996 end_addr = start_addr + (target_phys_addr_t)size;
1997 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00001998 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1999 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2000 unsigned long orig_memory = p->phys_offset;
2001 target_phys_addr_t start_addr2, end_addr2;
2002 int need_subpage = 0;
2003
2004 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2005 need_subpage);
2006 if (need_subpage) {
2007 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2008 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2009 &p->phys_offset, orig_memory);
2010 } else {
2011 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2012 >> IO_MEM_SHIFT];
2013 }
2014 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2015 } else {
2016 p->phys_offset = phys_offset;
2017 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2018 (phys_offset & IO_MEM_ROMD))
2019 phys_offset += TARGET_PAGE_SIZE;
2020 }
2021 } else {
2022 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2023 p->phys_offset = phys_offset;
2024 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2025 (phys_offset & IO_MEM_ROMD))
2026 phys_offset += TARGET_PAGE_SIZE;
2027 else {
2028 target_phys_addr_t start_addr2, end_addr2;
2029 int need_subpage = 0;
2030
2031 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2032 end_addr2, need_subpage);
2033
2034 if (need_subpage) {
2035 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2036 &p->phys_offset, IO_MEM_UNASSIGNED);
2037 subpage_register(subpage, start_addr2, end_addr2,
2038 phys_offset);
2039 }
2040 }
2041 }
bellard33417e72003-08-10 21:47:01 +00002042 }
ths3b46e622007-09-17 08:09:54 +00002043
bellard9d420372006-06-25 22:25:22 +00002044 /* since each CPU stores ram addresses in its TLB cache, we must
2045 reset the modified entries */
2046 /* XXX: slow ! */
2047 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2048 tlb_flush(env, 1);
2049 }
bellard33417e72003-08-10 21:47:01 +00002050}
2051
bellardba863452006-09-24 18:41:10 +00002052/* XXX: temporary until new memory mapping API */
2053uint32_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2054{
2055 PhysPageDesc *p;
2056
2057 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2058 if (!p)
2059 return IO_MEM_UNASSIGNED;
2060 return p->phys_offset;
2061}
2062
bellarde9a1ab12007-02-08 23:08:38 +00002063/* XXX: better than nothing */
2064ram_addr_t qemu_ram_alloc(unsigned int size)
2065{
2066 ram_addr_t addr;
2067 if ((phys_ram_alloc_offset + size) >= phys_ram_size) {
ths5fafdf22007-09-16 21:08:06 +00002068 fprintf(stderr, "Not enough memory (requested_size = %u, max memory = %d)\n",
bellarde9a1ab12007-02-08 23:08:38 +00002069 size, phys_ram_size);
2070 abort();
2071 }
2072 addr = phys_ram_alloc_offset;
2073 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2074 return addr;
2075}
2076
2077void qemu_ram_free(ram_addr_t addr)
2078{
2079}
2080
bellarda4193c82004-06-03 14:01:43 +00002081static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002082{
pbrook67d3b952006-12-18 05:03:52 +00002083#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002084 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002085#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002086#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002087 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002088#elif TARGET_CRIS
2089 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002090#endif
bellard33417e72003-08-10 21:47:01 +00002091 return 0;
2092}
2093
bellarda4193c82004-06-03 14:01:43 +00002094static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002095{
pbrook67d3b952006-12-18 05:03:52 +00002096#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002097 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002098#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002099#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002100 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002101#elif TARGET_CRIS
2102 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002103#endif
bellard33417e72003-08-10 21:47:01 +00002104}
2105
2106static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2107 unassigned_mem_readb,
2108 unassigned_mem_readb,
2109 unassigned_mem_readb,
2110};
2111
2112static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2113 unassigned_mem_writeb,
2114 unassigned_mem_writeb,
2115 unassigned_mem_writeb,
2116};
2117
bellarda4193c82004-06-03 14:01:43 +00002118static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002119{
bellard3a7d9292005-08-21 09:26:42 +00002120 unsigned long ram_addr;
2121 int dirty_flags;
2122 ram_addr = addr - (unsigned long)phys_ram_base;
2123 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2124 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2125#if !defined(CONFIG_USER_ONLY)
2126 tb_invalidate_phys_page_fast(ram_addr, 1);
2127 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2128#endif
2129 }
bellardc27004e2005-01-03 23:35:10 +00002130 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002131#ifdef USE_KQEMU
2132 if (cpu_single_env->kqemu_enabled &&
2133 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2134 kqemu_modify_page(cpu_single_env, ram_addr);
2135#endif
bellardf23db162005-08-21 19:12:28 +00002136 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2137 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2138 /* we remove the notdirty callback only if the code has been
2139 flushed */
2140 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002141 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002142}
2143
bellarda4193c82004-06-03 14:01:43 +00002144static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002145{
bellard3a7d9292005-08-21 09:26:42 +00002146 unsigned long ram_addr;
2147 int dirty_flags;
2148 ram_addr = addr - (unsigned long)phys_ram_base;
2149 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2150 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2151#if !defined(CONFIG_USER_ONLY)
2152 tb_invalidate_phys_page_fast(ram_addr, 2);
2153 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2154#endif
2155 }
bellardc27004e2005-01-03 23:35:10 +00002156 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002157#ifdef USE_KQEMU
2158 if (cpu_single_env->kqemu_enabled &&
2159 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2160 kqemu_modify_page(cpu_single_env, ram_addr);
2161#endif
bellardf23db162005-08-21 19:12:28 +00002162 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2163 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2164 /* we remove the notdirty callback only if the code has been
2165 flushed */
2166 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002167 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002168}
2169
bellarda4193c82004-06-03 14:01:43 +00002170static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002171{
bellard3a7d9292005-08-21 09:26:42 +00002172 unsigned long ram_addr;
2173 int dirty_flags;
2174 ram_addr = addr - (unsigned long)phys_ram_base;
2175 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2176 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2177#if !defined(CONFIG_USER_ONLY)
2178 tb_invalidate_phys_page_fast(ram_addr, 4);
2179 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2180#endif
2181 }
bellardc27004e2005-01-03 23:35:10 +00002182 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002183#ifdef USE_KQEMU
2184 if (cpu_single_env->kqemu_enabled &&
2185 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2186 kqemu_modify_page(cpu_single_env, ram_addr);
2187#endif
bellardf23db162005-08-21 19:12:28 +00002188 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2189 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2190 /* we remove the notdirty callback only if the code has been
2191 flushed */
2192 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002193 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002194}
2195
bellard3a7d9292005-08-21 09:26:42 +00002196static CPUReadMemoryFunc *error_mem_read[3] = {
2197 NULL, /* never used */
2198 NULL, /* never used */
2199 NULL, /* never used */
2200};
2201
bellard1ccde1c2004-02-06 19:46:14 +00002202static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2203 notdirty_mem_writeb,
2204 notdirty_mem_writew,
2205 notdirty_mem_writel,
2206};
2207
pbrook6658ffb2007-03-16 23:58:11 +00002208#if defined(CONFIG_SOFTMMU)
2209/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2210 so these check for a hit then pass through to the normal out-of-line
2211 phys routines. */
2212static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2213{
2214 return ldub_phys(addr);
2215}
2216
2217static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2218{
2219 return lduw_phys(addr);
2220}
2221
2222static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2223{
2224 return ldl_phys(addr);
2225}
2226
2227/* Generate a debug exception if a watchpoint has been hit.
2228 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002229 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002230static target_ulong check_watchpoint(target_phys_addr_t addr)
2231{
2232 CPUState *env = cpu_single_env;
2233 target_ulong watch;
2234 target_ulong retaddr;
2235 int i;
2236
2237 retaddr = addr;
2238 for (i = 0; i < env->nb_watchpoints; i++) {
2239 watch = env->watchpoint[i].vaddr;
2240 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002241 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002242 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2243 cpu_single_env->watchpoint_hit = i + 1;
2244 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2245 break;
2246 }
2247 }
2248 }
2249 return retaddr;
2250}
2251
2252static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2253 uint32_t val)
2254{
2255 addr = check_watchpoint(addr);
2256 stb_phys(addr, val);
2257}
2258
2259static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2260 uint32_t val)
2261{
2262 addr = check_watchpoint(addr);
2263 stw_phys(addr, val);
2264}
2265
2266static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2267 uint32_t val)
2268{
2269 addr = check_watchpoint(addr);
2270 stl_phys(addr, val);
2271}
2272
2273static CPUReadMemoryFunc *watch_mem_read[3] = {
2274 watch_mem_readb,
2275 watch_mem_readw,
2276 watch_mem_readl,
2277};
2278
2279static CPUWriteMemoryFunc *watch_mem_write[3] = {
2280 watch_mem_writeb,
2281 watch_mem_writew,
2282 watch_mem_writel,
2283};
2284#endif
2285
blueswir1db7b5422007-05-26 17:36:03 +00002286static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2287 unsigned int len)
2288{
2289 CPUReadMemoryFunc **mem_read;
2290 uint32_t ret;
2291 unsigned int idx;
2292
2293 idx = SUBPAGE_IDX(addr - mmio->base);
2294#if defined(DEBUG_SUBPAGE)
2295 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2296 mmio, len, addr, idx);
2297#endif
2298 mem_read = mmio->mem_read[idx];
2299 ret = (*mem_read[len])(mmio->opaque[idx], addr);
2300
2301 return ret;
2302}
2303
2304static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2305 uint32_t value, unsigned int len)
2306{
2307 CPUWriteMemoryFunc **mem_write;
2308 unsigned int idx;
2309
2310 idx = SUBPAGE_IDX(addr - mmio->base);
2311#if defined(DEBUG_SUBPAGE)
2312 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2313 mmio, len, addr, idx, value);
2314#endif
2315 mem_write = mmio->mem_write[idx];
2316 (*mem_write[len])(mmio->opaque[idx], addr, value);
2317}
2318
2319static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2320{
2321#if defined(DEBUG_SUBPAGE)
2322 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2323#endif
2324
2325 return subpage_readlen(opaque, addr, 0);
2326}
2327
2328static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2329 uint32_t value)
2330{
2331#if defined(DEBUG_SUBPAGE)
2332 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2333#endif
2334 subpage_writelen(opaque, addr, value, 0);
2335}
2336
2337static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2338{
2339#if defined(DEBUG_SUBPAGE)
2340 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2341#endif
2342
2343 return subpage_readlen(opaque, addr, 1);
2344}
2345
2346static void subpage_writew (void *opaque, target_phys_addr_t addr,
2347 uint32_t value)
2348{
2349#if defined(DEBUG_SUBPAGE)
2350 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2351#endif
2352 subpage_writelen(opaque, addr, value, 1);
2353}
2354
2355static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2356{
2357#if defined(DEBUG_SUBPAGE)
2358 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2359#endif
2360
2361 return subpage_readlen(opaque, addr, 2);
2362}
2363
2364static void subpage_writel (void *opaque,
2365 target_phys_addr_t addr, uint32_t value)
2366{
2367#if defined(DEBUG_SUBPAGE)
2368 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2369#endif
2370 subpage_writelen(opaque, addr, value, 2);
2371}
2372
2373static CPUReadMemoryFunc *subpage_read[] = {
2374 &subpage_readb,
2375 &subpage_readw,
2376 &subpage_readl,
2377};
2378
2379static CPUWriteMemoryFunc *subpage_write[] = {
2380 &subpage_writeb,
2381 &subpage_writew,
2382 &subpage_writel,
2383};
2384
2385static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2386 int memory)
2387{
2388 int idx, eidx;
2389
2390 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2391 return -1;
2392 idx = SUBPAGE_IDX(start);
2393 eidx = SUBPAGE_IDX(end);
2394#if defined(DEBUG_SUBPAGE)
2395 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2396 mmio, start, end, idx, eidx, memory);
2397#endif
2398 memory >>= IO_MEM_SHIFT;
2399 for (; idx <= eidx; idx++) {
2400 mmio->mem_read[idx] = io_mem_read[memory];
2401 mmio->mem_write[idx] = io_mem_write[memory];
2402 mmio->opaque[idx] = io_mem_opaque[memory];
2403 }
2404
2405 return 0;
2406}
2407
2408static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
2409 int orig_memory)
2410{
2411 subpage_t *mmio;
2412 int subpage_memory;
2413
2414 mmio = qemu_mallocz(sizeof(subpage_t));
2415 if (mmio != NULL) {
2416 mmio->base = base;
2417 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2418#if defined(DEBUG_SUBPAGE)
2419 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2420 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2421#endif
2422 *phys = subpage_memory | IO_MEM_SUBPAGE;
2423 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2424 }
2425
2426 return mmio;
2427}
2428
bellard33417e72003-08-10 21:47:01 +00002429static void io_mem_init(void)
2430{
bellard3a7d9292005-08-21 09:26:42 +00002431 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002432 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002433 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002434 io_mem_nb = 5;
2435
pbrook6658ffb2007-03-16 23:58:11 +00002436#if defined(CONFIG_SOFTMMU)
2437 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2438 watch_mem_write, NULL);
2439#endif
bellard1ccde1c2004-02-06 19:46:14 +00002440 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002441 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002442 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002443}
2444
2445/* mem_read and mem_write are arrays of functions containing the
2446 function to access byte (index 0), word (index 1) and dword (index
2447 2). All functions must be supplied. If io_index is non zero, the
2448 corresponding io zone is modified. If it is zero, a new io zone is
2449 allocated. The return value can be used with
2450 cpu_register_physical_memory(). (-1) is returned if error. */
2451int cpu_register_io_memory(int io_index,
2452 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002453 CPUWriteMemoryFunc **mem_write,
2454 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002455{
2456 int i;
2457
2458 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002459 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002460 return -1;
2461 io_index = io_mem_nb++;
2462 } else {
2463 if (io_index >= IO_MEM_NB_ENTRIES)
2464 return -1;
2465 }
bellardb5ff1b32005-11-26 10:38:39 +00002466
bellard33417e72003-08-10 21:47:01 +00002467 for(i = 0;i < 3; i++) {
2468 io_mem_read[io_index][i] = mem_read[i];
2469 io_mem_write[io_index][i] = mem_write[i];
2470 }
bellarda4193c82004-06-03 14:01:43 +00002471 io_mem_opaque[io_index] = opaque;
bellard33417e72003-08-10 21:47:01 +00002472 return io_index << IO_MEM_SHIFT;
2473}
bellard61382a52003-10-27 21:22:23 +00002474
bellard8926b512004-10-10 15:14:20 +00002475CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2476{
2477 return io_mem_write[io_index >> IO_MEM_SHIFT];
2478}
2479
2480CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2481{
2482 return io_mem_read[io_index >> IO_MEM_SHIFT];
2483}
2484
bellard13eb76e2004-01-24 15:23:36 +00002485/* physical memory access (slow version, mainly for debug) */
2486#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002487void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002488 int len, int is_write)
2489{
2490 int l, flags;
2491 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002492 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002493
2494 while (len > 0) {
2495 page = addr & TARGET_PAGE_MASK;
2496 l = (page + TARGET_PAGE_SIZE) - addr;
2497 if (l > len)
2498 l = len;
2499 flags = page_get_flags(page);
2500 if (!(flags & PAGE_VALID))
2501 return;
2502 if (is_write) {
2503 if (!(flags & PAGE_WRITE))
2504 return;
bellard579a97f2007-11-11 14:26:47 +00002505 /* XXX: this code should not depend on lock_user */
2506 if (!(p = lock_user(VERIFY_WRITE, addr, len, 0)))
2507 /* FIXME - should this return an error rather than just fail? */
2508 return;
pbrook53a59602006-03-25 19:31:22 +00002509 memcpy(p, buf, len);
2510 unlock_user(p, addr, len);
bellard13eb76e2004-01-24 15:23:36 +00002511 } else {
2512 if (!(flags & PAGE_READ))
2513 return;
bellard579a97f2007-11-11 14:26:47 +00002514 /* XXX: this code should not depend on lock_user */
2515 if (!(p = lock_user(VERIFY_READ, addr, len, 1)))
2516 /* FIXME - should this return an error rather than just fail? */
2517 return;
pbrook53a59602006-03-25 19:31:22 +00002518 memcpy(buf, p, len);
2519 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002520 }
2521 len -= l;
2522 buf += l;
2523 addr += l;
2524 }
2525}
bellard8df1cd02005-01-28 22:37:22 +00002526
bellard13eb76e2004-01-24 15:23:36 +00002527#else
ths5fafdf22007-09-16 21:08:06 +00002528void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002529 int len, int is_write)
2530{
2531 int l, io_index;
2532 uint8_t *ptr;
2533 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002534 target_phys_addr_t page;
2535 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002536 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002537
bellard13eb76e2004-01-24 15:23:36 +00002538 while (len > 0) {
2539 page = addr & TARGET_PAGE_MASK;
2540 l = (page + TARGET_PAGE_SIZE) - addr;
2541 if (l > len)
2542 l = len;
bellard92e873b2004-05-21 14:52:29 +00002543 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002544 if (!p) {
2545 pd = IO_MEM_UNASSIGNED;
2546 } else {
2547 pd = p->phys_offset;
2548 }
ths3b46e622007-09-17 08:09:54 +00002549
bellard13eb76e2004-01-24 15:23:36 +00002550 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002551 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002552 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002553 /* XXX: could force cpu_single_env to NULL to avoid
2554 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002555 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002556 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002557 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002558 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002559 l = 4;
2560 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002561 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002562 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002563 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002564 l = 2;
2565 } else {
bellard1c213d12005-09-03 10:49:04 +00002566 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002567 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002568 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002569 l = 1;
2570 }
2571 } else {
bellardb448f2f2004-02-25 23:24:04 +00002572 unsigned long addr1;
2573 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002574 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002575 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002576 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002577 if (!cpu_physical_memory_is_dirty(addr1)) {
2578 /* invalidate code */
2579 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2580 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002581 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002582 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002583 }
bellard13eb76e2004-01-24 15:23:36 +00002584 }
2585 } else {
ths5fafdf22007-09-16 21:08:06 +00002586 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002587 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002588 /* I/O case */
2589 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2590 if (l >= 4 && ((addr & 3) == 0)) {
2591 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002592 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002593 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002594 l = 4;
2595 } else if (l >= 2 && ((addr & 1) == 0)) {
2596 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002597 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002598 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002599 l = 2;
2600 } else {
bellard1c213d12005-09-03 10:49:04 +00002601 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002602 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002603 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002604 l = 1;
2605 }
2606 } else {
2607 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002608 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002609 (addr & ~TARGET_PAGE_MASK);
2610 memcpy(buf, ptr, l);
2611 }
2612 }
2613 len -= l;
2614 buf += l;
2615 addr += l;
2616 }
2617}
bellard8df1cd02005-01-28 22:37:22 +00002618
bellardd0ecd2a2006-04-23 17:14:48 +00002619/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002620void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002621 const uint8_t *buf, int len)
2622{
2623 int l;
2624 uint8_t *ptr;
2625 target_phys_addr_t page;
2626 unsigned long pd;
2627 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002628
bellardd0ecd2a2006-04-23 17:14:48 +00002629 while (len > 0) {
2630 page = addr & TARGET_PAGE_MASK;
2631 l = (page + TARGET_PAGE_SIZE) - addr;
2632 if (l > len)
2633 l = len;
2634 p = phys_page_find(page >> TARGET_PAGE_BITS);
2635 if (!p) {
2636 pd = IO_MEM_UNASSIGNED;
2637 } else {
2638 pd = p->phys_offset;
2639 }
ths3b46e622007-09-17 08:09:54 +00002640
bellardd0ecd2a2006-04-23 17:14:48 +00002641 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002642 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2643 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002644 /* do nothing */
2645 } else {
2646 unsigned long addr1;
2647 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2648 /* ROM/RAM case */
2649 ptr = phys_ram_base + addr1;
2650 memcpy(ptr, buf, l);
2651 }
2652 len -= l;
2653 buf += l;
2654 addr += l;
2655 }
2656}
2657
2658
bellard8df1cd02005-01-28 22:37:22 +00002659/* warning: addr must be aligned */
2660uint32_t ldl_phys(target_phys_addr_t addr)
2661{
2662 int io_index;
2663 uint8_t *ptr;
2664 uint32_t val;
2665 unsigned long pd;
2666 PhysPageDesc *p;
2667
2668 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2669 if (!p) {
2670 pd = IO_MEM_UNASSIGNED;
2671 } else {
2672 pd = p->phys_offset;
2673 }
ths3b46e622007-09-17 08:09:54 +00002674
ths5fafdf22007-09-16 21:08:06 +00002675 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002676 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002677 /* I/O case */
2678 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2679 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2680 } else {
2681 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002682 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002683 (addr & ~TARGET_PAGE_MASK);
2684 val = ldl_p(ptr);
2685 }
2686 return val;
2687}
2688
bellard84b7b8e2005-11-28 21:19:04 +00002689/* warning: addr must be aligned */
2690uint64_t ldq_phys(target_phys_addr_t addr)
2691{
2692 int io_index;
2693 uint8_t *ptr;
2694 uint64_t val;
2695 unsigned long pd;
2696 PhysPageDesc *p;
2697
2698 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2699 if (!p) {
2700 pd = IO_MEM_UNASSIGNED;
2701 } else {
2702 pd = p->phys_offset;
2703 }
ths3b46e622007-09-17 08:09:54 +00002704
bellard2a4188a2006-06-25 21:54:59 +00002705 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2706 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002707 /* I/O case */
2708 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2709#ifdef TARGET_WORDS_BIGENDIAN
2710 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2711 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2712#else
2713 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2714 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2715#endif
2716 } else {
2717 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002718 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002719 (addr & ~TARGET_PAGE_MASK);
2720 val = ldq_p(ptr);
2721 }
2722 return val;
2723}
2724
bellardaab33092005-10-30 20:48:42 +00002725/* XXX: optimize */
2726uint32_t ldub_phys(target_phys_addr_t addr)
2727{
2728 uint8_t val;
2729 cpu_physical_memory_read(addr, &val, 1);
2730 return val;
2731}
2732
2733/* XXX: optimize */
2734uint32_t lduw_phys(target_phys_addr_t addr)
2735{
2736 uint16_t val;
2737 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2738 return tswap16(val);
2739}
2740
bellard8df1cd02005-01-28 22:37:22 +00002741/* warning: addr must be aligned. The ram page is not masked as dirty
2742 and the code inside is not invalidated. It is useful if the dirty
2743 bits are used to track modified PTEs */
2744void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2745{
2746 int io_index;
2747 uint8_t *ptr;
2748 unsigned long pd;
2749 PhysPageDesc *p;
2750
2751 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2752 if (!p) {
2753 pd = IO_MEM_UNASSIGNED;
2754 } else {
2755 pd = p->phys_offset;
2756 }
ths3b46e622007-09-17 08:09:54 +00002757
bellard3a7d9292005-08-21 09:26:42 +00002758 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002759 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2760 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2761 } else {
ths5fafdf22007-09-16 21:08:06 +00002762 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002763 (addr & ~TARGET_PAGE_MASK);
2764 stl_p(ptr, val);
2765 }
2766}
2767
j_mayerbc98a7e2007-04-04 07:55:12 +00002768void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2769{
2770 int io_index;
2771 uint8_t *ptr;
2772 unsigned long pd;
2773 PhysPageDesc *p;
2774
2775 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2776 if (!p) {
2777 pd = IO_MEM_UNASSIGNED;
2778 } else {
2779 pd = p->phys_offset;
2780 }
ths3b46e622007-09-17 08:09:54 +00002781
j_mayerbc98a7e2007-04-04 07:55:12 +00002782 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2783 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2784#ifdef TARGET_WORDS_BIGENDIAN
2785 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2786 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2787#else
2788 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2789 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2790#endif
2791 } else {
ths5fafdf22007-09-16 21:08:06 +00002792 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002793 (addr & ~TARGET_PAGE_MASK);
2794 stq_p(ptr, val);
2795 }
2796}
2797
bellard8df1cd02005-01-28 22:37:22 +00002798/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002799void stl_phys(target_phys_addr_t addr, uint32_t val)
2800{
2801 int io_index;
2802 uint8_t *ptr;
2803 unsigned long pd;
2804 PhysPageDesc *p;
2805
2806 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2807 if (!p) {
2808 pd = IO_MEM_UNASSIGNED;
2809 } else {
2810 pd = p->phys_offset;
2811 }
ths3b46e622007-09-17 08:09:54 +00002812
bellard3a7d9292005-08-21 09:26:42 +00002813 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002814 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2815 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2816 } else {
2817 unsigned long addr1;
2818 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2819 /* RAM case */
2820 ptr = phys_ram_base + addr1;
2821 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002822 if (!cpu_physical_memory_is_dirty(addr1)) {
2823 /* invalidate code */
2824 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2825 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002826 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2827 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002828 }
bellard8df1cd02005-01-28 22:37:22 +00002829 }
2830}
2831
bellardaab33092005-10-30 20:48:42 +00002832/* XXX: optimize */
2833void stb_phys(target_phys_addr_t addr, uint32_t val)
2834{
2835 uint8_t v = val;
2836 cpu_physical_memory_write(addr, &v, 1);
2837}
2838
2839/* XXX: optimize */
2840void stw_phys(target_phys_addr_t addr, uint32_t val)
2841{
2842 uint16_t v = tswap16(val);
2843 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2844}
2845
2846/* XXX: optimize */
2847void stq_phys(target_phys_addr_t addr, uint64_t val)
2848{
2849 val = tswap64(val);
2850 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2851}
2852
bellard13eb76e2004-01-24 15:23:36 +00002853#endif
2854
2855/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002856int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002857 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002858{
2859 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002860 target_phys_addr_t phys_addr;
2861 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002862
2863 while (len > 0) {
2864 page = addr & TARGET_PAGE_MASK;
2865 phys_addr = cpu_get_phys_page_debug(env, page);
2866 /* if no physical page mapped, return an error */
2867 if (phys_addr == -1)
2868 return -1;
2869 l = (page + TARGET_PAGE_SIZE) - addr;
2870 if (l > len)
2871 l = len;
ths5fafdf22007-09-16 21:08:06 +00002872 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00002873 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002874 len -= l;
2875 buf += l;
2876 addr += l;
2877 }
2878 return 0;
2879}
2880
bellarde3db7222005-01-26 22:00:47 +00002881void dump_exec_info(FILE *f,
2882 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
2883{
2884 int i, target_code_size, max_target_code_size;
2885 int direct_jmp_count, direct_jmp2_count, cross_page;
2886 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00002887
bellarde3db7222005-01-26 22:00:47 +00002888 target_code_size = 0;
2889 max_target_code_size = 0;
2890 cross_page = 0;
2891 direct_jmp_count = 0;
2892 direct_jmp2_count = 0;
2893 for(i = 0; i < nb_tbs; i++) {
2894 tb = &tbs[i];
2895 target_code_size += tb->size;
2896 if (tb->size > max_target_code_size)
2897 max_target_code_size = tb->size;
2898 if (tb->page_addr[1] != -1)
2899 cross_page++;
2900 if (tb->tb_next_offset[0] != 0xffff) {
2901 direct_jmp_count++;
2902 if (tb->tb_next_offset[1] != 0xffff) {
2903 direct_jmp2_count++;
2904 }
2905 }
2906 }
2907 /* XXX: avoid using doubles ? */
2908 cpu_fprintf(f, "TB count %d\n", nb_tbs);
ths5fafdf22007-09-16 21:08:06 +00002909 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00002910 nb_tbs ? target_code_size / nb_tbs : 0,
2911 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00002912 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00002913 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
2914 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00002915 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
2916 cross_page,
bellarde3db7222005-01-26 22:00:47 +00002917 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
2918 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00002919 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00002920 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
2921 direct_jmp2_count,
2922 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
2923 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
2924 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
2925 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
2926}
2927
ths5fafdf22007-09-16 21:08:06 +00002928#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00002929
2930#define MMUSUFFIX _cmmu
2931#define GETPC() NULL
2932#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00002933#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00002934
2935#define SHIFT 0
2936#include "softmmu_template.h"
2937
2938#define SHIFT 1
2939#include "softmmu_template.h"
2940
2941#define SHIFT 2
2942#include "softmmu_template.h"
2943
2944#define SHIFT 3
2945#include "softmmu_template.h"
2946
2947#undef env
2948
2949#endif