blob: 631d8c564ec8fa3a1baec513976af3448eab0552 [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"
Alex Williamsoncc9e98c2010-06-25 11:09:43 -060039#include "hw/qdev.h"
aliguori74576192008-10-06 14:02:03 +000040#include "osdep.h"
aliguori7ba1e612008-11-05 16:04:33 +000041#include "kvm.h"
Blue Swirl29e922b2010-03-29 19:24:00 +000042#include "qemu-timer.h"
pbrook53a59602006-03-25 19:31:22 +000043#if defined(CONFIG_USER_ONLY)
44#include <qemu.h>
Riku Voipiofd052bf2010-01-25 14:30:49 +020045#include <signal.h>
Juergen Lockf01576f2010-03-25 22:32:16 +010046#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47#include <sys/param.h>
48#if __FreeBSD_version >= 700104
49#define HAVE_KINFO_GETVMMAP
50#define sigqueue sigqueue_freebsd /* avoid redefinition */
51#include <sys/time.h>
52#include <sys/proc.h>
53#include <machine/profile.h>
54#define _KERNEL
55#include <sys/user.h>
56#undef _KERNEL
57#undef sigqueue
58#include <libutil.h>
59#endif
60#endif
pbrook53a59602006-03-25 19:31:22 +000061#endif
bellard54936002003-05-13 00:25:15 +000062
bellardfd6ce8f2003-05-14 19:00:11 +000063//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000064//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000065//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000066//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000067
68/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000069//#define DEBUG_TB_CHECK
70//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000071
ths1196be32007-03-17 15:17:58 +000072//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000073//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000074
pbrook99773bd2006-04-16 15:14:59 +000075#if !defined(CONFIG_USER_ONLY)
76/* TB consistency checks only implemented for usermode emulation. */
77#undef DEBUG_TB_CHECK
78#endif
79
bellard9fa3e852004-01-04 18:06:42 +000080#define SMC_BITMAP_USE_THRESHOLD 10
81
blueswir1bdaf78e2008-10-04 07:24:27 +000082static TranslationBlock *tbs;
Stefan Weil24ab68a2010-07-19 18:23:17 +020083static int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000084TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +000085static int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000086/* any access to the tbs or the page table must use this lock */
Anthony Liguoric227f092009-10-01 16:12:16 -050087spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000088
blueswir1141ac462008-07-26 15:05:57 +000089#if defined(__arm__) || defined(__sparc_v9__)
90/* The prologue must be reachable with a direct jump. ARM and Sparc64
91 have limited branch ranges (possibly also PPC) so place it in a
blueswir1d03d8602008-07-10 17:21:31 +000092 section close to code segment. */
93#define code_gen_section \
94 __attribute__((__section__(".gen_code"))) \
95 __attribute__((aligned (32)))
Stefan Weilf8e2af12009-06-18 23:04:48 +020096#elif defined(_WIN32)
97/* Maximum alignment for Win32 is 16. */
98#define code_gen_section \
99 __attribute__((aligned (16)))
blueswir1d03d8602008-07-10 17:21:31 +0000100#else
101#define code_gen_section \
102 __attribute__((aligned (32)))
103#endif
104
105uint8_t code_gen_prologue[1024] code_gen_section;
blueswir1bdaf78e2008-10-04 07:24:27 +0000106static uint8_t *code_gen_buffer;
107static unsigned long code_gen_buffer_size;
bellard26a5f132008-05-28 12:30:31 +0000108/* threshold to flush the translated code buffer */
blueswir1bdaf78e2008-10-04 07:24:27 +0000109static unsigned long code_gen_buffer_max_size;
Stefan Weil24ab68a2010-07-19 18:23:17 +0200110static uint8_t *code_gen_ptr;
bellardfd6ce8f2003-05-14 19:00:11 +0000111
pbrooke2eef172008-06-08 01:09:01 +0000112#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +0000113int phys_ram_fd;
aliguori74576192008-10-06 14:02:03 +0000114static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +0000115
Alex Williamsonf471a172010-06-11 11:11:42 -0600116RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list) };
pbrooke2eef172008-06-08 01:09:01 +0000117#endif
bellard9fa3e852004-01-04 18:06:42 +0000118
bellard6a00d602005-11-21 23:25:50 +0000119CPUState *first_cpu;
120/* current CPU in the current thread. It is only valid inside
121 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000122CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000123/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +0000124 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +0000125 2 = Adaptive rate instruction counting. */
126int use_icount = 0;
127/* Current instruction counter. While executing translated code this may
128 include some instructions that have not yet been executed. */
129int64_t qemu_icount;
bellard6a00d602005-11-21 23:25:50 +0000130
bellard54936002003-05-13 00:25:15 +0000131typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000132 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000133 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000134 /* in order to optimize self modifying code, we count the number
135 of lookups we do to a given page to use a bitmap */
136 unsigned int code_write_count;
137 uint8_t *code_bitmap;
138#if defined(CONFIG_USER_ONLY)
139 unsigned long flags;
140#endif
bellard54936002003-05-13 00:25:15 +0000141} PageDesc;
142
Paul Brook41c1b1c2010-03-12 16:54:58 +0000143/* In system mode we want L1_MAP to be based on ram offsets,
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800144 while in user mode we want it to be based on virtual addresses. */
145#if !defined(CONFIG_USER_ONLY)
Paul Brook41c1b1c2010-03-12 16:54:58 +0000146#if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
147# define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
148#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800149# define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
Paul Brook41c1b1c2010-03-12 16:54:58 +0000150#endif
j_mayerbedb69e2007-04-05 20:08:21 +0000151#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800152# define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
j_mayerbedb69e2007-04-05 20:08:21 +0000153#endif
bellard54936002003-05-13 00:25:15 +0000154
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800155/* Size of the L2 (and L3, etc) page tables. */
156#define L2_BITS 10
bellard54936002003-05-13 00:25:15 +0000157#define L2_SIZE (1 << L2_BITS)
158
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800159/* The bits remaining after N lower levels of page tables. */
160#define P_L1_BITS_REM \
161 ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
162#define V_L1_BITS_REM \
163 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
164
165/* Size of the L1 page table. Avoid silly small sizes. */
166#if P_L1_BITS_REM < 4
167#define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
168#else
169#define P_L1_BITS P_L1_BITS_REM
170#endif
171
172#if V_L1_BITS_REM < 4
173#define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
174#else
175#define V_L1_BITS V_L1_BITS_REM
176#endif
177
178#define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
179#define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
180
181#define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
182#define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
183
bellard83fb7ad2004-07-05 21:25:26 +0000184unsigned long qemu_real_host_page_size;
185unsigned long qemu_host_page_bits;
186unsigned long qemu_host_page_size;
187unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000188
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800189/* This is a multi-level map on the virtual address space.
190 The bottom level has pointers to PageDesc. */
191static void *l1_map[V_L1_SIZE];
bellard54936002003-05-13 00:25:15 +0000192
pbrooke2eef172008-06-08 01:09:01 +0000193#if !defined(CONFIG_USER_ONLY)
Paul Brook41c1b1c2010-03-12 16:54:58 +0000194typedef struct PhysPageDesc {
195 /* offset in host memory of the page + io_index in the low bits */
196 ram_addr_t phys_offset;
197 ram_addr_t region_offset;
198} PhysPageDesc;
199
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800200/* This is a multi-level map on the physical address space.
201 The bottom level has pointers to PhysPageDesc. */
202static void *l1_phys_map[P_L1_SIZE];
Paul Brook6d9a1302010-02-28 23:55:53 +0000203
pbrooke2eef172008-06-08 01:09:01 +0000204static void io_mem_init(void);
205
bellard33417e72003-08-10 21:47:01 +0000206/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000207CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
208CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000209void *io_mem_opaque[IO_MEM_NB_ENTRIES];
blueswir1511d2b12009-03-07 15:32:56 +0000210static char io_mem_used[IO_MEM_NB_ENTRIES];
pbrook6658ffb2007-03-16 23:58:11 +0000211static int io_mem_watch;
212#endif
bellard33417e72003-08-10 21:47:01 +0000213
bellard34865132003-10-05 14:28:56 +0000214/* log support */
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200215#ifdef WIN32
216static const char *logfilename = "qemu.log";
217#else
blueswir1d9b630f2008-10-05 09:57:08 +0000218static const char *logfilename = "/tmp/qemu.log";
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200219#endif
bellard34865132003-10-05 14:28:56 +0000220FILE *logfile;
221int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000222static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000223
bellarde3db7222005-01-26 22:00:47 +0000224/* statistics */
Paul Brookb3755a92010-03-12 16:54:58 +0000225#if !defined(CONFIG_USER_ONLY)
bellarde3db7222005-01-26 22:00:47 +0000226static int tlb_flush_count;
Paul Brookb3755a92010-03-12 16:54:58 +0000227#endif
bellarde3db7222005-01-26 22:00:47 +0000228static int tb_flush_count;
229static int tb_phys_invalidate_count;
230
bellard7cb69ca2008-05-10 10:55:51 +0000231#ifdef _WIN32
232static void map_exec(void *addr, long size)
233{
234 DWORD old_protect;
235 VirtualProtect(addr, size,
236 PAGE_EXECUTE_READWRITE, &old_protect);
237
238}
239#else
240static void map_exec(void *addr, long size)
241{
bellard43694152008-05-29 09:35:57 +0000242 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000243
bellard43694152008-05-29 09:35:57 +0000244 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000245 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000246 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000247
248 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000249 end += page_size - 1;
250 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000251
252 mprotect((void *)start, end - start,
253 PROT_READ | PROT_WRITE | PROT_EXEC);
254}
255#endif
256
bellardb346ff42003-06-15 20:05:50 +0000257static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000258{
bellard83fb7ad2004-07-05 21:25:26 +0000259 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000260 TARGET_PAGE_SIZE */
aliguoric2b48b62008-11-11 22:06:42 +0000261#ifdef _WIN32
262 {
263 SYSTEM_INFO system_info;
264
265 GetSystemInfo(&system_info);
266 qemu_real_host_page_size = system_info.dwPageSize;
267 }
268#else
269 qemu_real_host_page_size = getpagesize();
270#endif
bellard83fb7ad2004-07-05 21:25:26 +0000271 if (qemu_host_page_size == 0)
272 qemu_host_page_size = qemu_real_host_page_size;
273 if (qemu_host_page_size < TARGET_PAGE_SIZE)
274 qemu_host_page_size = TARGET_PAGE_SIZE;
275 qemu_host_page_bits = 0;
276 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
277 qemu_host_page_bits++;
278 qemu_host_page_mask = ~(qemu_host_page_size - 1);
balrog50a95692007-12-12 01:16:23 +0000279
Paul Brook2e9a5712010-05-05 16:32:59 +0100280#if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
balrog50a95692007-12-12 01:16:23 +0000281 {
Juergen Lockf01576f2010-03-25 22:32:16 +0100282#ifdef HAVE_KINFO_GETVMMAP
283 struct kinfo_vmentry *freep;
284 int i, cnt;
285
286 freep = kinfo_getvmmap(getpid(), &cnt);
287 if (freep) {
288 mmap_lock();
289 for (i = 0; i < cnt; i++) {
290 unsigned long startaddr, endaddr;
291
292 startaddr = freep[i].kve_start;
293 endaddr = freep[i].kve_end;
294 if (h2g_valid(startaddr)) {
295 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
296
297 if (h2g_valid(endaddr)) {
298 endaddr = h2g(endaddr);
Aurelien Jarnofd436902010-04-10 17:20:36 +0200299 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
Juergen Lockf01576f2010-03-25 22:32:16 +0100300 } else {
301#if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
302 endaddr = ~0ul;
Aurelien Jarnofd436902010-04-10 17:20:36 +0200303 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
Juergen Lockf01576f2010-03-25 22:32:16 +0100304#endif
305 }
306 }
307 }
308 free(freep);
309 mmap_unlock();
310 }
311#else
balrog50a95692007-12-12 01:16:23 +0000312 FILE *f;
balrog50a95692007-12-12 01:16:23 +0000313
pbrook07765902008-05-31 16:33:53 +0000314 last_brk = (unsigned long)sbrk(0);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800315
Aurelien Jarnofd436902010-04-10 17:20:36 +0200316 f = fopen("/compat/linux/proc/self/maps", "r");
balrog50a95692007-12-12 01:16:23 +0000317 if (f) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800318 mmap_lock();
319
balrog50a95692007-12-12 01:16:23 +0000320 do {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800321 unsigned long startaddr, endaddr;
322 int n;
323
324 n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
325
326 if (n == 2 && h2g_valid(startaddr)) {
327 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
328
329 if (h2g_valid(endaddr)) {
330 endaddr = h2g(endaddr);
331 } else {
332 endaddr = ~0ul;
333 }
334 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
balrog50a95692007-12-12 01:16:23 +0000335 }
336 } while (!feof(f));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800337
balrog50a95692007-12-12 01:16:23 +0000338 fclose(f);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800339 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000340 }
Juergen Lockf01576f2010-03-25 22:32:16 +0100341#endif
balrog50a95692007-12-12 01:16:23 +0000342 }
343#endif
bellard54936002003-05-13 00:25:15 +0000344}
345
Paul Brook41c1b1c2010-03-12 16:54:58 +0000346static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
bellard54936002003-05-13 00:25:15 +0000347{
Paul Brook41c1b1c2010-03-12 16:54:58 +0000348 PageDesc *pd;
349 void **lp;
350 int i;
351
pbrook17e23772008-06-09 13:47:45 +0000352#if defined(CONFIG_USER_ONLY)
Paul Brook2e9a5712010-05-05 16:32:59 +0100353 /* We can't use qemu_malloc because it may recurse into a locked mutex. */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800354# define ALLOC(P, SIZE) \
355 do { \
356 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
357 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800358 } while (0)
pbrook17e23772008-06-09 13:47:45 +0000359#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800360# define ALLOC(P, SIZE) \
361 do { P = qemu_mallocz(SIZE); } while (0)
pbrook17e23772008-06-09 13:47:45 +0000362#endif
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800363
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800364 /* Level 1. Always allocated. */
365 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
366
367 /* Level 2..N-1. */
368 for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
369 void **p = *lp;
370
371 if (p == NULL) {
372 if (!alloc) {
373 return NULL;
374 }
375 ALLOC(p, sizeof(void *) * L2_SIZE);
376 *lp = p;
377 }
378
379 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000380 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800381
382 pd = *lp;
383 if (pd == NULL) {
384 if (!alloc) {
385 return NULL;
386 }
387 ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
388 *lp = pd;
389 }
390
391#undef ALLOC
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800392
393 return pd + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000394}
395
Paul Brook41c1b1c2010-03-12 16:54:58 +0000396static inline PageDesc *page_find(tb_page_addr_t index)
bellard54936002003-05-13 00:25:15 +0000397{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800398 return page_find_alloc(index, 0);
bellard54936002003-05-13 00:25:15 +0000399}
400
Paul Brook6d9a1302010-02-28 23:55:53 +0000401#if !defined(CONFIG_USER_ONLY)
Anthony Liguoric227f092009-10-01 16:12:16 -0500402static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000403{
pbrooke3f4e2a2006-04-08 20:02:06 +0000404 PhysPageDesc *pd;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800405 void **lp;
406 int i;
bellard92e873b2004-05-21 14:52:29 +0000407
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800408 /* Level 1. Always allocated. */
409 lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000410
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800411 /* Level 2..N-1. */
412 for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
413 void **p = *lp;
414 if (p == NULL) {
415 if (!alloc) {
416 return NULL;
417 }
418 *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE);
419 }
420 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000421 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800422
pbrooke3f4e2a2006-04-08 20:02:06 +0000423 pd = *lp;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800424 if (pd == NULL) {
pbrooke3f4e2a2006-04-08 20:02:06 +0000425 int i;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800426
427 if (!alloc) {
bellard108c49b2005-07-24 12:55:09 +0000428 return NULL;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800429 }
430
431 *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE);
432
pbrook67c4d232009-02-23 13:16:07 +0000433 for (i = 0; i < L2_SIZE; i++) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800434 pd[i].phys_offset = IO_MEM_UNASSIGNED;
435 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
pbrook67c4d232009-02-23 13:16:07 +0000436 }
bellard92e873b2004-05-21 14:52:29 +0000437 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800438
439 return pd + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000440}
441
Anthony Liguoric227f092009-10-01 16:12:16 -0500442static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000443{
bellard108c49b2005-07-24 12:55:09 +0000444 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000445}
446
Anthony Liguoric227f092009-10-01 16:12:16 -0500447static void tlb_protect_code(ram_addr_t ram_addr);
448static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000449 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000450#define mmap_lock() do { } while(0)
451#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000452#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000453
bellard43694152008-05-29 09:35:57 +0000454#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
455
456#if defined(CONFIG_USER_ONLY)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100457/* Currently it is not recommended to allocate big chunks of data in
bellard43694152008-05-29 09:35:57 +0000458 user mode. It will change when a dedicated libc will be used */
459#define USE_STATIC_CODE_GEN_BUFFER
460#endif
461
462#ifdef USE_STATIC_CODE_GEN_BUFFER
Aurelien Jarnoebf50fb2010-03-29 02:12:51 +0200463static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
464 __attribute__((aligned (CODE_GEN_ALIGN)));
bellard43694152008-05-29 09:35:57 +0000465#endif
466
blueswir18fcd3692008-08-17 20:26:25 +0000467static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000468{
bellard43694152008-05-29 09:35:57 +0000469#ifdef USE_STATIC_CODE_GEN_BUFFER
470 code_gen_buffer = static_code_gen_buffer;
471 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
472 map_exec(code_gen_buffer, code_gen_buffer_size);
473#else
bellard26a5f132008-05-28 12:30:31 +0000474 code_gen_buffer_size = tb_size;
475 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000476#if defined(CONFIG_USER_ONLY)
477 /* in user mode, phys_ram_size is not meaningful */
478 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
479#else
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100480 /* XXX: needs adjustments */
pbrook94a6b542009-04-11 17:15:54 +0000481 code_gen_buffer_size = (unsigned long)(ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000482#endif
bellard26a5f132008-05-28 12:30:31 +0000483 }
484 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
485 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
486 /* The code gen buffer location may have constraints depending on
487 the host cpu and OS */
488#if defined(__linux__)
489 {
490 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000491 void *start = NULL;
492
bellard26a5f132008-05-28 12:30:31 +0000493 flags = MAP_PRIVATE | MAP_ANONYMOUS;
494#if defined(__x86_64__)
495 flags |= MAP_32BIT;
496 /* Cannot map more than that */
497 if (code_gen_buffer_size > (800 * 1024 * 1024))
498 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000499#elif defined(__sparc_v9__)
500 // Map the buffer below 2G, so we can use direct calls and branches
501 flags |= MAP_FIXED;
502 start = (void *) 0x60000000UL;
503 if (code_gen_buffer_size > (512 * 1024 * 1024))
504 code_gen_buffer_size = (512 * 1024 * 1024);
balrog1cb06612008-12-01 02:10:17 +0000505#elif defined(__arm__)
balrog63d41242008-12-01 02:19:41 +0000506 /* Map the buffer below 32M, so we can use direct calls and branches */
balrog1cb06612008-12-01 02:10:17 +0000507 flags |= MAP_FIXED;
508 start = (void *) 0x01000000UL;
509 if (code_gen_buffer_size > 16 * 1024 * 1024)
510 code_gen_buffer_size = 16 * 1024 * 1024;
Richard Hendersoneba0b892010-06-04 12:14:14 -0700511#elif defined(__s390x__)
512 /* Map the buffer so that we can use direct calls and branches. */
513 /* We have a +- 4GB range on the branches; leave some slop. */
514 if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) {
515 code_gen_buffer_size = 3ul * 1024 * 1024 * 1024;
516 }
517 start = (void *)0x90000000UL;
bellard26a5f132008-05-28 12:30:31 +0000518#endif
blueswir1141ac462008-07-26 15:05:57 +0000519 code_gen_buffer = mmap(start, code_gen_buffer_size,
520 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000521 flags, -1, 0);
522 if (code_gen_buffer == MAP_FAILED) {
523 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
524 exit(1);
525 }
526 }
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100527#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguori06e67a82008-09-27 15:32:41 +0000528 {
529 int flags;
530 void *addr = NULL;
531 flags = MAP_PRIVATE | MAP_ANONYMOUS;
532#if defined(__x86_64__)
533 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
534 * 0x40000000 is free */
535 flags |= MAP_FIXED;
536 addr = (void *)0x40000000;
537 /* Cannot map more than that */
538 if (code_gen_buffer_size > (800 * 1024 * 1024))
539 code_gen_buffer_size = (800 * 1024 * 1024);
540#endif
541 code_gen_buffer = mmap(addr, code_gen_buffer_size,
542 PROT_WRITE | PROT_READ | PROT_EXEC,
543 flags, -1, 0);
544 if (code_gen_buffer == MAP_FAILED) {
545 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
546 exit(1);
547 }
548 }
bellard26a5f132008-05-28 12:30:31 +0000549#else
550 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
bellard26a5f132008-05-28 12:30:31 +0000551 map_exec(code_gen_buffer, code_gen_buffer_size);
552#endif
bellard43694152008-05-29 09:35:57 +0000553#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000554 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
555 code_gen_buffer_max_size = code_gen_buffer_size -
Aurelien Jarno239fda32010-06-03 19:29:31 +0200556 (TCG_MAX_OP_SIZE * OPC_MAX_SIZE);
bellard26a5f132008-05-28 12:30:31 +0000557 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
558 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
559}
560
561/* Must be called before using the QEMU cpus. 'tb_size' is the size
562 (in bytes) allocated to the translation buffer. Zero means default
563 size. */
564void cpu_exec_init_all(unsigned long tb_size)
565{
bellard26a5f132008-05-28 12:30:31 +0000566 cpu_gen_init();
567 code_gen_alloc(tb_size);
568 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000569 page_init();
pbrooke2eef172008-06-08 01:09:01 +0000570#if !defined(CONFIG_USER_ONLY)
bellard26a5f132008-05-28 12:30:31 +0000571 io_mem_init();
pbrooke2eef172008-06-08 01:09:01 +0000572#endif
Richard Henderson9002ec72010-05-06 08:50:41 -0700573#if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
574 /* There's no guest base to take into account, so go ahead and
575 initialize the prologue now. */
576 tcg_prologue_init(&tcg_ctx);
577#endif
bellard26a5f132008-05-28 12:30:31 +0000578}
579
pbrook9656f322008-07-01 20:01:19 +0000580#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
581
Juan Quintelae59fb372009-09-29 22:48:21 +0200582static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200583{
584 CPUState *env = opaque;
585
aurel323098dba2009-03-07 21:28:24 +0000586 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
587 version_id is increased. */
588 env->interrupt_request &= ~0x01;
pbrook9656f322008-07-01 20:01:19 +0000589 tlb_flush(env, 1);
590
591 return 0;
592}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200593
594static const VMStateDescription vmstate_cpu_common = {
595 .name = "cpu_common",
596 .version_id = 1,
597 .minimum_version_id = 1,
598 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200599 .post_load = cpu_common_post_load,
600 .fields = (VMStateField []) {
601 VMSTATE_UINT32(halted, CPUState),
602 VMSTATE_UINT32(interrupt_request, CPUState),
603 VMSTATE_END_OF_LIST()
604 }
605};
pbrook9656f322008-07-01 20:01:19 +0000606#endif
607
Glauber Costa950f1472009-06-09 12:15:18 -0400608CPUState *qemu_get_cpu(int cpu)
609{
610 CPUState *env = first_cpu;
611
612 while (env) {
613 if (env->cpu_index == cpu)
614 break;
615 env = env->next_cpu;
616 }
617
618 return env;
619}
620
bellard6a00d602005-11-21 23:25:50 +0000621void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000622{
bellard6a00d602005-11-21 23:25:50 +0000623 CPUState **penv;
624 int cpu_index;
625
pbrookc2764712009-03-07 15:24:59 +0000626#if defined(CONFIG_USER_ONLY)
627 cpu_list_lock();
628#endif
bellard6a00d602005-11-21 23:25:50 +0000629 env->next_cpu = NULL;
630 penv = &first_cpu;
631 cpu_index = 0;
632 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700633 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000634 cpu_index++;
635 }
636 env->cpu_index = cpu_index;
aliguori268a3622009-04-21 22:30:27 +0000637 env->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000638 QTAILQ_INIT(&env->breakpoints);
639 QTAILQ_INIT(&env->watchpoints);
bellard6a00d602005-11-21 23:25:50 +0000640 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000641#if defined(CONFIG_USER_ONLY)
642 cpu_list_unlock();
643#endif
pbrookb3c77242008-06-30 16:31:04 +0000644#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600645 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
646 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000647 cpu_save, cpu_load, env);
648#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000649}
650
bellard9fa3e852004-01-04 18:06:42 +0000651static inline void invalidate_page_bitmap(PageDesc *p)
652{
653 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000654 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000655 p->code_bitmap = NULL;
656 }
657 p->code_write_count = 0;
658}
659
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800660/* Set to NULL all the 'first_tb' fields in all PageDescs. */
661
662static void page_flush_tb_1 (int level, void **lp)
663{
664 int i;
665
666 if (*lp == NULL) {
667 return;
668 }
669 if (level == 0) {
670 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000671 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800672 pd[i].first_tb = NULL;
673 invalidate_page_bitmap(pd + i);
674 }
675 } else {
676 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000677 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800678 page_flush_tb_1 (level - 1, pp + i);
679 }
680 }
681}
682
bellardfd6ce8f2003-05-14 19:00:11 +0000683static void page_flush_tb(void)
684{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800685 int i;
686 for (i = 0; i < V_L1_SIZE; i++) {
687 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
bellardfd6ce8f2003-05-14 19:00:11 +0000688 }
689}
690
691/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000692/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000693void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000694{
bellard6a00d602005-11-21 23:25:50 +0000695 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000696#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000697 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
698 (unsigned long)(code_gen_ptr - code_gen_buffer),
699 nb_tbs, nb_tbs > 0 ?
700 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000701#endif
bellard26a5f132008-05-28 12:30:31 +0000702 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000703 cpu_abort(env1, "Internal error: code buffer overflow\n");
704
bellardfd6ce8f2003-05-14 19:00:11 +0000705 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000706
bellard6a00d602005-11-21 23:25:50 +0000707 for(env = first_cpu; env != NULL; env = env->next_cpu) {
708 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
709 }
bellard9fa3e852004-01-04 18:06:42 +0000710
bellard8a8a6082004-10-03 13:36:49 +0000711 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000712 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000713
bellardfd6ce8f2003-05-14 19:00:11 +0000714 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000715 /* XXX: flush processor icache at this point if cache flush is
716 expensive */
bellarde3db7222005-01-26 22:00:47 +0000717 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000718}
719
720#ifdef DEBUG_TB_CHECK
721
j_mayerbc98a7e2007-04-04 07:55:12 +0000722static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000723{
724 TranslationBlock *tb;
725 int i;
726 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000727 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
728 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000729 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
730 address >= tb->pc + tb->size)) {
Blue Swirl0bf9e312009-07-20 17:19:25 +0000731 printf("ERROR invalidate: address=" TARGET_FMT_lx
732 " PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000733 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000734 }
735 }
736 }
737}
738
739/* verify that all the pages have correct rights for code */
740static void tb_page_check(void)
741{
742 TranslationBlock *tb;
743 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000744
pbrook99773bd2006-04-16 15:14:59 +0000745 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
746 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000747 flags1 = page_get_flags(tb->pc);
748 flags2 = page_get_flags(tb->pc + tb->size - 1);
749 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
750 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000751 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000752 }
753 }
754 }
755}
756
757#endif
758
759/* invalidate one TB */
760static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
761 int next_offset)
762{
763 TranslationBlock *tb1;
764 for(;;) {
765 tb1 = *ptb;
766 if (tb1 == tb) {
767 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
768 break;
769 }
770 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
771 }
772}
773
bellard9fa3e852004-01-04 18:06:42 +0000774static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
775{
776 TranslationBlock *tb1;
777 unsigned int n1;
778
779 for(;;) {
780 tb1 = *ptb;
781 n1 = (long)tb1 & 3;
782 tb1 = (TranslationBlock *)((long)tb1 & ~3);
783 if (tb1 == tb) {
784 *ptb = tb1->page_next[n1];
785 break;
786 }
787 ptb = &tb1->page_next[n1];
788 }
789}
790
bellardd4e81642003-05-25 16:46:15 +0000791static inline void tb_jmp_remove(TranslationBlock *tb, int n)
792{
793 TranslationBlock *tb1, **ptb;
794 unsigned int n1;
795
796 ptb = &tb->jmp_next[n];
797 tb1 = *ptb;
798 if (tb1) {
799 /* find tb(n) in circular list */
800 for(;;) {
801 tb1 = *ptb;
802 n1 = (long)tb1 & 3;
803 tb1 = (TranslationBlock *)((long)tb1 & ~3);
804 if (n1 == n && tb1 == tb)
805 break;
806 if (n1 == 2) {
807 ptb = &tb1->jmp_first;
808 } else {
809 ptb = &tb1->jmp_next[n1];
810 }
811 }
812 /* now we can suppress tb(n) from the list */
813 *ptb = tb->jmp_next[n];
814
815 tb->jmp_next[n] = NULL;
816 }
817}
818
819/* reset the jump entry 'n' of a TB so that it is not chained to
820 another TB */
821static inline void tb_reset_jump(TranslationBlock *tb, int n)
822{
823 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
824}
825
Paul Brook41c1b1c2010-03-12 16:54:58 +0000826void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000827{
bellard6a00d602005-11-21 23:25:50 +0000828 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000829 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000830 unsigned int h, n1;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000831 tb_page_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000832 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000833
bellard9fa3e852004-01-04 18:06:42 +0000834 /* remove the TB from the hash list */
835 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
836 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000837 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000838 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000839
bellard9fa3e852004-01-04 18:06:42 +0000840 /* remove the TB from the page list */
841 if (tb->page_addr[0] != page_addr) {
842 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
843 tb_page_remove(&p->first_tb, tb);
844 invalidate_page_bitmap(p);
845 }
846 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
847 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
848 tb_page_remove(&p->first_tb, tb);
849 invalidate_page_bitmap(p);
850 }
851
bellard8a40a182005-11-20 10:35:40 +0000852 tb_invalidated_flag = 1;
853
854 /* remove the TB from the hash list */
855 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000856 for(env = first_cpu; env != NULL; env = env->next_cpu) {
857 if (env->tb_jmp_cache[h] == tb)
858 env->tb_jmp_cache[h] = NULL;
859 }
bellard8a40a182005-11-20 10:35:40 +0000860
861 /* suppress this TB from the two jump lists */
862 tb_jmp_remove(tb, 0);
863 tb_jmp_remove(tb, 1);
864
865 /* suppress any remaining jumps to this TB */
866 tb1 = tb->jmp_first;
867 for(;;) {
868 n1 = (long)tb1 & 3;
869 if (n1 == 2)
870 break;
871 tb1 = (TranslationBlock *)((long)tb1 & ~3);
872 tb2 = tb1->jmp_next[n1];
873 tb_reset_jump(tb1, n1);
874 tb1->jmp_next[n1] = NULL;
875 tb1 = tb2;
876 }
877 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
878
bellarde3db7222005-01-26 22:00:47 +0000879 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000880}
881
882static inline void set_bits(uint8_t *tab, int start, int len)
883{
884 int end, mask, end1;
885
886 end = start + len;
887 tab += start >> 3;
888 mask = 0xff << (start & 7);
889 if ((start & ~7) == (end & ~7)) {
890 if (start < end) {
891 mask &= ~(0xff << (end & 7));
892 *tab |= mask;
893 }
894 } else {
895 *tab++ |= mask;
896 start = (start + 8) & ~7;
897 end1 = end & ~7;
898 while (start < end1) {
899 *tab++ = 0xff;
900 start += 8;
901 }
902 if (start < end) {
903 mask = ~(0xff << (end & 7));
904 *tab |= mask;
905 }
906 }
907}
908
909static void build_page_bitmap(PageDesc *p)
910{
911 int n, tb_start, tb_end;
912 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000913
pbrookb2a70812008-06-09 13:57:23 +0000914 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000915
916 tb = p->first_tb;
917 while (tb != NULL) {
918 n = (long)tb & 3;
919 tb = (TranslationBlock *)((long)tb & ~3);
920 /* NOTE: this is subtle as a TB may span two physical pages */
921 if (n == 0) {
922 /* NOTE: tb_end may be after the end of the page, but
923 it is not a problem */
924 tb_start = tb->pc & ~TARGET_PAGE_MASK;
925 tb_end = tb_start + tb->size;
926 if (tb_end > TARGET_PAGE_SIZE)
927 tb_end = TARGET_PAGE_SIZE;
928 } else {
929 tb_start = 0;
930 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
931 }
932 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
933 tb = tb->page_next[n];
934 }
935}
936
pbrook2e70f6e2008-06-29 01:03:05 +0000937TranslationBlock *tb_gen_code(CPUState *env,
938 target_ulong pc, target_ulong cs_base,
939 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000940{
941 TranslationBlock *tb;
942 uint8_t *tc_ptr;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000943 tb_page_addr_t phys_pc, phys_page2;
944 target_ulong virt_page2;
bellardd720b932004-04-25 17:57:43 +0000945 int code_gen_size;
946
Paul Brook41c1b1c2010-03-12 16:54:58 +0000947 phys_pc = get_page_addr_code(env, pc);
bellardc27004e2005-01-03 23:35:10 +0000948 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000949 if (!tb) {
950 /* flush must be done */
951 tb_flush(env);
952 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000953 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000954 /* Don't forget to invalidate previous TB info. */
955 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000956 }
957 tc_ptr = code_gen_ptr;
958 tb->tc_ptr = tc_ptr;
959 tb->cs_base = cs_base;
960 tb->flags = flags;
961 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000962 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000963 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 +0000964
bellardd720b932004-04-25 17:57:43 +0000965 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000966 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000967 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000968 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
Paul Brook41c1b1c2010-03-12 16:54:58 +0000969 phys_page2 = get_page_addr_code(env, virt_page2);
bellardd720b932004-04-25 17:57:43 +0000970 }
Paul Brook41c1b1c2010-03-12 16:54:58 +0000971 tb_link_page(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +0000972 return tb;
bellardd720b932004-04-25 17:57:43 +0000973}
ths3b46e622007-09-17 08:09:54 +0000974
bellard9fa3e852004-01-04 18:06:42 +0000975/* invalidate all TBs which intersect with the target physical page
976 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000977 the same physical page. 'is_cpu_write_access' should be true if called
978 from a real cpu write access: the virtual CPU will exit the current
979 TB if code is modified inside this TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +0000980void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000981 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000982{
aliguori6b917542008-11-18 19:46:41 +0000983 TranslationBlock *tb, *tb_next, *saved_tb;
bellardd720b932004-04-25 17:57:43 +0000984 CPUState *env = cpu_single_env;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000985 tb_page_addr_t tb_start, tb_end;
aliguori6b917542008-11-18 19:46:41 +0000986 PageDesc *p;
987 int n;
988#ifdef TARGET_HAS_PRECISE_SMC
989 int current_tb_not_found = is_cpu_write_access;
990 TranslationBlock *current_tb = NULL;
991 int current_tb_modified = 0;
992 target_ulong current_pc = 0;
993 target_ulong current_cs_base = 0;
994 int current_flags = 0;
995#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000996
997 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000998 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000999 return;
ths5fafdf22007-09-16 21:08:06 +00001000 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +00001001 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1002 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +00001003 /* build code bitmap */
1004 build_page_bitmap(p);
1005 }
1006
1007 /* we remove all the TBs in the range [start, end[ */
1008 /* XXX: see if in some cases it could be faster to invalidate all the code */
1009 tb = p->first_tb;
1010 while (tb != NULL) {
1011 n = (long)tb & 3;
1012 tb = (TranslationBlock *)((long)tb & ~3);
1013 tb_next = tb->page_next[n];
1014 /* NOTE: this is subtle as a TB may span two physical pages */
1015 if (n == 0) {
1016 /* NOTE: tb_end may be after the end of the page, but
1017 it is not a problem */
1018 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1019 tb_end = tb_start + tb->size;
1020 } else {
1021 tb_start = tb->page_addr[1];
1022 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1023 }
1024 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +00001025#ifdef TARGET_HAS_PRECISE_SMC
1026 if (current_tb_not_found) {
1027 current_tb_not_found = 0;
1028 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001029 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +00001030 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +00001031 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +00001032 }
1033 }
1034 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001035 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001036 /* If we are modifying the current TB, we must stop
1037 its execution. We could be more precise by checking
1038 that the modification is after the current PC, but it
1039 would require a specialized function to partially
1040 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001041
bellardd720b932004-04-25 17:57:43 +00001042 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +00001043 cpu_restore_state(current_tb, env,
pbrook2e70f6e2008-06-29 01:03:05 +00001044 env->mem_io_pc, NULL);
aliguori6b917542008-11-18 19:46:41 +00001045 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1046 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001047 }
1048#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +00001049 /* we need to do that to handle the case where a signal
1050 occurs while doing tb_phys_invalidate() */
1051 saved_tb = NULL;
1052 if (env) {
1053 saved_tb = env->current_tb;
1054 env->current_tb = NULL;
1055 }
bellard9fa3e852004-01-04 18:06:42 +00001056 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +00001057 if (env) {
1058 env->current_tb = saved_tb;
1059 if (env->interrupt_request && env->current_tb)
1060 cpu_interrupt(env, env->interrupt_request);
1061 }
bellard9fa3e852004-01-04 18:06:42 +00001062 }
1063 tb = tb_next;
1064 }
1065#if !defined(CONFIG_USER_ONLY)
1066 /* if no code remaining, no need to continue to use slow writes */
1067 if (!p->first_tb) {
1068 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +00001069 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +00001070 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +00001071 }
1072 }
1073#endif
1074#ifdef TARGET_HAS_PRECISE_SMC
1075 if (current_tb_modified) {
1076 /* we generate a block containing just the instruction
1077 modifying the memory. It will ensure that it cannot modify
1078 itself */
bellardea1c1802004-06-14 18:56:36 +00001079 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001080 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001081 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001082 }
1083#endif
1084}
1085
1086/* len must be <= 8 and start must be a multiple of len */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001087static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +00001088{
1089 PageDesc *p;
1090 int offset, b;
bellard59817cc2004-02-16 22:01:13 +00001091#if 0
bellarda4193c82004-06-03 14:01:43 +00001092 if (1) {
aliguori93fcfe32009-01-15 22:34:14 +00001093 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1094 cpu_single_env->mem_io_vaddr, len,
1095 cpu_single_env->eip,
1096 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
bellard59817cc2004-02-16 22:01:13 +00001097 }
1098#endif
bellard9fa3e852004-01-04 18:06:42 +00001099 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001100 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001101 return;
1102 if (p->code_bitmap) {
1103 offset = start & ~TARGET_PAGE_MASK;
1104 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1105 if (b & ((1 << len) - 1))
1106 goto do_invalidate;
1107 } else {
1108 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001109 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001110 }
1111}
1112
bellard9fa3e852004-01-04 18:06:42 +00001113#if !defined(CONFIG_SOFTMMU)
Paul Brook41c1b1c2010-03-12 16:54:58 +00001114static void tb_invalidate_phys_page(tb_page_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001115 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001116{
aliguori6b917542008-11-18 19:46:41 +00001117 TranslationBlock *tb;
bellard9fa3e852004-01-04 18:06:42 +00001118 PageDesc *p;
aliguori6b917542008-11-18 19:46:41 +00001119 int n;
bellardd720b932004-04-25 17:57:43 +00001120#ifdef TARGET_HAS_PRECISE_SMC
aliguori6b917542008-11-18 19:46:41 +00001121 TranslationBlock *current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001122 CPUState *env = cpu_single_env;
aliguori6b917542008-11-18 19:46:41 +00001123 int current_tb_modified = 0;
1124 target_ulong current_pc = 0;
1125 target_ulong current_cs_base = 0;
1126 int current_flags = 0;
bellardd720b932004-04-25 17:57:43 +00001127#endif
bellard9fa3e852004-01-04 18:06:42 +00001128
1129 addr &= TARGET_PAGE_MASK;
1130 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001131 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001132 return;
1133 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001134#ifdef TARGET_HAS_PRECISE_SMC
1135 if (tb && pc != 0) {
1136 current_tb = tb_find_pc(pc);
1137 }
1138#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001139 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001140 n = (long)tb & 3;
1141 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001142#ifdef TARGET_HAS_PRECISE_SMC
1143 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001144 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001145 /* If we are modifying the current TB, we must stop
1146 its execution. We could be more precise by checking
1147 that the modification is after the current PC, but it
1148 would require a specialized function to partially
1149 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001150
bellardd720b932004-04-25 17:57:43 +00001151 current_tb_modified = 1;
1152 cpu_restore_state(current_tb, env, pc, puc);
aliguori6b917542008-11-18 19:46:41 +00001153 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1154 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001155 }
1156#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001157 tb_phys_invalidate(tb, addr);
1158 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001159 }
1160 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001161#ifdef TARGET_HAS_PRECISE_SMC
1162 if (current_tb_modified) {
1163 /* we generate a block containing just the instruction
1164 modifying the memory. It will ensure that it cannot modify
1165 itself */
bellardea1c1802004-06-14 18:56:36 +00001166 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001167 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001168 cpu_resume_from_signal(env, puc);
1169 }
1170#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001171}
bellard9fa3e852004-01-04 18:06:42 +00001172#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001173
1174/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001175static inline void tb_alloc_page(TranslationBlock *tb,
Paul Brook41c1b1c2010-03-12 16:54:58 +00001176 unsigned int n, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001177{
1178 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +00001179 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001180
bellard9fa3e852004-01-04 18:06:42 +00001181 tb->page_addr[n] = page_addr;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001182 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
bellard9fa3e852004-01-04 18:06:42 +00001183 tb->page_next[n] = p->first_tb;
1184 last_first_tb = p->first_tb;
1185 p->first_tb = (TranslationBlock *)((long)tb | n);
1186 invalidate_page_bitmap(p);
1187
bellard107db442004-06-22 18:48:46 +00001188#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001189
bellard9fa3e852004-01-04 18:06:42 +00001190#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001191 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001192 target_ulong addr;
1193 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001194 int prot;
1195
bellardfd6ce8f2003-05-14 19:00:11 +00001196 /* force the host page as non writable (writes will have a
1197 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001198 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001199 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001200 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1201 addr += TARGET_PAGE_SIZE) {
1202
1203 p2 = page_find (addr >> TARGET_PAGE_BITS);
1204 if (!p2)
1205 continue;
1206 prot |= p2->flags;
1207 p2->flags &= ~PAGE_WRITE;
pbrook53a59602006-03-25 19:31:22 +00001208 }
ths5fafdf22007-09-16 21:08:06 +00001209 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001210 (prot & PAGE_BITS) & ~PAGE_WRITE);
1211#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001212 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001213 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001214#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001215 }
bellard9fa3e852004-01-04 18:06:42 +00001216#else
1217 /* if some code is already present, then the pages are already
1218 protected. So we handle the case where only the first TB is
1219 allocated in a physical page */
1220 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001221 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001222 }
1223#endif
bellardd720b932004-04-25 17:57:43 +00001224
1225#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001226}
1227
1228/* Allocate a new translation block. Flush the translation buffer if
1229 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001230TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001231{
1232 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001233
bellard26a5f132008-05-28 12:30:31 +00001234 if (nb_tbs >= code_gen_max_blocks ||
1235 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001236 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001237 tb = &tbs[nb_tbs++];
1238 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001239 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001240 return tb;
1241}
1242
pbrook2e70f6e2008-06-29 01:03:05 +00001243void tb_free(TranslationBlock *tb)
1244{
thsbf20dc02008-06-30 17:22:19 +00001245 /* In practice this is mostly used for single use temporary TB
pbrook2e70f6e2008-06-29 01:03:05 +00001246 Ignore the hard cases and just back up if this TB happens to
1247 be the last one generated. */
1248 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1249 code_gen_ptr = tb->tc_ptr;
1250 nb_tbs--;
1251 }
1252}
1253
bellard9fa3e852004-01-04 18:06:42 +00001254/* add a new TB and link it to the physical page tables. phys_page2 is
1255 (-1) to indicate that only one page contains the TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001256void tb_link_page(TranslationBlock *tb,
1257 tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001258{
bellard9fa3e852004-01-04 18:06:42 +00001259 unsigned int h;
1260 TranslationBlock **ptb;
1261
pbrookc8a706f2008-06-02 16:16:42 +00001262 /* Grab the mmap lock to stop another thread invalidating this TB
1263 before we are done. */
1264 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001265 /* add in the physical hash table */
1266 h = tb_phys_hash_func(phys_pc);
1267 ptb = &tb_phys_hash[h];
1268 tb->phys_hash_next = *ptb;
1269 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001270
1271 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001272 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1273 if (phys_page2 != -1)
1274 tb_alloc_page(tb, 1, phys_page2);
1275 else
1276 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001277
bellardd4e81642003-05-25 16:46:15 +00001278 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1279 tb->jmp_next[0] = NULL;
1280 tb->jmp_next[1] = NULL;
1281
1282 /* init original jump addresses */
1283 if (tb->tb_next_offset[0] != 0xffff)
1284 tb_reset_jump(tb, 0);
1285 if (tb->tb_next_offset[1] != 0xffff)
1286 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001287
1288#ifdef DEBUG_TB_CHECK
1289 tb_page_check();
1290#endif
pbrookc8a706f2008-06-02 16:16:42 +00001291 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001292}
1293
bellarda513fe12003-05-27 23:29:48 +00001294/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1295 tb[1].tc_ptr. Return NULL if not found */
1296TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1297{
1298 int m_min, m_max, m;
1299 unsigned long v;
1300 TranslationBlock *tb;
1301
1302 if (nb_tbs <= 0)
1303 return NULL;
1304 if (tc_ptr < (unsigned long)code_gen_buffer ||
1305 tc_ptr >= (unsigned long)code_gen_ptr)
1306 return NULL;
1307 /* binary search (cf Knuth) */
1308 m_min = 0;
1309 m_max = nb_tbs - 1;
1310 while (m_min <= m_max) {
1311 m = (m_min + m_max) >> 1;
1312 tb = &tbs[m];
1313 v = (unsigned long)tb->tc_ptr;
1314 if (v == tc_ptr)
1315 return tb;
1316 else if (tc_ptr < v) {
1317 m_max = m - 1;
1318 } else {
1319 m_min = m + 1;
1320 }
ths5fafdf22007-09-16 21:08:06 +00001321 }
bellarda513fe12003-05-27 23:29:48 +00001322 return &tbs[m_max];
1323}
bellard75012672003-06-21 13:11:07 +00001324
bellardea041c02003-06-25 16:16:50 +00001325static void tb_reset_jump_recursive(TranslationBlock *tb);
1326
1327static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1328{
1329 TranslationBlock *tb1, *tb_next, **ptb;
1330 unsigned int n1;
1331
1332 tb1 = tb->jmp_next[n];
1333 if (tb1 != NULL) {
1334 /* find head of list */
1335 for(;;) {
1336 n1 = (long)tb1 & 3;
1337 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1338 if (n1 == 2)
1339 break;
1340 tb1 = tb1->jmp_next[n1];
1341 }
1342 /* we are now sure now that tb jumps to tb1 */
1343 tb_next = tb1;
1344
1345 /* remove tb from the jmp_first list */
1346 ptb = &tb_next->jmp_first;
1347 for(;;) {
1348 tb1 = *ptb;
1349 n1 = (long)tb1 & 3;
1350 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1351 if (n1 == n && tb1 == tb)
1352 break;
1353 ptb = &tb1->jmp_next[n1];
1354 }
1355 *ptb = tb->jmp_next[n];
1356 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001357
bellardea041c02003-06-25 16:16:50 +00001358 /* suppress the jump to next tb in generated code */
1359 tb_reset_jump(tb, n);
1360
bellard01243112004-01-04 15:48:17 +00001361 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001362 tb_reset_jump_recursive(tb_next);
1363 }
1364}
1365
1366static void tb_reset_jump_recursive(TranslationBlock *tb)
1367{
1368 tb_reset_jump_recursive2(tb, 0);
1369 tb_reset_jump_recursive2(tb, 1);
1370}
1371
bellard1fddef42005-04-17 19:16:13 +00001372#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +00001373#if defined(CONFIG_USER_ONLY)
1374static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1375{
1376 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1377}
1378#else
bellardd720b932004-04-25 17:57:43 +00001379static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1380{
Anthony Liguoric227f092009-10-01 16:12:16 -05001381 target_phys_addr_t addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00001382 target_ulong pd;
Anthony Liguoric227f092009-10-01 16:12:16 -05001383 ram_addr_t ram_addr;
pbrookc2f07f82006-04-08 17:14:56 +00001384 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001385
pbrookc2f07f82006-04-08 17:14:56 +00001386 addr = cpu_get_phys_page_debug(env, pc);
1387 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1388 if (!p) {
1389 pd = IO_MEM_UNASSIGNED;
1390 } else {
1391 pd = p->phys_offset;
1392 }
1393 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001394 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001395}
bellardc27004e2005-01-03 23:35:10 +00001396#endif
Paul Brook94df27f2010-02-28 23:47:45 +00001397#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +00001398
Paul Brookc527ee82010-03-01 03:31:14 +00001399#if defined(CONFIG_USER_ONLY)
1400void cpu_watchpoint_remove_all(CPUState *env, int mask)
1401
1402{
1403}
1404
1405int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1406 int flags, CPUWatchpoint **watchpoint)
1407{
1408 return -ENOSYS;
1409}
1410#else
pbrook6658ffb2007-03-16 23:58:11 +00001411/* Add a watchpoint. */
aliguoria1d1bb32008-11-18 20:07:32 +00001412int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1413 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +00001414{
aliguorib4051332008-11-18 20:14:20 +00001415 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +00001416 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001417
aliguorib4051332008-11-18 20:14:20 +00001418 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1419 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1420 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1421 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1422 return -EINVAL;
1423 }
aliguoria1d1bb32008-11-18 20:07:32 +00001424 wp = qemu_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +00001425
aliguoria1d1bb32008-11-18 20:07:32 +00001426 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +00001427 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +00001428 wp->flags = flags;
1429
aliguori2dc9f412008-11-18 20:56:59 +00001430 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001431 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001432 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001433 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001434 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001435
pbrook6658ffb2007-03-16 23:58:11 +00001436 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +00001437
1438 if (watchpoint)
1439 *watchpoint = wp;
1440 return 0;
pbrook6658ffb2007-03-16 23:58:11 +00001441}
1442
aliguoria1d1bb32008-11-18 20:07:32 +00001443/* Remove a specific watchpoint. */
1444int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1445 int flags)
pbrook6658ffb2007-03-16 23:58:11 +00001446{
aliguorib4051332008-11-18 20:14:20 +00001447 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +00001448 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001449
Blue Swirl72cf2d42009-09-12 07:36:22 +00001450 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001451 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +00001452 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +00001453 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +00001454 return 0;
1455 }
1456 }
aliguoria1d1bb32008-11-18 20:07:32 +00001457 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +00001458}
1459
aliguoria1d1bb32008-11-18 20:07:32 +00001460/* Remove a specific watchpoint by reference. */
1461void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1462{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001463 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +00001464
aliguoria1d1bb32008-11-18 20:07:32 +00001465 tlb_flush_page(env, watchpoint->vaddr);
1466
1467 qemu_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +00001468}
1469
aliguoria1d1bb32008-11-18 20:07:32 +00001470/* Remove all matching watchpoints. */
1471void cpu_watchpoint_remove_all(CPUState *env, int mask)
1472{
aliguoric0ce9982008-11-25 22:13:57 +00001473 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001474
Blue Swirl72cf2d42009-09-12 07:36:22 +00001475 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001476 if (wp->flags & mask)
1477 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +00001478 }
aliguoria1d1bb32008-11-18 20:07:32 +00001479}
Paul Brookc527ee82010-03-01 03:31:14 +00001480#endif
aliguoria1d1bb32008-11-18 20:07:32 +00001481
1482/* Add a breakpoint. */
1483int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1484 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001485{
bellard1fddef42005-04-17 19:16:13 +00001486#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001487 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +00001488
aliguoria1d1bb32008-11-18 20:07:32 +00001489 bp = qemu_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +00001490
1491 bp->pc = pc;
1492 bp->flags = flags;
1493
aliguori2dc9f412008-11-18 20:56:59 +00001494 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001495 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001496 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001497 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001498 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001499
1500 breakpoint_invalidate(env, pc);
1501
1502 if (breakpoint)
1503 *breakpoint = bp;
1504 return 0;
1505#else
1506 return -ENOSYS;
1507#endif
1508}
1509
1510/* Remove a specific breakpoint. */
1511int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1512{
1513#if defined(TARGET_HAS_ICE)
1514 CPUBreakpoint *bp;
1515
Blue Swirl72cf2d42009-09-12 07:36:22 +00001516 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00001517 if (bp->pc == pc && bp->flags == flags) {
1518 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +00001519 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +00001520 }
bellard4c3a88a2003-07-26 12:06:08 +00001521 }
aliguoria1d1bb32008-11-18 20:07:32 +00001522 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +00001523#else
aliguoria1d1bb32008-11-18 20:07:32 +00001524 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +00001525#endif
1526}
1527
aliguoria1d1bb32008-11-18 20:07:32 +00001528/* Remove a specific breakpoint by reference. */
1529void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001530{
bellard1fddef42005-04-17 19:16:13 +00001531#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001532 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +00001533
aliguoria1d1bb32008-11-18 20:07:32 +00001534 breakpoint_invalidate(env, breakpoint->pc);
1535
1536 qemu_free(breakpoint);
1537#endif
1538}
1539
1540/* Remove all matching breakpoints. */
1541void cpu_breakpoint_remove_all(CPUState *env, int mask)
1542{
1543#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001544 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001545
Blue Swirl72cf2d42009-09-12 07:36:22 +00001546 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001547 if (bp->flags & mask)
1548 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +00001549 }
bellard4c3a88a2003-07-26 12:06:08 +00001550#endif
1551}
1552
bellardc33a3462003-07-29 20:50:33 +00001553/* enable or disable single step mode. EXCP_DEBUG is returned by the
1554 CPU loop after each instruction */
1555void cpu_single_step(CPUState *env, int enabled)
1556{
bellard1fddef42005-04-17 19:16:13 +00001557#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001558 if (env->singlestep_enabled != enabled) {
1559 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +00001560 if (kvm_enabled())
1561 kvm_update_guest_debug(env, 0);
1562 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001563 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +00001564 /* XXX: only flush what is necessary */
1565 tb_flush(env);
1566 }
bellardc33a3462003-07-29 20:50:33 +00001567 }
1568#endif
1569}
1570
bellard34865132003-10-05 14:28:56 +00001571/* enable or disable low levels log */
1572void cpu_set_log(int log_flags)
1573{
1574 loglevel = log_flags;
1575 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001576 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001577 if (!logfile) {
1578 perror(logfilename);
1579 _exit(1);
1580 }
bellard9fa3e852004-01-04 18:06:42 +00001581#if !defined(CONFIG_SOFTMMU)
1582 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1583 {
blueswir1b55266b2008-09-20 08:07:15 +00001584 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001585 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1586 }
Filip Navarabf65f532009-07-27 10:02:04 -05001587#elif !defined(_WIN32)
1588 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellard34865132003-10-05 14:28:56 +00001589 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001590#endif
pbrooke735b912007-06-30 13:53:24 +00001591 log_append = 1;
1592 }
1593 if (!loglevel && logfile) {
1594 fclose(logfile);
1595 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001596 }
1597}
1598
1599void cpu_set_log_filename(const char *filename)
1600{
1601 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001602 if (logfile) {
1603 fclose(logfile);
1604 logfile = NULL;
1605 }
1606 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001607}
bellardc33a3462003-07-29 20:50:33 +00001608
aurel323098dba2009-03-07 21:28:24 +00001609static void cpu_unlink_tb(CPUState *env)
bellardea041c02003-06-25 16:16:50 +00001610{
pbrookd5975362008-06-07 20:50:51 +00001611 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1612 problem and hope the cpu will stop of its own accord. For userspace
1613 emulation this often isn't actually as bad as it sounds. Often
1614 signals are used primarily to interrupt blocking syscalls. */
aurel323098dba2009-03-07 21:28:24 +00001615 TranslationBlock *tb;
Anthony Liguoric227f092009-10-01 16:12:16 -05001616 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
aurel323098dba2009-03-07 21:28:24 +00001617
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001618 spin_lock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001619 tb = env->current_tb;
1620 /* if the cpu is currently executing code, we must unlink it and
1621 all the potentially executing TB */
Riku Voipiof76cfe52009-12-04 15:16:30 +02001622 if (tb) {
aurel323098dba2009-03-07 21:28:24 +00001623 env->current_tb = NULL;
1624 tb_reset_jump_recursive(tb);
aurel323098dba2009-03-07 21:28:24 +00001625 }
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001626 spin_unlock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001627}
1628
1629/* mask must never be zero, except for A20 change call */
1630void cpu_interrupt(CPUState *env, int mask)
1631{
1632 int old_mask;
1633
1634 old_mask = env->interrupt_request;
1635 env->interrupt_request |= mask;
1636
aliguori8edac962009-04-24 18:03:45 +00001637#ifndef CONFIG_USER_ONLY
1638 /*
1639 * If called from iothread context, wake the target cpu in
1640 * case its halted.
1641 */
1642 if (!qemu_cpu_self(env)) {
1643 qemu_cpu_kick(env);
1644 return;
1645 }
1646#endif
1647
pbrook2e70f6e2008-06-29 01:03:05 +00001648 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001649 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001650#ifndef CONFIG_USER_ONLY
pbrook2e70f6e2008-06-29 01:03:05 +00001651 if (!can_do_io(env)
aurel32be214e62009-03-06 21:48:00 +00001652 && (mask & ~old_mask) != 0) {
pbrook2e70f6e2008-06-29 01:03:05 +00001653 cpu_abort(env, "Raised interrupt while not in I/O function");
1654 }
1655#endif
1656 } else {
aurel323098dba2009-03-07 21:28:24 +00001657 cpu_unlink_tb(env);
bellardea041c02003-06-25 16:16:50 +00001658 }
1659}
1660
bellardb54ad042004-05-20 13:42:52 +00001661void cpu_reset_interrupt(CPUState *env, int mask)
1662{
1663 env->interrupt_request &= ~mask;
1664}
1665
aurel323098dba2009-03-07 21:28:24 +00001666void cpu_exit(CPUState *env)
1667{
1668 env->exit_request = 1;
1669 cpu_unlink_tb(env);
1670}
1671
blueswir1c7cd6a32008-10-02 18:27:46 +00001672const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001673 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001674 "show generated host assembly code for each compiled TB" },
1675 { CPU_LOG_TB_IN_ASM, "in_asm",
1676 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001677 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001678 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001679 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001680 "show micro ops "
1681#ifdef TARGET_I386
1682 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001683#endif
blueswir1e01a1152008-03-14 17:37:11 +00001684 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001685 { CPU_LOG_INT, "int",
1686 "show interrupts/exceptions in short format" },
1687 { CPU_LOG_EXEC, "exec",
1688 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001689 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001690 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001691#ifdef TARGET_I386
1692 { CPU_LOG_PCALL, "pcall",
1693 "show protected mode far calls/returns/exceptions" },
aliguorieca1bdf2009-01-26 19:54:31 +00001694 { CPU_LOG_RESET, "cpu_reset",
1695 "show CPU state before CPU resets" },
bellardf193c792004-03-21 17:06:25 +00001696#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001697#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001698 { CPU_LOG_IOPORT, "ioport",
1699 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001700#endif
bellardf193c792004-03-21 17:06:25 +00001701 { 0, NULL, NULL },
1702};
1703
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001704#ifndef CONFIG_USER_ONLY
1705static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
1706 = QLIST_HEAD_INITIALIZER(memory_client_list);
1707
1708static void cpu_notify_set_memory(target_phys_addr_t start_addr,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001709 ram_addr_t size,
1710 ram_addr_t phys_offset)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001711{
1712 CPUPhysMemoryClient *client;
1713 QLIST_FOREACH(client, &memory_client_list, list) {
1714 client->set_memory(client, start_addr, size, phys_offset);
1715 }
1716}
1717
1718static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001719 target_phys_addr_t end)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001720{
1721 CPUPhysMemoryClient *client;
1722 QLIST_FOREACH(client, &memory_client_list, list) {
1723 int r = client->sync_dirty_bitmap(client, start, end);
1724 if (r < 0)
1725 return r;
1726 }
1727 return 0;
1728}
1729
1730static int cpu_notify_migration_log(int enable)
1731{
1732 CPUPhysMemoryClient *client;
1733 QLIST_FOREACH(client, &memory_client_list, list) {
1734 int r = client->migration_log(client, enable);
1735 if (r < 0)
1736 return r;
1737 }
1738 return 0;
1739}
1740
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001741static void phys_page_for_each_1(CPUPhysMemoryClient *client,
1742 int level, void **lp)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001743{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001744 int i;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001745
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001746 if (*lp == NULL) {
1747 return;
1748 }
1749 if (level == 0) {
1750 PhysPageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00001751 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001752 if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
1753 client->set_memory(client, pd[i].region_offset,
1754 TARGET_PAGE_SIZE, pd[i].phys_offset);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001755 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001756 }
1757 } else {
1758 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00001759 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001760 phys_page_for_each_1(client, level - 1, pp + i);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001761 }
1762 }
1763}
1764
1765static void phys_page_for_each(CPUPhysMemoryClient *client)
1766{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001767 int i;
1768 for (i = 0; i < P_L1_SIZE; ++i) {
1769 phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
1770 l1_phys_map + 1);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001771 }
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001772}
1773
1774void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
1775{
1776 QLIST_INSERT_HEAD(&memory_client_list, client, list);
1777 phys_page_for_each(client);
1778}
1779
1780void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
1781{
1782 QLIST_REMOVE(client, list);
1783}
1784#endif
1785
bellardf193c792004-03-21 17:06:25 +00001786static int cmp1(const char *s1, int n, const char *s2)
1787{
1788 if (strlen(s2) != n)
1789 return 0;
1790 return memcmp(s1, s2, n) == 0;
1791}
ths3b46e622007-09-17 08:09:54 +00001792
bellardf193c792004-03-21 17:06:25 +00001793/* takes a comma separated list of log masks. Return 0 if error. */
1794int cpu_str_to_log_mask(const char *str)
1795{
blueswir1c7cd6a32008-10-02 18:27:46 +00001796 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001797 int mask;
1798 const char *p, *p1;
1799
1800 p = str;
1801 mask = 0;
1802 for(;;) {
1803 p1 = strchr(p, ',');
1804 if (!p1)
1805 p1 = p + strlen(p);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001806 if(cmp1(p,p1-p,"all")) {
1807 for(item = cpu_log_items; item->mask != 0; item++) {
1808 mask |= item->mask;
1809 }
1810 } else {
1811 for(item = cpu_log_items; item->mask != 0; item++) {
1812 if (cmp1(p, p1 - p, item->name))
1813 goto found;
1814 }
1815 return 0;
bellardf193c792004-03-21 17:06:25 +00001816 }
bellardf193c792004-03-21 17:06:25 +00001817 found:
1818 mask |= item->mask;
1819 if (*p1 != ',')
1820 break;
1821 p = p1 + 1;
1822 }
1823 return mask;
1824}
bellardea041c02003-06-25 16:16:50 +00001825
bellard75012672003-06-21 13:11:07 +00001826void cpu_abort(CPUState *env, const char *fmt, ...)
1827{
1828 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001829 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001830
1831 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001832 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001833 fprintf(stderr, "qemu: fatal: ");
1834 vfprintf(stderr, fmt, ap);
1835 fprintf(stderr, "\n");
1836#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001837 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1838#else
1839 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001840#endif
aliguori93fcfe32009-01-15 22:34:14 +00001841 if (qemu_log_enabled()) {
1842 qemu_log("qemu: fatal: ");
1843 qemu_log_vprintf(fmt, ap2);
1844 qemu_log("\n");
j_mayerf9373292007-09-29 12:18:20 +00001845#ifdef TARGET_I386
aliguori93fcfe32009-01-15 22:34:14 +00001846 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
j_mayerf9373292007-09-29 12:18:20 +00001847#else
aliguori93fcfe32009-01-15 22:34:14 +00001848 log_cpu_state(env, 0);
j_mayerf9373292007-09-29 12:18:20 +00001849#endif
aliguori31b1a7b2009-01-15 22:35:09 +00001850 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +00001851 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +00001852 }
pbrook493ae1f2007-11-23 16:53:59 +00001853 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001854 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +02001855#if defined(CONFIG_USER_ONLY)
1856 {
1857 struct sigaction act;
1858 sigfillset(&act.sa_mask);
1859 act.sa_handler = SIG_DFL;
1860 sigaction(SIGABRT, &act, NULL);
1861 }
1862#endif
bellard75012672003-06-21 13:11:07 +00001863 abort();
1864}
1865
thsc5be9f02007-02-28 20:20:53 +00001866CPUState *cpu_copy(CPUState *env)
1867{
ths01ba9812007-12-09 02:22:57 +00001868 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001869 CPUState *next_cpu = new_env->next_cpu;
1870 int cpu_index = new_env->cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001871#if defined(TARGET_HAS_ICE)
1872 CPUBreakpoint *bp;
1873 CPUWatchpoint *wp;
1874#endif
1875
thsc5be9f02007-02-28 20:20:53 +00001876 memcpy(new_env, env, sizeof(CPUState));
aliguori5a38f082009-01-15 20:16:51 +00001877
1878 /* Preserve chaining and index. */
thsc5be9f02007-02-28 20:20:53 +00001879 new_env->next_cpu = next_cpu;
1880 new_env->cpu_index = cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001881
1882 /* Clone all break/watchpoints.
1883 Note: Once we support ptrace with hw-debug register access, make sure
1884 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001885 QTAILQ_INIT(&env->breakpoints);
1886 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +00001887#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001888 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001889 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1890 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001891 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001892 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1893 wp->flags, NULL);
1894 }
1895#endif
1896
thsc5be9f02007-02-28 20:20:53 +00001897 return new_env;
1898}
1899
bellard01243112004-01-04 15:48:17 +00001900#if !defined(CONFIG_USER_ONLY)
1901
edgar_igl5c751e92008-05-06 08:44:21 +00001902static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1903{
1904 unsigned int i;
1905
1906 /* Discard jump cache entries for any tb which might potentially
1907 overlap the flushed page. */
1908 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1909 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001910 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001911
1912 i = tb_jmp_cache_hash_page(addr);
1913 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001914 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001915}
1916
Igor Kovalenko08738982009-07-12 02:15:40 +04001917static CPUTLBEntry s_cputlb_empty_entry = {
1918 .addr_read = -1,
1919 .addr_write = -1,
1920 .addr_code = -1,
1921 .addend = -1,
1922};
1923
bellardee8b7022004-02-03 23:35:10 +00001924/* NOTE: if flush_global is true, also flush global entries (not
1925 implemented yet) */
1926void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001927{
bellard33417e72003-08-10 21:47:01 +00001928 int i;
bellard01243112004-01-04 15:48:17 +00001929
bellard9fa3e852004-01-04 18:06:42 +00001930#if defined(DEBUG_TLB)
1931 printf("tlb_flush:\n");
1932#endif
bellard01243112004-01-04 15:48:17 +00001933 /* must reset current TB so that interrupts cannot modify the
1934 links while we are modifying them */
1935 env->current_tb = NULL;
1936
bellard33417e72003-08-10 21:47:01 +00001937 for(i = 0; i < CPU_TLB_SIZE; i++) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001938 int mmu_idx;
1939 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001940 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001941 }
bellard33417e72003-08-10 21:47:01 +00001942 }
bellard9fa3e852004-01-04 18:06:42 +00001943
bellard8a40a182005-11-20 10:35:40 +00001944 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001945
Paul Brookd4c430a2010-03-17 02:14:28 +00001946 env->tlb_flush_addr = -1;
1947 env->tlb_flush_mask = 0;
bellarde3db7222005-01-26 22:00:47 +00001948 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001949}
1950
bellard274da6b2004-05-20 21:56:27 +00001951static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001952{
ths5fafdf22007-09-16 21:08:06 +00001953 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001954 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001955 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001956 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001957 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001958 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001959 *tlb_entry = s_cputlb_empty_entry;
bellard84b7b8e2005-11-28 21:19:04 +00001960 }
bellard61382a52003-10-27 21:22:23 +00001961}
1962
bellard2e126692004-04-25 21:28:44 +00001963void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001964{
bellard8a40a182005-11-20 10:35:40 +00001965 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001966 int mmu_idx;
bellard01243112004-01-04 15:48:17 +00001967
bellard9fa3e852004-01-04 18:06:42 +00001968#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001969 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001970#endif
Paul Brookd4c430a2010-03-17 02:14:28 +00001971 /* Check if we need to flush due to large pages. */
1972 if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
1973#if defined(DEBUG_TLB)
1974 printf("tlb_flush_page: forced full flush ("
1975 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
1976 env->tlb_flush_addr, env->tlb_flush_mask);
1977#endif
1978 tlb_flush(env, 1);
1979 return;
1980 }
bellard01243112004-01-04 15:48:17 +00001981 /* must reset current TB so that interrupts cannot modify the
1982 links while we are modifying them */
1983 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001984
bellard61382a52003-10-27 21:22:23 +00001985 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001986 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001987 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1988 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
bellard01243112004-01-04 15:48:17 +00001989
edgar_igl5c751e92008-05-06 08:44:21 +00001990 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001991}
1992
bellard9fa3e852004-01-04 18:06:42 +00001993/* update the TLBs so that writes to code in the virtual page 'addr'
1994 can be detected */
Anthony Liguoric227f092009-10-01 16:12:16 -05001995static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001996{
ths5fafdf22007-09-16 21:08:06 +00001997 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001998 ram_addr + TARGET_PAGE_SIZE,
1999 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00002000}
2001
bellard9fa3e852004-01-04 18:06:42 +00002002/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00002003 tested for self modifying code */
Anthony Liguoric227f092009-10-01 16:12:16 -05002004static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00002005 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00002006{
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002007 cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG);
bellard1ccde1c2004-02-06 19:46:14 +00002008}
2009
ths5fafdf22007-09-16 21:08:06 +00002010static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00002011 unsigned long start, unsigned long length)
2012{
2013 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00002014 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
2015 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00002016 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00002017 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00002018 }
2019 }
2020}
2021
pbrook5579c7f2009-04-11 14:47:08 +00002022/* Note: start and end must be within the same ram block. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002023void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00002024 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00002025{
2026 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00002027 unsigned long length, start1;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002028 int i;
bellard1ccde1c2004-02-06 19:46:14 +00002029
2030 start &= TARGET_PAGE_MASK;
2031 end = TARGET_PAGE_ALIGN(end);
2032
2033 length = end - start;
2034 if (length == 0)
2035 return;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002036 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00002037
bellard1ccde1c2004-02-06 19:46:14 +00002038 /* we modify the TLB cache so that the dirty bit will be set again
2039 when accessing the range */
pbrook5579c7f2009-04-11 14:47:08 +00002040 start1 = (unsigned long)qemu_get_ram_ptr(start);
2041 /* Chek that we don't span multiple blocks - this breaks the
2042 address comparisons below. */
2043 if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1
2044 != (end - 1) - start) {
2045 abort();
2046 }
2047
bellard6a00d602005-11-21 23:25:50 +00002048 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002049 int mmu_idx;
2050 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2051 for(i = 0; i < CPU_TLB_SIZE; i++)
2052 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
2053 start1, length);
2054 }
bellard6a00d602005-11-21 23:25:50 +00002055 }
bellard1ccde1c2004-02-06 19:46:14 +00002056}
2057
aliguori74576192008-10-06 14:02:03 +00002058int cpu_physical_memory_set_dirty_tracking(int enable)
2059{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002060 int ret = 0;
aliguori74576192008-10-06 14:02:03 +00002061 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002062 ret = cpu_notify_migration_log(!!enable);
2063 return ret;
aliguori74576192008-10-06 14:02:03 +00002064}
2065
2066int cpu_physical_memory_get_dirty_tracking(void)
2067{
2068 return in_migration;
2069}
2070
Anthony Liguoric227f092009-10-01 16:12:16 -05002071int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
2072 target_phys_addr_t end_addr)
aliguori2bec46d2008-11-24 20:21:41 +00002073{
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +02002074 int ret;
Jan Kiszka151f7742009-05-01 20:52:47 +02002075
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002076 ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
Jan Kiszka151f7742009-05-01 20:52:47 +02002077 return ret;
aliguori2bec46d2008-11-24 20:21:41 +00002078}
2079
bellard3a7d9292005-08-21 09:26:42 +00002080static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
2081{
Anthony Liguoric227f092009-10-01 16:12:16 -05002082 ram_addr_t ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00002083 void *p;
bellard3a7d9292005-08-21 09:26:42 +00002084
bellard84b7b8e2005-11-28 21:19:04 +00002085 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
pbrook5579c7f2009-04-11 14:47:08 +00002086 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
2087 + tlb_entry->addend);
Marcelo Tosattie8902612010-10-11 15:31:19 -03002088 ram_addr = qemu_ram_addr_from_host_nofail(p);
bellard3a7d9292005-08-21 09:26:42 +00002089 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00002090 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00002091 }
2092 }
2093}
2094
2095/* update the TLB according to the current state of the dirty bits */
2096void cpu_tlb_update_dirty(CPUState *env)
2097{
2098 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002099 int mmu_idx;
2100 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2101 for(i = 0; i < CPU_TLB_SIZE; i++)
2102 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
2103 }
bellard3a7d9292005-08-21 09:26:42 +00002104}
2105
pbrook0f459d12008-06-09 00:20:13 +00002106static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002107{
pbrook0f459d12008-06-09 00:20:13 +00002108 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
2109 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00002110}
2111
pbrook0f459d12008-06-09 00:20:13 +00002112/* update the TLB corresponding to virtual page vaddr
2113 so that it is no longer dirty */
2114static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002115{
bellard1ccde1c2004-02-06 19:46:14 +00002116 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002117 int mmu_idx;
bellard1ccde1c2004-02-06 19:46:14 +00002118
pbrook0f459d12008-06-09 00:20:13 +00002119 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00002120 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002121 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2122 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
bellard9fa3e852004-01-04 18:06:42 +00002123}
2124
Paul Brookd4c430a2010-03-17 02:14:28 +00002125/* Our TLB does not support large pages, so remember the area covered by
2126 large pages and trigger a full TLB flush if these are invalidated. */
2127static void tlb_add_large_page(CPUState *env, target_ulong vaddr,
2128 target_ulong size)
2129{
2130 target_ulong mask = ~(size - 1);
2131
2132 if (env->tlb_flush_addr == (target_ulong)-1) {
2133 env->tlb_flush_addr = vaddr & mask;
2134 env->tlb_flush_mask = mask;
2135 return;
2136 }
2137 /* Extend the existing region to include the new page.
2138 This is a compromise between unnecessary flushes and the cost
2139 of maintaining a full variable size TLB. */
2140 mask &= env->tlb_flush_mask;
2141 while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
2142 mask <<= 1;
2143 }
2144 env->tlb_flush_addr &= mask;
2145 env->tlb_flush_mask = mask;
2146}
2147
2148/* Add a new TLB entry. At most one entry for a given virtual address
2149 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
2150 supplied size is only used by tlb_flush_page. */
2151void tlb_set_page(CPUState *env, target_ulong vaddr,
2152 target_phys_addr_t paddr, int prot,
2153 int mmu_idx, target_ulong size)
bellard9fa3e852004-01-04 18:06:42 +00002154{
bellard92e873b2004-05-21 14:52:29 +00002155 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00002156 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00002157 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00002158 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00002159 target_ulong code_address;
Paul Brook355b1942010-04-05 00:28:53 +01002160 unsigned long addend;
bellard84b7b8e2005-11-28 21:19:04 +00002161 CPUTLBEntry *te;
aliguoria1d1bb32008-11-18 20:07:32 +00002162 CPUWatchpoint *wp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002163 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00002164
Paul Brookd4c430a2010-03-17 02:14:28 +00002165 assert(size >= TARGET_PAGE_SIZE);
2166 if (size != TARGET_PAGE_SIZE) {
2167 tlb_add_large_page(env, vaddr, size);
2168 }
bellard92e873b2004-05-21 14:52:29 +00002169 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00002170 if (!p) {
2171 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00002172 } else {
2173 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00002174 }
2175#if defined(DEBUG_TLB)
Stefan Weil7fd3f492010-09-30 22:39:51 +02002176 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
2177 " prot=%x idx=%d pd=0x%08lx\n",
2178 vaddr, paddr, prot, mmu_idx, pd);
bellard9fa3e852004-01-04 18:06:42 +00002179#endif
2180
pbrook0f459d12008-06-09 00:20:13 +00002181 address = vaddr;
2182 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2183 /* IO memory case (romd handled later) */
2184 address |= TLB_MMIO;
2185 }
pbrook5579c7f2009-04-11 14:47:08 +00002186 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
pbrook0f459d12008-06-09 00:20:13 +00002187 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2188 /* Normal RAM. */
2189 iotlb = pd & TARGET_PAGE_MASK;
2190 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2191 iotlb |= IO_MEM_NOTDIRTY;
2192 else
2193 iotlb |= IO_MEM_ROM;
2194 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002195 /* IO handlers are currently passed a physical address.
pbrook0f459d12008-06-09 00:20:13 +00002196 It would be nice to pass an offset from the base address
2197 of that region. This would avoid having to special case RAM,
2198 and avoid full address decoding in every device.
2199 We can't use the high bits of pd for this because
2200 IO_MEM_ROMD uses these as a ram address. */
pbrook8da3ff12008-12-01 18:59:50 +00002201 iotlb = (pd & ~TARGET_PAGE_MASK);
2202 if (p) {
pbrook8da3ff12008-12-01 18:59:50 +00002203 iotlb += p->region_offset;
2204 } else {
2205 iotlb += paddr;
2206 }
pbrook0f459d12008-06-09 00:20:13 +00002207 }
pbrook6658ffb2007-03-16 23:58:11 +00002208
pbrook0f459d12008-06-09 00:20:13 +00002209 code_address = address;
2210 /* Make accesses to pages with watchpoints go via the
2211 watchpoint trap routines. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002212 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00002213 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
Jun Koibf298f82010-05-06 14:36:59 +09002214 /* Avoid trapping reads of pages with a write breakpoint. */
2215 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
2216 iotlb = io_mem_watch + paddr;
2217 address |= TLB_MMIO;
2218 break;
2219 }
pbrook6658ffb2007-03-16 23:58:11 +00002220 }
pbrook0f459d12008-06-09 00:20:13 +00002221 }
balrogd79acba2007-06-26 20:01:13 +00002222
pbrook0f459d12008-06-09 00:20:13 +00002223 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2224 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2225 te = &env->tlb_table[mmu_idx][index];
2226 te->addend = addend - vaddr;
2227 if (prot & PAGE_READ) {
2228 te->addr_read = address;
2229 } else {
2230 te->addr_read = -1;
2231 }
edgar_igl5c751e92008-05-06 08:44:21 +00002232
pbrook0f459d12008-06-09 00:20:13 +00002233 if (prot & PAGE_EXEC) {
2234 te->addr_code = code_address;
2235 } else {
2236 te->addr_code = -1;
2237 }
2238 if (prot & PAGE_WRITE) {
2239 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2240 (pd & IO_MEM_ROMD)) {
2241 /* Write access calls the I/O callback. */
2242 te->addr_write = address | TLB_MMIO;
2243 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2244 !cpu_physical_memory_is_dirty(pd)) {
2245 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00002246 } else {
pbrook0f459d12008-06-09 00:20:13 +00002247 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00002248 }
pbrook0f459d12008-06-09 00:20:13 +00002249 } else {
2250 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00002251 }
bellard9fa3e852004-01-04 18:06:42 +00002252}
2253
bellard01243112004-01-04 15:48:17 +00002254#else
2255
bellardee8b7022004-02-03 23:35:10 +00002256void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00002257{
2258}
2259
bellard2e126692004-04-25 21:28:44 +00002260void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00002261{
2262}
2263
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002264/*
2265 * Walks guest process memory "regions" one by one
2266 * and calls callback function 'fn' for each region.
2267 */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002268
2269struct walk_memory_regions_data
bellard9fa3e852004-01-04 18:06:42 +00002270{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002271 walk_memory_regions_fn fn;
2272 void *priv;
2273 unsigned long start;
2274 int prot;
2275};
bellard9fa3e852004-01-04 18:06:42 +00002276
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002277static int walk_memory_regions_end(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002278 abi_ulong end, int new_prot)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002279{
2280 if (data->start != -1ul) {
2281 int rc = data->fn(data->priv, data->start, end, data->prot);
2282 if (rc != 0) {
2283 return rc;
bellard9fa3e852004-01-04 18:06:42 +00002284 }
bellard33417e72003-08-10 21:47:01 +00002285 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002286
2287 data->start = (new_prot ? end : -1ul);
2288 data->prot = new_prot;
2289
2290 return 0;
2291}
2292
2293static int walk_memory_regions_1(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002294 abi_ulong base, int level, void **lp)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002295{
Paul Brookb480d9b2010-03-12 23:23:29 +00002296 abi_ulong pa;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002297 int i, rc;
2298
2299 if (*lp == NULL) {
2300 return walk_memory_regions_end(data, base, 0);
2301 }
2302
2303 if (level == 0) {
2304 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002305 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002306 int prot = pd[i].flags;
2307
2308 pa = base | (i << TARGET_PAGE_BITS);
2309 if (prot != data->prot) {
2310 rc = walk_memory_regions_end(data, pa, prot);
2311 if (rc != 0) {
2312 return rc;
2313 }
2314 }
2315 }
2316 } else {
2317 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002318 for (i = 0; i < L2_SIZE; ++i) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002319 pa = base | ((abi_ulong)i <<
2320 (TARGET_PAGE_BITS + L2_BITS * level));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002321 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2322 if (rc != 0) {
2323 return rc;
2324 }
2325 }
2326 }
2327
2328 return 0;
2329}
2330
2331int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2332{
2333 struct walk_memory_regions_data data;
2334 unsigned long i;
2335
2336 data.fn = fn;
2337 data.priv = priv;
2338 data.start = -1ul;
2339 data.prot = 0;
2340
2341 for (i = 0; i < V_L1_SIZE; i++) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002342 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002343 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
2344 if (rc != 0) {
2345 return rc;
2346 }
2347 }
2348
2349 return walk_memory_regions_end(&data, 0, 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002350}
2351
Paul Brookb480d9b2010-03-12 23:23:29 +00002352static int dump_region(void *priv, abi_ulong start,
2353 abi_ulong end, unsigned long prot)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002354{
2355 FILE *f = (FILE *)priv;
2356
Paul Brookb480d9b2010-03-12 23:23:29 +00002357 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
2358 " "TARGET_ABI_FMT_lx" %c%c%c\n",
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002359 start, end, end - start,
2360 ((prot & PAGE_READ) ? 'r' : '-'),
2361 ((prot & PAGE_WRITE) ? 'w' : '-'),
2362 ((prot & PAGE_EXEC) ? 'x' : '-'));
2363
2364 return (0);
2365}
2366
2367/* dump memory mappings */
2368void page_dump(FILE *f)
2369{
2370 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2371 "start", "end", "size", "prot");
2372 walk_memory_regions(f, dump_region);
bellard33417e72003-08-10 21:47:01 +00002373}
2374
pbrook53a59602006-03-25 19:31:22 +00002375int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002376{
bellard9fa3e852004-01-04 18:06:42 +00002377 PageDesc *p;
2378
2379 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002380 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002381 return 0;
2382 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002383}
2384
Richard Henderson376a7902010-03-10 15:57:04 -08002385/* Modify the flags of a page and invalidate the code if necessary.
2386 The flag PAGE_WRITE_ORG is positioned automatically depending
2387 on PAGE_WRITE. The mmap_lock should already be held. */
pbrook53a59602006-03-25 19:31:22 +00002388void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002389{
Richard Henderson376a7902010-03-10 15:57:04 -08002390 target_ulong addr, len;
bellard9fa3e852004-01-04 18:06:42 +00002391
Richard Henderson376a7902010-03-10 15:57:04 -08002392 /* This function should never be called with addresses outside the
2393 guest address space. If this assert fires, it probably indicates
2394 a missing call to h2g_valid. */
Paul Brookb480d9b2010-03-12 23:23:29 +00002395#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2396 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002397#endif
2398 assert(start < end);
2399
bellard9fa3e852004-01-04 18:06:42 +00002400 start = start & TARGET_PAGE_MASK;
2401 end = TARGET_PAGE_ALIGN(end);
Richard Henderson376a7902010-03-10 15:57:04 -08002402
2403 if (flags & PAGE_WRITE) {
bellard9fa3e852004-01-04 18:06:42 +00002404 flags |= PAGE_WRITE_ORG;
Richard Henderson376a7902010-03-10 15:57:04 -08002405 }
2406
2407 for (addr = start, len = end - start;
2408 len != 0;
2409 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2410 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2411
2412 /* If the write protection bit is set, then we invalidate
2413 the code inside. */
ths5fafdf22007-09-16 21:08:06 +00002414 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002415 (flags & PAGE_WRITE) &&
2416 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002417 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002418 }
2419 p->flags = flags;
2420 }
bellard9fa3e852004-01-04 18:06:42 +00002421}
2422
ths3d97b402007-11-02 19:02:07 +00002423int page_check_range(target_ulong start, target_ulong len, int flags)
2424{
2425 PageDesc *p;
2426 target_ulong end;
2427 target_ulong addr;
2428
Richard Henderson376a7902010-03-10 15:57:04 -08002429 /* This function should never be called with addresses outside the
2430 guest address space. If this assert fires, it probably indicates
2431 a missing call to h2g_valid. */
Blue Swirl338e9e62010-03-13 09:48:08 +00002432#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2433 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002434#endif
2435
Richard Henderson3e0650a2010-03-29 10:54:42 -07002436 if (len == 0) {
2437 return 0;
2438 }
Richard Henderson376a7902010-03-10 15:57:04 -08002439 if (start + len - 1 < start) {
2440 /* We've wrapped around. */
balrog55f280c2008-10-28 10:24:11 +00002441 return -1;
Richard Henderson376a7902010-03-10 15:57:04 -08002442 }
balrog55f280c2008-10-28 10:24:11 +00002443
ths3d97b402007-11-02 19:02:07 +00002444 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2445 start = start & TARGET_PAGE_MASK;
2446
Richard Henderson376a7902010-03-10 15:57:04 -08002447 for (addr = start, len = end - start;
2448 len != 0;
2449 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
ths3d97b402007-11-02 19:02:07 +00002450 p = page_find(addr >> TARGET_PAGE_BITS);
2451 if( !p )
2452 return -1;
2453 if( !(p->flags & PAGE_VALID) )
2454 return -1;
2455
bellarddae32702007-11-14 10:51:00 +00002456 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002457 return -1;
bellarddae32702007-11-14 10:51:00 +00002458 if (flags & PAGE_WRITE) {
2459 if (!(p->flags & PAGE_WRITE_ORG))
2460 return -1;
2461 /* unprotect the page if it was put read-only because it
2462 contains translated code */
2463 if (!(p->flags & PAGE_WRITE)) {
2464 if (!page_unprotect(addr, 0, NULL))
2465 return -1;
2466 }
2467 return 0;
2468 }
ths3d97b402007-11-02 19:02:07 +00002469 }
2470 return 0;
2471}
2472
bellard9fa3e852004-01-04 18:06:42 +00002473/* called from signal handler: invalidate the code and unprotect the
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002474 page. Return TRUE if the fault was successfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002475int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002476{
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002477 unsigned int prot;
2478 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002479 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002480
pbrookc8a706f2008-06-02 16:16:42 +00002481 /* Technically this isn't safe inside a signal handler. However we
2482 know this only ever happens in a synchronous SEGV handler, so in
2483 practice it seems to be ok. */
2484 mmap_lock();
2485
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002486 p = page_find(address >> TARGET_PAGE_BITS);
2487 if (!p) {
pbrookc8a706f2008-06-02 16:16:42 +00002488 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002489 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002490 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002491
bellard9fa3e852004-01-04 18:06:42 +00002492 /* if the page was really writable, then we change its
2493 protection back to writable */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002494 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2495 host_start = address & qemu_host_page_mask;
2496 host_end = host_start + qemu_host_page_size;
2497
2498 prot = 0;
2499 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2500 p = page_find(addr >> TARGET_PAGE_BITS);
2501 p->flags |= PAGE_WRITE;
2502 prot |= p->flags;
2503
bellard9fa3e852004-01-04 18:06:42 +00002504 /* and since the content will be modified, we must invalidate
2505 the corresponding translated code. */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002506 tb_invalidate_phys_page(addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002507#ifdef DEBUG_TB_CHECK
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002508 tb_invalidate_check(addr);
bellard9fa3e852004-01-04 18:06:42 +00002509#endif
bellard9fa3e852004-01-04 18:06:42 +00002510 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002511 mprotect((void *)g2h(host_start), qemu_host_page_size,
2512 prot & PAGE_BITS);
2513
2514 mmap_unlock();
2515 return 1;
bellard9fa3e852004-01-04 18:06:42 +00002516 }
pbrookc8a706f2008-06-02 16:16:42 +00002517 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002518 return 0;
2519}
2520
bellard6a00d602005-11-21 23:25:50 +00002521static inline void tlb_set_dirty(CPUState *env,
2522 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002523{
2524}
bellard9fa3e852004-01-04 18:06:42 +00002525#endif /* defined(CONFIG_USER_ONLY) */
2526
pbrooke2eef172008-06-08 01:09:01 +00002527#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +00002528
Paul Brookc04b2b72010-03-01 03:31:14 +00002529#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2530typedef struct subpage_t {
2531 target_phys_addr_t base;
Richard Hendersonf6405242010-04-22 16:47:31 -07002532 ram_addr_t sub_io_index[TARGET_PAGE_SIZE];
2533 ram_addr_t region_offset[TARGET_PAGE_SIZE];
Paul Brookc04b2b72010-03-01 03:31:14 +00002534} subpage_t;
2535
Anthony Liguoric227f092009-10-01 16:12:16 -05002536static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2537 ram_addr_t memory, ram_addr_t region_offset);
Richard Hendersonf6405242010-04-22 16:47:31 -07002538static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2539 ram_addr_t orig_memory,
2540 ram_addr_t region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002541#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2542 need_subpage) \
2543 do { \
2544 if (addr > start_addr) \
2545 start_addr2 = 0; \
2546 else { \
2547 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2548 if (start_addr2 > 0) \
2549 need_subpage = 1; \
2550 } \
2551 \
blueswir149e9fba2007-05-30 17:25:06 +00002552 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002553 end_addr2 = TARGET_PAGE_SIZE - 1; \
2554 else { \
2555 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2556 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2557 need_subpage = 1; \
2558 } \
2559 } while (0)
2560
Michael S. Tsirkin8f2498f2009-09-29 18:53:16 +02002561/* register physical memory.
2562 For RAM, 'size' must be a multiple of the target page size.
2563 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
pbrook8da3ff12008-12-01 18:59:50 +00002564 io memory page. The address used when calling the IO function is
2565 the offset from the start of the region, plus region_offset. Both
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002566 start_addr and region_offset are rounded down to a page boundary
pbrook8da3ff12008-12-01 18:59:50 +00002567 before calculating this offset. This should not be a problem unless
2568 the low bits of start_addr and region_offset differ. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002569void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2570 ram_addr_t size,
2571 ram_addr_t phys_offset,
2572 ram_addr_t region_offset)
bellard33417e72003-08-10 21:47:01 +00002573{
Anthony Liguoric227f092009-10-01 16:12:16 -05002574 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002575 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002576 CPUState *env;
Anthony Liguoric227f092009-10-01 16:12:16 -05002577 ram_addr_t orig_size = size;
Richard Hendersonf6405242010-04-22 16:47:31 -07002578 subpage_t *subpage;
bellard33417e72003-08-10 21:47:01 +00002579
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002580 cpu_notify_set_memory(start_addr, size, phys_offset);
2581
pbrook67c4d232009-02-23 13:16:07 +00002582 if (phys_offset == IO_MEM_UNASSIGNED) {
2583 region_offset = start_addr;
2584 }
pbrook8da3ff12008-12-01 18:59:50 +00002585 region_offset &= TARGET_PAGE_MASK;
bellard5fd386f2004-05-23 21:11:22 +00002586 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
Anthony Liguoric227f092009-10-01 16:12:16 -05002587 end_addr = start_addr + (target_phys_addr_t)size;
blueswir149e9fba2007-05-30 17:25:06 +00002588 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002589 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2590 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002591 ram_addr_t orig_memory = p->phys_offset;
2592 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002593 int need_subpage = 0;
2594
2595 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2596 need_subpage);
Richard Hendersonf6405242010-04-22 16:47:31 -07002597 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002598 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2599 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002600 &p->phys_offset, orig_memory,
2601 p->region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002602 } else {
2603 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2604 >> IO_MEM_SHIFT];
2605 }
pbrook8da3ff12008-12-01 18:59:50 +00002606 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2607 region_offset);
2608 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002609 } else {
2610 p->phys_offset = phys_offset;
2611 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2612 (phys_offset & IO_MEM_ROMD))
2613 phys_offset += TARGET_PAGE_SIZE;
2614 }
2615 } else {
2616 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2617 p->phys_offset = phys_offset;
pbrook8da3ff12008-12-01 18:59:50 +00002618 p->region_offset = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002619 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
pbrook8da3ff12008-12-01 18:59:50 +00002620 (phys_offset & IO_MEM_ROMD)) {
blueswir1db7b5422007-05-26 17:36:03 +00002621 phys_offset += TARGET_PAGE_SIZE;
pbrook0e8f0962008-12-02 09:02:15 +00002622 } else {
Anthony Liguoric227f092009-10-01 16:12:16 -05002623 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002624 int need_subpage = 0;
2625
2626 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2627 end_addr2, need_subpage);
2628
Richard Hendersonf6405242010-04-22 16:47:31 -07002629 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002630 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002631 &p->phys_offset, IO_MEM_UNASSIGNED,
pbrook67c4d232009-02-23 13:16:07 +00002632 addr & TARGET_PAGE_MASK);
blueswir1db7b5422007-05-26 17:36:03 +00002633 subpage_register(subpage, start_addr2, end_addr2,
pbrook8da3ff12008-12-01 18:59:50 +00002634 phys_offset, region_offset);
2635 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002636 }
2637 }
2638 }
pbrook8da3ff12008-12-01 18:59:50 +00002639 region_offset += TARGET_PAGE_SIZE;
bellard33417e72003-08-10 21:47:01 +00002640 }
ths3b46e622007-09-17 08:09:54 +00002641
bellard9d420372006-06-25 22:25:22 +00002642 /* since each CPU stores ram addresses in its TLB cache, we must
2643 reset the modified entries */
2644 /* XXX: slow ! */
2645 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2646 tlb_flush(env, 1);
2647 }
bellard33417e72003-08-10 21:47:01 +00002648}
2649
bellardba863452006-09-24 18:41:10 +00002650/* XXX: temporary until new memory mapping API */
Anthony Liguoric227f092009-10-01 16:12:16 -05002651ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002652{
2653 PhysPageDesc *p;
2654
2655 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2656 if (!p)
2657 return IO_MEM_UNASSIGNED;
2658 return p->phys_offset;
2659}
2660
Anthony Liguoric227f092009-10-01 16:12:16 -05002661void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002662{
2663 if (kvm_enabled())
2664 kvm_coalesce_mmio_region(addr, size);
2665}
2666
Anthony Liguoric227f092009-10-01 16:12:16 -05002667void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002668{
2669 if (kvm_enabled())
2670 kvm_uncoalesce_mmio_region(addr, size);
2671}
2672
Sheng Yang62a27442010-01-26 19:21:16 +08002673void qemu_flush_coalesced_mmio_buffer(void)
2674{
2675 if (kvm_enabled())
2676 kvm_flush_coalesced_mmio_buffer();
2677}
2678
Marcelo Tosattic9027602010-03-01 20:25:08 -03002679#if defined(__linux__) && !defined(TARGET_S390X)
2680
2681#include <sys/vfs.h>
2682
2683#define HUGETLBFS_MAGIC 0x958458f6
2684
2685static long gethugepagesize(const char *path)
2686{
2687 struct statfs fs;
2688 int ret;
2689
2690 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002691 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002692 } while (ret != 0 && errno == EINTR);
2693
2694 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002695 perror(path);
2696 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002697 }
2698
2699 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002700 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002701
2702 return fs.f_bsize;
2703}
2704
Alex Williamson04b16652010-07-02 11:13:17 -06002705static void *file_ram_alloc(RAMBlock *block,
2706 ram_addr_t memory,
2707 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -03002708{
2709 char *filename;
2710 void *area;
2711 int fd;
2712#ifdef MAP_POPULATE
2713 int flags;
2714#endif
2715 unsigned long hpagesize;
2716
2717 hpagesize = gethugepagesize(path);
2718 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002719 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002720 }
2721
2722 if (memory < hpagesize) {
2723 return NULL;
2724 }
2725
2726 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2727 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2728 return NULL;
2729 }
2730
2731 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002732 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002733 }
2734
2735 fd = mkstemp(filename);
2736 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002737 perror("unable to create backing store for hugepages");
2738 free(filename);
2739 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002740 }
2741 unlink(filename);
2742 free(filename);
2743
2744 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2745
2746 /*
2747 * ftruncate is not supported by hugetlbfs in older
2748 * hosts, so don't bother bailing out on errors.
2749 * If anything goes wrong with it under other filesystems,
2750 * mmap will fail.
2751 */
2752 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002753 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -03002754
2755#ifdef MAP_POPULATE
2756 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2757 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2758 * to sidestep this quirk.
2759 */
2760 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2761 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2762#else
2763 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2764#endif
2765 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002766 perror("file_ram_alloc: can't mmap RAM pages");
2767 close(fd);
2768 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002769 }
Alex Williamson04b16652010-07-02 11:13:17 -06002770 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002771 return area;
2772}
2773#endif
2774
Alex Williamsond17b5282010-06-25 11:08:38 -06002775static ram_addr_t find_ram_offset(ram_addr_t size)
2776{
Alex Williamson04b16652010-07-02 11:13:17 -06002777 RAMBlock *block, *next_block;
Blue Swirl09d7ae92010-07-07 19:37:53 +00002778 ram_addr_t offset = 0, mingap = ULONG_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06002779
2780 if (QLIST_EMPTY(&ram_list.blocks))
2781 return 0;
2782
2783 QLIST_FOREACH(block, &ram_list.blocks, next) {
2784 ram_addr_t end, next = ULONG_MAX;
2785
2786 end = block->offset + block->length;
2787
2788 QLIST_FOREACH(next_block, &ram_list.blocks, next) {
2789 if (next_block->offset >= end) {
2790 next = MIN(next, next_block->offset);
2791 }
2792 }
2793 if (next - end >= size && next - end < mingap) {
2794 offset = end;
2795 mingap = next - end;
2796 }
2797 }
2798 return offset;
2799}
2800
2801static ram_addr_t last_ram_offset(void)
2802{
Alex Williamsond17b5282010-06-25 11:08:38 -06002803 RAMBlock *block;
2804 ram_addr_t last = 0;
2805
2806 QLIST_FOREACH(block, &ram_list.blocks, next)
2807 last = MAX(last, block->offset + block->length);
2808
2809 return last;
2810}
2811
Cam Macdonell84b89d72010-07-26 18:10:57 -06002812ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002813 ram_addr_t size, void *host)
Cam Macdonell84b89d72010-07-26 18:10:57 -06002814{
2815 RAMBlock *new_block, *block;
2816
2817 size = TARGET_PAGE_ALIGN(size);
2818 new_block = qemu_mallocz(sizeof(*new_block));
2819
2820 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
2821 char *id = dev->parent_bus->info->get_dev_path(dev);
2822 if (id) {
2823 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
2824 qemu_free(id);
2825 }
2826 }
2827 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
2828
2829 QLIST_FOREACH(block, &ram_list.blocks, next) {
2830 if (!strcmp(block->idstr, new_block->idstr)) {
2831 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
2832 new_block->idstr);
2833 abort();
2834 }
2835 }
2836
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002837 if (host) {
2838 new_block->host = host;
2839 } else {
2840 if (mem_path) {
2841#if defined (__linux__) && !defined(TARGET_S390X)
2842 new_block->host = file_ram_alloc(new_block, size, mem_path);
2843 if (!new_block->host) {
2844 new_block->host = qemu_vmalloc(size);
Andreas Färbere78815a2010-09-25 11:26:05 +00002845 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002846 }
2847#else
2848 fprintf(stderr, "-mem-path option unsupported\n");
2849 exit(1);
2850#endif
2851 } else {
2852#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2853 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2854 new_block->host = mmap((void*)0x1000000, size,
2855 PROT_EXEC|PROT_READ|PROT_WRITE,
2856 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
2857#else
2858 new_block->host = qemu_vmalloc(size);
2859#endif
Andreas Färbere78815a2010-09-25 11:26:05 +00002860 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002861 }
2862 }
Cam Macdonell84b89d72010-07-26 18:10:57 -06002863
2864 new_block->offset = find_ram_offset(size);
2865 new_block->length = size;
2866
2867 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
2868
2869 ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
2870 last_ram_offset() >> TARGET_PAGE_BITS);
2871 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
2872 0xff, size >> TARGET_PAGE_BITS);
2873
2874 if (kvm_enabled())
2875 kvm_setup_guest_memory(new_block->host, size);
2876
2877 return new_block->offset;
2878}
2879
Alex Williamson1724f042010-06-25 11:09:35 -06002880ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
pbrook94a6b542009-04-11 17:15:54 +00002881{
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002882 return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
pbrook94a6b542009-04-11 17:15:54 +00002883}
bellarde9a1ab12007-02-08 23:08:38 +00002884
Anthony Liguoric227f092009-10-01 16:12:16 -05002885void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00002886{
Alex Williamson04b16652010-07-02 11:13:17 -06002887 RAMBlock *block;
2888
2889 QLIST_FOREACH(block, &ram_list.blocks, next) {
2890 if (addr == block->offset) {
2891 QLIST_REMOVE(block, next);
2892 if (mem_path) {
2893#if defined (__linux__) && !defined(TARGET_S390X)
2894 if (block->fd) {
2895 munmap(block->host, block->length);
2896 close(block->fd);
2897 } else {
2898 qemu_vfree(block->host);
2899 }
2900#endif
2901 } else {
2902#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2903 munmap(block->host, block->length);
2904#else
2905 qemu_vfree(block->host);
2906#endif
2907 }
2908 qemu_free(block);
2909 return;
2910 }
2911 }
2912
bellarde9a1ab12007-02-08 23:08:38 +00002913}
2914
pbrookdc828ca2009-04-09 22:21:07 +00002915/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00002916 With the exception of the softmmu code in this file, this should
2917 only be used for local memory (e.g. video ram) that the device owns,
2918 and knows it isn't going to access beyond the end of the block.
2919
2920 It should not be used for general purpose DMA.
2921 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2922 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002923void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00002924{
pbrook94a6b542009-04-11 17:15:54 +00002925 RAMBlock *block;
2926
Alex Williamsonf471a172010-06-11 11:11:42 -06002927 QLIST_FOREACH(block, &ram_list.blocks, next) {
2928 if (addr - block->offset < block->length) {
2929 QLIST_REMOVE(block, next);
2930 QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
2931 return block->host + (addr - block->offset);
2932 }
pbrook94a6b542009-04-11 17:15:54 +00002933 }
Alex Williamsonf471a172010-06-11 11:11:42 -06002934
2935 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2936 abort();
2937
2938 return NULL;
pbrookdc828ca2009-04-09 22:21:07 +00002939}
2940
Marcelo Tosattie8902612010-10-11 15:31:19 -03002941int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00002942{
pbrook94a6b542009-04-11 17:15:54 +00002943 RAMBlock *block;
2944 uint8_t *host = ptr;
2945
Alex Williamsonf471a172010-06-11 11:11:42 -06002946 QLIST_FOREACH(block, &ram_list.blocks, next) {
2947 if (host - block->host < block->length) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03002948 *ram_addr = block->offset + (host - block->host);
2949 return 0;
Alex Williamsonf471a172010-06-11 11:11:42 -06002950 }
pbrook94a6b542009-04-11 17:15:54 +00002951 }
Marcelo Tosattie8902612010-10-11 15:31:19 -03002952 return -1;
2953}
Alex Williamsonf471a172010-06-11 11:11:42 -06002954
Marcelo Tosattie8902612010-10-11 15:31:19 -03002955/* Some of the softmmu routines need to translate from a host pointer
2956 (typically a TLB entry) back to a ram offset. */
2957ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
2958{
2959 ram_addr_t ram_addr;
Alex Williamsonf471a172010-06-11 11:11:42 -06002960
Marcelo Tosattie8902612010-10-11 15:31:19 -03002961 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
2962 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2963 abort();
2964 }
2965 return ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00002966}
2967
Anthony Liguoric227f092009-10-01 16:12:16 -05002968static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002969{
pbrook67d3b952006-12-18 05:03:52 +00002970#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002971 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002972#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002973#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002974 do_unassigned_access(addr, 0, 0, 0, 1);
2975#endif
2976 return 0;
2977}
2978
Anthony Liguoric227f092009-10-01 16:12:16 -05002979static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002980{
2981#ifdef DEBUG_UNASSIGNED
2982 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2983#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002984#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002985 do_unassigned_access(addr, 0, 0, 0, 2);
2986#endif
2987 return 0;
2988}
2989
Anthony Liguoric227f092009-10-01 16:12:16 -05002990static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002991{
2992#ifdef DEBUG_UNASSIGNED
2993 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2994#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002995#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002996 do_unassigned_access(addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002997#endif
bellard33417e72003-08-10 21:47:01 +00002998 return 0;
2999}
3000
Anthony Liguoric227f092009-10-01 16:12:16 -05003001static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00003002{
pbrook67d3b952006-12-18 05:03:52 +00003003#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00003004 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00003005#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02003006#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00003007 do_unassigned_access(addr, 1, 0, 0, 1);
3008#endif
3009}
3010
Anthony Liguoric227f092009-10-01 16:12:16 -05003011static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003012{
3013#ifdef DEBUG_UNASSIGNED
3014 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3015#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02003016#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00003017 do_unassigned_access(addr, 1, 0, 0, 2);
3018#endif
3019}
3020
Anthony Liguoric227f092009-10-01 16:12:16 -05003021static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003022{
3023#ifdef DEBUG_UNASSIGNED
3024 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3025#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02003026#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00003027 do_unassigned_access(addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00003028#endif
bellard33417e72003-08-10 21:47:01 +00003029}
3030
Blue Swirld60efc62009-08-25 18:29:31 +00003031static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
bellard33417e72003-08-10 21:47:01 +00003032 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00003033 unassigned_mem_readw,
3034 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00003035};
3036
Blue Swirld60efc62009-08-25 18:29:31 +00003037static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
bellard33417e72003-08-10 21:47:01 +00003038 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00003039 unassigned_mem_writew,
3040 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00003041};
3042
Anthony Liguoric227f092009-10-01 16:12:16 -05003043static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003044 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003045{
bellard3a7d9292005-08-21 09:26:42 +00003046 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003047 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003048 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3049#if !defined(CONFIG_USER_ONLY)
3050 tb_invalidate_phys_page_fast(ram_addr, 1);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003051 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003052#endif
3053 }
pbrook5579c7f2009-04-11 14:47:08 +00003054 stb_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003055 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003056 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003057 /* we remove the notdirty callback only if the code has been
3058 flushed */
3059 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003060 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003061}
3062
Anthony Liguoric227f092009-10-01 16:12:16 -05003063static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003064 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003065{
bellard3a7d9292005-08-21 09:26:42 +00003066 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003067 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003068 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3069#if !defined(CONFIG_USER_ONLY)
3070 tb_invalidate_phys_page_fast(ram_addr, 2);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003071 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003072#endif
3073 }
pbrook5579c7f2009-04-11 14:47:08 +00003074 stw_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003075 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003076 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003077 /* we remove the notdirty callback only if the code has been
3078 flushed */
3079 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003080 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003081}
3082
Anthony Liguoric227f092009-10-01 16:12:16 -05003083static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003084 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003085{
bellard3a7d9292005-08-21 09:26:42 +00003086 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003087 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003088 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3089#if !defined(CONFIG_USER_ONLY)
3090 tb_invalidate_phys_page_fast(ram_addr, 4);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003091 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003092#endif
3093 }
pbrook5579c7f2009-04-11 14:47:08 +00003094 stl_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003095 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003096 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003097 /* we remove the notdirty callback only if the code has been
3098 flushed */
3099 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003100 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003101}
3102
Blue Swirld60efc62009-08-25 18:29:31 +00003103static CPUReadMemoryFunc * const error_mem_read[3] = {
bellard3a7d9292005-08-21 09:26:42 +00003104 NULL, /* never used */
3105 NULL, /* never used */
3106 NULL, /* never used */
3107};
3108
Blue Swirld60efc62009-08-25 18:29:31 +00003109static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
bellard1ccde1c2004-02-06 19:46:14 +00003110 notdirty_mem_writeb,
3111 notdirty_mem_writew,
3112 notdirty_mem_writel,
3113};
3114
pbrook0f459d12008-06-09 00:20:13 +00003115/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00003116static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00003117{
3118 CPUState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00003119 target_ulong pc, cs_base;
3120 TranslationBlock *tb;
pbrook0f459d12008-06-09 00:20:13 +00003121 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00003122 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00003123 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00003124
aliguori06d55cc2008-11-18 20:24:06 +00003125 if (env->watchpoint_hit) {
3126 /* We re-entered the check after replacing the TB. Now raise
3127 * the debug interrupt so that is will trigger after the
3128 * current instruction. */
3129 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
3130 return;
3131 }
pbrook2e70f6e2008-06-29 01:03:05 +00003132 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003133 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00003134 if ((vaddr == (wp->vaddr & len_mask) ||
3135 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00003136 wp->flags |= BP_WATCHPOINT_HIT;
3137 if (!env->watchpoint_hit) {
3138 env->watchpoint_hit = wp;
3139 tb = tb_find_pc(env->mem_io_pc);
3140 if (!tb) {
3141 cpu_abort(env, "check_watchpoint: could not find TB for "
3142 "pc=%p", (void *)env->mem_io_pc);
3143 }
3144 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
3145 tb_phys_invalidate(tb, -1);
3146 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
3147 env->exception_index = EXCP_DEBUG;
3148 } else {
3149 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
3150 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
3151 }
3152 cpu_resume_from_signal(env, NULL);
aliguori06d55cc2008-11-18 20:24:06 +00003153 }
aliguori6e140f22008-11-18 20:37:55 +00003154 } else {
3155 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00003156 }
3157 }
3158}
3159
pbrook6658ffb2007-03-16 23:58:11 +00003160/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
3161 so these check for a hit then pass through to the normal out-of-line
3162 phys routines. */
Anthony Liguoric227f092009-10-01 16:12:16 -05003163static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003164{
aliguorib4051332008-11-18 20:14:20 +00003165 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003166 return ldub_phys(addr);
3167}
3168
Anthony Liguoric227f092009-10-01 16:12:16 -05003169static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003170{
aliguorib4051332008-11-18 20:14:20 +00003171 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003172 return lduw_phys(addr);
3173}
3174
Anthony Liguoric227f092009-10-01 16:12:16 -05003175static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003176{
aliguorib4051332008-11-18 20:14:20 +00003177 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003178 return ldl_phys(addr);
3179}
3180
Anthony Liguoric227f092009-10-01 16:12:16 -05003181static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003182 uint32_t val)
3183{
aliguorib4051332008-11-18 20:14:20 +00003184 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003185 stb_phys(addr, val);
3186}
3187
Anthony Liguoric227f092009-10-01 16:12:16 -05003188static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003189 uint32_t val)
3190{
aliguorib4051332008-11-18 20:14:20 +00003191 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003192 stw_phys(addr, val);
3193}
3194
Anthony Liguoric227f092009-10-01 16:12:16 -05003195static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003196 uint32_t val)
3197{
aliguorib4051332008-11-18 20:14:20 +00003198 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003199 stl_phys(addr, val);
3200}
3201
Blue Swirld60efc62009-08-25 18:29:31 +00003202static CPUReadMemoryFunc * const watch_mem_read[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003203 watch_mem_readb,
3204 watch_mem_readw,
3205 watch_mem_readl,
3206};
3207
Blue Swirld60efc62009-08-25 18:29:31 +00003208static CPUWriteMemoryFunc * const watch_mem_write[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003209 watch_mem_writeb,
3210 watch_mem_writew,
3211 watch_mem_writel,
3212};
pbrook6658ffb2007-03-16 23:58:11 +00003213
Richard Hendersonf6405242010-04-22 16:47:31 -07003214static inline uint32_t subpage_readlen (subpage_t *mmio,
3215 target_phys_addr_t addr,
3216 unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003217{
Richard Hendersonf6405242010-04-22 16:47:31 -07003218 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003219#if defined(DEBUG_SUBPAGE)
3220 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3221 mmio, len, addr, idx);
3222#endif
blueswir1db7b5422007-05-26 17:36:03 +00003223
Richard Hendersonf6405242010-04-22 16:47:31 -07003224 addr += mmio->region_offset[idx];
3225 idx = mmio->sub_io_index[idx];
3226 return io_mem_read[idx][len](io_mem_opaque[idx], addr);
blueswir1db7b5422007-05-26 17:36:03 +00003227}
3228
Anthony Liguoric227f092009-10-01 16:12:16 -05003229static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
Richard Hendersonf6405242010-04-22 16:47:31 -07003230 uint32_t value, unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003231{
Richard Hendersonf6405242010-04-22 16:47:31 -07003232 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003233#if defined(DEBUG_SUBPAGE)
Richard Hendersonf6405242010-04-22 16:47:31 -07003234 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n",
3235 __func__, mmio, len, addr, idx, value);
blueswir1db7b5422007-05-26 17:36:03 +00003236#endif
Richard Hendersonf6405242010-04-22 16:47:31 -07003237
3238 addr += mmio->region_offset[idx];
3239 idx = mmio->sub_io_index[idx];
3240 io_mem_write[idx][len](io_mem_opaque[idx], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00003241}
3242
Anthony Liguoric227f092009-10-01 16:12:16 -05003243static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003244{
blueswir1db7b5422007-05-26 17:36:03 +00003245 return subpage_readlen(opaque, addr, 0);
3246}
3247
Anthony Liguoric227f092009-10-01 16:12:16 -05003248static void subpage_writeb (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003249 uint32_t value)
3250{
blueswir1db7b5422007-05-26 17:36:03 +00003251 subpage_writelen(opaque, addr, value, 0);
3252}
3253
Anthony Liguoric227f092009-10-01 16:12:16 -05003254static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003255{
blueswir1db7b5422007-05-26 17:36:03 +00003256 return subpage_readlen(opaque, addr, 1);
3257}
3258
Anthony Liguoric227f092009-10-01 16:12:16 -05003259static void subpage_writew (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003260 uint32_t value)
3261{
blueswir1db7b5422007-05-26 17:36:03 +00003262 subpage_writelen(opaque, addr, value, 1);
3263}
3264
Anthony Liguoric227f092009-10-01 16:12:16 -05003265static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003266{
blueswir1db7b5422007-05-26 17:36:03 +00003267 return subpage_readlen(opaque, addr, 2);
3268}
3269
Richard Hendersonf6405242010-04-22 16:47:31 -07003270static void subpage_writel (void *opaque, target_phys_addr_t addr,
3271 uint32_t value)
blueswir1db7b5422007-05-26 17:36:03 +00003272{
blueswir1db7b5422007-05-26 17:36:03 +00003273 subpage_writelen(opaque, addr, value, 2);
3274}
3275
Blue Swirld60efc62009-08-25 18:29:31 +00003276static CPUReadMemoryFunc * const subpage_read[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003277 &subpage_readb,
3278 &subpage_readw,
3279 &subpage_readl,
3280};
3281
Blue Swirld60efc62009-08-25 18:29:31 +00003282static CPUWriteMemoryFunc * const subpage_write[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003283 &subpage_writeb,
3284 &subpage_writew,
3285 &subpage_writel,
3286};
3287
Anthony Liguoric227f092009-10-01 16:12:16 -05003288static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3289 ram_addr_t memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003290{
3291 int idx, eidx;
3292
3293 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3294 return -1;
3295 idx = SUBPAGE_IDX(start);
3296 eidx = SUBPAGE_IDX(end);
3297#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00003298 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00003299 mmio, start, end, idx, eidx, memory);
3300#endif
Gleb Natapov95c318f2010-07-29 10:41:45 +03003301 if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
3302 memory = IO_MEM_UNASSIGNED;
Richard Hendersonf6405242010-04-22 16:47:31 -07003303 memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
blueswir1db7b5422007-05-26 17:36:03 +00003304 for (; idx <= eidx; idx++) {
Richard Hendersonf6405242010-04-22 16:47:31 -07003305 mmio->sub_io_index[idx] = memory;
3306 mmio->region_offset[idx] = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00003307 }
3308
3309 return 0;
3310}
3311
Richard Hendersonf6405242010-04-22 16:47:31 -07003312static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3313 ram_addr_t orig_memory,
3314 ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003315{
Anthony Liguoric227f092009-10-01 16:12:16 -05003316 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00003317 int subpage_memory;
3318
Anthony Liguoric227f092009-10-01 16:12:16 -05003319 mmio = qemu_mallocz(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00003320
3321 mmio->base = base;
Avi Kivity1eed09c2009-06-14 11:38:51 +03003322 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio);
blueswir1db7b5422007-05-26 17:36:03 +00003323#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00003324 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3325 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00003326#endif
aliguori1eec6142009-02-05 22:06:18 +00003327 *phys = subpage_memory | IO_MEM_SUBPAGE;
Richard Hendersonf6405242010-04-22 16:47:31 -07003328 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00003329
3330 return mmio;
3331}
3332
aliguori88715652009-02-11 15:20:58 +00003333static int get_free_io_mem_idx(void)
3334{
3335 int i;
3336
3337 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3338 if (!io_mem_used[i]) {
3339 io_mem_used[i] = 1;
3340 return i;
3341 }
Riku Voipioc6703b42009-12-03 15:56:05 +02003342 fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
aliguori88715652009-02-11 15:20:58 +00003343 return -1;
3344}
3345
bellard33417e72003-08-10 21:47:01 +00003346/* mem_read and mem_write are arrays of functions containing the
3347 function to access byte (index 0), word (index 1) and dword (index
Paul Brook0b4e6e32009-04-30 18:37:55 +01003348 2). Functions can be omitted with a NULL function pointer.
blueswir13ee89922008-01-02 19:45:26 +00003349 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00003350 modified. If it is zero, a new io zone is allocated. The return
3351 value can be used with cpu_register_physical_memory(). (-1) is
3352 returned if error. */
Avi Kivity1eed09c2009-06-14 11:38:51 +03003353static int cpu_register_io_memory_fixed(int io_index,
Blue Swirld60efc62009-08-25 18:29:31 +00003354 CPUReadMemoryFunc * const *mem_read,
3355 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03003356 void *opaque)
bellard33417e72003-08-10 21:47:01 +00003357{
Richard Henderson3cab7212010-05-07 09:52:51 -07003358 int i;
3359
bellard33417e72003-08-10 21:47:01 +00003360 if (io_index <= 0) {
aliguori88715652009-02-11 15:20:58 +00003361 io_index = get_free_io_mem_idx();
3362 if (io_index == -1)
3363 return io_index;
bellard33417e72003-08-10 21:47:01 +00003364 } else {
Avi Kivity1eed09c2009-06-14 11:38:51 +03003365 io_index >>= IO_MEM_SHIFT;
bellard33417e72003-08-10 21:47:01 +00003366 if (io_index >= IO_MEM_NB_ENTRIES)
3367 return -1;
3368 }
bellardb5ff1b32005-11-26 10:38:39 +00003369
Richard Henderson3cab7212010-05-07 09:52:51 -07003370 for (i = 0; i < 3; ++i) {
3371 io_mem_read[io_index][i]
3372 = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]);
3373 }
3374 for (i = 0; i < 3; ++i) {
3375 io_mem_write[io_index][i]
3376 = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]);
3377 }
bellarda4193c82004-06-03 14:01:43 +00003378 io_mem_opaque[io_index] = opaque;
Richard Hendersonf6405242010-04-22 16:47:31 -07003379
3380 return (io_index << IO_MEM_SHIFT);
bellard33417e72003-08-10 21:47:01 +00003381}
bellard61382a52003-10-27 21:22:23 +00003382
Blue Swirld60efc62009-08-25 18:29:31 +00003383int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
3384 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03003385 void *opaque)
3386{
3387 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
3388}
3389
aliguori88715652009-02-11 15:20:58 +00003390void cpu_unregister_io_memory(int io_table_address)
3391{
3392 int i;
3393 int io_index = io_table_address >> IO_MEM_SHIFT;
3394
3395 for (i=0;i < 3; i++) {
3396 io_mem_read[io_index][i] = unassigned_mem_read[i];
3397 io_mem_write[io_index][i] = unassigned_mem_write[i];
3398 }
3399 io_mem_opaque[io_index] = NULL;
3400 io_mem_used[io_index] = 0;
3401}
3402
Avi Kivitye9179ce2009-06-14 11:38:52 +03003403static void io_mem_init(void)
3404{
3405 int i;
3406
3407 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
3408 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
3409 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
3410 for (i=0; i<5; i++)
3411 io_mem_used[i] = 1;
3412
3413 io_mem_watch = cpu_register_io_memory(watch_mem_read,
3414 watch_mem_write, NULL);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003415}
3416
pbrooke2eef172008-06-08 01:09:01 +00003417#endif /* !defined(CONFIG_USER_ONLY) */
3418
bellard13eb76e2004-01-24 15:23:36 +00003419/* physical memory access (slow version, mainly for debug) */
3420#if defined(CONFIG_USER_ONLY)
Paul Brooka68fe892010-03-01 00:08:59 +00003421int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3422 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003423{
3424 int l, flags;
3425 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00003426 void * p;
bellard13eb76e2004-01-24 15:23:36 +00003427
3428 while (len > 0) {
3429 page = addr & TARGET_PAGE_MASK;
3430 l = (page + TARGET_PAGE_SIZE) - addr;
3431 if (l > len)
3432 l = len;
3433 flags = page_get_flags(page);
3434 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00003435 return -1;
bellard13eb76e2004-01-24 15:23:36 +00003436 if (is_write) {
3437 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00003438 return -1;
bellard579a97f2007-11-11 14:26:47 +00003439 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003440 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00003441 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003442 memcpy(p, buf, l);
3443 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00003444 } else {
3445 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00003446 return -1;
bellard579a97f2007-11-11 14:26:47 +00003447 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003448 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00003449 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003450 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00003451 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00003452 }
3453 len -= l;
3454 buf += l;
3455 addr += l;
3456 }
Paul Brooka68fe892010-03-01 00:08:59 +00003457 return 0;
bellard13eb76e2004-01-24 15:23:36 +00003458}
bellard8df1cd02005-01-28 22:37:22 +00003459
bellard13eb76e2004-01-24 15:23:36 +00003460#else
Anthony Liguoric227f092009-10-01 16:12:16 -05003461void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00003462 int len, int is_write)
3463{
3464 int l, io_index;
3465 uint8_t *ptr;
3466 uint32_t val;
Anthony Liguoric227f092009-10-01 16:12:16 -05003467 target_phys_addr_t page;
bellard2e126692004-04-25 21:28:44 +00003468 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00003469 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003470
bellard13eb76e2004-01-24 15:23:36 +00003471 while (len > 0) {
3472 page = addr & TARGET_PAGE_MASK;
3473 l = (page + TARGET_PAGE_SIZE) - addr;
3474 if (l > len)
3475 l = len;
bellard92e873b2004-05-21 14:52:29 +00003476 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00003477 if (!p) {
3478 pd = IO_MEM_UNASSIGNED;
3479 } else {
3480 pd = p->phys_offset;
3481 }
ths3b46e622007-09-17 08:09:54 +00003482
bellard13eb76e2004-01-24 15:23:36 +00003483 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00003484 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003485 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003486 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003487 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003488 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard6a00d602005-11-21 23:25:50 +00003489 /* XXX: could force cpu_single_env to NULL to avoid
3490 potential bugs */
aurel326c2934d2009-02-18 21:37:17 +00003491 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003492 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003493 val = ldl_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003494 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003495 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003496 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003497 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003498 val = lduw_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003499 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003500 l = 2;
3501 } else {
bellard1c213d12005-09-03 10:49:04 +00003502 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003503 val = ldub_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003504 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003505 l = 1;
3506 }
3507 } else {
bellardb448f2f2004-02-25 23:24:04 +00003508 unsigned long addr1;
3509 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00003510 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003511 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00003512 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00003513 if (!cpu_physical_memory_is_dirty(addr1)) {
3514 /* invalidate code */
3515 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3516 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003517 cpu_physical_memory_set_dirty_flags(
3518 addr1, (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00003519 }
bellard13eb76e2004-01-24 15:23:36 +00003520 }
3521 } else {
ths5fafdf22007-09-16 21:08:06 +00003522 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003523 !(pd & IO_MEM_ROMD)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003524 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003525 /* I/O case */
3526 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003527 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003528 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3529 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003530 /* 32 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003531 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003532 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003533 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003534 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003535 /* 16 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003536 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003537 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003538 l = 2;
3539 } else {
bellard1c213d12005-09-03 10:49:04 +00003540 /* 8 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003541 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003542 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003543 l = 1;
3544 }
3545 } else {
3546 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003547 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00003548 (addr & ~TARGET_PAGE_MASK);
3549 memcpy(buf, ptr, l);
3550 }
3551 }
3552 len -= l;
3553 buf += l;
3554 addr += l;
3555 }
3556}
bellard8df1cd02005-01-28 22:37:22 +00003557
bellardd0ecd2a2006-04-23 17:14:48 +00003558/* used for ROM loading : can write in RAM and ROM */
Anthony Liguoric227f092009-10-01 16:12:16 -05003559void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00003560 const uint8_t *buf, int len)
3561{
3562 int l;
3563 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003564 target_phys_addr_t page;
bellardd0ecd2a2006-04-23 17:14:48 +00003565 unsigned long pd;
3566 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003567
bellardd0ecd2a2006-04-23 17:14:48 +00003568 while (len > 0) {
3569 page = addr & TARGET_PAGE_MASK;
3570 l = (page + TARGET_PAGE_SIZE) - addr;
3571 if (l > len)
3572 l = len;
3573 p = phys_page_find(page >> TARGET_PAGE_BITS);
3574 if (!p) {
3575 pd = IO_MEM_UNASSIGNED;
3576 } else {
3577 pd = p->phys_offset;
3578 }
ths3b46e622007-09-17 08:09:54 +00003579
bellardd0ecd2a2006-04-23 17:14:48 +00003580 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00003581 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3582 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00003583 /* do nothing */
3584 } else {
3585 unsigned long addr1;
3586 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3587 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003588 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00003589 memcpy(ptr, buf, l);
3590 }
3591 len -= l;
3592 buf += l;
3593 addr += l;
3594 }
3595}
3596
aliguori6d16c2f2009-01-22 16:59:11 +00003597typedef struct {
3598 void *buffer;
Anthony Liguoric227f092009-10-01 16:12:16 -05003599 target_phys_addr_t addr;
3600 target_phys_addr_t len;
aliguori6d16c2f2009-01-22 16:59:11 +00003601} BounceBuffer;
3602
3603static BounceBuffer bounce;
3604
aliguoriba223c22009-01-22 16:59:16 +00003605typedef struct MapClient {
3606 void *opaque;
3607 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00003608 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00003609} MapClient;
3610
Blue Swirl72cf2d42009-09-12 07:36:22 +00003611static QLIST_HEAD(map_client_list, MapClient) map_client_list
3612 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003613
3614void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3615{
3616 MapClient *client = qemu_malloc(sizeof(*client));
3617
3618 client->opaque = opaque;
3619 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003620 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00003621 return client;
3622}
3623
3624void cpu_unregister_map_client(void *_client)
3625{
3626 MapClient *client = (MapClient *)_client;
3627
Blue Swirl72cf2d42009-09-12 07:36:22 +00003628 QLIST_REMOVE(client, link);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003629 qemu_free(client);
aliguoriba223c22009-01-22 16:59:16 +00003630}
3631
3632static void cpu_notify_map_clients(void)
3633{
3634 MapClient *client;
3635
Blue Swirl72cf2d42009-09-12 07:36:22 +00003636 while (!QLIST_EMPTY(&map_client_list)) {
3637 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003638 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003639 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00003640 }
3641}
3642
aliguori6d16c2f2009-01-22 16:59:11 +00003643/* Map a physical memory region into a host virtual address.
3644 * May map a subset of the requested range, given by and returned in *plen.
3645 * May return NULL if resources needed to perform the mapping are exhausted.
3646 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00003647 * Use cpu_register_map_client() to know when retrying the map operation is
3648 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00003649 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003650void *cpu_physical_memory_map(target_phys_addr_t addr,
3651 target_phys_addr_t *plen,
aliguori6d16c2f2009-01-22 16:59:11 +00003652 int is_write)
3653{
Anthony Liguoric227f092009-10-01 16:12:16 -05003654 target_phys_addr_t len = *plen;
3655 target_phys_addr_t done = 0;
aliguori6d16c2f2009-01-22 16:59:11 +00003656 int l;
3657 uint8_t *ret = NULL;
3658 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003659 target_phys_addr_t page;
aliguori6d16c2f2009-01-22 16:59:11 +00003660 unsigned long pd;
3661 PhysPageDesc *p;
3662 unsigned long addr1;
3663
3664 while (len > 0) {
3665 page = addr & TARGET_PAGE_MASK;
3666 l = (page + TARGET_PAGE_SIZE) - addr;
3667 if (l > len)
3668 l = len;
3669 p = phys_page_find(page >> TARGET_PAGE_BITS);
3670 if (!p) {
3671 pd = IO_MEM_UNASSIGNED;
3672 } else {
3673 pd = p->phys_offset;
3674 }
3675
3676 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3677 if (done || bounce.buffer) {
3678 break;
3679 }
3680 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3681 bounce.addr = addr;
3682 bounce.len = l;
3683 if (!is_write) {
3684 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3685 }
3686 ptr = bounce.buffer;
3687 } else {
3688 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003689 ptr = qemu_get_ram_ptr(addr1);
aliguori6d16c2f2009-01-22 16:59:11 +00003690 }
3691 if (!done) {
3692 ret = ptr;
3693 } else if (ret + done != ptr) {
3694 break;
3695 }
3696
3697 len -= l;
3698 addr += l;
3699 done += l;
3700 }
3701 *plen = done;
3702 return ret;
3703}
3704
3705/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3706 * Will also mark the memory as dirty if is_write == 1. access_len gives
3707 * the amount of memory that was actually read or written by the caller.
3708 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003709void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3710 int is_write, target_phys_addr_t access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00003711{
3712 if (buffer != bounce.buffer) {
3713 if (is_write) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03003714 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00003715 while (access_len) {
3716 unsigned l;
3717 l = TARGET_PAGE_SIZE;
3718 if (l > access_len)
3719 l = access_len;
3720 if (!cpu_physical_memory_is_dirty(addr1)) {
3721 /* invalidate code */
3722 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3723 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003724 cpu_physical_memory_set_dirty_flags(
3725 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori6d16c2f2009-01-22 16:59:11 +00003726 }
3727 addr1 += l;
3728 access_len -= l;
3729 }
3730 }
3731 return;
3732 }
3733 if (is_write) {
3734 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3735 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00003736 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00003737 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00003738 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00003739}
bellardd0ecd2a2006-04-23 17:14:48 +00003740
bellard8df1cd02005-01-28 22:37:22 +00003741/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003742uint32_t ldl_phys(target_phys_addr_t addr)
bellard8df1cd02005-01-28 22:37:22 +00003743{
3744 int io_index;
3745 uint8_t *ptr;
3746 uint32_t val;
3747 unsigned long pd;
3748 PhysPageDesc *p;
3749
3750 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3751 if (!p) {
3752 pd = IO_MEM_UNASSIGNED;
3753 } else {
3754 pd = p->phys_offset;
3755 }
ths3b46e622007-09-17 08:09:54 +00003756
ths5fafdf22007-09-16 21:08:06 +00003757 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003758 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00003759 /* I/O case */
3760 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003761 if (p)
3762 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003763 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3764 } else {
3765 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003766 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00003767 (addr & ~TARGET_PAGE_MASK);
3768 val = ldl_p(ptr);
3769 }
3770 return val;
3771}
3772
bellard84b7b8e2005-11-28 21:19:04 +00003773/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003774uint64_t ldq_phys(target_phys_addr_t addr)
bellard84b7b8e2005-11-28 21:19:04 +00003775{
3776 int io_index;
3777 uint8_t *ptr;
3778 uint64_t val;
3779 unsigned long pd;
3780 PhysPageDesc *p;
3781
3782 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3783 if (!p) {
3784 pd = IO_MEM_UNASSIGNED;
3785 } else {
3786 pd = p->phys_offset;
3787 }
ths3b46e622007-09-17 08:09:54 +00003788
bellard2a4188a2006-06-25 21:54:59 +00003789 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3790 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00003791 /* I/O case */
3792 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003793 if (p)
3794 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard84b7b8e2005-11-28 21:19:04 +00003795#ifdef TARGET_WORDS_BIGENDIAN
3796 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3797 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3798#else
3799 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3800 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3801#endif
3802 } else {
3803 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003804 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00003805 (addr & ~TARGET_PAGE_MASK);
3806 val = ldq_p(ptr);
3807 }
3808 return val;
3809}
3810
bellardaab33092005-10-30 20:48:42 +00003811/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003812uint32_t ldub_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003813{
3814 uint8_t val;
3815 cpu_physical_memory_read(addr, &val, 1);
3816 return val;
3817}
3818
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03003819/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003820uint32_t lduw_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003821{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03003822 int io_index;
3823 uint8_t *ptr;
3824 uint64_t val;
3825 unsigned long pd;
3826 PhysPageDesc *p;
3827
3828 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3829 if (!p) {
3830 pd = IO_MEM_UNASSIGNED;
3831 } else {
3832 pd = p->phys_offset;
3833 }
3834
3835 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3836 !(pd & IO_MEM_ROMD)) {
3837 /* I/O case */
3838 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3839 if (p)
3840 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3841 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
3842 } else {
3843 /* RAM case */
3844 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3845 (addr & ~TARGET_PAGE_MASK);
3846 val = lduw_p(ptr);
3847 }
3848 return val;
bellardaab33092005-10-30 20:48:42 +00003849}
3850
bellard8df1cd02005-01-28 22:37:22 +00003851/* warning: addr must be aligned. The ram page is not masked as dirty
3852 and the code inside is not invalidated. It is useful if the dirty
3853 bits are used to track modified PTEs */
Anthony Liguoric227f092009-10-01 16:12:16 -05003854void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003855{
3856 int io_index;
3857 uint8_t *ptr;
3858 unsigned long pd;
3859 PhysPageDesc *p;
3860
3861 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3862 if (!p) {
3863 pd = IO_MEM_UNASSIGNED;
3864 } else {
3865 pd = p->phys_offset;
3866 }
ths3b46e622007-09-17 08:09:54 +00003867
bellard3a7d9292005-08-21 09:26:42 +00003868 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003869 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003870 if (p)
3871 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003872 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3873 } else {
aliguori74576192008-10-06 14:02:03 +00003874 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003875 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003876 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00003877
3878 if (unlikely(in_migration)) {
3879 if (!cpu_physical_memory_is_dirty(addr1)) {
3880 /* invalidate code */
3881 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3882 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003883 cpu_physical_memory_set_dirty_flags(
3884 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00003885 }
3886 }
bellard8df1cd02005-01-28 22:37:22 +00003887 }
3888}
3889
Anthony Liguoric227f092009-10-01 16:12:16 -05003890void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
j_mayerbc98a7e2007-04-04 07:55:12 +00003891{
3892 int io_index;
3893 uint8_t *ptr;
3894 unsigned long pd;
3895 PhysPageDesc *p;
3896
3897 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3898 if (!p) {
3899 pd = IO_MEM_UNASSIGNED;
3900 } else {
3901 pd = p->phys_offset;
3902 }
ths3b46e622007-09-17 08:09:54 +00003903
j_mayerbc98a7e2007-04-04 07:55:12 +00003904 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3905 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003906 if (p)
3907 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
j_mayerbc98a7e2007-04-04 07:55:12 +00003908#ifdef TARGET_WORDS_BIGENDIAN
3909 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3910 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3911#else
3912 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3913 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3914#endif
3915 } else {
pbrook5579c7f2009-04-11 14:47:08 +00003916 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00003917 (addr & ~TARGET_PAGE_MASK);
3918 stq_p(ptr, val);
3919 }
3920}
3921
bellard8df1cd02005-01-28 22:37:22 +00003922/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003923void stl_phys(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003924{
3925 int io_index;
3926 uint8_t *ptr;
3927 unsigned long pd;
3928 PhysPageDesc *p;
3929
3930 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3931 if (!p) {
3932 pd = IO_MEM_UNASSIGNED;
3933 } else {
3934 pd = p->phys_offset;
3935 }
ths3b46e622007-09-17 08:09:54 +00003936
bellard3a7d9292005-08-21 09:26:42 +00003937 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003938 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003939 if (p)
3940 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003941 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3942 } else {
3943 unsigned long addr1;
3944 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3945 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003946 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003947 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00003948 if (!cpu_physical_memory_is_dirty(addr1)) {
3949 /* invalidate code */
3950 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3951 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003952 cpu_physical_memory_set_dirty_flags(addr1,
3953 (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00003954 }
bellard8df1cd02005-01-28 22:37:22 +00003955 }
3956}
3957
bellardaab33092005-10-30 20:48:42 +00003958/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003959void stb_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003960{
3961 uint8_t v = val;
3962 cpu_physical_memory_write(addr, &v, 1);
3963}
3964
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03003965/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003966void stw_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003967{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03003968 int io_index;
3969 uint8_t *ptr;
3970 unsigned long pd;
3971 PhysPageDesc *p;
3972
3973 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3974 if (!p) {
3975 pd = IO_MEM_UNASSIGNED;
3976 } else {
3977 pd = p->phys_offset;
3978 }
3979
3980 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3981 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3982 if (p)
3983 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3984 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
3985 } else {
3986 unsigned long addr1;
3987 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3988 /* RAM case */
3989 ptr = qemu_get_ram_ptr(addr1);
3990 stw_p(ptr, val);
3991 if (!cpu_physical_memory_is_dirty(addr1)) {
3992 /* invalidate code */
3993 tb_invalidate_phys_page_range(addr1, addr1 + 2, 0);
3994 /* set dirty bit */
3995 cpu_physical_memory_set_dirty_flags(addr1,
3996 (0xff & ~CODE_DIRTY_FLAG));
3997 }
3998 }
bellardaab33092005-10-30 20:48:42 +00003999}
4000
4001/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004002void stq_phys(target_phys_addr_t addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00004003{
4004 val = tswap64(val);
4005 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
4006}
4007
aliguori5e2972f2009-03-28 17:51:36 +00004008/* virtual memory access for debug (includes writing to ROM) */
ths5fafdf22007-09-16 21:08:06 +00004009int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00004010 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00004011{
4012 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05004013 target_phys_addr_t phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00004014 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00004015
4016 while (len > 0) {
4017 page = addr & TARGET_PAGE_MASK;
4018 phys_addr = cpu_get_phys_page_debug(env, page);
4019 /* if no physical page mapped, return an error */
4020 if (phys_addr == -1)
4021 return -1;
4022 l = (page + TARGET_PAGE_SIZE) - addr;
4023 if (l > len)
4024 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00004025 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00004026 if (is_write)
4027 cpu_physical_memory_write_rom(phys_addr, buf, l);
4028 else
aliguori5e2972f2009-03-28 17:51:36 +00004029 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00004030 len -= l;
4031 buf += l;
4032 addr += l;
4033 }
4034 return 0;
4035}
Paul Brooka68fe892010-03-01 00:08:59 +00004036#endif
bellard13eb76e2004-01-24 15:23:36 +00004037
pbrook2e70f6e2008-06-29 01:03:05 +00004038/* in deterministic execution mode, instructions doing device I/Os
4039 must be at the end of the TB */
4040void cpu_io_recompile(CPUState *env, void *retaddr)
4041{
4042 TranslationBlock *tb;
4043 uint32_t n, cflags;
4044 target_ulong pc, cs_base;
4045 uint64_t flags;
4046
4047 tb = tb_find_pc((unsigned long)retaddr);
4048 if (!tb) {
4049 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
4050 retaddr);
4051 }
4052 n = env->icount_decr.u16.low + tb->icount;
4053 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
4054 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00004055 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00004056 n = n - env->icount_decr.u16.low;
4057 /* Generate a new TB ending on the I/O insn. */
4058 n++;
4059 /* On MIPS and SH, delay slot instructions can only be restarted if
4060 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00004061 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00004062 branch. */
4063#if defined(TARGET_MIPS)
4064 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
4065 env->active_tc.PC -= 4;
4066 env->icount_decr.u16.low++;
4067 env->hflags &= ~MIPS_HFLAG_BMASK;
4068 }
4069#elif defined(TARGET_SH4)
4070 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
4071 && n > 1) {
4072 env->pc -= 2;
4073 env->icount_decr.u16.low++;
4074 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
4075 }
4076#endif
4077 /* This should never happen. */
4078 if (n > CF_COUNT_MASK)
4079 cpu_abort(env, "TB too big during recompile");
4080
4081 cflags = n | CF_LAST_IO;
4082 pc = tb->pc;
4083 cs_base = tb->cs_base;
4084 flags = tb->flags;
4085 tb_phys_invalidate(tb, -1);
4086 /* FIXME: In theory this could raise an exception. In practice
4087 we have already translated the block once so it's probably ok. */
4088 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00004089 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00004090 the first in the TB) then we end up generating a whole new TB and
4091 repeating the fault, which is horribly inefficient.
4092 Better would be to execute just this insn uncached, or generate a
4093 second new TB. */
4094 cpu_resume_from_signal(env, NULL);
4095}
4096
Paul Brookb3755a92010-03-12 16:54:58 +00004097#if !defined(CONFIG_USER_ONLY)
4098
bellarde3db7222005-01-26 22:00:47 +00004099void dump_exec_info(FILE *f,
4100 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
4101{
4102 int i, target_code_size, max_target_code_size;
4103 int direct_jmp_count, direct_jmp2_count, cross_page;
4104 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00004105
bellarde3db7222005-01-26 22:00:47 +00004106 target_code_size = 0;
4107 max_target_code_size = 0;
4108 cross_page = 0;
4109 direct_jmp_count = 0;
4110 direct_jmp2_count = 0;
4111 for(i = 0; i < nb_tbs; i++) {
4112 tb = &tbs[i];
4113 target_code_size += tb->size;
4114 if (tb->size > max_target_code_size)
4115 max_target_code_size = tb->size;
4116 if (tb->page_addr[1] != -1)
4117 cross_page++;
4118 if (tb->tb_next_offset[0] != 0xffff) {
4119 direct_jmp_count++;
4120 if (tb->tb_next_offset[1] != 0xffff) {
4121 direct_jmp2_count++;
4122 }
4123 }
4124 }
4125 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00004126 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00004127 cpu_fprintf(f, "gen code size %ld/%ld\n",
4128 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
4129 cpu_fprintf(f, "TB count %d/%d\n",
4130 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00004131 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00004132 nb_tbs ? target_code_size / nb_tbs : 0,
4133 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00004134 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00004135 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
4136 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00004137 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
4138 cross_page,
bellarde3db7222005-01-26 22:00:47 +00004139 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
4140 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00004141 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00004142 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
4143 direct_jmp2_count,
4144 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00004145 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00004146 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
4147 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
4148 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00004149 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00004150}
4151
bellard61382a52003-10-27 21:22:23 +00004152#define MMUSUFFIX _cmmu
4153#define GETPC() NULL
4154#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00004155#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00004156
4157#define SHIFT 0
4158#include "softmmu_template.h"
4159
4160#define SHIFT 1
4161#include "softmmu_template.h"
4162
4163#define SHIFT 2
4164#include "softmmu_template.h"
4165
4166#define SHIFT 3
4167#include "softmmu_template.h"
4168
4169#undef env
4170
4171#endif