blob: 6285b3d40563b8c973979e4fa308895d8152edf5 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
bellardfd6ce8f2003-05-14 19:00:11 +00002 * virtual page mapping and translated block handling
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
bellard67b915a2004-03-31 23:37:16 +000020#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000021#ifdef _WIN32
ths4fddf622007-12-17 04:42:29 +000022#define WIN32_LEAN_AND_MEAN
bellardd5a8f072004-09-29 21:15:28 +000023#include <windows.h>
24#else
bellarda98d49b2004-11-14 16:22:05 +000025#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000026#include <sys/mman.h>
27#endif
bellard54936002003-05-13 00:25:15 +000028#include <stdlib.h>
29#include <stdio.h>
30#include <stdarg.h>
31#include <string.h>
32#include <errno.h>
33#include <unistd.h>
34#include <inttypes.h>
35
bellard6180a182003-09-30 21:04:53 +000036#include "cpu.h"
37#include "exec-all.h"
aurel32ca10f862008-04-11 21:35:42 +000038#include "qemu-common.h"
bellardb67d9a52008-05-23 09:57:34 +000039#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000040#include "hw/hw.h"
aliguori74576192008-10-06 14:02:03 +000041#include "osdep.h"
aliguori7ba1e612008-11-05 16:04:33 +000042#include "kvm.h"
pbrook53a59602006-03-25 19:31:22 +000043#if defined(CONFIG_USER_ONLY)
44#include <qemu.h>
45#endif
bellard54936002003-05-13 00:25:15 +000046
bellardfd6ce8f2003-05-14 19:00:11 +000047//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000048//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000049//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000050//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000051
52/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000053//#define DEBUG_TB_CHECK
54//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000055
ths1196be32007-03-17 15:17:58 +000056//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000057//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000058
pbrook99773bd2006-04-16 15:14:59 +000059#if !defined(CONFIG_USER_ONLY)
60/* TB consistency checks only implemented for usermode emulation. */
61#undef DEBUG_TB_CHECK
62#endif
63
bellard9fa3e852004-01-04 18:06:42 +000064#define SMC_BITMAP_USE_THRESHOLD 10
65
66#define MMAP_AREA_START 0x00000000
67#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000068
bellard108c49b2005-07-24 12:55:09 +000069#if defined(TARGET_SPARC64)
70#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000071#elif defined(TARGET_SPARC)
72#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000073#elif defined(TARGET_ALPHA)
74#define TARGET_PHYS_ADDR_SPACE_BITS 42
75#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000076#elif defined(TARGET_PPC64)
77#define TARGET_PHYS_ADDR_SPACE_BITS 42
aurel3200f82b82008-04-27 21:12:55 +000078#elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
79#define TARGET_PHYS_ADDR_SPACE_BITS 42
80#elif defined(TARGET_I386) && !defined(USE_KQEMU)
81#define TARGET_PHYS_ADDR_SPACE_BITS 36
bellard108c49b2005-07-24 12:55:09 +000082#else
83/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
84#define TARGET_PHYS_ADDR_SPACE_BITS 32
85#endif
86
blueswir1bdaf78e2008-10-04 07:24:27 +000087static TranslationBlock *tbs;
bellard26a5f132008-05-28 12:30:31 +000088int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000089TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +000090static int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000091/* any access to the tbs or the page table must use this lock */
92spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000093
blueswir1141ac462008-07-26 15:05:57 +000094#if defined(__arm__) || defined(__sparc_v9__)
95/* The prologue must be reachable with a direct jump. ARM and Sparc64
96 have limited branch ranges (possibly also PPC) so place it in a
blueswir1d03d8602008-07-10 17:21:31 +000097 section close to code segment. */
98#define code_gen_section \
99 __attribute__((__section__(".gen_code"))) \
100 __attribute__((aligned (32)))
101#else
102#define code_gen_section \
103 __attribute__((aligned (32)))
104#endif
105
106uint8_t code_gen_prologue[1024] code_gen_section;
blueswir1bdaf78e2008-10-04 07:24:27 +0000107static uint8_t *code_gen_buffer;
108static unsigned long code_gen_buffer_size;
bellard26a5f132008-05-28 12:30:31 +0000109/* threshold to flush the translated code buffer */
blueswir1bdaf78e2008-10-04 07:24:27 +0000110static unsigned long code_gen_buffer_max_size;
bellardfd6ce8f2003-05-14 19:00:11 +0000111uint8_t *code_gen_ptr;
112
pbrooke2eef172008-06-08 01:09:01 +0000113#if !defined(CONFIG_USER_ONLY)
aurel3200f82b82008-04-27 21:12:55 +0000114ram_addr_t phys_ram_size;
bellard9fa3e852004-01-04 18:06:42 +0000115int phys_ram_fd;
116uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +0000117uint8_t *phys_ram_dirty;
aliguori74576192008-10-06 14:02:03 +0000118static int in_migration;
bellarde9a1ab12007-02-08 23:08:38 +0000119static ram_addr_t phys_ram_alloc_offset = 0;
pbrooke2eef172008-06-08 01:09:01 +0000120#endif
bellard9fa3e852004-01-04 18:06:42 +0000121
bellard6a00d602005-11-21 23:25:50 +0000122CPUState *first_cpu;
123/* current CPU in the current thread. It is only valid inside
124 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000125CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000126/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +0000127 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +0000128 2 = Adaptive rate instruction counting. */
129int use_icount = 0;
130/* Current instruction counter. While executing translated code this may
131 include some instructions that have not yet been executed. */
132int64_t qemu_icount;
bellard6a00d602005-11-21 23:25:50 +0000133
bellard54936002003-05-13 00:25:15 +0000134typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000135 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000136 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000137 /* in order to optimize self modifying code, we count the number
138 of lookups we do to a given page to use a bitmap */
139 unsigned int code_write_count;
140 uint8_t *code_bitmap;
141#if defined(CONFIG_USER_ONLY)
142 unsigned long flags;
143#endif
bellard54936002003-05-13 00:25:15 +0000144} PageDesc;
145
bellard92e873b2004-05-21 14:52:29 +0000146typedef struct PhysPageDesc {
pbrook0f459d12008-06-09 00:20:13 +0000147 /* offset in host memory of the page + io_index in the low bits */
aurel3200f82b82008-04-27 21:12:55 +0000148 ram_addr_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000149} PhysPageDesc;
150
bellard54936002003-05-13 00:25:15 +0000151#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000152#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
153/* XXX: this is a temporary hack for alpha target.
154 * In the future, this is to be replaced by a multi-level table
155 * to actually be able to handle the complete 64 bits address space.
156 */
157#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
158#else
aurel3203875442008-04-22 20:45:18 +0000159#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000160#endif
bellard54936002003-05-13 00:25:15 +0000161
162#define L1_SIZE (1 << L1_BITS)
163#define L2_SIZE (1 << L2_BITS)
164
bellard83fb7ad2004-07-05 21:25:26 +0000165unsigned long qemu_real_host_page_size;
166unsigned long qemu_host_page_bits;
167unsigned long qemu_host_page_size;
168unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000169
bellard92e873b2004-05-21 14:52:29 +0000170/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000171static PageDesc *l1_map[L1_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +0000172static PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000173
pbrooke2eef172008-06-08 01:09:01 +0000174#if !defined(CONFIG_USER_ONLY)
175static void io_mem_init(void);
176
bellard33417e72003-08-10 21:47:01 +0000177/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000178CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
179CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000180void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000181static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000182static int io_mem_watch;
183#endif
bellard33417e72003-08-10 21:47:01 +0000184
bellard34865132003-10-05 14:28:56 +0000185/* log support */
blueswir1d9b630f2008-10-05 09:57:08 +0000186static const char *logfilename = "/tmp/qemu.log";
bellard34865132003-10-05 14:28:56 +0000187FILE *logfile;
188int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000189static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000190
bellarde3db7222005-01-26 22:00:47 +0000191/* statistics */
192static int tlb_flush_count;
193static int tb_flush_count;
194static int tb_phys_invalidate_count;
195
blueswir1db7b5422007-05-26 17:36:03 +0000196#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
197typedef struct subpage_t {
198 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000199 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
200 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
201 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000202} subpage_t;
203
bellard7cb69ca2008-05-10 10:55:51 +0000204#ifdef _WIN32
205static void map_exec(void *addr, long size)
206{
207 DWORD old_protect;
208 VirtualProtect(addr, size,
209 PAGE_EXECUTE_READWRITE, &old_protect);
210
211}
212#else
213static void map_exec(void *addr, long size)
214{
bellard43694152008-05-29 09:35:57 +0000215 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000216
bellard43694152008-05-29 09:35:57 +0000217 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000218 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000219 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000220
221 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000222 end += page_size - 1;
223 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000224
225 mprotect((void *)start, end - start,
226 PROT_READ | PROT_WRITE | PROT_EXEC);
227}
228#endif
229
bellardb346ff42003-06-15 20:05:50 +0000230static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000231{
bellard83fb7ad2004-07-05 21:25:26 +0000232 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000233 TARGET_PAGE_SIZE */
aliguori15ed71b2008-11-11 21:48:59 +0000234 qemu_real_host_page_size = qemu_getpagesize();
bellard83fb7ad2004-07-05 21:25:26 +0000235 if (qemu_host_page_size == 0)
236 qemu_host_page_size = qemu_real_host_page_size;
237 if (qemu_host_page_size < TARGET_PAGE_SIZE)
238 qemu_host_page_size = TARGET_PAGE_SIZE;
239 qemu_host_page_bits = 0;
240 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
241 qemu_host_page_bits++;
242 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000243 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
244 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000245
246#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
247 {
248 long long startaddr, endaddr;
249 FILE *f;
250 int n;
251
pbrookc8a706f2008-06-02 16:16:42 +0000252 mmap_lock();
pbrook07765902008-05-31 16:33:53 +0000253 last_brk = (unsigned long)sbrk(0);
balrog50a95692007-12-12 01:16:23 +0000254 f = fopen("/proc/self/maps", "r");
255 if (f) {
256 do {
257 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
258 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000259 startaddr = MIN(startaddr,
260 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
261 endaddr = MIN(endaddr,
262 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
pbrookb5fc9092008-05-29 13:56:10 +0000263 page_set_flags(startaddr & TARGET_PAGE_MASK,
balrog50a95692007-12-12 01:16:23 +0000264 TARGET_PAGE_ALIGN(endaddr),
265 PAGE_RESERVED);
266 }
267 } while (!feof(f));
268 fclose(f);
269 }
pbrookc8a706f2008-06-02 16:16:42 +0000270 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000271 }
272#endif
bellard54936002003-05-13 00:25:15 +0000273}
274
aliguori434929b2008-09-15 15:56:30 +0000275static inline PageDesc **page_l1_map(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000276{
pbrook17e23772008-06-09 13:47:45 +0000277#if TARGET_LONG_BITS > 32
278 /* Host memory outside guest VM. For 32-bit targets we have already
279 excluded high addresses. */
thsd8173e02008-08-29 13:10:00 +0000280 if (index > ((target_ulong)L2_SIZE * L1_SIZE))
pbrook17e23772008-06-09 13:47:45 +0000281 return NULL;
282#endif
aliguori434929b2008-09-15 15:56:30 +0000283 return &l1_map[index >> L2_BITS];
284}
285
286static inline PageDesc *page_find_alloc(target_ulong index)
287{
288 PageDesc **lp, *p;
289 lp = page_l1_map(index);
290 if (!lp)
291 return NULL;
292
bellard54936002003-05-13 00:25:15 +0000293 p = *lp;
294 if (!p) {
295 /* allocate if not found */
pbrook17e23772008-06-09 13:47:45 +0000296#if defined(CONFIG_USER_ONLY)
297 unsigned long addr;
298 size_t len = sizeof(PageDesc) * L2_SIZE;
299 /* Don't use qemu_malloc because it may recurse. */
300 p = mmap(0, len, PROT_READ | PROT_WRITE,
301 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
bellard54936002003-05-13 00:25:15 +0000302 *lp = p;
pbrook17e23772008-06-09 13:47:45 +0000303 addr = h2g(p);
304 if (addr == (target_ulong)addr) {
305 page_set_flags(addr & TARGET_PAGE_MASK,
306 TARGET_PAGE_ALIGN(addr + len),
307 PAGE_RESERVED);
308 }
309#else
310 p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
311 *lp = p;
312#endif
bellard54936002003-05-13 00:25:15 +0000313 }
314 return p + (index & (L2_SIZE - 1));
315}
316
aurel3200f82b82008-04-27 21:12:55 +0000317static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000318{
aliguori434929b2008-09-15 15:56:30 +0000319 PageDesc **lp, *p;
320 lp = page_l1_map(index);
321 if (!lp)
322 return NULL;
bellard54936002003-05-13 00:25:15 +0000323
aliguori434929b2008-09-15 15:56:30 +0000324 p = *lp;
bellard54936002003-05-13 00:25:15 +0000325 if (!p)
326 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000327 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000328}
329
bellard108c49b2005-07-24 12:55:09 +0000330static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000331{
bellard108c49b2005-07-24 12:55:09 +0000332 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000333 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000334
bellard108c49b2005-07-24 12:55:09 +0000335 p = (void **)l1_phys_map;
336#if TARGET_PHYS_ADDR_SPACE_BITS > 32
337
338#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
339#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
340#endif
341 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000342 p = *lp;
343 if (!p) {
344 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000345 if (!alloc)
346 return NULL;
347 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
348 memset(p, 0, sizeof(void *) * L1_SIZE);
349 *lp = p;
350 }
351#endif
352 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000353 pd = *lp;
354 if (!pd) {
355 int i;
bellard108c49b2005-07-24 12:55:09 +0000356 /* allocate if not found */
357 if (!alloc)
358 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000359 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
360 *lp = pd;
361 for (i = 0; i < L2_SIZE; i++)
362 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000363 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000364 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000365}
366
bellard108c49b2005-07-24 12:55:09 +0000367static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000368{
bellard108c49b2005-07-24 12:55:09 +0000369 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000370}
371
bellard9fa3e852004-01-04 18:06:42 +0000372#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000373static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000374static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000375 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000376#define mmap_lock() do { } while(0)
377#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000378#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000379
bellard43694152008-05-29 09:35:57 +0000380#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
381
382#if defined(CONFIG_USER_ONLY)
383/* Currently it is not recommanded to allocate big chunks of data in
384 user mode. It will change when a dedicated libc will be used */
385#define USE_STATIC_CODE_GEN_BUFFER
386#endif
387
388#ifdef USE_STATIC_CODE_GEN_BUFFER
389static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
390#endif
391
blueswir18fcd3692008-08-17 20:26:25 +0000392static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000393{
bellard43694152008-05-29 09:35:57 +0000394#ifdef USE_STATIC_CODE_GEN_BUFFER
395 code_gen_buffer = static_code_gen_buffer;
396 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
397 map_exec(code_gen_buffer, code_gen_buffer_size);
398#else
bellard26a5f132008-05-28 12:30:31 +0000399 code_gen_buffer_size = tb_size;
400 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000401#if defined(CONFIG_USER_ONLY)
402 /* in user mode, phys_ram_size is not meaningful */
403 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
404#else
bellard26a5f132008-05-28 12:30:31 +0000405 /* XXX: needs ajustments */
aliguori174a9a12008-09-24 14:10:36 +0000406 code_gen_buffer_size = (unsigned long)(phys_ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000407#endif
bellard26a5f132008-05-28 12:30:31 +0000408 }
409 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
410 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
411 /* The code gen buffer location may have constraints depending on
412 the host cpu and OS */
413#if defined(__linux__)
414 {
415 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000416 void *start = NULL;
417
bellard26a5f132008-05-28 12:30:31 +0000418 flags = MAP_PRIVATE | MAP_ANONYMOUS;
419#if defined(__x86_64__)
420 flags |= MAP_32BIT;
421 /* Cannot map more than that */
422 if (code_gen_buffer_size > (800 * 1024 * 1024))
423 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000424#elif defined(__sparc_v9__)
425 // Map the buffer below 2G, so we can use direct calls and branches
426 flags |= MAP_FIXED;
427 start = (void *) 0x60000000UL;
428 if (code_gen_buffer_size > (512 * 1024 * 1024))
429 code_gen_buffer_size = (512 * 1024 * 1024);
bellard26a5f132008-05-28 12:30:31 +0000430#endif
blueswir1141ac462008-07-26 15:05:57 +0000431 code_gen_buffer = mmap(start, code_gen_buffer_size,
432 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000433 flags, -1, 0);
434 if (code_gen_buffer == MAP_FAILED) {
435 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
436 exit(1);
437 }
438 }
aliguori06e67a82008-09-27 15:32:41 +0000439#elif defined(__FreeBSD__)
440 {
441 int flags;
442 void *addr = NULL;
443 flags = MAP_PRIVATE | MAP_ANONYMOUS;
444#if defined(__x86_64__)
445 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
446 * 0x40000000 is free */
447 flags |= MAP_FIXED;
448 addr = (void *)0x40000000;
449 /* Cannot map more than that */
450 if (code_gen_buffer_size > (800 * 1024 * 1024))
451 code_gen_buffer_size = (800 * 1024 * 1024);
452#endif
453 code_gen_buffer = mmap(addr, code_gen_buffer_size,
454 PROT_WRITE | PROT_READ | PROT_EXEC,
455 flags, -1, 0);
456 if (code_gen_buffer == MAP_FAILED) {
457 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
458 exit(1);
459 }
460 }
bellard26a5f132008-05-28 12:30:31 +0000461#else
462 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
463 if (!code_gen_buffer) {
464 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
465 exit(1);
466 }
467 map_exec(code_gen_buffer, code_gen_buffer_size);
468#endif
bellard43694152008-05-29 09:35:57 +0000469#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000470 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
471 code_gen_buffer_max_size = code_gen_buffer_size -
472 code_gen_max_block_size();
473 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
474 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
475}
476
477/* Must be called before using the QEMU cpus. 'tb_size' is the size
478 (in bytes) allocated to the translation buffer. Zero means default
479 size. */
480void cpu_exec_init_all(unsigned long tb_size)
481{
bellard26a5f132008-05-28 12:30:31 +0000482 cpu_gen_init();
483 code_gen_alloc(tb_size);
484 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000485 page_init();
pbrooke2eef172008-06-08 01:09:01 +0000486#if !defined(CONFIG_USER_ONLY)
bellard26a5f132008-05-28 12:30:31 +0000487 io_mem_init();
pbrooke2eef172008-06-08 01:09:01 +0000488#endif
bellard26a5f132008-05-28 12:30:31 +0000489}
490
pbrook9656f322008-07-01 20:01:19 +0000491#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
492
493#define CPU_COMMON_SAVE_VERSION 1
494
495static void cpu_common_save(QEMUFile *f, void *opaque)
496{
497 CPUState *env = opaque;
498
499 qemu_put_be32s(f, &env->halted);
500 qemu_put_be32s(f, &env->interrupt_request);
501}
502
503static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
504{
505 CPUState *env = opaque;
506
507 if (version_id != CPU_COMMON_SAVE_VERSION)
508 return -EINVAL;
509
510 qemu_get_be32s(f, &env->halted);
pbrook75f482a2008-07-01 21:53:33 +0000511 qemu_get_be32s(f, &env->interrupt_request);
pbrook9656f322008-07-01 20:01:19 +0000512 tlb_flush(env, 1);
513
514 return 0;
515}
516#endif
517
bellard6a00d602005-11-21 23:25:50 +0000518void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000519{
bellard6a00d602005-11-21 23:25:50 +0000520 CPUState **penv;
521 int cpu_index;
522
bellard6a00d602005-11-21 23:25:50 +0000523 env->next_cpu = NULL;
524 penv = &first_cpu;
525 cpu_index = 0;
526 while (*penv != NULL) {
527 penv = (CPUState **)&(*penv)->next_cpu;
528 cpu_index++;
529 }
530 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000531 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000532 *penv = env;
pbrookb3c77242008-06-30 16:31:04 +0000533#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
pbrook9656f322008-07-01 20:01:19 +0000534 register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
535 cpu_common_save, cpu_common_load, env);
pbrookb3c77242008-06-30 16:31:04 +0000536 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
537 cpu_save, cpu_load, env);
538#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000539}
540
bellard9fa3e852004-01-04 18:06:42 +0000541static inline void invalidate_page_bitmap(PageDesc *p)
542{
543 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000544 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000545 p->code_bitmap = NULL;
546 }
547 p->code_write_count = 0;
548}
549
bellardfd6ce8f2003-05-14 19:00:11 +0000550/* set to NULL all the 'first_tb' fields in all PageDescs */
551static void page_flush_tb(void)
552{
553 int i, j;
554 PageDesc *p;
555
556 for(i = 0; i < L1_SIZE; i++) {
557 p = l1_map[i];
558 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000559 for(j = 0; j < L2_SIZE; j++) {
560 p->first_tb = NULL;
561 invalidate_page_bitmap(p);
562 p++;
563 }
bellardfd6ce8f2003-05-14 19:00:11 +0000564 }
565 }
566}
567
568/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000569/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000570void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000571{
bellard6a00d602005-11-21 23:25:50 +0000572 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000573#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000574 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
575 (unsigned long)(code_gen_ptr - code_gen_buffer),
576 nb_tbs, nb_tbs > 0 ?
577 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000578#endif
bellard26a5f132008-05-28 12:30:31 +0000579 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000580 cpu_abort(env1, "Internal error: code buffer overflow\n");
581
bellardfd6ce8f2003-05-14 19:00:11 +0000582 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000583
bellard6a00d602005-11-21 23:25:50 +0000584 for(env = first_cpu; env != NULL; env = env->next_cpu) {
585 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
586 }
bellard9fa3e852004-01-04 18:06:42 +0000587
bellard8a8a6082004-10-03 13:36:49 +0000588 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000589 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000590
bellardfd6ce8f2003-05-14 19:00:11 +0000591 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000592 /* XXX: flush processor icache at this point if cache flush is
593 expensive */
bellarde3db7222005-01-26 22:00:47 +0000594 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000595}
596
597#ifdef DEBUG_TB_CHECK
598
j_mayerbc98a7e2007-04-04 07:55:12 +0000599static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000600{
601 TranslationBlock *tb;
602 int i;
603 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000604 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
605 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000606 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
607 address >= tb->pc + tb->size)) {
608 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000609 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000610 }
611 }
612 }
613}
614
615/* verify that all the pages have correct rights for code */
616static void tb_page_check(void)
617{
618 TranslationBlock *tb;
619 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000620
pbrook99773bd2006-04-16 15:14:59 +0000621 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
622 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000623 flags1 = page_get_flags(tb->pc);
624 flags2 = page_get_flags(tb->pc + tb->size - 1);
625 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
626 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000627 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000628 }
629 }
630 }
631}
632
blueswir1bdaf78e2008-10-04 07:24:27 +0000633static void tb_jmp_check(TranslationBlock *tb)
bellardd4e81642003-05-25 16:46:15 +0000634{
635 TranslationBlock *tb1;
636 unsigned int n1;
637
638 /* suppress any remaining jumps to this TB */
639 tb1 = tb->jmp_first;
640 for(;;) {
641 n1 = (long)tb1 & 3;
642 tb1 = (TranslationBlock *)((long)tb1 & ~3);
643 if (n1 == 2)
644 break;
645 tb1 = tb1->jmp_next[n1];
646 }
647 /* check end of list */
648 if (tb1 != tb) {
649 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
650 }
651}
652
bellardfd6ce8f2003-05-14 19:00:11 +0000653#endif
654
655/* invalidate one TB */
656static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
657 int next_offset)
658{
659 TranslationBlock *tb1;
660 for(;;) {
661 tb1 = *ptb;
662 if (tb1 == tb) {
663 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
664 break;
665 }
666 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
667 }
668}
669
bellard9fa3e852004-01-04 18:06:42 +0000670static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
671{
672 TranslationBlock *tb1;
673 unsigned int n1;
674
675 for(;;) {
676 tb1 = *ptb;
677 n1 = (long)tb1 & 3;
678 tb1 = (TranslationBlock *)((long)tb1 & ~3);
679 if (tb1 == tb) {
680 *ptb = tb1->page_next[n1];
681 break;
682 }
683 ptb = &tb1->page_next[n1];
684 }
685}
686
bellardd4e81642003-05-25 16:46:15 +0000687static inline void tb_jmp_remove(TranslationBlock *tb, int n)
688{
689 TranslationBlock *tb1, **ptb;
690 unsigned int n1;
691
692 ptb = &tb->jmp_next[n];
693 tb1 = *ptb;
694 if (tb1) {
695 /* find tb(n) in circular list */
696 for(;;) {
697 tb1 = *ptb;
698 n1 = (long)tb1 & 3;
699 tb1 = (TranslationBlock *)((long)tb1 & ~3);
700 if (n1 == n && tb1 == tb)
701 break;
702 if (n1 == 2) {
703 ptb = &tb1->jmp_first;
704 } else {
705 ptb = &tb1->jmp_next[n1];
706 }
707 }
708 /* now we can suppress tb(n) from the list */
709 *ptb = tb->jmp_next[n];
710
711 tb->jmp_next[n] = NULL;
712 }
713}
714
715/* reset the jump entry 'n' of a TB so that it is not chained to
716 another TB */
717static inline void tb_reset_jump(TranslationBlock *tb, int n)
718{
719 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
720}
721
pbrook2e70f6e2008-06-29 01:03:05 +0000722void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000723{
bellard6a00d602005-11-21 23:25:50 +0000724 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000725 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000726 unsigned int h, n1;
aurel3200f82b82008-04-27 21:12:55 +0000727 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000728 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000729
bellard9fa3e852004-01-04 18:06:42 +0000730 /* remove the TB from the hash list */
731 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
732 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000733 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000734 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000735
bellard9fa3e852004-01-04 18:06:42 +0000736 /* remove the TB from the page list */
737 if (tb->page_addr[0] != page_addr) {
738 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
739 tb_page_remove(&p->first_tb, tb);
740 invalidate_page_bitmap(p);
741 }
742 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
743 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
744 tb_page_remove(&p->first_tb, tb);
745 invalidate_page_bitmap(p);
746 }
747
bellard8a40a182005-11-20 10:35:40 +0000748 tb_invalidated_flag = 1;
749
750 /* remove the TB from the hash list */
751 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000752 for(env = first_cpu; env != NULL; env = env->next_cpu) {
753 if (env->tb_jmp_cache[h] == tb)
754 env->tb_jmp_cache[h] = NULL;
755 }
bellard8a40a182005-11-20 10:35:40 +0000756
757 /* suppress this TB from the two jump lists */
758 tb_jmp_remove(tb, 0);
759 tb_jmp_remove(tb, 1);
760
761 /* suppress any remaining jumps to this TB */
762 tb1 = tb->jmp_first;
763 for(;;) {
764 n1 = (long)tb1 & 3;
765 if (n1 == 2)
766 break;
767 tb1 = (TranslationBlock *)((long)tb1 & ~3);
768 tb2 = tb1->jmp_next[n1];
769 tb_reset_jump(tb1, n1);
770 tb1->jmp_next[n1] = NULL;
771 tb1 = tb2;
772 }
773 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
774
bellarde3db7222005-01-26 22:00:47 +0000775 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000776}
777
778static inline void set_bits(uint8_t *tab, int start, int len)
779{
780 int end, mask, end1;
781
782 end = start + len;
783 tab += start >> 3;
784 mask = 0xff << (start & 7);
785 if ((start & ~7) == (end & ~7)) {
786 if (start < end) {
787 mask &= ~(0xff << (end & 7));
788 *tab |= mask;
789 }
790 } else {
791 *tab++ |= mask;
792 start = (start + 8) & ~7;
793 end1 = end & ~7;
794 while (start < end1) {
795 *tab++ = 0xff;
796 start += 8;
797 }
798 if (start < end) {
799 mask = ~(0xff << (end & 7));
800 *tab |= mask;
801 }
802 }
803}
804
805static void build_page_bitmap(PageDesc *p)
806{
807 int n, tb_start, tb_end;
808 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000809
pbrookb2a70812008-06-09 13:57:23 +0000810 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000811 if (!p->code_bitmap)
812 return;
bellard9fa3e852004-01-04 18:06:42 +0000813
814 tb = p->first_tb;
815 while (tb != NULL) {
816 n = (long)tb & 3;
817 tb = (TranslationBlock *)((long)tb & ~3);
818 /* NOTE: this is subtle as a TB may span two physical pages */
819 if (n == 0) {
820 /* NOTE: tb_end may be after the end of the page, but
821 it is not a problem */
822 tb_start = tb->pc & ~TARGET_PAGE_MASK;
823 tb_end = tb_start + tb->size;
824 if (tb_end > TARGET_PAGE_SIZE)
825 tb_end = TARGET_PAGE_SIZE;
826 } else {
827 tb_start = 0;
828 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
829 }
830 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
831 tb = tb->page_next[n];
832 }
833}
834
pbrook2e70f6e2008-06-29 01:03:05 +0000835TranslationBlock *tb_gen_code(CPUState *env,
836 target_ulong pc, target_ulong cs_base,
837 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000838{
839 TranslationBlock *tb;
840 uint8_t *tc_ptr;
841 target_ulong phys_pc, phys_page2, virt_page2;
842 int code_gen_size;
843
bellardc27004e2005-01-03 23:35:10 +0000844 phys_pc = get_phys_addr_code(env, pc);
845 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000846 if (!tb) {
847 /* flush must be done */
848 tb_flush(env);
849 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000850 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000851 /* Don't forget to invalidate previous TB info. */
852 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000853 }
854 tc_ptr = code_gen_ptr;
855 tb->tc_ptr = tc_ptr;
856 tb->cs_base = cs_base;
857 tb->flags = flags;
858 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000859 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000860 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 +0000861
bellardd720b932004-04-25 17:57:43 +0000862 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000863 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000864 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000865 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000866 phys_page2 = get_phys_addr_code(env, virt_page2);
867 }
868 tb_link_phys(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +0000869 return tb;
bellardd720b932004-04-25 17:57:43 +0000870}
ths3b46e622007-09-17 08:09:54 +0000871
bellard9fa3e852004-01-04 18:06:42 +0000872/* invalidate all TBs which intersect with the target physical page
873 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000874 the same physical page. 'is_cpu_write_access' should be true if called
875 from a real cpu write access: the virtual CPU will exit the current
876 TB if code is modified inside this TB. */
aurel3200f82b82008-04-27 21:12:55 +0000877void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000878 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000879{
bellardd720b932004-04-25 17:57:43 +0000880 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000881 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000882 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000883 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000884 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000885 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000886
887 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000888 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000889 return;
ths5fafdf22007-09-16 21:08:06 +0000890 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000891 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
892 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000893 /* build code bitmap */
894 build_page_bitmap(p);
895 }
896
897 /* we remove all the TBs in the range [start, end[ */
898 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000899 current_tb_not_found = is_cpu_write_access;
900 current_tb_modified = 0;
901 current_tb = NULL; /* avoid warning */
902 current_pc = 0; /* avoid warning */
903 current_cs_base = 0; /* avoid warning */
904 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000905 tb = p->first_tb;
906 while (tb != NULL) {
907 n = (long)tb & 3;
908 tb = (TranslationBlock *)((long)tb & ~3);
909 tb_next = tb->page_next[n];
910 /* NOTE: this is subtle as a TB may span two physical pages */
911 if (n == 0) {
912 /* NOTE: tb_end may be after the end of the page, but
913 it is not a problem */
914 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
915 tb_end = tb_start + tb->size;
916 } else {
917 tb_start = tb->page_addr[1];
918 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
919 }
920 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000921#ifdef TARGET_HAS_PRECISE_SMC
922 if (current_tb_not_found) {
923 current_tb_not_found = 0;
924 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000925 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +0000926 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +0000927 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +0000928 }
929 }
930 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +0000931 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +0000932 /* If we are modifying the current TB, we must stop
933 its execution. We could be more precise by checking
934 that the modification is after the current PC, but it
935 would require a specialized function to partially
936 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000937
bellardd720b932004-04-25 17:57:43 +0000938 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000939 cpu_restore_state(current_tb, env,
pbrook2e70f6e2008-06-29 01:03:05 +0000940 env->mem_io_pc, NULL);
bellardd720b932004-04-25 17:57:43 +0000941#if defined(TARGET_I386)
942 current_flags = env->hflags;
943 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
944 current_cs_base = (target_ulong)env->segs[R_CS].base;
945 current_pc = current_cs_base + env->eip;
946#else
947#error unsupported CPU
948#endif
949 }
950#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000951 /* we need to do that to handle the case where a signal
952 occurs while doing tb_phys_invalidate() */
953 saved_tb = NULL;
954 if (env) {
955 saved_tb = env->current_tb;
956 env->current_tb = NULL;
957 }
bellard9fa3e852004-01-04 18:06:42 +0000958 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000959 if (env) {
960 env->current_tb = saved_tb;
961 if (env->interrupt_request && env->current_tb)
962 cpu_interrupt(env, env->interrupt_request);
963 }
bellard9fa3e852004-01-04 18:06:42 +0000964 }
965 tb = tb_next;
966 }
967#if !defined(CONFIG_USER_ONLY)
968 /* if no code remaining, no need to continue to use slow writes */
969 if (!p->first_tb) {
970 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000971 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +0000972 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +0000973 }
974 }
975#endif
976#ifdef TARGET_HAS_PRECISE_SMC
977 if (current_tb_modified) {
978 /* we generate a block containing just the instruction
979 modifying the memory. It will ensure that it cannot modify
980 itself */
bellardea1c1802004-06-14 18:56:36 +0000981 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000982 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +0000983 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000984 }
985#endif
986}
987
988/* len must be <= 8 and start must be a multiple of len */
aurel3200f82b82008-04-27 21:12:55 +0000989static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000990{
991 PageDesc *p;
992 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000993#if 0
bellarda4193c82004-06-03 14:01:43 +0000994 if (1) {
995 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000996 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
pbrook2e70f6e2008-06-29 01:03:05 +0000997 cpu_single_env->mem_io_vaddr, len,
ths5fafdf22007-09-16 21:08:06 +0000998 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000999 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
1000 }
bellard59817cc2004-02-16 22:01:13 +00001001 }
1002#endif
bellard9fa3e852004-01-04 18:06:42 +00001003 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001004 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001005 return;
1006 if (p->code_bitmap) {
1007 offset = start & ~TARGET_PAGE_MASK;
1008 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1009 if (b & ((1 << len) - 1))
1010 goto do_invalidate;
1011 } else {
1012 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001013 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001014 }
1015}
1016
bellard9fa3e852004-01-04 18:06:42 +00001017#if !defined(CONFIG_SOFTMMU)
aurel3200f82b82008-04-27 21:12:55 +00001018static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001019 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001020{
bellardd720b932004-04-25 17:57:43 +00001021 int n, current_flags, current_tb_modified;
1022 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +00001023 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001024 TranslationBlock *tb, *current_tb;
1025#ifdef TARGET_HAS_PRECISE_SMC
1026 CPUState *env = cpu_single_env;
1027#endif
bellard9fa3e852004-01-04 18:06:42 +00001028
1029 addr &= TARGET_PAGE_MASK;
1030 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001031 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001032 return;
1033 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001034 current_tb_modified = 0;
1035 current_tb = NULL;
1036 current_pc = 0; /* avoid warning */
1037 current_cs_base = 0; /* avoid warning */
1038 current_flags = 0; /* avoid warning */
1039#ifdef TARGET_HAS_PRECISE_SMC
1040 if (tb && pc != 0) {
1041 current_tb = tb_find_pc(pc);
1042 }
1043#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001044 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001045 n = (long)tb & 3;
1046 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001047#ifdef TARGET_HAS_PRECISE_SMC
1048 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001049 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001050 /* If we are modifying the current TB, we must stop
1051 its execution. We could be more precise by checking
1052 that the modification is after the current PC, but it
1053 would require a specialized function to partially
1054 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001055
bellardd720b932004-04-25 17:57:43 +00001056 current_tb_modified = 1;
1057 cpu_restore_state(current_tb, env, pc, puc);
1058#if defined(TARGET_I386)
1059 current_flags = env->hflags;
1060 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
1061 current_cs_base = (target_ulong)env->segs[R_CS].base;
1062 current_pc = current_cs_base + env->eip;
1063#else
1064#error unsupported CPU
1065#endif
1066 }
1067#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001068 tb_phys_invalidate(tb, addr);
1069 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001070 }
1071 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001072#ifdef TARGET_HAS_PRECISE_SMC
1073 if (current_tb_modified) {
1074 /* we generate a block containing just the instruction
1075 modifying the memory. It will ensure that it cannot modify
1076 itself */
bellardea1c1802004-06-14 18:56:36 +00001077 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001078 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001079 cpu_resume_from_signal(env, puc);
1080 }
1081#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001082}
bellard9fa3e852004-01-04 18:06:42 +00001083#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001084
1085/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001086static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +00001087 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001088{
1089 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +00001090 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001091
bellard9fa3e852004-01-04 18:06:42 +00001092 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +00001093 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001094 tb->page_next[n] = p->first_tb;
1095 last_first_tb = p->first_tb;
1096 p->first_tb = (TranslationBlock *)((long)tb | n);
1097 invalidate_page_bitmap(p);
1098
bellard107db442004-06-22 18:48:46 +00001099#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001100
bellard9fa3e852004-01-04 18:06:42 +00001101#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001102 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001103 target_ulong addr;
1104 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001105 int prot;
1106
bellardfd6ce8f2003-05-14 19:00:11 +00001107 /* force the host page as non writable (writes will have a
1108 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001109 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001110 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001111 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1112 addr += TARGET_PAGE_SIZE) {
1113
1114 p2 = page_find (addr >> TARGET_PAGE_BITS);
1115 if (!p2)
1116 continue;
1117 prot |= p2->flags;
1118 p2->flags &= ~PAGE_WRITE;
1119 page_get_flags(addr);
1120 }
ths5fafdf22007-09-16 21:08:06 +00001121 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001122 (prot & PAGE_BITS) & ~PAGE_WRITE);
1123#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001124 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001125 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001126#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001127 }
bellard9fa3e852004-01-04 18:06:42 +00001128#else
1129 /* if some code is already present, then the pages are already
1130 protected. So we handle the case where only the first TB is
1131 allocated in a physical page */
1132 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001133 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001134 }
1135#endif
bellardd720b932004-04-25 17:57:43 +00001136
1137#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001138}
1139
1140/* Allocate a new translation block. Flush the translation buffer if
1141 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001142TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001143{
1144 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001145
bellard26a5f132008-05-28 12:30:31 +00001146 if (nb_tbs >= code_gen_max_blocks ||
1147 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001148 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001149 tb = &tbs[nb_tbs++];
1150 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001151 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001152 return tb;
1153}
1154
pbrook2e70f6e2008-06-29 01:03:05 +00001155void tb_free(TranslationBlock *tb)
1156{
thsbf20dc02008-06-30 17:22:19 +00001157 /* In practice this is mostly used for single use temporary TB
pbrook2e70f6e2008-06-29 01:03:05 +00001158 Ignore the hard cases and just back up if this TB happens to
1159 be the last one generated. */
1160 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1161 code_gen_ptr = tb->tc_ptr;
1162 nb_tbs--;
1163 }
1164}
1165
bellard9fa3e852004-01-04 18:06:42 +00001166/* add a new TB and link it to the physical page tables. phys_page2 is
1167 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001168void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001169 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001170{
bellard9fa3e852004-01-04 18:06:42 +00001171 unsigned int h;
1172 TranslationBlock **ptb;
1173
pbrookc8a706f2008-06-02 16:16:42 +00001174 /* Grab the mmap lock to stop another thread invalidating this TB
1175 before we are done. */
1176 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001177 /* add in the physical hash table */
1178 h = tb_phys_hash_func(phys_pc);
1179 ptb = &tb_phys_hash[h];
1180 tb->phys_hash_next = *ptb;
1181 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001182
1183 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001184 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1185 if (phys_page2 != -1)
1186 tb_alloc_page(tb, 1, phys_page2);
1187 else
1188 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001189
bellardd4e81642003-05-25 16:46:15 +00001190 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1191 tb->jmp_next[0] = NULL;
1192 tb->jmp_next[1] = NULL;
1193
1194 /* init original jump addresses */
1195 if (tb->tb_next_offset[0] != 0xffff)
1196 tb_reset_jump(tb, 0);
1197 if (tb->tb_next_offset[1] != 0xffff)
1198 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001199
1200#ifdef DEBUG_TB_CHECK
1201 tb_page_check();
1202#endif
pbrookc8a706f2008-06-02 16:16:42 +00001203 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001204}
1205
bellarda513fe12003-05-27 23:29:48 +00001206/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1207 tb[1].tc_ptr. Return NULL if not found */
1208TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1209{
1210 int m_min, m_max, m;
1211 unsigned long v;
1212 TranslationBlock *tb;
1213
1214 if (nb_tbs <= 0)
1215 return NULL;
1216 if (tc_ptr < (unsigned long)code_gen_buffer ||
1217 tc_ptr >= (unsigned long)code_gen_ptr)
1218 return NULL;
1219 /* binary search (cf Knuth) */
1220 m_min = 0;
1221 m_max = nb_tbs - 1;
1222 while (m_min <= m_max) {
1223 m = (m_min + m_max) >> 1;
1224 tb = &tbs[m];
1225 v = (unsigned long)tb->tc_ptr;
1226 if (v == tc_ptr)
1227 return tb;
1228 else if (tc_ptr < v) {
1229 m_max = m - 1;
1230 } else {
1231 m_min = m + 1;
1232 }
ths5fafdf22007-09-16 21:08:06 +00001233 }
bellarda513fe12003-05-27 23:29:48 +00001234 return &tbs[m_max];
1235}
bellard75012672003-06-21 13:11:07 +00001236
bellardea041c02003-06-25 16:16:50 +00001237static void tb_reset_jump_recursive(TranslationBlock *tb);
1238
1239static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1240{
1241 TranslationBlock *tb1, *tb_next, **ptb;
1242 unsigned int n1;
1243
1244 tb1 = tb->jmp_next[n];
1245 if (tb1 != NULL) {
1246 /* find head of list */
1247 for(;;) {
1248 n1 = (long)tb1 & 3;
1249 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1250 if (n1 == 2)
1251 break;
1252 tb1 = tb1->jmp_next[n1];
1253 }
1254 /* we are now sure now that tb jumps to tb1 */
1255 tb_next = tb1;
1256
1257 /* remove tb from the jmp_first list */
1258 ptb = &tb_next->jmp_first;
1259 for(;;) {
1260 tb1 = *ptb;
1261 n1 = (long)tb1 & 3;
1262 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1263 if (n1 == n && tb1 == tb)
1264 break;
1265 ptb = &tb1->jmp_next[n1];
1266 }
1267 *ptb = tb->jmp_next[n];
1268 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001269
bellardea041c02003-06-25 16:16:50 +00001270 /* suppress the jump to next tb in generated code */
1271 tb_reset_jump(tb, n);
1272
bellard01243112004-01-04 15:48:17 +00001273 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001274 tb_reset_jump_recursive(tb_next);
1275 }
1276}
1277
1278static void tb_reset_jump_recursive(TranslationBlock *tb)
1279{
1280 tb_reset_jump_recursive2(tb, 0);
1281 tb_reset_jump_recursive2(tb, 1);
1282}
1283
bellard1fddef42005-04-17 19:16:13 +00001284#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001285static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1286{
j_mayer9b3c35e2007-04-07 11:21:28 +00001287 target_phys_addr_t addr;
1288 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001289 ram_addr_t ram_addr;
1290 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001291
pbrookc2f07f82006-04-08 17:14:56 +00001292 addr = cpu_get_phys_page_debug(env, pc);
1293 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1294 if (!p) {
1295 pd = IO_MEM_UNASSIGNED;
1296 } else {
1297 pd = p->phys_offset;
1298 }
1299 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001300 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001301}
bellardc27004e2005-01-03 23:35:10 +00001302#endif
bellardd720b932004-04-25 17:57:43 +00001303
pbrook6658ffb2007-03-16 23:58:11 +00001304/* Add a watchpoint. */
pbrook0f459d12008-06-09 00:20:13 +00001305int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type)
pbrook6658ffb2007-03-16 23:58:11 +00001306{
1307 int i;
1308
1309 for (i = 0; i < env->nb_watchpoints; i++) {
1310 if (addr == env->watchpoint[i].vaddr)
1311 return 0;
1312 }
1313 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1314 return -1;
1315
1316 i = env->nb_watchpoints++;
1317 env->watchpoint[i].vaddr = addr;
pbrook0f459d12008-06-09 00:20:13 +00001318 env->watchpoint[i].type = type;
pbrook6658ffb2007-03-16 23:58:11 +00001319 tlb_flush_page(env, addr);
1320 /* FIXME: This flush is needed because of the hack to make memory ops
1321 terminate the TB. It can be removed once the proper IO trap and
1322 re-execute bits are in. */
1323 tb_flush(env);
1324 return i;
1325}
1326
1327/* Remove a watchpoint. */
1328int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1329{
1330 int i;
1331
1332 for (i = 0; i < env->nb_watchpoints; i++) {
1333 if (addr == env->watchpoint[i].vaddr) {
1334 env->nb_watchpoints--;
1335 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1336 tlb_flush_page(env, addr);
1337 return 0;
1338 }
1339 }
1340 return -1;
1341}
1342
edgar_igl7d03f822008-05-17 18:58:29 +00001343/* Remove all watchpoints. */
1344void cpu_watchpoint_remove_all(CPUState *env) {
1345 int i;
1346
1347 for (i = 0; i < env->nb_watchpoints; i++) {
1348 tlb_flush_page(env, env->watchpoint[i].vaddr);
1349 }
1350 env->nb_watchpoints = 0;
1351}
1352
bellardc33a3462003-07-29 20:50:33 +00001353/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1354 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001355int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001356{
bellard1fddef42005-04-17 19:16:13 +00001357#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001358 int i;
ths3b46e622007-09-17 08:09:54 +00001359
bellard4c3a88a2003-07-26 12:06:08 +00001360 for(i = 0; i < env->nb_breakpoints; i++) {
1361 if (env->breakpoints[i] == pc)
1362 return 0;
1363 }
1364
1365 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1366 return -1;
1367 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001368
bellardd720b932004-04-25 17:57:43 +00001369 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001370 return 0;
1371#else
1372 return -1;
1373#endif
1374}
1375
edgar_igl7d03f822008-05-17 18:58:29 +00001376/* remove all breakpoints */
1377void cpu_breakpoint_remove_all(CPUState *env) {
1378#if defined(TARGET_HAS_ICE)
1379 int i;
1380 for(i = 0; i < env->nb_breakpoints; i++) {
1381 breakpoint_invalidate(env, env->breakpoints[i]);
1382 }
1383 env->nb_breakpoints = 0;
1384#endif
1385}
1386
bellard4c3a88a2003-07-26 12:06:08 +00001387/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001388int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001389{
bellard1fddef42005-04-17 19:16:13 +00001390#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001391 int i;
1392 for(i = 0; i < env->nb_breakpoints; i++) {
1393 if (env->breakpoints[i] == pc)
1394 goto found;
1395 }
1396 return -1;
1397 found:
bellard4c3a88a2003-07-26 12:06:08 +00001398 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001399 if (i < env->nb_breakpoints)
1400 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001401
1402 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001403 return 0;
1404#else
1405 return -1;
1406#endif
1407}
1408
bellardc33a3462003-07-29 20:50:33 +00001409/* enable or disable single step mode. EXCP_DEBUG is returned by the
1410 CPU loop after each instruction */
1411void cpu_single_step(CPUState *env, int enabled)
1412{
bellard1fddef42005-04-17 19:16:13 +00001413#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001414 if (env->singlestep_enabled != enabled) {
1415 env->singlestep_enabled = enabled;
1416 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001417 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001418 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001419 }
1420#endif
1421}
1422
bellard34865132003-10-05 14:28:56 +00001423/* enable or disable low levels log */
1424void cpu_set_log(int log_flags)
1425{
1426 loglevel = log_flags;
1427 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001428 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001429 if (!logfile) {
1430 perror(logfilename);
1431 _exit(1);
1432 }
bellard9fa3e852004-01-04 18:06:42 +00001433#if !defined(CONFIG_SOFTMMU)
1434 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1435 {
blueswir1b55266b2008-09-20 08:07:15 +00001436 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001437 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1438 }
1439#else
bellard34865132003-10-05 14:28:56 +00001440 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001441#endif
pbrooke735b912007-06-30 13:53:24 +00001442 log_append = 1;
1443 }
1444 if (!loglevel && logfile) {
1445 fclose(logfile);
1446 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001447 }
1448}
1449
1450void cpu_set_log_filename(const char *filename)
1451{
1452 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001453 if (logfile) {
1454 fclose(logfile);
1455 logfile = NULL;
1456 }
1457 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001458}
bellardc33a3462003-07-29 20:50:33 +00001459
bellard01243112004-01-04 15:48:17 +00001460/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001461void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001462{
pbrookd5975362008-06-07 20:50:51 +00001463#if !defined(USE_NPTL)
bellardea041c02003-06-25 16:16:50 +00001464 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001465 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
pbrookd5975362008-06-07 20:50:51 +00001466#endif
pbrook2e70f6e2008-06-29 01:03:05 +00001467 int old_mask;
bellard59817cc2004-02-16 22:01:13 +00001468
pbrook2e70f6e2008-06-29 01:03:05 +00001469 old_mask = env->interrupt_request;
pbrookd5975362008-06-07 20:50:51 +00001470 /* FIXME: This is probably not threadsafe. A different thread could
thsbf20dc02008-06-30 17:22:19 +00001471 be in the middle of a read-modify-write operation. */
bellard68a79312003-06-30 13:12:32 +00001472 env->interrupt_request |= mask;
pbrookd5975362008-06-07 20:50:51 +00001473#if defined(USE_NPTL)
1474 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1475 problem and hope the cpu will stop of its own accord. For userspace
1476 emulation this often isn't actually as bad as it sounds. Often
1477 signals are used primarily to interrupt blocking syscalls. */
1478#else
pbrook2e70f6e2008-06-29 01:03:05 +00001479 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001480 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001481#ifndef CONFIG_USER_ONLY
1482 /* CPU_INTERRUPT_EXIT isn't a real interrupt. It just means
1483 an async event happened and we need to process it. */
1484 if (!can_do_io(env)
1485 && (mask & ~(old_mask | CPU_INTERRUPT_EXIT)) != 0) {
1486 cpu_abort(env, "Raised interrupt while not in I/O function");
1487 }
1488#endif
1489 } else {
1490 tb = env->current_tb;
1491 /* if the cpu is currently executing code, we must unlink it and
1492 all the potentially executing TB */
1493 if (tb && !testandset(&interrupt_lock)) {
1494 env->current_tb = NULL;
1495 tb_reset_jump_recursive(tb);
1496 resetlock(&interrupt_lock);
1497 }
bellardea041c02003-06-25 16:16:50 +00001498 }
pbrookd5975362008-06-07 20:50:51 +00001499#endif
bellardea041c02003-06-25 16:16:50 +00001500}
1501
bellardb54ad042004-05-20 13:42:52 +00001502void cpu_reset_interrupt(CPUState *env, int mask)
1503{
1504 env->interrupt_request &= ~mask;
1505}
1506
blueswir1c7cd6a32008-10-02 18:27:46 +00001507const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001508 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001509 "show generated host assembly code for each compiled TB" },
1510 { CPU_LOG_TB_IN_ASM, "in_asm",
1511 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001512 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001513 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001514 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001515 "show micro ops "
1516#ifdef TARGET_I386
1517 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001518#endif
blueswir1e01a1152008-03-14 17:37:11 +00001519 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001520 { CPU_LOG_INT, "int",
1521 "show interrupts/exceptions in short format" },
1522 { CPU_LOG_EXEC, "exec",
1523 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001524 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001525 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001526#ifdef TARGET_I386
1527 { CPU_LOG_PCALL, "pcall",
1528 "show protected mode far calls/returns/exceptions" },
1529#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001530#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001531 { CPU_LOG_IOPORT, "ioport",
1532 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001533#endif
bellardf193c792004-03-21 17:06:25 +00001534 { 0, NULL, NULL },
1535};
1536
1537static int cmp1(const char *s1, int n, const char *s2)
1538{
1539 if (strlen(s2) != n)
1540 return 0;
1541 return memcmp(s1, s2, n) == 0;
1542}
ths3b46e622007-09-17 08:09:54 +00001543
bellardf193c792004-03-21 17:06:25 +00001544/* takes a comma separated list of log masks. Return 0 if error. */
1545int cpu_str_to_log_mask(const char *str)
1546{
blueswir1c7cd6a32008-10-02 18:27:46 +00001547 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001548 int mask;
1549 const char *p, *p1;
1550
1551 p = str;
1552 mask = 0;
1553 for(;;) {
1554 p1 = strchr(p, ',');
1555 if (!p1)
1556 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001557 if(cmp1(p,p1-p,"all")) {
1558 for(item = cpu_log_items; item->mask != 0; item++) {
1559 mask |= item->mask;
1560 }
1561 } else {
bellardf193c792004-03-21 17:06:25 +00001562 for(item = cpu_log_items; item->mask != 0; item++) {
1563 if (cmp1(p, p1 - p, item->name))
1564 goto found;
1565 }
1566 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001567 }
bellardf193c792004-03-21 17:06:25 +00001568 found:
1569 mask |= item->mask;
1570 if (*p1 != ',')
1571 break;
1572 p = p1 + 1;
1573 }
1574 return mask;
1575}
bellardea041c02003-06-25 16:16:50 +00001576
bellard75012672003-06-21 13:11:07 +00001577void cpu_abort(CPUState *env, const char *fmt, ...)
1578{
1579 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001580 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001581
1582 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001583 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001584 fprintf(stderr, "qemu: fatal: ");
1585 vfprintf(stderr, fmt, ap);
1586 fprintf(stderr, "\n");
1587#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001588 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1589#else
1590 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001591#endif
balrog924edca2007-06-10 14:07:13 +00001592 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001593 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001594 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001595 fprintf(logfile, "\n");
1596#ifdef TARGET_I386
1597 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1598#else
1599 cpu_dump_state(env, logfile, fprintf, 0);
1600#endif
balrog924edca2007-06-10 14:07:13 +00001601 fflush(logfile);
1602 fclose(logfile);
1603 }
pbrook493ae1f2007-11-23 16:53:59 +00001604 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001605 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001606 abort();
1607}
1608
thsc5be9f02007-02-28 20:20:53 +00001609CPUState *cpu_copy(CPUState *env)
1610{
ths01ba9812007-12-09 02:22:57 +00001611 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001612 /* preserve chaining and index */
1613 CPUState *next_cpu = new_env->next_cpu;
1614 int cpu_index = new_env->cpu_index;
1615 memcpy(new_env, env, sizeof(CPUState));
1616 new_env->next_cpu = next_cpu;
1617 new_env->cpu_index = cpu_index;
1618 return new_env;
1619}
1620
bellard01243112004-01-04 15:48:17 +00001621#if !defined(CONFIG_USER_ONLY)
1622
edgar_igl5c751e92008-05-06 08:44:21 +00001623static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1624{
1625 unsigned int i;
1626
1627 /* Discard jump cache entries for any tb which might potentially
1628 overlap the flushed page. */
1629 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1630 memset (&env->tb_jmp_cache[i], 0,
1631 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1632
1633 i = tb_jmp_cache_hash_page(addr);
1634 memset (&env->tb_jmp_cache[i], 0,
1635 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1636}
1637
bellardee8b7022004-02-03 23:35:10 +00001638/* NOTE: if flush_global is true, also flush global entries (not
1639 implemented yet) */
1640void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001641{
bellard33417e72003-08-10 21:47:01 +00001642 int i;
bellard01243112004-01-04 15:48:17 +00001643
bellard9fa3e852004-01-04 18:06:42 +00001644#if defined(DEBUG_TLB)
1645 printf("tlb_flush:\n");
1646#endif
bellard01243112004-01-04 15:48:17 +00001647 /* must reset current TB so that interrupts cannot modify the
1648 links while we are modifying them */
1649 env->current_tb = NULL;
1650
bellard33417e72003-08-10 21:47:01 +00001651 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001652 env->tlb_table[0][i].addr_read = -1;
1653 env->tlb_table[0][i].addr_write = -1;
1654 env->tlb_table[0][i].addr_code = -1;
1655 env->tlb_table[1][i].addr_read = -1;
1656 env->tlb_table[1][i].addr_write = -1;
1657 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001658#if (NB_MMU_MODES >= 3)
1659 env->tlb_table[2][i].addr_read = -1;
1660 env->tlb_table[2][i].addr_write = -1;
1661 env->tlb_table[2][i].addr_code = -1;
1662#if (NB_MMU_MODES == 4)
1663 env->tlb_table[3][i].addr_read = -1;
1664 env->tlb_table[3][i].addr_write = -1;
1665 env->tlb_table[3][i].addr_code = -1;
1666#endif
1667#endif
bellard33417e72003-08-10 21:47:01 +00001668 }
bellard9fa3e852004-01-04 18:06:42 +00001669
bellard8a40a182005-11-20 10:35:40 +00001670 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001671
bellard0a962c02005-02-10 22:00:27 +00001672#ifdef USE_KQEMU
1673 if (env->kqemu_enabled) {
1674 kqemu_flush(env, flush_global);
1675 }
1676#endif
bellarde3db7222005-01-26 22:00:47 +00001677 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001678}
1679
bellard274da6b2004-05-20 21:56:27 +00001680static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001681{
ths5fafdf22007-09-16 21:08:06 +00001682 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001683 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001684 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001685 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001686 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001687 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1688 tlb_entry->addr_read = -1;
1689 tlb_entry->addr_write = -1;
1690 tlb_entry->addr_code = -1;
1691 }
bellard61382a52003-10-27 21:22:23 +00001692}
1693
bellard2e126692004-04-25 21:28:44 +00001694void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001695{
bellard8a40a182005-11-20 10:35:40 +00001696 int i;
bellard01243112004-01-04 15:48:17 +00001697
bellard9fa3e852004-01-04 18:06:42 +00001698#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001699 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001700#endif
bellard01243112004-01-04 15:48:17 +00001701 /* must reset current TB so that interrupts cannot modify the
1702 links while we are modifying them */
1703 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001704
bellard61382a52003-10-27 21:22:23 +00001705 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001706 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001707 tlb_flush_entry(&env->tlb_table[0][i], addr);
1708 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001709#if (NB_MMU_MODES >= 3)
1710 tlb_flush_entry(&env->tlb_table[2][i], addr);
1711#if (NB_MMU_MODES == 4)
1712 tlb_flush_entry(&env->tlb_table[3][i], addr);
1713#endif
1714#endif
bellard01243112004-01-04 15:48:17 +00001715
edgar_igl5c751e92008-05-06 08:44:21 +00001716 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001717
bellard0a962c02005-02-10 22:00:27 +00001718#ifdef USE_KQEMU
1719 if (env->kqemu_enabled) {
1720 kqemu_flush_page(env, addr);
1721 }
1722#endif
bellard9fa3e852004-01-04 18:06:42 +00001723}
1724
bellard9fa3e852004-01-04 18:06:42 +00001725/* update the TLBs so that writes to code in the virtual page 'addr'
1726 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001727static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001728{
ths5fafdf22007-09-16 21:08:06 +00001729 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001730 ram_addr + TARGET_PAGE_SIZE,
1731 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001732}
1733
bellard9fa3e852004-01-04 18:06:42 +00001734/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001735 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001736static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001737 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001738{
bellard3a7d9292005-08-21 09:26:42 +00001739 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001740}
1741
ths5fafdf22007-09-16 21:08:06 +00001742static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001743 unsigned long start, unsigned long length)
1744{
1745 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001746 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1747 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001748 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00001749 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001750 }
1751 }
1752}
1753
bellard3a7d9292005-08-21 09:26:42 +00001754void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001755 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001756{
1757 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001758 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001759 int i, mask, len;
1760 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001761
1762 start &= TARGET_PAGE_MASK;
1763 end = TARGET_PAGE_ALIGN(end);
1764
1765 length = end - start;
1766 if (length == 0)
1767 return;
bellard0a962c02005-02-10 22:00:27 +00001768 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001769#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001770 /* XXX: should not depend on cpu context */
1771 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001772 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001773 ram_addr_t addr;
1774 addr = start;
1775 for(i = 0; i < len; i++) {
1776 kqemu_set_notdirty(env, addr);
1777 addr += TARGET_PAGE_SIZE;
1778 }
bellard3a7d9292005-08-21 09:26:42 +00001779 }
1780#endif
bellardf23db162005-08-21 19:12:28 +00001781 mask = ~dirty_flags;
1782 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1783 for(i = 0; i < len; i++)
1784 p[i] &= mask;
1785
bellard1ccde1c2004-02-06 19:46:14 +00001786 /* we modify the TLB cache so that the dirty bit will be set again
1787 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001788 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001789 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1790 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001791 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001792 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001793 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001794#if (NB_MMU_MODES >= 3)
1795 for(i = 0; i < CPU_TLB_SIZE; i++)
1796 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1797#if (NB_MMU_MODES == 4)
1798 for(i = 0; i < CPU_TLB_SIZE; i++)
1799 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1800#endif
1801#endif
bellard6a00d602005-11-21 23:25:50 +00001802 }
bellard1ccde1c2004-02-06 19:46:14 +00001803}
1804
aliguori74576192008-10-06 14:02:03 +00001805int cpu_physical_memory_set_dirty_tracking(int enable)
1806{
1807 in_migration = enable;
1808 return 0;
1809}
1810
1811int cpu_physical_memory_get_dirty_tracking(void)
1812{
1813 return in_migration;
1814}
1815
bellard3a7d9292005-08-21 09:26:42 +00001816static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1817{
1818 ram_addr_t ram_addr;
1819
bellard84b7b8e2005-11-28 21:19:04 +00001820 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001821 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001822 tlb_entry->addend - (unsigned long)phys_ram_base;
1823 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00001824 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001825 }
1826 }
1827}
1828
1829/* update the TLB according to the current state of the dirty bits */
1830void cpu_tlb_update_dirty(CPUState *env)
1831{
1832 int i;
1833 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001834 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001835 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001836 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001837#if (NB_MMU_MODES >= 3)
1838 for(i = 0; i < CPU_TLB_SIZE; i++)
1839 tlb_update_dirty(&env->tlb_table[2][i]);
1840#if (NB_MMU_MODES == 4)
1841 for(i = 0; i < CPU_TLB_SIZE; i++)
1842 tlb_update_dirty(&env->tlb_table[3][i]);
1843#endif
1844#endif
bellard3a7d9292005-08-21 09:26:42 +00001845}
1846
pbrook0f459d12008-06-09 00:20:13 +00001847static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001848{
pbrook0f459d12008-06-09 00:20:13 +00001849 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1850 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00001851}
1852
pbrook0f459d12008-06-09 00:20:13 +00001853/* update the TLB corresponding to virtual page vaddr
1854 so that it is no longer dirty */
1855static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001856{
bellard1ccde1c2004-02-06 19:46:14 +00001857 int i;
1858
pbrook0f459d12008-06-09 00:20:13 +00001859 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00001860 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
pbrook0f459d12008-06-09 00:20:13 +00001861 tlb_set_dirty1(&env->tlb_table[0][i], vaddr);
1862 tlb_set_dirty1(&env->tlb_table[1][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001863#if (NB_MMU_MODES >= 3)
pbrook0f459d12008-06-09 00:20:13 +00001864 tlb_set_dirty1(&env->tlb_table[2][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001865#if (NB_MMU_MODES == 4)
pbrook0f459d12008-06-09 00:20:13 +00001866 tlb_set_dirty1(&env->tlb_table[3][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001867#endif
1868#endif
bellard9fa3e852004-01-04 18:06:42 +00001869}
1870
bellard59817cc2004-02-16 22:01:13 +00001871/* add a new TLB entry. At most one entry for a given virtual address
1872 is permitted. Return 0 if OK or 2 if the page could not be mapped
1873 (can only happen in non SOFTMMU mode for I/O pages or pages
1874 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001875int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1876 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001877 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001878{
bellard92e873b2004-05-21 14:52:29 +00001879 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001880 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001881 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001882 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00001883 target_ulong code_address;
bellard108c49b2005-07-24 12:55:09 +00001884 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001885 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001886 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001887 int i;
pbrook0f459d12008-06-09 00:20:13 +00001888 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00001889
bellard92e873b2004-05-21 14:52:29 +00001890 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001891 if (!p) {
1892 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001893 } else {
1894 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001895 }
1896#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001897 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1898 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001899#endif
1900
1901 ret = 0;
pbrook0f459d12008-06-09 00:20:13 +00001902 address = vaddr;
1903 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
1904 /* IO memory case (romd handled later) */
1905 address |= TLB_MMIO;
1906 }
1907 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1908 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
1909 /* Normal RAM. */
1910 iotlb = pd & TARGET_PAGE_MASK;
1911 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
1912 iotlb |= IO_MEM_NOTDIRTY;
1913 else
1914 iotlb |= IO_MEM_ROM;
1915 } else {
1916 /* IO handlers are currently passed a phsical address.
1917 It would be nice to pass an offset from the base address
1918 of that region. This would avoid having to special case RAM,
1919 and avoid full address decoding in every device.
1920 We can't use the high bits of pd for this because
1921 IO_MEM_ROMD uses these as a ram address. */
1922 iotlb = (pd & ~TARGET_PAGE_MASK) + paddr;
1923 }
pbrook6658ffb2007-03-16 23:58:11 +00001924
pbrook0f459d12008-06-09 00:20:13 +00001925 code_address = address;
1926 /* Make accesses to pages with watchpoints go via the
1927 watchpoint trap routines. */
1928 for (i = 0; i < env->nb_watchpoints; i++) {
1929 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1930 iotlb = io_mem_watch + paddr;
1931 /* TODO: The memory case can be optimized by not trapping
1932 reads of pages with a write breakpoint. */
1933 address |= TLB_MMIO;
pbrook6658ffb2007-03-16 23:58:11 +00001934 }
pbrook0f459d12008-06-09 00:20:13 +00001935 }
balrogd79acba2007-06-26 20:01:13 +00001936
pbrook0f459d12008-06-09 00:20:13 +00001937 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1938 env->iotlb[mmu_idx][index] = iotlb - vaddr;
1939 te = &env->tlb_table[mmu_idx][index];
1940 te->addend = addend - vaddr;
1941 if (prot & PAGE_READ) {
1942 te->addr_read = address;
1943 } else {
1944 te->addr_read = -1;
1945 }
edgar_igl5c751e92008-05-06 08:44:21 +00001946
pbrook0f459d12008-06-09 00:20:13 +00001947 if (prot & PAGE_EXEC) {
1948 te->addr_code = code_address;
1949 } else {
1950 te->addr_code = -1;
1951 }
1952 if (prot & PAGE_WRITE) {
1953 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
1954 (pd & IO_MEM_ROMD)) {
1955 /* Write access calls the I/O callback. */
1956 te->addr_write = address | TLB_MMIO;
1957 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
1958 !cpu_physical_memory_is_dirty(pd)) {
1959 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00001960 } else {
pbrook0f459d12008-06-09 00:20:13 +00001961 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001962 }
pbrook0f459d12008-06-09 00:20:13 +00001963 } else {
1964 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001965 }
bellard9fa3e852004-01-04 18:06:42 +00001966 return ret;
1967}
1968
bellard01243112004-01-04 15:48:17 +00001969#else
1970
bellardee8b7022004-02-03 23:35:10 +00001971void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001972{
1973}
1974
bellard2e126692004-04-25 21:28:44 +00001975void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001976{
1977}
1978
ths5fafdf22007-09-16 21:08:06 +00001979int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1980 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001981 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001982{
bellard9fa3e852004-01-04 18:06:42 +00001983 return 0;
1984}
bellard33417e72003-08-10 21:47:01 +00001985
bellard9fa3e852004-01-04 18:06:42 +00001986/* dump memory mappings */
1987void page_dump(FILE *f)
1988{
1989 unsigned long start, end;
1990 int i, j, prot, prot1;
1991 PageDesc *p;
1992
1993 fprintf(f, "%-8s %-8s %-8s %s\n",
1994 "start", "end", "size", "prot");
1995 start = -1;
1996 end = -1;
1997 prot = 0;
1998 for(i = 0; i <= L1_SIZE; i++) {
1999 if (i < L1_SIZE)
2000 p = l1_map[i];
2001 else
2002 p = NULL;
2003 for(j = 0;j < L2_SIZE; j++) {
2004 if (!p)
2005 prot1 = 0;
2006 else
2007 prot1 = p[j].flags;
2008 if (prot1 != prot) {
2009 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
2010 if (start != -1) {
2011 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00002012 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00002013 prot & PAGE_READ ? 'r' : '-',
2014 prot & PAGE_WRITE ? 'w' : '-',
2015 prot & PAGE_EXEC ? 'x' : '-');
2016 }
2017 if (prot1 != 0)
2018 start = end;
2019 else
2020 start = -1;
2021 prot = prot1;
2022 }
2023 if (!p)
2024 break;
2025 }
bellard33417e72003-08-10 21:47:01 +00002026 }
bellard33417e72003-08-10 21:47:01 +00002027}
2028
pbrook53a59602006-03-25 19:31:22 +00002029int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002030{
bellard9fa3e852004-01-04 18:06:42 +00002031 PageDesc *p;
2032
2033 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002034 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002035 return 0;
2036 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002037}
2038
bellard9fa3e852004-01-04 18:06:42 +00002039/* modify the flags of a page and invalidate the code if
2040 necessary. The flag PAGE_WRITE_ORG is positionned automatically
2041 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00002042void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002043{
2044 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002045 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00002046
pbrookc8a706f2008-06-02 16:16:42 +00002047 /* mmap_lock should already be held. */
bellard9fa3e852004-01-04 18:06:42 +00002048 start = start & TARGET_PAGE_MASK;
2049 end = TARGET_PAGE_ALIGN(end);
2050 if (flags & PAGE_WRITE)
2051 flags |= PAGE_WRITE_ORG;
bellard9fa3e852004-01-04 18:06:42 +00002052 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2053 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
pbrook17e23772008-06-09 13:47:45 +00002054 /* We may be called for host regions that are outside guest
2055 address space. */
2056 if (!p)
2057 return;
bellard9fa3e852004-01-04 18:06:42 +00002058 /* if the write protection is set, then we invalidate the code
2059 inside */
ths5fafdf22007-09-16 21:08:06 +00002060 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002061 (flags & PAGE_WRITE) &&
2062 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002063 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002064 }
2065 p->flags = flags;
2066 }
bellard9fa3e852004-01-04 18:06:42 +00002067}
2068
ths3d97b402007-11-02 19:02:07 +00002069int page_check_range(target_ulong start, target_ulong len, int flags)
2070{
2071 PageDesc *p;
2072 target_ulong end;
2073 target_ulong addr;
2074
balrog55f280c2008-10-28 10:24:11 +00002075 if (start + len < start)
2076 /* we've wrapped around */
2077 return -1;
2078
ths3d97b402007-11-02 19:02:07 +00002079 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2080 start = start & TARGET_PAGE_MASK;
2081
ths3d97b402007-11-02 19:02:07 +00002082 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2083 p = page_find(addr >> TARGET_PAGE_BITS);
2084 if( !p )
2085 return -1;
2086 if( !(p->flags & PAGE_VALID) )
2087 return -1;
2088
bellarddae32702007-11-14 10:51:00 +00002089 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002090 return -1;
bellarddae32702007-11-14 10:51:00 +00002091 if (flags & PAGE_WRITE) {
2092 if (!(p->flags & PAGE_WRITE_ORG))
2093 return -1;
2094 /* unprotect the page if it was put read-only because it
2095 contains translated code */
2096 if (!(p->flags & PAGE_WRITE)) {
2097 if (!page_unprotect(addr, 0, NULL))
2098 return -1;
2099 }
2100 return 0;
2101 }
ths3d97b402007-11-02 19:02:07 +00002102 }
2103 return 0;
2104}
2105
bellard9fa3e852004-01-04 18:06:42 +00002106/* called from signal handler: invalidate the code and unprotect the
2107 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002108int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002109{
2110 unsigned int page_index, prot, pindex;
2111 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002112 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002113
pbrookc8a706f2008-06-02 16:16:42 +00002114 /* Technically this isn't safe inside a signal handler. However we
2115 know this only ever happens in a synchronous SEGV handler, so in
2116 practice it seems to be ok. */
2117 mmap_lock();
2118
bellard83fb7ad2004-07-05 21:25:26 +00002119 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002120 page_index = host_start >> TARGET_PAGE_BITS;
2121 p1 = page_find(page_index);
pbrookc8a706f2008-06-02 16:16:42 +00002122 if (!p1) {
2123 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002124 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002125 }
bellard83fb7ad2004-07-05 21:25:26 +00002126 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002127 p = p1;
2128 prot = 0;
2129 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2130 prot |= p->flags;
2131 p++;
2132 }
2133 /* if the page was really writable, then we change its
2134 protection back to writable */
2135 if (prot & PAGE_WRITE_ORG) {
2136 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2137 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002138 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002139 (prot & PAGE_BITS) | PAGE_WRITE);
2140 p1[pindex].flags |= PAGE_WRITE;
2141 /* and since the content will be modified, we must invalidate
2142 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002143 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002144#ifdef DEBUG_TB_CHECK
2145 tb_invalidate_check(address);
2146#endif
pbrookc8a706f2008-06-02 16:16:42 +00002147 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002148 return 1;
2149 }
2150 }
pbrookc8a706f2008-06-02 16:16:42 +00002151 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002152 return 0;
2153}
2154
bellard6a00d602005-11-21 23:25:50 +00002155static inline void tlb_set_dirty(CPUState *env,
2156 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002157{
2158}
bellard9fa3e852004-01-04 18:06:42 +00002159#endif /* defined(CONFIG_USER_ONLY) */
2160
pbrooke2eef172008-06-08 01:09:01 +00002161#if !defined(CONFIG_USER_ONLY)
blueswir1db7b5422007-05-26 17:36:03 +00002162static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002163 ram_addr_t memory);
2164static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2165 ram_addr_t orig_memory);
blueswir1db7b5422007-05-26 17:36:03 +00002166#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2167 need_subpage) \
2168 do { \
2169 if (addr > start_addr) \
2170 start_addr2 = 0; \
2171 else { \
2172 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2173 if (start_addr2 > 0) \
2174 need_subpage = 1; \
2175 } \
2176 \
blueswir149e9fba2007-05-30 17:25:06 +00002177 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002178 end_addr2 = TARGET_PAGE_SIZE - 1; \
2179 else { \
2180 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2181 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2182 need_subpage = 1; \
2183 } \
2184 } while (0)
2185
bellard33417e72003-08-10 21:47:01 +00002186/* register physical memory. 'size' must be a multiple of the target
2187 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2188 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002189void cpu_register_physical_memory(target_phys_addr_t start_addr,
aurel3200f82b82008-04-27 21:12:55 +00002190 ram_addr_t size,
2191 ram_addr_t phys_offset)
bellard33417e72003-08-10 21:47:01 +00002192{
bellard108c49b2005-07-24 12:55:09 +00002193 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002194 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002195 CPUState *env;
aurel3200f82b82008-04-27 21:12:55 +00002196 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002197 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002198
bellardda260242008-05-30 20:48:25 +00002199#ifdef USE_KQEMU
2200 /* XXX: should not depend on cpu context */
2201 env = first_cpu;
2202 if (env->kqemu_enabled) {
2203 kqemu_set_phys_mem(start_addr, size, phys_offset);
2204 }
2205#endif
aliguori7ba1e612008-11-05 16:04:33 +00002206 if (kvm_enabled())
2207 kvm_set_phys_mem(start_addr, size, phys_offset);
2208
bellard5fd386f2004-05-23 21:11:22 +00002209 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002210 end_addr = start_addr + (target_phys_addr_t)size;
2211 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002212 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2213 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
aurel3200f82b82008-04-27 21:12:55 +00002214 ram_addr_t orig_memory = p->phys_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002215 target_phys_addr_t start_addr2, end_addr2;
2216 int need_subpage = 0;
2217
2218 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2219 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002220 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002221 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2222 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2223 &p->phys_offset, orig_memory);
2224 } else {
2225 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2226 >> IO_MEM_SHIFT];
2227 }
2228 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2229 } else {
2230 p->phys_offset = phys_offset;
2231 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2232 (phys_offset & IO_MEM_ROMD))
2233 phys_offset += TARGET_PAGE_SIZE;
2234 }
2235 } else {
2236 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2237 p->phys_offset = phys_offset;
2238 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2239 (phys_offset & IO_MEM_ROMD))
2240 phys_offset += TARGET_PAGE_SIZE;
2241 else {
2242 target_phys_addr_t start_addr2, end_addr2;
2243 int need_subpage = 0;
2244
2245 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2246 end_addr2, need_subpage);
2247
blueswir14254fab2008-01-01 16:57:19 +00002248 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002249 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2250 &p->phys_offset, IO_MEM_UNASSIGNED);
2251 subpage_register(subpage, start_addr2, end_addr2,
2252 phys_offset);
2253 }
2254 }
2255 }
bellard33417e72003-08-10 21:47:01 +00002256 }
ths3b46e622007-09-17 08:09:54 +00002257
bellard9d420372006-06-25 22:25:22 +00002258 /* since each CPU stores ram addresses in its TLB cache, we must
2259 reset the modified entries */
2260 /* XXX: slow ! */
2261 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2262 tlb_flush(env, 1);
2263 }
bellard33417e72003-08-10 21:47:01 +00002264}
2265
bellardba863452006-09-24 18:41:10 +00002266/* XXX: temporary until new memory mapping API */
aurel3200f82b82008-04-27 21:12:55 +00002267ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002268{
2269 PhysPageDesc *p;
2270
2271 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2272 if (!p)
2273 return IO_MEM_UNASSIGNED;
2274 return p->phys_offset;
2275}
2276
bellarde9a1ab12007-02-08 23:08:38 +00002277/* XXX: better than nothing */
aurel3200f82b82008-04-27 21:12:55 +00002278ram_addr_t qemu_ram_alloc(ram_addr_t size)
bellarde9a1ab12007-02-08 23:08:38 +00002279{
2280 ram_addr_t addr;
balrog7fb4fdc2008-04-24 17:59:27 +00002281 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
ths012a7042008-10-02 17:34:21 +00002282 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
bellarded441462008-05-23 11:56:45 +00002283 (uint64_t)size, (uint64_t)phys_ram_size);
bellarde9a1ab12007-02-08 23:08:38 +00002284 abort();
2285 }
2286 addr = phys_ram_alloc_offset;
2287 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2288 return addr;
2289}
2290
2291void qemu_ram_free(ram_addr_t addr)
2292{
2293}
2294
bellarda4193c82004-06-03 14:01:43 +00002295static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002296{
pbrook67d3b952006-12-18 05:03:52 +00002297#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002298 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002299#endif
blueswir1e18231a2008-10-06 18:46:28 +00002300#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2301 do_unassigned_access(addr, 0, 0, 0, 1);
2302#endif
2303 return 0;
2304}
2305
2306static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
2307{
2308#ifdef DEBUG_UNASSIGNED
2309 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2310#endif
2311#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2312 do_unassigned_access(addr, 0, 0, 0, 2);
2313#endif
2314 return 0;
2315}
2316
2317static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
2318{
2319#ifdef DEBUG_UNASSIGNED
2320 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2321#endif
2322#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2323 do_unassigned_access(addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002324#endif
bellard33417e72003-08-10 21:47:01 +00002325 return 0;
2326}
2327
bellarda4193c82004-06-03 14:01:43 +00002328static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002329{
pbrook67d3b952006-12-18 05:03:52 +00002330#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002331 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002332#endif
blueswir1e18231a2008-10-06 18:46:28 +00002333#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2334 do_unassigned_access(addr, 1, 0, 0, 1);
2335#endif
2336}
2337
2338static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2339{
2340#ifdef DEBUG_UNASSIGNED
2341 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2342#endif
2343#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2344 do_unassigned_access(addr, 1, 0, 0, 2);
2345#endif
2346}
2347
2348static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2349{
2350#ifdef DEBUG_UNASSIGNED
2351 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2352#endif
2353#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2354 do_unassigned_access(addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002355#endif
bellard33417e72003-08-10 21:47:01 +00002356}
2357
2358static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2359 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00002360 unassigned_mem_readw,
2361 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00002362};
2363
2364static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2365 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00002366 unassigned_mem_writew,
2367 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00002368};
2369
pbrook0f459d12008-06-09 00:20:13 +00002370static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2371 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002372{
bellard3a7d9292005-08-21 09:26:42 +00002373 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002374 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2375 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2376#if !defined(CONFIG_USER_ONLY)
2377 tb_invalidate_phys_page_fast(ram_addr, 1);
2378 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2379#endif
2380 }
pbrook0f459d12008-06-09 00:20:13 +00002381 stb_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002382#ifdef USE_KQEMU
2383 if (cpu_single_env->kqemu_enabled &&
2384 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2385 kqemu_modify_page(cpu_single_env, ram_addr);
2386#endif
bellardf23db162005-08-21 19:12:28 +00002387 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2388 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2389 /* we remove the notdirty callback only if the code has been
2390 flushed */
2391 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002392 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002393}
2394
pbrook0f459d12008-06-09 00:20:13 +00002395static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2396 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002397{
bellard3a7d9292005-08-21 09:26:42 +00002398 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002399 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2400 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2401#if !defined(CONFIG_USER_ONLY)
2402 tb_invalidate_phys_page_fast(ram_addr, 2);
2403 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2404#endif
2405 }
pbrook0f459d12008-06-09 00:20:13 +00002406 stw_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002407#ifdef USE_KQEMU
2408 if (cpu_single_env->kqemu_enabled &&
2409 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2410 kqemu_modify_page(cpu_single_env, ram_addr);
2411#endif
bellardf23db162005-08-21 19:12:28 +00002412 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2413 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2414 /* we remove the notdirty callback only if the code has been
2415 flushed */
2416 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002417 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002418}
2419
pbrook0f459d12008-06-09 00:20:13 +00002420static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2421 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002422{
bellard3a7d9292005-08-21 09:26:42 +00002423 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002424 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2425 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2426#if !defined(CONFIG_USER_ONLY)
2427 tb_invalidate_phys_page_fast(ram_addr, 4);
2428 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2429#endif
2430 }
pbrook0f459d12008-06-09 00:20:13 +00002431 stl_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002432#ifdef USE_KQEMU
2433 if (cpu_single_env->kqemu_enabled &&
2434 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2435 kqemu_modify_page(cpu_single_env, ram_addr);
2436#endif
bellardf23db162005-08-21 19:12:28 +00002437 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2438 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2439 /* we remove the notdirty callback only if the code has been
2440 flushed */
2441 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002442 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002443}
2444
bellard3a7d9292005-08-21 09:26:42 +00002445static CPUReadMemoryFunc *error_mem_read[3] = {
2446 NULL, /* never used */
2447 NULL, /* never used */
2448 NULL, /* never used */
2449};
2450
bellard1ccde1c2004-02-06 19:46:14 +00002451static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2452 notdirty_mem_writeb,
2453 notdirty_mem_writew,
2454 notdirty_mem_writel,
2455};
2456
pbrook0f459d12008-06-09 00:20:13 +00002457/* Generate a debug exception if a watchpoint has been hit. */
2458static void check_watchpoint(int offset, int flags)
2459{
2460 CPUState *env = cpu_single_env;
2461 target_ulong vaddr;
2462 int i;
2463
pbrook2e70f6e2008-06-29 01:03:05 +00002464 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
pbrook0f459d12008-06-09 00:20:13 +00002465 for (i = 0; i < env->nb_watchpoints; i++) {
2466 if (vaddr == env->watchpoint[i].vaddr
2467 && (env->watchpoint[i].type & flags)) {
2468 env->watchpoint_hit = i + 1;
2469 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2470 break;
2471 }
2472 }
2473}
2474
pbrook6658ffb2007-03-16 23:58:11 +00002475/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2476 so these check for a hit then pass through to the normal out-of-line
2477 phys routines. */
2478static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2479{
pbrook0f459d12008-06-09 00:20:13 +00002480 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002481 return ldub_phys(addr);
2482}
2483
2484static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2485{
pbrook0f459d12008-06-09 00:20:13 +00002486 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002487 return lduw_phys(addr);
2488}
2489
2490static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2491{
pbrook0f459d12008-06-09 00:20:13 +00002492 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002493 return ldl_phys(addr);
2494}
2495
pbrook6658ffb2007-03-16 23:58:11 +00002496static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2497 uint32_t val)
2498{
pbrook0f459d12008-06-09 00:20:13 +00002499 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002500 stb_phys(addr, val);
2501}
2502
2503static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2504 uint32_t val)
2505{
pbrook0f459d12008-06-09 00:20:13 +00002506 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002507 stw_phys(addr, val);
2508}
2509
2510static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2511 uint32_t val)
2512{
pbrook0f459d12008-06-09 00:20:13 +00002513 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002514 stl_phys(addr, val);
2515}
2516
2517static CPUReadMemoryFunc *watch_mem_read[3] = {
2518 watch_mem_readb,
2519 watch_mem_readw,
2520 watch_mem_readl,
2521};
2522
2523static CPUWriteMemoryFunc *watch_mem_write[3] = {
2524 watch_mem_writeb,
2525 watch_mem_writew,
2526 watch_mem_writel,
2527};
pbrook6658ffb2007-03-16 23:58:11 +00002528
blueswir1db7b5422007-05-26 17:36:03 +00002529static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2530 unsigned int len)
2531{
blueswir1db7b5422007-05-26 17:36:03 +00002532 uint32_t ret;
2533 unsigned int idx;
2534
2535 idx = SUBPAGE_IDX(addr - mmio->base);
2536#if defined(DEBUG_SUBPAGE)
2537 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2538 mmio, len, addr, idx);
2539#endif
blueswir13ee89922008-01-02 19:45:26 +00002540 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002541
2542 return ret;
2543}
2544
2545static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2546 uint32_t value, unsigned int len)
2547{
blueswir1db7b5422007-05-26 17:36:03 +00002548 unsigned int idx;
2549
2550 idx = SUBPAGE_IDX(addr - mmio->base);
2551#if defined(DEBUG_SUBPAGE)
2552 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2553 mmio, len, addr, idx, value);
2554#endif
blueswir13ee89922008-01-02 19:45:26 +00002555 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002556}
2557
2558static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2559{
2560#if defined(DEBUG_SUBPAGE)
2561 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2562#endif
2563
2564 return subpage_readlen(opaque, addr, 0);
2565}
2566
2567static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2568 uint32_t value)
2569{
2570#if defined(DEBUG_SUBPAGE)
2571 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2572#endif
2573 subpage_writelen(opaque, addr, value, 0);
2574}
2575
2576static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2577{
2578#if defined(DEBUG_SUBPAGE)
2579 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2580#endif
2581
2582 return subpage_readlen(opaque, addr, 1);
2583}
2584
2585static void subpage_writew (void *opaque, target_phys_addr_t addr,
2586 uint32_t value)
2587{
2588#if defined(DEBUG_SUBPAGE)
2589 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2590#endif
2591 subpage_writelen(opaque, addr, value, 1);
2592}
2593
2594static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2595{
2596#if defined(DEBUG_SUBPAGE)
2597 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2598#endif
2599
2600 return subpage_readlen(opaque, addr, 2);
2601}
2602
2603static void subpage_writel (void *opaque,
2604 target_phys_addr_t addr, uint32_t value)
2605{
2606#if defined(DEBUG_SUBPAGE)
2607 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2608#endif
2609 subpage_writelen(opaque, addr, value, 2);
2610}
2611
2612static CPUReadMemoryFunc *subpage_read[] = {
2613 &subpage_readb,
2614 &subpage_readw,
2615 &subpage_readl,
2616};
2617
2618static CPUWriteMemoryFunc *subpage_write[] = {
2619 &subpage_writeb,
2620 &subpage_writew,
2621 &subpage_writel,
2622};
2623
2624static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002625 ram_addr_t memory)
blueswir1db7b5422007-05-26 17:36:03 +00002626{
2627 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002628 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002629
2630 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2631 return -1;
2632 idx = SUBPAGE_IDX(start);
2633 eidx = SUBPAGE_IDX(end);
2634#if defined(DEBUG_SUBPAGE)
2635 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2636 mmio, start, end, idx, eidx, memory);
2637#endif
2638 memory >>= IO_MEM_SHIFT;
2639 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002640 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002641 if (io_mem_read[memory][i]) {
2642 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2643 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2644 }
2645 if (io_mem_write[memory][i]) {
2646 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2647 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2648 }
blueswir14254fab2008-01-01 16:57:19 +00002649 }
blueswir1db7b5422007-05-26 17:36:03 +00002650 }
2651
2652 return 0;
2653}
2654
aurel3200f82b82008-04-27 21:12:55 +00002655static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2656 ram_addr_t orig_memory)
blueswir1db7b5422007-05-26 17:36:03 +00002657{
2658 subpage_t *mmio;
2659 int subpage_memory;
2660
2661 mmio = qemu_mallocz(sizeof(subpage_t));
2662 if (mmio != NULL) {
2663 mmio->base = base;
2664 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2665#if defined(DEBUG_SUBPAGE)
2666 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2667 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2668#endif
2669 *phys = subpage_memory | IO_MEM_SUBPAGE;
2670 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2671 }
2672
2673 return mmio;
2674}
2675
bellard33417e72003-08-10 21:47:01 +00002676static void io_mem_init(void)
2677{
bellard3a7d9292005-08-21 09:26:42 +00002678 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002679 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002680 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002681 io_mem_nb = 5;
2682
pbrook0f459d12008-06-09 00:20:13 +00002683 io_mem_watch = cpu_register_io_memory(0, watch_mem_read,
pbrook6658ffb2007-03-16 23:58:11 +00002684 watch_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002685 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002686 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002687 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002688}
2689
2690/* mem_read and mem_write are arrays of functions containing the
2691 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002692 2). Functions can be omitted with a NULL function pointer. The
2693 registered functions may be modified dynamically later.
2694 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002695 modified. If it is zero, a new io zone is allocated. The return
2696 value can be used with cpu_register_physical_memory(). (-1) is
2697 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002698int cpu_register_io_memory(int io_index,
2699 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002700 CPUWriteMemoryFunc **mem_write,
2701 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002702{
blueswir14254fab2008-01-01 16:57:19 +00002703 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002704
2705 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002706 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002707 return -1;
2708 io_index = io_mem_nb++;
2709 } else {
2710 if (io_index >= IO_MEM_NB_ENTRIES)
2711 return -1;
2712 }
bellardb5ff1b32005-11-26 10:38:39 +00002713
bellard33417e72003-08-10 21:47:01 +00002714 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002715 if (!mem_read[i] || !mem_write[i])
2716 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002717 io_mem_read[io_index][i] = mem_read[i];
2718 io_mem_write[io_index][i] = mem_write[i];
2719 }
bellarda4193c82004-06-03 14:01:43 +00002720 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002721 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002722}
bellard61382a52003-10-27 21:22:23 +00002723
bellard8926b512004-10-10 15:14:20 +00002724CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2725{
2726 return io_mem_write[io_index >> IO_MEM_SHIFT];
2727}
2728
2729CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2730{
2731 return io_mem_read[io_index >> IO_MEM_SHIFT];
2732}
2733
pbrooke2eef172008-06-08 01:09:01 +00002734#endif /* !defined(CONFIG_USER_ONLY) */
2735
bellard13eb76e2004-01-24 15:23:36 +00002736/* physical memory access (slow version, mainly for debug) */
2737#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002738void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002739 int len, int is_write)
2740{
2741 int l, flags;
2742 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002743 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002744
2745 while (len > 0) {
2746 page = addr & TARGET_PAGE_MASK;
2747 l = (page + TARGET_PAGE_SIZE) - addr;
2748 if (l > len)
2749 l = len;
2750 flags = page_get_flags(page);
2751 if (!(flags & PAGE_VALID))
2752 return;
2753 if (is_write) {
2754 if (!(flags & PAGE_WRITE))
2755 return;
bellard579a97f2007-11-11 14:26:47 +00002756 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002757 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00002758 /* FIXME - should this return an error rather than just fail? */
2759 return;
aurel3272fb7da2008-04-27 23:53:45 +00002760 memcpy(p, buf, l);
2761 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00002762 } else {
2763 if (!(flags & PAGE_READ))
2764 return;
bellard579a97f2007-11-11 14:26:47 +00002765 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002766 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00002767 /* FIXME - should this return an error rather than just fail? */
2768 return;
aurel3272fb7da2008-04-27 23:53:45 +00002769 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00002770 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002771 }
2772 len -= l;
2773 buf += l;
2774 addr += l;
2775 }
2776}
bellard8df1cd02005-01-28 22:37:22 +00002777
bellard13eb76e2004-01-24 15:23:36 +00002778#else
ths5fafdf22007-09-16 21:08:06 +00002779void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002780 int len, int is_write)
2781{
2782 int l, io_index;
2783 uint8_t *ptr;
2784 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002785 target_phys_addr_t page;
2786 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002787 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002788
bellard13eb76e2004-01-24 15:23:36 +00002789 while (len > 0) {
2790 page = addr & TARGET_PAGE_MASK;
2791 l = (page + TARGET_PAGE_SIZE) - addr;
2792 if (l > len)
2793 l = len;
bellard92e873b2004-05-21 14:52:29 +00002794 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002795 if (!p) {
2796 pd = IO_MEM_UNASSIGNED;
2797 } else {
2798 pd = p->phys_offset;
2799 }
ths3b46e622007-09-17 08:09:54 +00002800
bellard13eb76e2004-01-24 15:23:36 +00002801 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002802 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002803 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002804 /* XXX: could force cpu_single_env to NULL to avoid
2805 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002806 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002807 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002808 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002809 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002810 l = 4;
2811 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002812 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002813 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002814 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002815 l = 2;
2816 } else {
bellard1c213d12005-09-03 10:49:04 +00002817 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002818 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002819 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002820 l = 1;
2821 }
2822 } else {
bellardb448f2f2004-02-25 23:24:04 +00002823 unsigned long addr1;
2824 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002825 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002826 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002827 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002828 if (!cpu_physical_memory_is_dirty(addr1)) {
2829 /* invalidate code */
2830 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2831 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002832 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002833 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002834 }
bellard13eb76e2004-01-24 15:23:36 +00002835 }
2836 } else {
ths5fafdf22007-09-16 21:08:06 +00002837 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002838 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002839 /* I/O case */
2840 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2841 if (l >= 4 && ((addr & 3) == 0)) {
2842 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002843 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002844 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002845 l = 4;
2846 } else if (l >= 2 && ((addr & 1) == 0)) {
2847 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002848 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002849 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002850 l = 2;
2851 } else {
bellard1c213d12005-09-03 10:49:04 +00002852 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002853 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002854 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002855 l = 1;
2856 }
2857 } else {
2858 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002859 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002860 (addr & ~TARGET_PAGE_MASK);
2861 memcpy(buf, ptr, l);
2862 }
2863 }
2864 len -= l;
2865 buf += l;
2866 addr += l;
2867 }
2868}
bellard8df1cd02005-01-28 22:37:22 +00002869
bellardd0ecd2a2006-04-23 17:14:48 +00002870/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002871void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002872 const uint8_t *buf, int len)
2873{
2874 int l;
2875 uint8_t *ptr;
2876 target_phys_addr_t page;
2877 unsigned long pd;
2878 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002879
bellardd0ecd2a2006-04-23 17:14:48 +00002880 while (len > 0) {
2881 page = addr & TARGET_PAGE_MASK;
2882 l = (page + TARGET_PAGE_SIZE) - addr;
2883 if (l > len)
2884 l = len;
2885 p = phys_page_find(page >> TARGET_PAGE_BITS);
2886 if (!p) {
2887 pd = IO_MEM_UNASSIGNED;
2888 } else {
2889 pd = p->phys_offset;
2890 }
ths3b46e622007-09-17 08:09:54 +00002891
bellardd0ecd2a2006-04-23 17:14:48 +00002892 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002893 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2894 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002895 /* do nothing */
2896 } else {
2897 unsigned long addr1;
2898 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2899 /* ROM/RAM case */
2900 ptr = phys_ram_base + addr1;
2901 memcpy(ptr, buf, l);
2902 }
2903 len -= l;
2904 buf += l;
2905 addr += l;
2906 }
2907}
2908
2909
bellard8df1cd02005-01-28 22:37:22 +00002910/* warning: addr must be aligned */
2911uint32_t ldl_phys(target_phys_addr_t addr)
2912{
2913 int io_index;
2914 uint8_t *ptr;
2915 uint32_t val;
2916 unsigned long pd;
2917 PhysPageDesc *p;
2918
2919 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2920 if (!p) {
2921 pd = IO_MEM_UNASSIGNED;
2922 } else {
2923 pd = p->phys_offset;
2924 }
ths3b46e622007-09-17 08:09:54 +00002925
ths5fafdf22007-09-16 21:08:06 +00002926 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002927 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002928 /* I/O case */
2929 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2930 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2931 } else {
2932 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002933 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002934 (addr & ~TARGET_PAGE_MASK);
2935 val = ldl_p(ptr);
2936 }
2937 return val;
2938}
2939
bellard84b7b8e2005-11-28 21:19:04 +00002940/* warning: addr must be aligned */
2941uint64_t ldq_phys(target_phys_addr_t addr)
2942{
2943 int io_index;
2944 uint8_t *ptr;
2945 uint64_t val;
2946 unsigned long pd;
2947 PhysPageDesc *p;
2948
2949 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2950 if (!p) {
2951 pd = IO_MEM_UNASSIGNED;
2952 } else {
2953 pd = p->phys_offset;
2954 }
ths3b46e622007-09-17 08:09:54 +00002955
bellard2a4188a2006-06-25 21:54:59 +00002956 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2957 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002958 /* I/O case */
2959 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2960#ifdef TARGET_WORDS_BIGENDIAN
2961 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2962 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2963#else
2964 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2965 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2966#endif
2967 } else {
2968 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002969 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002970 (addr & ~TARGET_PAGE_MASK);
2971 val = ldq_p(ptr);
2972 }
2973 return val;
2974}
2975
bellardaab33092005-10-30 20:48:42 +00002976/* XXX: optimize */
2977uint32_t ldub_phys(target_phys_addr_t addr)
2978{
2979 uint8_t val;
2980 cpu_physical_memory_read(addr, &val, 1);
2981 return val;
2982}
2983
2984/* XXX: optimize */
2985uint32_t lduw_phys(target_phys_addr_t addr)
2986{
2987 uint16_t val;
2988 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2989 return tswap16(val);
2990}
2991
bellard8df1cd02005-01-28 22:37:22 +00002992/* warning: addr must be aligned. The ram page is not masked as dirty
2993 and the code inside is not invalidated. It is useful if the dirty
2994 bits are used to track modified PTEs */
2995void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2996{
2997 int io_index;
2998 uint8_t *ptr;
2999 unsigned long pd;
3000 PhysPageDesc *p;
3001
3002 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3003 if (!p) {
3004 pd = IO_MEM_UNASSIGNED;
3005 } else {
3006 pd = p->phys_offset;
3007 }
ths3b46e622007-09-17 08:09:54 +00003008
bellard3a7d9292005-08-21 09:26:42 +00003009 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003010 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3011 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3012 } else {
aliguori74576192008-10-06 14:02:03 +00003013 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3014 ptr = phys_ram_base + addr1;
bellard8df1cd02005-01-28 22:37:22 +00003015 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00003016
3017 if (unlikely(in_migration)) {
3018 if (!cpu_physical_memory_is_dirty(addr1)) {
3019 /* invalidate code */
3020 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3021 /* set dirty bit */
3022 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3023 (0xff & ~CODE_DIRTY_FLAG);
3024 }
3025 }
bellard8df1cd02005-01-28 22:37:22 +00003026 }
3027}
3028
j_mayerbc98a7e2007-04-04 07:55:12 +00003029void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
3030{
3031 int io_index;
3032 uint8_t *ptr;
3033 unsigned long pd;
3034 PhysPageDesc *p;
3035
3036 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3037 if (!p) {
3038 pd = IO_MEM_UNASSIGNED;
3039 } else {
3040 pd = p->phys_offset;
3041 }
ths3b46e622007-09-17 08:09:54 +00003042
j_mayerbc98a7e2007-04-04 07:55:12 +00003043 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3044 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3045#ifdef TARGET_WORDS_BIGENDIAN
3046 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3047 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3048#else
3049 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3050 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3051#endif
3052 } else {
ths5fafdf22007-09-16 21:08:06 +00003053 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00003054 (addr & ~TARGET_PAGE_MASK);
3055 stq_p(ptr, val);
3056 }
3057}
3058
bellard8df1cd02005-01-28 22:37:22 +00003059/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00003060void stl_phys(target_phys_addr_t addr, uint32_t val)
3061{
3062 int io_index;
3063 uint8_t *ptr;
3064 unsigned long pd;
3065 PhysPageDesc *p;
3066
3067 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3068 if (!p) {
3069 pd = IO_MEM_UNASSIGNED;
3070 } else {
3071 pd = p->phys_offset;
3072 }
ths3b46e622007-09-17 08:09:54 +00003073
bellard3a7d9292005-08-21 09:26:42 +00003074 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003075 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3076 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3077 } else {
3078 unsigned long addr1;
3079 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3080 /* RAM case */
3081 ptr = phys_ram_base + addr1;
3082 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00003083 if (!cpu_physical_memory_is_dirty(addr1)) {
3084 /* invalidate code */
3085 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3086 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00003087 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3088 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00003089 }
bellard8df1cd02005-01-28 22:37:22 +00003090 }
3091}
3092
bellardaab33092005-10-30 20:48:42 +00003093/* XXX: optimize */
3094void stb_phys(target_phys_addr_t addr, uint32_t val)
3095{
3096 uint8_t v = val;
3097 cpu_physical_memory_write(addr, &v, 1);
3098}
3099
3100/* XXX: optimize */
3101void stw_phys(target_phys_addr_t addr, uint32_t val)
3102{
3103 uint16_t v = tswap16(val);
3104 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3105}
3106
3107/* XXX: optimize */
3108void stq_phys(target_phys_addr_t addr, uint64_t val)
3109{
3110 val = tswap64(val);
3111 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3112}
3113
bellard13eb76e2004-01-24 15:23:36 +00003114#endif
3115
3116/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00003117int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00003118 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003119{
3120 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00003121 target_phys_addr_t phys_addr;
3122 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00003123
3124 while (len > 0) {
3125 page = addr & TARGET_PAGE_MASK;
3126 phys_addr = cpu_get_phys_page_debug(env, page);
3127 /* if no physical page mapped, return an error */
3128 if (phys_addr == -1)
3129 return -1;
3130 l = (page + TARGET_PAGE_SIZE) - addr;
3131 if (l > len)
3132 l = len;
ths5fafdf22007-09-16 21:08:06 +00003133 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00003134 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00003135 len -= l;
3136 buf += l;
3137 addr += l;
3138 }
3139 return 0;
3140}
3141
pbrook2e70f6e2008-06-29 01:03:05 +00003142/* in deterministic execution mode, instructions doing device I/Os
3143 must be at the end of the TB */
3144void cpu_io_recompile(CPUState *env, void *retaddr)
3145{
3146 TranslationBlock *tb;
3147 uint32_t n, cflags;
3148 target_ulong pc, cs_base;
3149 uint64_t flags;
3150
3151 tb = tb_find_pc((unsigned long)retaddr);
3152 if (!tb) {
3153 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3154 retaddr);
3155 }
3156 n = env->icount_decr.u16.low + tb->icount;
3157 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3158 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00003159 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00003160 n = n - env->icount_decr.u16.low;
3161 /* Generate a new TB ending on the I/O insn. */
3162 n++;
3163 /* On MIPS and SH, delay slot instructions can only be restarted if
3164 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00003165 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00003166 branch. */
3167#if defined(TARGET_MIPS)
3168 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3169 env->active_tc.PC -= 4;
3170 env->icount_decr.u16.low++;
3171 env->hflags &= ~MIPS_HFLAG_BMASK;
3172 }
3173#elif defined(TARGET_SH4)
3174 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3175 && n > 1) {
3176 env->pc -= 2;
3177 env->icount_decr.u16.low++;
3178 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3179 }
3180#endif
3181 /* This should never happen. */
3182 if (n > CF_COUNT_MASK)
3183 cpu_abort(env, "TB too big during recompile");
3184
3185 cflags = n | CF_LAST_IO;
3186 pc = tb->pc;
3187 cs_base = tb->cs_base;
3188 flags = tb->flags;
3189 tb_phys_invalidate(tb, -1);
3190 /* FIXME: In theory this could raise an exception. In practice
3191 we have already translated the block once so it's probably ok. */
3192 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00003193 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00003194 the first in the TB) then we end up generating a whole new TB and
3195 repeating the fault, which is horribly inefficient.
3196 Better would be to execute just this insn uncached, or generate a
3197 second new TB. */
3198 cpu_resume_from_signal(env, NULL);
3199}
3200
bellarde3db7222005-01-26 22:00:47 +00003201void dump_exec_info(FILE *f,
3202 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3203{
3204 int i, target_code_size, max_target_code_size;
3205 int direct_jmp_count, direct_jmp2_count, cross_page;
3206 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003207
bellarde3db7222005-01-26 22:00:47 +00003208 target_code_size = 0;
3209 max_target_code_size = 0;
3210 cross_page = 0;
3211 direct_jmp_count = 0;
3212 direct_jmp2_count = 0;
3213 for(i = 0; i < nb_tbs; i++) {
3214 tb = &tbs[i];
3215 target_code_size += tb->size;
3216 if (tb->size > max_target_code_size)
3217 max_target_code_size = tb->size;
3218 if (tb->page_addr[1] != -1)
3219 cross_page++;
3220 if (tb->tb_next_offset[0] != 0xffff) {
3221 direct_jmp_count++;
3222 if (tb->tb_next_offset[1] != 0xffff) {
3223 direct_jmp2_count++;
3224 }
3225 }
3226 }
3227 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003228 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003229 cpu_fprintf(f, "gen code size %ld/%ld\n",
3230 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3231 cpu_fprintf(f, "TB count %d/%d\n",
3232 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003233 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003234 nb_tbs ? target_code_size / nb_tbs : 0,
3235 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003236 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003237 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3238 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003239 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3240 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003241 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3242 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003243 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003244 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3245 direct_jmp2_count,
3246 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003247 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003248 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3249 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3250 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003251 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003252}
3253
ths5fafdf22007-09-16 21:08:06 +00003254#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003255
3256#define MMUSUFFIX _cmmu
3257#define GETPC() NULL
3258#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003259#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003260
3261#define SHIFT 0
3262#include "softmmu_template.h"
3263
3264#define SHIFT 1
3265#include "softmmu_template.h"
3266
3267#define SHIFT 2
3268#include "softmmu_template.h"
3269
3270#define SHIFT 3
3271#include "softmmu_template.h"
3272
3273#undef env
3274
3275#endif