blob: 06d92538f7722eb298d69625b2ed327ccb7c0113 [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"
pbrook53a59602006-03-25 19:31:22 +000040#if defined(CONFIG_USER_ONLY)
41#include <qemu.h>
42#endif
bellard54936002003-05-13 00:25:15 +000043
bellardfd6ce8f2003-05-14 19:00:11 +000044//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000045//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000046//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000047//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000048
49/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000050//#define DEBUG_TB_CHECK
51//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000052
ths1196be32007-03-17 15:17:58 +000053//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000054//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000055
pbrook99773bd2006-04-16 15:14:59 +000056#if !defined(CONFIG_USER_ONLY)
57/* TB consistency checks only implemented for usermode emulation. */
58#undef DEBUG_TB_CHECK
59#endif
60
bellard9fa3e852004-01-04 18:06:42 +000061#define SMC_BITMAP_USE_THRESHOLD 10
62
63#define MMAP_AREA_START 0x00000000
64#define MMAP_AREA_END 0xa8000000
bellardfd6ce8f2003-05-14 19:00:11 +000065
bellard108c49b2005-07-24 12:55:09 +000066#if defined(TARGET_SPARC64)
67#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000068#elif defined(TARGET_SPARC)
69#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000070#elif defined(TARGET_ALPHA)
71#define TARGET_PHYS_ADDR_SPACE_BITS 42
72#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000073#elif defined(TARGET_PPC64)
74#define TARGET_PHYS_ADDR_SPACE_BITS 42
aurel3200f82b82008-04-27 21:12:55 +000075#elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
76#define TARGET_PHYS_ADDR_SPACE_BITS 42
77#elif defined(TARGET_I386) && !defined(USE_KQEMU)
78#define TARGET_PHYS_ADDR_SPACE_BITS 36
bellard108c49b2005-07-24 12:55:09 +000079#else
80/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
81#define TARGET_PHYS_ADDR_SPACE_BITS 32
82#endif
83
pbrookfab94c02008-05-24 13:56:15 +000084TranslationBlock *tbs;
bellard26a5f132008-05-28 12:30:31 +000085int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000086TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bellardfd6ce8f2003-05-14 19:00:11 +000087int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000088/* any access to the tbs or the page table must use this lock */
89spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000090
bellard7cb69ca2008-05-10 10:55:51 +000091uint8_t code_gen_prologue[1024] __attribute__((aligned (32)));
bellard26a5f132008-05-28 12:30:31 +000092uint8_t *code_gen_buffer;
93unsigned long code_gen_buffer_size;
94/* threshold to flush the translated code buffer */
95unsigned long code_gen_buffer_max_size;
bellardfd6ce8f2003-05-14 19:00:11 +000096uint8_t *code_gen_ptr;
97
pbrooke2eef172008-06-08 01:09:01 +000098#if !defined(CONFIG_USER_ONLY)
aurel3200f82b82008-04-27 21:12:55 +000099ram_addr_t phys_ram_size;
bellard9fa3e852004-01-04 18:06:42 +0000100int phys_ram_fd;
101uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +0000102uint8_t *phys_ram_dirty;
bellarde9a1ab12007-02-08 23:08:38 +0000103static ram_addr_t phys_ram_alloc_offset = 0;
pbrooke2eef172008-06-08 01:09:01 +0000104#endif
bellard9fa3e852004-01-04 18:06:42 +0000105
bellard6a00d602005-11-21 23:25:50 +0000106CPUState *first_cpu;
107/* current CPU in the current thread. It is only valid inside
108 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000109CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000110/* 0 = Do not count executed instructions.
111 1 = Precice instruction counting.
112 2 = Adaptive rate instruction counting. */
113int use_icount = 0;
114/* Current instruction counter. While executing translated code this may
115 include some instructions that have not yet been executed. */
116int64_t qemu_icount;
bellard6a00d602005-11-21 23:25:50 +0000117
bellard54936002003-05-13 00:25:15 +0000118typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000119 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000120 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000121 /* in order to optimize self modifying code, we count the number
122 of lookups we do to a given page to use a bitmap */
123 unsigned int code_write_count;
124 uint8_t *code_bitmap;
125#if defined(CONFIG_USER_ONLY)
126 unsigned long flags;
127#endif
bellard54936002003-05-13 00:25:15 +0000128} PageDesc;
129
bellard92e873b2004-05-21 14:52:29 +0000130typedef struct PhysPageDesc {
pbrook0f459d12008-06-09 00:20:13 +0000131 /* offset in host memory of the page + io_index in the low bits */
aurel3200f82b82008-04-27 21:12:55 +0000132 ram_addr_t phys_offset;
bellard92e873b2004-05-21 14:52:29 +0000133} PhysPageDesc;
134
bellard54936002003-05-13 00:25:15 +0000135#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000136#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
137/* XXX: this is a temporary hack for alpha target.
138 * In the future, this is to be replaced by a multi-level table
139 * to actually be able to handle the complete 64 bits address space.
140 */
141#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
142#else
aurel3203875442008-04-22 20:45:18 +0000143#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000144#endif
bellard54936002003-05-13 00:25:15 +0000145
146#define L1_SIZE (1 << L1_BITS)
147#define L2_SIZE (1 << L2_BITS)
148
bellard83fb7ad2004-07-05 21:25:26 +0000149unsigned long qemu_real_host_page_size;
150unsigned long qemu_host_page_bits;
151unsigned long qemu_host_page_size;
152unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000153
bellard92e873b2004-05-21 14:52:29 +0000154/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000155static PageDesc *l1_map[L1_SIZE];
bellard0a962c02005-02-10 22:00:27 +0000156PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000157
pbrooke2eef172008-06-08 01:09:01 +0000158#if !defined(CONFIG_USER_ONLY)
159static void io_mem_init(void);
160
bellard33417e72003-08-10 21:47:01 +0000161/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000162CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
163CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000164void *io_mem_opaque[IO_MEM_NB_ENTRIES];
bellard33417e72003-08-10 21:47:01 +0000165static int io_mem_nb;
pbrook6658ffb2007-03-16 23:58:11 +0000166static int io_mem_watch;
167#endif
bellard33417e72003-08-10 21:47:01 +0000168
bellard34865132003-10-05 14:28:56 +0000169/* log support */
170char *logfilename = "/tmp/qemu.log";
171FILE *logfile;
172int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000173static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000174
bellarde3db7222005-01-26 22:00:47 +0000175/* statistics */
176static int tlb_flush_count;
177static int tb_flush_count;
178static int tb_phys_invalidate_count;
179
blueswir1db7b5422007-05-26 17:36:03 +0000180#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
181typedef struct subpage_t {
182 target_phys_addr_t base;
blueswir13ee89922008-01-02 19:45:26 +0000183 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
184 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
185 void *opaque[TARGET_PAGE_SIZE][2][4];
blueswir1db7b5422007-05-26 17:36:03 +0000186} subpage_t;
187
bellard7cb69ca2008-05-10 10:55:51 +0000188#ifdef _WIN32
189static void map_exec(void *addr, long size)
190{
191 DWORD old_protect;
192 VirtualProtect(addr, size,
193 PAGE_EXECUTE_READWRITE, &old_protect);
194
195}
196#else
197static void map_exec(void *addr, long size)
198{
bellard43694152008-05-29 09:35:57 +0000199 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000200
bellard43694152008-05-29 09:35:57 +0000201 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000202 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000203 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000204
205 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000206 end += page_size - 1;
207 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000208
209 mprotect((void *)start, end - start,
210 PROT_READ | PROT_WRITE | PROT_EXEC);
211}
212#endif
213
bellardb346ff42003-06-15 20:05:50 +0000214static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000215{
bellard83fb7ad2004-07-05 21:25:26 +0000216 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000217 TARGET_PAGE_SIZE */
bellard67b915a2004-03-31 23:37:16 +0000218#ifdef _WIN32
bellardd5a8f072004-09-29 21:15:28 +0000219 {
220 SYSTEM_INFO system_info;
221 DWORD old_protect;
ths3b46e622007-09-17 08:09:54 +0000222
bellardd5a8f072004-09-29 21:15:28 +0000223 GetSystemInfo(&system_info);
224 qemu_real_host_page_size = system_info.dwPageSize;
bellardd5a8f072004-09-29 21:15:28 +0000225 }
bellard67b915a2004-03-31 23:37:16 +0000226#else
bellard83fb7ad2004-07-05 21:25:26 +0000227 qemu_real_host_page_size = getpagesize();
bellard67b915a2004-03-31 23:37:16 +0000228#endif
bellard83fb7ad2004-07-05 21:25:26 +0000229 if (qemu_host_page_size == 0)
230 qemu_host_page_size = qemu_real_host_page_size;
231 if (qemu_host_page_size < TARGET_PAGE_SIZE)
232 qemu_host_page_size = TARGET_PAGE_SIZE;
233 qemu_host_page_bits = 0;
234 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
235 qemu_host_page_bits++;
236 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000237 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
238 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000239
240#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
241 {
242 long long startaddr, endaddr;
243 FILE *f;
244 int n;
245
pbrookc8a706f2008-06-02 16:16:42 +0000246 mmap_lock();
pbrook07765902008-05-31 16:33:53 +0000247 last_brk = (unsigned long)sbrk(0);
balrog50a95692007-12-12 01:16:23 +0000248 f = fopen("/proc/self/maps", "r");
249 if (f) {
250 do {
251 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
252 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000253 startaddr = MIN(startaddr,
254 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
255 endaddr = MIN(endaddr,
256 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
pbrookb5fc9092008-05-29 13:56:10 +0000257 page_set_flags(startaddr & TARGET_PAGE_MASK,
balrog50a95692007-12-12 01:16:23 +0000258 TARGET_PAGE_ALIGN(endaddr),
259 PAGE_RESERVED);
260 }
261 } while (!feof(f));
262 fclose(f);
263 }
pbrookc8a706f2008-06-02 16:16:42 +0000264 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000265 }
266#endif
bellard54936002003-05-13 00:25:15 +0000267}
268
aurel3200f82b82008-04-27 21:12:55 +0000269static inline PageDesc *page_find_alloc(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000270{
bellard54936002003-05-13 00:25:15 +0000271 PageDesc **lp, *p;
272
pbrook17e23772008-06-09 13:47:45 +0000273#if TARGET_LONG_BITS > 32
274 /* Host memory outside guest VM. For 32-bit targets we have already
275 excluded high addresses. */
276 if (index > ((target_ulong)L2_SIZE * L1_SIZE * TARGET_PAGE_SIZE))
277 return NULL;
278#endif
bellard54936002003-05-13 00:25:15 +0000279 lp = &l1_map[index >> L2_BITS];
280 p = *lp;
281 if (!p) {
282 /* allocate if not found */
pbrook17e23772008-06-09 13:47:45 +0000283#if defined(CONFIG_USER_ONLY)
284 unsigned long addr;
285 size_t len = sizeof(PageDesc) * L2_SIZE;
286 /* Don't use qemu_malloc because it may recurse. */
287 p = mmap(0, len, PROT_READ | PROT_WRITE,
288 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
bellard54936002003-05-13 00:25:15 +0000289 *lp = p;
pbrook17e23772008-06-09 13:47:45 +0000290 addr = h2g(p);
291 if (addr == (target_ulong)addr) {
292 page_set_flags(addr & TARGET_PAGE_MASK,
293 TARGET_PAGE_ALIGN(addr + len),
294 PAGE_RESERVED);
295 }
296#else
297 p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
298 *lp = p;
299#endif
bellard54936002003-05-13 00:25:15 +0000300 }
301 return p + (index & (L2_SIZE - 1));
302}
303
aurel3200f82b82008-04-27 21:12:55 +0000304static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000305{
bellard54936002003-05-13 00:25:15 +0000306 PageDesc *p;
307
bellard54936002003-05-13 00:25:15 +0000308 p = l1_map[index >> L2_BITS];
309 if (!p)
310 return 0;
bellardfd6ce8f2003-05-14 19:00:11 +0000311 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000312}
313
bellard108c49b2005-07-24 12:55:09 +0000314static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000315{
bellard108c49b2005-07-24 12:55:09 +0000316 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000317 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000318
bellard108c49b2005-07-24 12:55:09 +0000319 p = (void **)l1_phys_map;
320#if TARGET_PHYS_ADDR_SPACE_BITS > 32
321
322#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
323#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
324#endif
325 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000326 p = *lp;
327 if (!p) {
328 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000329 if (!alloc)
330 return NULL;
331 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
332 memset(p, 0, sizeof(void *) * L1_SIZE);
333 *lp = p;
334 }
335#endif
336 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000337 pd = *lp;
338 if (!pd) {
339 int i;
bellard108c49b2005-07-24 12:55:09 +0000340 /* allocate if not found */
341 if (!alloc)
342 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000343 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
344 *lp = pd;
345 for (i = 0; i < L2_SIZE; i++)
346 pd[i].phys_offset = IO_MEM_UNASSIGNED;
bellard92e873b2004-05-21 14:52:29 +0000347 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000348 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000349}
350
bellard108c49b2005-07-24 12:55:09 +0000351static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000352{
bellard108c49b2005-07-24 12:55:09 +0000353 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000354}
355
bellard9fa3e852004-01-04 18:06:42 +0000356#if !defined(CONFIG_USER_ONLY)
bellard6a00d602005-11-21 23:25:50 +0000357static void tlb_protect_code(ram_addr_t ram_addr);
ths5fafdf22007-09-16 21:08:06 +0000358static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000359 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000360#define mmap_lock() do { } while(0)
361#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000362#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000363
bellard43694152008-05-29 09:35:57 +0000364#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
365
366#if defined(CONFIG_USER_ONLY)
367/* Currently it is not recommanded to allocate big chunks of data in
368 user mode. It will change when a dedicated libc will be used */
369#define USE_STATIC_CODE_GEN_BUFFER
370#endif
371
372#ifdef USE_STATIC_CODE_GEN_BUFFER
373static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
374#endif
375
bellard26a5f132008-05-28 12:30:31 +0000376void code_gen_alloc(unsigned long tb_size)
377{
bellard43694152008-05-29 09:35:57 +0000378#ifdef USE_STATIC_CODE_GEN_BUFFER
379 code_gen_buffer = static_code_gen_buffer;
380 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
381 map_exec(code_gen_buffer, code_gen_buffer_size);
382#else
bellard26a5f132008-05-28 12:30:31 +0000383 code_gen_buffer_size = tb_size;
384 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000385#if defined(CONFIG_USER_ONLY)
386 /* in user mode, phys_ram_size is not meaningful */
387 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
388#else
bellard26a5f132008-05-28 12:30:31 +0000389 /* XXX: needs ajustments */
390 code_gen_buffer_size = (int)(phys_ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000391#endif
bellard26a5f132008-05-28 12:30:31 +0000392 }
393 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
394 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
395 /* The code gen buffer location may have constraints depending on
396 the host cpu and OS */
397#if defined(__linux__)
398 {
399 int flags;
400 flags = MAP_PRIVATE | MAP_ANONYMOUS;
401#if defined(__x86_64__)
402 flags |= MAP_32BIT;
403 /* Cannot map more than that */
404 if (code_gen_buffer_size > (800 * 1024 * 1024))
405 code_gen_buffer_size = (800 * 1024 * 1024);
406#endif
407 code_gen_buffer = mmap(NULL, code_gen_buffer_size,
408 PROT_WRITE | PROT_READ | PROT_EXEC,
409 flags, -1, 0);
410 if (code_gen_buffer == MAP_FAILED) {
411 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
412 exit(1);
413 }
414 }
415#else
416 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
417 if (!code_gen_buffer) {
418 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
419 exit(1);
420 }
421 map_exec(code_gen_buffer, code_gen_buffer_size);
422#endif
bellard43694152008-05-29 09:35:57 +0000423#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000424 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
425 code_gen_buffer_max_size = code_gen_buffer_size -
426 code_gen_max_block_size();
427 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
428 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
429}
430
431/* Must be called before using the QEMU cpus. 'tb_size' is the size
432 (in bytes) allocated to the translation buffer. Zero means default
433 size. */
434void cpu_exec_init_all(unsigned long tb_size)
435{
bellard26a5f132008-05-28 12:30:31 +0000436 cpu_gen_init();
437 code_gen_alloc(tb_size);
438 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000439 page_init();
pbrooke2eef172008-06-08 01:09:01 +0000440#if !defined(CONFIG_USER_ONLY)
bellard26a5f132008-05-28 12:30:31 +0000441 io_mem_init();
pbrooke2eef172008-06-08 01:09:01 +0000442#endif
bellard26a5f132008-05-28 12:30:31 +0000443}
444
bellard6a00d602005-11-21 23:25:50 +0000445void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000446{
bellard6a00d602005-11-21 23:25:50 +0000447 CPUState **penv;
448 int cpu_index;
449
bellard6a00d602005-11-21 23:25:50 +0000450 env->next_cpu = NULL;
451 penv = &first_cpu;
452 cpu_index = 0;
453 while (*penv != NULL) {
454 penv = (CPUState **)&(*penv)->next_cpu;
455 cpu_index++;
456 }
457 env->cpu_index = cpu_index;
pbrook6658ffb2007-03-16 23:58:11 +0000458 env->nb_watchpoints = 0;
bellard6a00d602005-11-21 23:25:50 +0000459 *penv = env;
bellardfd6ce8f2003-05-14 19:00:11 +0000460}
461
bellard9fa3e852004-01-04 18:06:42 +0000462static inline void invalidate_page_bitmap(PageDesc *p)
463{
464 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000465 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000466 p->code_bitmap = NULL;
467 }
468 p->code_write_count = 0;
469}
470
bellardfd6ce8f2003-05-14 19:00:11 +0000471/* set to NULL all the 'first_tb' fields in all PageDescs */
472static void page_flush_tb(void)
473{
474 int i, j;
475 PageDesc *p;
476
477 for(i = 0; i < L1_SIZE; i++) {
478 p = l1_map[i];
479 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000480 for(j = 0; j < L2_SIZE; j++) {
481 p->first_tb = NULL;
482 invalidate_page_bitmap(p);
483 p++;
484 }
bellardfd6ce8f2003-05-14 19:00:11 +0000485 }
486 }
487}
488
489/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000490/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000491void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000492{
bellard6a00d602005-11-21 23:25:50 +0000493 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000494#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000495 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
496 (unsigned long)(code_gen_ptr - code_gen_buffer),
497 nb_tbs, nb_tbs > 0 ?
498 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000499#endif
bellard26a5f132008-05-28 12:30:31 +0000500 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000501 cpu_abort(env1, "Internal error: code buffer overflow\n");
502
bellardfd6ce8f2003-05-14 19:00:11 +0000503 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000504
bellard6a00d602005-11-21 23:25:50 +0000505 for(env = first_cpu; env != NULL; env = env->next_cpu) {
506 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
507 }
bellard9fa3e852004-01-04 18:06:42 +0000508
bellard8a8a6082004-10-03 13:36:49 +0000509 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000510 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000511
bellardfd6ce8f2003-05-14 19:00:11 +0000512 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000513 /* XXX: flush processor icache at this point if cache flush is
514 expensive */
bellarde3db7222005-01-26 22:00:47 +0000515 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000516}
517
518#ifdef DEBUG_TB_CHECK
519
j_mayerbc98a7e2007-04-04 07:55:12 +0000520static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000521{
522 TranslationBlock *tb;
523 int i;
524 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000525 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
526 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000527 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
528 address >= tb->pc + tb->size)) {
529 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000530 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000531 }
532 }
533 }
534}
535
536/* verify that all the pages have correct rights for code */
537static void tb_page_check(void)
538{
539 TranslationBlock *tb;
540 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000541
pbrook99773bd2006-04-16 15:14:59 +0000542 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
543 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000544 flags1 = page_get_flags(tb->pc);
545 flags2 = page_get_flags(tb->pc + tb->size - 1);
546 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
547 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000548 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000549 }
550 }
551 }
552}
553
bellardd4e81642003-05-25 16:46:15 +0000554void tb_jmp_check(TranslationBlock *tb)
555{
556 TranslationBlock *tb1;
557 unsigned int n1;
558
559 /* suppress any remaining jumps to this TB */
560 tb1 = tb->jmp_first;
561 for(;;) {
562 n1 = (long)tb1 & 3;
563 tb1 = (TranslationBlock *)((long)tb1 & ~3);
564 if (n1 == 2)
565 break;
566 tb1 = tb1->jmp_next[n1];
567 }
568 /* check end of list */
569 if (tb1 != tb) {
570 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
571 }
572}
573
bellardfd6ce8f2003-05-14 19:00:11 +0000574#endif
575
576/* invalidate one TB */
577static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
578 int next_offset)
579{
580 TranslationBlock *tb1;
581 for(;;) {
582 tb1 = *ptb;
583 if (tb1 == tb) {
584 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
585 break;
586 }
587 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
588 }
589}
590
bellard9fa3e852004-01-04 18:06:42 +0000591static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
592{
593 TranslationBlock *tb1;
594 unsigned int n1;
595
596 for(;;) {
597 tb1 = *ptb;
598 n1 = (long)tb1 & 3;
599 tb1 = (TranslationBlock *)((long)tb1 & ~3);
600 if (tb1 == tb) {
601 *ptb = tb1->page_next[n1];
602 break;
603 }
604 ptb = &tb1->page_next[n1];
605 }
606}
607
bellardd4e81642003-05-25 16:46:15 +0000608static inline void tb_jmp_remove(TranslationBlock *tb, int n)
609{
610 TranslationBlock *tb1, **ptb;
611 unsigned int n1;
612
613 ptb = &tb->jmp_next[n];
614 tb1 = *ptb;
615 if (tb1) {
616 /* find tb(n) in circular list */
617 for(;;) {
618 tb1 = *ptb;
619 n1 = (long)tb1 & 3;
620 tb1 = (TranslationBlock *)((long)tb1 & ~3);
621 if (n1 == n && tb1 == tb)
622 break;
623 if (n1 == 2) {
624 ptb = &tb1->jmp_first;
625 } else {
626 ptb = &tb1->jmp_next[n1];
627 }
628 }
629 /* now we can suppress tb(n) from the list */
630 *ptb = tb->jmp_next[n];
631
632 tb->jmp_next[n] = NULL;
633 }
634}
635
636/* reset the jump entry 'n' of a TB so that it is not chained to
637 another TB */
638static inline void tb_reset_jump(TranslationBlock *tb, int n)
639{
640 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
641}
642
pbrook2e70f6e2008-06-29 01:03:05 +0000643void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000644{
bellard6a00d602005-11-21 23:25:50 +0000645 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000646 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000647 unsigned int h, n1;
aurel3200f82b82008-04-27 21:12:55 +0000648 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000649 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000650
bellard9fa3e852004-01-04 18:06:42 +0000651 /* remove the TB from the hash list */
652 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
653 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000654 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000655 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000656
bellard9fa3e852004-01-04 18:06:42 +0000657 /* remove the TB from the page list */
658 if (tb->page_addr[0] != page_addr) {
659 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
660 tb_page_remove(&p->first_tb, tb);
661 invalidate_page_bitmap(p);
662 }
663 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
664 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
665 tb_page_remove(&p->first_tb, tb);
666 invalidate_page_bitmap(p);
667 }
668
bellard8a40a182005-11-20 10:35:40 +0000669 tb_invalidated_flag = 1;
670
671 /* remove the TB from the hash list */
672 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000673 for(env = first_cpu; env != NULL; env = env->next_cpu) {
674 if (env->tb_jmp_cache[h] == tb)
675 env->tb_jmp_cache[h] = NULL;
676 }
bellard8a40a182005-11-20 10:35:40 +0000677
678 /* suppress this TB from the two jump lists */
679 tb_jmp_remove(tb, 0);
680 tb_jmp_remove(tb, 1);
681
682 /* suppress any remaining jumps to this TB */
683 tb1 = tb->jmp_first;
684 for(;;) {
685 n1 = (long)tb1 & 3;
686 if (n1 == 2)
687 break;
688 tb1 = (TranslationBlock *)((long)tb1 & ~3);
689 tb2 = tb1->jmp_next[n1];
690 tb_reset_jump(tb1, n1);
691 tb1->jmp_next[n1] = NULL;
692 tb1 = tb2;
693 }
694 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
695
bellarde3db7222005-01-26 22:00:47 +0000696 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000697}
698
699static inline void set_bits(uint8_t *tab, int start, int len)
700{
701 int end, mask, end1;
702
703 end = start + len;
704 tab += start >> 3;
705 mask = 0xff << (start & 7);
706 if ((start & ~7) == (end & ~7)) {
707 if (start < end) {
708 mask &= ~(0xff << (end & 7));
709 *tab |= mask;
710 }
711 } else {
712 *tab++ |= mask;
713 start = (start + 8) & ~7;
714 end1 = end & ~7;
715 while (start < end1) {
716 *tab++ = 0xff;
717 start += 8;
718 }
719 if (start < end) {
720 mask = ~(0xff << (end & 7));
721 *tab |= mask;
722 }
723 }
724}
725
726static void build_page_bitmap(PageDesc *p)
727{
728 int n, tb_start, tb_end;
729 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000730
pbrookb2a70812008-06-09 13:57:23 +0000731 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000732 if (!p->code_bitmap)
733 return;
bellard9fa3e852004-01-04 18:06:42 +0000734
735 tb = p->first_tb;
736 while (tb != NULL) {
737 n = (long)tb & 3;
738 tb = (TranslationBlock *)((long)tb & ~3);
739 /* NOTE: this is subtle as a TB may span two physical pages */
740 if (n == 0) {
741 /* NOTE: tb_end may be after the end of the page, but
742 it is not a problem */
743 tb_start = tb->pc & ~TARGET_PAGE_MASK;
744 tb_end = tb_start + tb->size;
745 if (tb_end > TARGET_PAGE_SIZE)
746 tb_end = TARGET_PAGE_SIZE;
747 } else {
748 tb_start = 0;
749 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
750 }
751 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
752 tb = tb->page_next[n];
753 }
754}
755
pbrook2e70f6e2008-06-29 01:03:05 +0000756TranslationBlock *tb_gen_code(CPUState *env,
757 target_ulong pc, target_ulong cs_base,
758 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000759{
760 TranslationBlock *tb;
761 uint8_t *tc_ptr;
762 target_ulong phys_pc, phys_page2, virt_page2;
763 int code_gen_size;
764
bellardc27004e2005-01-03 23:35:10 +0000765 phys_pc = get_phys_addr_code(env, pc);
766 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000767 if (!tb) {
768 /* flush must be done */
769 tb_flush(env);
770 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000771 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000772 /* Don't forget to invalidate previous TB info. */
773 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000774 }
775 tc_ptr = code_gen_ptr;
776 tb->tc_ptr = tc_ptr;
777 tb->cs_base = cs_base;
778 tb->flags = flags;
779 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000780 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000781 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 +0000782
bellardd720b932004-04-25 17:57:43 +0000783 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000784 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000785 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000786 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000787 phys_page2 = get_phys_addr_code(env, virt_page2);
788 }
789 tb_link_phys(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +0000790 return tb;
bellardd720b932004-04-25 17:57:43 +0000791}
ths3b46e622007-09-17 08:09:54 +0000792
bellard9fa3e852004-01-04 18:06:42 +0000793/* invalidate all TBs which intersect with the target physical page
794 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000795 the same physical page. 'is_cpu_write_access' should be true if called
796 from a real cpu write access: the virtual CPU will exit the current
797 TB if code is modified inside this TB. */
aurel3200f82b82008-04-27 21:12:55 +0000798void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000799 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000800{
bellardd720b932004-04-25 17:57:43 +0000801 int n, current_tb_modified, current_tb_not_found, current_flags;
bellardd720b932004-04-25 17:57:43 +0000802 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000803 PageDesc *p;
bellardea1c1802004-06-14 18:56:36 +0000804 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
bellard9fa3e852004-01-04 18:06:42 +0000805 target_ulong tb_start, tb_end;
bellardd720b932004-04-25 17:57:43 +0000806 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000807
808 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000809 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000810 return;
ths5fafdf22007-09-16 21:08:06 +0000811 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000812 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
813 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000814 /* build code bitmap */
815 build_page_bitmap(p);
816 }
817
818 /* we remove all the TBs in the range [start, end[ */
819 /* XXX: see if in some cases it could be faster to invalidate all the code */
bellardd720b932004-04-25 17:57:43 +0000820 current_tb_not_found = is_cpu_write_access;
821 current_tb_modified = 0;
822 current_tb = NULL; /* avoid warning */
823 current_pc = 0; /* avoid warning */
824 current_cs_base = 0; /* avoid warning */
825 current_flags = 0; /* avoid warning */
bellard9fa3e852004-01-04 18:06:42 +0000826 tb = p->first_tb;
827 while (tb != NULL) {
828 n = (long)tb & 3;
829 tb = (TranslationBlock *)((long)tb & ~3);
830 tb_next = tb->page_next[n];
831 /* NOTE: this is subtle as a TB may span two physical pages */
832 if (n == 0) {
833 /* NOTE: tb_end may be after the end of the page, but
834 it is not a problem */
835 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
836 tb_end = tb_start + tb->size;
837 } else {
838 tb_start = tb->page_addr[1];
839 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
840 }
841 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000842#ifdef TARGET_HAS_PRECISE_SMC
843 if (current_tb_not_found) {
844 current_tb_not_found = 0;
845 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000846 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +0000847 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +0000848 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +0000849 }
850 }
851 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +0000852 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +0000853 /* If we are modifying the current TB, we must stop
854 its execution. We could be more precise by checking
855 that the modification is after the current PC, but it
856 would require a specialized function to partially
857 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000858
bellardd720b932004-04-25 17:57:43 +0000859 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000860 cpu_restore_state(current_tb, env,
pbrook2e70f6e2008-06-29 01:03:05 +0000861 env->mem_io_pc, NULL);
bellardd720b932004-04-25 17:57:43 +0000862#if defined(TARGET_I386)
863 current_flags = env->hflags;
864 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
865 current_cs_base = (target_ulong)env->segs[R_CS].base;
866 current_pc = current_cs_base + env->eip;
867#else
868#error unsupported CPU
869#endif
870 }
871#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000872 /* we need to do that to handle the case where a signal
873 occurs while doing tb_phys_invalidate() */
874 saved_tb = NULL;
875 if (env) {
876 saved_tb = env->current_tb;
877 env->current_tb = NULL;
878 }
bellard9fa3e852004-01-04 18:06:42 +0000879 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000880 if (env) {
881 env->current_tb = saved_tb;
882 if (env->interrupt_request && env->current_tb)
883 cpu_interrupt(env, env->interrupt_request);
884 }
bellard9fa3e852004-01-04 18:06:42 +0000885 }
886 tb = tb_next;
887 }
888#if !defined(CONFIG_USER_ONLY)
889 /* if no code remaining, no need to continue to use slow writes */
890 if (!p->first_tb) {
891 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +0000892 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +0000893 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +0000894 }
895 }
896#endif
897#ifdef TARGET_HAS_PRECISE_SMC
898 if (current_tb_modified) {
899 /* we generate a block containing just the instruction
900 modifying the memory. It will ensure that it cannot modify
901 itself */
bellardea1c1802004-06-14 18:56:36 +0000902 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000903 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +0000904 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +0000905 }
906#endif
907}
908
909/* len must be <= 8 and start must be a multiple of len */
aurel3200f82b82008-04-27 21:12:55 +0000910static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +0000911{
912 PageDesc *p;
913 int offset, b;
bellard59817cc2004-02-16 22:01:13 +0000914#if 0
bellarda4193c82004-06-03 14:01:43 +0000915 if (1) {
916 if (loglevel) {
ths5fafdf22007-09-16 21:08:06 +0000917 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
pbrook2e70f6e2008-06-29 01:03:05 +0000918 cpu_single_env->mem_io_vaddr, len,
ths5fafdf22007-09-16 21:08:06 +0000919 cpu_single_env->eip,
bellarda4193c82004-06-03 14:01:43 +0000920 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
921 }
bellard59817cc2004-02-16 22:01:13 +0000922 }
923#endif
bellard9fa3e852004-01-04 18:06:42 +0000924 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000925 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000926 return;
927 if (p->code_bitmap) {
928 offset = start & ~TARGET_PAGE_MASK;
929 b = p->code_bitmap[offset >> 3] >> (offset & 7);
930 if (b & ((1 << len) - 1))
931 goto do_invalidate;
932 } else {
933 do_invalidate:
bellardd720b932004-04-25 17:57:43 +0000934 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +0000935 }
936}
937
bellard9fa3e852004-01-04 18:06:42 +0000938#if !defined(CONFIG_SOFTMMU)
aurel3200f82b82008-04-27 21:12:55 +0000939static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +0000940 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +0000941{
bellardd720b932004-04-25 17:57:43 +0000942 int n, current_flags, current_tb_modified;
943 target_ulong current_pc, current_cs_base;
bellard9fa3e852004-01-04 18:06:42 +0000944 PageDesc *p;
bellardd720b932004-04-25 17:57:43 +0000945 TranslationBlock *tb, *current_tb;
946#ifdef TARGET_HAS_PRECISE_SMC
947 CPUState *env = cpu_single_env;
948#endif
bellard9fa3e852004-01-04 18:06:42 +0000949
950 addr &= TARGET_PAGE_MASK;
951 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000952 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +0000953 return;
954 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +0000955 current_tb_modified = 0;
956 current_tb = NULL;
957 current_pc = 0; /* avoid warning */
958 current_cs_base = 0; /* avoid warning */
959 current_flags = 0; /* avoid warning */
960#ifdef TARGET_HAS_PRECISE_SMC
961 if (tb && pc != 0) {
962 current_tb = tb_find_pc(pc);
963 }
964#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000965 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +0000966 n = (long)tb & 3;
967 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +0000968#ifdef TARGET_HAS_PRECISE_SMC
969 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +0000970 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +0000971 /* If we are modifying the current TB, we must stop
972 its execution. We could be more precise by checking
973 that the modification is after the current PC, but it
974 would require a specialized function to partially
975 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000976
bellardd720b932004-04-25 17:57:43 +0000977 current_tb_modified = 1;
978 cpu_restore_state(current_tb, env, pc, puc);
979#if defined(TARGET_I386)
980 current_flags = env->hflags;
981 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
982 current_cs_base = (target_ulong)env->segs[R_CS].base;
983 current_pc = current_cs_base + env->eip;
984#else
985#error unsupported CPU
986#endif
987 }
988#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000989 tb_phys_invalidate(tb, addr);
990 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +0000991 }
992 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +0000993#ifdef TARGET_HAS_PRECISE_SMC
994 if (current_tb_modified) {
995 /* we generate a block containing just the instruction
996 modifying the memory. It will ensure that it cannot modify
997 itself */
bellardea1c1802004-06-14 18:56:36 +0000998 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000999 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001000 cpu_resume_from_signal(env, puc);
1001 }
1002#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001003}
bellard9fa3e852004-01-04 18:06:42 +00001004#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001005
1006/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001007static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +00001008 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001009{
1010 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +00001011 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001012
bellard9fa3e852004-01-04 18:06:42 +00001013 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +00001014 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001015 tb->page_next[n] = p->first_tb;
1016 last_first_tb = p->first_tb;
1017 p->first_tb = (TranslationBlock *)((long)tb | n);
1018 invalidate_page_bitmap(p);
1019
bellard107db442004-06-22 18:48:46 +00001020#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001021
bellard9fa3e852004-01-04 18:06:42 +00001022#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001023 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001024 target_ulong addr;
1025 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001026 int prot;
1027
bellardfd6ce8f2003-05-14 19:00:11 +00001028 /* force the host page as non writable (writes will have a
1029 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001030 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001031 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001032 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1033 addr += TARGET_PAGE_SIZE) {
1034
1035 p2 = page_find (addr >> TARGET_PAGE_BITS);
1036 if (!p2)
1037 continue;
1038 prot |= p2->flags;
1039 p2->flags &= ~PAGE_WRITE;
1040 page_get_flags(addr);
1041 }
ths5fafdf22007-09-16 21:08:06 +00001042 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001043 (prot & PAGE_BITS) & ~PAGE_WRITE);
1044#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001045 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001046 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001047#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001048 }
bellard9fa3e852004-01-04 18:06:42 +00001049#else
1050 /* if some code is already present, then the pages are already
1051 protected. So we handle the case where only the first TB is
1052 allocated in a physical page */
1053 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001054 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001055 }
1056#endif
bellardd720b932004-04-25 17:57:43 +00001057
1058#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001059}
1060
1061/* Allocate a new translation block. Flush the translation buffer if
1062 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001063TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001064{
1065 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001066
bellard26a5f132008-05-28 12:30:31 +00001067 if (nb_tbs >= code_gen_max_blocks ||
1068 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001069 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001070 tb = &tbs[nb_tbs++];
1071 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001072 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001073 return tb;
1074}
1075
pbrook2e70f6e2008-06-29 01:03:05 +00001076void tb_free(TranslationBlock *tb)
1077{
1078 /* In practice this is mostly used for single use temorary TB
1079 Ignore the hard cases and just back up if this TB happens to
1080 be the last one generated. */
1081 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1082 code_gen_ptr = tb->tc_ptr;
1083 nb_tbs--;
1084 }
1085}
1086
bellard9fa3e852004-01-04 18:06:42 +00001087/* add a new TB and link it to the physical page tables. phys_page2 is
1088 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001089void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001090 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001091{
bellard9fa3e852004-01-04 18:06:42 +00001092 unsigned int h;
1093 TranslationBlock **ptb;
1094
pbrookc8a706f2008-06-02 16:16:42 +00001095 /* Grab the mmap lock to stop another thread invalidating this TB
1096 before we are done. */
1097 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001098 /* add in the physical hash table */
1099 h = tb_phys_hash_func(phys_pc);
1100 ptb = &tb_phys_hash[h];
1101 tb->phys_hash_next = *ptb;
1102 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001103
1104 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001105 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1106 if (phys_page2 != -1)
1107 tb_alloc_page(tb, 1, phys_page2);
1108 else
1109 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001110
bellardd4e81642003-05-25 16:46:15 +00001111 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1112 tb->jmp_next[0] = NULL;
1113 tb->jmp_next[1] = NULL;
1114
1115 /* init original jump addresses */
1116 if (tb->tb_next_offset[0] != 0xffff)
1117 tb_reset_jump(tb, 0);
1118 if (tb->tb_next_offset[1] != 0xffff)
1119 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001120
1121#ifdef DEBUG_TB_CHECK
1122 tb_page_check();
1123#endif
pbrookc8a706f2008-06-02 16:16:42 +00001124 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001125}
1126
bellarda513fe12003-05-27 23:29:48 +00001127/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1128 tb[1].tc_ptr. Return NULL if not found */
1129TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1130{
1131 int m_min, m_max, m;
1132 unsigned long v;
1133 TranslationBlock *tb;
1134
1135 if (nb_tbs <= 0)
1136 return NULL;
1137 if (tc_ptr < (unsigned long)code_gen_buffer ||
1138 tc_ptr >= (unsigned long)code_gen_ptr)
1139 return NULL;
1140 /* binary search (cf Knuth) */
1141 m_min = 0;
1142 m_max = nb_tbs - 1;
1143 while (m_min <= m_max) {
1144 m = (m_min + m_max) >> 1;
1145 tb = &tbs[m];
1146 v = (unsigned long)tb->tc_ptr;
1147 if (v == tc_ptr)
1148 return tb;
1149 else if (tc_ptr < v) {
1150 m_max = m - 1;
1151 } else {
1152 m_min = m + 1;
1153 }
ths5fafdf22007-09-16 21:08:06 +00001154 }
bellarda513fe12003-05-27 23:29:48 +00001155 return &tbs[m_max];
1156}
bellard75012672003-06-21 13:11:07 +00001157
bellardea041c02003-06-25 16:16:50 +00001158static void tb_reset_jump_recursive(TranslationBlock *tb);
1159
1160static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1161{
1162 TranslationBlock *tb1, *tb_next, **ptb;
1163 unsigned int n1;
1164
1165 tb1 = tb->jmp_next[n];
1166 if (tb1 != NULL) {
1167 /* find head of list */
1168 for(;;) {
1169 n1 = (long)tb1 & 3;
1170 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1171 if (n1 == 2)
1172 break;
1173 tb1 = tb1->jmp_next[n1];
1174 }
1175 /* we are now sure now that tb jumps to tb1 */
1176 tb_next = tb1;
1177
1178 /* remove tb from the jmp_first list */
1179 ptb = &tb_next->jmp_first;
1180 for(;;) {
1181 tb1 = *ptb;
1182 n1 = (long)tb1 & 3;
1183 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1184 if (n1 == n && tb1 == tb)
1185 break;
1186 ptb = &tb1->jmp_next[n1];
1187 }
1188 *ptb = tb->jmp_next[n];
1189 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001190
bellardea041c02003-06-25 16:16:50 +00001191 /* suppress the jump to next tb in generated code */
1192 tb_reset_jump(tb, n);
1193
bellard01243112004-01-04 15:48:17 +00001194 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001195 tb_reset_jump_recursive(tb_next);
1196 }
1197}
1198
1199static void tb_reset_jump_recursive(TranslationBlock *tb)
1200{
1201 tb_reset_jump_recursive2(tb, 0);
1202 tb_reset_jump_recursive2(tb, 1);
1203}
1204
bellard1fddef42005-04-17 19:16:13 +00001205#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001206static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1207{
j_mayer9b3c35e2007-04-07 11:21:28 +00001208 target_phys_addr_t addr;
1209 target_ulong pd;
pbrookc2f07f82006-04-08 17:14:56 +00001210 ram_addr_t ram_addr;
1211 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001212
pbrookc2f07f82006-04-08 17:14:56 +00001213 addr = cpu_get_phys_page_debug(env, pc);
1214 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1215 if (!p) {
1216 pd = IO_MEM_UNASSIGNED;
1217 } else {
1218 pd = p->phys_offset;
1219 }
1220 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001221 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001222}
bellardc27004e2005-01-03 23:35:10 +00001223#endif
bellardd720b932004-04-25 17:57:43 +00001224
pbrook6658ffb2007-03-16 23:58:11 +00001225/* Add a watchpoint. */
pbrook0f459d12008-06-09 00:20:13 +00001226int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type)
pbrook6658ffb2007-03-16 23:58:11 +00001227{
1228 int i;
1229
1230 for (i = 0; i < env->nb_watchpoints; i++) {
1231 if (addr == env->watchpoint[i].vaddr)
1232 return 0;
1233 }
1234 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1235 return -1;
1236
1237 i = env->nb_watchpoints++;
1238 env->watchpoint[i].vaddr = addr;
pbrook0f459d12008-06-09 00:20:13 +00001239 env->watchpoint[i].type = type;
pbrook6658ffb2007-03-16 23:58:11 +00001240 tlb_flush_page(env, addr);
1241 /* FIXME: This flush is needed because of the hack to make memory ops
1242 terminate the TB. It can be removed once the proper IO trap and
1243 re-execute bits are in. */
1244 tb_flush(env);
1245 return i;
1246}
1247
1248/* Remove a watchpoint. */
1249int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1250{
1251 int i;
1252
1253 for (i = 0; i < env->nb_watchpoints; i++) {
1254 if (addr == env->watchpoint[i].vaddr) {
1255 env->nb_watchpoints--;
1256 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1257 tlb_flush_page(env, addr);
1258 return 0;
1259 }
1260 }
1261 return -1;
1262}
1263
edgar_igl7d03f822008-05-17 18:58:29 +00001264/* Remove all watchpoints. */
1265void cpu_watchpoint_remove_all(CPUState *env) {
1266 int i;
1267
1268 for (i = 0; i < env->nb_watchpoints; i++) {
1269 tlb_flush_page(env, env->watchpoint[i].vaddr);
1270 }
1271 env->nb_watchpoints = 0;
1272}
1273
bellardc33a3462003-07-29 20:50:33 +00001274/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1275 breakpoint is reached */
bellard2e126692004-04-25 21:28:44 +00001276int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001277{
bellard1fddef42005-04-17 19:16:13 +00001278#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001279 int i;
ths3b46e622007-09-17 08:09:54 +00001280
bellard4c3a88a2003-07-26 12:06:08 +00001281 for(i = 0; i < env->nb_breakpoints; i++) {
1282 if (env->breakpoints[i] == pc)
1283 return 0;
1284 }
1285
1286 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1287 return -1;
1288 env->breakpoints[env->nb_breakpoints++] = pc;
ths3b46e622007-09-17 08:09:54 +00001289
bellardd720b932004-04-25 17:57:43 +00001290 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001291 return 0;
1292#else
1293 return -1;
1294#endif
1295}
1296
edgar_igl7d03f822008-05-17 18:58:29 +00001297/* remove all breakpoints */
1298void cpu_breakpoint_remove_all(CPUState *env) {
1299#if defined(TARGET_HAS_ICE)
1300 int i;
1301 for(i = 0; i < env->nb_breakpoints; i++) {
1302 breakpoint_invalidate(env, env->breakpoints[i]);
1303 }
1304 env->nb_breakpoints = 0;
1305#endif
1306}
1307
bellard4c3a88a2003-07-26 12:06:08 +00001308/* remove a breakpoint */
bellard2e126692004-04-25 21:28:44 +00001309int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
bellard4c3a88a2003-07-26 12:06:08 +00001310{
bellard1fddef42005-04-17 19:16:13 +00001311#if defined(TARGET_HAS_ICE)
bellard4c3a88a2003-07-26 12:06:08 +00001312 int i;
1313 for(i = 0; i < env->nb_breakpoints; i++) {
1314 if (env->breakpoints[i] == pc)
1315 goto found;
1316 }
1317 return -1;
1318 found:
bellard4c3a88a2003-07-26 12:06:08 +00001319 env->nb_breakpoints--;
bellard1fddef42005-04-17 19:16:13 +00001320 if (i < env->nb_breakpoints)
1321 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
bellardd720b932004-04-25 17:57:43 +00001322
1323 breakpoint_invalidate(env, pc);
bellard4c3a88a2003-07-26 12:06:08 +00001324 return 0;
1325#else
1326 return -1;
1327#endif
1328}
1329
bellardc33a3462003-07-29 20:50:33 +00001330/* enable or disable single step mode. EXCP_DEBUG is returned by the
1331 CPU loop after each instruction */
1332void cpu_single_step(CPUState *env, int enabled)
1333{
bellard1fddef42005-04-17 19:16:13 +00001334#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001335 if (env->singlestep_enabled != enabled) {
1336 env->singlestep_enabled = enabled;
1337 /* must flush all the translated code to avoid inconsistancies */
bellard9fa3e852004-01-04 18:06:42 +00001338 /* XXX: only flush what is necessary */
bellard01243112004-01-04 15:48:17 +00001339 tb_flush(env);
bellardc33a3462003-07-29 20:50:33 +00001340 }
1341#endif
1342}
1343
bellard34865132003-10-05 14:28:56 +00001344/* enable or disable low levels log */
1345void cpu_set_log(int log_flags)
1346{
1347 loglevel = log_flags;
1348 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001349 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001350 if (!logfile) {
1351 perror(logfilename);
1352 _exit(1);
1353 }
bellard9fa3e852004-01-04 18:06:42 +00001354#if !defined(CONFIG_SOFTMMU)
1355 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1356 {
1357 static uint8_t logfile_buf[4096];
1358 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1359 }
1360#else
bellard34865132003-10-05 14:28:56 +00001361 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001362#endif
pbrooke735b912007-06-30 13:53:24 +00001363 log_append = 1;
1364 }
1365 if (!loglevel && logfile) {
1366 fclose(logfile);
1367 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001368 }
1369}
1370
1371void cpu_set_log_filename(const char *filename)
1372{
1373 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001374 if (logfile) {
1375 fclose(logfile);
1376 logfile = NULL;
1377 }
1378 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001379}
bellardc33a3462003-07-29 20:50:33 +00001380
bellard01243112004-01-04 15:48:17 +00001381/* mask must never be zero, except for A20 change call */
bellard68a79312003-06-30 13:12:32 +00001382void cpu_interrupt(CPUState *env, int mask)
bellardea041c02003-06-25 16:16:50 +00001383{
pbrookd5975362008-06-07 20:50:51 +00001384#if !defined(USE_NPTL)
bellardea041c02003-06-25 16:16:50 +00001385 TranslationBlock *tb;
aurel3215a51152008-03-28 22:29:15 +00001386 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
pbrookd5975362008-06-07 20:50:51 +00001387#endif
pbrook2e70f6e2008-06-29 01:03:05 +00001388 int old_mask;
bellard59817cc2004-02-16 22:01:13 +00001389
pbrook2e70f6e2008-06-29 01:03:05 +00001390 old_mask = env->interrupt_request;
pbrookd5975362008-06-07 20:50:51 +00001391 /* FIXME: This is probably not threadsafe. A different thread could
1392 be in the mittle of a read-modify-write operation. */
bellard68a79312003-06-30 13:12:32 +00001393 env->interrupt_request |= mask;
pbrookd5975362008-06-07 20:50:51 +00001394#if defined(USE_NPTL)
1395 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1396 problem and hope the cpu will stop of its own accord. For userspace
1397 emulation this often isn't actually as bad as it sounds. Often
1398 signals are used primarily to interrupt blocking syscalls. */
1399#else
pbrook2e70f6e2008-06-29 01:03:05 +00001400 if (use_icount) {
1401 env->icount_decr.u16.high = 0x8000;
1402#ifndef CONFIG_USER_ONLY
1403 /* CPU_INTERRUPT_EXIT isn't a real interrupt. It just means
1404 an async event happened and we need to process it. */
1405 if (!can_do_io(env)
1406 && (mask & ~(old_mask | CPU_INTERRUPT_EXIT)) != 0) {
1407 cpu_abort(env, "Raised interrupt while not in I/O function");
1408 }
1409#endif
1410 } else {
1411 tb = env->current_tb;
1412 /* if the cpu is currently executing code, we must unlink it and
1413 all the potentially executing TB */
1414 if (tb && !testandset(&interrupt_lock)) {
1415 env->current_tb = NULL;
1416 tb_reset_jump_recursive(tb);
1417 resetlock(&interrupt_lock);
1418 }
bellardea041c02003-06-25 16:16:50 +00001419 }
pbrookd5975362008-06-07 20:50:51 +00001420#endif
bellardea041c02003-06-25 16:16:50 +00001421}
1422
bellardb54ad042004-05-20 13:42:52 +00001423void cpu_reset_interrupt(CPUState *env, int mask)
1424{
1425 env->interrupt_request &= ~mask;
1426}
1427
bellardf193c792004-03-21 17:06:25 +00001428CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001429 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001430 "show generated host assembly code for each compiled TB" },
1431 { CPU_LOG_TB_IN_ASM, "in_asm",
1432 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001433 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001434 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001435 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001436 "show micro ops "
1437#ifdef TARGET_I386
1438 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001439#endif
blueswir1e01a1152008-03-14 17:37:11 +00001440 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001441 { CPU_LOG_INT, "int",
1442 "show interrupts/exceptions in short format" },
1443 { CPU_LOG_EXEC, "exec",
1444 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001445 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001446 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001447#ifdef TARGET_I386
1448 { CPU_LOG_PCALL, "pcall",
1449 "show protected mode far calls/returns/exceptions" },
1450#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001451#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001452 { CPU_LOG_IOPORT, "ioport",
1453 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001454#endif
bellardf193c792004-03-21 17:06:25 +00001455 { 0, NULL, NULL },
1456};
1457
1458static int cmp1(const char *s1, int n, const char *s2)
1459{
1460 if (strlen(s2) != n)
1461 return 0;
1462 return memcmp(s1, s2, n) == 0;
1463}
ths3b46e622007-09-17 08:09:54 +00001464
bellardf193c792004-03-21 17:06:25 +00001465/* takes a comma separated list of log masks. Return 0 if error. */
1466int cpu_str_to_log_mask(const char *str)
1467{
1468 CPULogItem *item;
1469 int mask;
1470 const char *p, *p1;
1471
1472 p = str;
1473 mask = 0;
1474 for(;;) {
1475 p1 = strchr(p, ',');
1476 if (!p1)
1477 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001478 if(cmp1(p,p1-p,"all")) {
1479 for(item = cpu_log_items; item->mask != 0; item++) {
1480 mask |= item->mask;
1481 }
1482 } else {
bellardf193c792004-03-21 17:06:25 +00001483 for(item = cpu_log_items; item->mask != 0; item++) {
1484 if (cmp1(p, p1 - p, item->name))
1485 goto found;
1486 }
1487 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001488 }
bellardf193c792004-03-21 17:06:25 +00001489 found:
1490 mask |= item->mask;
1491 if (*p1 != ',')
1492 break;
1493 p = p1 + 1;
1494 }
1495 return mask;
1496}
bellardea041c02003-06-25 16:16:50 +00001497
bellard75012672003-06-21 13:11:07 +00001498void cpu_abort(CPUState *env, const char *fmt, ...)
1499{
1500 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001501 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001502
1503 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001504 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001505 fprintf(stderr, "qemu: fatal: ");
1506 vfprintf(stderr, fmt, ap);
1507 fprintf(stderr, "\n");
1508#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001509 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1510#else
1511 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001512#endif
balrog924edca2007-06-10 14:07:13 +00001513 if (logfile) {
j_mayerf9373292007-09-29 12:18:20 +00001514 fprintf(logfile, "qemu: fatal: ");
pbrook493ae1f2007-11-23 16:53:59 +00001515 vfprintf(logfile, fmt, ap2);
j_mayerf9373292007-09-29 12:18:20 +00001516 fprintf(logfile, "\n");
1517#ifdef TARGET_I386
1518 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1519#else
1520 cpu_dump_state(env, logfile, fprintf, 0);
1521#endif
balrog924edca2007-06-10 14:07:13 +00001522 fflush(logfile);
1523 fclose(logfile);
1524 }
pbrook493ae1f2007-11-23 16:53:59 +00001525 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001526 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001527 abort();
1528}
1529
thsc5be9f02007-02-28 20:20:53 +00001530CPUState *cpu_copy(CPUState *env)
1531{
ths01ba9812007-12-09 02:22:57 +00001532 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001533 /* preserve chaining and index */
1534 CPUState *next_cpu = new_env->next_cpu;
1535 int cpu_index = new_env->cpu_index;
1536 memcpy(new_env, env, sizeof(CPUState));
1537 new_env->next_cpu = next_cpu;
1538 new_env->cpu_index = cpu_index;
1539 return new_env;
1540}
1541
bellard01243112004-01-04 15:48:17 +00001542#if !defined(CONFIG_USER_ONLY)
1543
edgar_igl5c751e92008-05-06 08:44:21 +00001544static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1545{
1546 unsigned int i;
1547
1548 /* Discard jump cache entries for any tb which might potentially
1549 overlap the flushed page. */
1550 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1551 memset (&env->tb_jmp_cache[i], 0,
1552 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1553
1554 i = tb_jmp_cache_hash_page(addr);
1555 memset (&env->tb_jmp_cache[i], 0,
1556 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1557}
1558
bellardee8b7022004-02-03 23:35:10 +00001559/* NOTE: if flush_global is true, also flush global entries (not
1560 implemented yet) */
1561void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001562{
bellard33417e72003-08-10 21:47:01 +00001563 int i;
bellard01243112004-01-04 15:48:17 +00001564
bellard9fa3e852004-01-04 18:06:42 +00001565#if defined(DEBUG_TLB)
1566 printf("tlb_flush:\n");
1567#endif
bellard01243112004-01-04 15:48:17 +00001568 /* must reset current TB so that interrupts cannot modify the
1569 links while we are modifying them */
1570 env->current_tb = NULL;
1571
bellard33417e72003-08-10 21:47:01 +00001572 for(i = 0; i < CPU_TLB_SIZE; i++) {
bellard84b7b8e2005-11-28 21:19:04 +00001573 env->tlb_table[0][i].addr_read = -1;
1574 env->tlb_table[0][i].addr_write = -1;
1575 env->tlb_table[0][i].addr_code = -1;
1576 env->tlb_table[1][i].addr_read = -1;
1577 env->tlb_table[1][i].addr_write = -1;
1578 env->tlb_table[1][i].addr_code = -1;
j_mayer6fa4cea2007-04-05 06:43:27 +00001579#if (NB_MMU_MODES >= 3)
1580 env->tlb_table[2][i].addr_read = -1;
1581 env->tlb_table[2][i].addr_write = -1;
1582 env->tlb_table[2][i].addr_code = -1;
1583#if (NB_MMU_MODES == 4)
1584 env->tlb_table[3][i].addr_read = -1;
1585 env->tlb_table[3][i].addr_write = -1;
1586 env->tlb_table[3][i].addr_code = -1;
1587#endif
1588#endif
bellard33417e72003-08-10 21:47:01 +00001589 }
bellard9fa3e852004-01-04 18:06:42 +00001590
bellard8a40a182005-11-20 10:35:40 +00001591 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001592
bellard0a962c02005-02-10 22:00:27 +00001593#ifdef USE_KQEMU
1594 if (env->kqemu_enabled) {
1595 kqemu_flush(env, flush_global);
1596 }
1597#endif
bellarde3db7222005-01-26 22:00:47 +00001598 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001599}
1600
bellard274da6b2004-05-20 21:56:27 +00001601static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001602{
ths5fafdf22007-09-16 21:08:06 +00001603 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001604 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001605 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001606 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001607 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001608 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1609 tlb_entry->addr_read = -1;
1610 tlb_entry->addr_write = -1;
1611 tlb_entry->addr_code = -1;
1612 }
bellard61382a52003-10-27 21:22:23 +00001613}
1614
bellard2e126692004-04-25 21:28:44 +00001615void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001616{
bellard8a40a182005-11-20 10:35:40 +00001617 int i;
bellard01243112004-01-04 15:48:17 +00001618
bellard9fa3e852004-01-04 18:06:42 +00001619#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001620 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001621#endif
bellard01243112004-01-04 15:48:17 +00001622 /* must reset current TB so that interrupts cannot modify the
1623 links while we are modifying them */
1624 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001625
bellard61382a52003-10-27 21:22:23 +00001626 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001627 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard84b7b8e2005-11-28 21:19:04 +00001628 tlb_flush_entry(&env->tlb_table[0][i], addr);
1629 tlb_flush_entry(&env->tlb_table[1][i], addr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001630#if (NB_MMU_MODES >= 3)
1631 tlb_flush_entry(&env->tlb_table[2][i], addr);
1632#if (NB_MMU_MODES == 4)
1633 tlb_flush_entry(&env->tlb_table[3][i], addr);
1634#endif
1635#endif
bellard01243112004-01-04 15:48:17 +00001636
edgar_igl5c751e92008-05-06 08:44:21 +00001637 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001638
bellard0a962c02005-02-10 22:00:27 +00001639#ifdef USE_KQEMU
1640 if (env->kqemu_enabled) {
1641 kqemu_flush_page(env, addr);
1642 }
1643#endif
bellard9fa3e852004-01-04 18:06:42 +00001644}
1645
bellard9fa3e852004-01-04 18:06:42 +00001646/* update the TLBs so that writes to code in the virtual page 'addr'
1647 can be detected */
bellard6a00d602005-11-21 23:25:50 +00001648static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001649{
ths5fafdf22007-09-16 21:08:06 +00001650 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001651 ram_addr + TARGET_PAGE_SIZE,
1652 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001653}
1654
bellard9fa3e852004-01-04 18:06:42 +00001655/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001656 tested for self modifying code */
ths5fafdf22007-09-16 21:08:06 +00001657static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001658 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001659{
bellard3a7d9292005-08-21 09:26:42 +00001660 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001661}
1662
ths5fafdf22007-09-16 21:08:06 +00001663static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001664 unsigned long start, unsigned long length)
1665{
1666 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001667 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1668 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001669 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00001670 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001671 }
1672 }
1673}
1674
bellard3a7d9292005-08-21 09:26:42 +00001675void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001676 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001677{
1678 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001679 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001680 int i, mask, len;
1681 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001682
1683 start &= TARGET_PAGE_MASK;
1684 end = TARGET_PAGE_ALIGN(end);
1685
1686 length = end - start;
1687 if (length == 0)
1688 return;
bellard0a962c02005-02-10 22:00:27 +00001689 len = length >> TARGET_PAGE_BITS;
bellard3a7d9292005-08-21 09:26:42 +00001690#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001691 /* XXX: should not depend on cpu context */
1692 env = first_cpu;
bellard3a7d9292005-08-21 09:26:42 +00001693 if (env->kqemu_enabled) {
bellardf23db162005-08-21 19:12:28 +00001694 ram_addr_t addr;
1695 addr = start;
1696 for(i = 0; i < len; i++) {
1697 kqemu_set_notdirty(env, addr);
1698 addr += TARGET_PAGE_SIZE;
1699 }
bellard3a7d9292005-08-21 09:26:42 +00001700 }
1701#endif
bellardf23db162005-08-21 19:12:28 +00001702 mask = ~dirty_flags;
1703 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1704 for(i = 0; i < len; i++)
1705 p[i] &= mask;
1706
bellard1ccde1c2004-02-06 19:46:14 +00001707 /* we modify the TLB cache so that the dirty bit will be set again
1708 when accessing the range */
bellard59817cc2004-02-16 22:01:13 +00001709 start1 = start + (unsigned long)phys_ram_base;
bellard6a00d602005-11-21 23:25:50 +00001710 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1711 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001712 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
bellard6a00d602005-11-21 23:25:50 +00001713 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001714 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
j_mayer6fa4cea2007-04-05 06:43:27 +00001715#if (NB_MMU_MODES >= 3)
1716 for(i = 0; i < CPU_TLB_SIZE; i++)
1717 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1718#if (NB_MMU_MODES == 4)
1719 for(i = 0; i < CPU_TLB_SIZE; i++)
1720 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1721#endif
1722#endif
bellard6a00d602005-11-21 23:25:50 +00001723 }
bellard1ccde1c2004-02-06 19:46:14 +00001724}
1725
bellard3a7d9292005-08-21 09:26:42 +00001726static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1727{
1728 ram_addr_t ram_addr;
1729
bellard84b7b8e2005-11-28 21:19:04 +00001730 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
ths5fafdf22007-09-16 21:08:06 +00001731 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
bellard3a7d9292005-08-21 09:26:42 +00001732 tlb_entry->addend - (unsigned long)phys_ram_base;
1733 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00001734 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001735 }
1736 }
1737}
1738
1739/* update the TLB according to the current state of the dirty bits */
1740void cpu_tlb_update_dirty(CPUState *env)
1741{
1742 int i;
1743 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001744 tlb_update_dirty(&env->tlb_table[0][i]);
bellard3a7d9292005-08-21 09:26:42 +00001745 for(i = 0; i < CPU_TLB_SIZE; i++)
bellard84b7b8e2005-11-28 21:19:04 +00001746 tlb_update_dirty(&env->tlb_table[1][i]);
j_mayer6fa4cea2007-04-05 06:43:27 +00001747#if (NB_MMU_MODES >= 3)
1748 for(i = 0; i < CPU_TLB_SIZE; i++)
1749 tlb_update_dirty(&env->tlb_table[2][i]);
1750#if (NB_MMU_MODES == 4)
1751 for(i = 0; i < CPU_TLB_SIZE; i++)
1752 tlb_update_dirty(&env->tlb_table[3][i]);
1753#endif
1754#endif
bellard3a7d9292005-08-21 09:26:42 +00001755}
1756
pbrook0f459d12008-06-09 00:20:13 +00001757static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001758{
pbrook0f459d12008-06-09 00:20:13 +00001759 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1760 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00001761}
1762
pbrook0f459d12008-06-09 00:20:13 +00001763/* update the TLB corresponding to virtual page vaddr
1764 so that it is no longer dirty */
1765static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001766{
bellard1ccde1c2004-02-06 19:46:14 +00001767 int i;
1768
pbrook0f459d12008-06-09 00:20:13 +00001769 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00001770 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
pbrook0f459d12008-06-09 00:20:13 +00001771 tlb_set_dirty1(&env->tlb_table[0][i], vaddr);
1772 tlb_set_dirty1(&env->tlb_table[1][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001773#if (NB_MMU_MODES >= 3)
pbrook0f459d12008-06-09 00:20:13 +00001774 tlb_set_dirty1(&env->tlb_table[2][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001775#if (NB_MMU_MODES == 4)
pbrook0f459d12008-06-09 00:20:13 +00001776 tlb_set_dirty1(&env->tlb_table[3][i], vaddr);
j_mayer6fa4cea2007-04-05 06:43:27 +00001777#endif
1778#endif
bellard9fa3e852004-01-04 18:06:42 +00001779}
1780
bellard59817cc2004-02-16 22:01:13 +00001781/* add a new TLB entry. At most one entry for a given virtual address
1782 is permitted. Return 0 if OK or 2 if the page could not be mapped
1783 (can only happen in non SOFTMMU mode for I/O pages or pages
1784 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001785int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1786 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001787 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001788{
bellard92e873b2004-05-21 14:52:29 +00001789 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001790 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001791 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001792 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00001793 target_ulong code_address;
bellard108c49b2005-07-24 12:55:09 +00001794 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001795 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001796 CPUTLBEntry *te;
pbrook6658ffb2007-03-16 23:58:11 +00001797 int i;
pbrook0f459d12008-06-09 00:20:13 +00001798 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00001799
bellard92e873b2004-05-21 14:52:29 +00001800 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001801 if (!p) {
1802 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001803 } else {
1804 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001805 }
1806#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001807 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1808 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001809#endif
1810
1811 ret = 0;
pbrook0f459d12008-06-09 00:20:13 +00001812 address = vaddr;
1813 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
1814 /* IO memory case (romd handled later) */
1815 address |= TLB_MMIO;
1816 }
1817 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1818 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
1819 /* Normal RAM. */
1820 iotlb = pd & TARGET_PAGE_MASK;
1821 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
1822 iotlb |= IO_MEM_NOTDIRTY;
1823 else
1824 iotlb |= IO_MEM_ROM;
1825 } else {
1826 /* IO handlers are currently passed a phsical address.
1827 It would be nice to pass an offset from the base address
1828 of that region. This would avoid having to special case RAM,
1829 and avoid full address decoding in every device.
1830 We can't use the high bits of pd for this because
1831 IO_MEM_ROMD uses these as a ram address. */
1832 iotlb = (pd & ~TARGET_PAGE_MASK) + paddr;
1833 }
pbrook6658ffb2007-03-16 23:58:11 +00001834
pbrook0f459d12008-06-09 00:20:13 +00001835 code_address = address;
1836 /* Make accesses to pages with watchpoints go via the
1837 watchpoint trap routines. */
1838 for (i = 0; i < env->nb_watchpoints; i++) {
1839 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1840 iotlb = io_mem_watch + paddr;
1841 /* TODO: The memory case can be optimized by not trapping
1842 reads of pages with a write breakpoint. */
1843 address |= TLB_MMIO;
pbrook6658ffb2007-03-16 23:58:11 +00001844 }
pbrook0f459d12008-06-09 00:20:13 +00001845 }
balrogd79acba2007-06-26 20:01:13 +00001846
pbrook0f459d12008-06-09 00:20:13 +00001847 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1848 env->iotlb[mmu_idx][index] = iotlb - vaddr;
1849 te = &env->tlb_table[mmu_idx][index];
1850 te->addend = addend - vaddr;
1851 if (prot & PAGE_READ) {
1852 te->addr_read = address;
1853 } else {
1854 te->addr_read = -1;
1855 }
edgar_igl5c751e92008-05-06 08:44:21 +00001856
pbrook0f459d12008-06-09 00:20:13 +00001857 if (prot & PAGE_EXEC) {
1858 te->addr_code = code_address;
1859 } else {
1860 te->addr_code = -1;
1861 }
1862 if (prot & PAGE_WRITE) {
1863 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
1864 (pd & IO_MEM_ROMD)) {
1865 /* Write access calls the I/O callback. */
1866 te->addr_write = address | TLB_MMIO;
1867 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
1868 !cpu_physical_memory_is_dirty(pd)) {
1869 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00001870 } else {
pbrook0f459d12008-06-09 00:20:13 +00001871 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00001872 }
pbrook0f459d12008-06-09 00:20:13 +00001873 } else {
1874 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00001875 }
bellard9fa3e852004-01-04 18:06:42 +00001876 return ret;
1877}
1878
bellard01243112004-01-04 15:48:17 +00001879#else
1880
bellardee8b7022004-02-03 23:35:10 +00001881void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00001882{
1883}
1884
bellard2e126692004-04-25 21:28:44 +00001885void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00001886{
1887}
1888
ths5fafdf22007-09-16 21:08:06 +00001889int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1890 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001891 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00001892{
bellard9fa3e852004-01-04 18:06:42 +00001893 return 0;
1894}
bellard33417e72003-08-10 21:47:01 +00001895
bellard9fa3e852004-01-04 18:06:42 +00001896/* dump memory mappings */
1897void page_dump(FILE *f)
1898{
1899 unsigned long start, end;
1900 int i, j, prot, prot1;
1901 PageDesc *p;
1902
1903 fprintf(f, "%-8s %-8s %-8s %s\n",
1904 "start", "end", "size", "prot");
1905 start = -1;
1906 end = -1;
1907 prot = 0;
1908 for(i = 0; i <= L1_SIZE; i++) {
1909 if (i < L1_SIZE)
1910 p = l1_map[i];
1911 else
1912 p = NULL;
1913 for(j = 0;j < L2_SIZE; j++) {
1914 if (!p)
1915 prot1 = 0;
1916 else
1917 prot1 = p[j].flags;
1918 if (prot1 != prot) {
1919 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
1920 if (start != -1) {
1921 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001922 start, end, end - start,
bellard9fa3e852004-01-04 18:06:42 +00001923 prot & PAGE_READ ? 'r' : '-',
1924 prot & PAGE_WRITE ? 'w' : '-',
1925 prot & PAGE_EXEC ? 'x' : '-');
1926 }
1927 if (prot1 != 0)
1928 start = end;
1929 else
1930 start = -1;
1931 prot = prot1;
1932 }
1933 if (!p)
1934 break;
1935 }
bellard33417e72003-08-10 21:47:01 +00001936 }
bellard33417e72003-08-10 21:47:01 +00001937}
1938
pbrook53a59602006-03-25 19:31:22 +00001939int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00001940{
bellard9fa3e852004-01-04 18:06:42 +00001941 PageDesc *p;
1942
1943 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00001944 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001945 return 0;
1946 return p->flags;
bellard33417e72003-08-10 21:47:01 +00001947}
1948
bellard9fa3e852004-01-04 18:06:42 +00001949/* modify the flags of a page and invalidate the code if
1950 necessary. The flag PAGE_WRITE_ORG is positionned automatically
1951 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00001952void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00001953{
1954 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00001955 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00001956
pbrookc8a706f2008-06-02 16:16:42 +00001957 /* mmap_lock should already be held. */
bellard9fa3e852004-01-04 18:06:42 +00001958 start = start & TARGET_PAGE_MASK;
1959 end = TARGET_PAGE_ALIGN(end);
1960 if (flags & PAGE_WRITE)
1961 flags |= PAGE_WRITE_ORG;
bellard9fa3e852004-01-04 18:06:42 +00001962 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1963 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
pbrook17e23772008-06-09 13:47:45 +00001964 /* We may be called for host regions that are outside guest
1965 address space. */
1966 if (!p)
1967 return;
bellard9fa3e852004-01-04 18:06:42 +00001968 /* if the write protection is set, then we invalidate the code
1969 inside */
ths5fafdf22007-09-16 21:08:06 +00001970 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00001971 (flags & PAGE_WRITE) &&
1972 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00001973 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001974 }
1975 p->flags = flags;
1976 }
bellard9fa3e852004-01-04 18:06:42 +00001977}
1978
ths3d97b402007-11-02 19:02:07 +00001979int page_check_range(target_ulong start, target_ulong len, int flags)
1980{
1981 PageDesc *p;
1982 target_ulong end;
1983 target_ulong addr;
1984
1985 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
1986 start = start & TARGET_PAGE_MASK;
1987
1988 if( end < start )
1989 /* we've wrapped around */
1990 return -1;
1991 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
1992 p = page_find(addr >> TARGET_PAGE_BITS);
1993 if( !p )
1994 return -1;
1995 if( !(p->flags & PAGE_VALID) )
1996 return -1;
1997
bellarddae32702007-11-14 10:51:00 +00001998 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00001999 return -1;
bellarddae32702007-11-14 10:51:00 +00002000 if (flags & PAGE_WRITE) {
2001 if (!(p->flags & PAGE_WRITE_ORG))
2002 return -1;
2003 /* unprotect the page if it was put read-only because it
2004 contains translated code */
2005 if (!(p->flags & PAGE_WRITE)) {
2006 if (!page_unprotect(addr, 0, NULL))
2007 return -1;
2008 }
2009 return 0;
2010 }
ths3d97b402007-11-02 19:02:07 +00002011 }
2012 return 0;
2013}
2014
bellard9fa3e852004-01-04 18:06:42 +00002015/* called from signal handler: invalidate the code and unprotect the
2016 page. Return TRUE if the fault was succesfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002017int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002018{
2019 unsigned int page_index, prot, pindex;
2020 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002021 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002022
pbrookc8a706f2008-06-02 16:16:42 +00002023 /* Technically this isn't safe inside a signal handler. However we
2024 know this only ever happens in a synchronous SEGV handler, so in
2025 practice it seems to be ok. */
2026 mmap_lock();
2027
bellard83fb7ad2004-07-05 21:25:26 +00002028 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002029 page_index = host_start >> TARGET_PAGE_BITS;
2030 p1 = page_find(page_index);
pbrookc8a706f2008-06-02 16:16:42 +00002031 if (!p1) {
2032 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002033 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002034 }
bellard83fb7ad2004-07-05 21:25:26 +00002035 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002036 p = p1;
2037 prot = 0;
2038 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2039 prot |= p->flags;
2040 p++;
2041 }
2042 /* if the page was really writable, then we change its
2043 protection back to writable */
2044 if (prot & PAGE_WRITE_ORG) {
2045 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2046 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002047 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002048 (prot & PAGE_BITS) | PAGE_WRITE);
2049 p1[pindex].flags |= PAGE_WRITE;
2050 /* and since the content will be modified, we must invalidate
2051 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002052 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002053#ifdef DEBUG_TB_CHECK
2054 tb_invalidate_check(address);
2055#endif
pbrookc8a706f2008-06-02 16:16:42 +00002056 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002057 return 1;
2058 }
2059 }
pbrookc8a706f2008-06-02 16:16:42 +00002060 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002061 return 0;
2062}
2063
bellard6a00d602005-11-21 23:25:50 +00002064static inline void tlb_set_dirty(CPUState *env,
2065 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002066{
2067}
bellard9fa3e852004-01-04 18:06:42 +00002068#endif /* defined(CONFIG_USER_ONLY) */
2069
pbrooke2eef172008-06-08 01:09:01 +00002070#if !defined(CONFIG_USER_ONLY)
blueswir1db7b5422007-05-26 17:36:03 +00002071static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002072 ram_addr_t memory);
2073static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2074 ram_addr_t orig_memory);
blueswir1db7b5422007-05-26 17:36:03 +00002075#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2076 need_subpage) \
2077 do { \
2078 if (addr > start_addr) \
2079 start_addr2 = 0; \
2080 else { \
2081 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2082 if (start_addr2 > 0) \
2083 need_subpage = 1; \
2084 } \
2085 \
blueswir149e9fba2007-05-30 17:25:06 +00002086 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002087 end_addr2 = TARGET_PAGE_SIZE - 1; \
2088 else { \
2089 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2090 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2091 need_subpage = 1; \
2092 } \
2093 } while (0)
2094
bellard33417e72003-08-10 21:47:01 +00002095/* register physical memory. 'size' must be a multiple of the target
2096 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2097 io memory page */
ths5fafdf22007-09-16 21:08:06 +00002098void cpu_register_physical_memory(target_phys_addr_t start_addr,
aurel3200f82b82008-04-27 21:12:55 +00002099 ram_addr_t size,
2100 ram_addr_t phys_offset)
bellard33417e72003-08-10 21:47:01 +00002101{
bellard108c49b2005-07-24 12:55:09 +00002102 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002103 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002104 CPUState *env;
aurel3200f82b82008-04-27 21:12:55 +00002105 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002106 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002107
bellardda260242008-05-30 20:48:25 +00002108#ifdef USE_KQEMU
2109 /* XXX: should not depend on cpu context */
2110 env = first_cpu;
2111 if (env->kqemu_enabled) {
2112 kqemu_set_phys_mem(start_addr, size, phys_offset);
2113 }
2114#endif
bellard5fd386f2004-05-23 21:11:22 +00002115 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
blueswir149e9fba2007-05-30 17:25:06 +00002116 end_addr = start_addr + (target_phys_addr_t)size;
2117 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002118 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2119 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
aurel3200f82b82008-04-27 21:12:55 +00002120 ram_addr_t orig_memory = p->phys_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002121 target_phys_addr_t start_addr2, end_addr2;
2122 int need_subpage = 0;
2123
2124 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2125 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002126 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002127 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2128 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2129 &p->phys_offset, orig_memory);
2130 } else {
2131 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2132 >> IO_MEM_SHIFT];
2133 }
2134 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2135 } else {
2136 p->phys_offset = phys_offset;
2137 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2138 (phys_offset & IO_MEM_ROMD))
2139 phys_offset += TARGET_PAGE_SIZE;
2140 }
2141 } else {
2142 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2143 p->phys_offset = phys_offset;
2144 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2145 (phys_offset & IO_MEM_ROMD))
2146 phys_offset += TARGET_PAGE_SIZE;
2147 else {
2148 target_phys_addr_t start_addr2, end_addr2;
2149 int need_subpage = 0;
2150
2151 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2152 end_addr2, need_subpage);
2153
blueswir14254fab2008-01-01 16:57:19 +00002154 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002155 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2156 &p->phys_offset, IO_MEM_UNASSIGNED);
2157 subpage_register(subpage, start_addr2, end_addr2,
2158 phys_offset);
2159 }
2160 }
2161 }
bellard33417e72003-08-10 21:47:01 +00002162 }
ths3b46e622007-09-17 08:09:54 +00002163
bellard9d420372006-06-25 22:25:22 +00002164 /* since each CPU stores ram addresses in its TLB cache, we must
2165 reset the modified entries */
2166 /* XXX: slow ! */
2167 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2168 tlb_flush(env, 1);
2169 }
bellard33417e72003-08-10 21:47:01 +00002170}
2171
bellardba863452006-09-24 18:41:10 +00002172/* XXX: temporary until new memory mapping API */
aurel3200f82b82008-04-27 21:12:55 +00002173ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002174{
2175 PhysPageDesc *p;
2176
2177 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2178 if (!p)
2179 return IO_MEM_UNASSIGNED;
2180 return p->phys_offset;
2181}
2182
bellarde9a1ab12007-02-08 23:08:38 +00002183/* XXX: better than nothing */
aurel3200f82b82008-04-27 21:12:55 +00002184ram_addr_t qemu_ram_alloc(ram_addr_t size)
bellarde9a1ab12007-02-08 23:08:38 +00002185{
2186 ram_addr_t addr;
balrog7fb4fdc2008-04-24 17:59:27 +00002187 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
bellarded441462008-05-23 11:56:45 +00002188 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 "\n",
2189 (uint64_t)size, (uint64_t)phys_ram_size);
bellarde9a1ab12007-02-08 23:08:38 +00002190 abort();
2191 }
2192 addr = phys_ram_alloc_offset;
2193 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2194 return addr;
2195}
2196
2197void qemu_ram_free(ram_addr_t addr)
2198{
2199}
2200
bellarda4193c82004-06-03 14:01:43 +00002201static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002202{
pbrook67d3b952006-12-18 05:03:52 +00002203#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002204 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002205#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002206#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002207 do_unassigned_access(addr, 0, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002208#elif TARGET_CRIS
2209 do_unassigned_access(addr, 0, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002210#endif
bellard33417e72003-08-10 21:47:01 +00002211 return 0;
2212}
2213
bellarda4193c82004-06-03 14:01:43 +00002214static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002215{
pbrook67d3b952006-12-18 05:03:52 +00002216#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002217 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002218#endif
blueswir1b4f0a312007-05-06 17:59:24 +00002219#ifdef TARGET_SPARC
blueswir16c36d3f2007-05-17 19:30:10 +00002220 do_unassigned_access(addr, 1, 0, 0);
thsf1ccf902007-10-08 13:16:14 +00002221#elif TARGET_CRIS
2222 do_unassigned_access(addr, 1, 0, 0);
blueswir1b4f0a312007-05-06 17:59:24 +00002223#endif
bellard33417e72003-08-10 21:47:01 +00002224}
2225
2226static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2227 unassigned_mem_readb,
2228 unassigned_mem_readb,
2229 unassigned_mem_readb,
2230};
2231
2232static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2233 unassigned_mem_writeb,
2234 unassigned_mem_writeb,
2235 unassigned_mem_writeb,
2236};
2237
pbrook0f459d12008-06-09 00:20:13 +00002238static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2239 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002240{
bellard3a7d9292005-08-21 09:26:42 +00002241 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002242 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2243 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2244#if !defined(CONFIG_USER_ONLY)
2245 tb_invalidate_phys_page_fast(ram_addr, 1);
2246 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2247#endif
2248 }
pbrook0f459d12008-06-09 00:20:13 +00002249 stb_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002250#ifdef USE_KQEMU
2251 if (cpu_single_env->kqemu_enabled &&
2252 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2253 kqemu_modify_page(cpu_single_env, ram_addr);
2254#endif
bellardf23db162005-08-21 19:12:28 +00002255 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2256 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2257 /* we remove the notdirty callback only if the code has been
2258 flushed */
2259 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002260 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002261}
2262
pbrook0f459d12008-06-09 00:20:13 +00002263static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2264 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002265{
bellard3a7d9292005-08-21 09:26:42 +00002266 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002267 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2268 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2269#if !defined(CONFIG_USER_ONLY)
2270 tb_invalidate_phys_page_fast(ram_addr, 2);
2271 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2272#endif
2273 }
pbrook0f459d12008-06-09 00:20:13 +00002274 stw_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002275#ifdef USE_KQEMU
2276 if (cpu_single_env->kqemu_enabled &&
2277 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2278 kqemu_modify_page(cpu_single_env, ram_addr);
2279#endif
bellardf23db162005-08-21 19:12:28 +00002280 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2281 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2282 /* we remove the notdirty callback only if the code has been
2283 flushed */
2284 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002285 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002286}
2287
pbrook0f459d12008-06-09 00:20:13 +00002288static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2289 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002290{
bellard3a7d9292005-08-21 09:26:42 +00002291 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002292 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2293 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2294#if !defined(CONFIG_USER_ONLY)
2295 tb_invalidate_phys_page_fast(ram_addr, 4);
2296 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2297#endif
2298 }
pbrook0f459d12008-06-09 00:20:13 +00002299 stl_p(phys_ram_base + ram_addr, val);
bellardf32fc642006-02-08 22:43:39 +00002300#ifdef USE_KQEMU
2301 if (cpu_single_env->kqemu_enabled &&
2302 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2303 kqemu_modify_page(cpu_single_env, ram_addr);
2304#endif
bellardf23db162005-08-21 19:12:28 +00002305 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2306 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2307 /* we remove the notdirty callback only if the code has been
2308 flushed */
2309 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002310 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002311}
2312
bellard3a7d9292005-08-21 09:26:42 +00002313static CPUReadMemoryFunc *error_mem_read[3] = {
2314 NULL, /* never used */
2315 NULL, /* never used */
2316 NULL, /* never used */
2317};
2318
bellard1ccde1c2004-02-06 19:46:14 +00002319static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2320 notdirty_mem_writeb,
2321 notdirty_mem_writew,
2322 notdirty_mem_writel,
2323};
2324
pbrook0f459d12008-06-09 00:20:13 +00002325/* Generate a debug exception if a watchpoint has been hit. */
2326static void check_watchpoint(int offset, int flags)
2327{
2328 CPUState *env = cpu_single_env;
2329 target_ulong vaddr;
2330 int i;
2331
pbrook2e70f6e2008-06-29 01:03:05 +00002332 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
pbrook0f459d12008-06-09 00:20:13 +00002333 for (i = 0; i < env->nb_watchpoints; i++) {
2334 if (vaddr == env->watchpoint[i].vaddr
2335 && (env->watchpoint[i].type & flags)) {
2336 env->watchpoint_hit = i + 1;
2337 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2338 break;
2339 }
2340 }
2341}
2342
pbrook6658ffb2007-03-16 23:58:11 +00002343/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2344 so these check for a hit then pass through to the normal out-of-line
2345 phys routines. */
2346static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2347{
pbrook0f459d12008-06-09 00:20:13 +00002348 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002349 return ldub_phys(addr);
2350}
2351
2352static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2353{
pbrook0f459d12008-06-09 00:20:13 +00002354 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002355 return lduw_phys(addr);
2356}
2357
2358static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2359{
pbrook0f459d12008-06-09 00:20:13 +00002360 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002361 return ldl_phys(addr);
2362}
2363
pbrook6658ffb2007-03-16 23:58:11 +00002364static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2365 uint32_t val)
2366{
pbrook0f459d12008-06-09 00:20:13 +00002367 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002368 stb_phys(addr, val);
2369}
2370
2371static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2372 uint32_t val)
2373{
pbrook0f459d12008-06-09 00:20:13 +00002374 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002375 stw_phys(addr, val);
2376}
2377
2378static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2379 uint32_t val)
2380{
pbrook0f459d12008-06-09 00:20:13 +00002381 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002382 stl_phys(addr, val);
2383}
2384
2385static CPUReadMemoryFunc *watch_mem_read[3] = {
2386 watch_mem_readb,
2387 watch_mem_readw,
2388 watch_mem_readl,
2389};
2390
2391static CPUWriteMemoryFunc *watch_mem_write[3] = {
2392 watch_mem_writeb,
2393 watch_mem_writew,
2394 watch_mem_writel,
2395};
pbrook6658ffb2007-03-16 23:58:11 +00002396
blueswir1db7b5422007-05-26 17:36:03 +00002397static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2398 unsigned int len)
2399{
blueswir1db7b5422007-05-26 17:36:03 +00002400 uint32_t ret;
2401 unsigned int idx;
2402
2403 idx = SUBPAGE_IDX(addr - mmio->base);
2404#if defined(DEBUG_SUBPAGE)
2405 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2406 mmio, len, addr, idx);
2407#endif
blueswir13ee89922008-01-02 19:45:26 +00002408 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
blueswir1db7b5422007-05-26 17:36:03 +00002409
2410 return ret;
2411}
2412
2413static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2414 uint32_t value, unsigned int len)
2415{
blueswir1db7b5422007-05-26 17:36:03 +00002416 unsigned int idx;
2417
2418 idx = SUBPAGE_IDX(addr - mmio->base);
2419#if defined(DEBUG_SUBPAGE)
2420 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2421 mmio, len, addr, idx, value);
2422#endif
blueswir13ee89922008-01-02 19:45:26 +00002423 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00002424}
2425
2426static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2427{
2428#if defined(DEBUG_SUBPAGE)
2429 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2430#endif
2431
2432 return subpage_readlen(opaque, addr, 0);
2433}
2434
2435static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2436 uint32_t value)
2437{
2438#if defined(DEBUG_SUBPAGE)
2439 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2440#endif
2441 subpage_writelen(opaque, addr, value, 0);
2442}
2443
2444static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2445{
2446#if defined(DEBUG_SUBPAGE)
2447 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2448#endif
2449
2450 return subpage_readlen(opaque, addr, 1);
2451}
2452
2453static void subpage_writew (void *opaque, target_phys_addr_t addr,
2454 uint32_t value)
2455{
2456#if defined(DEBUG_SUBPAGE)
2457 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2458#endif
2459 subpage_writelen(opaque, addr, value, 1);
2460}
2461
2462static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2463{
2464#if defined(DEBUG_SUBPAGE)
2465 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2466#endif
2467
2468 return subpage_readlen(opaque, addr, 2);
2469}
2470
2471static void subpage_writel (void *opaque,
2472 target_phys_addr_t addr, uint32_t value)
2473{
2474#if defined(DEBUG_SUBPAGE)
2475 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2476#endif
2477 subpage_writelen(opaque, addr, value, 2);
2478}
2479
2480static CPUReadMemoryFunc *subpage_read[] = {
2481 &subpage_readb,
2482 &subpage_readw,
2483 &subpage_readl,
2484};
2485
2486static CPUWriteMemoryFunc *subpage_write[] = {
2487 &subpage_writeb,
2488 &subpage_writew,
2489 &subpage_writel,
2490};
2491
2492static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
aurel3200f82b82008-04-27 21:12:55 +00002493 ram_addr_t memory)
blueswir1db7b5422007-05-26 17:36:03 +00002494{
2495 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002496 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002497
2498 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2499 return -1;
2500 idx = SUBPAGE_IDX(start);
2501 eidx = SUBPAGE_IDX(end);
2502#if defined(DEBUG_SUBPAGE)
2503 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2504 mmio, start, end, idx, eidx, memory);
2505#endif
2506 memory >>= IO_MEM_SHIFT;
2507 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002508 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002509 if (io_mem_read[memory][i]) {
2510 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2511 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2512 }
2513 if (io_mem_write[memory][i]) {
2514 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2515 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2516 }
blueswir14254fab2008-01-01 16:57:19 +00002517 }
blueswir1db7b5422007-05-26 17:36:03 +00002518 }
2519
2520 return 0;
2521}
2522
aurel3200f82b82008-04-27 21:12:55 +00002523static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2524 ram_addr_t orig_memory)
blueswir1db7b5422007-05-26 17:36:03 +00002525{
2526 subpage_t *mmio;
2527 int subpage_memory;
2528
2529 mmio = qemu_mallocz(sizeof(subpage_t));
2530 if (mmio != NULL) {
2531 mmio->base = base;
2532 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2533#if defined(DEBUG_SUBPAGE)
2534 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2535 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2536#endif
2537 *phys = subpage_memory | IO_MEM_SUBPAGE;
2538 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2539 }
2540
2541 return mmio;
2542}
2543
bellard33417e72003-08-10 21:47:01 +00002544static void io_mem_init(void)
2545{
bellard3a7d9292005-08-21 09:26:42 +00002546 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
bellarda4193c82004-06-03 14:01:43 +00002547 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
bellard3a7d9292005-08-21 09:26:42 +00002548 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002549 io_mem_nb = 5;
2550
pbrook0f459d12008-06-09 00:20:13 +00002551 io_mem_watch = cpu_register_io_memory(0, watch_mem_read,
pbrook6658ffb2007-03-16 23:58:11 +00002552 watch_mem_write, NULL);
bellard1ccde1c2004-02-06 19:46:14 +00002553 /* alloc dirty bits array */
bellard0a962c02005-02-10 22:00:27 +00002554 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
bellard3a7d9292005-08-21 09:26:42 +00002555 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002556}
2557
2558/* mem_read and mem_write are arrays of functions containing the
2559 function to access byte (index 0), word (index 1) and dword (index
blueswir13ee89922008-01-02 19:45:26 +00002560 2). Functions can be omitted with a NULL function pointer. The
2561 registered functions may be modified dynamically later.
2562 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002563 modified. If it is zero, a new io zone is allocated. The return
2564 value can be used with cpu_register_physical_memory(). (-1) is
2565 returned if error. */
bellard33417e72003-08-10 21:47:01 +00002566int cpu_register_io_memory(int io_index,
2567 CPUReadMemoryFunc **mem_read,
bellarda4193c82004-06-03 14:01:43 +00002568 CPUWriteMemoryFunc **mem_write,
2569 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002570{
blueswir14254fab2008-01-01 16:57:19 +00002571 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002572
2573 if (io_index <= 0) {
bellardb5ff1b32005-11-26 10:38:39 +00002574 if (io_mem_nb >= IO_MEM_NB_ENTRIES)
bellard33417e72003-08-10 21:47:01 +00002575 return -1;
2576 io_index = io_mem_nb++;
2577 } else {
2578 if (io_index >= IO_MEM_NB_ENTRIES)
2579 return -1;
2580 }
bellardb5ff1b32005-11-26 10:38:39 +00002581
bellard33417e72003-08-10 21:47:01 +00002582 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002583 if (!mem_read[i] || !mem_write[i])
2584 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002585 io_mem_read[io_index][i] = mem_read[i];
2586 io_mem_write[io_index][i] = mem_write[i];
2587 }
bellarda4193c82004-06-03 14:01:43 +00002588 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002589 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002590}
bellard61382a52003-10-27 21:22:23 +00002591
bellard8926b512004-10-10 15:14:20 +00002592CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2593{
2594 return io_mem_write[io_index >> IO_MEM_SHIFT];
2595}
2596
2597CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2598{
2599 return io_mem_read[io_index >> IO_MEM_SHIFT];
2600}
2601
pbrooke2eef172008-06-08 01:09:01 +00002602#endif /* !defined(CONFIG_USER_ONLY) */
2603
bellard13eb76e2004-01-24 15:23:36 +00002604/* physical memory access (slow version, mainly for debug) */
2605#if defined(CONFIG_USER_ONLY)
ths5fafdf22007-09-16 21:08:06 +00002606void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002607 int len, int is_write)
2608{
2609 int l, flags;
2610 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002611 void * p;
bellard13eb76e2004-01-24 15:23:36 +00002612
2613 while (len > 0) {
2614 page = addr & TARGET_PAGE_MASK;
2615 l = (page + TARGET_PAGE_SIZE) - addr;
2616 if (l > len)
2617 l = len;
2618 flags = page_get_flags(page);
2619 if (!(flags & PAGE_VALID))
2620 return;
2621 if (is_write) {
2622 if (!(flags & PAGE_WRITE))
2623 return;
bellard579a97f2007-11-11 14:26:47 +00002624 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002625 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00002626 /* FIXME - should this return an error rather than just fail? */
2627 return;
aurel3272fb7da2008-04-27 23:53:45 +00002628 memcpy(p, buf, l);
2629 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00002630 } else {
2631 if (!(flags & PAGE_READ))
2632 return;
bellard579a97f2007-11-11 14:26:47 +00002633 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00002634 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00002635 /* FIXME - should this return an error rather than just fail? */
2636 return;
aurel3272fb7da2008-04-27 23:53:45 +00002637 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00002638 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00002639 }
2640 len -= l;
2641 buf += l;
2642 addr += l;
2643 }
2644}
bellard8df1cd02005-01-28 22:37:22 +00002645
bellard13eb76e2004-01-24 15:23:36 +00002646#else
ths5fafdf22007-09-16 21:08:06 +00002647void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002648 int len, int is_write)
2649{
2650 int l, io_index;
2651 uint8_t *ptr;
2652 uint32_t val;
bellard2e126692004-04-25 21:28:44 +00002653 target_phys_addr_t page;
2654 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00002655 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002656
bellard13eb76e2004-01-24 15:23:36 +00002657 while (len > 0) {
2658 page = addr & TARGET_PAGE_MASK;
2659 l = (page + TARGET_PAGE_SIZE) - addr;
2660 if (l > len)
2661 l = len;
bellard92e873b2004-05-21 14:52:29 +00002662 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00002663 if (!p) {
2664 pd = IO_MEM_UNASSIGNED;
2665 } else {
2666 pd = p->phys_offset;
2667 }
ths3b46e622007-09-17 08:09:54 +00002668
bellard13eb76e2004-01-24 15:23:36 +00002669 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00002670 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard13eb76e2004-01-24 15:23:36 +00002671 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
bellard6a00d602005-11-21 23:25:50 +00002672 /* XXX: could force cpu_single_env to NULL to avoid
2673 potential bugs */
bellard13eb76e2004-01-24 15:23:36 +00002674 if (l >= 4 && ((addr & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002675 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002676 val = ldl_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002677 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002678 l = 4;
2679 } else if (l >= 2 && ((addr & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00002680 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002681 val = lduw_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002682 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002683 l = 2;
2684 } else {
bellard1c213d12005-09-03 10:49:04 +00002685 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002686 val = ldub_p(buf);
bellarda4193c82004-06-03 14:01:43 +00002687 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
bellard13eb76e2004-01-24 15:23:36 +00002688 l = 1;
2689 }
2690 } else {
bellardb448f2f2004-02-25 23:24:04 +00002691 unsigned long addr1;
2692 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00002693 /* RAM case */
bellardb448f2f2004-02-25 23:24:04 +00002694 ptr = phys_ram_base + addr1;
bellard13eb76e2004-01-24 15:23:36 +00002695 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00002696 if (!cpu_physical_memory_is_dirty(addr1)) {
2697 /* invalidate code */
2698 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2699 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00002700 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00002701 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002702 }
bellard13eb76e2004-01-24 15:23:36 +00002703 }
2704 } else {
ths5fafdf22007-09-16 21:08:06 +00002705 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002706 !(pd & IO_MEM_ROMD)) {
bellard13eb76e2004-01-24 15:23:36 +00002707 /* I/O case */
2708 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2709 if (l >= 4 && ((addr & 3) == 0)) {
2710 /* 32 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002711 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002712 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002713 l = 4;
2714 } else if (l >= 2 && ((addr & 1) == 0)) {
2715 /* 16 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002716 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002717 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002718 l = 2;
2719 } else {
bellard1c213d12005-09-03 10:49:04 +00002720 /* 8 bit read access */
bellarda4193c82004-06-03 14:01:43 +00002721 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
bellardc27004e2005-01-03 23:35:10 +00002722 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002723 l = 1;
2724 }
2725 } else {
2726 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002727 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00002728 (addr & ~TARGET_PAGE_MASK);
2729 memcpy(buf, ptr, l);
2730 }
2731 }
2732 len -= l;
2733 buf += l;
2734 addr += l;
2735 }
2736}
bellard8df1cd02005-01-28 22:37:22 +00002737
bellardd0ecd2a2006-04-23 17:14:48 +00002738/* used for ROM loading : can write in RAM and ROM */
ths5fafdf22007-09-16 21:08:06 +00002739void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002740 const uint8_t *buf, int len)
2741{
2742 int l;
2743 uint8_t *ptr;
2744 target_phys_addr_t page;
2745 unsigned long pd;
2746 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00002747
bellardd0ecd2a2006-04-23 17:14:48 +00002748 while (len > 0) {
2749 page = addr & TARGET_PAGE_MASK;
2750 l = (page + TARGET_PAGE_SIZE) - addr;
2751 if (l > len)
2752 l = len;
2753 p = phys_page_find(page >> TARGET_PAGE_BITS);
2754 if (!p) {
2755 pd = IO_MEM_UNASSIGNED;
2756 } else {
2757 pd = p->phys_offset;
2758 }
ths3b46e622007-09-17 08:09:54 +00002759
bellardd0ecd2a2006-04-23 17:14:48 +00002760 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00002761 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2762 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00002763 /* do nothing */
2764 } else {
2765 unsigned long addr1;
2766 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2767 /* ROM/RAM case */
2768 ptr = phys_ram_base + addr1;
2769 memcpy(ptr, buf, l);
2770 }
2771 len -= l;
2772 buf += l;
2773 addr += l;
2774 }
2775}
2776
2777
bellard8df1cd02005-01-28 22:37:22 +00002778/* warning: addr must be aligned */
2779uint32_t ldl_phys(target_phys_addr_t addr)
2780{
2781 int io_index;
2782 uint8_t *ptr;
2783 uint32_t val;
2784 unsigned long pd;
2785 PhysPageDesc *p;
2786
2787 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2788 if (!p) {
2789 pd = IO_MEM_UNASSIGNED;
2790 } else {
2791 pd = p->phys_offset;
2792 }
ths3b46e622007-09-17 08:09:54 +00002793
ths5fafdf22007-09-16 21:08:06 +00002794 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00002795 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00002796 /* I/O case */
2797 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2798 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2799 } else {
2800 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002801 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002802 (addr & ~TARGET_PAGE_MASK);
2803 val = ldl_p(ptr);
2804 }
2805 return val;
2806}
2807
bellard84b7b8e2005-11-28 21:19:04 +00002808/* warning: addr must be aligned */
2809uint64_t ldq_phys(target_phys_addr_t addr)
2810{
2811 int io_index;
2812 uint8_t *ptr;
2813 uint64_t val;
2814 unsigned long pd;
2815 PhysPageDesc *p;
2816
2817 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2818 if (!p) {
2819 pd = IO_MEM_UNASSIGNED;
2820 } else {
2821 pd = p->phys_offset;
2822 }
ths3b46e622007-09-17 08:09:54 +00002823
bellard2a4188a2006-06-25 21:54:59 +00002824 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2825 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00002826 /* I/O case */
2827 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2828#ifdef TARGET_WORDS_BIGENDIAN
2829 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2830 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2831#else
2832 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2833 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2834#endif
2835 } else {
2836 /* RAM case */
ths5fafdf22007-09-16 21:08:06 +00002837 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00002838 (addr & ~TARGET_PAGE_MASK);
2839 val = ldq_p(ptr);
2840 }
2841 return val;
2842}
2843
bellardaab33092005-10-30 20:48:42 +00002844/* XXX: optimize */
2845uint32_t ldub_phys(target_phys_addr_t addr)
2846{
2847 uint8_t val;
2848 cpu_physical_memory_read(addr, &val, 1);
2849 return val;
2850}
2851
2852/* XXX: optimize */
2853uint32_t lduw_phys(target_phys_addr_t addr)
2854{
2855 uint16_t val;
2856 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2857 return tswap16(val);
2858}
2859
bellard8df1cd02005-01-28 22:37:22 +00002860/* warning: addr must be aligned. The ram page is not masked as dirty
2861 and the code inside is not invalidated. It is useful if the dirty
2862 bits are used to track modified PTEs */
2863void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2864{
2865 int io_index;
2866 uint8_t *ptr;
2867 unsigned long pd;
2868 PhysPageDesc *p;
2869
2870 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2871 if (!p) {
2872 pd = IO_MEM_UNASSIGNED;
2873 } else {
2874 pd = p->phys_offset;
2875 }
ths3b46e622007-09-17 08:09:54 +00002876
bellard3a7d9292005-08-21 09:26:42 +00002877 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002878 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2879 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2880 } else {
ths5fafdf22007-09-16 21:08:06 +00002881 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00002882 (addr & ~TARGET_PAGE_MASK);
2883 stl_p(ptr, val);
2884 }
2885}
2886
j_mayerbc98a7e2007-04-04 07:55:12 +00002887void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
2888{
2889 int io_index;
2890 uint8_t *ptr;
2891 unsigned long pd;
2892 PhysPageDesc *p;
2893
2894 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2895 if (!p) {
2896 pd = IO_MEM_UNASSIGNED;
2897 } else {
2898 pd = p->phys_offset;
2899 }
ths3b46e622007-09-17 08:09:54 +00002900
j_mayerbc98a7e2007-04-04 07:55:12 +00002901 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2902 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2903#ifdef TARGET_WORDS_BIGENDIAN
2904 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
2905 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
2906#else
2907 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2908 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
2909#endif
2910 } else {
ths5fafdf22007-09-16 21:08:06 +00002911 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00002912 (addr & ~TARGET_PAGE_MASK);
2913 stq_p(ptr, val);
2914 }
2915}
2916
bellard8df1cd02005-01-28 22:37:22 +00002917/* warning: addr must be aligned */
bellard8df1cd02005-01-28 22:37:22 +00002918void stl_phys(target_phys_addr_t addr, uint32_t val)
2919{
2920 int io_index;
2921 uint8_t *ptr;
2922 unsigned long pd;
2923 PhysPageDesc *p;
2924
2925 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2926 if (!p) {
2927 pd = IO_MEM_UNASSIGNED;
2928 } else {
2929 pd = p->phys_offset;
2930 }
ths3b46e622007-09-17 08:09:54 +00002931
bellard3a7d9292005-08-21 09:26:42 +00002932 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00002933 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2934 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2935 } else {
2936 unsigned long addr1;
2937 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2938 /* RAM case */
2939 ptr = phys_ram_base + addr1;
2940 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00002941 if (!cpu_physical_memory_is_dirty(addr1)) {
2942 /* invalidate code */
2943 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2944 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00002945 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2946 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00002947 }
bellard8df1cd02005-01-28 22:37:22 +00002948 }
2949}
2950
bellardaab33092005-10-30 20:48:42 +00002951/* XXX: optimize */
2952void stb_phys(target_phys_addr_t addr, uint32_t val)
2953{
2954 uint8_t v = val;
2955 cpu_physical_memory_write(addr, &v, 1);
2956}
2957
2958/* XXX: optimize */
2959void stw_phys(target_phys_addr_t addr, uint32_t val)
2960{
2961 uint16_t v = tswap16(val);
2962 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
2963}
2964
2965/* XXX: optimize */
2966void stq_phys(target_phys_addr_t addr, uint64_t val)
2967{
2968 val = tswap64(val);
2969 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
2970}
2971
bellard13eb76e2004-01-24 15:23:36 +00002972#endif
2973
2974/* virtual memory access for debug */
ths5fafdf22007-09-16 21:08:06 +00002975int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002976 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002977{
2978 int l;
j_mayer9b3c35e2007-04-07 11:21:28 +00002979 target_phys_addr_t phys_addr;
2980 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002981
2982 while (len > 0) {
2983 page = addr & TARGET_PAGE_MASK;
2984 phys_addr = cpu_get_phys_page_debug(env, page);
2985 /* if no physical page mapped, return an error */
2986 if (phys_addr == -1)
2987 return -1;
2988 l = (page + TARGET_PAGE_SIZE) - addr;
2989 if (l > len)
2990 l = len;
ths5fafdf22007-09-16 21:08:06 +00002991 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
bellardb448f2f2004-02-25 23:24:04 +00002992 buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002993 len -= l;
2994 buf += l;
2995 addr += l;
2996 }
2997 return 0;
2998}
2999
pbrook2e70f6e2008-06-29 01:03:05 +00003000/* in deterministic execution mode, instructions doing device I/Os
3001 must be at the end of the TB */
3002void cpu_io_recompile(CPUState *env, void *retaddr)
3003{
3004 TranslationBlock *tb;
3005 uint32_t n, cflags;
3006 target_ulong pc, cs_base;
3007 uint64_t flags;
3008
3009 tb = tb_find_pc((unsigned long)retaddr);
3010 if (!tb) {
3011 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3012 retaddr);
3013 }
3014 n = env->icount_decr.u16.low + tb->icount;
3015 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3016 /* Calculate how many instructions had been executed before the fault
3017 occured. */
3018 n = n - env->icount_decr.u16.low;
3019 /* Generate a new TB ending on the I/O insn. */
3020 n++;
3021 /* On MIPS and SH, delay slot instructions can only be restarted if
3022 they were already the first instruction in the TB. If this is not
3023 the first instruction in a TB then re-execute the preceeding
3024 branch. */
3025#if defined(TARGET_MIPS)
3026 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3027 env->active_tc.PC -= 4;
3028 env->icount_decr.u16.low++;
3029 env->hflags &= ~MIPS_HFLAG_BMASK;
3030 }
3031#elif defined(TARGET_SH4)
3032 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3033 && n > 1) {
3034 env->pc -= 2;
3035 env->icount_decr.u16.low++;
3036 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3037 }
3038#endif
3039 /* This should never happen. */
3040 if (n > CF_COUNT_MASK)
3041 cpu_abort(env, "TB too big during recompile");
3042
3043 cflags = n | CF_LAST_IO;
3044 pc = tb->pc;
3045 cs_base = tb->cs_base;
3046 flags = tb->flags;
3047 tb_phys_invalidate(tb, -1);
3048 /* FIXME: In theory this could raise an exception. In practice
3049 we have already translated the block once so it's probably ok. */
3050 tb_gen_code(env, pc, cs_base, flags, cflags);
3051 /* TODO: If env->pc != tb->pc (i.e. the failuting instruction was not
3052 the first in the TB) then we end up generating a whole new TB and
3053 repeating the fault, which is horribly inefficient.
3054 Better would be to execute just this insn uncached, or generate a
3055 second new TB. */
3056 cpu_resume_from_signal(env, NULL);
3057}
3058
bellarde3db7222005-01-26 22:00:47 +00003059void dump_exec_info(FILE *f,
3060 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3061{
3062 int i, target_code_size, max_target_code_size;
3063 int direct_jmp_count, direct_jmp2_count, cross_page;
3064 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003065
bellarde3db7222005-01-26 22:00:47 +00003066 target_code_size = 0;
3067 max_target_code_size = 0;
3068 cross_page = 0;
3069 direct_jmp_count = 0;
3070 direct_jmp2_count = 0;
3071 for(i = 0; i < nb_tbs; i++) {
3072 tb = &tbs[i];
3073 target_code_size += tb->size;
3074 if (tb->size > max_target_code_size)
3075 max_target_code_size = tb->size;
3076 if (tb->page_addr[1] != -1)
3077 cross_page++;
3078 if (tb->tb_next_offset[0] != 0xffff) {
3079 direct_jmp_count++;
3080 if (tb->tb_next_offset[1] != 0xffff) {
3081 direct_jmp2_count++;
3082 }
3083 }
3084 }
3085 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003086 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003087 cpu_fprintf(f, "gen code size %ld/%ld\n",
3088 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3089 cpu_fprintf(f, "TB count %d/%d\n",
3090 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003091 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003092 nb_tbs ? target_code_size / nb_tbs : 0,
3093 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003094 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003095 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3096 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003097 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3098 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003099 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3100 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003101 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003102 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3103 direct_jmp2_count,
3104 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003105 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003106 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3107 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3108 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003109 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003110}
3111
ths5fafdf22007-09-16 21:08:06 +00003112#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003113
3114#define MMUSUFFIX _cmmu
3115#define GETPC() NULL
3116#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003117#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003118
3119#define SHIFT 0
3120#include "softmmu_template.h"
3121
3122#define SHIFT 1
3123#include "softmmu_template.h"
3124
3125#define SHIFT 2
3126#include "softmmu_template.h"
3127
3128#define SHIFT 3
3129#include "softmmu_template.h"
3130
3131#undef env
3132
3133#endif