blob: 877de89f1d692668169968b368310ed1e8c7d0e1 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
bellardfd6ce8f2003-05-14 19:00:11 +00002 * virtual page mapping and translated block handling
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
bellard67b915a2004-03-31 23:37:16 +000020#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000021#ifdef _WIN32
ths4fddf622007-12-17 04:42:29 +000022#define WIN32_LEAN_AND_MEAN
bellardd5a8f072004-09-29 21:15:28 +000023#include <windows.h>
24#else
bellarda98d49b2004-11-14 16:22:05 +000025#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000026#include <sys/mman.h>
27#endif
bellard54936002003-05-13 00:25:15 +000028#include <stdlib.h>
29#include <stdio.h>
30#include <stdarg.h>
31#include <string.h>
32#include <errno.h>
33#include <unistd.h>
34#include <inttypes.h>
35
bellard6180a182003-09-30 21:04:53 +000036#include "cpu.h"
37#include "exec-all.h"
aurel32ca10f862008-04-11 21:35:42 +000038#include "qemu-common.h"
pbrook53a59602006-03-25 19:31:22 +000039#if defined(CONFIG_USER_ONLY)
40#include <qemu.h>
41#endif
bellard54936002003-05-13 00:25:15 +000042
bellardfd6ce8f2003-05-14 19:00:11 +000043//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000044//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000045//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000046//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000047
48/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000049//#define DEBUG_TB_CHECK
50//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000051
ths1196be32007-03-17 15:17:58 +000052//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000053//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000054
pbrook99773bd2006-04-16 15:14:59 +000055#if !defined(CONFIG_USER_ONLY)
56/* TB consistency checks only implemented for usermode emulation. */
57#undef DEBUG_TB_CHECK
58#endif
59
bellardfd6ce8f2003-05-14 19:00:11 +000060/* threshold to flush the translated code buffer */
blueswir1d07bde82007-12-11 19:35:45 +000061#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - code_gen_max_block_size())
bellardfd6ce8f2003-05-14 19:00:11 +000062
bellard9fa3e852004-01-04 18:06:42 +000063#define SMC_BITMAP_USE_THRESHOLD 10
64
65#define MMAP_AREA_START 0x00000000
66#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000067
bellard108c49b2005-07-24 12:55:09 +000068#if defined(TARGET_SPARC64)
69#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000070#elif defined(TARGET_SPARC)
71#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000072#elif defined(TARGET_ALPHA)
73#define TARGET_PHYS_ADDR_SPACE_BITS 42
74#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000075#elif defined(TARGET_PPC64)
76#define TARGET_PHYS_ADDR_SPACE_BITS 42
77#else
78/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
79#define TARGET_PHYS_ADDR_SPACE_BITS 32
80#endif
81
bellardfd6ce8f2003-05-14 19:00:11 +000082TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
bellard9fa3e852004-01-04 18:06:42 +000083TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000084int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000085/* any access to the tbs or the page table must use this lock */
86spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000087
bellardb8076a72005-04-07 22:20:31 +000088uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
bellardfd6ce8f2003-05-14 19:00:11 +000089uint8_t *code_gen_ptr;
90
bellard9fa3e852004-01-04 18:06:42 +000091int phys_ram_size;
92int phys_ram_fd;
93uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +000094uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +000095static ram_addr_t phys_ram_alloc_offset = 0;
bellard9fa3e852004-01-04 18:06:42 +000096
bellard6a00d602005-11-21 23:25:50 +000097CPUState *first_cpu;
98/* current CPU in the current thread. It is only valid inside
99 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000100CPUState *cpu_single_env;
bellard6a00d602005-11-21 23:25:50 +0000101
bellard54936002003-05-13 00:25:15 +0000102typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000103 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000104 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000105 /* in order to optimize self modifying code, we count the number
106 of lookups we do to a given page to use a bitmap */
107 unsigned int code_write_count;
108 uint8_t *code_bitmap;
109#if defined(CONFIG_USER_ONLY)
110 unsigned long flags;
111#endif
bellard54936002003-05-13 00:25:15 +0000112} PageDesc;
113
bellard92e873b2004-05-21 14:52:29 +0000114typedef struct PhysPageDesc {
115 /* offset in host memory of the page + io_index in the low 12 bits */
bellarde04f40b2005-04-24 18:02:38 +0000116 uint32_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000117} PhysPageDesc;
118
bellard54936002003-05-13 00:25:15 +0000119#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000120#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
121/* XXX: this is a temporary hack for alpha target.
122 * In the future, this is to be replaced by a multi-level table
123 * to actually be able to handle the complete 64 bits address space.
124 */
125#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
126#else
bellard54936002003-05-13 00:25:15 +0000127#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000128#endif
bellard54936002003-05-13 00:25:15 +0000129
130#define L1_SIZE (1 << L1_BITS)
131#define L2_SIZE (1 << L2_BITS)
132
bellard33417e72003-08-10 21:47:01 +0000133static void io_mem_init(void);
bellardfd6ce8f2003-05-14 19:00:11 +0000134
bellard83fb7ad2004-07-05 21:25:26 +0000135unsigned long qemu_real_host_page_size;
136unsigned long qemu_host_page_bits;
137unsigned long qemu_host_page_size;
138unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000139
bellard92e873b2004-05-21 14:52:29 +0000140/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000141static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000142PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000143
bellard33417e72003-08-10 21:47:01 +0000144/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000145CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
146CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000147void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000148static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000149#if defined(CONFIG_SOFTMMU)
150static int io_mem_watch;
151#endif
bellard33417e72003-08-10 21:47:01 +0000152
bellard34865132003-10-05 14:28:56 +0000153/* log support */
154char *logfilename = "/tmp/qemu.log";
155FILE *logfile;
156int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000157static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000158
bellarde3db7222005-01-26 22:00:47 +0000159/* statistics */
160static int tlb_flush_count;
161static int tb_flush_count;
162static int tb_phys_invalidate_count;
163
blueswir1db7b5422007-05-26 17:36:03 +0000164#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
165typedef struct subpage_t {
166 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000167 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
168 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
169 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000170} subpage_t;
171
bellardb346ff42003-06-15 20:05:50 +0000172static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000173{
bellard83fb7ad2004-07-05 21:25:26 +0000174 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000175 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000176#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000177 {
178 SYSTEM_INFO system_info;
179 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000180
bellardd5a8f072004-09-29 21:15:28 +0000181 GetSystemInfo(&system_info);
182 qemu_real_host_page_size = system_info.dwPageSize;
ths3b46e622007-09-17 08:09:54 +0000183
bellardd5a8f072004-09-29 21:15:28 +0000184 VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
185 PAGE_EXECUTE_READWRITE, &old_protect);
186 }
bellard67b915a2004-03-31 23:37:16 +0000187#else
bellard83fb7ad2004-07-05 21:25:26 +0000188 qemu_real_host_page_size = getpagesize();
bellardd5a8f072004-09-29 21:15:28 +0000189 {
190 unsigned long start, end;
191
192 start = (unsigned long)code_gen_buffer;
193 start &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000194
bellardd5a8f072004-09-29 21:15:28 +0000195 end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
196 end += qemu_real_host_page_size - 1;
197 end &= ~(qemu_real_host_page_size - 1);
ths3b46e622007-09-17 08:09:54 +0000198
ths5fafdf22007-09-16 21:08:06 +0000199 mprotect((void *)start, end - start,
bellardd5a8f072004-09-29 21:15:28 +0000200 PROT_READ | PROT_WRITE | PROT_EXEC);
201 }
bellard67b915a2004-03-31 23:37:16 +0000202#endif
bellardd5a8f072004-09-29 21:15:28 +0000203
bellard83fb7ad2004-07-05 21:25:26 +0000204 if (qemu_host_page_size == 0)
205 qemu_host_page_size = qemu_real_host_page_size;
206 if (qemu_host_page_size < TARGET_PAGE_SIZE)
207 qemu_host_page_size = TARGET_PAGE_SIZE;
208 qemu_host_page_bits = 0;
209 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
210 qemu_host_page_bits++;
211 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000212 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
213 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000214
215#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
216 {
217 long long startaddr, endaddr;
218 FILE *f;
219 int n;
220
221 f = fopen("/proc/self/maps", "r");
222 if (f) {
223 do {
224 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
225 if (n == 2) {
226 page_set_flags(TARGET_PAGE_ALIGN(startaddr),
227 TARGET_PAGE_ALIGN(endaddr),
228 PAGE_RESERVED);
229 }
230 } while (!feof(f));
231 fclose(f);
232 }
233 }
234#endif
bellard54936002003-05-13 00:25:15 +0000235}
236
bellardfd6ce8f2003-05-14 19:00:11 +0000237static inline PageDesc *page_find_alloc(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000238{
bellard54936002003-05-13 00:25:15 +0000239 PageDesc **lp, *p;
240
bellard54936002003-05-13 00:25:15 +0000241 lp = &l1_map[index >> L2_BITS];
242 p = *lp;
243 if (!p) {
244 /* allocate if not found */
bellard59817cc2004-02-16 22:01:13 +0000245 p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
bellardfd6ce8f2003-05-14 19:00:11 +0000246 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
bellard54936002003-05-13 00:25:15 +0000247 *lp = p;
248 }
249 return p + (index & (L2_SIZE - 1));
250}
251
bellardfd6ce8f2003-05-14 19:00:11 +0000252static inline PageDesc *page_find(unsigned int index)
bellard54936002003-05-13 00:25:15 +0000253{
bellard54936002003-05-13 00:25:15 +0000254 PageDesc *p;
255
bellard54936002003-05-13 00:25:15 +0000256 p = l1_map[index >> L2_BITS];
257 if (!p)
258 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000259 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000260}
261
bellard108c49b2005-07-24 12:55:09 +0000262static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000263{
bellard108c49b2005-07-24 12:55:09 +0000264 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000265 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000266
bellard108c49b2005-07-24 12:55:09 +0000267 p = (void **)l1_phys_map;
268#if TARGET_PHYS_ADDR_SPACE_BITS > 32
269
270#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
271#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
272#endif
273 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000274 p = *lp;
275 if (!p) {
276 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000277 if (!alloc)
278 return NULL;
279 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
280 memset(p, 0, sizeof(void *) * L1_SIZE);
281 *lp = p;
282 }
283#endif
284 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000285 pd = *lp;
286 if (!pd) {
287 int i;
bellard108c49b2005-07-24 12:55:09 +0000288 /* allocate if not found */
289 if (!alloc)
290 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000291 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
292 *lp = pd;
293 for (i = 0; i < L2_SIZE; i++)
294 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000295 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000296 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000297}
298
bellard108c49b2005-07-24 12:55:09 +0000299static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000300{
bellard108c49b2005-07-24 12:55:09 +0000301 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000302}
303
bellard9fa3e852004-01-04 18:06:42 +0000304#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000305static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000306static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000307 target_ulong vaddr);
bellard9fa3e852004-01-04 18:06:42 +0000308#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000309
bellard6a00d602005-11-21 23:25:50 +0000310void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000311{
bellard6a00d602005-11-21 23:25:50 +0000312 CPUState **penv;
313 int cpu_index;
314
bellardfd6ce8f2003-05-14 19:00:11 +0000315 if (!code_gen_ptr) {
bellard57fec1f2008-02-01 10:50:11 +0000316 cpu_gen_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000317 code_gen_ptr = code_gen_buffer;
bellardb346ff42003-06-15 20:05:50 +0000318 page_init();
bellard33417e72003-08-10 21:47:01 +0000319 io_mem_init();
bellardfd6ce8f2003-05-14 19:00:11 +0000320 }
bellard6a00d602005-11-21 23:25:50 +0000321 env->next_cpu = NULL;
322 penv = &first_cpu;
323 cpu_index = 0;
324 while (*penv != NULL) {
325 penv = (CPUState **)&(*penv)->next_cpu;
326 cpu_index++;
327 }
328 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000329 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000330 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000331}
332
bellard9fa3e852004-01-04 18:06:42 +0000333static inline void invalidate_page_bitmap(PageDesc *p)
334{
335 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000336 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000337 p->code_bitmap = NULL;
338 }
339 p->code_write_count = 0;
340}
341
bellardfd6ce8f2003-05-14 19:00:11 +0000342/* set to NULL all the 'first_tb' fields in all PageDescs */
343static void page_flush_tb(void)
344{
345 int i, j;
346 PageDesc *p;
347
348 for(i = 0; i < L1_SIZE; i++) {
349 p = l1_map[i];
350 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000351 for(j = 0; j < L2_SIZE; j++) {
352 p->first_tb = NULL;
353 invalidate_page_bitmap(p);
354 p++;
355 }
bellardfd6ce8f2003-05-14 19:00:11 +0000356 }
357 }
358}
359
360/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000361/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000362void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000363{
bellard6a00d602005-11-21 23:25:50 +0000364 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000365#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000366 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
367 (unsigned long)(code_gen_ptr - code_gen_buffer),
368 nb_tbs, nb_tbs > 0 ?
369 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000370#endif
pbrooka208e542008-03-31 17:07:36 +0000371 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > CODE_GEN_BUFFER_SIZE)
372 cpu_abort(env1, "Internal error: code buffer overflow\n");
373
bellardfd6ce8f2003-05-14 19:00:11 +0000374 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000375
bellard6a00d602005-11-21 23:25:50 +0000376 for(env = first_cpu; env != NULL; env = env->next_cpu) {
377 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
378 }
bellard9fa3e852004-01-04 18:06:42 +0000379
bellard8a8a6082004-10-03 13:36:49 +0000380 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000381 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000382
bellardfd6ce8f2003-05-14 19:00:11 +0000383 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000384 /* XXX: flush processor icache at this point if cache flush is
385 expensive */
bellarde3db7222005-01-26 22:00:47 +0000386 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000387}
388
389#ifdef DEBUG_TB_CHECK
390
j_mayerbc98a7e2007-04-04 07:55:12 +0000391static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000392{
393 TranslationBlock *tb;
394 int i;
395 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000396 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
397 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000398 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
399 address >= tb->pc + tb->size)) {
400 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000401 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000402 }
403 }
404 }
405}
406
407/* verify that all the pages have correct rights for code */
408static void tb_page_check(void)
409{
410 TranslationBlock *tb;
411 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000412
pbrook99773bd2006-04-16 15:14:59 +0000413 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
414 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000415 flags1 = page_get_flags(tb->pc);
416 flags2 = page_get_flags(tb->pc + tb->size - 1);
417 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
418 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000419 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000420 }
421 }
422 }
423}
424
bellardd4e81642003-05-25 16:46:15 +0000425void tb_jmp_check(TranslationBlock *tb)
426{
427 TranslationBlock *tb1;
428 unsigned int n1;
429
430 /* suppress any remaining jumps to this TB */
431 tb1 = tb->jmp_first;
432 for(;;) {
433 n1 = (long)tb1 & 3;
434 tb1 = (TranslationBlock *)((long)tb1 & ~3);
435 if (n1 == 2)
436 break;
437 tb1 = tb1->jmp_next[n1];
438 }
439 /* check end of list */
440 if (tb1 != tb) {
441 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
442 }
443}
444
bellardfd6ce8f2003-05-14 19:00:11 +0000445#endif
446
447/* invalidate one TB */
448static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
449 int next_offset)
450{
451 TranslationBlock *tb1;
452 for(;;) {
453 tb1 = *ptb;
454 if (tb1 == tb) {
455 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
456 break;
457 }
458 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
459 }
460}
461
bellard9fa3e852004-01-04 18:06:42 +0000462static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
463{
464 TranslationBlock *tb1;
465 unsigned int n1;
466
467 for(;;) {
468 tb1 = *ptb;
469 n1 = (long)tb1 & 3;
470 tb1 = (TranslationBlock *)((long)tb1 & ~3);
471 if (tb1 == tb) {
472 *ptb = tb1->page_next[n1];
473 break;
474 }
475 ptb = &tb1->page_next[n1];
476 }
477}
478
bellardd4e81642003-05-25 16:46:15 +0000479static inline void tb_jmp_remove(TranslationBlock *tb, int n)
480{
481 TranslationBlock *tb1, **ptb;
482 unsigned int n1;
483
484 ptb = &tb->jmp_next[n];
485 tb1 = *ptb;
486 if (tb1) {
487 /* find tb(n) in circular list */
488 for(;;) {
489 tb1 = *ptb;
490 n1 = (long)tb1 & 3;
491 tb1 = (TranslationBlock *)((long)tb1 & ~3);
492 if (n1 == n && tb1 == tb)
493 break;
494 if (n1 == 2) {
495 ptb = &tb1->jmp_first;
496 } else {
497 ptb = &tb1->jmp_next[n1];
498 }
499 }
500 /* now we can suppress tb(n) from the list */
501 *ptb = tb->jmp_next[n];
502
503 tb->jmp_next[n] = NULL;
504 }
505}
506
507/* reset the jump entry 'n' of a TB so that it is not chained to
508 another TB */
509static inline void tb_reset_jump(TranslationBlock *tb, int n)
510{
511 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
512}
513
bellard9fa3e852004-01-04 18:06:42 +0000514static inline void tb_phys_invalidate(TranslationBlock *tb, unsigned int page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000515{
bellard6a00d602005-11-21 23:25:50 +0000516 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000517 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000518 unsigned int h, n1;
bellard9fa3e852004-01-04 18:06:42 +0000519 target_ulong phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000520 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000521
bellard9fa3e852004-01-04 18:06:42 +0000522 /* remove the TB from the hash list */
523 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
524 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000525 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000526 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000527
bellard9fa3e852004-01-04 18:06:42 +0000528 /* remove the TB from the page list */
529 if (tb->page_addr[0] != page_addr) {
530 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
531 tb_page_remove(&p->first_tb, tb);
532 invalidate_page_bitmap(p);
533 }
534 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
535 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
536 tb_page_remove(&p->first_tb, tb);
537 invalidate_page_bitmap(p);
538 }
539
bellard8a40a182005-11-20 10:35:40 +0000540 tb_invalidated_flag = 1;
541
542 /* remove the TB from the hash list */
543 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000544 for(env = first_cpu; env != NULL; env = env->next_cpu) {
545 if (env->tb_jmp_cache[h] == tb)
546 env->tb_jmp_cache[h] = NULL;
547 }
bellard8a40a182005-11-20 10:35:40 +0000548
549 /* suppress this TB from the two jump lists */
550 tb_jmp_remove(tb, 0);
551 tb_jmp_remove(tb, 1);
552
553 /* suppress any remaining jumps to this TB */
554 tb1 = tb->jmp_first;
555 for(;;) {
556 n1 = (long)tb1 & 3;
557 if (n1 == 2)
558 break;
559 tb1 = (TranslationBlock *)((long)tb1 & ~3);
560 tb2 = tb1->jmp_next[n1];
561 tb_reset_jump(tb1, n1);
562 tb1->jmp_next[n1] = NULL;
563 tb1 = tb2;
564 }
565 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
566
bellarde3db7222005-01-26 22:00:47 +0000567 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000568}
569
570static inline void set_bits(uint8_t *tab, int start, int len)
571{
572 int end, mask, end1;
573
574 end = start + len;
575 tab += start >> 3;
576 mask = 0xff << (start & 7);
577 if ((start & ~7) == (end & ~7)) {
578 if (start < end) {
579 mask &= ~(0xff << (end & 7));
580 *tab |= mask;
581 }
582 } else {
583 *tab++ |= mask;
584 start = (start + 8) & ~7;
585 end1 = end & ~7;
586 while (start < end1) {
587 *tab++ = 0xff;
588 start += 8;
589 }
590 if (start < end) {
591 mask = ~(0xff << (end & 7));
592 *tab |= mask;
593 }
594 }
595}
596
597static void build_page_bitmap(PageDesc *p)
598{
599 int n, tb_start, tb_end;
600 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000601
bellard59817cc2004-02-16 22:01:13 +0000602 p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000603 if (!p->code_bitmap)
604 return;
605 memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
606
607 tb = p->first_tb;
608 while (tb != NULL) {
609 n = (long)tb & 3;
610 tb = (TranslationBlock *)((long)tb & ~3);
611 /* NOTE: this is subtle as a TB may span two physical pages */
612 if (n == 0) {
613 /* NOTE: tb_end may be after the end of the page, but
614 it is not a problem */
615 tb_start = tb->pc & ~TARGET_PAGE_MASK;
616 tb_end = tb_start + tb->size;
617 if (tb_end > TARGET_PAGE_SIZE)
618 tb_end = TARGET_PAGE_SIZE;
619 } else {
620 tb_start = 0;
621 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
622 }
623 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
624 tb = tb->page_next[n];
625 }
626}
627
bellardd720b932004-04-25 17:57:43 +0000628#ifdef TARGET_HAS_PRECISE_SMC
629
ths5fafdf22007-09-16 21:08:06 +0000630static void tb_gen_code(CPUState *env,
bellardd720b932004-04-25 17:57:43 +0000631 target_ulong pc, target_ulong cs_base, int flags,
632 int cflags)
633{
634 TranslationBlock *tb;
635 uint8_t *tc_ptr;
636 target_ulong phys_pc, phys_page2, virt_page2;
637 int code_gen_size;
638
bellardc27004e2005-01-03 23:35:10 +0000639 phys_pc = get_phys_addr_code(env, pc);
640 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000641 if (!tb) {
642 /* flush must be done */
643 tb_flush(env);
644 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000645 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000646 }
647 tc_ptr = code_gen_ptr;
648 tb->tc_ptr = tc_ptr;
649 tb->cs_base = cs_base;
650 tb->flags = flags;
651 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000652 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000653 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 +0000654
bellardd720b932004-04-25 17:57:43 +0000655 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000656 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000657 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000658 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000659 phys_page2 = get_phys_addr_code(env, virt_page2);
660 }
661 tb_link_phys(tb, phys_pc, phys_page2);
662}
663#endif
ths3b46e622007-09-17 08:09:54 +0000664
bellard9fa3e852004-01-04 18:06:42 +0000665/* invalidate all TBs which intersect with the target physical page
666 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000667 the same physical page. 'is_cpu_write_access' should be true if called
668 from a real cpu write access: the virtual CPU will exit the current
669 TB if code is modified inside this TB. */
ths5fafdf22007-09-16 21:08:06 +0000670void tb_invalidate_phys_page_range(target_ulong start, target_ulong end,
bellardd720b932004-04-25 17:57:43 +0000671 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000672{
bellardd720b932004-04-25 17:57:43 +0000673 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000674 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000675 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000676 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000677 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000678 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000679
680 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000681 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000682 return;
ths5fafdf22007-09-16 21:08:06 +0000683 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000684 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
685 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000686 /* build code bitmap */
687 build_page_bitmap(p);
688 }
689
690 /* we remove all the TBs in the range [start, end[ */
691 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000692 current_tb_not_found = is_cpu_write_access;
693 current_tb_modified = 0;
694 current_tb = NULL; /* avoid warning */
695 current_pc = 0; /* avoid warning */
696 current_cs_base = 0; /* avoid warning */
697 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000698 tb = p->first_tb;
699 while (tb != NULL) {
700 n = (long)tb & 3;
701 tb = (TranslationBlock *)((long)tb & ~3);
702 tb_next = tb->page_next[n];
703 /* NOTE: this is subtle as a TB may span two physical pages */
704 if (n == 0) {
705 /* NOTE: tb_end may be after the end of the page, but
706 it is not a problem */
707 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
708 tb_end = tb_start + tb->size;
709 } else {
710 tb_start = tb->page_addr[1];
711 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
712 }
713 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000714#ifdef TARGET_HAS_PRECISE_SMC
715 if (current_tb_not_found) {
716 current_tb_not_found = 0;
717 current_tb = NULL;
718 if (env->mem_write_pc) {
719 /* now we have a real cpu fault */
720 current_tb = tb_find_pc(env->mem_write_pc);
721 }
722 }
723 if (current_tb == tb &&
724 !(current_tb->cflags & CF_SINGLE_INSN)) {
725 /* If we are modifying the current TB, we must stop
726 its execution. We could be more precise by checking
727 that the modification is after the current PC, but it
728 would require a specialized function to partially
729 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000730
bellardd720b932004-04-25 17:57:43 +0000731 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000732 cpu_restore_state(current_tb, env,
bellardd720b932004-04-25 17:57:43 +0000733 env->mem_write_pc, NULL);
734#if defined(TARGET_I386)
735 current_flags = env->hflags;
736 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
737 current_cs_base = (target_ulong)env->segs[R_CS].base;
738 current_pc = current_cs_base + env->eip;
739#else
740#error unsupported CPU
741#endif
742 }
743#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000744 /* we need to do that to handle the case where a signal
745 occurs while doing tb_phys_invalidate() */
746 saved_tb = NULL;
747 if (env) {
748 saved_tb = env->current_tb;
749 env->current_tb = NULL;
750 }
bellard9fa3e852004-01-04 18:06:42 +0000751 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000752 if (env) {
753 env->current_tb = saved_tb;
754 if (env->interrupt_request && env->current_tb)
755 cpu_interrupt(env, env->interrupt_request);
756 }
bellard9fa3e852004-01-04 18:06:42 +0000757 }
758 tb = tb_next;
759 }
760#if !defined(CONFIG_USER_ONLY)
761 /* if no code remaining, no need to continue to use slow writes */
762 if (!p->first_tb) {
763 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000764 if (is_cpu_write_access) {
765 tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
766 }
767 }
768#endif
769#ifdef TARGET_HAS_PRECISE_SMC
770 if (current_tb_modified) {
771 /* we generate a block containing just the instruction
772 modifying the memory. It will ensure that it cannot modify
773 itself */
bellardea1c1802004-06-14 18:56:36 +0000774 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000775 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000776 CF_SINGLE_INSN);
777 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000778 }
779#endif
780}
781
782/* len must be <= 8 and start must be a multiple of len */
bellardd720b932004-04-25 17:57:43 +0000783static inline void tb_invalidate_phys_page_fast(target_ulong start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000784{
785 PageDesc *p;
786 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000787#if 0
bellarda4193c82004-06-03 14:01:43 +0000788 if (1) {
789 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000790 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
791 cpu_single_env->mem_write_vaddr, len,
792 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000793 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
794 }
bellard59817cc2004-02-16 22:01:13 +0000795 }
796#endif
bellard9fa3e852004-01-04 18:06:42 +0000797 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000798 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000799 return;
800 if (p->code_bitmap) {
801 offset = start & ~TARGET_PAGE_MASK;
802 b = p->code_bitmap[offset >> 3] >> (offset & 7);
803 if (b & ((1 << len) - 1))
804 goto do_invalidate;
805 } else {
806 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000807 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000808 }
809}
810
bellard9fa3e852004-01-04 18:06:42 +0000811#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +0000812static void tb_invalidate_phys_page(target_ulong addr,
bellardd720b932004-04-25 17:57:43 +0000813 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000814{
bellardd720b932004-04-25 17:57:43 +0000815 int n, current_flags, current_tb_modified;
816 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000817 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000818 TranslationBlock *tb, *current_tb;
819#ifdef TARGET_HAS_PRECISE_SMC
820 CPUState *env = cpu_single_env;
821#endif
bellard9fa3e852004-01-04 18:06:42 +0000822
823 addr &= TARGET_PAGE_MASK;
824 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000825 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000826 return;
827 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000828 current_tb_modified = 0;
829 current_tb = NULL;
830 current_pc = 0; /* avoid warning */
831 current_cs_base = 0; /* avoid warning */
832 current_flags = 0; /* avoid warning */
833#ifdef TARGET_HAS_PRECISE_SMC
834 if (tb && pc != 0) {
835 current_tb = tb_find_pc(pc);
836 }
837#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000838 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000839 n = (long)tb & 3;
840 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000841#ifdef TARGET_HAS_PRECISE_SMC
842 if (current_tb == tb &&
843 !(current_tb->cflags & CF_SINGLE_INSN)) {
844 /* If we are modifying the current TB, we must stop
845 its execution. We could be more precise by checking
846 that the modification is after the current PC, but it
847 would require a specialized function to partially
848 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000849
bellardd720b932004-04-25 17:57:43 +0000850 current_tb_modified = 1;
851 cpu_restore_state(current_tb, env, pc, puc);
852#if defined(TARGET_I386)
853 current_flags = env->hflags;
854 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
855 current_cs_base = (target_ulong)env->segs[R_CS].base;
856 current_pc = current_cs_base + env->eip;
857#else
858#error unsupported CPU
859#endif
860 }
861#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000862 tb_phys_invalidate(tb, addr);
863 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000864 }
865 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000866#ifdef TARGET_HAS_PRECISE_SMC
867 if (current_tb_modified) {
868 /* we generate a block containing just the instruction
869 modifying the memory. It will ensure that it cannot modify
870 itself */
bellardea1c1802004-06-14 18:56:36 +0000871 env->current_tb = NULL;
ths5fafdf22007-09-16 21:08:06 +0000872 tb_gen_code(env, current_pc, current_cs_base, current_flags,
bellardd720b932004-04-25 17:57:43 +0000873 CF_SINGLE_INSN);
874 cpu_resume_from_signal(env, puc);
875 }
876#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000877}
bellard9fa3e852004-01-04 18:06:42 +0000878#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000879
880/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +0000881static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +0000882 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000883{
884 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +0000885 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000886
bellard9fa3e852004-01-04 18:06:42 +0000887 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +0000888 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +0000889 tb->page_next[n] = p->first_tb;
890 last_first_tb = p->first_tb;
891 p->first_tb = (TranslationBlock *)((long)tb | n);
892 invalidate_page_bitmap(p);
893
bellard107db442004-06-22 18:48:46 +0000894#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +0000895
bellard9fa3e852004-01-04 18:06:42 +0000896#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +0000897 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +0000898 target_ulong addr;
899 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +0000900 int prot;
901
bellardfd6ce8f2003-05-14 19:00:11 +0000902 /* force the host page as non writable (writes will have a
903 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +0000904 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +0000905 prot = 0;
pbrook53a59602006-03-25 19:31:22 +0000906 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
907 addr += TARGET_PAGE_SIZE) {
908
909 p2 = page_find (addr >> TARGET_PAGE_BITS);
910 if (!p2)
911 continue;
912 prot |= p2->flags;
913 p2->flags &= ~PAGE_WRITE;
914 page_get_flags(addr);
915 }
ths5fafdf22007-09-16 21:08:06 +0000916 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +0000917 (prot & PAGE_BITS) & ~PAGE_WRITE);
918#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +0000919 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +0000920 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +0000921#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000922 }
bellard9fa3e852004-01-04 18:06:42 +0000923#else
924 /* if some code is already present, then the pages are already
925 protected. So we handle the case where only the first TB is
926 allocated in a physical page */
927 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +0000928 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +0000929 }
930#endif
bellardd720b932004-04-25 17:57:43 +0000931
932#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +0000933}
934
935/* Allocate a new translation block. Flush the translation buffer if
936 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +0000937TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +0000938{
939 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000940
ths5fafdf22007-09-16 21:08:06 +0000941 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
bellardfd6ce8f2003-05-14 19:00:11 +0000942 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
bellardd4e81642003-05-25 16:46:15 +0000943 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +0000944 tb = &tbs[nb_tbs++];
945 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +0000946 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +0000947 return tb;
948}
949
bellard9fa3e852004-01-04 18:06:42 +0000950/* add a new TB and link it to the physical page tables. phys_page2 is
951 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +0000952void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +0000953 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +0000954{
bellard9fa3e852004-01-04 18:06:42 +0000955 unsigned int h;
956 TranslationBlock **ptb;
957
958 /* add in the physical hash table */
959 h = tb_phys_hash_func(phys_pc);
960 ptb = &tb_phys_hash[h];
961 tb->phys_hash_next = *ptb;
962 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +0000963
964 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +0000965 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
966 if (phys_page2 != -1)
967 tb_alloc_page(tb, 1, phys_page2);
968 else
969 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +0000970
bellardd4e81642003-05-25 16:46:15 +0000971 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
972 tb->jmp_next[0] = NULL;
973 tb->jmp_next[1] = NULL;
974
975 /* init original jump addresses */
976 if (tb->tb_next_offset[0] != 0xffff)
977 tb_reset_jump(tb, 0);
978 if (tb->tb_next_offset[1] != 0xffff)
979 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +0000980
981#ifdef DEBUG_TB_CHECK
982 tb_page_check();
983#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000984}
985
bellarda513fe12003-05-27 23:29:48 +0000986/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
987 tb[1].tc_ptr. Return NULL if not found */
988TranslationBlock *tb_find_pc(unsigned long tc_ptr)
989{
990 int m_min, m_max, m;
991 unsigned long v;
992 TranslationBlock *tb;
993
994 if (nb_tbs <= 0)
995 return NULL;
996 if (tc_ptr < (unsigned long)code_gen_buffer ||
997 tc_ptr >= (unsigned long)code_gen_ptr)
998 return NULL;
999 /* binary search (cf Knuth) */
1000 m_min = 0;
1001 m_max = nb_tbs - 1;
1002 while (m_min <= m_max) {
1003 m = (m_min + m_max) >> 1;
1004 tb = &tbs[m];
1005 v = (unsigned long)tb->tc_ptr;
1006 if (v == tc_ptr)
1007 return tb;
1008 else if (tc_ptr < v) {
1009 m_max = m - 1;
1010 } else {
1011 m_min = m + 1;
1012 }
ths5fafdf22007-09-16 21:08:06 +00001013 }
bellarda513fe12003-05-27 23:29:48 +00001014 return &tbs[m_max];
1015}
bellard75012672003-06-21 13:11:07 +00001016
bellardea041c02003-06-25 16:16:50 +00001017static void tb_reset_jump_recursive(TranslationBlock *tb);
1018
1019static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1020{
1021 TranslationBlock *tb1, *tb_next, **ptb;
1022 unsigned int n1;
1023
1024 tb1 = tb->jmp_next[n];
1025 if (tb1 != NULL) {
1026 /* find head of list */
1027 for(;;) {
1028 n1 = (long)tb1 & 3;
1029 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1030 if (n1 == 2)
1031 break;
1032 tb1 = tb1->jmp_next[n1];
1033 }
1034 /* we are now sure now that tb jumps to tb1 */
1035 tb_next = tb1;
1036
1037 /* remove tb from the jmp_first list */
1038 ptb = &tb_next->jmp_first;
1039 for(;;) {
1040 tb1 = *ptb;
1041 n1 = (long)tb1 & 3;
1042 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1043 if (n1 == n && tb1 == tb)
1044 break;
1045 ptb = &tb1->jmp_next[n1];
1046 }
1047 *ptb = tb->jmp_next[n];
1048 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001049
bellardea041c02003-06-25 16:16:50 +00001050 /* suppress the jump to next tb in generated code */
1051 tb_reset_jump(tb, n);
1052
bellard01243112004-01-04 15:48:17 +00001053 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001054 tb_reset_jump_recursive(tb_next);
1055 }
1056}
1057
1058static void tb_reset_jump_recursive(TranslationBlock *tb)
1059{
1060 tb_reset_jump_recursive2(tb, 0);
1061 tb_reset_jump_recursive2(tb, 1);
1062}
1063
bellard1fddef42005-04-17 19:16:13 +00001064#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001065static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1066{
j_mayer9b3c35e2007-04-07 11:21:28 +00001067 target_phys_addr_t addr;
1068 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001069 ram_addr_t ram_addr;
1070 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001071
pbrookc2f07f82006-04-08 17:14:56 +00001072 addr = cpu_get_phys_page_debug(env, pc);
1073 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1074 if (!p) {
1075 pd = IO_MEM_UNASSIGNED;
1076 } else {
1077 pd = p->phys_offset;
1078 }
1079 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001080 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001081}
bellardc27004e2005-01-03 23:35:10 +00001082#endif
bellardd720b932004-04-25 17:57:43 +00001083
pbrook6658ffb2007-03-16 23:58:11 +00001084/* Add a watchpoint. */
1085int cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1086{
1087 int i;
1088
1089 for (i = 0; i < env->nb_watchpoints; i++) {
1090 if (addr == env->watchpoint[i].vaddr)
1091 return 0;
1092 }
1093 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1094 return -1;
1095
1096 i = env->nb_watchpoints++;
1097 env->watchpoint[i].vaddr = addr;
1098 tlb_flush_page(env, addr);
1099 /* FIXME: This flush is needed because of the hack to make memory ops
1100 terminate the TB. It can be removed once the proper IO trap and
1101 re-execute bits are in. */
1102 tb_flush(env);
1103 return i;
1104}
1105
1106/* Remove a watchpoint. */
1107int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1108{
1109 int i;
1110
1111 for (i = 0; i < env->nb_watchpoints; i++) {
1112 if (addr == env->watchpoint[i].vaddr) {
1113 env->nb_watchpoints--;
1114 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1115 tlb_flush_page(env, addr);
1116 return 0;
1117 }
1118 }
1119 return -1;
1120}
1121
bellardc33a3462003-07-29 20:50:33 +00001122/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1123 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001124int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001125{
bellard1fddef42005-04-17 19:16:13 +00001126#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001127 int i;
ths3b46e622007-09-17 08:09:54 +00001128
bellard4c3a88a2003-07-26 12:06:08 +00001129 for(i = 0; i < env->nb_breakpoints; i++) {
1130 if (env->breakpoints[i] == pc)
1131 return 0;
1132 }
1133
1134 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1135 return -1;
1136 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001137
bellardd720b932004-04-25 17:57:43 +00001138 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001139 return 0;
1140#else
1141 return -1;
1142#endif
1143}
1144
1145/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001146int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001147{
bellard1fddef42005-04-17 19:16:13 +00001148#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001149 int i;
1150 for(i = 0; i < env->nb_breakpoints; i++) {
1151 if (env->breakpoints[i] == pc)
1152 goto found;
1153 }
1154 return -1;
1155 found:
bellard4c3a88a2003-07-26 12:06:08 +00001156 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001157 if (i < env->nb_breakpoints)
1158 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001159
1160 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001161 return 0;
1162#else
1163 return -1;
1164#endif
1165}
1166
bellardc33a3462003-07-29 20:50:33 +00001167/* enable or disable single step mode. EXCP_DEBUG is returned by the
1168 CPU loop after each instruction */
1169void cpu_single_step(CPUState *env, int enabled)
1170{
bellard1fddef42005-04-17 19:16:13 +00001171#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001172 if (env->singlestep_enabled != enabled) {
1173 env->singlestep_enabled = enabled;
1174 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001175 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001176 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001177 }
1178#endif
1179}
1180
bellard34865132003-10-05 14:28:56 +00001181/* enable or disable low levels log */
1182void cpu_set_log(int log_flags)
1183{
1184 loglevel = log_flags;
1185 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001186 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001187 if (!logfile) {
1188 perror(logfilename);
1189 _exit(1);
1190 }
bellard9fa3e852004-01-04 18:06:42 +00001191#if !defined(CONFIG_SOFTMMU)
1192 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1193 {
1194 static uint8_t logfile_buf[4096];
1195 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1196 }
1197#else
bellard34865132003-10-05 14:28:56 +00001198 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001199#endif
pbrooke735b912007-06-30 13:53:24 +00001200 log_append = 1;
1201 }
1202 if (!loglevel && logfile) {
1203 fclose(logfile);
1204 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001205 }
1206}
1207
1208void cpu_set_log_filename(const char *filename)
1209{
1210 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001211 if (logfile) {
1212 fclose(logfile);
1213 logfile = NULL;
1214 }
1215 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001216}
bellardc33a3462003-07-29 20:50:33 +00001217
bellard01243112004-01-04 15:48:17 +00001218/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001219void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001220{
1221 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001222 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
bellard59817cc2004-02-16 22:01:13 +00001223
bellard68a79312003-06-30 13:12:32 +00001224 env->interrupt_request |= mask;
bellardea041c02003-06-25 16:16:50 +00001225 /* if the cpu is currently executing code, we must unlink it and
1226 all the potentially executing TB */
1227 tb = env->current_tb;
bellardee8b7022004-02-03 23:35:10 +00001228 if (tb && !testandset(&interrupt_lock)) {
1229 env->current_tb = NULL;
bellardea041c02003-06-25 16:16:50 +00001230 tb_reset_jump_recursive(tb);
aurel3215a51152008-03-28 22:29:15 +00001231 resetlock(&interrupt_lock);
bellardea041c02003-06-25 16:16:50 +00001232 }
1233}
1234
bellardb54ad042004-05-20 13:42:52 +00001235void cpu_reset_interrupt(CPUState *env, int mask)
1236{
1237 env->interrupt_request &= ~mask;
1238}
1239
bellardf193c792004-03-21 17:06:25 +00001240CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001241 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001242 "show generated host assembly code for each compiled TB" },
1243 { CPU_LOG_TB_IN_ASM, "in_asm",
1244 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001245 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001246 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001247 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001248 "show micro ops "
1249#ifdef TARGET_I386
1250 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001251#endif
blueswir1e01a1152008-03-14 17:37:11 +00001252 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001253 { CPU_LOG_INT, "int",
1254 "show interrupts/exceptions in short format" },
1255 { CPU_LOG_EXEC, "exec",
1256 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001257 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001258 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001259#ifdef TARGET_I386
1260 { CPU_LOG_PCALL, "pcall",
1261 "show protected mode far calls/returns/exceptions" },
1262#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001263#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001264 { CPU_LOG_IOPORT, "ioport",
1265 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001266#endif
bellardf193c792004-03-21 17:06:25 +00001267 { 0, NULL, NULL },
1268};
1269
1270static int cmp1(const char *s1, int n, const char *s2)
1271{
1272 if (strlen(s2) != n)
1273 return 0;
1274 return memcmp(s1, s2, n) == 0;
1275}
ths3b46e622007-09-17 08:09:54 +00001276
bellardf193c792004-03-21 17:06:25 +00001277/* takes a comma separated list of log masks. Return 0 if error. */
1278int cpu_str_to_log_mask(const char *str)
1279{
1280 CPULogItem *item;
1281 int mask;
1282 const char *p, *p1;
1283
1284 p = str;
1285 mask = 0;
1286 for(;;) {
1287 p1 = strchr(p, ',');
1288 if (!p1)
1289 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001290 if(cmp1(p,p1-p,"all")) {
1291 for(item = cpu_log_items; item->mask != 0; item++) {
1292 mask |= item->mask;
1293 }
1294 } else {
bellardf193c792004-03-21 17:06:25 +00001295 for(item = cpu_log_items; item->mask != 0; item++) {
1296 if (cmp1(p, p1 - p, item->name))
1297 goto found;
1298 }
1299 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001300 }
bellardf193c792004-03-21 17:06:25 +00001301 found:
1302 mask |= item->mask;
1303 if (*p1 != ',')
1304 break;
1305 p = p1 + 1;
1306 }
1307 return mask;
1308}
bellardea041c02003-06-25 16:16:50 +00001309
bellard75012672003-06-21 13:11:07 +00001310void cpu_abort(CPUState *env, const char *fmt, ...)
1311{
1312 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001313 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001314
1315 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001316 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001317 fprintf(stderr, "qemu: fatal: ");
1318 vfprintf(stderr, fmt, ap);
1319 fprintf(stderr, "\n");
1320#ifdef TARGET_I386
ths0573fbf2007-09-23 15:28:04 +00001321 if(env->intercept & INTERCEPT_SVM_MASK) {
1322 /* most probably the virtual machine should not
1323 be shut down but rather caught by the VMM */
1324 vmexit(SVM_EXIT_SHUTDOWN, 0);
1325 }
bellard7fe48482004-10-09 18:08:01 +00001326 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1327#else
1328 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001329#endif
balrog924edca2007-06-10 14:07:13 +00001330 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001331 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001332 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001333 fprintf(logfile, "\n");
1334#ifdef TARGET_I386
1335 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1336#else
1337 cpu_dump_state(env, logfile, fprintf, 0);
1338#endif
balrog924edca2007-06-10 14:07:13 +00001339 fflush(logfile);
1340 fclose(logfile);
1341 }
pbrook493ae1f2007-11-23 16:53:59 +00001342 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001343 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001344 abort();
1345}
1346
thsc5be9f02007-02-28 20:20:53 +00001347CPUState *cpu_copy(CPUState *env)
1348{
ths01ba9812007-12-09 02:22:57 +00001349 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001350 /* preserve chaining and index */
1351 CPUState *next_cpu = new_env->next_cpu;
1352 int cpu_index = new_env->cpu_index;
1353 memcpy(new_env, env, sizeof(CPUState));
1354 new_env->next_cpu = next_cpu;
1355 new_env->cpu_index = cpu_index;
1356 return new_env;
1357}
1358
bellard01243112004-01-04 15:48:17 +00001359#if !defined(CONFIG_USER_ONLY)
1360
bellardee8b7022004-02-03 23:35:10 +00001361/* NOTE: if flush_global is true, also flush global entries (not
1362 implemented yet) */
1363void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001364{
bellard33417e72003-08-10 21:47:01 +00001365 int i;
bellard01243112004-01-04 15:48:17 +00001366
bellard9fa3e852004-01-04 18:06:42 +00001367#if defined(DEBUG_TLB)
1368 printf("tlb_flush:\n");
1369#endif
bellard01243112004-01-04 15:48:17 +00001370 /* must reset current TB so that interrupts cannot modify the
1371 links while we are modifying them */
1372 env->current_tb = NULL;
1373
bellard33417e72003-08-10 21:47:01 +00001374 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001375 env->tlb_table[0][i].addr_read = -1;
1376 env->tlb_table[0][i].addr_write = -1;
1377 env->tlb_table[0][i].addr_code = -1;
1378 env->tlb_table[1][i].addr_read = -1;
1379 env->tlb_table[1][i].addr_write = -1;
1380 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001381#if (NB_MMU_MODES >= 3)
1382 env->tlb_table[2][i].addr_read = -1;
1383 env->tlb_table[2][i].addr_write = -1;
1384 env->tlb_table[2][i].addr_code = -1;
1385#if (NB_MMU_MODES == 4)
1386 env->tlb_table[3][i].addr_read = -1;
1387 env->tlb_table[3][i].addr_write = -1;
1388 env->tlb_table[3][i].addr_code = -1;
1389#endif
1390#endif
bellard33417e72003-08-10 21:47:01 +00001391 }
bellard9fa3e852004-01-04 18:06:42 +00001392
bellard8a40a182005-11-20 10:35:40 +00001393 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001394
1395#if !defined(CONFIG_SOFTMMU)
1396 munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
1397#endif
bellard0a962c02005-02-10 22:00:27 +00001398#ifdef USE_KQEMU
1399 if (env->kqemu_enabled) {
1400 kqemu_flush(env, flush_global);
1401 }
1402#endif
bellarde3db7222005-01-26 22:00:47 +00001403 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001404}
1405
bellard274da6b2004-05-20 21:56:27 +00001406static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001407{
ths5fafdf22007-09-16 21:08:06 +00001408 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001409 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001410 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001411 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001412 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001413 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1414 tlb_entry->addr_read = -1;
1415 tlb_entry->addr_write = -1;
1416 tlb_entry->addr_code = -1;
1417 }
bellard61382a52003-10-27 21:22:23 +00001418}
1419
bellard2e126692004-04-25 21:28:44 +00001420void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001421{
bellard8a40a182005-11-20 10:35:40 +00001422 int i;
bellard9fa3e852004-01-04 18:06:42 +00001423 TranslationBlock *tb;
bellard01243112004-01-04 15:48:17 +00001424
bellard9fa3e852004-01-04 18:06:42 +00001425#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001426 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001427#endif
bellard01243112004-01-04 15:48:17 +00001428 /* must reset current TB so that interrupts cannot modify the
1429 links while we are modifying them */
1430 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001431
bellard61382a52003-10-27 21:22:23 +00001432 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001433 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001434 tlb_flush_entry(&env->tlb_table[0][i], addr);
1435 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001436#if (NB_MMU_MODES >= 3)
1437 tlb_flush_entry(&env->tlb_table[2][i], addr);
1438#if (NB_MMU_MODES == 4)
1439 tlb_flush_entry(&env->tlb_table[3][i], addr);
1440#endif
1441#endif
bellard01243112004-01-04 15:48:17 +00001442
pbrookb362e5e2006-11-12 20:40:55 +00001443 /* Discard jump cache entries for any tb which might potentially
1444 overlap the flushed page. */
1445 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1446 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
1447
1448 i = tb_jmp_cache_hash_page(addr);
1449 memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
bellard9fa3e852004-01-04 18:06:42 +00001450
bellard01243112004-01-04 15:48:17 +00001451#if !defined(CONFIG_SOFTMMU)
bellard9fa3e852004-01-04 18:06:42 +00001452 if (addr < MMAP_AREA_END)
bellard01243112004-01-04 15:48:17 +00001453 munmap((void *)addr, TARGET_PAGE_SIZE);
bellard61382a52003-10-27 21:22:23 +00001454#endif
bellard0a962c02005-02-10 22:00:27 +00001455#ifdef USE_KQEMU
1456 if (env->kqemu_enabled) {
1457 kqemu_flush_page(env, addr);
1458 }
1459#endif
bellard9fa3e852004-01-04 18:06:42 +00001460}
1461
bellard9fa3e852004-01-04 18:06:42 +00001462/* update the TLBs so that writes to code in the virtual page 'addr'
1463 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001464static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001465{
ths5fafdf22007-09-16 21:08:06 +00001466 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001467 ram_addr + TARGET_PAGE_SIZE,
1468 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001469}
1470
bellard9fa3e852004-01-04 18:06:42 +00001471/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001472 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001473static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001474 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001475{
bellard3a7d9292005-08-21 09:26:42 +00001476 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001477}
1478
ths5fafdf22007-09-16 21:08:06 +00001479static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001480 unsigned long start, unsigned long length)
1481{
1482 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001483 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1484 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001485 if ((addr - start) < length) {
bellard84b7b8e2005-11-28 21:19:04 +00001486 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001487 }
1488 }
1489}
1490
bellard3a7d9292005-08-21 09:26:42 +00001491void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001492 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001493{
1494 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001495 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001496 int i, mask, len;
1497 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001498
1499 start &= TARGET_PAGE_MASK;
1500 end = TARGET_PAGE_ALIGN(end);
1501
1502 length = end - start;
1503 if (length == 0)
1504 return;
bellard0a962c02005-02-10 22:00:27 +00001505 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001506#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001507 /* XXX: should not depend on cpu context */
1508 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001509 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001510 ram_addr_t addr;
1511 addr = start;
1512 for(i = 0; i < len; i++) {
1513 kqemu_set_notdirty(env, addr);
1514 addr += TARGET_PAGE_SIZE;
1515 }
bellard3a7d9292005-08-21 09:26:42 +00001516 }
1517#endif
bellardf23db162005-08-21 19:12:28 +00001518 mask = ~dirty_flags;
1519 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1520 for(i = 0; i < len; i++)
1521 p[i] &= mask;
1522
bellard1ccde1c2004-02-06 19:46:14 +00001523 /* we modify the TLB cache so that the dirty bit will be set again
1524 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001525 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001526 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1527 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001528 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001529 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001530 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001531#if (NB_MMU_MODES >= 3)
1532 for(i = 0; i < CPU_TLB_SIZE; i++)
1533 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1534#if (NB_MMU_MODES == 4)
1535 for(i = 0; i < CPU_TLB_SIZE; i++)
1536 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1537#endif
1538#endif
bellard6a00d602005-11-21 23:25:50 +00001539 }
bellard59817cc2004-02-16 22:01:13 +00001540
1541#if !defined(CONFIG_SOFTMMU)
1542 /* XXX: this is expensive */
1543 {
1544 VirtPageDesc *p;
1545 int j;
1546 target_ulong addr;
1547
1548 for(i = 0; i < L1_SIZE; i++) {
1549 p = l1_virt_map[i];
1550 if (p) {
1551 addr = i << (TARGET_PAGE_BITS + L2_BITS);
1552 for(j = 0; j < L2_SIZE; j++) {
1553 if (p->valid_tag == virt_valid_tag &&
1554 p->phys_addr >= start && p->phys_addr < end &&
1555 (p->prot & PROT_WRITE)) {
1556 if (addr < MMAP_AREA_END) {
ths5fafdf22007-09-16 21:08:06 +00001557 mprotect((void *)addr, TARGET_PAGE_SIZE,
bellard59817cc2004-02-16 22:01:13 +00001558 p->prot & ~PROT_WRITE);
1559 }
1560 }
1561 addr += TARGET_PAGE_SIZE;
1562 p++;
1563 }
1564 }
1565 }
1566 }
1567#endif
bellard1ccde1c2004-02-06 19:46:14 +00001568}
1569
bellard3a7d9292005-08-21 09:26:42 +00001570static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1571{
1572 ram_addr_t ram_addr;
1573
bellard84b7b8e2005-11-28 21:19:04 +00001574 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001575 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001576 tlb_entry->addend - (unsigned long)phys_ram_base;
1577 if (!cpu_physical_memory_is_dirty(ram_addr)) {
bellard84b7b8e2005-11-28 21:19:04 +00001578 tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001579 }
1580 }
1581}
1582
1583/* update the TLB according to the current state of the dirty bits */
1584void cpu_tlb_update_dirty(CPUState *env)
1585{
1586 int i;
1587 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001588 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001589 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001590 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001591#if (NB_MMU_MODES >= 3)
1592 for(i = 0; i < CPU_TLB_SIZE; i++)
1593 tlb_update_dirty(&env->tlb_table[2][i]);
1594#if (NB_MMU_MODES == 4)
1595 for(i = 0; i < CPU_TLB_SIZE; i++)
1596 tlb_update_dirty(&env->tlb_table[3][i]);
1597#endif
1598#endif
bellard3a7d9292005-08-21 09:26:42 +00001599}
1600
ths5fafdf22007-09-16 21:08:06 +00001601static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
bellard108c49b2005-07-24 12:55:09 +00001602 unsigned long start)
bellard1ccde1c2004-02-06 19:46:14 +00001603{
1604 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001605 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
1606 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001607 if (addr == start) {
bellard84b7b8e2005-11-28 21:19:04 +00001608 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
bellard1ccde1c2004-02-06 19:46:14 +00001609 }
1610 }
1611}
1612
1613/* update the TLB corresponding to virtual page vaddr and phys addr
1614 addr so that it is no longer dirty */
bellard6a00d602005-11-21 23:25:50 +00001615static inline void tlb_set_dirty(CPUState *env,
1616 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001617{
bellard1ccde1c2004-02-06 19:46:14 +00001618 int i;
1619
bellard1ccde1c2004-02-06 19:46:14 +00001620 addr &= TARGET_PAGE_MASK;
1621 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001622 tlb_set_dirty1(&env->tlb_table[0][i], addr);
1623 tlb_set_dirty1(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001624#if (NB_MMU_MODES >= 3)
1625 tlb_set_dirty1(&env->tlb_table[2][i], addr);
1626#if (NB_MMU_MODES == 4)
1627 tlb_set_dirty1(&env->tlb_table[3][i], addr);
1628#endif
1629#endif
bellard9fa3e852004-01-04 18:06:42 +00001630}
1631
bellard59817cc2004-02-16 22:01:13 +00001632/* add a new TLB entry. At most one entry for a given virtual address
1633 is permitted. Return 0 if OK or 2 if the page could not be mapped
1634 (can only happen in non SOFTMMU mode for I/O pages or pages
1635 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001636int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1637 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001638 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001639{
bellard92e873b2004-05-21 14:52:29 +00001640 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001641 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001642 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001643 target_ulong address;
bellard108c49b2005-07-24 12:55:09 +00001644 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001645 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001646 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001647 int i;
bellard9fa3e852004-01-04 18:06:42 +00001648
bellard92e873b2004-05-21 14:52:29 +00001649 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001650 if (!p) {
1651 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001652 } else {
1653 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001654 }
1655#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001656 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1657 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001658#endif
1659
1660 ret = 0;
1661#if !defined(CONFIG_SOFTMMU)
ths5fafdf22007-09-16 21:08:06 +00001662 if (is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001663#endif
1664 {
bellard2a4188a2006-06-25 21:54:59 +00001665 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
bellard9fa3e852004-01-04 18:06:42 +00001666 /* IO memory case */
1667 address = vaddr | pd;
1668 addend = paddr;
1669 } else {
1670 /* standard memory */
1671 address = vaddr;
1672 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1673 }
pbrook6658ffb2007-03-16 23:58:11 +00001674
1675 /* Make accesses to pages with watchpoints go via the
1676 watchpoint trap routines. */
1677 for (i = 0; i < env->nb_watchpoints; i++) {
1678 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1679 if (address & ~TARGET_PAGE_MASK) {
balrogd79acba2007-06-26 20:01:13 +00001680 env->watchpoint[i].addend = 0;
pbrook6658ffb2007-03-16 23:58:11 +00001681 address = vaddr | io_mem_watch;
1682 } else {
balrogd79acba2007-06-26 20:01:13 +00001683 env->watchpoint[i].addend = pd - paddr +
1684 (unsigned long) phys_ram_base;
pbrook6658ffb2007-03-16 23:58:11 +00001685 /* TODO: Figure out how to make read watchpoints coexist
1686 with code. */
1687 pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
1688 }
1689 }
1690 }
balrogd79acba2007-06-26 20:01:13 +00001691
bellard90f18422005-07-24 10:17:31 +00001692 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard9fa3e852004-01-04 18:06:42 +00001693 addend -= vaddr;
j_mayer6ebbf392007-10-14 07:07:08 +00001694 te = &env->tlb_table[mmu_idx][index];
bellard84b7b8e2005-11-28 21:19:04 +00001695 te->addend = addend;
bellard67b915a2004-03-31 23:37:16 +00001696 if (prot & PAGE_READ) {
bellard84b7b8e2005-11-28 21:19:04 +00001697 te->addr_read = address;
bellard9fa3e852004-01-04 18:06:42 +00001698 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001699 te->addr_read = -1;
1700 }
1701 if (prot & PAGE_EXEC) {
1702 te->addr_code = address;
1703 } else {
1704 te->addr_code = -1;
bellard9fa3e852004-01-04 18:06:42 +00001705 }
bellard67b915a2004-03-31 23:37:16 +00001706 if (prot & PAGE_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001707 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellard856074e2006-07-04 09:47:34 +00001708 (pd & IO_MEM_ROMD)) {
1709 /* write access calls the I/O callback */
ths5fafdf22007-09-16 21:08:06 +00001710 te->addr_write = vaddr |
bellard856074e2006-07-04 09:47:34 +00001711 (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
ths5fafdf22007-09-16 21:08:06 +00001712 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard1ccde1c2004-02-06 19:46:14 +00001713 !cpu_physical_memory_is_dirty(pd)) {
bellard84b7b8e2005-11-28 21:19:04 +00001714 te->addr_write = vaddr | IO_MEM_NOTDIRTY;
bellard9fa3e852004-01-04 18:06:42 +00001715 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001716 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001717 }
1718 } else {
bellard84b7b8e2005-11-28 21:19:04 +00001719 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001720 }
1721 }
1722#if !defined(CONFIG_SOFTMMU)
1723 else {
1724 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
1725 /* IO access: no mapping is done as it will be handled by the
1726 soft MMU */
1727 if (!(env->hflags & HF_SOFTMMU_MASK))
1728 ret = 2;
1729 } else {
1730 void *map_addr;
bellard9fa3e852004-01-04 18:06:42 +00001731
bellard59817cc2004-02-16 22:01:13 +00001732 if (vaddr >= MMAP_AREA_END) {
1733 ret = 2;
1734 } else {
1735 if (prot & PROT_WRITE) {
ths5fafdf22007-09-16 21:08:06 +00001736 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
bellardd720b932004-04-25 17:57:43 +00001737#if defined(TARGET_HAS_SMC) || 1
bellard59817cc2004-02-16 22:01:13 +00001738 first_tb ||
bellardd720b932004-04-25 17:57:43 +00001739#endif
ths5fafdf22007-09-16 21:08:06 +00001740 ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
bellard59817cc2004-02-16 22:01:13 +00001741 !cpu_physical_memory_is_dirty(pd))) {
1742 /* ROM: we do as if code was inside */
1743 /* if code is present, we only map as read only and save the
1744 original mapping */
1745 VirtPageDesc *vp;
ths3b46e622007-09-17 08:09:54 +00001746
bellard90f18422005-07-24 10:17:31 +00001747 vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
bellard59817cc2004-02-16 22:01:13 +00001748 vp->phys_addr = pd;
1749 vp->prot = prot;
1750 vp->valid_tag = virt_valid_tag;
1751 prot &= ~PAGE_WRITE;
1752 }
bellard9fa3e852004-01-04 18:06:42 +00001753 }
ths5fafdf22007-09-16 21:08:06 +00001754 map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
bellard59817cc2004-02-16 22:01:13 +00001755 MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
1756 if (map_addr == MAP_FAILED) {
1757 cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
1758 paddr, vaddr);
1759 }
bellard9fa3e852004-01-04 18:06:42 +00001760 }
1761 }
1762 }
1763#endif
1764 return ret;
1765}
1766
1767/* called from signal handler: invalidate the code and unprotect the
1768 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001769int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001770{
1771#if !defined(CONFIG_SOFTMMU)
1772 VirtPageDesc *vp;
1773
1774#if defined(DEBUG_TLB)
1775 printf("page_unprotect: addr=0x%08x\n", addr);
1776#endif
1777 addr &= TARGET_PAGE_MASK;
bellard59817cc2004-02-16 22:01:13 +00001778
1779 /* if it is not mapped, no need to worry here */
1780 if (addr >= MMAP_AREA_END)
1781 return 0;
bellard9fa3e852004-01-04 18:06:42 +00001782 vp = virt_page_find(addr >> TARGET_PAGE_BITS);
1783 if (!vp)
1784 return 0;
1785 /* NOTE: in this case, validate_tag is _not_ tested as it
1786 validates only the code TLB */
1787 if (vp->valid_tag != virt_valid_tag)
1788 return 0;
1789 if (!(vp->prot & PAGE_WRITE))
1790 return 0;
1791#if defined(DEBUG_TLB)
ths5fafdf22007-09-16 21:08:06 +00001792 printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
bellard9fa3e852004-01-04 18:06:42 +00001793 addr, vp->phys_addr, vp->prot);
1794#endif
bellard59817cc2004-02-16 22:01:13 +00001795 if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
1796 cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
1797 (unsigned long)addr, vp->prot);
bellardd720b932004-04-25 17:57:43 +00001798 /* set the dirty bit */
bellard0a962c02005-02-10 22:00:27 +00001799 phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
bellardd720b932004-04-25 17:57:43 +00001800 /* flush the code inside */
1801 tb_invalidate_phys_page(vp->phys_addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001802 return 1;
1803#else
1804 return 0;
1805#endif
bellard33417e72003-08-10 21:47:01 +00001806}
1807
bellard01243112004-01-04 15:48:17 +00001808#else
1809
bellardee8b7022004-02-03 23:35:10 +00001810void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001811{
1812}
1813
bellard2e126692004-04-25 21:28:44 +00001814void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001815{
1816}
1817
ths5fafdf22007-09-16 21:08:06 +00001818int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1819 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001820 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001821{
bellard9fa3e852004-01-04 18:06:42 +00001822 return 0;
1823}
bellard33417e72003-08-10 21:47:01 +00001824
bellard9fa3e852004-01-04 18:06:42 +00001825/* dump memory mappings */
1826void page_dump(FILE *f)
1827{
1828 unsigned long start, end;
1829 int i, j, prot, prot1;
1830 PageDesc *p;
1831
1832 fprintf(f, "%-8s %-8s %-8s %s\n",
1833 "start", "end", "size", "prot");
1834 start = -1;
1835 end = -1;
1836 prot = 0;
1837 for(i = 0; i <= L1_SIZE; i++) {
1838 if (i < L1_SIZE)
1839 p = l1_map[i];
1840 else
1841 p = NULL;
1842 for(j = 0;j < L2_SIZE; j++) {
1843 if (!p)
1844 prot1 = 0;
1845 else
1846 prot1 = p[j].flags;
1847 if (prot1 != prot) {
1848 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1849 if (start != -1) {
1850 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001851 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001852 prot & PAGE_READ ? 'r' : '-',
1853 prot & PAGE_WRITE ? 'w' : '-',
1854 prot & PAGE_EXEC ? 'x' : '-');
1855 }
1856 if (prot1 != 0)
1857 start = end;
1858 else
1859 start = -1;
1860 prot = prot1;
1861 }
1862 if (!p)
1863 break;
1864 }
bellard33417e72003-08-10 21:47:01 +00001865 }
bellard33417e72003-08-10 21:47:01 +00001866}
1867
pbrook53a59602006-03-25 19:31:22 +00001868int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001869{
bellard9fa3e852004-01-04 18:06:42 +00001870 PageDesc *p;
1871
1872 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001873 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001874 return 0;
1875 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001876}
1877
bellard9fa3e852004-01-04 18:06:42 +00001878/* modify the flags of a page and invalidate the code if
1879 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1880 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001881void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001882{
1883 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001884 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001885
1886 start = start & TARGET_PAGE_MASK;
1887 end = TARGET_PAGE_ALIGN(end);
1888 if (flags & PAGE_WRITE)
1889 flags |= PAGE_WRITE_ORG;
1890 spin_lock(&tb_lock);
1891 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1892 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
1893 /* if the write protection is set, then we invalidate the code
1894 inside */
ths5fafdf22007-09-16 21:08:06 +00001895 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001896 (flags & PAGE_WRITE) &&
1897 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001898 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001899 }
1900 p->flags = flags;
1901 }
1902 spin_unlock(&tb_lock);
1903}
1904
ths3d97b402007-11-02 19:02:07 +00001905int page_check_range(target_ulong start, target_ulong len, int flags)
1906{
1907 PageDesc *p;
1908 target_ulong end;
1909 target_ulong addr;
1910
1911 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
1912 start = start & TARGET_PAGE_MASK;
1913
1914 if( end < start )
1915 /* we've wrapped around */
1916 return -1;
1917 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1918 p = page_find(addr >> TARGET_PAGE_BITS);
1919 if( !p )
1920 return -1;
1921 if( !(p->flags & PAGE_VALID) )
1922 return -1;
1923
bellarddae32702007-11-14 10:51:00 +00001924 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00001925 return -1;
bellarddae32702007-11-14 10:51:00 +00001926 if (flags & PAGE_WRITE) {
1927 if (!(p->flags & PAGE_WRITE_ORG))
1928 return -1;
1929 /* unprotect the page if it was put read-only because it
1930 contains translated code */
1931 if (!(p->flags & PAGE_WRITE)) {
1932 if (!page_unprotect(addr, 0, NULL))
1933 return -1;
1934 }
1935 return 0;
1936 }
ths3d97b402007-11-02 19:02:07 +00001937 }
1938 return 0;
1939}
1940
bellard9fa3e852004-01-04 18:06:42 +00001941/* called from signal handler: invalidate the code and unprotect the
1942 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00001943int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001944{
1945 unsigned int page_index, prot, pindex;
1946 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00001947 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00001948
bellard83fb7ad2004-07-05 21:25:26 +00001949 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00001950 page_index = host_start >> TARGET_PAGE_BITS;
1951 p1 = page_find(page_index);
1952 if (!p1)
1953 return 0;
bellard83fb7ad2004-07-05 21:25:26 +00001954 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00001955 p = p1;
1956 prot = 0;
1957 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
1958 prot |= p->flags;
1959 p++;
1960 }
1961 /* if the page was really writable, then we change its
1962 protection back to writable */
1963 if (prot & PAGE_WRITE_ORG) {
1964 pindex = (address - host_start) >> TARGET_PAGE_BITS;
1965 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00001966 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00001967 (prot & PAGE_BITS) | PAGE_WRITE);
1968 p1[pindex].flags |= PAGE_WRITE;
1969 /* and since the content will be modified, we must invalidate
1970 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00001971 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00001972#ifdef DEBUG_TB_CHECK
1973 tb_invalidate_check(address);
1974#endif
1975 return 1;
1976 }
1977 }
1978 return 0;
1979}
1980
bellard6a00d602005-11-21 23:25:50 +00001981static inline void tlb_set_dirty(CPUState *env,
1982 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001983{
1984}
bellard9fa3e852004-01-04 18:06:42 +00001985#endif /* defined(CONFIG_USER_ONLY) */
1986
blueswir1db7b5422007-05-26 17:36:03 +00001987static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1988 int memory);
1989static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
1990 int orig_memory);
1991#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
1992 need_subpage) \
1993 do { \
1994 if (addr > start_addr) \
1995 start_addr2 = 0; \
1996 else { \
1997 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
1998 if (start_addr2 > 0) \
1999 need_subpage = 1; \
2000 } \
2001 \
blueswir149e9fba2007-05-30 17:25:06 +00002002 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002003 end_addr2 = TARGET_PAGE_SIZE - 1; \
2004 else { \
2005 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2006 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2007 need_subpage = 1; \
2008 } \
2009 } while (0)
2010
bellard33417e72003-08-10 21:47:01 +00002011/* register physical memory. 'size' must be a multiple of the target
2012 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2013 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002014void cpu_register_physical_memory(target_phys_addr_t start_addr,
bellard2e126692004-04-25 21:28:44 +00002015 unsigned long size,
2016 unsigned long phys_offset)
bellard33417e72003-08-10 21:47:01 +00002017{
bellard108c49b2005-07-24 12:55:09 +00002018 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002019 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002020 CPUState *env;
blueswir1db7b5422007-05-26 17:36:03 +00002021 unsigned long orig_size = size;
2022 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002023
bellard5fd386f2004-05-23 21:11:22 +00002024 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002025 end_addr = start_addr + (target_phys_addr_t)size;
2026 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002027 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2028 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2029 unsigned long orig_memory = p->phys_offset;
2030 target_phys_addr_t start_addr2, end_addr2;
2031 int need_subpage = 0;
2032
2033 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2034 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002035 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002036 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2037 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2038 &p->phys_offset, orig_memory);
2039 } else {
2040 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2041 >> IO_MEM_SHIFT];
2042 }
2043 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2044 } else {
2045 p->phys_offset = phys_offset;
2046 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2047 (phys_offset & IO_MEM_ROMD))
2048 phys_offset += TARGET_PAGE_SIZE;
2049 }
2050 } else {
2051 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2052 p->phys_offset = phys_offset;
2053 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2054 (phys_offset & IO_MEM_ROMD))
2055 phys_offset += TARGET_PAGE_SIZE;
2056 else {
2057 target_phys_addr_t start_addr2, end_addr2;
2058 int need_subpage = 0;
2059
2060 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2061 end_addr2, need_subpage);
2062
blueswir14254fab2008-01-01 16:57:19 +00002063 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002064 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2065 &p->phys_offset, IO_MEM_UNASSIGNED);
2066 subpage_register(subpage, start_addr2, end_addr2,
2067 phys_offset);
2068 }
2069 }
2070 }
bellard33417e72003-08-10 21:47:01 +00002071 }
ths3b46e622007-09-17 08:09:54 +00002072
bellard9d420372006-06-25 22:25:22 +00002073 /* since each CPU stores ram addresses in its TLB cache, we must
2074 reset the modified entries */
2075 /* XXX: slow ! */
2076 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2077 tlb_flush(env, 1);
2078 }
bellard33417e72003-08-10 21:47:01 +00002079}
2080
bellardba863452006-09-24 18:41:10 +00002081/* XXX: temporary until new memory mapping API */
2082uint32_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2083{
2084 PhysPageDesc *p;
2085
2086 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2087 if (!p)
2088 return IO_MEM_UNASSIGNED;
2089 return p->phys_offset;
2090}
2091
bellarde9a1ab12007-02-08 23:08:38 +00002092/* XXX: better than nothing */
2093ram_addr_t qemu_ram_alloc(unsigned int size)
2094{
2095 ram_addr_t addr;
2096 if ((phys_ram_alloc_offset + size) >= phys_ram_size) {
ths5fafdf22007-09-16 21:08:06 +00002097 fprintf(stderr, "Not enough memory (requested_size = %u, max memory = %d)\n",
bellarde9a1ab12007-02-08 23:08:38 +00002098 size, phys_ram_size);
2099 abort();
2100 }
2101 addr = phys_ram_alloc_offset;
2102 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2103 return addr;
2104}
2105
2106void qemu_ram_free(ram_addr_t addr)
2107{
2108}
2109
bellarda4193c82004-06-03 14:01:43 +00002110static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002111{
pbrook67d3b952006-12-18 05:03:52 +00002112#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002113 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002114#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002115#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002116 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002117#elif TARGET_CRIS
2118 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002119#endif
bellard33417e72003-08-10 21:47:01 +00002120 return 0;
2121}
2122
bellarda4193c82004-06-03 14:01:43 +00002123static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002124{
pbrook67d3b952006-12-18 05:03:52 +00002125#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002126 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002127#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002128#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002129 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002130#elif TARGET_CRIS
2131 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002132#endif
bellard33417e72003-08-10 21:47:01 +00002133}
2134
2135static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2136 unassigned_mem_readb,
2137 unassigned_mem_readb,
2138 unassigned_mem_readb,
2139};
2140
2141static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2142 unassigned_mem_writeb,
2143 unassigned_mem_writeb,
2144 unassigned_mem_writeb,
2145};
2146
bellarda4193c82004-06-03 14:01:43 +00002147static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002148{
bellard3a7d9292005-08-21 09:26:42 +00002149 unsigned long ram_addr;
2150 int dirty_flags;
2151 ram_addr = addr - (unsigned long)phys_ram_base;
2152 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2153 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2154#if !defined(CONFIG_USER_ONLY)
2155 tb_invalidate_phys_page_fast(ram_addr, 1);
2156 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2157#endif
2158 }
bellardc27004e2005-01-03 23:35:10 +00002159 stb_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002160#ifdef USE_KQEMU
2161 if (cpu_single_env->kqemu_enabled &&
2162 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2163 kqemu_modify_page(cpu_single_env, ram_addr);
2164#endif
bellardf23db162005-08-21 19:12:28 +00002165 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2166 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2167 /* we remove the notdirty callback only if the code has been
2168 flushed */
2169 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002170 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002171}
2172
bellarda4193c82004-06-03 14:01:43 +00002173static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002174{
bellard3a7d9292005-08-21 09:26:42 +00002175 unsigned long ram_addr;
2176 int dirty_flags;
2177 ram_addr = addr - (unsigned long)phys_ram_base;
2178 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2179 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2180#if !defined(CONFIG_USER_ONLY)
2181 tb_invalidate_phys_page_fast(ram_addr, 2);
2182 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2183#endif
2184 }
bellardc27004e2005-01-03 23:35:10 +00002185 stw_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002186#ifdef USE_KQEMU
2187 if (cpu_single_env->kqemu_enabled &&
2188 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2189 kqemu_modify_page(cpu_single_env, ram_addr);
2190#endif
bellardf23db162005-08-21 19:12:28 +00002191 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2192 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2193 /* we remove the notdirty callback only if the code has been
2194 flushed */
2195 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002196 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002197}
2198
bellarda4193c82004-06-03 14:01:43 +00002199static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002200{
bellard3a7d9292005-08-21 09:26:42 +00002201 unsigned long ram_addr;
2202 int dirty_flags;
2203 ram_addr = addr - (unsigned long)phys_ram_base;
2204 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2205 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2206#if !defined(CONFIG_USER_ONLY)
2207 tb_invalidate_phys_page_fast(ram_addr, 4);
2208 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2209#endif
2210 }
bellardc27004e2005-01-03 23:35:10 +00002211 stl_p((uint8_t *)(long)addr, val);
bellardf32fc642006-02-08 22:43:39 +00002212#ifdef USE_KQEMU
2213 if (cpu_single_env->kqemu_enabled &&
2214 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2215 kqemu_modify_page(cpu_single_env, ram_addr);
2216#endif
bellardf23db162005-08-21 19:12:28 +00002217 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2218 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2219 /* we remove the notdirty callback only if the code has been
2220 flushed */
2221 if (dirty_flags == 0xff)
bellard6a00d602005-11-21 23:25:50 +00002222 tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002223}
2224
bellard3a7d9292005-08-21 09:26:42 +00002225static CPUReadMemoryFunc *error_mem_read[3] = {
2226 NULL, /* never used */
2227 NULL, /* never used */
2228 NULL, /* never used */
2229};
2230
bellard1ccde1c2004-02-06 19:46:14 +00002231static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2232 notdirty_mem_writeb,
2233 notdirty_mem_writew,
2234 notdirty_mem_writel,
2235};
2236
pbrook6658ffb2007-03-16 23:58:11 +00002237#if defined(CONFIG_SOFTMMU)
2238/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2239 so these check for a hit then pass through to the normal out-of-line
2240 phys routines. */
2241static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2242{
2243 return ldub_phys(addr);
2244}
2245
2246static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2247{
2248 return lduw_phys(addr);
2249}
2250
2251static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2252{
2253 return ldl_phys(addr);
2254}
2255
2256/* Generate a debug exception if a watchpoint has been hit.
2257 Returns the real physical address of the access. addr will be a host
balrogd79acba2007-06-26 20:01:13 +00002258 address in case of a RAM location. */
pbrook6658ffb2007-03-16 23:58:11 +00002259static target_ulong check_watchpoint(target_phys_addr_t addr)
2260{
2261 CPUState *env = cpu_single_env;
2262 target_ulong watch;
2263 target_ulong retaddr;
2264 int i;
2265
2266 retaddr = addr;
2267 for (i = 0; i < env->nb_watchpoints; i++) {
2268 watch = env->watchpoint[i].vaddr;
2269 if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
balrogd79acba2007-06-26 20:01:13 +00002270 retaddr = addr - env->watchpoint[i].addend;
pbrook6658ffb2007-03-16 23:58:11 +00002271 if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
2272 cpu_single_env->watchpoint_hit = i + 1;
2273 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
2274 break;
2275 }
2276 }
2277 }
2278 return retaddr;
2279}
2280
2281static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2282 uint32_t val)
2283{
2284 addr = check_watchpoint(addr);
2285 stb_phys(addr, val);
2286}
2287
2288static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2289 uint32_t val)
2290{
2291 addr = check_watchpoint(addr);
2292 stw_phys(addr, val);
2293}
2294
2295static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2296 uint32_t val)
2297{
2298 addr = check_watchpoint(addr);
2299 stl_phys(addr, val);
2300}
2301
2302static CPUReadMemoryFunc *watch_mem_read[3] = {
2303 watch_mem_readb,
2304 watch_mem_readw,
2305 watch_mem_readl,
2306};
2307
2308static CPUWriteMemoryFunc *watch_mem_write[3] = {
2309 watch_mem_writeb,
2310 watch_mem_writew,
2311 watch_mem_writel,
2312};
2313#endif
2314
blueswir1db7b5422007-05-26 17:36:03 +00002315static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2316 unsigned int len)
2317{
blueswir1db7b5422007-05-26 17:36:03 +00002318 uint32_t ret;
2319 unsigned int idx;
2320
2321 idx = SUBPAGE_IDX(addr - mmio->base);
2322#if defined(DEBUG_SUBPAGE)
2323 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2324 mmio, len, addr, idx);
2325#endif
blueswir13ee89922008-01-02 19:45:26 +00002326 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002327
2328 return ret;
2329}
2330
2331static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2332 uint32_t value, unsigned int len)
2333{
blueswir1db7b5422007-05-26 17:36:03 +00002334 unsigned int idx;
2335
2336 idx = SUBPAGE_IDX(addr - mmio->base);
2337#if defined(DEBUG_SUBPAGE)
2338 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2339 mmio, len, addr, idx, value);
2340#endif
blueswir13ee89922008-01-02 19:45:26 +00002341 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002342}
2343
2344static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2345{
2346#if defined(DEBUG_SUBPAGE)
2347 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2348#endif
2349
2350 return subpage_readlen(opaque, addr, 0);
2351}
2352
2353static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2354 uint32_t value)
2355{
2356#if defined(DEBUG_SUBPAGE)
2357 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2358#endif
2359 subpage_writelen(opaque, addr, value, 0);
2360}
2361
2362static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2363{
2364#if defined(DEBUG_SUBPAGE)
2365 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2366#endif
2367
2368 return subpage_readlen(opaque, addr, 1);
2369}
2370
2371static void subpage_writew (void *opaque, target_phys_addr_t addr,
2372 uint32_t value)
2373{
2374#if defined(DEBUG_SUBPAGE)
2375 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2376#endif
2377 subpage_writelen(opaque, addr, value, 1);
2378}
2379
2380static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2381{
2382#if defined(DEBUG_SUBPAGE)
2383 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2384#endif
2385
2386 return subpage_readlen(opaque, addr, 2);
2387}
2388
2389static void subpage_writel (void *opaque,
2390 target_phys_addr_t addr, uint32_t value)
2391{
2392#if defined(DEBUG_SUBPAGE)
2393 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2394#endif
2395 subpage_writelen(opaque, addr, value, 2);
2396}
2397
2398static CPUReadMemoryFunc *subpage_read[] = {
2399 &subpage_readb,
2400 &subpage_readw,
2401 &subpage_readl,
2402};
2403
2404static CPUWriteMemoryFunc *subpage_write[] = {
2405 &subpage_writeb,
2406 &subpage_writew,
2407 &subpage_writel,
2408};
2409
2410static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2411 int memory)
2412{
2413 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002414 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002415
2416 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2417 return -1;
2418 idx = SUBPAGE_IDX(start);
2419 eidx = SUBPAGE_IDX(end);
2420#if defined(DEBUG_SUBPAGE)
2421 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2422 mmio, start, end, idx, eidx, memory);
2423#endif
2424 memory >>= IO_MEM_SHIFT;
2425 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002426 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002427 if (io_mem_read[memory][i]) {
2428 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2429 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2430 }
2431 if (io_mem_write[memory][i]) {
2432 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2433 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2434 }
blueswir14254fab2008-01-01 16:57:19 +00002435 }
blueswir1db7b5422007-05-26 17:36:03 +00002436 }
2437
2438 return 0;
2439}
2440
2441static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
2442 int orig_memory)
2443{
2444 subpage_t *mmio;
2445 int subpage_memory;
2446
2447 mmio = qemu_mallocz(sizeof(subpage_t));
2448 if (mmio != NULL) {
2449 mmio->base = base;
2450 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2451#if defined(DEBUG_SUBPAGE)
2452 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2453 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2454#endif
2455 *phys = subpage_memory | IO_MEM_SUBPAGE;
2456 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2457 }
2458
2459 return mmio;
2460}
2461
bellard33417e72003-08-10 21:47:01 +00002462static void io_mem_init(void)
2463{
bellard3a7d9292005-08-21 09:26:42 +00002464 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002465 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002466 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002467 io_mem_nb = 5;
2468
pbrook6658ffb2007-03-16 23:58:11 +00002469#if defined(CONFIG_SOFTMMU)
2470 io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2471 watch_mem_write, NULL);
2472#endif
bellard1ccde1c2004-02-06 19:46:14 +00002473 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002474 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002475 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002476}
2477
2478/* mem_read and mem_write are arrays of functions containing the
2479 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002480 2). Functions can be omitted with a NULL function pointer. The
2481 registered functions may be modified dynamically later.
2482 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002483 modified. If it is zero, a new io zone is allocated. The return
2484 value can be used with cpu_register_physical_memory(). (-1) is
2485 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002486int cpu_register_io_memory(int io_index,
2487 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002488 CPUWriteMemoryFunc **mem_write,
2489 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002490{
blueswir14254fab2008-01-01 16:57:19 +00002491 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002492
2493 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002494 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002495 return -1;
2496 io_index = io_mem_nb++;
2497 } else {
2498 if (io_index >= IO_MEM_NB_ENTRIES)
2499 return -1;
2500 }
bellardb5ff1b32005-11-26 10:38:39 +00002501
bellard33417e72003-08-10 21:47:01 +00002502 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002503 if (!mem_read[i] || !mem_write[i])
2504 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002505 io_mem_read[io_index][i] = mem_read[i];
2506 io_mem_write[io_index][i] = mem_write[i];
2507 }
bellarda4193c82004-06-03 14:01:43 +00002508 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002509 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002510}
bellard61382a52003-10-27 21:22:23 +00002511
bellard8926b512004-10-10 15:14:20 +00002512CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2513{
2514 return io_mem_write[io_index >> IO_MEM_SHIFT];
2515}
2516
2517CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2518{
2519 return io_mem_read[io_index >> IO_MEM_SHIFT];
2520}
2521
bellard13eb76e2004-01-24 15:23:36 +00002522/* physical memory access (slow version, mainly for debug) */
2523#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002524void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002525 int len, int is_write)
2526{
2527 int l, flags;
2528 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002529 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002530
2531 while (len > 0) {
2532 page = addr & TARGET_PAGE_MASK;
2533 l = (page + TARGET_PAGE_SIZE) - addr;
2534 if (l > len)
2535 l = len;
2536 flags = page_get_flags(page);
2537 if (!(flags & PAGE_VALID))
2538 return;
2539 if (is_write) {
2540 if (!(flags & PAGE_WRITE))
2541 return;
bellard579a97f2007-11-11 14:26:47 +00002542 /* XXX: this code should not depend on lock_user */
2543 if (!(p = lock_user(VERIFY_WRITE, addr, len, 0)))
2544 /* FIXME - should this return an error rather than just fail? */
2545 return;
pbrook53a59602006-03-25 19:31:22 +00002546 memcpy(p, buf, len);
2547 unlock_user(p, addr, len);
bellard13eb76e2004-01-24 15:23:36 +00002548 } else {
2549 if (!(flags & PAGE_READ))
2550 return;
bellard579a97f2007-11-11 14:26:47 +00002551 /* XXX: this code should not depend on lock_user */
2552 if (!(p = lock_user(VERIFY_READ, addr, len, 1)))
2553 /* FIXME - should this return an error rather than just fail? */
2554 return;
pbrook53a59602006-03-25 19:31:22 +00002555 memcpy(buf, p, len);
2556 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002557 }
2558 len -= l;
2559 buf += l;
2560 addr += l;
2561 }
2562}
bellard8df1cd02005-01-28 22:37:22 +00002563
bellard13eb76e2004-01-24 15:23:36 +00002564#else
ths5fafdf22007-09-16 21:08:06 +00002565void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002566 int len, int is_write)
2567{
2568 int l, io_index;
2569 uint8_t *ptr;
2570 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002571 target_phys_addr_t page;
2572 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002573 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002574
bellard13eb76e2004-01-24 15:23:36 +00002575 while (len > 0) {
2576 page = addr & TARGET_PAGE_MASK;
2577 l = (page + TARGET_PAGE_SIZE) - addr;
2578 if (l > len)
2579 l = len;
bellard92e873b2004-05-21 14:52:29 +00002580 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002581 if (!p) {
2582 pd = IO_MEM_UNASSIGNED;
2583 } else {
2584 pd = p->phys_offset;
2585 }
ths3b46e622007-09-17 08:09:54 +00002586
bellard13eb76e2004-01-24 15:23:36 +00002587 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002588 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002589 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002590 /* XXX: could force cpu_single_env to NULL to avoid
2591 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002592 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002593 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002594 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002595 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002596 l = 4;
2597 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002598 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002599 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002600 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002601 l = 2;
2602 } else {
bellard1c213d12005-09-03 10:49:04 +00002603 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002604 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002605 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002606 l = 1;
2607 }
2608 } else {
bellardb448f2f2004-02-25 23:24:04 +00002609 unsigned long addr1;
2610 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002611 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002612 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002613 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002614 if (!cpu_physical_memory_is_dirty(addr1)) {
2615 /* invalidate code */
2616 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2617 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002618 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002619 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002620 }
bellard13eb76e2004-01-24 15:23:36 +00002621 }
2622 } else {
ths5fafdf22007-09-16 21:08:06 +00002623 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002624 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002625 /* I/O case */
2626 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2627 if (l >= 4 && ((addr & 3) == 0)) {
2628 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002629 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002630 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002631 l = 4;
2632 } else if (l >= 2 && ((addr & 1) == 0)) {
2633 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002634 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002635 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002636 l = 2;
2637 } else {
bellard1c213d12005-09-03 10:49:04 +00002638 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002639 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002640 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002641 l = 1;
2642 }
2643 } else {
2644 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002645 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002646 (addr & ~TARGET_PAGE_MASK);
2647 memcpy(buf, ptr, l);
2648 }
2649 }
2650 len -= l;
2651 buf += l;
2652 addr += l;
2653 }
2654}
bellard8df1cd02005-01-28 22:37:22 +00002655
bellardd0ecd2a2006-04-23 17:14:48 +00002656/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002657void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002658 const uint8_t *buf, int len)
2659{
2660 int l;
2661 uint8_t *ptr;
2662 target_phys_addr_t page;
2663 unsigned long pd;
2664 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002665
bellardd0ecd2a2006-04-23 17:14:48 +00002666 while (len > 0) {
2667 page = addr & TARGET_PAGE_MASK;
2668 l = (page + TARGET_PAGE_SIZE) - addr;
2669 if (l > len)
2670 l = len;
2671 p = phys_page_find(page >> TARGET_PAGE_BITS);
2672 if (!p) {
2673 pd = IO_MEM_UNASSIGNED;
2674 } else {
2675 pd = p->phys_offset;
2676 }
ths3b46e622007-09-17 08:09:54 +00002677
bellardd0ecd2a2006-04-23 17:14:48 +00002678 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002679 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2680 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002681 /* do nothing */
2682 } else {
2683 unsigned long addr1;
2684 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2685 /* ROM/RAM case */
2686 ptr = phys_ram_base + addr1;
2687 memcpy(ptr, buf, l);
2688 }
2689 len -= l;
2690 buf += l;
2691 addr += l;
2692 }
2693}
2694
2695
bellard8df1cd02005-01-28 22:37:22 +00002696/* warning: addr must be aligned */
2697uint32_t ldl_phys(target_phys_addr_t addr)
2698{
2699 int io_index;
2700 uint8_t *ptr;
2701 uint32_t val;
2702 unsigned long pd;
2703 PhysPageDesc *p;
2704
2705 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2706 if (!p) {
2707 pd = IO_MEM_UNASSIGNED;
2708 } else {
2709 pd = p->phys_offset;
2710 }
ths3b46e622007-09-17 08:09:54 +00002711
ths5fafdf22007-09-16 21:08:06 +00002712 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002713 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002714 /* I/O case */
2715 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2716 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2717 } else {
2718 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002719 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002720 (addr & ~TARGET_PAGE_MASK);
2721 val = ldl_p(ptr);
2722 }
2723 return val;
2724}
2725
bellard84b7b8e2005-11-28 21:19:04 +00002726/* warning: addr must be aligned */
2727uint64_t ldq_phys(target_phys_addr_t addr)
2728{
2729 int io_index;
2730 uint8_t *ptr;
2731 uint64_t val;
2732 unsigned long pd;
2733 PhysPageDesc *p;
2734
2735 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2736 if (!p) {
2737 pd = IO_MEM_UNASSIGNED;
2738 } else {
2739 pd = p->phys_offset;
2740 }
ths3b46e622007-09-17 08:09:54 +00002741
bellard2a4188a2006-06-25 21:54:59 +00002742 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2743 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002744 /* I/O case */
2745 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2746#ifdef TARGET_WORDS_BIGENDIAN
2747 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2748 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2749#else
2750 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2751 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2752#endif
2753 } else {
2754 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002755 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002756 (addr & ~TARGET_PAGE_MASK);
2757 val = ldq_p(ptr);
2758 }
2759 return val;
2760}
2761
bellardaab33092005-10-30 20:48:42 +00002762/* XXX: optimize */
2763uint32_t ldub_phys(target_phys_addr_t addr)
2764{
2765 uint8_t val;
2766 cpu_physical_memory_read(addr, &val, 1);
2767 return val;
2768}
2769
2770/* XXX: optimize */
2771uint32_t lduw_phys(target_phys_addr_t addr)
2772{
2773 uint16_t val;
2774 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2775 return tswap16(val);
2776}
2777
bellard8df1cd02005-01-28 22:37:22 +00002778/* warning: addr must be aligned. The ram page is not masked as dirty
2779 and the code inside is not invalidated. It is useful if the dirty
2780 bits are used to track modified PTEs */
2781void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2782{
2783 int io_index;
2784 uint8_t *ptr;
2785 unsigned long pd;
2786 PhysPageDesc *p;
2787
2788 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2789 if (!p) {
2790 pd = IO_MEM_UNASSIGNED;
2791 } else {
2792 pd = p->phys_offset;
2793 }
ths3b46e622007-09-17 08:09:54 +00002794
bellard3a7d9292005-08-21 09:26:42 +00002795 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002796 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2797 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2798 } else {
ths5fafdf22007-09-16 21:08:06 +00002799 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002800 (addr & ~TARGET_PAGE_MASK);
2801 stl_p(ptr, val);
2802 }
2803}
2804
j_mayerbc98a7e2007-04-04 07:55:12 +00002805void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2806{
2807 int io_index;
2808 uint8_t *ptr;
2809 unsigned long pd;
2810 PhysPageDesc *p;
2811
2812 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2813 if (!p) {
2814 pd = IO_MEM_UNASSIGNED;
2815 } else {
2816 pd = p->phys_offset;
2817 }
ths3b46e622007-09-17 08:09:54 +00002818
j_mayerbc98a7e2007-04-04 07:55:12 +00002819 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2820 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2821#ifdef TARGET_WORDS_BIGENDIAN
2822 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2823 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2824#else
2825 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2826 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2827#endif
2828 } else {
ths5fafdf22007-09-16 21:08:06 +00002829 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002830 (addr & ~TARGET_PAGE_MASK);
2831 stq_p(ptr, val);
2832 }
2833}
2834
bellard8df1cd02005-01-28 22:37:22 +00002835/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002836void stl_phys(target_phys_addr_t addr, uint32_t val)
2837{
2838 int io_index;
2839 uint8_t *ptr;
2840 unsigned long pd;
2841 PhysPageDesc *p;
2842
2843 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2844 if (!p) {
2845 pd = IO_MEM_UNASSIGNED;
2846 } else {
2847 pd = p->phys_offset;
2848 }
ths3b46e622007-09-17 08:09:54 +00002849
bellard3a7d9292005-08-21 09:26:42 +00002850 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002851 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2852 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2853 } else {
2854 unsigned long addr1;
2855 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2856 /* RAM case */
2857 ptr = phys_ram_base + addr1;
2858 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002859 if (!cpu_physical_memory_is_dirty(addr1)) {
2860 /* invalidate code */
2861 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2862 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002863 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2864 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002865 }
bellard8df1cd02005-01-28 22:37:22 +00002866 }
2867}
2868
bellardaab33092005-10-30 20:48:42 +00002869/* XXX: optimize */
2870void stb_phys(target_phys_addr_t addr, uint32_t val)
2871{
2872 uint8_t v = val;
2873 cpu_physical_memory_write(addr, &v, 1);
2874}
2875
2876/* XXX: optimize */
2877void stw_phys(target_phys_addr_t addr, uint32_t val)
2878{
2879 uint16_t v = tswap16(val);
2880 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2881}
2882
2883/* XXX: optimize */
2884void stq_phys(target_phys_addr_t addr, uint64_t val)
2885{
2886 val = tswap64(val);
2887 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2888}
2889
bellard13eb76e2004-01-24 15:23:36 +00002890#endif
2891
2892/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002893int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002894 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002895{
2896 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002897 target_phys_addr_t phys_addr;
2898 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002899
2900 while (len > 0) {
2901 page = addr & TARGET_PAGE_MASK;
2902 phys_addr = cpu_get_phys_page_debug(env, page);
2903 /* if no physical page mapped, return an error */
2904 if (phys_addr == -1)
2905 return -1;
2906 l = (page + TARGET_PAGE_SIZE) - addr;
2907 if (l > len)
2908 l = len;
ths5fafdf22007-09-16 21:08:06 +00002909 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00002910 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002911 len -= l;
2912 buf += l;
2913 addr += l;
2914 }
2915 return 0;
2916}
2917
bellarde3db7222005-01-26 22:00:47 +00002918void dump_exec_info(FILE *f,
2919 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
2920{
2921 int i, target_code_size, max_target_code_size;
2922 int direct_jmp_count, direct_jmp2_count, cross_page;
2923 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00002924
bellarde3db7222005-01-26 22:00:47 +00002925 target_code_size = 0;
2926 max_target_code_size = 0;
2927 cross_page = 0;
2928 direct_jmp_count = 0;
2929 direct_jmp2_count = 0;
2930 for(i = 0; i < nb_tbs; i++) {
2931 tb = &tbs[i];
2932 target_code_size += tb->size;
2933 if (tb->size > max_target_code_size)
2934 max_target_code_size = tb->size;
2935 if (tb->page_addr[1] != -1)
2936 cross_page++;
2937 if (tb->tb_next_offset[0] != 0xffff) {
2938 direct_jmp_count++;
2939 if (tb->tb_next_offset[1] != 0xffff) {
2940 direct_jmp2_count++;
2941 }
2942 }
2943 }
2944 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00002945 cpu_fprintf(f, "Translation buffer state:\n");
bellarde3db7222005-01-26 22:00:47 +00002946 cpu_fprintf(f, "TB count %d\n", nb_tbs);
ths5fafdf22007-09-16 21:08:06 +00002947 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00002948 nb_tbs ? target_code_size / nb_tbs : 0,
2949 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00002950 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00002951 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
2952 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00002953 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
2954 cross_page,
bellarde3db7222005-01-26 22:00:47 +00002955 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
2956 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00002957 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00002958 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
2959 direct_jmp2_count,
2960 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00002961 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00002962 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
2963 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
2964 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellard57fec1f2008-02-01 10:50:11 +00002965#ifdef CONFIG_PROFILER
2966 {
2967 int64_t tot;
2968 tot = dyngen_interm_time + dyngen_code_time;
2969 cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2970 tot, tot / 2.4e9);
2971 cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2972 dyngen_tb_count,
2973 dyngen_tb_count1 - dyngen_tb_count,
2974 dyngen_tb_count1 ? (double)(dyngen_tb_count1 - dyngen_tb_count) / dyngen_tb_count1 * 100.0 : 0);
2975 cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n",
2976 dyngen_tb_count ? (double)dyngen_op_count / dyngen_tb_count : 0, dyngen_op_count_max);
2977 cpu_fprintf(f, "old ops/total ops %0.1f%%\n",
2978 dyngen_op_count ? (double)dyngen_old_op_count / dyngen_op_count * 100.0 : 0);
2979 cpu_fprintf(f, "deleted ops/TB %0.2f\n",
2980 dyngen_tb_count ?
2981 (double)dyngen_tcg_del_op_count / dyngen_tb_count : 0);
2982 cpu_fprintf(f, "cycles/op %0.1f\n",
2983 dyngen_op_count ? (double)tot / dyngen_op_count : 0);
2984 cpu_fprintf(f, "cycles/in byte %0.1f\n",
2985 dyngen_code_in_len ? (double)tot / dyngen_code_in_len : 0);
2986 cpu_fprintf(f, "cycles/out byte %0.1f\n",
2987 dyngen_code_out_len ? (double)tot / dyngen_code_out_len : 0);
2988 if (tot == 0)
2989 tot = 1;
2990 cpu_fprintf(f, " gen_interm time %0.1f%%\n",
2991 (double)dyngen_interm_time / tot * 100.0);
2992 cpu_fprintf(f, " gen_code time %0.1f%%\n",
2993 (double)dyngen_code_time / tot * 100.0);
2994 cpu_fprintf(f, "cpu_restore count %" PRId64 "\n",
2995 dyngen_restore_count);
2996 cpu_fprintf(f, " avg cycles %0.1f\n",
2997 dyngen_restore_count ? (double)dyngen_restore_time / dyngen_restore_count : 0);
2998 {
2999 extern void dump_op_count(void);
3000 dump_op_count();
3001 }
3002 }
3003#endif
bellarde3db7222005-01-26 22:00:47 +00003004}
3005
ths5fafdf22007-09-16 21:08:06 +00003006#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003007
3008#define MMUSUFFIX _cmmu
3009#define GETPC() NULL
3010#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003011#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003012
3013#define SHIFT 0
3014#include "softmmu_template.h"
3015
3016#define SHIFT 1
3017#include "softmmu_template.h"
3018
3019#define SHIFT 2
3020#include "softmmu_template.h"
3021
3022#define SHIFT 3
3023#include "softmmu_template.h"
3024
3025#undef env
3026
3027#endif