blob: 7b7fb5ba00ca7a03ee7c71d70aba0c183d52dfaa [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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard54936002003-05-13 00:25:15 +000018 */
bellard67b915a2004-03-31 23:37:16 +000019#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000020#ifdef _WIN32
21#include <windows.h>
22#else
bellarda98d49b2004-11-14 16:22:05 +000023#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000024#include <sys/mman.h>
25#endif
bellard54936002003-05-13 00:25:15 +000026#include <stdlib.h>
27#include <stdio.h>
28#include <stdarg.h>
29#include <string.h>
30#include <errno.h>
31#include <unistd.h>
32#include <inttypes.h>
33
bellard6180a182003-09-30 21:04:53 +000034#include "cpu.h"
35#include "exec-all.h"
aurel32ca10f862008-04-11 21:35:42 +000036#include "qemu-common.h"
bellardb67d9a52008-05-23 09:57:34 +000037#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000038#include "hw/hw.h"
aliguori74576192008-10-06 14:02:03 +000039#include "osdep.h"
aliguori7ba1e612008-11-05 16:04:33 +000040#include "kvm.h"
pbrook53a59602006-03-25 19:31:22 +000041#if defined(CONFIG_USER_ONLY)
42#include <qemu.h>
43#endif
bellard54936002003-05-13 00:25:15 +000044
bellardfd6ce8f2003-05-14 19:00:11 +000045//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000046//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000047//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000048//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000049
50/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000051//#define DEBUG_TB_CHECK
52//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000053
ths1196be32007-03-17 15:17:58 +000054//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000055//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000056
pbrook99773bd2006-04-16 15:14:59 +000057#if !defined(CONFIG_USER_ONLY)
58/* TB consistency checks only implemented for usermode emulation. */
59#undef DEBUG_TB_CHECK
60#endif
61
bellard9fa3e852004-01-04 18:06:42 +000062#define SMC_BITMAP_USE_THRESHOLD 10
63
bellard108c49b2005-07-24 12:55:09 +000064#if defined(TARGET_SPARC64)
65#define TARGET_PHYS_ADDR_SPACE_BITS 41
blueswir15dcb6b92007-05-19 12:58:30 +000066#elif defined(TARGET_SPARC)
67#define TARGET_PHYS_ADDR_SPACE_BITS 36
j_mayerbedb69e2007-04-05 20:08:21 +000068#elif defined(TARGET_ALPHA)
69#define TARGET_PHYS_ADDR_SPACE_BITS 42
70#define TARGET_VIRT_ADDR_SPACE_BITS 42
bellard108c49b2005-07-24 12:55:09 +000071#elif defined(TARGET_PPC64)
72#define TARGET_PHYS_ADDR_SPACE_BITS 42
Anthony Liguori4a1418e2009-08-10 17:07:24 -050073#elif defined(TARGET_X86_64)
aurel3200f82b82008-04-27 21:12:55 +000074#define TARGET_PHYS_ADDR_SPACE_BITS 42
Anthony Liguori4a1418e2009-08-10 17:07:24 -050075#elif defined(TARGET_I386)
aurel3200f82b82008-04-27 21:12:55 +000076#define TARGET_PHYS_ADDR_SPACE_BITS 36
bellard108c49b2005-07-24 12:55:09 +000077#else
bellard108c49b2005-07-24 12:55:09 +000078#define TARGET_PHYS_ADDR_SPACE_BITS 32
79#endif
80
blueswir1bdaf78e2008-10-04 07:24:27 +000081static TranslationBlock *tbs;
bellard26a5f132008-05-28 12:30:31 +000082int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000083TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +000084static int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000085/* any access to the tbs or the page table must use this lock */
Anthony Liguoric227f092009-10-01 16:12:16 -050086spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000087
blueswir1141ac462008-07-26 15:05:57 +000088#if defined(__arm__) || defined(__sparc_v9__)
89/* The prologue must be reachable with a direct jump. ARM and Sparc64
90 have limited branch ranges (possibly also PPC) so place it in a
blueswir1d03d8602008-07-10 17:21:31 +000091 section close to code segment. */
92#define code_gen_section \
93 __attribute__((__section__(".gen_code"))) \
94 __attribute__((aligned (32)))
Stefan Weilf8e2af12009-06-18 23:04:48 +020095#elif defined(_WIN32)
96/* Maximum alignment for Win32 is 16. */
97#define code_gen_section \
98 __attribute__((aligned (16)))
blueswir1d03d8602008-07-10 17:21:31 +000099#else
100#define code_gen_section \
101 __attribute__((aligned (32)))
102#endif
103
104uint8_t code_gen_prologue[1024] code_gen_section;
blueswir1bdaf78e2008-10-04 07:24:27 +0000105static uint8_t *code_gen_buffer;
106static unsigned long code_gen_buffer_size;
bellard26a5f132008-05-28 12:30:31 +0000107/* threshold to flush the translated code buffer */
blueswir1bdaf78e2008-10-04 07:24:27 +0000108static unsigned long code_gen_buffer_max_size;
bellardfd6ce8f2003-05-14 19:00:11 +0000109uint8_t *code_gen_ptr;
110
pbrooke2eef172008-06-08 01:09:01 +0000111#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +0000112int phys_ram_fd;
bellard1ccde1c2004-02-06 19:46:14 +0000113uint8_t *phys_ram_dirty;
aliguori74576192008-10-06 14:02:03 +0000114static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +0000115
116typedef struct RAMBlock {
117 uint8_t *host;
Anthony Liguoric227f092009-10-01 16:12:16 -0500118 ram_addr_t offset;
119 ram_addr_t length;
pbrook94a6b542009-04-11 17:15:54 +0000120 struct RAMBlock *next;
121} RAMBlock;
122
123static RAMBlock *ram_blocks;
124/* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100125 then we can no longer assume contiguous ram offsets, and external uses
pbrook94a6b542009-04-11 17:15:54 +0000126 of this variable will break. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500127ram_addr_t last_ram_offset;
pbrooke2eef172008-06-08 01:09:01 +0000128#endif
bellard9fa3e852004-01-04 18:06:42 +0000129
bellard6a00d602005-11-21 23:25:50 +0000130CPUState *first_cpu;
131/* current CPU in the current thread. It is only valid inside
132 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000133CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000134/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +0000135 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +0000136 2 = Adaptive rate instruction counting. */
137int use_icount = 0;
138/* Current instruction counter. While executing translated code this may
139 include some instructions that have not yet been executed. */
140int64_t qemu_icount;
bellard6a00d602005-11-21 23:25:50 +0000141
bellard54936002003-05-13 00:25:15 +0000142typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000143 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000144 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000145 /* in order to optimize self modifying code, we count the number
146 of lookups we do to a given page to use a bitmap */
147 unsigned int code_write_count;
148 uint8_t *code_bitmap;
149#if defined(CONFIG_USER_ONLY)
150 unsigned long flags;
151#endif
bellard54936002003-05-13 00:25:15 +0000152} PageDesc;
153
bellard92e873b2004-05-21 14:52:29 +0000154typedef struct PhysPageDesc {
pbrook0f459d12008-06-09 00:20:13 +0000155 /* offset in host memory of the page + io_index in the low bits */
Anthony Liguoric227f092009-10-01 16:12:16 -0500156 ram_addr_t phys_offset;
157 ram_addr_t region_offset;
bellard92e873b2004-05-21 14:52:29 +0000158} PhysPageDesc;
159
bellard54936002003-05-13 00:25:15 +0000160#define L2_BITS 10
j_mayerbedb69e2007-04-05 20:08:21 +0000161#if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
162/* XXX: this is a temporary hack for alpha target.
163 * In the future, this is to be replaced by a multi-level table
164 * to actually be able to handle the complete 64 bits address space.
165 */
166#define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
167#else
aurel3203875442008-04-22 20:45:18 +0000168#define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
j_mayerbedb69e2007-04-05 20:08:21 +0000169#endif
bellard54936002003-05-13 00:25:15 +0000170
171#define L1_SIZE (1 << L1_BITS)
172#define L2_SIZE (1 << L2_BITS)
173
bellard83fb7ad2004-07-05 21:25:26 +0000174unsigned long qemu_real_host_page_size;
175unsigned long qemu_host_page_bits;
176unsigned long qemu_host_page_size;
177unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000178
bellard92e873b2004-05-21 14:52:29 +0000179/* XXX: for system emulation, it could just be an array */
bellard54936002003-05-13 00:25:15 +0000180static PageDesc *l1_map[L1_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +0000181static PhysPageDesc **l1_phys_map;
bellard54936002003-05-13 00:25:15 +0000182
pbrooke2eef172008-06-08 01:09:01 +0000183#if !defined(CONFIG_USER_ONLY)
184static void io_mem_init(void);
185
bellard33417e72003-08-10 21:47:01 +0000186/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000187CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
188CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000189void *io_mem_opaque[IO_MEM_NB_ENTRIES];
blueswir1511d2b12009-03-07 15:32:56 +0000190static char io_mem_used[IO_MEM_NB_ENTRIES];
pbrook6658ffb2007-03-16 23:58:11 +0000191static int io_mem_watch;
192#endif
bellard33417e72003-08-10 21:47:01 +0000193
bellard34865132003-10-05 14:28:56 +0000194/* log support */
blueswir1d9b630f2008-10-05 09:57:08 +0000195static const char *logfilename = "/tmp/qemu.log";
bellard34865132003-10-05 14:28:56 +0000196FILE *logfile;
197int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000198static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000199
bellarde3db7222005-01-26 22:00:47 +0000200/* statistics */
201static int tlb_flush_count;
202static int tb_flush_count;
203static int tb_phys_invalidate_count;
204
blueswir1db7b5422007-05-26 17:36:03 +0000205#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
Anthony Liguoric227f092009-10-01 16:12:16 -0500206typedef struct subpage_t {
207 target_phys_addr_t base;
Blue Swirld60efc62009-08-25 18:29:31 +0000208 CPUReadMemoryFunc * const *mem_read[TARGET_PAGE_SIZE][4];
209 CPUWriteMemoryFunc * const *mem_write[TARGET_PAGE_SIZE][4];
blueswir13ee89922008-01-02 19:45:26 +0000210 void *opaque[TARGET_PAGE_SIZE][2][4];
Anthony Liguoric227f092009-10-01 16:12:16 -0500211 ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4];
212} subpage_t;
blueswir1db7b5422007-05-26 17:36:03 +0000213
bellard7cb69ca2008-05-10 10:55:51 +0000214#ifdef _WIN32
215static void map_exec(void *addr, long size)
216{
217 DWORD old_protect;
218 VirtualProtect(addr, size,
219 PAGE_EXECUTE_READWRITE, &old_protect);
220
221}
222#else
223static void map_exec(void *addr, long size)
224{
bellard43694152008-05-29 09:35:57 +0000225 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000226
bellard43694152008-05-29 09:35:57 +0000227 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000228 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000229 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000230
231 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000232 end += page_size - 1;
233 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000234
235 mprotect((void *)start, end - start,
236 PROT_READ | PROT_WRITE | PROT_EXEC);
237}
238#endif
239
bellardb346ff42003-06-15 20:05:50 +0000240static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000241{
bellard83fb7ad2004-07-05 21:25:26 +0000242 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000243 TARGET_PAGE_SIZE */
aliguoric2b48b62008-11-11 22:06:42 +0000244#ifdef _WIN32
245 {
246 SYSTEM_INFO system_info;
247
248 GetSystemInfo(&system_info);
249 qemu_real_host_page_size = system_info.dwPageSize;
250 }
251#else
252 qemu_real_host_page_size = getpagesize();
253#endif
bellard83fb7ad2004-07-05 21:25:26 +0000254 if (qemu_host_page_size == 0)
255 qemu_host_page_size = qemu_real_host_page_size;
256 if (qemu_host_page_size < TARGET_PAGE_SIZE)
257 qemu_host_page_size = TARGET_PAGE_SIZE;
258 qemu_host_page_bits = 0;
259 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
260 qemu_host_page_bits++;
261 qemu_host_page_mask = ~(qemu_host_page_size - 1);
bellard108c49b2005-07-24 12:55:09 +0000262 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
263 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
balrog50a95692007-12-12 01:16:23 +0000264
265#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
266 {
267 long long startaddr, endaddr;
268 FILE *f;
269 int n;
270
pbrookc8a706f2008-06-02 16:16:42 +0000271 mmap_lock();
pbrook07765902008-05-31 16:33:53 +0000272 last_brk = (unsigned long)sbrk(0);
balrog50a95692007-12-12 01:16:23 +0000273 f = fopen("/proc/self/maps", "r");
274 if (f) {
275 do {
276 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
277 if (n == 2) {
blueswir1e0b8d652008-05-03 17:51:24 +0000278 startaddr = MIN(startaddr,
279 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
280 endaddr = MIN(endaddr,
281 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
pbrookb5fc9092008-05-29 13:56:10 +0000282 page_set_flags(startaddr & TARGET_PAGE_MASK,
balrog50a95692007-12-12 01:16:23 +0000283 TARGET_PAGE_ALIGN(endaddr),
284 PAGE_RESERVED);
285 }
286 } while (!feof(f));
287 fclose(f);
288 }
pbrookc8a706f2008-06-02 16:16:42 +0000289 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000290 }
291#endif
bellard54936002003-05-13 00:25:15 +0000292}
293
aliguori434929b2008-09-15 15:56:30 +0000294static inline PageDesc **page_l1_map(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000295{
pbrook17e23772008-06-09 13:47:45 +0000296#if TARGET_LONG_BITS > 32
297 /* Host memory outside guest VM. For 32-bit targets we have already
298 excluded high addresses. */
thsd8173e02008-08-29 13:10:00 +0000299 if (index > ((target_ulong)L2_SIZE * L1_SIZE))
pbrook17e23772008-06-09 13:47:45 +0000300 return NULL;
301#endif
aliguori434929b2008-09-15 15:56:30 +0000302 return &l1_map[index >> L2_BITS];
303}
304
305static inline PageDesc *page_find_alloc(target_ulong index)
306{
307 PageDesc **lp, *p;
308 lp = page_l1_map(index);
309 if (!lp)
310 return NULL;
311
bellard54936002003-05-13 00:25:15 +0000312 p = *lp;
313 if (!p) {
314 /* allocate if not found */
pbrook17e23772008-06-09 13:47:45 +0000315#if defined(CONFIG_USER_ONLY)
pbrook17e23772008-06-09 13:47:45 +0000316 size_t len = sizeof(PageDesc) * L2_SIZE;
317 /* Don't use qemu_malloc because it may recurse. */
Blue Swirl660f11b2009-07-31 21:16:51 +0000318 p = mmap(NULL, len, PROT_READ | PROT_WRITE,
pbrook17e23772008-06-09 13:47:45 +0000319 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
bellard54936002003-05-13 00:25:15 +0000320 *lp = p;
aurel32fb1c2cd2008-12-08 18:12:26 +0000321 if (h2g_valid(p)) {
322 unsigned long addr = h2g(p);
pbrook17e23772008-06-09 13:47:45 +0000323 page_set_flags(addr & TARGET_PAGE_MASK,
324 TARGET_PAGE_ALIGN(addr + len),
325 PAGE_RESERVED);
326 }
327#else
328 p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
329 *lp = p;
330#endif
bellard54936002003-05-13 00:25:15 +0000331 }
332 return p + (index & (L2_SIZE - 1));
333}
334
aurel3200f82b82008-04-27 21:12:55 +0000335static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000336{
aliguori434929b2008-09-15 15:56:30 +0000337 PageDesc **lp, *p;
338 lp = page_l1_map(index);
339 if (!lp)
340 return NULL;
bellard54936002003-05-13 00:25:15 +0000341
aliguori434929b2008-09-15 15:56:30 +0000342 p = *lp;
Blue Swirl660f11b2009-07-31 21:16:51 +0000343 if (!p) {
344 return NULL;
345 }
bellardfd6ce8f2003-05-14 19:00:11 +0000346 return p + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000347}
348
Anthony Liguoric227f092009-10-01 16:12:16 -0500349static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000350{
bellard108c49b2005-07-24 12:55:09 +0000351 void **lp, **p;
pbrooke3f4e2a2006-04-08 20:02:06 +0000352 PhysPageDesc *pd;
bellard92e873b2004-05-21 14:52:29 +0000353
bellard108c49b2005-07-24 12:55:09 +0000354 p = (void **)l1_phys_map;
355#if TARGET_PHYS_ADDR_SPACE_BITS > 32
356
357#if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
358#error unsupported TARGET_PHYS_ADDR_SPACE_BITS
359#endif
360 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000361 p = *lp;
362 if (!p) {
363 /* allocate if not found */
bellard108c49b2005-07-24 12:55:09 +0000364 if (!alloc)
365 return NULL;
366 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
367 memset(p, 0, sizeof(void *) * L1_SIZE);
368 *lp = p;
369 }
370#endif
371 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
pbrooke3f4e2a2006-04-08 20:02:06 +0000372 pd = *lp;
373 if (!pd) {
374 int i;
bellard108c49b2005-07-24 12:55:09 +0000375 /* allocate if not found */
376 if (!alloc)
377 return NULL;
pbrooke3f4e2a2006-04-08 20:02:06 +0000378 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
379 *lp = pd;
pbrook67c4d232009-02-23 13:16:07 +0000380 for (i = 0; i < L2_SIZE; i++) {
pbrooke3f4e2a2006-04-08 20:02:06 +0000381 pd[i].phys_offset = IO_MEM_UNASSIGNED;
pbrook67c4d232009-02-23 13:16:07 +0000382 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
383 }
bellard92e873b2004-05-21 14:52:29 +0000384 }
pbrooke3f4e2a2006-04-08 20:02:06 +0000385 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000386}
387
Anthony Liguoric227f092009-10-01 16:12:16 -0500388static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000389{
bellard108c49b2005-07-24 12:55:09 +0000390 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000391}
392
bellard9fa3e852004-01-04 18:06:42 +0000393#if !defined(CONFIG_USER_ONLY)
Anthony Liguoric227f092009-10-01 16:12:16 -0500394static void tlb_protect_code(ram_addr_t ram_addr);
395static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000396 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000397#define mmap_lock() do { } while(0)
398#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000399#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000400
bellard43694152008-05-29 09:35:57 +0000401#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
402
403#if defined(CONFIG_USER_ONLY)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100404/* Currently it is not recommended to allocate big chunks of data in
bellard43694152008-05-29 09:35:57 +0000405 user mode. It will change when a dedicated libc will be used */
406#define USE_STATIC_CODE_GEN_BUFFER
407#endif
408
409#ifdef USE_STATIC_CODE_GEN_BUFFER
410static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
411#endif
412
blueswir18fcd3692008-08-17 20:26:25 +0000413static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000414{
bellard43694152008-05-29 09:35:57 +0000415#ifdef USE_STATIC_CODE_GEN_BUFFER
416 code_gen_buffer = static_code_gen_buffer;
417 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
418 map_exec(code_gen_buffer, code_gen_buffer_size);
419#else
bellard26a5f132008-05-28 12:30:31 +0000420 code_gen_buffer_size = tb_size;
421 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000422#if defined(CONFIG_USER_ONLY)
423 /* in user mode, phys_ram_size is not meaningful */
424 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
425#else
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100426 /* XXX: needs adjustments */
pbrook94a6b542009-04-11 17:15:54 +0000427 code_gen_buffer_size = (unsigned long)(ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000428#endif
bellard26a5f132008-05-28 12:30:31 +0000429 }
430 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
431 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
432 /* The code gen buffer location may have constraints depending on
433 the host cpu and OS */
434#if defined(__linux__)
435 {
436 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000437 void *start = NULL;
438
bellard26a5f132008-05-28 12:30:31 +0000439 flags = MAP_PRIVATE | MAP_ANONYMOUS;
440#if defined(__x86_64__)
441 flags |= MAP_32BIT;
442 /* Cannot map more than that */
443 if (code_gen_buffer_size > (800 * 1024 * 1024))
444 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000445#elif defined(__sparc_v9__)
446 // Map the buffer below 2G, so we can use direct calls and branches
447 flags |= MAP_FIXED;
448 start = (void *) 0x60000000UL;
449 if (code_gen_buffer_size > (512 * 1024 * 1024))
450 code_gen_buffer_size = (512 * 1024 * 1024);
balrog1cb06612008-12-01 02:10:17 +0000451#elif defined(__arm__)
balrog63d41242008-12-01 02:19:41 +0000452 /* Map the buffer below 32M, so we can use direct calls and branches */
balrog1cb06612008-12-01 02:10:17 +0000453 flags |= MAP_FIXED;
454 start = (void *) 0x01000000UL;
455 if (code_gen_buffer_size > 16 * 1024 * 1024)
456 code_gen_buffer_size = 16 * 1024 * 1024;
bellard26a5f132008-05-28 12:30:31 +0000457#endif
blueswir1141ac462008-07-26 15:05:57 +0000458 code_gen_buffer = mmap(start, code_gen_buffer_size,
459 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000460 flags, -1, 0);
461 if (code_gen_buffer == MAP_FAILED) {
462 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
463 exit(1);
464 }
465 }
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100466#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguori06e67a82008-09-27 15:32:41 +0000467 {
468 int flags;
469 void *addr = NULL;
470 flags = MAP_PRIVATE | MAP_ANONYMOUS;
471#if defined(__x86_64__)
472 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
473 * 0x40000000 is free */
474 flags |= MAP_FIXED;
475 addr = (void *)0x40000000;
476 /* Cannot map more than that */
477 if (code_gen_buffer_size > (800 * 1024 * 1024))
478 code_gen_buffer_size = (800 * 1024 * 1024);
479#endif
480 code_gen_buffer = mmap(addr, code_gen_buffer_size,
481 PROT_WRITE | PROT_READ | PROT_EXEC,
482 flags, -1, 0);
483 if (code_gen_buffer == MAP_FAILED) {
484 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
485 exit(1);
486 }
487 }
bellard26a5f132008-05-28 12:30:31 +0000488#else
489 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
bellard26a5f132008-05-28 12:30:31 +0000490 map_exec(code_gen_buffer, code_gen_buffer_size);
491#endif
bellard43694152008-05-29 09:35:57 +0000492#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000493 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
494 code_gen_buffer_max_size = code_gen_buffer_size -
495 code_gen_max_block_size();
496 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
497 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
498}
499
500/* Must be called before using the QEMU cpus. 'tb_size' is the size
501 (in bytes) allocated to the translation buffer. Zero means default
502 size. */
503void cpu_exec_init_all(unsigned long tb_size)
504{
bellard26a5f132008-05-28 12:30:31 +0000505 cpu_gen_init();
506 code_gen_alloc(tb_size);
507 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000508 page_init();
pbrooke2eef172008-06-08 01:09:01 +0000509#if !defined(CONFIG_USER_ONLY)
bellard26a5f132008-05-28 12:30:31 +0000510 io_mem_init();
pbrooke2eef172008-06-08 01:09:01 +0000511#endif
bellard26a5f132008-05-28 12:30:31 +0000512}
513
pbrook9656f322008-07-01 20:01:19 +0000514#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
515
Juan Quintelad4bfa4d2009-09-29 22:48:22 +0200516static void cpu_common_pre_save(void *opaque)
pbrook9656f322008-07-01 20:01:19 +0000517{
Juan Quintelad4bfa4d2009-09-29 22:48:22 +0200518 CPUState *env = opaque;
pbrook9656f322008-07-01 20:01:19 +0000519
Avi Kivity4c0960c2009-08-17 23:19:53 +0300520 cpu_synchronize_state(env);
pbrook9656f322008-07-01 20:01:19 +0000521}
522
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200523static int cpu_common_pre_load(void *opaque)
pbrook9656f322008-07-01 20:01:19 +0000524{
525 CPUState *env = opaque;
526
Avi Kivity4c0960c2009-08-17 23:19:53 +0300527 cpu_synchronize_state(env);
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200528 return 0;
529}
pbrook9656f322008-07-01 20:01:19 +0000530
Juan Quintelae59fb372009-09-29 22:48:21 +0200531static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200532{
533 CPUState *env = opaque;
534
aurel323098dba2009-03-07 21:28:24 +0000535 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
536 version_id is increased. */
537 env->interrupt_request &= ~0x01;
pbrook9656f322008-07-01 20:01:19 +0000538 tlb_flush(env, 1);
539
540 return 0;
541}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200542
543static const VMStateDescription vmstate_cpu_common = {
544 .name = "cpu_common",
545 .version_id = 1,
546 .minimum_version_id = 1,
547 .minimum_version_id_old = 1,
548 .pre_save = cpu_common_pre_save,
549 .pre_load = cpu_common_pre_load,
550 .post_load = cpu_common_post_load,
551 .fields = (VMStateField []) {
552 VMSTATE_UINT32(halted, CPUState),
553 VMSTATE_UINT32(interrupt_request, CPUState),
554 VMSTATE_END_OF_LIST()
555 }
556};
pbrook9656f322008-07-01 20:01:19 +0000557#endif
558
Glauber Costa950f1472009-06-09 12:15:18 -0400559CPUState *qemu_get_cpu(int cpu)
560{
561 CPUState *env = first_cpu;
562
563 while (env) {
564 if (env->cpu_index == cpu)
565 break;
566 env = env->next_cpu;
567 }
568
569 return env;
570}
571
bellard6a00d602005-11-21 23:25:50 +0000572void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000573{
bellard6a00d602005-11-21 23:25:50 +0000574 CPUState **penv;
575 int cpu_index;
576
pbrookc2764712009-03-07 15:24:59 +0000577#if defined(CONFIG_USER_ONLY)
578 cpu_list_lock();
579#endif
bellard6a00d602005-11-21 23:25:50 +0000580 env->next_cpu = NULL;
581 penv = &first_cpu;
582 cpu_index = 0;
583 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700584 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000585 cpu_index++;
586 }
587 env->cpu_index = cpu_index;
aliguori268a3622009-04-21 22:30:27 +0000588 env->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000589 QTAILQ_INIT(&env->breakpoints);
590 QTAILQ_INIT(&env->watchpoints);
bellard6a00d602005-11-21 23:25:50 +0000591 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000592#if defined(CONFIG_USER_ONLY)
593 cpu_list_unlock();
594#endif
pbrookb3c77242008-06-30 16:31:04 +0000595#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200596 vmstate_register(cpu_index, &vmstate_cpu_common, env);
pbrookb3c77242008-06-30 16:31:04 +0000597 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
598 cpu_save, cpu_load, env);
599#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000600}
601
bellard9fa3e852004-01-04 18:06:42 +0000602static inline void invalidate_page_bitmap(PageDesc *p)
603{
604 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000605 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000606 p->code_bitmap = NULL;
607 }
608 p->code_write_count = 0;
609}
610
bellardfd6ce8f2003-05-14 19:00:11 +0000611/* set to NULL all the 'first_tb' fields in all PageDescs */
612static void page_flush_tb(void)
613{
614 int i, j;
615 PageDesc *p;
616
617 for(i = 0; i < L1_SIZE; i++) {
618 p = l1_map[i];
619 if (p) {
bellard9fa3e852004-01-04 18:06:42 +0000620 for(j = 0; j < L2_SIZE; j++) {
621 p->first_tb = NULL;
622 invalidate_page_bitmap(p);
623 p++;
624 }
bellardfd6ce8f2003-05-14 19:00:11 +0000625 }
626 }
627}
628
629/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000630/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000631void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000632{
bellard6a00d602005-11-21 23:25:50 +0000633 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000634#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000635 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
636 (unsigned long)(code_gen_ptr - code_gen_buffer),
637 nb_tbs, nb_tbs > 0 ?
638 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000639#endif
bellard26a5f132008-05-28 12:30:31 +0000640 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000641 cpu_abort(env1, "Internal error: code buffer overflow\n");
642
bellardfd6ce8f2003-05-14 19:00:11 +0000643 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000644
bellard6a00d602005-11-21 23:25:50 +0000645 for(env = first_cpu; env != NULL; env = env->next_cpu) {
646 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
647 }
bellard9fa3e852004-01-04 18:06:42 +0000648
bellard8a8a6082004-10-03 13:36:49 +0000649 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000650 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000651
bellardfd6ce8f2003-05-14 19:00:11 +0000652 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000653 /* XXX: flush processor icache at this point if cache flush is
654 expensive */
bellarde3db7222005-01-26 22:00:47 +0000655 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000656}
657
658#ifdef DEBUG_TB_CHECK
659
j_mayerbc98a7e2007-04-04 07:55:12 +0000660static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000661{
662 TranslationBlock *tb;
663 int i;
664 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000665 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
666 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000667 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
668 address >= tb->pc + tb->size)) {
Blue Swirl0bf9e312009-07-20 17:19:25 +0000669 printf("ERROR invalidate: address=" TARGET_FMT_lx
670 " PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000671 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000672 }
673 }
674 }
675}
676
677/* verify that all the pages have correct rights for code */
678static void tb_page_check(void)
679{
680 TranslationBlock *tb;
681 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000682
pbrook99773bd2006-04-16 15:14:59 +0000683 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
684 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000685 flags1 = page_get_flags(tb->pc);
686 flags2 = page_get_flags(tb->pc + tb->size - 1);
687 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
688 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000689 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000690 }
691 }
692 }
693}
694
695#endif
696
697/* invalidate one TB */
698static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
699 int next_offset)
700{
701 TranslationBlock *tb1;
702 for(;;) {
703 tb1 = *ptb;
704 if (tb1 == tb) {
705 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
706 break;
707 }
708 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
709 }
710}
711
bellard9fa3e852004-01-04 18:06:42 +0000712static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
713{
714 TranslationBlock *tb1;
715 unsigned int n1;
716
717 for(;;) {
718 tb1 = *ptb;
719 n1 = (long)tb1 & 3;
720 tb1 = (TranslationBlock *)((long)tb1 & ~3);
721 if (tb1 == tb) {
722 *ptb = tb1->page_next[n1];
723 break;
724 }
725 ptb = &tb1->page_next[n1];
726 }
727}
728
bellardd4e81642003-05-25 16:46:15 +0000729static inline void tb_jmp_remove(TranslationBlock *tb, int n)
730{
731 TranslationBlock *tb1, **ptb;
732 unsigned int n1;
733
734 ptb = &tb->jmp_next[n];
735 tb1 = *ptb;
736 if (tb1) {
737 /* find tb(n) in circular list */
738 for(;;) {
739 tb1 = *ptb;
740 n1 = (long)tb1 & 3;
741 tb1 = (TranslationBlock *)((long)tb1 & ~3);
742 if (n1 == n && tb1 == tb)
743 break;
744 if (n1 == 2) {
745 ptb = &tb1->jmp_first;
746 } else {
747 ptb = &tb1->jmp_next[n1];
748 }
749 }
750 /* now we can suppress tb(n) from the list */
751 *ptb = tb->jmp_next[n];
752
753 tb->jmp_next[n] = NULL;
754 }
755}
756
757/* reset the jump entry 'n' of a TB so that it is not chained to
758 another TB */
759static inline void tb_reset_jump(TranslationBlock *tb, int n)
760{
761 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
762}
763
pbrook2e70f6e2008-06-29 01:03:05 +0000764void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000765{
bellard6a00d602005-11-21 23:25:50 +0000766 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000767 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000768 unsigned int h, n1;
Anthony Liguoric227f092009-10-01 16:12:16 -0500769 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000770 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000771
bellard9fa3e852004-01-04 18:06:42 +0000772 /* remove the TB from the hash list */
773 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
774 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000775 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000776 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000777
bellard9fa3e852004-01-04 18:06:42 +0000778 /* remove the TB from the page list */
779 if (tb->page_addr[0] != page_addr) {
780 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
781 tb_page_remove(&p->first_tb, tb);
782 invalidate_page_bitmap(p);
783 }
784 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
785 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
786 tb_page_remove(&p->first_tb, tb);
787 invalidate_page_bitmap(p);
788 }
789
bellard8a40a182005-11-20 10:35:40 +0000790 tb_invalidated_flag = 1;
791
792 /* remove the TB from the hash list */
793 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000794 for(env = first_cpu; env != NULL; env = env->next_cpu) {
795 if (env->tb_jmp_cache[h] == tb)
796 env->tb_jmp_cache[h] = NULL;
797 }
bellard8a40a182005-11-20 10:35:40 +0000798
799 /* suppress this TB from the two jump lists */
800 tb_jmp_remove(tb, 0);
801 tb_jmp_remove(tb, 1);
802
803 /* suppress any remaining jumps to this TB */
804 tb1 = tb->jmp_first;
805 for(;;) {
806 n1 = (long)tb1 & 3;
807 if (n1 == 2)
808 break;
809 tb1 = (TranslationBlock *)((long)tb1 & ~3);
810 tb2 = tb1->jmp_next[n1];
811 tb_reset_jump(tb1, n1);
812 tb1->jmp_next[n1] = NULL;
813 tb1 = tb2;
814 }
815 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
816
bellarde3db7222005-01-26 22:00:47 +0000817 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000818}
819
820static inline void set_bits(uint8_t *tab, int start, int len)
821{
822 int end, mask, end1;
823
824 end = start + len;
825 tab += start >> 3;
826 mask = 0xff << (start & 7);
827 if ((start & ~7) == (end & ~7)) {
828 if (start < end) {
829 mask &= ~(0xff << (end & 7));
830 *tab |= mask;
831 }
832 } else {
833 *tab++ |= mask;
834 start = (start + 8) & ~7;
835 end1 = end & ~7;
836 while (start < end1) {
837 *tab++ = 0xff;
838 start += 8;
839 }
840 if (start < end) {
841 mask = ~(0xff << (end & 7));
842 *tab |= mask;
843 }
844 }
845}
846
847static void build_page_bitmap(PageDesc *p)
848{
849 int n, tb_start, tb_end;
850 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000851
pbrookb2a70812008-06-09 13:57:23 +0000852 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000853
854 tb = p->first_tb;
855 while (tb != NULL) {
856 n = (long)tb & 3;
857 tb = (TranslationBlock *)((long)tb & ~3);
858 /* NOTE: this is subtle as a TB may span two physical pages */
859 if (n == 0) {
860 /* NOTE: tb_end may be after the end of the page, but
861 it is not a problem */
862 tb_start = tb->pc & ~TARGET_PAGE_MASK;
863 tb_end = tb_start + tb->size;
864 if (tb_end > TARGET_PAGE_SIZE)
865 tb_end = TARGET_PAGE_SIZE;
866 } else {
867 tb_start = 0;
868 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
869 }
870 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
871 tb = tb->page_next[n];
872 }
873}
874
pbrook2e70f6e2008-06-29 01:03:05 +0000875TranslationBlock *tb_gen_code(CPUState *env,
876 target_ulong pc, target_ulong cs_base,
877 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000878{
879 TranslationBlock *tb;
880 uint8_t *tc_ptr;
881 target_ulong phys_pc, phys_page2, virt_page2;
882 int code_gen_size;
883
bellardc27004e2005-01-03 23:35:10 +0000884 phys_pc = get_phys_addr_code(env, pc);
885 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000886 if (!tb) {
887 /* flush must be done */
888 tb_flush(env);
889 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000890 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000891 /* Don't forget to invalidate previous TB info. */
892 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000893 }
894 tc_ptr = code_gen_ptr;
895 tb->tc_ptr = tc_ptr;
896 tb->cs_base = cs_base;
897 tb->flags = flags;
898 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000899 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000900 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 +0000901
bellardd720b932004-04-25 17:57:43 +0000902 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000903 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000904 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000905 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000906 phys_page2 = get_phys_addr_code(env, virt_page2);
907 }
908 tb_link_phys(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +0000909 return tb;
bellardd720b932004-04-25 17:57:43 +0000910}
ths3b46e622007-09-17 08:09:54 +0000911
bellard9fa3e852004-01-04 18:06:42 +0000912/* invalidate all TBs which intersect with the target physical page
913 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000914 the same physical page. 'is_cpu_write_access' should be true if called
915 from a real cpu write access: the virtual CPU will exit the current
916 TB if code is modified inside this TB. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500917void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000918 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000919{
aliguori6b917542008-11-18 19:46:41 +0000920 TranslationBlock *tb, *tb_next, *saved_tb;
bellardd720b932004-04-25 17:57:43 +0000921 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000922 target_ulong tb_start, tb_end;
aliguori6b917542008-11-18 19:46:41 +0000923 PageDesc *p;
924 int n;
925#ifdef TARGET_HAS_PRECISE_SMC
926 int current_tb_not_found = is_cpu_write_access;
927 TranslationBlock *current_tb = NULL;
928 int current_tb_modified = 0;
929 target_ulong current_pc = 0;
930 target_ulong current_cs_base = 0;
931 int current_flags = 0;
932#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000933
934 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000935 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000936 return;
ths5fafdf22007-09-16 21:08:06 +0000937 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000938 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
939 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000940 /* build code bitmap */
941 build_page_bitmap(p);
942 }
943
944 /* we remove all the TBs in the range [start, end[ */
945 /* XXX: see if in some cases it could be faster to invalidate all the code */
946 tb = p->first_tb;
947 while (tb != NULL) {
948 n = (long)tb & 3;
949 tb = (TranslationBlock *)((long)tb & ~3);
950 tb_next = tb->page_next[n];
951 /* NOTE: this is subtle as a TB may span two physical pages */
952 if (n == 0) {
953 /* NOTE: tb_end may be after the end of the page, but
954 it is not a problem */
955 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
956 tb_end = tb_start + tb->size;
957 } else {
958 tb_start = tb->page_addr[1];
959 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
960 }
961 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000962#ifdef TARGET_HAS_PRECISE_SMC
963 if (current_tb_not_found) {
964 current_tb_not_found = 0;
965 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000966 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +0000967 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +0000968 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +0000969 }
970 }
971 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +0000972 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +0000973 /* If we are modifying the current TB, we must stop
974 its execution. We could be more precise by checking
975 that the modification is after the current PC, but it
976 would require a specialized function to partially
977 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +0000978
bellardd720b932004-04-25 17:57:43 +0000979 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +0000980 cpu_restore_state(current_tb, env,
pbrook2e70f6e2008-06-29 01:03:05 +0000981 env->mem_io_pc, NULL);
aliguori6b917542008-11-18 19:46:41 +0000982 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
983 &current_flags);
bellardd720b932004-04-25 17:57:43 +0000984 }
985#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +0000986 /* we need to do that to handle the case where a signal
987 occurs while doing tb_phys_invalidate() */
988 saved_tb = NULL;
989 if (env) {
990 saved_tb = env->current_tb;
991 env->current_tb = NULL;
992 }
bellard9fa3e852004-01-04 18:06:42 +0000993 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +0000994 if (env) {
995 env->current_tb = saved_tb;
996 if (env->interrupt_request && env->current_tb)
997 cpu_interrupt(env, env->interrupt_request);
998 }
bellard9fa3e852004-01-04 18:06:42 +0000999 }
1000 tb = tb_next;
1001 }
1002#if !defined(CONFIG_USER_ONLY)
1003 /* if no code remaining, no need to continue to use slow writes */
1004 if (!p->first_tb) {
1005 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +00001006 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +00001007 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +00001008 }
1009 }
1010#endif
1011#ifdef TARGET_HAS_PRECISE_SMC
1012 if (current_tb_modified) {
1013 /* we generate a block containing just the instruction
1014 modifying the memory. It will ensure that it cannot modify
1015 itself */
bellardea1c1802004-06-14 18:56:36 +00001016 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001017 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001018 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001019 }
1020#endif
1021}
1022
1023/* len must be <= 8 and start must be a multiple of len */
Anthony Liguoric227f092009-10-01 16:12:16 -05001024static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +00001025{
1026 PageDesc *p;
1027 int offset, b;
bellard59817cc2004-02-16 22:01:13 +00001028#if 0
bellarda4193c82004-06-03 14:01:43 +00001029 if (1) {
aliguori93fcfe32009-01-15 22:34:14 +00001030 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1031 cpu_single_env->mem_io_vaddr, len,
1032 cpu_single_env->eip,
1033 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
bellard59817cc2004-02-16 22:01:13 +00001034 }
1035#endif
bellard9fa3e852004-01-04 18:06:42 +00001036 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001037 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001038 return;
1039 if (p->code_bitmap) {
1040 offset = start & ~TARGET_PAGE_MASK;
1041 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1042 if (b & ((1 << len) - 1))
1043 goto do_invalidate;
1044 } else {
1045 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001046 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001047 }
1048}
1049
bellard9fa3e852004-01-04 18:06:42 +00001050#if !defined(CONFIG_SOFTMMU)
Anthony Liguoric227f092009-10-01 16:12:16 -05001051static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001052 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001053{
aliguori6b917542008-11-18 19:46:41 +00001054 TranslationBlock *tb;
bellard9fa3e852004-01-04 18:06:42 +00001055 PageDesc *p;
aliguori6b917542008-11-18 19:46:41 +00001056 int n;
bellardd720b932004-04-25 17:57:43 +00001057#ifdef TARGET_HAS_PRECISE_SMC
aliguori6b917542008-11-18 19:46:41 +00001058 TranslationBlock *current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001059 CPUState *env = cpu_single_env;
aliguori6b917542008-11-18 19:46:41 +00001060 int current_tb_modified = 0;
1061 target_ulong current_pc = 0;
1062 target_ulong current_cs_base = 0;
1063 int current_flags = 0;
bellardd720b932004-04-25 17:57:43 +00001064#endif
bellard9fa3e852004-01-04 18:06:42 +00001065
1066 addr &= TARGET_PAGE_MASK;
1067 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001068 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001069 return;
1070 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001071#ifdef TARGET_HAS_PRECISE_SMC
1072 if (tb && pc != 0) {
1073 current_tb = tb_find_pc(pc);
1074 }
1075#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001076 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001077 n = (long)tb & 3;
1078 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001079#ifdef TARGET_HAS_PRECISE_SMC
1080 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001081 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001082 /* If we are modifying the current TB, we must stop
1083 its execution. We could be more precise by checking
1084 that the modification is after the current PC, but it
1085 would require a specialized function to partially
1086 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001087
bellardd720b932004-04-25 17:57:43 +00001088 current_tb_modified = 1;
1089 cpu_restore_state(current_tb, env, pc, puc);
aliguori6b917542008-11-18 19:46:41 +00001090 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1091 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001092 }
1093#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001094 tb_phys_invalidate(tb, addr);
1095 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001096 }
1097 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001098#ifdef TARGET_HAS_PRECISE_SMC
1099 if (current_tb_modified) {
1100 /* we generate a block containing just the instruction
1101 modifying the memory. It will ensure that it cannot modify
1102 itself */
bellardea1c1802004-06-14 18:56:36 +00001103 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001104 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001105 cpu_resume_from_signal(env, puc);
1106 }
1107#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001108}
bellard9fa3e852004-01-04 18:06:42 +00001109#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001110
1111/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001112static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +00001113 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001114{
1115 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +00001116 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001117
bellard9fa3e852004-01-04 18:06:42 +00001118 tb->page_addr[n] = page_addr;
bellard3a7d9292005-08-21 09:26:42 +00001119 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001120 tb->page_next[n] = p->first_tb;
1121 last_first_tb = p->first_tb;
1122 p->first_tb = (TranslationBlock *)((long)tb | n);
1123 invalidate_page_bitmap(p);
1124
bellard107db442004-06-22 18:48:46 +00001125#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001126
bellard9fa3e852004-01-04 18:06:42 +00001127#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001128 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001129 target_ulong addr;
1130 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001131 int prot;
1132
bellardfd6ce8f2003-05-14 19:00:11 +00001133 /* force the host page as non writable (writes will have a
1134 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001135 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001136 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001137 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1138 addr += TARGET_PAGE_SIZE) {
1139
1140 p2 = page_find (addr >> TARGET_PAGE_BITS);
1141 if (!p2)
1142 continue;
1143 prot |= p2->flags;
1144 p2->flags &= ~PAGE_WRITE;
1145 page_get_flags(addr);
1146 }
ths5fafdf22007-09-16 21:08:06 +00001147 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001148 (prot & PAGE_BITS) & ~PAGE_WRITE);
1149#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001150 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001151 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001152#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001153 }
bellard9fa3e852004-01-04 18:06:42 +00001154#else
1155 /* if some code is already present, then the pages are already
1156 protected. So we handle the case where only the first TB is
1157 allocated in a physical page */
1158 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001159 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001160 }
1161#endif
bellardd720b932004-04-25 17:57:43 +00001162
1163#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001164}
1165
1166/* Allocate a new translation block. Flush the translation buffer if
1167 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001168TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001169{
1170 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001171
bellard26a5f132008-05-28 12:30:31 +00001172 if (nb_tbs >= code_gen_max_blocks ||
1173 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001174 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001175 tb = &tbs[nb_tbs++];
1176 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001177 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001178 return tb;
1179}
1180
pbrook2e70f6e2008-06-29 01:03:05 +00001181void tb_free(TranslationBlock *tb)
1182{
thsbf20dc02008-06-30 17:22:19 +00001183 /* In practice this is mostly used for single use temporary TB
pbrook2e70f6e2008-06-29 01:03:05 +00001184 Ignore the hard cases and just back up if this TB happens to
1185 be the last one generated. */
1186 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1187 code_gen_ptr = tb->tc_ptr;
1188 nb_tbs--;
1189 }
1190}
1191
bellard9fa3e852004-01-04 18:06:42 +00001192/* add a new TB and link it to the physical page tables. phys_page2 is
1193 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001194void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001195 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001196{
bellard9fa3e852004-01-04 18:06:42 +00001197 unsigned int h;
1198 TranslationBlock **ptb;
1199
pbrookc8a706f2008-06-02 16:16:42 +00001200 /* Grab the mmap lock to stop another thread invalidating this TB
1201 before we are done. */
1202 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001203 /* add in the physical hash table */
1204 h = tb_phys_hash_func(phys_pc);
1205 ptb = &tb_phys_hash[h];
1206 tb->phys_hash_next = *ptb;
1207 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001208
1209 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001210 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1211 if (phys_page2 != -1)
1212 tb_alloc_page(tb, 1, phys_page2);
1213 else
1214 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001215
bellardd4e81642003-05-25 16:46:15 +00001216 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1217 tb->jmp_next[0] = NULL;
1218 tb->jmp_next[1] = NULL;
1219
1220 /* init original jump addresses */
1221 if (tb->tb_next_offset[0] != 0xffff)
1222 tb_reset_jump(tb, 0);
1223 if (tb->tb_next_offset[1] != 0xffff)
1224 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001225
1226#ifdef DEBUG_TB_CHECK
1227 tb_page_check();
1228#endif
pbrookc8a706f2008-06-02 16:16:42 +00001229 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001230}
1231
bellarda513fe12003-05-27 23:29:48 +00001232/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1233 tb[1].tc_ptr. Return NULL if not found */
1234TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1235{
1236 int m_min, m_max, m;
1237 unsigned long v;
1238 TranslationBlock *tb;
1239
1240 if (nb_tbs <= 0)
1241 return NULL;
1242 if (tc_ptr < (unsigned long)code_gen_buffer ||
1243 tc_ptr >= (unsigned long)code_gen_ptr)
1244 return NULL;
1245 /* binary search (cf Knuth) */
1246 m_min = 0;
1247 m_max = nb_tbs - 1;
1248 while (m_min <= m_max) {
1249 m = (m_min + m_max) >> 1;
1250 tb = &tbs[m];
1251 v = (unsigned long)tb->tc_ptr;
1252 if (v == tc_ptr)
1253 return tb;
1254 else if (tc_ptr < v) {
1255 m_max = m - 1;
1256 } else {
1257 m_min = m + 1;
1258 }
ths5fafdf22007-09-16 21:08:06 +00001259 }
bellarda513fe12003-05-27 23:29:48 +00001260 return &tbs[m_max];
1261}
bellard75012672003-06-21 13:11:07 +00001262
bellardea041c02003-06-25 16:16:50 +00001263static void tb_reset_jump_recursive(TranslationBlock *tb);
1264
1265static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1266{
1267 TranslationBlock *tb1, *tb_next, **ptb;
1268 unsigned int n1;
1269
1270 tb1 = tb->jmp_next[n];
1271 if (tb1 != NULL) {
1272 /* find head of list */
1273 for(;;) {
1274 n1 = (long)tb1 & 3;
1275 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1276 if (n1 == 2)
1277 break;
1278 tb1 = tb1->jmp_next[n1];
1279 }
1280 /* we are now sure now that tb jumps to tb1 */
1281 tb_next = tb1;
1282
1283 /* remove tb from the jmp_first list */
1284 ptb = &tb_next->jmp_first;
1285 for(;;) {
1286 tb1 = *ptb;
1287 n1 = (long)tb1 & 3;
1288 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1289 if (n1 == n && tb1 == tb)
1290 break;
1291 ptb = &tb1->jmp_next[n1];
1292 }
1293 *ptb = tb->jmp_next[n];
1294 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001295
bellardea041c02003-06-25 16:16:50 +00001296 /* suppress the jump to next tb in generated code */
1297 tb_reset_jump(tb, n);
1298
bellard01243112004-01-04 15:48:17 +00001299 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001300 tb_reset_jump_recursive(tb_next);
1301 }
1302}
1303
1304static void tb_reset_jump_recursive(TranslationBlock *tb)
1305{
1306 tb_reset_jump_recursive2(tb, 0);
1307 tb_reset_jump_recursive2(tb, 1);
1308}
1309
bellard1fddef42005-04-17 19:16:13 +00001310#if defined(TARGET_HAS_ICE)
bellardd720b932004-04-25 17:57:43 +00001311static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1312{
Anthony Liguoric227f092009-10-01 16:12:16 -05001313 target_phys_addr_t addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00001314 target_ulong pd;
Anthony Liguoric227f092009-10-01 16:12:16 -05001315 ram_addr_t ram_addr;
pbrookc2f07f82006-04-08 17:14:56 +00001316 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001317
pbrookc2f07f82006-04-08 17:14:56 +00001318 addr = cpu_get_phys_page_debug(env, pc);
1319 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1320 if (!p) {
1321 pd = IO_MEM_UNASSIGNED;
1322 } else {
1323 pd = p->phys_offset;
1324 }
1325 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001326 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001327}
bellardc27004e2005-01-03 23:35:10 +00001328#endif
bellardd720b932004-04-25 17:57:43 +00001329
pbrook6658ffb2007-03-16 23:58:11 +00001330/* Add a watchpoint. */
aliguoria1d1bb32008-11-18 20:07:32 +00001331int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1332 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +00001333{
aliguorib4051332008-11-18 20:14:20 +00001334 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +00001335 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001336
aliguorib4051332008-11-18 20:14:20 +00001337 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1338 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1339 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1340 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1341 return -EINVAL;
1342 }
aliguoria1d1bb32008-11-18 20:07:32 +00001343 wp = qemu_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +00001344
aliguoria1d1bb32008-11-18 20:07:32 +00001345 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +00001346 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +00001347 wp->flags = flags;
1348
aliguori2dc9f412008-11-18 20:56:59 +00001349 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001350 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001351 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001352 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001353 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001354
pbrook6658ffb2007-03-16 23:58:11 +00001355 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +00001356
1357 if (watchpoint)
1358 *watchpoint = wp;
1359 return 0;
pbrook6658ffb2007-03-16 23:58:11 +00001360}
1361
aliguoria1d1bb32008-11-18 20:07:32 +00001362/* Remove a specific watchpoint. */
1363int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1364 int flags)
pbrook6658ffb2007-03-16 23:58:11 +00001365{
aliguorib4051332008-11-18 20:14:20 +00001366 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +00001367 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001368
Blue Swirl72cf2d42009-09-12 07:36:22 +00001369 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001370 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +00001371 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +00001372 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +00001373 return 0;
1374 }
1375 }
aliguoria1d1bb32008-11-18 20:07:32 +00001376 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +00001377}
1378
aliguoria1d1bb32008-11-18 20:07:32 +00001379/* Remove a specific watchpoint by reference. */
1380void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1381{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001382 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +00001383
aliguoria1d1bb32008-11-18 20:07:32 +00001384 tlb_flush_page(env, watchpoint->vaddr);
1385
1386 qemu_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +00001387}
1388
aliguoria1d1bb32008-11-18 20:07:32 +00001389/* Remove all matching watchpoints. */
1390void cpu_watchpoint_remove_all(CPUState *env, int mask)
1391{
aliguoric0ce9982008-11-25 22:13:57 +00001392 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001393
Blue Swirl72cf2d42009-09-12 07:36:22 +00001394 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001395 if (wp->flags & mask)
1396 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +00001397 }
aliguoria1d1bb32008-11-18 20:07:32 +00001398}
1399
1400/* Add a breakpoint. */
1401int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1402 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001403{
bellard1fddef42005-04-17 19:16:13 +00001404#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001405 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +00001406
aliguoria1d1bb32008-11-18 20:07:32 +00001407 bp = qemu_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +00001408
1409 bp->pc = pc;
1410 bp->flags = flags;
1411
aliguori2dc9f412008-11-18 20:56:59 +00001412 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001413 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001414 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001415 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001416 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001417
1418 breakpoint_invalidate(env, pc);
1419
1420 if (breakpoint)
1421 *breakpoint = bp;
1422 return 0;
1423#else
1424 return -ENOSYS;
1425#endif
1426}
1427
1428/* Remove a specific breakpoint. */
1429int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1430{
1431#if defined(TARGET_HAS_ICE)
1432 CPUBreakpoint *bp;
1433
Blue Swirl72cf2d42009-09-12 07:36:22 +00001434 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00001435 if (bp->pc == pc && bp->flags == flags) {
1436 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +00001437 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +00001438 }
bellard4c3a88a2003-07-26 12:06:08 +00001439 }
aliguoria1d1bb32008-11-18 20:07:32 +00001440 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +00001441#else
aliguoria1d1bb32008-11-18 20:07:32 +00001442 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +00001443#endif
1444}
1445
aliguoria1d1bb32008-11-18 20:07:32 +00001446/* Remove a specific breakpoint by reference. */
1447void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001448{
bellard1fddef42005-04-17 19:16:13 +00001449#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001450 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +00001451
aliguoria1d1bb32008-11-18 20:07:32 +00001452 breakpoint_invalidate(env, breakpoint->pc);
1453
1454 qemu_free(breakpoint);
1455#endif
1456}
1457
1458/* Remove all matching breakpoints. */
1459void cpu_breakpoint_remove_all(CPUState *env, int mask)
1460{
1461#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001462 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001463
Blue Swirl72cf2d42009-09-12 07:36:22 +00001464 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001465 if (bp->flags & mask)
1466 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +00001467 }
bellard4c3a88a2003-07-26 12:06:08 +00001468#endif
1469}
1470
bellardc33a3462003-07-29 20:50:33 +00001471/* enable or disable single step mode. EXCP_DEBUG is returned by the
1472 CPU loop after each instruction */
1473void cpu_single_step(CPUState *env, int enabled)
1474{
bellard1fddef42005-04-17 19:16:13 +00001475#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001476 if (env->singlestep_enabled != enabled) {
1477 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +00001478 if (kvm_enabled())
1479 kvm_update_guest_debug(env, 0);
1480 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001481 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +00001482 /* XXX: only flush what is necessary */
1483 tb_flush(env);
1484 }
bellardc33a3462003-07-29 20:50:33 +00001485 }
1486#endif
1487}
1488
bellard34865132003-10-05 14:28:56 +00001489/* enable or disable low levels log */
1490void cpu_set_log(int log_flags)
1491{
1492 loglevel = log_flags;
1493 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001494 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001495 if (!logfile) {
1496 perror(logfilename);
1497 _exit(1);
1498 }
bellard9fa3e852004-01-04 18:06:42 +00001499#if !defined(CONFIG_SOFTMMU)
1500 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1501 {
blueswir1b55266b2008-09-20 08:07:15 +00001502 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001503 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1504 }
Filip Navarabf65f532009-07-27 10:02:04 -05001505#elif !defined(_WIN32)
1506 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellard34865132003-10-05 14:28:56 +00001507 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001508#endif
pbrooke735b912007-06-30 13:53:24 +00001509 log_append = 1;
1510 }
1511 if (!loglevel && logfile) {
1512 fclose(logfile);
1513 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001514 }
1515}
1516
1517void cpu_set_log_filename(const char *filename)
1518{
1519 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001520 if (logfile) {
1521 fclose(logfile);
1522 logfile = NULL;
1523 }
1524 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001525}
bellardc33a3462003-07-29 20:50:33 +00001526
aurel323098dba2009-03-07 21:28:24 +00001527static void cpu_unlink_tb(CPUState *env)
bellardea041c02003-06-25 16:16:50 +00001528{
Juan Quintela2f7bb872009-07-27 16:13:24 +02001529#if defined(CONFIG_USE_NPTL)
pbrookd5975362008-06-07 20:50:51 +00001530 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1531 problem and hope the cpu will stop of its own accord. For userspace
1532 emulation this often isn't actually as bad as it sounds. Often
1533 signals are used primarily to interrupt blocking syscalls. */
1534#else
aurel323098dba2009-03-07 21:28:24 +00001535 TranslationBlock *tb;
Anthony Liguoric227f092009-10-01 16:12:16 -05001536 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
aurel323098dba2009-03-07 21:28:24 +00001537
1538 tb = env->current_tb;
1539 /* if the cpu is currently executing code, we must unlink it and
1540 all the potentially executing TB */
1541 if (tb && !testandset(&interrupt_lock)) {
1542 env->current_tb = NULL;
1543 tb_reset_jump_recursive(tb);
1544 resetlock(&interrupt_lock);
1545 }
1546#endif
1547}
1548
1549/* mask must never be zero, except for A20 change call */
1550void cpu_interrupt(CPUState *env, int mask)
1551{
1552 int old_mask;
1553
1554 old_mask = env->interrupt_request;
1555 env->interrupt_request |= mask;
1556
aliguori8edac962009-04-24 18:03:45 +00001557#ifndef CONFIG_USER_ONLY
1558 /*
1559 * If called from iothread context, wake the target cpu in
1560 * case its halted.
1561 */
1562 if (!qemu_cpu_self(env)) {
1563 qemu_cpu_kick(env);
1564 return;
1565 }
1566#endif
1567
pbrook2e70f6e2008-06-29 01:03:05 +00001568 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001569 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001570#ifndef CONFIG_USER_ONLY
pbrook2e70f6e2008-06-29 01:03:05 +00001571 if (!can_do_io(env)
aurel32be214e62009-03-06 21:48:00 +00001572 && (mask & ~old_mask) != 0) {
pbrook2e70f6e2008-06-29 01:03:05 +00001573 cpu_abort(env, "Raised interrupt while not in I/O function");
1574 }
1575#endif
1576 } else {
aurel323098dba2009-03-07 21:28:24 +00001577 cpu_unlink_tb(env);
bellardea041c02003-06-25 16:16:50 +00001578 }
1579}
1580
bellardb54ad042004-05-20 13:42:52 +00001581void cpu_reset_interrupt(CPUState *env, int mask)
1582{
1583 env->interrupt_request &= ~mask;
1584}
1585
aurel323098dba2009-03-07 21:28:24 +00001586void cpu_exit(CPUState *env)
1587{
1588 env->exit_request = 1;
1589 cpu_unlink_tb(env);
1590}
1591
blueswir1c7cd6a32008-10-02 18:27:46 +00001592const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001593 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001594 "show generated host assembly code for each compiled TB" },
1595 { CPU_LOG_TB_IN_ASM, "in_asm",
1596 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001597 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001598 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001599 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001600 "show micro ops "
1601#ifdef TARGET_I386
1602 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001603#endif
blueswir1e01a1152008-03-14 17:37:11 +00001604 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001605 { CPU_LOG_INT, "int",
1606 "show interrupts/exceptions in short format" },
1607 { CPU_LOG_EXEC, "exec",
1608 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001609 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001610 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001611#ifdef TARGET_I386
1612 { CPU_LOG_PCALL, "pcall",
1613 "show protected mode far calls/returns/exceptions" },
aliguorieca1bdf2009-01-26 19:54:31 +00001614 { CPU_LOG_RESET, "cpu_reset",
1615 "show CPU state before CPU resets" },
bellardf193c792004-03-21 17:06:25 +00001616#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001617#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001618 { CPU_LOG_IOPORT, "ioport",
1619 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001620#endif
bellardf193c792004-03-21 17:06:25 +00001621 { 0, NULL, NULL },
1622};
1623
1624static int cmp1(const char *s1, int n, const char *s2)
1625{
1626 if (strlen(s2) != n)
1627 return 0;
1628 return memcmp(s1, s2, n) == 0;
1629}
ths3b46e622007-09-17 08:09:54 +00001630
bellardf193c792004-03-21 17:06:25 +00001631/* takes a comma separated list of log masks. Return 0 if error. */
1632int cpu_str_to_log_mask(const char *str)
1633{
blueswir1c7cd6a32008-10-02 18:27:46 +00001634 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001635 int mask;
1636 const char *p, *p1;
1637
1638 p = str;
1639 mask = 0;
1640 for(;;) {
1641 p1 = strchr(p, ',');
1642 if (!p1)
1643 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001644 if(cmp1(p,p1-p,"all")) {
1645 for(item = cpu_log_items; item->mask != 0; item++) {
1646 mask |= item->mask;
1647 }
1648 } else {
bellardf193c792004-03-21 17:06:25 +00001649 for(item = cpu_log_items; item->mask != 0; item++) {
1650 if (cmp1(p, p1 - p, item->name))
1651 goto found;
1652 }
1653 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001654 }
bellardf193c792004-03-21 17:06:25 +00001655 found:
1656 mask |= item->mask;
1657 if (*p1 != ',')
1658 break;
1659 p = p1 + 1;
1660 }
1661 return mask;
1662}
bellardea041c02003-06-25 16:16:50 +00001663
bellard75012672003-06-21 13:11:07 +00001664void cpu_abort(CPUState *env, const char *fmt, ...)
1665{
1666 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001667 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001668
1669 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001670 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001671 fprintf(stderr, "qemu: fatal: ");
1672 vfprintf(stderr, fmt, ap);
1673 fprintf(stderr, "\n");
1674#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001675 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1676#else
1677 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001678#endif
aliguori93fcfe32009-01-15 22:34:14 +00001679 if (qemu_log_enabled()) {
1680 qemu_log("qemu: fatal: ");
1681 qemu_log_vprintf(fmt, ap2);
1682 qemu_log("\n");
j_mayerf9373292007-09-29 12:18:20 +00001683#ifdef TARGET_I386
aliguori93fcfe32009-01-15 22:34:14 +00001684 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
j_mayerf9373292007-09-29 12:18:20 +00001685#else
aliguori93fcfe32009-01-15 22:34:14 +00001686 log_cpu_state(env, 0);
j_mayerf9373292007-09-29 12:18:20 +00001687#endif
aliguori31b1a7b2009-01-15 22:35:09 +00001688 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +00001689 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +00001690 }
pbrook493ae1f2007-11-23 16:53:59 +00001691 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001692 va_end(ap);
bellard75012672003-06-21 13:11:07 +00001693 abort();
1694}
1695
thsc5be9f02007-02-28 20:20:53 +00001696CPUState *cpu_copy(CPUState *env)
1697{
ths01ba9812007-12-09 02:22:57 +00001698 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001699 CPUState *next_cpu = new_env->next_cpu;
1700 int cpu_index = new_env->cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001701#if defined(TARGET_HAS_ICE)
1702 CPUBreakpoint *bp;
1703 CPUWatchpoint *wp;
1704#endif
1705
thsc5be9f02007-02-28 20:20:53 +00001706 memcpy(new_env, env, sizeof(CPUState));
aliguori5a38f082009-01-15 20:16:51 +00001707
1708 /* Preserve chaining and index. */
thsc5be9f02007-02-28 20:20:53 +00001709 new_env->next_cpu = next_cpu;
1710 new_env->cpu_index = cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001711
1712 /* Clone all break/watchpoints.
1713 Note: Once we support ptrace with hw-debug register access, make sure
1714 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001715 QTAILQ_INIT(&env->breakpoints);
1716 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +00001717#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001718 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001719 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1720 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001721 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001722 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1723 wp->flags, NULL);
1724 }
1725#endif
1726
thsc5be9f02007-02-28 20:20:53 +00001727 return new_env;
1728}
1729
bellard01243112004-01-04 15:48:17 +00001730#if !defined(CONFIG_USER_ONLY)
1731
edgar_igl5c751e92008-05-06 08:44:21 +00001732static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1733{
1734 unsigned int i;
1735
1736 /* Discard jump cache entries for any tb which might potentially
1737 overlap the flushed page. */
1738 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1739 memset (&env->tb_jmp_cache[i], 0,
1740 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1741
1742 i = tb_jmp_cache_hash_page(addr);
1743 memset (&env->tb_jmp_cache[i], 0,
1744 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1745}
1746
Igor Kovalenko08738982009-07-12 02:15:40 +04001747static CPUTLBEntry s_cputlb_empty_entry = {
1748 .addr_read = -1,
1749 .addr_write = -1,
1750 .addr_code = -1,
1751 .addend = -1,
1752};
1753
bellardee8b7022004-02-03 23:35:10 +00001754/* NOTE: if flush_global is true, also flush global entries (not
1755 implemented yet) */
1756void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001757{
bellard33417e72003-08-10 21:47:01 +00001758 int i;
bellard01243112004-01-04 15:48:17 +00001759
bellard9fa3e852004-01-04 18:06:42 +00001760#if defined(DEBUG_TLB)
1761 printf("tlb_flush:\n");
1762#endif
bellard01243112004-01-04 15:48:17 +00001763 /* must reset current TB so that interrupts cannot modify the
1764 links while we are modifying them */
1765 env->current_tb = NULL;
1766
bellard33417e72003-08-10 21:47:01 +00001767 for(i = 0; i < CPU_TLB_SIZE; i++) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001768 int mmu_idx;
1769 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001770 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001771 }
bellard33417e72003-08-10 21:47:01 +00001772 }
bellard9fa3e852004-01-04 18:06:42 +00001773
bellard8a40a182005-11-20 10:35:40 +00001774 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001775
bellarde3db7222005-01-26 22:00:47 +00001776 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001777}
1778
bellard274da6b2004-05-20 21:56:27 +00001779static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001780{
ths5fafdf22007-09-16 21:08:06 +00001781 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001782 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001783 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001784 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001785 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001786 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001787 *tlb_entry = s_cputlb_empty_entry;
bellard84b7b8e2005-11-28 21:19:04 +00001788 }
bellard61382a52003-10-27 21:22:23 +00001789}
1790
bellard2e126692004-04-25 21:28:44 +00001791void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001792{
bellard8a40a182005-11-20 10:35:40 +00001793 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001794 int mmu_idx;
bellard01243112004-01-04 15:48:17 +00001795
bellard9fa3e852004-01-04 18:06:42 +00001796#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001797 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001798#endif
bellard01243112004-01-04 15:48:17 +00001799 /* must reset current TB so that interrupts cannot modify the
1800 links while we are modifying them */
1801 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001802
bellard61382a52003-10-27 21:22:23 +00001803 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001804 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001805 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1806 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
bellard01243112004-01-04 15:48:17 +00001807
edgar_igl5c751e92008-05-06 08:44:21 +00001808 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001809}
1810
bellard9fa3e852004-01-04 18:06:42 +00001811/* update the TLBs so that writes to code in the virtual page 'addr'
1812 can be detected */
Anthony Liguoric227f092009-10-01 16:12:16 -05001813static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001814{
ths5fafdf22007-09-16 21:08:06 +00001815 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001816 ram_addr + TARGET_PAGE_SIZE,
1817 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001818}
1819
bellard9fa3e852004-01-04 18:06:42 +00001820/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001821 tested for self modifying code */
Anthony Liguoric227f092009-10-01 16:12:16 -05001822static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001823 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001824{
bellard3a7d9292005-08-21 09:26:42 +00001825 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001826}
1827
ths5fafdf22007-09-16 21:08:06 +00001828static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001829 unsigned long start, unsigned long length)
1830{
1831 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001832 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1833 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001834 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00001835 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001836 }
1837 }
1838}
1839
pbrook5579c7f2009-04-11 14:47:08 +00001840/* Note: start and end must be within the same ram block. */
Anthony Liguoric227f092009-10-01 16:12:16 -05001841void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001842 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001843{
1844 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001845 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001846 int i, mask, len;
1847 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001848
1849 start &= TARGET_PAGE_MASK;
1850 end = TARGET_PAGE_ALIGN(end);
1851
1852 length = end - start;
1853 if (length == 0)
1854 return;
bellard0a962c02005-02-10 22:00:27 +00001855 len = length >> TARGET_PAGE_BITS;
bellardf23db162005-08-21 19:12:28 +00001856 mask = ~dirty_flags;
1857 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1858 for(i = 0; i < len; i++)
1859 p[i] &= mask;
1860
bellard1ccde1c2004-02-06 19:46:14 +00001861 /* we modify the TLB cache so that the dirty bit will be set again
1862 when accessing the range */
pbrook5579c7f2009-04-11 14:47:08 +00001863 start1 = (unsigned long)qemu_get_ram_ptr(start);
1864 /* Chek that we don't span multiple blocks - this breaks the
1865 address comparisons below. */
1866 if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1
1867 != (end - 1) - start) {
1868 abort();
1869 }
1870
bellard6a00d602005-11-21 23:25:50 +00001871 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001872 int mmu_idx;
1873 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1874 for(i = 0; i < CPU_TLB_SIZE; i++)
1875 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
1876 start1, length);
1877 }
bellard6a00d602005-11-21 23:25:50 +00001878 }
bellard1ccde1c2004-02-06 19:46:14 +00001879}
1880
aliguori74576192008-10-06 14:02:03 +00001881int cpu_physical_memory_set_dirty_tracking(int enable)
1882{
1883 in_migration = enable;
Jan Kiszkab0a46a32009-05-02 00:22:51 +02001884 if (kvm_enabled()) {
1885 return kvm_set_migration_log(enable);
1886 }
aliguori74576192008-10-06 14:02:03 +00001887 return 0;
1888}
1889
1890int cpu_physical_memory_get_dirty_tracking(void)
1891{
1892 return in_migration;
1893}
1894
Anthony Liguoric227f092009-10-01 16:12:16 -05001895int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1896 target_phys_addr_t end_addr)
aliguori2bec46d2008-11-24 20:21:41 +00001897{
Jan Kiszka151f7742009-05-01 20:52:47 +02001898 int ret = 0;
1899
aliguori2bec46d2008-11-24 20:21:41 +00001900 if (kvm_enabled())
Jan Kiszka151f7742009-05-01 20:52:47 +02001901 ret = kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
1902 return ret;
aliguori2bec46d2008-11-24 20:21:41 +00001903}
1904
bellard3a7d9292005-08-21 09:26:42 +00001905static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1906{
Anthony Liguoric227f092009-10-01 16:12:16 -05001907 ram_addr_t ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00001908 void *p;
bellard3a7d9292005-08-21 09:26:42 +00001909
bellard84b7b8e2005-11-28 21:19:04 +00001910 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
pbrook5579c7f2009-04-11 14:47:08 +00001911 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
1912 + tlb_entry->addend);
1913 ram_addr = qemu_ram_addr_from_host(p);
bellard3a7d9292005-08-21 09:26:42 +00001914 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00001915 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00001916 }
1917 }
1918}
1919
1920/* update the TLB according to the current state of the dirty bits */
1921void cpu_tlb_update_dirty(CPUState *env)
1922{
1923 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001924 int mmu_idx;
1925 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1926 for(i = 0; i < CPU_TLB_SIZE; i++)
1927 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
1928 }
bellard3a7d9292005-08-21 09:26:42 +00001929}
1930
pbrook0f459d12008-06-09 00:20:13 +00001931static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001932{
pbrook0f459d12008-06-09 00:20:13 +00001933 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1934 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00001935}
1936
pbrook0f459d12008-06-09 00:20:13 +00001937/* update the TLB corresponding to virtual page vaddr
1938 so that it is no longer dirty */
1939static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00001940{
bellard1ccde1c2004-02-06 19:46:14 +00001941 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001942 int mmu_idx;
bellard1ccde1c2004-02-06 19:46:14 +00001943
pbrook0f459d12008-06-09 00:20:13 +00001944 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00001945 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001946 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1947 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
bellard9fa3e852004-01-04 18:06:42 +00001948}
1949
bellard59817cc2004-02-16 22:01:13 +00001950/* add a new TLB entry. At most one entry for a given virtual address
1951 is permitted. Return 0 if OK or 2 if the page could not be mapped
1952 (can only happen in non SOFTMMU mode for I/O pages or pages
1953 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00001954int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
Anthony Liguoric227f092009-10-01 16:12:16 -05001955 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00001956 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00001957{
bellard92e873b2004-05-21 14:52:29 +00001958 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00001959 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00001960 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00001961 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00001962 target_ulong code_address;
Anthony Liguoric227f092009-10-01 16:12:16 -05001963 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00001964 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00001965 CPUTLBEntry *te;
aliguoria1d1bb32008-11-18 20:07:32 +00001966 CPUWatchpoint *wp;
Anthony Liguoric227f092009-10-01 16:12:16 -05001967 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00001968
bellard92e873b2004-05-21 14:52:29 +00001969 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00001970 if (!p) {
1971 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00001972 } else {
1973 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00001974 }
1975#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00001976 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1977 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00001978#endif
1979
1980 ret = 0;
pbrook0f459d12008-06-09 00:20:13 +00001981 address = vaddr;
1982 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
1983 /* IO memory case (romd handled later) */
1984 address |= TLB_MMIO;
1985 }
pbrook5579c7f2009-04-11 14:47:08 +00001986 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
pbrook0f459d12008-06-09 00:20:13 +00001987 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
1988 /* Normal RAM. */
1989 iotlb = pd & TARGET_PAGE_MASK;
1990 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
1991 iotlb |= IO_MEM_NOTDIRTY;
1992 else
1993 iotlb |= IO_MEM_ROM;
1994 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001995 /* IO handlers are currently passed a physical address.
pbrook0f459d12008-06-09 00:20:13 +00001996 It would be nice to pass an offset from the base address
1997 of that region. This would avoid having to special case RAM,
1998 and avoid full address decoding in every device.
1999 We can't use the high bits of pd for this because
2000 IO_MEM_ROMD uses these as a ram address. */
pbrook8da3ff12008-12-01 18:59:50 +00002001 iotlb = (pd & ~TARGET_PAGE_MASK);
2002 if (p) {
pbrook8da3ff12008-12-01 18:59:50 +00002003 iotlb += p->region_offset;
2004 } else {
2005 iotlb += paddr;
2006 }
pbrook0f459d12008-06-09 00:20:13 +00002007 }
pbrook6658ffb2007-03-16 23:58:11 +00002008
pbrook0f459d12008-06-09 00:20:13 +00002009 code_address = address;
2010 /* Make accesses to pages with watchpoints go via the
2011 watchpoint trap routines. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002012 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00002013 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
pbrook0f459d12008-06-09 00:20:13 +00002014 iotlb = io_mem_watch + paddr;
2015 /* TODO: The memory case can be optimized by not trapping
2016 reads of pages with a write breakpoint. */
2017 address |= TLB_MMIO;
pbrook6658ffb2007-03-16 23:58:11 +00002018 }
pbrook0f459d12008-06-09 00:20:13 +00002019 }
balrogd79acba2007-06-26 20:01:13 +00002020
pbrook0f459d12008-06-09 00:20:13 +00002021 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2022 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2023 te = &env->tlb_table[mmu_idx][index];
2024 te->addend = addend - vaddr;
2025 if (prot & PAGE_READ) {
2026 te->addr_read = address;
2027 } else {
2028 te->addr_read = -1;
2029 }
edgar_igl5c751e92008-05-06 08:44:21 +00002030
pbrook0f459d12008-06-09 00:20:13 +00002031 if (prot & PAGE_EXEC) {
2032 te->addr_code = code_address;
2033 } else {
2034 te->addr_code = -1;
2035 }
2036 if (prot & PAGE_WRITE) {
2037 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2038 (pd & IO_MEM_ROMD)) {
2039 /* Write access calls the I/O callback. */
2040 te->addr_write = address | TLB_MMIO;
2041 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2042 !cpu_physical_memory_is_dirty(pd)) {
2043 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00002044 } else {
pbrook0f459d12008-06-09 00:20:13 +00002045 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00002046 }
pbrook0f459d12008-06-09 00:20:13 +00002047 } else {
2048 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00002049 }
bellard9fa3e852004-01-04 18:06:42 +00002050 return ret;
2051}
2052
bellard01243112004-01-04 15:48:17 +00002053#else
2054
bellardee8b7022004-02-03 23:35:10 +00002055void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00002056{
2057}
2058
bellard2e126692004-04-25 21:28:44 +00002059void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00002060{
2061}
2062
ths5fafdf22007-09-16 21:08:06 +00002063int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
Anthony Liguoric227f092009-10-01 16:12:16 -05002064 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00002065 int mmu_idx, int is_softmmu)
bellard33417e72003-08-10 21:47:01 +00002066{
bellard9fa3e852004-01-04 18:06:42 +00002067 return 0;
2068}
bellard33417e72003-08-10 21:47:01 +00002069
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002070/*
2071 * Walks guest process memory "regions" one by one
2072 * and calls callback function 'fn' for each region.
2073 */
2074int walk_memory_regions(void *priv,
2075 int (*fn)(void *, unsigned long, unsigned long, unsigned long))
bellard9fa3e852004-01-04 18:06:42 +00002076{
2077 unsigned long start, end;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002078 PageDesc *p = NULL;
bellard9fa3e852004-01-04 18:06:42 +00002079 int i, j, prot, prot1;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002080 int rc = 0;
bellard9fa3e852004-01-04 18:06:42 +00002081
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002082 start = end = -1;
bellard9fa3e852004-01-04 18:06:42 +00002083 prot = 0;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002084
2085 for (i = 0; i <= L1_SIZE; i++) {
2086 p = (i < L1_SIZE) ? l1_map[i] : NULL;
2087 for (j = 0; j < L2_SIZE; j++) {
2088 prot1 = (p == NULL) ? 0 : p[j].flags;
2089 /*
2090 * "region" is one continuous chunk of memory
2091 * that has same protection flags set.
2092 */
bellard9fa3e852004-01-04 18:06:42 +00002093 if (prot1 != prot) {
2094 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
2095 if (start != -1) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002096 rc = (*fn)(priv, start, end, prot);
2097 /* callback can stop iteration by returning != 0 */
2098 if (rc != 0)
2099 return (rc);
bellard9fa3e852004-01-04 18:06:42 +00002100 }
2101 if (prot1 != 0)
2102 start = end;
2103 else
2104 start = -1;
2105 prot = prot1;
2106 }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002107 if (p == NULL)
bellard9fa3e852004-01-04 18:06:42 +00002108 break;
2109 }
bellard33417e72003-08-10 21:47:01 +00002110 }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002111 return (rc);
2112}
2113
2114static int dump_region(void *priv, unsigned long start,
2115 unsigned long end, unsigned long prot)
2116{
2117 FILE *f = (FILE *)priv;
2118
2119 (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
2120 start, end, end - start,
2121 ((prot & PAGE_READ) ? 'r' : '-'),
2122 ((prot & PAGE_WRITE) ? 'w' : '-'),
2123 ((prot & PAGE_EXEC) ? 'x' : '-'));
2124
2125 return (0);
2126}
2127
2128/* dump memory mappings */
2129void page_dump(FILE *f)
2130{
2131 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2132 "start", "end", "size", "prot");
2133 walk_memory_regions(f, dump_region);
bellard33417e72003-08-10 21:47:01 +00002134}
2135
pbrook53a59602006-03-25 19:31:22 +00002136int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002137{
bellard9fa3e852004-01-04 18:06:42 +00002138 PageDesc *p;
2139
2140 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002141 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002142 return 0;
2143 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002144}
2145
bellard9fa3e852004-01-04 18:06:42 +00002146/* modify the flags of a page and invalidate the code if
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002147 necessary. The flag PAGE_WRITE_ORG is positioned automatically
bellard9fa3e852004-01-04 18:06:42 +00002148 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00002149void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002150{
2151 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002152 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00002153
pbrookc8a706f2008-06-02 16:16:42 +00002154 /* mmap_lock should already be held. */
bellard9fa3e852004-01-04 18:06:42 +00002155 start = start & TARGET_PAGE_MASK;
2156 end = TARGET_PAGE_ALIGN(end);
2157 if (flags & PAGE_WRITE)
2158 flags |= PAGE_WRITE_ORG;
bellard9fa3e852004-01-04 18:06:42 +00002159 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2160 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
pbrook17e23772008-06-09 13:47:45 +00002161 /* We may be called for host regions that are outside guest
2162 address space. */
2163 if (!p)
2164 return;
bellard9fa3e852004-01-04 18:06:42 +00002165 /* if the write protection is set, then we invalidate the code
2166 inside */
ths5fafdf22007-09-16 21:08:06 +00002167 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002168 (flags & PAGE_WRITE) &&
2169 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002170 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002171 }
2172 p->flags = flags;
2173 }
bellard9fa3e852004-01-04 18:06:42 +00002174}
2175
ths3d97b402007-11-02 19:02:07 +00002176int page_check_range(target_ulong start, target_ulong len, int flags)
2177{
2178 PageDesc *p;
2179 target_ulong end;
2180 target_ulong addr;
2181
balrog55f280c2008-10-28 10:24:11 +00002182 if (start + len < start)
2183 /* we've wrapped around */
2184 return -1;
2185
ths3d97b402007-11-02 19:02:07 +00002186 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2187 start = start & TARGET_PAGE_MASK;
2188
ths3d97b402007-11-02 19:02:07 +00002189 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2190 p = page_find(addr >> TARGET_PAGE_BITS);
2191 if( !p )
2192 return -1;
2193 if( !(p->flags & PAGE_VALID) )
2194 return -1;
2195
bellarddae32702007-11-14 10:51:00 +00002196 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002197 return -1;
bellarddae32702007-11-14 10:51:00 +00002198 if (flags & PAGE_WRITE) {
2199 if (!(p->flags & PAGE_WRITE_ORG))
2200 return -1;
2201 /* unprotect the page if it was put read-only because it
2202 contains translated code */
2203 if (!(p->flags & PAGE_WRITE)) {
2204 if (!page_unprotect(addr, 0, NULL))
2205 return -1;
2206 }
2207 return 0;
2208 }
ths3d97b402007-11-02 19:02:07 +00002209 }
2210 return 0;
2211}
2212
bellard9fa3e852004-01-04 18:06:42 +00002213/* called from signal handler: invalidate the code and unprotect the
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002214 page. Return TRUE if the fault was successfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002215int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002216{
2217 unsigned int page_index, prot, pindex;
2218 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002219 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002220
pbrookc8a706f2008-06-02 16:16:42 +00002221 /* Technically this isn't safe inside a signal handler. However we
2222 know this only ever happens in a synchronous SEGV handler, so in
2223 practice it seems to be ok. */
2224 mmap_lock();
2225
bellard83fb7ad2004-07-05 21:25:26 +00002226 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002227 page_index = host_start >> TARGET_PAGE_BITS;
2228 p1 = page_find(page_index);
pbrookc8a706f2008-06-02 16:16:42 +00002229 if (!p1) {
2230 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002231 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002232 }
bellard83fb7ad2004-07-05 21:25:26 +00002233 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002234 p = p1;
2235 prot = 0;
2236 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2237 prot |= p->flags;
2238 p++;
2239 }
2240 /* if the page was really writable, then we change its
2241 protection back to writable */
2242 if (prot & PAGE_WRITE_ORG) {
2243 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2244 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002245 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002246 (prot & PAGE_BITS) | PAGE_WRITE);
2247 p1[pindex].flags |= PAGE_WRITE;
2248 /* and since the content will be modified, we must invalidate
2249 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002250 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002251#ifdef DEBUG_TB_CHECK
2252 tb_invalidate_check(address);
2253#endif
pbrookc8a706f2008-06-02 16:16:42 +00002254 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002255 return 1;
2256 }
2257 }
pbrookc8a706f2008-06-02 16:16:42 +00002258 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002259 return 0;
2260}
2261
bellard6a00d602005-11-21 23:25:50 +00002262static inline void tlb_set_dirty(CPUState *env,
2263 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002264{
2265}
bellard9fa3e852004-01-04 18:06:42 +00002266#endif /* defined(CONFIG_USER_ONLY) */
2267
pbrooke2eef172008-06-08 01:09:01 +00002268#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +00002269
Anthony Liguoric227f092009-10-01 16:12:16 -05002270static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2271 ram_addr_t memory, ram_addr_t region_offset);
2272static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2273 ram_addr_t orig_memory, ram_addr_t region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002274#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2275 need_subpage) \
2276 do { \
2277 if (addr > start_addr) \
2278 start_addr2 = 0; \
2279 else { \
2280 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2281 if (start_addr2 > 0) \
2282 need_subpage = 1; \
2283 } \
2284 \
blueswir149e9fba2007-05-30 17:25:06 +00002285 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002286 end_addr2 = TARGET_PAGE_SIZE - 1; \
2287 else { \
2288 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2289 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2290 need_subpage = 1; \
2291 } \
2292 } while (0)
2293
Michael S. Tsirkin8f2498f2009-09-29 18:53:16 +02002294/* register physical memory.
2295 For RAM, 'size' must be a multiple of the target page size.
2296 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
pbrook8da3ff12008-12-01 18:59:50 +00002297 io memory page. The address used when calling the IO function is
2298 the offset from the start of the region, plus region_offset. Both
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002299 start_addr and region_offset are rounded down to a page boundary
pbrook8da3ff12008-12-01 18:59:50 +00002300 before calculating this offset. This should not be a problem unless
2301 the low bits of start_addr and region_offset differ. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002302void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2303 ram_addr_t size,
2304 ram_addr_t phys_offset,
2305 ram_addr_t region_offset)
bellard33417e72003-08-10 21:47:01 +00002306{
Anthony Liguoric227f092009-10-01 16:12:16 -05002307 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002308 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002309 CPUState *env;
Anthony Liguoric227f092009-10-01 16:12:16 -05002310 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002311 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002312
aliguori7ba1e612008-11-05 16:04:33 +00002313 if (kvm_enabled())
2314 kvm_set_phys_mem(start_addr, size, phys_offset);
2315
pbrook67c4d232009-02-23 13:16:07 +00002316 if (phys_offset == IO_MEM_UNASSIGNED) {
2317 region_offset = start_addr;
2318 }
pbrook8da3ff12008-12-01 18:59:50 +00002319 region_offset &= TARGET_PAGE_MASK;
bellard5fd386f2004-05-23 21:11:22 +00002320 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
Anthony Liguoric227f092009-10-01 16:12:16 -05002321 end_addr = start_addr + (target_phys_addr_t)size;
blueswir149e9fba2007-05-30 17:25:06 +00002322 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002323 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2324 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002325 ram_addr_t orig_memory = p->phys_offset;
2326 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002327 int need_subpage = 0;
2328
2329 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2330 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002331 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002332 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2333 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002334 &p->phys_offset, orig_memory,
2335 p->region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002336 } else {
2337 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2338 >> IO_MEM_SHIFT];
2339 }
pbrook8da3ff12008-12-01 18:59:50 +00002340 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2341 region_offset);
2342 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002343 } else {
2344 p->phys_offset = phys_offset;
2345 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2346 (phys_offset & IO_MEM_ROMD))
2347 phys_offset += TARGET_PAGE_SIZE;
2348 }
2349 } else {
2350 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2351 p->phys_offset = phys_offset;
pbrook8da3ff12008-12-01 18:59:50 +00002352 p->region_offset = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002353 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
pbrook8da3ff12008-12-01 18:59:50 +00002354 (phys_offset & IO_MEM_ROMD)) {
blueswir1db7b5422007-05-26 17:36:03 +00002355 phys_offset += TARGET_PAGE_SIZE;
pbrook0e8f0962008-12-02 09:02:15 +00002356 } else {
Anthony Liguoric227f092009-10-01 16:12:16 -05002357 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002358 int need_subpage = 0;
2359
2360 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2361 end_addr2, need_subpage);
2362
blueswir14254fab2008-01-01 16:57:19 +00002363 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002364 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002365 &p->phys_offset, IO_MEM_UNASSIGNED,
pbrook67c4d232009-02-23 13:16:07 +00002366 addr & TARGET_PAGE_MASK);
blueswir1db7b5422007-05-26 17:36:03 +00002367 subpage_register(subpage, start_addr2, end_addr2,
pbrook8da3ff12008-12-01 18:59:50 +00002368 phys_offset, region_offset);
2369 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002370 }
2371 }
2372 }
pbrook8da3ff12008-12-01 18:59:50 +00002373 region_offset += TARGET_PAGE_SIZE;
bellard33417e72003-08-10 21:47:01 +00002374 }
ths3b46e622007-09-17 08:09:54 +00002375
bellard9d420372006-06-25 22:25:22 +00002376 /* since each CPU stores ram addresses in its TLB cache, we must
2377 reset the modified entries */
2378 /* XXX: slow ! */
2379 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2380 tlb_flush(env, 1);
2381 }
bellard33417e72003-08-10 21:47:01 +00002382}
2383
bellardba863452006-09-24 18:41:10 +00002384/* XXX: temporary until new memory mapping API */
Anthony Liguoric227f092009-10-01 16:12:16 -05002385ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002386{
2387 PhysPageDesc *p;
2388
2389 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2390 if (!p)
2391 return IO_MEM_UNASSIGNED;
2392 return p->phys_offset;
2393}
2394
Anthony Liguoric227f092009-10-01 16:12:16 -05002395void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002396{
2397 if (kvm_enabled())
2398 kvm_coalesce_mmio_region(addr, size);
2399}
2400
Anthony Liguoric227f092009-10-01 16:12:16 -05002401void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002402{
2403 if (kvm_enabled())
2404 kvm_uncoalesce_mmio_region(addr, size);
2405}
2406
Anthony Liguoric227f092009-10-01 16:12:16 -05002407ram_addr_t qemu_ram_alloc(ram_addr_t size)
pbrook94a6b542009-04-11 17:15:54 +00002408{
2409 RAMBlock *new_block;
2410
pbrook94a6b542009-04-11 17:15:54 +00002411 size = TARGET_PAGE_ALIGN(size);
2412 new_block = qemu_malloc(sizeof(*new_block));
2413
Alexander Graf6b024942009-12-05 12:44:25 +01002414#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2415 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2416 new_block->host = mmap((void*)0x1000000, size, PROT_EXEC|PROT_READ|PROT_WRITE,
2417 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
2418#else
pbrook94a6b542009-04-11 17:15:54 +00002419 new_block->host = qemu_vmalloc(size);
Alexander Graf6b024942009-12-05 12:44:25 +01002420#endif
Izik Eidusccb167e2009-10-08 16:39:39 +02002421#ifdef MADV_MERGEABLE
2422 madvise(new_block->host, size, MADV_MERGEABLE);
2423#endif
pbrook94a6b542009-04-11 17:15:54 +00002424 new_block->offset = last_ram_offset;
2425 new_block->length = size;
2426
2427 new_block->next = ram_blocks;
2428 ram_blocks = new_block;
2429
2430 phys_ram_dirty = qemu_realloc(phys_ram_dirty,
2431 (last_ram_offset + size) >> TARGET_PAGE_BITS);
2432 memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
2433 0xff, size >> TARGET_PAGE_BITS);
2434
2435 last_ram_offset += size;
2436
Jan Kiszka6f0437e2009-04-26 18:03:40 +02002437 if (kvm_enabled())
2438 kvm_setup_guest_memory(new_block->host, size);
2439
pbrook94a6b542009-04-11 17:15:54 +00002440 return new_block->offset;
2441}
bellarde9a1ab12007-02-08 23:08:38 +00002442
Anthony Liguoric227f092009-10-01 16:12:16 -05002443void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00002444{
pbrook94a6b542009-04-11 17:15:54 +00002445 /* TODO: implement this. */
bellarde9a1ab12007-02-08 23:08:38 +00002446}
2447
pbrookdc828ca2009-04-09 22:21:07 +00002448/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00002449 With the exception of the softmmu code in this file, this should
2450 only be used for local memory (e.g. video ram) that the device owns,
2451 and knows it isn't going to access beyond the end of the block.
2452
2453 It should not be used for general purpose DMA.
2454 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2455 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002456void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00002457{
pbrook94a6b542009-04-11 17:15:54 +00002458 RAMBlock *prev;
2459 RAMBlock **prevp;
2460 RAMBlock *block;
2461
pbrook94a6b542009-04-11 17:15:54 +00002462 prev = NULL;
2463 prevp = &ram_blocks;
2464 block = ram_blocks;
2465 while (block && (block->offset > addr
2466 || block->offset + block->length <= addr)) {
2467 if (prev)
2468 prevp = &prev->next;
2469 prev = block;
2470 block = block->next;
2471 }
2472 if (!block) {
2473 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2474 abort();
2475 }
2476 /* Move this entry to to start of the list. */
2477 if (prev) {
2478 prev->next = block->next;
2479 block->next = *prevp;
2480 *prevp = block;
2481 }
2482 return block->host + (addr - block->offset);
pbrookdc828ca2009-04-09 22:21:07 +00002483}
2484
pbrook5579c7f2009-04-11 14:47:08 +00002485/* Some of the softmmu routines need to translate from a host pointer
2486 (typically a TLB entry) back to a ram offset. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002487ram_addr_t qemu_ram_addr_from_host(void *ptr)
pbrook5579c7f2009-04-11 14:47:08 +00002488{
pbrook94a6b542009-04-11 17:15:54 +00002489 RAMBlock *prev;
2490 RAMBlock **prevp;
2491 RAMBlock *block;
2492 uint8_t *host = ptr;
2493
pbrook94a6b542009-04-11 17:15:54 +00002494 prev = NULL;
2495 prevp = &ram_blocks;
2496 block = ram_blocks;
2497 while (block && (block->host > host
2498 || block->host + block->length <= host)) {
2499 if (prev)
2500 prevp = &prev->next;
2501 prev = block;
2502 block = block->next;
2503 }
2504 if (!block) {
2505 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2506 abort();
2507 }
2508 return block->offset + (host - block->host);
pbrook5579c7f2009-04-11 14:47:08 +00002509}
2510
Anthony Liguoric227f092009-10-01 16:12:16 -05002511static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002512{
pbrook67d3b952006-12-18 05:03:52 +00002513#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002514 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002515#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002516#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002517 do_unassigned_access(addr, 0, 0, 0, 1);
2518#endif
2519 return 0;
2520}
2521
Anthony Liguoric227f092009-10-01 16:12:16 -05002522static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002523{
2524#ifdef DEBUG_UNASSIGNED
2525 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2526#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002527#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002528 do_unassigned_access(addr, 0, 0, 0, 2);
2529#endif
2530 return 0;
2531}
2532
Anthony Liguoric227f092009-10-01 16:12:16 -05002533static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002534{
2535#ifdef DEBUG_UNASSIGNED
2536 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2537#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002538#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002539 do_unassigned_access(addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002540#endif
bellard33417e72003-08-10 21:47:01 +00002541 return 0;
2542}
2543
Anthony Liguoric227f092009-10-01 16:12:16 -05002544static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002545{
pbrook67d3b952006-12-18 05:03:52 +00002546#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002547 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002548#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002549#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002550 do_unassigned_access(addr, 1, 0, 0, 1);
2551#endif
2552}
2553
Anthony Liguoric227f092009-10-01 16:12:16 -05002554static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00002555{
2556#ifdef DEBUG_UNASSIGNED
2557 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2558#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002559#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002560 do_unassigned_access(addr, 1, 0, 0, 2);
2561#endif
2562}
2563
Anthony Liguoric227f092009-10-01 16:12:16 -05002564static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00002565{
2566#ifdef DEBUG_UNASSIGNED
2567 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2568#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002569#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002570 do_unassigned_access(addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002571#endif
bellard33417e72003-08-10 21:47:01 +00002572}
2573
Blue Swirld60efc62009-08-25 18:29:31 +00002574static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
bellard33417e72003-08-10 21:47:01 +00002575 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00002576 unassigned_mem_readw,
2577 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00002578};
2579
Blue Swirld60efc62009-08-25 18:29:31 +00002580static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
bellard33417e72003-08-10 21:47:01 +00002581 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00002582 unassigned_mem_writew,
2583 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00002584};
2585
Anthony Liguoric227f092009-10-01 16:12:16 -05002586static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002587 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002588{
bellard3a7d9292005-08-21 09:26:42 +00002589 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002590 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2591 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2592#if !defined(CONFIG_USER_ONLY)
2593 tb_invalidate_phys_page_fast(ram_addr, 1);
2594 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2595#endif
2596 }
pbrook5579c7f2009-04-11 14:47:08 +00002597 stb_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002598 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2599 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2600 /* we remove the notdirty callback only if the code has been
2601 flushed */
2602 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002603 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002604}
2605
Anthony Liguoric227f092009-10-01 16:12:16 -05002606static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002607 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002608{
bellard3a7d9292005-08-21 09:26:42 +00002609 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002610 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2611 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2612#if !defined(CONFIG_USER_ONLY)
2613 tb_invalidate_phys_page_fast(ram_addr, 2);
2614 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2615#endif
2616 }
pbrook5579c7f2009-04-11 14:47:08 +00002617 stw_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002618 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2619 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2620 /* we remove the notdirty callback only if the code has been
2621 flushed */
2622 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002623 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002624}
2625
Anthony Liguoric227f092009-10-01 16:12:16 -05002626static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002627 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002628{
bellard3a7d9292005-08-21 09:26:42 +00002629 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002630 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2631 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2632#if !defined(CONFIG_USER_ONLY)
2633 tb_invalidate_phys_page_fast(ram_addr, 4);
2634 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2635#endif
2636 }
pbrook5579c7f2009-04-11 14:47:08 +00002637 stl_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002638 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2639 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2640 /* we remove the notdirty callback only if the code has been
2641 flushed */
2642 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002643 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002644}
2645
Blue Swirld60efc62009-08-25 18:29:31 +00002646static CPUReadMemoryFunc * const error_mem_read[3] = {
bellard3a7d9292005-08-21 09:26:42 +00002647 NULL, /* never used */
2648 NULL, /* never used */
2649 NULL, /* never used */
2650};
2651
Blue Swirld60efc62009-08-25 18:29:31 +00002652static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
bellard1ccde1c2004-02-06 19:46:14 +00002653 notdirty_mem_writeb,
2654 notdirty_mem_writew,
2655 notdirty_mem_writel,
2656};
2657
pbrook0f459d12008-06-09 00:20:13 +00002658/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00002659static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00002660{
2661 CPUState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00002662 target_ulong pc, cs_base;
2663 TranslationBlock *tb;
pbrook0f459d12008-06-09 00:20:13 +00002664 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00002665 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00002666 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00002667
aliguori06d55cc2008-11-18 20:24:06 +00002668 if (env->watchpoint_hit) {
2669 /* We re-entered the check after replacing the TB. Now raise
2670 * the debug interrupt so that is will trigger after the
2671 * current instruction. */
2672 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2673 return;
2674 }
pbrook2e70f6e2008-06-29 01:03:05 +00002675 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002676 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00002677 if ((vaddr == (wp->vaddr & len_mask) ||
2678 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00002679 wp->flags |= BP_WATCHPOINT_HIT;
2680 if (!env->watchpoint_hit) {
2681 env->watchpoint_hit = wp;
2682 tb = tb_find_pc(env->mem_io_pc);
2683 if (!tb) {
2684 cpu_abort(env, "check_watchpoint: could not find TB for "
2685 "pc=%p", (void *)env->mem_io_pc);
2686 }
2687 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
2688 tb_phys_invalidate(tb, -1);
2689 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2690 env->exception_index = EXCP_DEBUG;
2691 } else {
2692 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2693 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
2694 }
2695 cpu_resume_from_signal(env, NULL);
aliguori06d55cc2008-11-18 20:24:06 +00002696 }
aliguori6e140f22008-11-18 20:37:55 +00002697 } else {
2698 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00002699 }
2700 }
2701}
2702
pbrook6658ffb2007-03-16 23:58:11 +00002703/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2704 so these check for a hit then pass through to the normal out-of-line
2705 phys routines. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002706static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00002707{
aliguorib4051332008-11-18 20:14:20 +00002708 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002709 return ldub_phys(addr);
2710}
2711
Anthony Liguoric227f092009-10-01 16:12:16 -05002712static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00002713{
aliguorib4051332008-11-18 20:14:20 +00002714 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002715 return lduw_phys(addr);
2716}
2717
Anthony Liguoric227f092009-10-01 16:12:16 -05002718static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00002719{
aliguorib4051332008-11-18 20:14:20 +00002720 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002721 return ldl_phys(addr);
2722}
2723
Anthony Liguoric227f092009-10-01 16:12:16 -05002724static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00002725 uint32_t val)
2726{
aliguorib4051332008-11-18 20:14:20 +00002727 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002728 stb_phys(addr, val);
2729}
2730
Anthony Liguoric227f092009-10-01 16:12:16 -05002731static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00002732 uint32_t val)
2733{
aliguorib4051332008-11-18 20:14:20 +00002734 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002735 stw_phys(addr, val);
2736}
2737
Anthony Liguoric227f092009-10-01 16:12:16 -05002738static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00002739 uint32_t val)
2740{
aliguorib4051332008-11-18 20:14:20 +00002741 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00002742 stl_phys(addr, val);
2743}
2744
Blue Swirld60efc62009-08-25 18:29:31 +00002745static CPUReadMemoryFunc * const watch_mem_read[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00002746 watch_mem_readb,
2747 watch_mem_readw,
2748 watch_mem_readl,
2749};
2750
Blue Swirld60efc62009-08-25 18:29:31 +00002751static CPUWriteMemoryFunc * const watch_mem_write[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00002752 watch_mem_writeb,
2753 watch_mem_writew,
2754 watch_mem_writel,
2755};
pbrook6658ffb2007-03-16 23:58:11 +00002756
Anthony Liguoric227f092009-10-01 16:12:16 -05002757static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00002758 unsigned int len)
2759{
blueswir1db7b5422007-05-26 17:36:03 +00002760 uint32_t ret;
2761 unsigned int idx;
2762
pbrook8da3ff12008-12-01 18:59:50 +00002763 idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00002764#if defined(DEBUG_SUBPAGE)
2765 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2766 mmio, len, addr, idx);
2767#endif
pbrook8da3ff12008-12-01 18:59:50 +00002768 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
2769 addr + mmio->region_offset[idx][0][len]);
blueswir1db7b5422007-05-26 17:36:03 +00002770
2771 return ret;
2772}
2773
Anthony Liguoric227f092009-10-01 16:12:16 -05002774static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00002775 uint32_t value, unsigned int len)
2776{
blueswir1db7b5422007-05-26 17:36:03 +00002777 unsigned int idx;
2778
pbrook8da3ff12008-12-01 18:59:50 +00002779 idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00002780#if defined(DEBUG_SUBPAGE)
2781 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2782 mmio, len, addr, idx, value);
2783#endif
pbrook8da3ff12008-12-01 18:59:50 +00002784 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
2785 addr + mmio->region_offset[idx][1][len],
2786 value);
blueswir1db7b5422007-05-26 17:36:03 +00002787}
2788
Anthony Liguoric227f092009-10-01 16:12:16 -05002789static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00002790{
2791#if defined(DEBUG_SUBPAGE)
2792 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2793#endif
2794
2795 return subpage_readlen(opaque, addr, 0);
2796}
2797
Anthony Liguoric227f092009-10-01 16:12:16 -05002798static void subpage_writeb (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00002799 uint32_t value)
2800{
2801#if defined(DEBUG_SUBPAGE)
2802 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2803#endif
2804 subpage_writelen(opaque, addr, value, 0);
2805}
2806
Anthony Liguoric227f092009-10-01 16:12:16 -05002807static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00002808{
2809#if defined(DEBUG_SUBPAGE)
2810 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2811#endif
2812
2813 return subpage_readlen(opaque, addr, 1);
2814}
2815
Anthony Liguoric227f092009-10-01 16:12:16 -05002816static void subpage_writew (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00002817 uint32_t value)
2818{
2819#if defined(DEBUG_SUBPAGE)
2820 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2821#endif
2822 subpage_writelen(opaque, addr, value, 1);
2823}
2824
Anthony Liguoric227f092009-10-01 16:12:16 -05002825static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00002826{
2827#if defined(DEBUG_SUBPAGE)
2828 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2829#endif
2830
2831 return subpage_readlen(opaque, addr, 2);
2832}
2833
2834static void subpage_writel (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -05002835 target_phys_addr_t addr, uint32_t value)
blueswir1db7b5422007-05-26 17:36:03 +00002836{
2837#if defined(DEBUG_SUBPAGE)
2838 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2839#endif
2840 subpage_writelen(opaque, addr, value, 2);
2841}
2842
Blue Swirld60efc62009-08-25 18:29:31 +00002843static CPUReadMemoryFunc * const subpage_read[] = {
blueswir1db7b5422007-05-26 17:36:03 +00002844 &subpage_readb,
2845 &subpage_readw,
2846 &subpage_readl,
2847};
2848
Blue Swirld60efc62009-08-25 18:29:31 +00002849static CPUWriteMemoryFunc * const subpage_write[] = {
blueswir1db7b5422007-05-26 17:36:03 +00002850 &subpage_writeb,
2851 &subpage_writew,
2852 &subpage_writel,
2853};
2854
Anthony Liguoric227f092009-10-01 16:12:16 -05002855static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2856 ram_addr_t memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00002857{
2858 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00002859 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00002860
2861 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2862 return -1;
2863 idx = SUBPAGE_IDX(start);
2864 eidx = SUBPAGE_IDX(end);
2865#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00002866 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00002867 mmio, start, end, idx, eidx, memory);
2868#endif
2869 memory >>= IO_MEM_SHIFT;
2870 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00002871 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00002872 if (io_mem_read[memory][i]) {
2873 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2874 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
pbrook8da3ff12008-12-01 18:59:50 +00002875 mmio->region_offset[idx][0][i] = region_offset;
blueswir13ee89922008-01-02 19:45:26 +00002876 }
2877 if (io_mem_write[memory][i]) {
2878 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2879 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
pbrook8da3ff12008-12-01 18:59:50 +00002880 mmio->region_offset[idx][1][i] = region_offset;
blueswir13ee89922008-01-02 19:45:26 +00002881 }
blueswir14254fab2008-01-01 16:57:19 +00002882 }
blueswir1db7b5422007-05-26 17:36:03 +00002883 }
2884
2885 return 0;
2886}
2887
Anthony Liguoric227f092009-10-01 16:12:16 -05002888static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2889 ram_addr_t orig_memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00002890{
Anthony Liguoric227f092009-10-01 16:12:16 -05002891 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00002892 int subpage_memory;
2893
Anthony Liguoric227f092009-10-01 16:12:16 -05002894 mmio = qemu_mallocz(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00002895
2896 mmio->base = base;
Avi Kivity1eed09c2009-06-14 11:38:51 +03002897 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio);
blueswir1db7b5422007-05-26 17:36:03 +00002898#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00002899 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2900 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00002901#endif
aliguori1eec6142009-02-05 22:06:18 +00002902 *phys = subpage_memory | IO_MEM_SUBPAGE;
2903 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory,
pbrook8da3ff12008-12-01 18:59:50 +00002904 region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002905
2906 return mmio;
2907}
2908
aliguori88715652009-02-11 15:20:58 +00002909static int get_free_io_mem_idx(void)
2910{
2911 int i;
2912
2913 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
2914 if (!io_mem_used[i]) {
2915 io_mem_used[i] = 1;
2916 return i;
2917 }
2918
2919 return -1;
2920}
2921
bellard33417e72003-08-10 21:47:01 +00002922/* mem_read and mem_write are arrays of functions containing the
2923 function to access byte (index 0), word (index 1) and dword (index
Paul Brook0b4e6e32009-04-30 18:37:55 +01002924 2). Functions can be omitted with a NULL function pointer.
blueswir13ee89922008-01-02 19:45:26 +00002925 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00002926 modified. If it is zero, a new io zone is allocated. The return
2927 value can be used with cpu_register_physical_memory(). (-1) is
2928 returned if error. */
Avi Kivity1eed09c2009-06-14 11:38:51 +03002929static int cpu_register_io_memory_fixed(int io_index,
Blue Swirld60efc62009-08-25 18:29:31 +00002930 CPUReadMemoryFunc * const *mem_read,
2931 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03002932 void *opaque)
bellard33417e72003-08-10 21:47:01 +00002933{
blueswir14254fab2008-01-01 16:57:19 +00002934 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00002935
2936 if (io_index <= 0) {
aliguori88715652009-02-11 15:20:58 +00002937 io_index = get_free_io_mem_idx();
2938 if (io_index == -1)
2939 return io_index;
bellard33417e72003-08-10 21:47:01 +00002940 } else {
Avi Kivity1eed09c2009-06-14 11:38:51 +03002941 io_index >>= IO_MEM_SHIFT;
bellard33417e72003-08-10 21:47:01 +00002942 if (io_index >= IO_MEM_NB_ENTRIES)
2943 return -1;
2944 }
bellardb5ff1b32005-11-26 10:38:39 +00002945
bellard33417e72003-08-10 21:47:01 +00002946 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00002947 if (!mem_read[i] || !mem_write[i])
2948 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00002949 io_mem_read[io_index][i] = mem_read[i];
2950 io_mem_write[io_index][i] = mem_write[i];
2951 }
bellarda4193c82004-06-03 14:01:43 +00002952 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00002953 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00002954}
bellard61382a52003-10-27 21:22:23 +00002955
Blue Swirld60efc62009-08-25 18:29:31 +00002956int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
2957 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03002958 void *opaque)
2959{
2960 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
2961}
2962
aliguori88715652009-02-11 15:20:58 +00002963void cpu_unregister_io_memory(int io_table_address)
2964{
2965 int i;
2966 int io_index = io_table_address >> IO_MEM_SHIFT;
2967
2968 for (i=0;i < 3; i++) {
2969 io_mem_read[io_index][i] = unassigned_mem_read[i];
2970 io_mem_write[io_index][i] = unassigned_mem_write[i];
2971 }
2972 io_mem_opaque[io_index] = NULL;
2973 io_mem_used[io_index] = 0;
2974}
2975
Avi Kivitye9179ce2009-06-14 11:38:52 +03002976static void io_mem_init(void)
2977{
2978 int i;
2979
2980 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
2981 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
2982 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
2983 for (i=0; i<5; i++)
2984 io_mem_used[i] = 1;
2985
2986 io_mem_watch = cpu_register_io_memory(watch_mem_read,
2987 watch_mem_write, NULL);
Avi Kivitye9179ce2009-06-14 11:38:52 +03002988}
2989
pbrooke2eef172008-06-08 01:09:01 +00002990#endif /* !defined(CONFIG_USER_ONLY) */
2991
bellard13eb76e2004-01-24 15:23:36 +00002992/* physical memory access (slow version, mainly for debug) */
2993#if defined(CONFIG_USER_ONLY)
Anthony Liguoric227f092009-10-01 16:12:16 -05002994void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00002995 int len, int is_write)
2996{
2997 int l, flags;
2998 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00002999 void * p;
bellard13eb76e2004-01-24 15:23:36 +00003000
3001 while (len > 0) {
3002 page = addr & TARGET_PAGE_MASK;
3003 l = (page + TARGET_PAGE_SIZE) - addr;
3004 if (l > len)
3005 l = len;
3006 flags = page_get_flags(page);
3007 if (!(flags & PAGE_VALID))
3008 return;
3009 if (is_write) {
3010 if (!(flags & PAGE_WRITE))
3011 return;
bellard579a97f2007-11-11 14:26:47 +00003012 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003013 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
bellard579a97f2007-11-11 14:26:47 +00003014 /* FIXME - should this return an error rather than just fail? */
3015 return;
aurel3272fb7da2008-04-27 23:53:45 +00003016 memcpy(p, buf, l);
3017 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00003018 } else {
3019 if (!(flags & PAGE_READ))
3020 return;
bellard579a97f2007-11-11 14:26:47 +00003021 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003022 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
bellard579a97f2007-11-11 14:26:47 +00003023 /* FIXME - should this return an error rather than just fail? */
3024 return;
aurel3272fb7da2008-04-27 23:53:45 +00003025 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00003026 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00003027 }
3028 len -= l;
3029 buf += l;
3030 addr += l;
3031 }
3032}
bellard8df1cd02005-01-28 22:37:22 +00003033
bellard13eb76e2004-01-24 15:23:36 +00003034#else
Anthony Liguoric227f092009-10-01 16:12:16 -05003035void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00003036 int len, int is_write)
3037{
3038 int l, io_index;
3039 uint8_t *ptr;
3040 uint32_t val;
Anthony Liguoric227f092009-10-01 16:12:16 -05003041 target_phys_addr_t page;
bellard2e126692004-04-25 21:28:44 +00003042 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00003043 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003044
bellard13eb76e2004-01-24 15:23:36 +00003045 while (len > 0) {
3046 page = addr & TARGET_PAGE_MASK;
3047 l = (page + TARGET_PAGE_SIZE) - addr;
3048 if (l > len)
3049 l = len;
bellard92e873b2004-05-21 14:52:29 +00003050 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00003051 if (!p) {
3052 pd = IO_MEM_UNASSIGNED;
3053 } else {
3054 pd = p->phys_offset;
3055 }
ths3b46e622007-09-17 08:09:54 +00003056
bellard13eb76e2004-01-24 15:23:36 +00003057 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00003058 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003059 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003060 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003061 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003062 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard6a00d602005-11-21 23:25:50 +00003063 /* XXX: could force cpu_single_env to NULL to avoid
3064 potential bugs */
aurel326c2934d2009-02-18 21:37:17 +00003065 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003066 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003067 val = ldl_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003068 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003069 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003070 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003071 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003072 val = lduw_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003073 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003074 l = 2;
3075 } else {
bellard1c213d12005-09-03 10:49:04 +00003076 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003077 val = ldub_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003078 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003079 l = 1;
3080 }
3081 } else {
bellardb448f2f2004-02-25 23:24:04 +00003082 unsigned long addr1;
3083 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00003084 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003085 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00003086 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00003087 if (!cpu_physical_memory_is_dirty(addr1)) {
3088 /* invalidate code */
3089 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3090 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00003091 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00003092 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00003093 }
bellard13eb76e2004-01-24 15:23:36 +00003094 }
3095 } else {
ths5fafdf22007-09-16 21:08:06 +00003096 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003097 !(pd & IO_MEM_ROMD)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003098 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003099 /* I/O case */
3100 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003101 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003102 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3103 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003104 /* 32 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003105 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003106 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003107 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003108 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003109 /* 16 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003110 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003111 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003112 l = 2;
3113 } else {
bellard1c213d12005-09-03 10:49:04 +00003114 /* 8 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003115 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003116 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003117 l = 1;
3118 }
3119 } else {
3120 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003121 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00003122 (addr & ~TARGET_PAGE_MASK);
3123 memcpy(buf, ptr, l);
3124 }
3125 }
3126 len -= l;
3127 buf += l;
3128 addr += l;
3129 }
3130}
bellard8df1cd02005-01-28 22:37:22 +00003131
bellardd0ecd2a2006-04-23 17:14:48 +00003132/* used for ROM loading : can write in RAM and ROM */
Anthony Liguoric227f092009-10-01 16:12:16 -05003133void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00003134 const uint8_t *buf, int len)
3135{
3136 int l;
3137 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003138 target_phys_addr_t page;
bellardd0ecd2a2006-04-23 17:14:48 +00003139 unsigned long pd;
3140 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003141
bellardd0ecd2a2006-04-23 17:14:48 +00003142 while (len > 0) {
3143 page = addr & TARGET_PAGE_MASK;
3144 l = (page + TARGET_PAGE_SIZE) - addr;
3145 if (l > len)
3146 l = len;
3147 p = phys_page_find(page >> TARGET_PAGE_BITS);
3148 if (!p) {
3149 pd = IO_MEM_UNASSIGNED;
3150 } else {
3151 pd = p->phys_offset;
3152 }
ths3b46e622007-09-17 08:09:54 +00003153
bellardd0ecd2a2006-04-23 17:14:48 +00003154 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00003155 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3156 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00003157 /* do nothing */
3158 } else {
3159 unsigned long addr1;
3160 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3161 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003162 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00003163 memcpy(ptr, buf, l);
3164 }
3165 len -= l;
3166 buf += l;
3167 addr += l;
3168 }
3169}
3170
aliguori6d16c2f2009-01-22 16:59:11 +00003171typedef struct {
3172 void *buffer;
Anthony Liguoric227f092009-10-01 16:12:16 -05003173 target_phys_addr_t addr;
3174 target_phys_addr_t len;
aliguori6d16c2f2009-01-22 16:59:11 +00003175} BounceBuffer;
3176
3177static BounceBuffer bounce;
3178
aliguoriba223c22009-01-22 16:59:16 +00003179typedef struct MapClient {
3180 void *opaque;
3181 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00003182 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00003183} MapClient;
3184
Blue Swirl72cf2d42009-09-12 07:36:22 +00003185static QLIST_HEAD(map_client_list, MapClient) map_client_list
3186 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003187
3188void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3189{
3190 MapClient *client = qemu_malloc(sizeof(*client));
3191
3192 client->opaque = opaque;
3193 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003194 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00003195 return client;
3196}
3197
3198void cpu_unregister_map_client(void *_client)
3199{
3200 MapClient *client = (MapClient *)_client;
3201
Blue Swirl72cf2d42009-09-12 07:36:22 +00003202 QLIST_REMOVE(client, link);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003203 qemu_free(client);
aliguoriba223c22009-01-22 16:59:16 +00003204}
3205
3206static void cpu_notify_map_clients(void)
3207{
3208 MapClient *client;
3209
Blue Swirl72cf2d42009-09-12 07:36:22 +00003210 while (!QLIST_EMPTY(&map_client_list)) {
3211 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003212 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003213 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00003214 }
3215}
3216
aliguori6d16c2f2009-01-22 16:59:11 +00003217/* Map a physical memory region into a host virtual address.
3218 * May map a subset of the requested range, given by and returned in *plen.
3219 * May return NULL if resources needed to perform the mapping are exhausted.
3220 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00003221 * Use cpu_register_map_client() to know when retrying the map operation is
3222 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00003223 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003224void *cpu_physical_memory_map(target_phys_addr_t addr,
3225 target_phys_addr_t *plen,
aliguori6d16c2f2009-01-22 16:59:11 +00003226 int is_write)
3227{
Anthony Liguoric227f092009-10-01 16:12:16 -05003228 target_phys_addr_t len = *plen;
3229 target_phys_addr_t done = 0;
aliguori6d16c2f2009-01-22 16:59:11 +00003230 int l;
3231 uint8_t *ret = NULL;
3232 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003233 target_phys_addr_t page;
aliguori6d16c2f2009-01-22 16:59:11 +00003234 unsigned long pd;
3235 PhysPageDesc *p;
3236 unsigned long addr1;
3237
3238 while (len > 0) {
3239 page = addr & TARGET_PAGE_MASK;
3240 l = (page + TARGET_PAGE_SIZE) - addr;
3241 if (l > len)
3242 l = len;
3243 p = phys_page_find(page >> TARGET_PAGE_BITS);
3244 if (!p) {
3245 pd = IO_MEM_UNASSIGNED;
3246 } else {
3247 pd = p->phys_offset;
3248 }
3249
3250 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3251 if (done || bounce.buffer) {
3252 break;
3253 }
3254 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3255 bounce.addr = addr;
3256 bounce.len = l;
3257 if (!is_write) {
3258 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3259 }
3260 ptr = bounce.buffer;
3261 } else {
3262 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003263 ptr = qemu_get_ram_ptr(addr1);
aliguori6d16c2f2009-01-22 16:59:11 +00003264 }
3265 if (!done) {
3266 ret = ptr;
3267 } else if (ret + done != ptr) {
3268 break;
3269 }
3270
3271 len -= l;
3272 addr += l;
3273 done += l;
3274 }
3275 *plen = done;
3276 return ret;
3277}
3278
3279/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3280 * Will also mark the memory as dirty if is_write == 1. access_len gives
3281 * the amount of memory that was actually read or written by the caller.
3282 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003283void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3284 int is_write, target_phys_addr_t access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00003285{
3286 if (buffer != bounce.buffer) {
3287 if (is_write) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003288 ram_addr_t addr1 = qemu_ram_addr_from_host(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00003289 while (access_len) {
3290 unsigned l;
3291 l = TARGET_PAGE_SIZE;
3292 if (l > access_len)
3293 l = access_len;
3294 if (!cpu_physical_memory_is_dirty(addr1)) {
3295 /* invalidate code */
3296 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3297 /* set dirty bit */
3298 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3299 (0xff & ~CODE_DIRTY_FLAG);
3300 }
3301 addr1 += l;
3302 access_len -= l;
3303 }
3304 }
3305 return;
3306 }
3307 if (is_write) {
3308 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3309 }
3310 qemu_free(bounce.buffer);
3311 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00003312 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00003313}
bellardd0ecd2a2006-04-23 17:14:48 +00003314
bellard8df1cd02005-01-28 22:37:22 +00003315/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003316uint32_t ldl_phys(target_phys_addr_t addr)
bellard8df1cd02005-01-28 22:37:22 +00003317{
3318 int io_index;
3319 uint8_t *ptr;
3320 uint32_t val;
3321 unsigned long pd;
3322 PhysPageDesc *p;
3323
3324 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3325 if (!p) {
3326 pd = IO_MEM_UNASSIGNED;
3327 } else {
3328 pd = p->phys_offset;
3329 }
ths3b46e622007-09-17 08:09:54 +00003330
ths5fafdf22007-09-16 21:08:06 +00003331 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003332 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00003333 /* I/O case */
3334 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003335 if (p)
3336 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003337 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3338 } else {
3339 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003340 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00003341 (addr & ~TARGET_PAGE_MASK);
3342 val = ldl_p(ptr);
3343 }
3344 return val;
3345}
3346
bellard84b7b8e2005-11-28 21:19:04 +00003347/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003348uint64_t ldq_phys(target_phys_addr_t addr)
bellard84b7b8e2005-11-28 21:19:04 +00003349{
3350 int io_index;
3351 uint8_t *ptr;
3352 uint64_t val;
3353 unsigned long pd;
3354 PhysPageDesc *p;
3355
3356 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3357 if (!p) {
3358 pd = IO_MEM_UNASSIGNED;
3359 } else {
3360 pd = p->phys_offset;
3361 }
ths3b46e622007-09-17 08:09:54 +00003362
bellard2a4188a2006-06-25 21:54:59 +00003363 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3364 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00003365 /* I/O case */
3366 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003367 if (p)
3368 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard84b7b8e2005-11-28 21:19:04 +00003369#ifdef TARGET_WORDS_BIGENDIAN
3370 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3371 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3372#else
3373 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3374 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3375#endif
3376 } else {
3377 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003378 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00003379 (addr & ~TARGET_PAGE_MASK);
3380 val = ldq_p(ptr);
3381 }
3382 return val;
3383}
3384
bellardaab33092005-10-30 20:48:42 +00003385/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003386uint32_t ldub_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003387{
3388 uint8_t val;
3389 cpu_physical_memory_read(addr, &val, 1);
3390 return val;
3391}
3392
3393/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003394uint32_t lduw_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003395{
3396 uint16_t val;
3397 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
3398 return tswap16(val);
3399}
3400
bellard8df1cd02005-01-28 22:37:22 +00003401/* warning: addr must be aligned. The ram page is not masked as dirty
3402 and the code inside is not invalidated. It is useful if the dirty
3403 bits are used to track modified PTEs */
Anthony Liguoric227f092009-10-01 16:12:16 -05003404void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003405{
3406 int io_index;
3407 uint8_t *ptr;
3408 unsigned long pd;
3409 PhysPageDesc *p;
3410
3411 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3412 if (!p) {
3413 pd = IO_MEM_UNASSIGNED;
3414 } else {
3415 pd = p->phys_offset;
3416 }
ths3b46e622007-09-17 08:09:54 +00003417
bellard3a7d9292005-08-21 09:26:42 +00003418 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003419 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003420 if (p)
3421 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003422 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3423 } else {
aliguori74576192008-10-06 14:02:03 +00003424 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003425 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003426 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00003427
3428 if (unlikely(in_migration)) {
3429 if (!cpu_physical_memory_is_dirty(addr1)) {
3430 /* invalidate code */
3431 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3432 /* set dirty bit */
3433 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3434 (0xff & ~CODE_DIRTY_FLAG);
3435 }
3436 }
bellard8df1cd02005-01-28 22:37:22 +00003437 }
3438}
3439
Anthony Liguoric227f092009-10-01 16:12:16 -05003440void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
j_mayerbc98a7e2007-04-04 07:55:12 +00003441{
3442 int io_index;
3443 uint8_t *ptr;
3444 unsigned long pd;
3445 PhysPageDesc *p;
3446
3447 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3448 if (!p) {
3449 pd = IO_MEM_UNASSIGNED;
3450 } else {
3451 pd = p->phys_offset;
3452 }
ths3b46e622007-09-17 08:09:54 +00003453
j_mayerbc98a7e2007-04-04 07:55:12 +00003454 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3455 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003456 if (p)
3457 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
j_mayerbc98a7e2007-04-04 07:55:12 +00003458#ifdef TARGET_WORDS_BIGENDIAN
3459 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3460 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3461#else
3462 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3463 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3464#endif
3465 } else {
pbrook5579c7f2009-04-11 14:47:08 +00003466 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00003467 (addr & ~TARGET_PAGE_MASK);
3468 stq_p(ptr, val);
3469 }
3470}
3471
bellard8df1cd02005-01-28 22:37:22 +00003472/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003473void stl_phys(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003474{
3475 int io_index;
3476 uint8_t *ptr;
3477 unsigned long pd;
3478 PhysPageDesc *p;
3479
3480 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3481 if (!p) {
3482 pd = IO_MEM_UNASSIGNED;
3483 } else {
3484 pd = p->phys_offset;
3485 }
ths3b46e622007-09-17 08:09:54 +00003486
bellard3a7d9292005-08-21 09:26:42 +00003487 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003488 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003489 if (p)
3490 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003491 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3492 } else {
3493 unsigned long addr1;
3494 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3495 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003496 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003497 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00003498 if (!cpu_physical_memory_is_dirty(addr1)) {
3499 /* invalidate code */
3500 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3501 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00003502 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3503 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00003504 }
bellard8df1cd02005-01-28 22:37:22 +00003505 }
3506}
3507
bellardaab33092005-10-30 20:48:42 +00003508/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003509void stb_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003510{
3511 uint8_t v = val;
3512 cpu_physical_memory_write(addr, &v, 1);
3513}
3514
3515/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003516void stw_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003517{
3518 uint16_t v = tswap16(val);
3519 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3520}
3521
3522/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003523void stq_phys(target_phys_addr_t addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00003524{
3525 val = tswap64(val);
3526 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3527}
3528
bellard13eb76e2004-01-24 15:23:36 +00003529#endif
3530
aliguori5e2972f2009-03-28 17:51:36 +00003531/* virtual memory access for debug (includes writing to ROM) */
ths5fafdf22007-09-16 21:08:06 +00003532int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00003533 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003534{
3535 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05003536 target_phys_addr_t phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00003537 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00003538
3539 while (len > 0) {
3540 page = addr & TARGET_PAGE_MASK;
3541 phys_addr = cpu_get_phys_page_debug(env, page);
3542 /* if no physical page mapped, return an error */
3543 if (phys_addr == -1)
3544 return -1;
3545 l = (page + TARGET_PAGE_SIZE) - addr;
3546 if (l > len)
3547 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00003548 phys_addr += (addr & ~TARGET_PAGE_MASK);
3549#if !defined(CONFIG_USER_ONLY)
3550 if (is_write)
3551 cpu_physical_memory_write_rom(phys_addr, buf, l);
3552 else
3553#endif
3554 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00003555 len -= l;
3556 buf += l;
3557 addr += l;
3558 }
3559 return 0;
3560}
3561
pbrook2e70f6e2008-06-29 01:03:05 +00003562/* in deterministic execution mode, instructions doing device I/Os
3563 must be at the end of the TB */
3564void cpu_io_recompile(CPUState *env, void *retaddr)
3565{
3566 TranslationBlock *tb;
3567 uint32_t n, cflags;
3568 target_ulong pc, cs_base;
3569 uint64_t flags;
3570
3571 tb = tb_find_pc((unsigned long)retaddr);
3572 if (!tb) {
3573 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3574 retaddr);
3575 }
3576 n = env->icount_decr.u16.low + tb->icount;
3577 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3578 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00003579 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00003580 n = n - env->icount_decr.u16.low;
3581 /* Generate a new TB ending on the I/O insn. */
3582 n++;
3583 /* On MIPS and SH, delay slot instructions can only be restarted if
3584 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00003585 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00003586 branch. */
3587#if defined(TARGET_MIPS)
3588 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3589 env->active_tc.PC -= 4;
3590 env->icount_decr.u16.low++;
3591 env->hflags &= ~MIPS_HFLAG_BMASK;
3592 }
3593#elif defined(TARGET_SH4)
3594 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3595 && n > 1) {
3596 env->pc -= 2;
3597 env->icount_decr.u16.low++;
3598 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3599 }
3600#endif
3601 /* This should never happen. */
3602 if (n > CF_COUNT_MASK)
3603 cpu_abort(env, "TB too big during recompile");
3604
3605 cflags = n | CF_LAST_IO;
3606 pc = tb->pc;
3607 cs_base = tb->cs_base;
3608 flags = tb->flags;
3609 tb_phys_invalidate(tb, -1);
3610 /* FIXME: In theory this could raise an exception. In practice
3611 we have already translated the block once so it's probably ok. */
3612 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00003613 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00003614 the first in the TB) then we end up generating a whole new TB and
3615 repeating the fault, which is horribly inefficient.
3616 Better would be to execute just this insn uncached, or generate a
3617 second new TB. */
3618 cpu_resume_from_signal(env, NULL);
3619}
3620
bellarde3db7222005-01-26 22:00:47 +00003621void dump_exec_info(FILE *f,
3622 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3623{
3624 int i, target_code_size, max_target_code_size;
3625 int direct_jmp_count, direct_jmp2_count, cross_page;
3626 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003627
bellarde3db7222005-01-26 22:00:47 +00003628 target_code_size = 0;
3629 max_target_code_size = 0;
3630 cross_page = 0;
3631 direct_jmp_count = 0;
3632 direct_jmp2_count = 0;
3633 for(i = 0; i < nb_tbs; i++) {
3634 tb = &tbs[i];
3635 target_code_size += tb->size;
3636 if (tb->size > max_target_code_size)
3637 max_target_code_size = tb->size;
3638 if (tb->page_addr[1] != -1)
3639 cross_page++;
3640 if (tb->tb_next_offset[0] != 0xffff) {
3641 direct_jmp_count++;
3642 if (tb->tb_next_offset[1] != 0xffff) {
3643 direct_jmp2_count++;
3644 }
3645 }
3646 }
3647 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003648 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003649 cpu_fprintf(f, "gen code size %ld/%ld\n",
3650 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3651 cpu_fprintf(f, "TB count %d/%d\n",
3652 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003653 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003654 nb_tbs ? target_code_size / nb_tbs : 0,
3655 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003656 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003657 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3658 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003659 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3660 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003661 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3662 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003663 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003664 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3665 direct_jmp2_count,
3666 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003667 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003668 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3669 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3670 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003671 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003672}
3673
ths5fafdf22007-09-16 21:08:06 +00003674#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003675
3676#define MMUSUFFIX _cmmu
3677#define GETPC() NULL
3678#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003679#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003680
3681#define SHIFT 0
3682#include "softmmu_template.h"
3683
3684#define SHIFT 1
3685#include "softmmu_template.h"
3686
3687#define SHIFT 2
3688#include "softmmu_template.h"
3689
3690#define SHIFT 3
3691#include "softmmu_template.h"
3692
3693#undef env
3694
3695#endif