blob: 3a1fcd7bfaf7566aa2361a77bba86946c4934bd2 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
bellardfd6ce8f2003-05-14 19:00:11 +00002 * virtual page mapping and translated block handling
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard54936002003-05-13 00:25:15 +000018 */
bellard67b915a2004-03-31 23:37:16 +000019#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000020#ifdef _WIN32
21#include <windows.h>
22#else
bellarda98d49b2004-11-14 16:22:05 +000023#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000024#include <sys/mman.h>
25#endif
bellard54936002003-05-13 00:25:15 +000026#include <stdlib.h>
27#include <stdio.h>
28#include <stdarg.h>
29#include <string.h>
30#include <errno.h>
31#include <unistd.h>
32#include <inttypes.h>
33
bellard6180a182003-09-30 21:04:53 +000034#include "cpu.h"
35#include "exec-all.h"
aurel32ca10f862008-04-11 21:35:42 +000036#include "qemu-common.h"
bellardb67d9a52008-05-23 09:57:34 +000037#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000038#include "hw/hw.h"
aliguori74576192008-10-06 14:02:03 +000039#include "osdep.h"
aliguori7ba1e612008-11-05 16:04:33 +000040#include "kvm.h"
pbrook53a59602006-03-25 19:31:22 +000041#if defined(CONFIG_USER_ONLY)
42#include <qemu.h>
Riku Voipiofd052bf2010-01-25 14:30:49 +020043#include <signal.h>
pbrook53a59602006-03-25 19:31:22 +000044#endif
bellard54936002003-05-13 00:25:15 +000045
bellardfd6ce8f2003-05-14 19:00:11 +000046//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000047//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000048//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000049//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000050
51/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000052//#define DEBUG_TB_CHECK
53//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000054
ths1196be32007-03-17 15:17:58 +000055//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000056//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000057
pbrook99773bd2006-04-16 15:14:59 +000058#if !defined(CONFIG_USER_ONLY)
59/* TB consistency checks only implemented for usermode emulation. */
60#undef DEBUG_TB_CHECK
61#endif
62
bellard9fa3e852004-01-04 18:06:42 +000063#define SMC_BITMAP_USE_THRESHOLD 10
64
blueswir1bdaf78e2008-10-04 07:24:27 +000065static TranslationBlock *tbs;
bellard26a5f132008-05-28 12:30:31 +000066int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000067TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +000068static int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000069/* any access to the tbs or the page table must use this lock */
Anthony Liguoric227f092009-10-01 16:12:16 -050070spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000071
blueswir1141ac462008-07-26 15:05:57 +000072#if defined(__arm__) || defined(__sparc_v9__)
73/* The prologue must be reachable with a direct jump. ARM and Sparc64
74 have limited branch ranges (possibly also PPC) so place it in a
blueswir1d03d8602008-07-10 17:21:31 +000075 section close to code segment. */
76#define code_gen_section \
77 __attribute__((__section__(".gen_code"))) \
78 __attribute__((aligned (32)))
Stefan Weilf8e2af12009-06-18 23:04:48 +020079#elif defined(_WIN32)
80/* Maximum alignment for Win32 is 16. */
81#define code_gen_section \
82 __attribute__((aligned (16)))
blueswir1d03d8602008-07-10 17:21:31 +000083#else
84#define code_gen_section \
85 __attribute__((aligned (32)))
86#endif
87
88uint8_t code_gen_prologue[1024] code_gen_section;
blueswir1bdaf78e2008-10-04 07:24:27 +000089static uint8_t *code_gen_buffer;
90static unsigned long code_gen_buffer_size;
bellard26a5f132008-05-28 12:30:31 +000091/* threshold to flush the translated code buffer */
blueswir1bdaf78e2008-10-04 07:24:27 +000092static unsigned long code_gen_buffer_max_size;
bellardfd6ce8f2003-05-14 19:00:11 +000093uint8_t *code_gen_ptr;
94
pbrooke2eef172008-06-08 01:09:01 +000095#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +000096int phys_ram_fd;
bellard1ccde1c2004-02-06 19:46:14 +000097uint8_t *phys_ram_dirty;
aliguori74576192008-10-06 14:02:03 +000098static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +000099
100typedef struct RAMBlock {
101 uint8_t *host;
Anthony Liguoric227f092009-10-01 16:12:16 -0500102 ram_addr_t offset;
103 ram_addr_t length;
pbrook94a6b542009-04-11 17:15:54 +0000104 struct RAMBlock *next;
105} RAMBlock;
106
107static RAMBlock *ram_blocks;
108/* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100109 then we can no longer assume contiguous ram offsets, and external uses
pbrook94a6b542009-04-11 17:15:54 +0000110 of this variable will break. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500111ram_addr_t last_ram_offset;
pbrooke2eef172008-06-08 01:09:01 +0000112#endif
bellard9fa3e852004-01-04 18:06:42 +0000113
bellard6a00d602005-11-21 23:25:50 +0000114CPUState *first_cpu;
115/* current CPU in the current thread. It is only valid inside
116 cpu_exec() */
ths5fafdf22007-09-16 21:08:06 +0000117CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000118/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +0000119 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +0000120 2 = Adaptive rate instruction counting. */
121int use_icount = 0;
122/* Current instruction counter. While executing translated code this may
123 include some instructions that have not yet been executed. */
124int64_t qemu_icount;
bellard6a00d602005-11-21 23:25:50 +0000125
bellard54936002003-05-13 00:25:15 +0000126typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000127 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000128 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000129 /* in order to optimize self modifying code, we count the number
130 of lookups we do to a given page to use a bitmap */
131 unsigned int code_write_count;
132 uint8_t *code_bitmap;
133#if defined(CONFIG_USER_ONLY)
134 unsigned long flags;
135#endif
bellard54936002003-05-13 00:25:15 +0000136} PageDesc;
137
bellard92e873b2004-05-21 14:52:29 +0000138typedef struct PhysPageDesc {
pbrook0f459d12008-06-09 00:20:13 +0000139 /* offset in host memory of the page + io_index in the low bits */
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 ram_addr_t phys_offset;
141 ram_addr_t region_offset;
bellard92e873b2004-05-21 14:52:29 +0000142} PhysPageDesc;
143
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800144/* In system mode we want L1_MAP to be based on physical addresses,
145 while in user mode we want it to be based on virtual addresses. */
146#if !defined(CONFIG_USER_ONLY)
147# define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
j_mayerbedb69e2007-04-05 20:08:21 +0000148#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800149# define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
j_mayerbedb69e2007-04-05 20:08:21 +0000150#endif
bellard54936002003-05-13 00:25:15 +0000151
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800152/* Size of the L2 (and L3, etc) page tables. */
153#define L2_BITS 10
bellard54936002003-05-13 00:25:15 +0000154#define L2_SIZE (1 << L2_BITS)
155
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800156/* The bits remaining after N lower levels of page tables. */
157#define P_L1_BITS_REM \
158 ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
159#define V_L1_BITS_REM \
160 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
161
162/* Size of the L1 page table. Avoid silly small sizes. */
163#if P_L1_BITS_REM < 4
164#define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
165#else
166#define P_L1_BITS P_L1_BITS_REM
167#endif
168
169#if V_L1_BITS_REM < 4
170#define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
171#else
172#define V_L1_BITS V_L1_BITS_REM
173#endif
174
175#define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
176#define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
177
178#define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
179#define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
180
bellard83fb7ad2004-07-05 21:25:26 +0000181unsigned long qemu_real_host_page_size;
182unsigned long qemu_host_page_bits;
183unsigned long qemu_host_page_size;
184unsigned long qemu_host_page_mask;
bellard54936002003-05-13 00:25:15 +0000185
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800186/* This is a multi-level map on the virtual address space.
187 The bottom level has pointers to PageDesc. */
188static void *l1_map[V_L1_SIZE];
bellard54936002003-05-13 00:25:15 +0000189
pbrooke2eef172008-06-08 01:09:01 +0000190#if !defined(CONFIG_USER_ONLY)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800191/* This is a multi-level map on the physical address space.
192 The bottom level has pointers to PhysPageDesc. */
193static void *l1_phys_map[P_L1_SIZE];
Paul Brook6d9a1302010-02-28 23:55:53 +0000194
pbrooke2eef172008-06-08 01:09:01 +0000195static void io_mem_init(void);
196
bellard33417e72003-08-10 21:47:01 +0000197/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000198CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
199CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000200void *io_mem_opaque[IO_MEM_NB_ENTRIES];
blueswir1511d2b12009-03-07 15:32:56 +0000201static char io_mem_used[IO_MEM_NB_ENTRIES];
pbrook6658ffb2007-03-16 23:58:11 +0000202static int io_mem_watch;
203#endif
bellard33417e72003-08-10 21:47:01 +0000204
bellard34865132003-10-05 14:28:56 +0000205/* log support */
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200206#ifdef WIN32
207static const char *logfilename = "qemu.log";
208#else
blueswir1d9b630f2008-10-05 09:57:08 +0000209static const char *logfilename = "/tmp/qemu.log";
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200210#endif
bellard34865132003-10-05 14:28:56 +0000211FILE *logfile;
212int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000213static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000214
bellarde3db7222005-01-26 22:00:47 +0000215/* statistics */
216static int tlb_flush_count;
217static int tb_flush_count;
218static int tb_phys_invalidate_count;
219
bellard7cb69ca2008-05-10 10:55:51 +0000220#ifdef _WIN32
221static void map_exec(void *addr, long size)
222{
223 DWORD old_protect;
224 VirtualProtect(addr, size,
225 PAGE_EXECUTE_READWRITE, &old_protect);
226
227}
228#else
229static void map_exec(void *addr, long size)
230{
bellard43694152008-05-29 09:35:57 +0000231 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000232
bellard43694152008-05-29 09:35:57 +0000233 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000234 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000235 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000236
237 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000238 end += page_size - 1;
239 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000240
241 mprotect((void *)start, end - start,
242 PROT_READ | PROT_WRITE | PROT_EXEC);
243}
244#endif
245
bellardb346ff42003-06-15 20:05:50 +0000246static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000247{
bellard83fb7ad2004-07-05 21:25:26 +0000248 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000249 TARGET_PAGE_SIZE */
aliguoric2b48b62008-11-11 22:06:42 +0000250#ifdef _WIN32
251 {
252 SYSTEM_INFO system_info;
253
254 GetSystemInfo(&system_info);
255 qemu_real_host_page_size = system_info.dwPageSize;
256 }
257#else
258 qemu_real_host_page_size = getpagesize();
259#endif
bellard83fb7ad2004-07-05 21:25:26 +0000260 if (qemu_host_page_size == 0)
261 qemu_host_page_size = qemu_real_host_page_size;
262 if (qemu_host_page_size < TARGET_PAGE_SIZE)
263 qemu_host_page_size = TARGET_PAGE_SIZE;
264 qemu_host_page_bits = 0;
265 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
266 qemu_host_page_bits++;
267 qemu_host_page_mask = ~(qemu_host_page_size - 1);
balrog50a95692007-12-12 01:16:23 +0000268
269#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
270 {
balrog50a95692007-12-12 01:16:23 +0000271 FILE *f;
balrog50a95692007-12-12 01:16:23 +0000272
pbrook07765902008-05-31 16:33:53 +0000273 last_brk = (unsigned long)sbrk(0);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800274
balrog50a95692007-12-12 01:16:23 +0000275 f = fopen("/proc/self/maps", "r");
276 if (f) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800277 mmap_lock();
278
balrog50a95692007-12-12 01:16:23 +0000279 do {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800280 unsigned long startaddr, endaddr;
281 int n;
282
283 n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
284
285 if (n == 2 && h2g_valid(startaddr)) {
286 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
287
288 if (h2g_valid(endaddr)) {
289 endaddr = h2g(endaddr);
290 } else {
291 endaddr = ~0ul;
292 }
293 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
balrog50a95692007-12-12 01:16:23 +0000294 }
295 } while (!feof(f));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800296
balrog50a95692007-12-12 01:16:23 +0000297 fclose(f);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800298 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000299 }
300 }
301#endif
bellard54936002003-05-13 00:25:15 +0000302}
303
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800304static PageDesc *page_find_alloc(target_ulong index, int alloc)
bellard54936002003-05-13 00:25:15 +0000305{
pbrook17e23772008-06-09 13:47:45 +0000306#if defined(CONFIG_USER_ONLY)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800307 /* We can't use qemu_malloc because it may recurse into a locked mutex.
308 Neither can we record the new pages we reserve while allocating a
309 given page because that may recurse into an unallocated page table
310 entry. Stuff the allocations we do make into a queue and process
311 them after having completed one entire page table allocation. */
312
313 unsigned long reserve[2 * (V_L1_SHIFT / L2_BITS)];
314 int reserve_idx = 0;
315
316# define ALLOC(P, SIZE) \
317 do { \
318 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
319 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
320 if (h2g_valid(P)) { \
321 reserve[reserve_idx] = h2g(P); \
322 reserve[reserve_idx + 1] = SIZE; \
323 reserve_idx += 2; \
324 } \
325 } while (0)
pbrook17e23772008-06-09 13:47:45 +0000326#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800327# define ALLOC(P, SIZE) \
328 do { P = qemu_mallocz(SIZE); } while (0)
pbrook17e23772008-06-09 13:47:45 +0000329#endif
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800330
331 PageDesc *pd;
332 void **lp;
333 int i;
334
335 /* Level 1. Always allocated. */
336 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
337
338 /* Level 2..N-1. */
339 for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
340 void **p = *lp;
341
342 if (p == NULL) {
343 if (!alloc) {
344 return NULL;
345 }
346 ALLOC(p, sizeof(void *) * L2_SIZE);
347 *lp = p;
348 }
349
350 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000351 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800352
353 pd = *lp;
354 if (pd == NULL) {
355 if (!alloc) {
356 return NULL;
357 }
358 ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
359 *lp = pd;
360 }
361
362#undef ALLOC
363#if defined(CONFIG_USER_ONLY)
364 for (i = 0; i < reserve_idx; i += 2) {
365 unsigned long addr = reserve[i];
366 unsigned long len = reserve[i + 1];
367
368 page_set_flags(addr & TARGET_PAGE_MASK,
369 TARGET_PAGE_ALIGN(addr + len),
370 PAGE_RESERVED);
371 }
372#endif
373
374 return pd + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000375}
376
aurel3200f82b82008-04-27 21:12:55 +0000377static inline PageDesc *page_find(target_ulong index)
bellard54936002003-05-13 00:25:15 +0000378{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800379 return page_find_alloc(index, 0);
bellard54936002003-05-13 00:25:15 +0000380}
381
Paul Brook6d9a1302010-02-28 23:55:53 +0000382#if !defined(CONFIG_USER_ONLY)
Anthony Liguoric227f092009-10-01 16:12:16 -0500383static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000384{
pbrooke3f4e2a2006-04-08 20:02:06 +0000385 PhysPageDesc *pd;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800386 void **lp;
387 int i;
bellard92e873b2004-05-21 14:52:29 +0000388
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800389 /* Level 1. Always allocated. */
390 lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000391
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800392 /* Level 2..N-1. */
393 for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
394 void **p = *lp;
395 if (p == NULL) {
396 if (!alloc) {
397 return NULL;
398 }
399 *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE);
400 }
401 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000402 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800403
pbrooke3f4e2a2006-04-08 20:02:06 +0000404 pd = *lp;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800405 if (pd == NULL) {
pbrooke3f4e2a2006-04-08 20:02:06 +0000406 int i;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800407
408 if (!alloc) {
bellard108c49b2005-07-24 12:55:09 +0000409 return NULL;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800410 }
411
412 *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE);
413
pbrook67c4d232009-02-23 13:16:07 +0000414 for (i = 0; i < L2_SIZE; i++) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800415 pd[i].phys_offset = IO_MEM_UNASSIGNED;
416 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
pbrook67c4d232009-02-23 13:16:07 +0000417 }
bellard92e873b2004-05-21 14:52:29 +0000418 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800419
420 return pd + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000421}
422
Anthony Liguoric227f092009-10-01 16:12:16 -0500423static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000424{
bellard108c49b2005-07-24 12:55:09 +0000425 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000426}
427
Anthony Liguoric227f092009-10-01 16:12:16 -0500428static void tlb_protect_code(ram_addr_t ram_addr);
429static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000430 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000431#define mmap_lock() do { } while(0)
432#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000433#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000434
bellard43694152008-05-29 09:35:57 +0000435#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
436
437#if defined(CONFIG_USER_ONLY)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100438/* Currently it is not recommended to allocate big chunks of data in
bellard43694152008-05-29 09:35:57 +0000439 user mode. It will change when a dedicated libc will be used */
440#define USE_STATIC_CODE_GEN_BUFFER
441#endif
442
443#ifdef USE_STATIC_CODE_GEN_BUFFER
444static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
445#endif
446
blueswir18fcd3692008-08-17 20:26:25 +0000447static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000448{
bellard43694152008-05-29 09:35:57 +0000449#ifdef USE_STATIC_CODE_GEN_BUFFER
450 code_gen_buffer = static_code_gen_buffer;
451 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
452 map_exec(code_gen_buffer, code_gen_buffer_size);
453#else
bellard26a5f132008-05-28 12:30:31 +0000454 code_gen_buffer_size = tb_size;
455 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000456#if defined(CONFIG_USER_ONLY)
457 /* in user mode, phys_ram_size is not meaningful */
458 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
459#else
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100460 /* XXX: needs adjustments */
pbrook94a6b542009-04-11 17:15:54 +0000461 code_gen_buffer_size = (unsigned long)(ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000462#endif
bellard26a5f132008-05-28 12:30:31 +0000463 }
464 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
465 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
466 /* The code gen buffer location may have constraints depending on
467 the host cpu and OS */
468#if defined(__linux__)
469 {
470 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000471 void *start = NULL;
472
bellard26a5f132008-05-28 12:30:31 +0000473 flags = MAP_PRIVATE | MAP_ANONYMOUS;
474#if defined(__x86_64__)
475 flags |= MAP_32BIT;
476 /* Cannot map more than that */
477 if (code_gen_buffer_size > (800 * 1024 * 1024))
478 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000479#elif defined(__sparc_v9__)
480 // Map the buffer below 2G, so we can use direct calls and branches
481 flags |= MAP_FIXED;
482 start = (void *) 0x60000000UL;
483 if (code_gen_buffer_size > (512 * 1024 * 1024))
484 code_gen_buffer_size = (512 * 1024 * 1024);
balrog1cb06612008-12-01 02:10:17 +0000485#elif defined(__arm__)
balrog63d41242008-12-01 02:19:41 +0000486 /* Map the buffer below 32M, so we can use direct calls and branches */
balrog1cb06612008-12-01 02:10:17 +0000487 flags |= MAP_FIXED;
488 start = (void *) 0x01000000UL;
489 if (code_gen_buffer_size > 16 * 1024 * 1024)
490 code_gen_buffer_size = 16 * 1024 * 1024;
bellard26a5f132008-05-28 12:30:31 +0000491#endif
blueswir1141ac462008-07-26 15:05:57 +0000492 code_gen_buffer = mmap(start, code_gen_buffer_size,
493 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000494 flags, -1, 0);
495 if (code_gen_buffer == MAP_FAILED) {
496 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
497 exit(1);
498 }
499 }
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100500#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguori06e67a82008-09-27 15:32:41 +0000501 {
502 int flags;
503 void *addr = NULL;
504 flags = MAP_PRIVATE | MAP_ANONYMOUS;
505#if defined(__x86_64__)
506 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
507 * 0x40000000 is free */
508 flags |= MAP_FIXED;
509 addr = (void *)0x40000000;
510 /* Cannot map more than that */
511 if (code_gen_buffer_size > (800 * 1024 * 1024))
512 code_gen_buffer_size = (800 * 1024 * 1024);
513#endif
514 code_gen_buffer = mmap(addr, code_gen_buffer_size,
515 PROT_WRITE | PROT_READ | PROT_EXEC,
516 flags, -1, 0);
517 if (code_gen_buffer == MAP_FAILED) {
518 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
519 exit(1);
520 }
521 }
bellard26a5f132008-05-28 12:30:31 +0000522#else
523 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
bellard26a5f132008-05-28 12:30:31 +0000524 map_exec(code_gen_buffer, code_gen_buffer_size);
525#endif
bellard43694152008-05-29 09:35:57 +0000526#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000527 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
528 code_gen_buffer_max_size = code_gen_buffer_size -
529 code_gen_max_block_size();
530 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
531 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
532}
533
534/* Must be called before using the QEMU cpus. 'tb_size' is the size
535 (in bytes) allocated to the translation buffer. Zero means default
536 size. */
537void cpu_exec_init_all(unsigned long tb_size)
538{
bellard26a5f132008-05-28 12:30:31 +0000539 cpu_gen_init();
540 code_gen_alloc(tb_size);
541 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000542 page_init();
pbrooke2eef172008-06-08 01:09:01 +0000543#if !defined(CONFIG_USER_ONLY)
bellard26a5f132008-05-28 12:30:31 +0000544 io_mem_init();
pbrooke2eef172008-06-08 01:09:01 +0000545#endif
bellard26a5f132008-05-28 12:30:31 +0000546}
547
pbrook9656f322008-07-01 20:01:19 +0000548#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
549
Juan Quintelae59fb372009-09-29 22:48:21 +0200550static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200551{
552 CPUState *env = opaque;
553
aurel323098dba2009-03-07 21:28:24 +0000554 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
555 version_id is increased. */
556 env->interrupt_request &= ~0x01;
pbrook9656f322008-07-01 20:01:19 +0000557 tlb_flush(env, 1);
558
559 return 0;
560}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200561
562static const VMStateDescription vmstate_cpu_common = {
563 .name = "cpu_common",
564 .version_id = 1,
565 .minimum_version_id = 1,
566 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200567 .post_load = cpu_common_post_load,
568 .fields = (VMStateField []) {
569 VMSTATE_UINT32(halted, CPUState),
570 VMSTATE_UINT32(interrupt_request, CPUState),
571 VMSTATE_END_OF_LIST()
572 }
573};
pbrook9656f322008-07-01 20:01:19 +0000574#endif
575
Glauber Costa950f1472009-06-09 12:15:18 -0400576CPUState *qemu_get_cpu(int cpu)
577{
578 CPUState *env = first_cpu;
579
580 while (env) {
581 if (env->cpu_index == cpu)
582 break;
583 env = env->next_cpu;
584 }
585
586 return env;
587}
588
bellard6a00d602005-11-21 23:25:50 +0000589void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000590{
bellard6a00d602005-11-21 23:25:50 +0000591 CPUState **penv;
592 int cpu_index;
593
pbrookc2764712009-03-07 15:24:59 +0000594#if defined(CONFIG_USER_ONLY)
595 cpu_list_lock();
596#endif
bellard6a00d602005-11-21 23:25:50 +0000597 env->next_cpu = NULL;
598 penv = &first_cpu;
599 cpu_index = 0;
600 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700601 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000602 cpu_index++;
603 }
604 env->cpu_index = cpu_index;
aliguori268a3622009-04-21 22:30:27 +0000605 env->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000606 QTAILQ_INIT(&env->breakpoints);
607 QTAILQ_INIT(&env->watchpoints);
bellard6a00d602005-11-21 23:25:50 +0000608 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000609#if defined(CONFIG_USER_ONLY)
610 cpu_list_unlock();
611#endif
pbrookb3c77242008-06-30 16:31:04 +0000612#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200613 vmstate_register(cpu_index, &vmstate_cpu_common, env);
pbrookb3c77242008-06-30 16:31:04 +0000614 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
615 cpu_save, cpu_load, env);
616#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000617}
618
bellard9fa3e852004-01-04 18:06:42 +0000619static inline void invalidate_page_bitmap(PageDesc *p)
620{
621 if (p->code_bitmap) {
bellard59817cc2004-02-16 22:01:13 +0000622 qemu_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000623 p->code_bitmap = NULL;
624 }
625 p->code_write_count = 0;
626}
627
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800628/* Set to NULL all the 'first_tb' fields in all PageDescs. */
629
630static void page_flush_tb_1 (int level, void **lp)
631{
632 int i;
633
634 if (*lp == NULL) {
635 return;
636 }
637 if (level == 0) {
638 PageDesc *pd = *lp;
639 for (i = 0; i < L2_BITS; ++i) {
640 pd[i].first_tb = NULL;
641 invalidate_page_bitmap(pd + i);
642 }
643 } else {
644 void **pp = *lp;
645 for (i = 0; i < L2_BITS; ++i) {
646 page_flush_tb_1 (level - 1, pp + i);
647 }
648 }
649}
650
bellardfd6ce8f2003-05-14 19:00:11 +0000651static void page_flush_tb(void)
652{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800653 int i;
654 for (i = 0; i < V_L1_SIZE; i++) {
655 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
bellardfd6ce8f2003-05-14 19:00:11 +0000656 }
657}
658
659/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000660/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000661void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000662{
bellard6a00d602005-11-21 23:25:50 +0000663 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000664#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000665 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
666 (unsigned long)(code_gen_ptr - code_gen_buffer),
667 nb_tbs, nb_tbs > 0 ?
668 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000669#endif
bellard26a5f132008-05-28 12:30:31 +0000670 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000671 cpu_abort(env1, "Internal error: code buffer overflow\n");
672
bellardfd6ce8f2003-05-14 19:00:11 +0000673 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000674
bellard6a00d602005-11-21 23:25:50 +0000675 for(env = first_cpu; env != NULL; env = env->next_cpu) {
676 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
677 }
bellard9fa3e852004-01-04 18:06:42 +0000678
bellard8a8a6082004-10-03 13:36:49 +0000679 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000680 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000681
bellardfd6ce8f2003-05-14 19:00:11 +0000682 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000683 /* XXX: flush processor icache at this point if cache flush is
684 expensive */
bellarde3db7222005-01-26 22:00:47 +0000685 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000686}
687
688#ifdef DEBUG_TB_CHECK
689
j_mayerbc98a7e2007-04-04 07:55:12 +0000690static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000691{
692 TranslationBlock *tb;
693 int i;
694 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000695 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
696 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000697 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
698 address >= tb->pc + tb->size)) {
Blue Swirl0bf9e312009-07-20 17:19:25 +0000699 printf("ERROR invalidate: address=" TARGET_FMT_lx
700 " PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000701 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000702 }
703 }
704 }
705}
706
707/* verify that all the pages have correct rights for code */
708static void tb_page_check(void)
709{
710 TranslationBlock *tb;
711 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000712
pbrook99773bd2006-04-16 15:14:59 +0000713 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
714 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000715 flags1 = page_get_flags(tb->pc);
716 flags2 = page_get_flags(tb->pc + tb->size - 1);
717 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
718 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000719 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000720 }
721 }
722 }
723}
724
725#endif
726
727/* invalidate one TB */
728static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
729 int next_offset)
730{
731 TranslationBlock *tb1;
732 for(;;) {
733 tb1 = *ptb;
734 if (tb1 == tb) {
735 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
736 break;
737 }
738 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
739 }
740}
741
bellard9fa3e852004-01-04 18:06:42 +0000742static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
743{
744 TranslationBlock *tb1;
745 unsigned int n1;
746
747 for(;;) {
748 tb1 = *ptb;
749 n1 = (long)tb1 & 3;
750 tb1 = (TranslationBlock *)((long)tb1 & ~3);
751 if (tb1 == tb) {
752 *ptb = tb1->page_next[n1];
753 break;
754 }
755 ptb = &tb1->page_next[n1];
756 }
757}
758
bellardd4e81642003-05-25 16:46:15 +0000759static inline void tb_jmp_remove(TranslationBlock *tb, int n)
760{
761 TranslationBlock *tb1, **ptb;
762 unsigned int n1;
763
764 ptb = &tb->jmp_next[n];
765 tb1 = *ptb;
766 if (tb1) {
767 /* find tb(n) in circular list */
768 for(;;) {
769 tb1 = *ptb;
770 n1 = (long)tb1 & 3;
771 tb1 = (TranslationBlock *)((long)tb1 & ~3);
772 if (n1 == n && tb1 == tb)
773 break;
774 if (n1 == 2) {
775 ptb = &tb1->jmp_first;
776 } else {
777 ptb = &tb1->jmp_next[n1];
778 }
779 }
780 /* now we can suppress tb(n) from the list */
781 *ptb = tb->jmp_next[n];
782
783 tb->jmp_next[n] = NULL;
784 }
785}
786
787/* reset the jump entry 'n' of a TB so that it is not chained to
788 another TB */
789static inline void tb_reset_jump(TranslationBlock *tb, int n)
790{
791 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
792}
793
pbrook2e70f6e2008-06-29 01:03:05 +0000794void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000795{
bellard6a00d602005-11-21 23:25:50 +0000796 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000797 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000798 unsigned int h, n1;
Anthony Liguoric227f092009-10-01 16:12:16 -0500799 target_phys_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000800 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000801
bellard9fa3e852004-01-04 18:06:42 +0000802 /* remove the TB from the hash list */
803 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
804 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000805 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000806 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000807
bellard9fa3e852004-01-04 18:06:42 +0000808 /* remove the TB from the page list */
809 if (tb->page_addr[0] != page_addr) {
810 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
811 tb_page_remove(&p->first_tb, tb);
812 invalidate_page_bitmap(p);
813 }
814 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
815 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
816 tb_page_remove(&p->first_tb, tb);
817 invalidate_page_bitmap(p);
818 }
819
bellard8a40a182005-11-20 10:35:40 +0000820 tb_invalidated_flag = 1;
821
822 /* remove the TB from the hash list */
823 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000824 for(env = first_cpu; env != NULL; env = env->next_cpu) {
825 if (env->tb_jmp_cache[h] == tb)
826 env->tb_jmp_cache[h] = NULL;
827 }
bellard8a40a182005-11-20 10:35:40 +0000828
829 /* suppress this TB from the two jump lists */
830 tb_jmp_remove(tb, 0);
831 tb_jmp_remove(tb, 1);
832
833 /* suppress any remaining jumps to this TB */
834 tb1 = tb->jmp_first;
835 for(;;) {
836 n1 = (long)tb1 & 3;
837 if (n1 == 2)
838 break;
839 tb1 = (TranslationBlock *)((long)tb1 & ~3);
840 tb2 = tb1->jmp_next[n1];
841 tb_reset_jump(tb1, n1);
842 tb1->jmp_next[n1] = NULL;
843 tb1 = tb2;
844 }
845 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
846
bellarde3db7222005-01-26 22:00:47 +0000847 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000848}
849
850static inline void set_bits(uint8_t *tab, int start, int len)
851{
852 int end, mask, end1;
853
854 end = start + len;
855 tab += start >> 3;
856 mask = 0xff << (start & 7);
857 if ((start & ~7) == (end & ~7)) {
858 if (start < end) {
859 mask &= ~(0xff << (end & 7));
860 *tab |= mask;
861 }
862 } else {
863 *tab++ |= mask;
864 start = (start + 8) & ~7;
865 end1 = end & ~7;
866 while (start < end1) {
867 *tab++ = 0xff;
868 start += 8;
869 }
870 if (start < end) {
871 mask = ~(0xff << (end & 7));
872 *tab |= mask;
873 }
874 }
875}
876
877static void build_page_bitmap(PageDesc *p)
878{
879 int n, tb_start, tb_end;
880 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000881
pbrookb2a70812008-06-09 13:57:23 +0000882 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000883
884 tb = p->first_tb;
885 while (tb != NULL) {
886 n = (long)tb & 3;
887 tb = (TranslationBlock *)((long)tb & ~3);
888 /* NOTE: this is subtle as a TB may span two physical pages */
889 if (n == 0) {
890 /* NOTE: tb_end may be after the end of the page, but
891 it is not a problem */
892 tb_start = tb->pc & ~TARGET_PAGE_MASK;
893 tb_end = tb_start + tb->size;
894 if (tb_end > TARGET_PAGE_SIZE)
895 tb_end = TARGET_PAGE_SIZE;
896 } else {
897 tb_start = 0;
898 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
899 }
900 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
901 tb = tb->page_next[n];
902 }
903}
904
pbrook2e70f6e2008-06-29 01:03:05 +0000905TranslationBlock *tb_gen_code(CPUState *env,
906 target_ulong pc, target_ulong cs_base,
907 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000908{
909 TranslationBlock *tb;
910 uint8_t *tc_ptr;
911 target_ulong phys_pc, phys_page2, virt_page2;
912 int code_gen_size;
913
bellardc27004e2005-01-03 23:35:10 +0000914 phys_pc = get_phys_addr_code(env, pc);
915 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000916 if (!tb) {
917 /* flush must be done */
918 tb_flush(env);
919 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000920 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000921 /* Don't forget to invalidate previous TB info. */
922 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000923 }
924 tc_ptr = code_gen_ptr;
925 tb->tc_ptr = tc_ptr;
926 tb->cs_base = cs_base;
927 tb->flags = flags;
928 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +0000929 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +0000930 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 +0000931
bellardd720b932004-04-25 17:57:43 +0000932 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +0000933 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +0000934 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +0000935 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
bellardd720b932004-04-25 17:57:43 +0000936 phys_page2 = get_phys_addr_code(env, virt_page2);
937 }
938 tb_link_phys(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +0000939 return tb;
bellardd720b932004-04-25 17:57:43 +0000940}
ths3b46e622007-09-17 08:09:54 +0000941
bellard9fa3e852004-01-04 18:06:42 +0000942/* invalidate all TBs which intersect with the target physical page
943 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +0000944 the same physical page. 'is_cpu_write_access' should be true if called
945 from a real cpu write access: the virtual CPU will exit the current
946 TB if code is modified inside this TB. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500947void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
bellardd720b932004-04-25 17:57:43 +0000948 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +0000949{
aliguori6b917542008-11-18 19:46:41 +0000950 TranslationBlock *tb, *tb_next, *saved_tb;
bellardd720b932004-04-25 17:57:43 +0000951 CPUState *env = cpu_single_env;
bellard9fa3e852004-01-04 18:06:42 +0000952 target_ulong tb_start, tb_end;
aliguori6b917542008-11-18 19:46:41 +0000953 PageDesc *p;
954 int n;
955#ifdef TARGET_HAS_PRECISE_SMC
956 int current_tb_not_found = is_cpu_write_access;
957 TranslationBlock *current_tb = NULL;
958 int current_tb_modified = 0;
959 target_ulong current_pc = 0;
960 target_ulong current_cs_base = 0;
961 int current_flags = 0;
962#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +0000963
964 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +0000965 if (!p)
bellard9fa3e852004-01-04 18:06:42 +0000966 return;
ths5fafdf22007-09-16 21:08:06 +0000967 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +0000968 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
969 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +0000970 /* build code bitmap */
971 build_page_bitmap(p);
972 }
973
974 /* we remove all the TBs in the range [start, end[ */
975 /* XXX: see if in some cases it could be faster to invalidate all the code */
976 tb = p->first_tb;
977 while (tb != NULL) {
978 n = (long)tb & 3;
979 tb = (TranslationBlock *)((long)tb & ~3);
980 tb_next = tb->page_next[n];
981 /* NOTE: this is subtle as a TB may span two physical pages */
982 if (n == 0) {
983 /* NOTE: tb_end may be after the end of the page, but
984 it is not a problem */
985 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
986 tb_end = tb_start + tb->size;
987 } else {
988 tb_start = tb->page_addr[1];
989 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
990 }
991 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +0000992#ifdef TARGET_HAS_PRECISE_SMC
993 if (current_tb_not_found) {
994 current_tb_not_found = 0;
995 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +0000996 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +0000997 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +0000998 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +0000999 }
1000 }
1001 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001002 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001003 /* If we are modifying the current TB, we must stop
1004 its execution. We could be more precise by checking
1005 that the modification is after the current PC, but it
1006 would require a specialized function to partially
1007 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001008
bellardd720b932004-04-25 17:57:43 +00001009 current_tb_modified = 1;
ths5fafdf22007-09-16 21:08:06 +00001010 cpu_restore_state(current_tb, env,
pbrook2e70f6e2008-06-29 01:03:05 +00001011 env->mem_io_pc, NULL);
aliguori6b917542008-11-18 19:46:41 +00001012 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1013 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001014 }
1015#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +00001016 /* we need to do that to handle the case where a signal
1017 occurs while doing tb_phys_invalidate() */
1018 saved_tb = NULL;
1019 if (env) {
1020 saved_tb = env->current_tb;
1021 env->current_tb = NULL;
1022 }
bellard9fa3e852004-01-04 18:06:42 +00001023 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +00001024 if (env) {
1025 env->current_tb = saved_tb;
1026 if (env->interrupt_request && env->current_tb)
1027 cpu_interrupt(env, env->interrupt_request);
1028 }
bellard9fa3e852004-01-04 18:06:42 +00001029 }
1030 tb = tb_next;
1031 }
1032#if !defined(CONFIG_USER_ONLY)
1033 /* if no code remaining, no need to continue to use slow writes */
1034 if (!p->first_tb) {
1035 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +00001036 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +00001037 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +00001038 }
1039 }
1040#endif
1041#ifdef TARGET_HAS_PRECISE_SMC
1042 if (current_tb_modified) {
1043 /* we generate a block containing just the instruction
1044 modifying the memory. It will ensure that it cannot modify
1045 itself */
bellardea1c1802004-06-14 18:56:36 +00001046 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001047 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001048 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001049 }
1050#endif
1051}
1052
1053/* len must be <= 8 and start must be a multiple of len */
Anthony Liguoric227f092009-10-01 16:12:16 -05001054static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +00001055{
1056 PageDesc *p;
1057 int offset, b;
bellard59817cc2004-02-16 22:01:13 +00001058#if 0
bellarda4193c82004-06-03 14:01:43 +00001059 if (1) {
aliguori93fcfe32009-01-15 22:34:14 +00001060 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1061 cpu_single_env->mem_io_vaddr, len,
1062 cpu_single_env->eip,
1063 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
bellard59817cc2004-02-16 22:01:13 +00001064 }
1065#endif
bellard9fa3e852004-01-04 18:06:42 +00001066 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001067 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001068 return;
1069 if (p->code_bitmap) {
1070 offset = start & ~TARGET_PAGE_MASK;
1071 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1072 if (b & ((1 << len) - 1))
1073 goto do_invalidate;
1074 } else {
1075 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001076 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001077 }
1078}
1079
bellard9fa3e852004-01-04 18:06:42 +00001080#if !defined(CONFIG_SOFTMMU)
Anthony Liguoric227f092009-10-01 16:12:16 -05001081static void tb_invalidate_phys_page(target_phys_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001082 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001083{
aliguori6b917542008-11-18 19:46:41 +00001084 TranslationBlock *tb;
bellard9fa3e852004-01-04 18:06:42 +00001085 PageDesc *p;
aliguori6b917542008-11-18 19:46:41 +00001086 int n;
bellardd720b932004-04-25 17:57:43 +00001087#ifdef TARGET_HAS_PRECISE_SMC
aliguori6b917542008-11-18 19:46:41 +00001088 TranslationBlock *current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001089 CPUState *env = cpu_single_env;
aliguori6b917542008-11-18 19:46:41 +00001090 int current_tb_modified = 0;
1091 target_ulong current_pc = 0;
1092 target_ulong current_cs_base = 0;
1093 int current_flags = 0;
bellardd720b932004-04-25 17:57:43 +00001094#endif
bellard9fa3e852004-01-04 18:06:42 +00001095
1096 addr &= TARGET_PAGE_MASK;
1097 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001098 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001099 return;
1100 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001101#ifdef TARGET_HAS_PRECISE_SMC
1102 if (tb && pc != 0) {
1103 current_tb = tb_find_pc(pc);
1104 }
1105#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001106 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001107 n = (long)tb & 3;
1108 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001109#ifdef TARGET_HAS_PRECISE_SMC
1110 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001111 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001112 /* If we are modifying the current TB, we must stop
1113 its execution. We could be more precise by checking
1114 that the modification is after the current PC, but it
1115 would require a specialized function to partially
1116 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001117
bellardd720b932004-04-25 17:57:43 +00001118 current_tb_modified = 1;
1119 cpu_restore_state(current_tb, env, pc, puc);
aliguori6b917542008-11-18 19:46:41 +00001120 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1121 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001122 }
1123#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001124 tb_phys_invalidate(tb, addr);
1125 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001126 }
1127 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001128#ifdef TARGET_HAS_PRECISE_SMC
1129 if (current_tb_modified) {
1130 /* we generate a block containing just the instruction
1131 modifying the memory. It will ensure that it cannot modify
1132 itself */
bellardea1c1802004-06-14 18:56:36 +00001133 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001134 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001135 cpu_resume_from_signal(env, puc);
1136 }
1137#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001138}
bellard9fa3e852004-01-04 18:06:42 +00001139#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001140
1141/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001142static inline void tb_alloc_page(TranslationBlock *tb,
pbrook53a59602006-03-25 19:31:22 +00001143 unsigned int n, target_ulong page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001144{
1145 PageDesc *p;
bellard9fa3e852004-01-04 18:06:42 +00001146 TranslationBlock *last_first_tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001147
bellard9fa3e852004-01-04 18:06:42 +00001148 tb->page_addr[n] = page_addr;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001149 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
bellard9fa3e852004-01-04 18:06:42 +00001150 tb->page_next[n] = p->first_tb;
1151 last_first_tb = p->first_tb;
1152 p->first_tb = (TranslationBlock *)((long)tb | n);
1153 invalidate_page_bitmap(p);
1154
bellard107db442004-06-22 18:48:46 +00001155#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001156
bellard9fa3e852004-01-04 18:06:42 +00001157#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001158 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001159 target_ulong addr;
1160 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001161 int prot;
1162
bellardfd6ce8f2003-05-14 19:00:11 +00001163 /* force the host page as non writable (writes will have a
1164 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001165 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001166 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001167 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1168 addr += TARGET_PAGE_SIZE) {
1169
1170 p2 = page_find (addr >> TARGET_PAGE_BITS);
1171 if (!p2)
1172 continue;
1173 prot |= p2->flags;
1174 p2->flags &= ~PAGE_WRITE;
1175 page_get_flags(addr);
1176 }
ths5fafdf22007-09-16 21:08:06 +00001177 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001178 (prot & PAGE_BITS) & ~PAGE_WRITE);
1179#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001180 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001181 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001182#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001183 }
bellard9fa3e852004-01-04 18:06:42 +00001184#else
1185 /* if some code is already present, then the pages are already
1186 protected. So we handle the case where only the first TB is
1187 allocated in a physical page */
1188 if (!last_first_tb) {
bellard6a00d602005-11-21 23:25:50 +00001189 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001190 }
1191#endif
bellardd720b932004-04-25 17:57:43 +00001192
1193#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001194}
1195
1196/* Allocate a new translation block. Flush the translation buffer if
1197 too many translation blocks or too much generated code. */
bellardc27004e2005-01-03 23:35:10 +00001198TranslationBlock *tb_alloc(target_ulong pc)
bellardfd6ce8f2003-05-14 19:00:11 +00001199{
1200 TranslationBlock *tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001201
bellard26a5f132008-05-28 12:30:31 +00001202 if (nb_tbs >= code_gen_max_blocks ||
1203 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
bellardd4e81642003-05-25 16:46:15 +00001204 return NULL;
bellardfd6ce8f2003-05-14 19:00:11 +00001205 tb = &tbs[nb_tbs++];
1206 tb->pc = pc;
bellardb448f2f2004-02-25 23:24:04 +00001207 tb->cflags = 0;
bellardd4e81642003-05-25 16:46:15 +00001208 return tb;
1209}
1210
pbrook2e70f6e2008-06-29 01:03:05 +00001211void tb_free(TranslationBlock *tb)
1212{
thsbf20dc02008-06-30 17:22:19 +00001213 /* In practice this is mostly used for single use temporary TB
pbrook2e70f6e2008-06-29 01:03:05 +00001214 Ignore the hard cases and just back up if this TB happens to
1215 be the last one generated. */
1216 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1217 code_gen_ptr = tb->tc_ptr;
1218 nb_tbs--;
1219 }
1220}
1221
bellard9fa3e852004-01-04 18:06:42 +00001222/* add a new TB and link it to the physical page tables. phys_page2 is
1223 (-1) to indicate that only one page contains the TB. */
ths5fafdf22007-09-16 21:08:06 +00001224void tb_link_phys(TranslationBlock *tb,
bellard9fa3e852004-01-04 18:06:42 +00001225 target_ulong phys_pc, target_ulong phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001226{
bellard9fa3e852004-01-04 18:06:42 +00001227 unsigned int h;
1228 TranslationBlock **ptb;
1229
pbrookc8a706f2008-06-02 16:16:42 +00001230 /* Grab the mmap lock to stop another thread invalidating this TB
1231 before we are done. */
1232 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001233 /* add in the physical hash table */
1234 h = tb_phys_hash_func(phys_pc);
1235 ptb = &tb_phys_hash[h];
1236 tb->phys_hash_next = *ptb;
1237 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001238
1239 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001240 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1241 if (phys_page2 != -1)
1242 tb_alloc_page(tb, 1, phys_page2);
1243 else
1244 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001245
bellardd4e81642003-05-25 16:46:15 +00001246 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1247 tb->jmp_next[0] = NULL;
1248 tb->jmp_next[1] = NULL;
1249
1250 /* init original jump addresses */
1251 if (tb->tb_next_offset[0] != 0xffff)
1252 tb_reset_jump(tb, 0);
1253 if (tb->tb_next_offset[1] != 0xffff)
1254 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001255
1256#ifdef DEBUG_TB_CHECK
1257 tb_page_check();
1258#endif
pbrookc8a706f2008-06-02 16:16:42 +00001259 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001260}
1261
bellarda513fe12003-05-27 23:29:48 +00001262/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1263 tb[1].tc_ptr. Return NULL if not found */
1264TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1265{
1266 int m_min, m_max, m;
1267 unsigned long v;
1268 TranslationBlock *tb;
1269
1270 if (nb_tbs <= 0)
1271 return NULL;
1272 if (tc_ptr < (unsigned long)code_gen_buffer ||
1273 tc_ptr >= (unsigned long)code_gen_ptr)
1274 return NULL;
1275 /* binary search (cf Knuth) */
1276 m_min = 0;
1277 m_max = nb_tbs - 1;
1278 while (m_min <= m_max) {
1279 m = (m_min + m_max) >> 1;
1280 tb = &tbs[m];
1281 v = (unsigned long)tb->tc_ptr;
1282 if (v == tc_ptr)
1283 return tb;
1284 else if (tc_ptr < v) {
1285 m_max = m - 1;
1286 } else {
1287 m_min = m + 1;
1288 }
ths5fafdf22007-09-16 21:08:06 +00001289 }
bellarda513fe12003-05-27 23:29:48 +00001290 return &tbs[m_max];
1291}
bellard75012672003-06-21 13:11:07 +00001292
bellardea041c02003-06-25 16:16:50 +00001293static void tb_reset_jump_recursive(TranslationBlock *tb);
1294
1295static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1296{
1297 TranslationBlock *tb1, *tb_next, **ptb;
1298 unsigned int n1;
1299
1300 tb1 = tb->jmp_next[n];
1301 if (tb1 != NULL) {
1302 /* find head of list */
1303 for(;;) {
1304 n1 = (long)tb1 & 3;
1305 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1306 if (n1 == 2)
1307 break;
1308 tb1 = tb1->jmp_next[n1];
1309 }
1310 /* we are now sure now that tb jumps to tb1 */
1311 tb_next = tb1;
1312
1313 /* remove tb from the jmp_first list */
1314 ptb = &tb_next->jmp_first;
1315 for(;;) {
1316 tb1 = *ptb;
1317 n1 = (long)tb1 & 3;
1318 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1319 if (n1 == n && tb1 == tb)
1320 break;
1321 ptb = &tb1->jmp_next[n1];
1322 }
1323 *ptb = tb->jmp_next[n];
1324 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001325
bellardea041c02003-06-25 16:16:50 +00001326 /* suppress the jump to next tb in generated code */
1327 tb_reset_jump(tb, n);
1328
bellard01243112004-01-04 15:48:17 +00001329 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001330 tb_reset_jump_recursive(tb_next);
1331 }
1332}
1333
1334static void tb_reset_jump_recursive(TranslationBlock *tb)
1335{
1336 tb_reset_jump_recursive2(tb, 0);
1337 tb_reset_jump_recursive2(tb, 1);
1338}
1339
bellard1fddef42005-04-17 19:16:13 +00001340#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +00001341#if defined(CONFIG_USER_ONLY)
1342static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1343{
1344 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1345}
1346#else
bellardd720b932004-04-25 17:57:43 +00001347static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1348{
Anthony Liguoric227f092009-10-01 16:12:16 -05001349 target_phys_addr_t addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00001350 target_ulong pd;
Anthony Liguoric227f092009-10-01 16:12:16 -05001351 ram_addr_t ram_addr;
pbrookc2f07f82006-04-08 17:14:56 +00001352 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001353
pbrookc2f07f82006-04-08 17:14:56 +00001354 addr = cpu_get_phys_page_debug(env, pc);
1355 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1356 if (!p) {
1357 pd = IO_MEM_UNASSIGNED;
1358 } else {
1359 pd = p->phys_offset;
1360 }
1361 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001362 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001363}
bellardc27004e2005-01-03 23:35:10 +00001364#endif
Paul Brook94df27f2010-02-28 23:47:45 +00001365#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +00001366
Paul Brookc527ee82010-03-01 03:31:14 +00001367#if defined(CONFIG_USER_ONLY)
1368void cpu_watchpoint_remove_all(CPUState *env, int mask)
1369
1370{
1371}
1372
1373int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1374 int flags, CPUWatchpoint **watchpoint)
1375{
1376 return -ENOSYS;
1377}
1378#else
pbrook6658ffb2007-03-16 23:58:11 +00001379/* Add a watchpoint. */
aliguoria1d1bb32008-11-18 20:07:32 +00001380int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1381 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +00001382{
aliguorib4051332008-11-18 20:14:20 +00001383 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +00001384 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001385
aliguorib4051332008-11-18 20:14:20 +00001386 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1387 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1388 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1389 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1390 return -EINVAL;
1391 }
aliguoria1d1bb32008-11-18 20:07:32 +00001392 wp = qemu_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +00001393
aliguoria1d1bb32008-11-18 20:07:32 +00001394 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +00001395 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +00001396 wp->flags = flags;
1397
aliguori2dc9f412008-11-18 20:56:59 +00001398 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001399 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001400 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001401 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001402 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001403
pbrook6658ffb2007-03-16 23:58:11 +00001404 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +00001405
1406 if (watchpoint)
1407 *watchpoint = wp;
1408 return 0;
pbrook6658ffb2007-03-16 23:58:11 +00001409}
1410
aliguoria1d1bb32008-11-18 20:07:32 +00001411/* Remove a specific watchpoint. */
1412int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1413 int flags)
pbrook6658ffb2007-03-16 23:58:11 +00001414{
aliguorib4051332008-11-18 20:14:20 +00001415 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +00001416 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001417
Blue Swirl72cf2d42009-09-12 07:36:22 +00001418 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001419 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +00001420 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +00001421 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +00001422 return 0;
1423 }
1424 }
aliguoria1d1bb32008-11-18 20:07:32 +00001425 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +00001426}
1427
aliguoria1d1bb32008-11-18 20:07:32 +00001428/* Remove a specific watchpoint by reference. */
1429void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1430{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001431 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +00001432
aliguoria1d1bb32008-11-18 20:07:32 +00001433 tlb_flush_page(env, watchpoint->vaddr);
1434
1435 qemu_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +00001436}
1437
aliguoria1d1bb32008-11-18 20:07:32 +00001438/* Remove all matching watchpoints. */
1439void cpu_watchpoint_remove_all(CPUState *env, int mask)
1440{
aliguoric0ce9982008-11-25 22:13:57 +00001441 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001442
Blue Swirl72cf2d42009-09-12 07:36:22 +00001443 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001444 if (wp->flags & mask)
1445 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +00001446 }
aliguoria1d1bb32008-11-18 20:07:32 +00001447}
Paul Brookc527ee82010-03-01 03:31:14 +00001448#endif
aliguoria1d1bb32008-11-18 20:07:32 +00001449
1450/* Add a breakpoint. */
1451int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1452 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001453{
bellard1fddef42005-04-17 19:16:13 +00001454#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001455 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +00001456
aliguoria1d1bb32008-11-18 20:07:32 +00001457 bp = qemu_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +00001458
1459 bp->pc = pc;
1460 bp->flags = flags;
1461
aliguori2dc9f412008-11-18 20:56:59 +00001462 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001463 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001464 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001465 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001466 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001467
1468 breakpoint_invalidate(env, pc);
1469
1470 if (breakpoint)
1471 *breakpoint = bp;
1472 return 0;
1473#else
1474 return -ENOSYS;
1475#endif
1476}
1477
1478/* Remove a specific breakpoint. */
1479int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1480{
1481#if defined(TARGET_HAS_ICE)
1482 CPUBreakpoint *bp;
1483
Blue Swirl72cf2d42009-09-12 07:36:22 +00001484 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00001485 if (bp->pc == pc && bp->flags == flags) {
1486 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +00001487 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +00001488 }
bellard4c3a88a2003-07-26 12:06:08 +00001489 }
aliguoria1d1bb32008-11-18 20:07:32 +00001490 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +00001491#else
aliguoria1d1bb32008-11-18 20:07:32 +00001492 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +00001493#endif
1494}
1495
aliguoria1d1bb32008-11-18 20:07:32 +00001496/* Remove a specific breakpoint by reference. */
1497void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001498{
bellard1fddef42005-04-17 19:16:13 +00001499#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001500 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +00001501
aliguoria1d1bb32008-11-18 20:07:32 +00001502 breakpoint_invalidate(env, breakpoint->pc);
1503
1504 qemu_free(breakpoint);
1505#endif
1506}
1507
1508/* Remove all matching breakpoints. */
1509void cpu_breakpoint_remove_all(CPUState *env, int mask)
1510{
1511#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001512 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001513
Blue Swirl72cf2d42009-09-12 07:36:22 +00001514 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001515 if (bp->flags & mask)
1516 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +00001517 }
bellard4c3a88a2003-07-26 12:06:08 +00001518#endif
1519}
1520
bellardc33a3462003-07-29 20:50:33 +00001521/* enable or disable single step mode. EXCP_DEBUG is returned by the
1522 CPU loop after each instruction */
1523void cpu_single_step(CPUState *env, int enabled)
1524{
bellard1fddef42005-04-17 19:16:13 +00001525#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001526 if (env->singlestep_enabled != enabled) {
1527 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +00001528 if (kvm_enabled())
1529 kvm_update_guest_debug(env, 0);
1530 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001531 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +00001532 /* XXX: only flush what is necessary */
1533 tb_flush(env);
1534 }
bellardc33a3462003-07-29 20:50:33 +00001535 }
1536#endif
1537}
1538
bellard34865132003-10-05 14:28:56 +00001539/* enable or disable low levels log */
1540void cpu_set_log(int log_flags)
1541{
1542 loglevel = log_flags;
1543 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001544 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001545 if (!logfile) {
1546 perror(logfilename);
1547 _exit(1);
1548 }
bellard9fa3e852004-01-04 18:06:42 +00001549#if !defined(CONFIG_SOFTMMU)
1550 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1551 {
blueswir1b55266b2008-09-20 08:07:15 +00001552 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001553 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1554 }
Filip Navarabf65f532009-07-27 10:02:04 -05001555#elif !defined(_WIN32)
1556 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellard34865132003-10-05 14:28:56 +00001557 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001558#endif
pbrooke735b912007-06-30 13:53:24 +00001559 log_append = 1;
1560 }
1561 if (!loglevel && logfile) {
1562 fclose(logfile);
1563 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001564 }
1565}
1566
1567void cpu_set_log_filename(const char *filename)
1568{
1569 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001570 if (logfile) {
1571 fclose(logfile);
1572 logfile = NULL;
1573 }
1574 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001575}
bellardc33a3462003-07-29 20:50:33 +00001576
aurel323098dba2009-03-07 21:28:24 +00001577static void cpu_unlink_tb(CPUState *env)
bellardea041c02003-06-25 16:16:50 +00001578{
pbrookd5975362008-06-07 20:50:51 +00001579 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1580 problem and hope the cpu will stop of its own accord. For userspace
1581 emulation this often isn't actually as bad as it sounds. Often
1582 signals are used primarily to interrupt blocking syscalls. */
aurel323098dba2009-03-07 21:28:24 +00001583 TranslationBlock *tb;
Anthony Liguoric227f092009-10-01 16:12:16 -05001584 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
aurel323098dba2009-03-07 21:28:24 +00001585
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001586 spin_lock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001587 tb = env->current_tb;
1588 /* if the cpu is currently executing code, we must unlink it and
1589 all the potentially executing TB */
Riku Voipiof76cfe52009-12-04 15:16:30 +02001590 if (tb) {
aurel323098dba2009-03-07 21:28:24 +00001591 env->current_tb = NULL;
1592 tb_reset_jump_recursive(tb);
aurel323098dba2009-03-07 21:28:24 +00001593 }
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001594 spin_unlock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001595}
1596
1597/* mask must never be zero, except for A20 change call */
1598void cpu_interrupt(CPUState *env, int mask)
1599{
1600 int old_mask;
1601
1602 old_mask = env->interrupt_request;
1603 env->interrupt_request |= mask;
1604
aliguori8edac962009-04-24 18:03:45 +00001605#ifndef CONFIG_USER_ONLY
1606 /*
1607 * If called from iothread context, wake the target cpu in
1608 * case its halted.
1609 */
1610 if (!qemu_cpu_self(env)) {
1611 qemu_cpu_kick(env);
1612 return;
1613 }
1614#endif
1615
pbrook2e70f6e2008-06-29 01:03:05 +00001616 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001617 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001618#ifndef CONFIG_USER_ONLY
pbrook2e70f6e2008-06-29 01:03:05 +00001619 if (!can_do_io(env)
aurel32be214e62009-03-06 21:48:00 +00001620 && (mask & ~old_mask) != 0) {
pbrook2e70f6e2008-06-29 01:03:05 +00001621 cpu_abort(env, "Raised interrupt while not in I/O function");
1622 }
1623#endif
1624 } else {
aurel323098dba2009-03-07 21:28:24 +00001625 cpu_unlink_tb(env);
bellardea041c02003-06-25 16:16:50 +00001626 }
1627}
1628
bellardb54ad042004-05-20 13:42:52 +00001629void cpu_reset_interrupt(CPUState *env, int mask)
1630{
1631 env->interrupt_request &= ~mask;
1632}
1633
aurel323098dba2009-03-07 21:28:24 +00001634void cpu_exit(CPUState *env)
1635{
1636 env->exit_request = 1;
1637 cpu_unlink_tb(env);
1638}
1639
blueswir1c7cd6a32008-10-02 18:27:46 +00001640const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001641 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001642 "show generated host assembly code for each compiled TB" },
1643 { CPU_LOG_TB_IN_ASM, "in_asm",
1644 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001645 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001646 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001647 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001648 "show micro ops "
1649#ifdef TARGET_I386
1650 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001651#endif
blueswir1e01a1152008-03-14 17:37:11 +00001652 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001653 { CPU_LOG_INT, "int",
1654 "show interrupts/exceptions in short format" },
1655 { CPU_LOG_EXEC, "exec",
1656 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001657 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001658 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001659#ifdef TARGET_I386
1660 { CPU_LOG_PCALL, "pcall",
1661 "show protected mode far calls/returns/exceptions" },
aliguorieca1bdf2009-01-26 19:54:31 +00001662 { CPU_LOG_RESET, "cpu_reset",
1663 "show CPU state before CPU resets" },
bellardf193c792004-03-21 17:06:25 +00001664#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001665#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001666 { CPU_LOG_IOPORT, "ioport",
1667 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001668#endif
bellardf193c792004-03-21 17:06:25 +00001669 { 0, NULL, NULL },
1670};
1671
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001672#ifndef CONFIG_USER_ONLY
1673static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
1674 = QLIST_HEAD_INITIALIZER(memory_client_list);
1675
1676static void cpu_notify_set_memory(target_phys_addr_t start_addr,
1677 ram_addr_t size,
1678 ram_addr_t phys_offset)
1679{
1680 CPUPhysMemoryClient *client;
1681 QLIST_FOREACH(client, &memory_client_list, list) {
1682 client->set_memory(client, start_addr, size, phys_offset);
1683 }
1684}
1685
1686static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
1687 target_phys_addr_t end)
1688{
1689 CPUPhysMemoryClient *client;
1690 QLIST_FOREACH(client, &memory_client_list, list) {
1691 int r = client->sync_dirty_bitmap(client, start, end);
1692 if (r < 0)
1693 return r;
1694 }
1695 return 0;
1696}
1697
1698static int cpu_notify_migration_log(int enable)
1699{
1700 CPUPhysMemoryClient *client;
1701 QLIST_FOREACH(client, &memory_client_list, list) {
1702 int r = client->migration_log(client, enable);
1703 if (r < 0)
1704 return r;
1705 }
1706 return 0;
1707}
1708
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001709static void phys_page_for_each_1(CPUPhysMemoryClient *client,
1710 int level, void **lp)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001711{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001712 int i;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001713
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001714 if (*lp == NULL) {
1715 return;
1716 }
1717 if (level == 0) {
1718 PhysPageDesc *pd = *lp;
1719 for (i = 0; i < L2_BITS; ++i) {
1720 if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
1721 client->set_memory(client, pd[i].region_offset,
1722 TARGET_PAGE_SIZE, pd[i].phys_offset);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001723 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001724 }
1725 } else {
1726 void **pp = *lp;
1727 for (i = 0; i < L2_BITS; ++i) {
1728 phys_page_for_each_1(client, level - 1, pp + i);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001729 }
1730 }
1731}
1732
1733static void phys_page_for_each(CPUPhysMemoryClient *client)
1734{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001735 int i;
1736 for (i = 0; i < P_L1_SIZE; ++i) {
1737 phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
1738 l1_phys_map + 1);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001739 }
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001740}
1741
1742void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
1743{
1744 QLIST_INSERT_HEAD(&memory_client_list, client, list);
1745 phys_page_for_each(client);
1746}
1747
1748void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
1749{
1750 QLIST_REMOVE(client, list);
1751}
1752#endif
1753
bellardf193c792004-03-21 17:06:25 +00001754static int cmp1(const char *s1, int n, const char *s2)
1755{
1756 if (strlen(s2) != n)
1757 return 0;
1758 return memcmp(s1, s2, n) == 0;
1759}
ths3b46e622007-09-17 08:09:54 +00001760
bellardf193c792004-03-21 17:06:25 +00001761/* takes a comma separated list of log masks. Return 0 if error. */
1762int cpu_str_to_log_mask(const char *str)
1763{
blueswir1c7cd6a32008-10-02 18:27:46 +00001764 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001765 int mask;
1766 const char *p, *p1;
1767
1768 p = str;
1769 mask = 0;
1770 for(;;) {
1771 p1 = strchr(p, ',');
1772 if (!p1)
1773 p1 = p + strlen(p);
bellard8e3a9fd2004-10-09 17:32:58 +00001774 if(cmp1(p,p1-p,"all")) {
1775 for(item = cpu_log_items; item->mask != 0; item++) {
1776 mask |= item->mask;
1777 }
1778 } else {
bellardf193c792004-03-21 17:06:25 +00001779 for(item = cpu_log_items; item->mask != 0; item++) {
1780 if (cmp1(p, p1 - p, item->name))
1781 goto found;
1782 }
1783 return 0;
bellard8e3a9fd2004-10-09 17:32:58 +00001784 }
bellardf193c792004-03-21 17:06:25 +00001785 found:
1786 mask |= item->mask;
1787 if (*p1 != ',')
1788 break;
1789 p = p1 + 1;
1790 }
1791 return mask;
1792}
bellardea041c02003-06-25 16:16:50 +00001793
bellard75012672003-06-21 13:11:07 +00001794void cpu_abort(CPUState *env, const char *fmt, ...)
1795{
1796 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001797 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001798
1799 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001800 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001801 fprintf(stderr, "qemu: fatal: ");
1802 vfprintf(stderr, fmt, ap);
1803 fprintf(stderr, "\n");
1804#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001805 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1806#else
1807 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001808#endif
aliguori93fcfe32009-01-15 22:34:14 +00001809 if (qemu_log_enabled()) {
1810 qemu_log("qemu: fatal: ");
1811 qemu_log_vprintf(fmt, ap2);
1812 qemu_log("\n");
j_mayerf9373292007-09-29 12:18:20 +00001813#ifdef TARGET_I386
aliguori93fcfe32009-01-15 22:34:14 +00001814 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
j_mayerf9373292007-09-29 12:18:20 +00001815#else
aliguori93fcfe32009-01-15 22:34:14 +00001816 log_cpu_state(env, 0);
j_mayerf9373292007-09-29 12:18:20 +00001817#endif
aliguori31b1a7b2009-01-15 22:35:09 +00001818 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +00001819 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +00001820 }
pbrook493ae1f2007-11-23 16:53:59 +00001821 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001822 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +02001823#if defined(CONFIG_USER_ONLY)
1824 {
1825 struct sigaction act;
1826 sigfillset(&act.sa_mask);
1827 act.sa_handler = SIG_DFL;
1828 sigaction(SIGABRT, &act, NULL);
1829 }
1830#endif
bellard75012672003-06-21 13:11:07 +00001831 abort();
1832}
1833
thsc5be9f02007-02-28 20:20:53 +00001834CPUState *cpu_copy(CPUState *env)
1835{
ths01ba9812007-12-09 02:22:57 +00001836 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001837 CPUState *next_cpu = new_env->next_cpu;
1838 int cpu_index = new_env->cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001839#if defined(TARGET_HAS_ICE)
1840 CPUBreakpoint *bp;
1841 CPUWatchpoint *wp;
1842#endif
1843
thsc5be9f02007-02-28 20:20:53 +00001844 memcpy(new_env, env, sizeof(CPUState));
aliguori5a38f082009-01-15 20:16:51 +00001845
1846 /* Preserve chaining and index. */
thsc5be9f02007-02-28 20:20:53 +00001847 new_env->next_cpu = next_cpu;
1848 new_env->cpu_index = cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001849
1850 /* Clone all break/watchpoints.
1851 Note: Once we support ptrace with hw-debug register access, make sure
1852 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001853 QTAILQ_INIT(&env->breakpoints);
1854 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +00001855#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001856 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001857 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1858 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001859 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001860 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1861 wp->flags, NULL);
1862 }
1863#endif
1864
thsc5be9f02007-02-28 20:20:53 +00001865 return new_env;
1866}
1867
bellard01243112004-01-04 15:48:17 +00001868#if !defined(CONFIG_USER_ONLY)
1869
edgar_igl5c751e92008-05-06 08:44:21 +00001870static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1871{
1872 unsigned int i;
1873
1874 /* Discard jump cache entries for any tb which might potentially
1875 overlap the flushed page. */
1876 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1877 memset (&env->tb_jmp_cache[i], 0,
1878 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1879
1880 i = tb_jmp_cache_hash_page(addr);
1881 memset (&env->tb_jmp_cache[i], 0,
1882 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1883}
1884
Igor Kovalenko08738982009-07-12 02:15:40 +04001885static CPUTLBEntry s_cputlb_empty_entry = {
1886 .addr_read = -1,
1887 .addr_write = -1,
1888 .addr_code = -1,
1889 .addend = -1,
1890};
1891
bellardee8b7022004-02-03 23:35:10 +00001892/* NOTE: if flush_global is true, also flush global entries (not
1893 implemented yet) */
1894void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001895{
bellard33417e72003-08-10 21:47:01 +00001896 int i;
bellard01243112004-01-04 15:48:17 +00001897
bellard9fa3e852004-01-04 18:06:42 +00001898#if defined(DEBUG_TLB)
1899 printf("tlb_flush:\n");
1900#endif
bellard01243112004-01-04 15:48:17 +00001901 /* must reset current TB so that interrupts cannot modify the
1902 links while we are modifying them */
1903 env->current_tb = NULL;
1904
bellard33417e72003-08-10 21:47:01 +00001905 for(i = 0; i < CPU_TLB_SIZE; i++) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001906 int mmu_idx;
1907 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001908 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001909 }
bellard33417e72003-08-10 21:47:01 +00001910 }
bellard9fa3e852004-01-04 18:06:42 +00001911
bellard8a40a182005-11-20 10:35:40 +00001912 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00001913
bellarde3db7222005-01-26 22:00:47 +00001914 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00001915}
1916
bellard274da6b2004-05-20 21:56:27 +00001917static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00001918{
ths5fafdf22007-09-16 21:08:06 +00001919 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00001920 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001921 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00001922 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00001923 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00001924 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Igor Kovalenko08738982009-07-12 02:15:40 +04001925 *tlb_entry = s_cputlb_empty_entry;
bellard84b7b8e2005-11-28 21:19:04 +00001926 }
bellard61382a52003-10-27 21:22:23 +00001927}
1928
bellard2e126692004-04-25 21:28:44 +00001929void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00001930{
bellard8a40a182005-11-20 10:35:40 +00001931 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001932 int mmu_idx;
bellard01243112004-01-04 15:48:17 +00001933
bellard9fa3e852004-01-04 18:06:42 +00001934#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00001935 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00001936#endif
bellard01243112004-01-04 15:48:17 +00001937 /* must reset current TB so that interrupts cannot modify the
1938 links while we are modifying them */
1939 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00001940
bellard61382a52003-10-27 21:22:23 +00001941 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00001942 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09001943 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1944 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
bellard01243112004-01-04 15:48:17 +00001945
edgar_igl5c751e92008-05-06 08:44:21 +00001946 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00001947}
1948
bellard9fa3e852004-01-04 18:06:42 +00001949/* update the TLBs so that writes to code in the virtual page 'addr'
1950 can be detected */
Anthony Liguoric227f092009-10-01 16:12:16 -05001951static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00001952{
ths5fafdf22007-09-16 21:08:06 +00001953 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00001954 ram_addr + TARGET_PAGE_SIZE,
1955 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00001956}
1957
bellard9fa3e852004-01-04 18:06:42 +00001958/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00001959 tested for self modifying code */
Anthony Liguoric227f092009-10-01 16:12:16 -05001960static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00001961 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00001962{
bellard3a7d9292005-08-21 09:26:42 +00001963 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
bellard1ccde1c2004-02-06 19:46:14 +00001964}
1965
ths5fafdf22007-09-16 21:08:06 +00001966static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00001967 unsigned long start, unsigned long length)
1968{
1969 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00001970 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1971 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00001972 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00001973 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00001974 }
1975 }
1976}
1977
pbrook5579c7f2009-04-11 14:47:08 +00001978/* Note: start and end must be within the same ram block. */
Anthony Liguoric227f092009-10-01 16:12:16 -05001979void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00001980 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00001981{
1982 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00001983 unsigned long length, start1;
bellard0a962c02005-02-10 22:00:27 +00001984 int i, mask, len;
1985 uint8_t *p;
bellard1ccde1c2004-02-06 19:46:14 +00001986
1987 start &= TARGET_PAGE_MASK;
1988 end = TARGET_PAGE_ALIGN(end);
1989
1990 length = end - start;
1991 if (length == 0)
1992 return;
bellard0a962c02005-02-10 22:00:27 +00001993 len = length >> TARGET_PAGE_BITS;
bellardf23db162005-08-21 19:12:28 +00001994 mask = ~dirty_flags;
1995 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1996 for(i = 0; i < len; i++)
1997 p[i] &= mask;
1998
bellard1ccde1c2004-02-06 19:46:14 +00001999 /* we modify the TLB cache so that the dirty bit will be set again
2000 when accessing the range */
pbrook5579c7f2009-04-11 14:47:08 +00002001 start1 = (unsigned long)qemu_get_ram_ptr(start);
2002 /* Chek that we don't span multiple blocks - this breaks the
2003 address comparisons below. */
2004 if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1
2005 != (end - 1) - start) {
2006 abort();
2007 }
2008
bellard6a00d602005-11-21 23:25:50 +00002009 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002010 int mmu_idx;
2011 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2012 for(i = 0; i < CPU_TLB_SIZE; i++)
2013 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
2014 start1, length);
2015 }
bellard6a00d602005-11-21 23:25:50 +00002016 }
bellard1ccde1c2004-02-06 19:46:14 +00002017}
2018
aliguori74576192008-10-06 14:02:03 +00002019int cpu_physical_memory_set_dirty_tracking(int enable)
2020{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002021 int ret = 0;
aliguori74576192008-10-06 14:02:03 +00002022 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002023 ret = cpu_notify_migration_log(!!enable);
2024 return ret;
aliguori74576192008-10-06 14:02:03 +00002025}
2026
2027int cpu_physical_memory_get_dirty_tracking(void)
2028{
2029 return in_migration;
2030}
2031
Anthony Liguoric227f092009-10-01 16:12:16 -05002032int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
2033 target_phys_addr_t end_addr)
aliguori2bec46d2008-11-24 20:21:41 +00002034{
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +02002035 int ret;
Jan Kiszka151f7742009-05-01 20:52:47 +02002036
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002037 ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
Jan Kiszka151f7742009-05-01 20:52:47 +02002038 return ret;
aliguori2bec46d2008-11-24 20:21:41 +00002039}
2040
bellard3a7d9292005-08-21 09:26:42 +00002041static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
2042{
Anthony Liguoric227f092009-10-01 16:12:16 -05002043 ram_addr_t ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00002044 void *p;
bellard3a7d9292005-08-21 09:26:42 +00002045
bellard84b7b8e2005-11-28 21:19:04 +00002046 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
pbrook5579c7f2009-04-11 14:47:08 +00002047 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
2048 + tlb_entry->addend);
2049 ram_addr = qemu_ram_addr_from_host(p);
bellard3a7d9292005-08-21 09:26:42 +00002050 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00002051 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00002052 }
2053 }
2054}
2055
2056/* update the TLB according to the current state of the dirty bits */
2057void cpu_tlb_update_dirty(CPUState *env)
2058{
2059 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002060 int mmu_idx;
2061 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2062 for(i = 0; i < CPU_TLB_SIZE; i++)
2063 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
2064 }
bellard3a7d9292005-08-21 09:26:42 +00002065}
2066
pbrook0f459d12008-06-09 00:20:13 +00002067static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002068{
pbrook0f459d12008-06-09 00:20:13 +00002069 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
2070 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00002071}
2072
pbrook0f459d12008-06-09 00:20:13 +00002073/* update the TLB corresponding to virtual page vaddr
2074 so that it is no longer dirty */
2075static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002076{
bellard1ccde1c2004-02-06 19:46:14 +00002077 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002078 int mmu_idx;
bellard1ccde1c2004-02-06 19:46:14 +00002079
pbrook0f459d12008-06-09 00:20:13 +00002080 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00002081 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002082 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2083 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
bellard9fa3e852004-01-04 18:06:42 +00002084}
2085
bellard59817cc2004-02-16 22:01:13 +00002086/* add a new TLB entry. At most one entry for a given virtual address
2087 is permitted. Return 0 if OK or 2 if the page could not be mapped
2088 (can only happen in non SOFTMMU mode for I/O pages or pages
2089 conflicting with the host address space). */
ths5fafdf22007-09-16 21:08:06 +00002090int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
Anthony Liguoric227f092009-10-01 16:12:16 -05002091 target_phys_addr_t paddr, int prot,
j_mayer6ebbf392007-10-14 07:07:08 +00002092 int mmu_idx, int is_softmmu)
bellard9fa3e852004-01-04 18:06:42 +00002093{
bellard92e873b2004-05-21 14:52:29 +00002094 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00002095 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00002096 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00002097 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00002098 target_ulong code_address;
Anthony Liguoric227f092009-10-01 16:12:16 -05002099 target_phys_addr_t addend;
bellard9fa3e852004-01-04 18:06:42 +00002100 int ret;
bellard84b7b8e2005-11-28 21:19:04 +00002101 CPUTLBEntry *te;
aliguoria1d1bb32008-11-18 20:07:32 +00002102 CPUWatchpoint *wp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002103 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00002104
bellard92e873b2004-05-21 14:52:29 +00002105 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00002106 if (!p) {
2107 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00002108 } else {
2109 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00002110 }
2111#if defined(DEBUG_TLB)
j_mayer6ebbf392007-10-14 07:07:08 +00002112 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
2113 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
bellard9fa3e852004-01-04 18:06:42 +00002114#endif
2115
2116 ret = 0;
pbrook0f459d12008-06-09 00:20:13 +00002117 address = vaddr;
2118 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2119 /* IO memory case (romd handled later) */
2120 address |= TLB_MMIO;
2121 }
pbrook5579c7f2009-04-11 14:47:08 +00002122 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
pbrook0f459d12008-06-09 00:20:13 +00002123 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2124 /* Normal RAM. */
2125 iotlb = pd & TARGET_PAGE_MASK;
2126 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2127 iotlb |= IO_MEM_NOTDIRTY;
2128 else
2129 iotlb |= IO_MEM_ROM;
2130 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002131 /* IO handlers are currently passed a physical address.
pbrook0f459d12008-06-09 00:20:13 +00002132 It would be nice to pass an offset from the base address
2133 of that region. This would avoid having to special case RAM,
2134 and avoid full address decoding in every device.
2135 We can't use the high bits of pd for this because
2136 IO_MEM_ROMD uses these as a ram address. */
pbrook8da3ff12008-12-01 18:59:50 +00002137 iotlb = (pd & ~TARGET_PAGE_MASK);
2138 if (p) {
pbrook8da3ff12008-12-01 18:59:50 +00002139 iotlb += p->region_offset;
2140 } else {
2141 iotlb += paddr;
2142 }
pbrook0f459d12008-06-09 00:20:13 +00002143 }
pbrook6658ffb2007-03-16 23:58:11 +00002144
pbrook0f459d12008-06-09 00:20:13 +00002145 code_address = address;
2146 /* Make accesses to pages with watchpoints go via the
2147 watchpoint trap routines. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002148 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00002149 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
pbrook0f459d12008-06-09 00:20:13 +00002150 iotlb = io_mem_watch + paddr;
2151 /* TODO: The memory case can be optimized by not trapping
2152 reads of pages with a write breakpoint. */
2153 address |= TLB_MMIO;
pbrook6658ffb2007-03-16 23:58:11 +00002154 }
pbrook0f459d12008-06-09 00:20:13 +00002155 }
balrogd79acba2007-06-26 20:01:13 +00002156
pbrook0f459d12008-06-09 00:20:13 +00002157 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2158 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2159 te = &env->tlb_table[mmu_idx][index];
2160 te->addend = addend - vaddr;
2161 if (prot & PAGE_READ) {
2162 te->addr_read = address;
2163 } else {
2164 te->addr_read = -1;
2165 }
edgar_igl5c751e92008-05-06 08:44:21 +00002166
pbrook0f459d12008-06-09 00:20:13 +00002167 if (prot & PAGE_EXEC) {
2168 te->addr_code = code_address;
2169 } else {
2170 te->addr_code = -1;
2171 }
2172 if (prot & PAGE_WRITE) {
2173 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2174 (pd & IO_MEM_ROMD)) {
2175 /* Write access calls the I/O callback. */
2176 te->addr_write = address | TLB_MMIO;
2177 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2178 !cpu_physical_memory_is_dirty(pd)) {
2179 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00002180 } else {
pbrook0f459d12008-06-09 00:20:13 +00002181 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00002182 }
pbrook0f459d12008-06-09 00:20:13 +00002183 } else {
2184 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00002185 }
bellard9fa3e852004-01-04 18:06:42 +00002186 return ret;
2187}
2188
bellard01243112004-01-04 15:48:17 +00002189#else
2190
bellardee8b7022004-02-03 23:35:10 +00002191void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00002192{
2193}
2194
bellard2e126692004-04-25 21:28:44 +00002195void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00002196{
2197}
2198
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002199/*
2200 * Walks guest process memory "regions" one by one
2201 * and calls callback function 'fn' for each region.
2202 */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002203
2204struct walk_memory_regions_data
bellard9fa3e852004-01-04 18:06:42 +00002205{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002206 walk_memory_regions_fn fn;
2207 void *priv;
2208 unsigned long start;
2209 int prot;
2210};
bellard9fa3e852004-01-04 18:06:42 +00002211
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002212static int walk_memory_regions_end(struct walk_memory_regions_data *data,
2213 unsigned long end, int new_prot)
2214{
2215 if (data->start != -1ul) {
2216 int rc = data->fn(data->priv, data->start, end, data->prot);
2217 if (rc != 0) {
2218 return rc;
bellard9fa3e852004-01-04 18:06:42 +00002219 }
bellard33417e72003-08-10 21:47:01 +00002220 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002221
2222 data->start = (new_prot ? end : -1ul);
2223 data->prot = new_prot;
2224
2225 return 0;
2226}
2227
2228static int walk_memory_regions_1(struct walk_memory_regions_data *data,
2229 unsigned long base, int level, void **lp)
2230{
2231 unsigned long pa;
2232 int i, rc;
2233
2234 if (*lp == NULL) {
2235 return walk_memory_regions_end(data, base, 0);
2236 }
2237
2238 if (level == 0) {
2239 PageDesc *pd = *lp;
2240 for (i = 0; i < L2_BITS; ++i) {
2241 int prot = pd[i].flags;
2242
2243 pa = base | (i << TARGET_PAGE_BITS);
2244 if (prot != data->prot) {
2245 rc = walk_memory_regions_end(data, pa, prot);
2246 if (rc != 0) {
2247 return rc;
2248 }
2249 }
2250 }
2251 } else {
2252 void **pp = *lp;
2253 for (i = 0; i < L2_BITS; ++i) {
2254 pa = base | (i << (TARGET_PAGE_BITS + L2_BITS * level));
2255 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2256 if (rc != 0) {
2257 return rc;
2258 }
2259 }
2260 }
2261
2262 return 0;
2263}
2264
2265int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2266{
2267 struct walk_memory_regions_data data;
2268 unsigned long i;
2269
2270 data.fn = fn;
2271 data.priv = priv;
2272 data.start = -1ul;
2273 data.prot = 0;
2274
2275 for (i = 0; i < V_L1_SIZE; i++) {
2276 int rc = walk_memory_regions_1(&data, i << V_L1_SHIFT,
2277 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
2278 if (rc != 0) {
2279 return rc;
2280 }
2281 }
2282
2283 return walk_memory_regions_end(&data, 0, 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002284}
2285
2286static int dump_region(void *priv, unsigned long start,
2287 unsigned long end, unsigned long prot)
2288{
2289 FILE *f = (FILE *)priv;
2290
2291 (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
2292 start, end, end - start,
2293 ((prot & PAGE_READ) ? 'r' : '-'),
2294 ((prot & PAGE_WRITE) ? 'w' : '-'),
2295 ((prot & PAGE_EXEC) ? 'x' : '-'));
2296
2297 return (0);
2298}
2299
2300/* dump memory mappings */
2301void page_dump(FILE *f)
2302{
2303 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2304 "start", "end", "size", "prot");
2305 walk_memory_regions(f, dump_region);
bellard33417e72003-08-10 21:47:01 +00002306}
2307
pbrook53a59602006-03-25 19:31:22 +00002308int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002309{
bellard9fa3e852004-01-04 18:06:42 +00002310 PageDesc *p;
2311
2312 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002313 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002314 return 0;
2315 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002316}
2317
bellard9fa3e852004-01-04 18:06:42 +00002318/* modify the flags of a page and invalidate the code if
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002319 necessary. The flag PAGE_WRITE_ORG is positioned automatically
bellard9fa3e852004-01-04 18:06:42 +00002320 depending on PAGE_WRITE */
pbrook53a59602006-03-25 19:31:22 +00002321void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002322{
2323 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002324 target_ulong addr;
bellard9fa3e852004-01-04 18:06:42 +00002325
pbrookc8a706f2008-06-02 16:16:42 +00002326 /* mmap_lock should already be held. */
bellard9fa3e852004-01-04 18:06:42 +00002327 start = start & TARGET_PAGE_MASK;
2328 end = TARGET_PAGE_ALIGN(end);
2329 if (flags & PAGE_WRITE)
2330 flags |= PAGE_WRITE_ORG;
bellard9fa3e852004-01-04 18:06:42 +00002331 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2332 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
pbrook17e23772008-06-09 13:47:45 +00002333 /* We may be called for host regions that are outside guest
2334 address space. */
2335 if (!p)
2336 return;
bellard9fa3e852004-01-04 18:06:42 +00002337 /* if the write protection is set, then we invalidate the code
2338 inside */
ths5fafdf22007-09-16 21:08:06 +00002339 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002340 (flags & PAGE_WRITE) &&
2341 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002342 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002343 }
2344 p->flags = flags;
2345 }
bellard9fa3e852004-01-04 18:06:42 +00002346}
2347
ths3d97b402007-11-02 19:02:07 +00002348int page_check_range(target_ulong start, target_ulong len, int flags)
2349{
2350 PageDesc *p;
2351 target_ulong end;
2352 target_ulong addr;
2353
balrog55f280c2008-10-28 10:24:11 +00002354 if (start + len < start)
2355 /* we've wrapped around */
2356 return -1;
2357
ths3d97b402007-11-02 19:02:07 +00002358 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2359 start = start & TARGET_PAGE_MASK;
2360
ths3d97b402007-11-02 19:02:07 +00002361 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2362 p = page_find(addr >> TARGET_PAGE_BITS);
2363 if( !p )
2364 return -1;
2365 if( !(p->flags & PAGE_VALID) )
2366 return -1;
2367
bellarddae32702007-11-14 10:51:00 +00002368 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002369 return -1;
bellarddae32702007-11-14 10:51:00 +00002370 if (flags & PAGE_WRITE) {
2371 if (!(p->flags & PAGE_WRITE_ORG))
2372 return -1;
2373 /* unprotect the page if it was put read-only because it
2374 contains translated code */
2375 if (!(p->flags & PAGE_WRITE)) {
2376 if (!page_unprotect(addr, 0, NULL))
2377 return -1;
2378 }
2379 return 0;
2380 }
ths3d97b402007-11-02 19:02:07 +00002381 }
2382 return 0;
2383}
2384
bellard9fa3e852004-01-04 18:06:42 +00002385/* called from signal handler: invalidate the code and unprotect the
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002386 page. Return TRUE if the fault was successfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002387int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002388{
2389 unsigned int page_index, prot, pindex;
2390 PageDesc *p, *p1;
pbrook53a59602006-03-25 19:31:22 +00002391 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002392
pbrookc8a706f2008-06-02 16:16:42 +00002393 /* Technically this isn't safe inside a signal handler. However we
2394 know this only ever happens in a synchronous SEGV handler, so in
2395 practice it seems to be ok. */
2396 mmap_lock();
2397
bellard83fb7ad2004-07-05 21:25:26 +00002398 host_start = address & qemu_host_page_mask;
bellard9fa3e852004-01-04 18:06:42 +00002399 page_index = host_start >> TARGET_PAGE_BITS;
2400 p1 = page_find(page_index);
pbrookc8a706f2008-06-02 16:16:42 +00002401 if (!p1) {
2402 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002403 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002404 }
bellard83fb7ad2004-07-05 21:25:26 +00002405 host_end = host_start + qemu_host_page_size;
bellard9fa3e852004-01-04 18:06:42 +00002406 p = p1;
2407 prot = 0;
2408 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2409 prot |= p->flags;
2410 p++;
2411 }
2412 /* if the page was really writable, then we change its
2413 protection back to writable */
2414 if (prot & PAGE_WRITE_ORG) {
2415 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2416 if (!(p1[pindex].flags & PAGE_WRITE)) {
ths5fafdf22007-09-16 21:08:06 +00002417 mprotect((void *)g2h(host_start), qemu_host_page_size,
bellard9fa3e852004-01-04 18:06:42 +00002418 (prot & PAGE_BITS) | PAGE_WRITE);
2419 p1[pindex].flags |= PAGE_WRITE;
2420 /* and since the content will be modified, we must invalidate
2421 the corresponding translated code. */
bellardd720b932004-04-25 17:57:43 +00002422 tb_invalidate_phys_page(address, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002423#ifdef DEBUG_TB_CHECK
2424 tb_invalidate_check(address);
2425#endif
pbrookc8a706f2008-06-02 16:16:42 +00002426 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002427 return 1;
2428 }
2429 }
pbrookc8a706f2008-06-02 16:16:42 +00002430 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002431 return 0;
2432}
2433
bellard6a00d602005-11-21 23:25:50 +00002434static inline void tlb_set_dirty(CPUState *env,
2435 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002436{
2437}
bellard9fa3e852004-01-04 18:06:42 +00002438#endif /* defined(CONFIG_USER_ONLY) */
2439
pbrooke2eef172008-06-08 01:09:01 +00002440#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +00002441
Paul Brookc04b2b72010-03-01 03:31:14 +00002442#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2443typedef struct subpage_t {
2444 target_phys_addr_t base;
2445 CPUReadMemoryFunc * const *mem_read[TARGET_PAGE_SIZE][4];
2446 CPUWriteMemoryFunc * const *mem_write[TARGET_PAGE_SIZE][4];
2447 void *opaque[TARGET_PAGE_SIZE][2][4];
2448 ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4];
2449} subpage_t;
2450
Anthony Liguoric227f092009-10-01 16:12:16 -05002451static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2452 ram_addr_t memory, ram_addr_t region_offset);
2453static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2454 ram_addr_t orig_memory, ram_addr_t region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002455#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2456 need_subpage) \
2457 do { \
2458 if (addr > start_addr) \
2459 start_addr2 = 0; \
2460 else { \
2461 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2462 if (start_addr2 > 0) \
2463 need_subpage = 1; \
2464 } \
2465 \
blueswir149e9fba2007-05-30 17:25:06 +00002466 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002467 end_addr2 = TARGET_PAGE_SIZE - 1; \
2468 else { \
2469 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2470 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2471 need_subpage = 1; \
2472 } \
2473 } while (0)
2474
Michael S. Tsirkin8f2498f2009-09-29 18:53:16 +02002475/* register physical memory.
2476 For RAM, 'size' must be a multiple of the target page size.
2477 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
pbrook8da3ff12008-12-01 18:59:50 +00002478 io memory page. The address used when calling the IO function is
2479 the offset from the start of the region, plus region_offset. Both
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002480 start_addr and region_offset are rounded down to a page boundary
pbrook8da3ff12008-12-01 18:59:50 +00002481 before calculating this offset. This should not be a problem unless
2482 the low bits of start_addr and region_offset differ. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002483void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2484 ram_addr_t size,
2485 ram_addr_t phys_offset,
2486 ram_addr_t region_offset)
bellard33417e72003-08-10 21:47:01 +00002487{
Anthony Liguoric227f092009-10-01 16:12:16 -05002488 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002489 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002490 CPUState *env;
Anthony Liguoric227f092009-10-01 16:12:16 -05002491 ram_addr_t orig_size = size;
blueswir1db7b5422007-05-26 17:36:03 +00002492 void *subpage;
bellard33417e72003-08-10 21:47:01 +00002493
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002494 cpu_notify_set_memory(start_addr, size, phys_offset);
2495
pbrook67c4d232009-02-23 13:16:07 +00002496 if (phys_offset == IO_MEM_UNASSIGNED) {
2497 region_offset = start_addr;
2498 }
pbrook8da3ff12008-12-01 18:59:50 +00002499 region_offset &= TARGET_PAGE_MASK;
bellard5fd386f2004-05-23 21:11:22 +00002500 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
Anthony Liguoric227f092009-10-01 16:12:16 -05002501 end_addr = start_addr + (target_phys_addr_t)size;
blueswir149e9fba2007-05-30 17:25:06 +00002502 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
blueswir1db7b5422007-05-26 17:36:03 +00002503 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2504 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002505 ram_addr_t orig_memory = p->phys_offset;
2506 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002507 int need_subpage = 0;
2508
2509 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2510 need_subpage);
blueswir14254fab2008-01-01 16:57:19 +00002511 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002512 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2513 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002514 &p->phys_offset, orig_memory,
2515 p->region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002516 } else {
2517 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2518 >> IO_MEM_SHIFT];
2519 }
pbrook8da3ff12008-12-01 18:59:50 +00002520 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2521 region_offset);
2522 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002523 } else {
2524 p->phys_offset = phys_offset;
2525 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2526 (phys_offset & IO_MEM_ROMD))
2527 phys_offset += TARGET_PAGE_SIZE;
2528 }
2529 } else {
2530 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2531 p->phys_offset = phys_offset;
pbrook8da3ff12008-12-01 18:59:50 +00002532 p->region_offset = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002533 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
pbrook8da3ff12008-12-01 18:59:50 +00002534 (phys_offset & IO_MEM_ROMD)) {
blueswir1db7b5422007-05-26 17:36:03 +00002535 phys_offset += TARGET_PAGE_SIZE;
pbrook0e8f0962008-12-02 09:02:15 +00002536 } else {
Anthony Liguoric227f092009-10-01 16:12:16 -05002537 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002538 int need_subpage = 0;
2539
2540 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2541 end_addr2, need_subpage);
2542
blueswir14254fab2008-01-01 16:57:19 +00002543 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
blueswir1db7b5422007-05-26 17:36:03 +00002544 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002545 &p->phys_offset, IO_MEM_UNASSIGNED,
pbrook67c4d232009-02-23 13:16:07 +00002546 addr & TARGET_PAGE_MASK);
blueswir1db7b5422007-05-26 17:36:03 +00002547 subpage_register(subpage, start_addr2, end_addr2,
pbrook8da3ff12008-12-01 18:59:50 +00002548 phys_offset, region_offset);
2549 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002550 }
2551 }
2552 }
pbrook8da3ff12008-12-01 18:59:50 +00002553 region_offset += TARGET_PAGE_SIZE;
bellard33417e72003-08-10 21:47:01 +00002554 }
ths3b46e622007-09-17 08:09:54 +00002555
bellard9d420372006-06-25 22:25:22 +00002556 /* since each CPU stores ram addresses in its TLB cache, we must
2557 reset the modified entries */
2558 /* XXX: slow ! */
2559 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2560 tlb_flush(env, 1);
2561 }
bellard33417e72003-08-10 21:47:01 +00002562}
2563
bellardba863452006-09-24 18:41:10 +00002564/* XXX: temporary until new memory mapping API */
Anthony Liguoric227f092009-10-01 16:12:16 -05002565ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002566{
2567 PhysPageDesc *p;
2568
2569 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2570 if (!p)
2571 return IO_MEM_UNASSIGNED;
2572 return p->phys_offset;
2573}
2574
Anthony Liguoric227f092009-10-01 16:12:16 -05002575void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002576{
2577 if (kvm_enabled())
2578 kvm_coalesce_mmio_region(addr, size);
2579}
2580
Anthony Liguoric227f092009-10-01 16:12:16 -05002581void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002582{
2583 if (kvm_enabled())
2584 kvm_uncoalesce_mmio_region(addr, size);
2585}
2586
Sheng Yang62a27442010-01-26 19:21:16 +08002587void qemu_flush_coalesced_mmio_buffer(void)
2588{
2589 if (kvm_enabled())
2590 kvm_flush_coalesced_mmio_buffer();
2591}
2592
Marcelo Tosattic9027602010-03-01 20:25:08 -03002593#if defined(__linux__) && !defined(TARGET_S390X)
2594
2595#include <sys/vfs.h>
2596
2597#define HUGETLBFS_MAGIC 0x958458f6
2598
2599static long gethugepagesize(const char *path)
2600{
2601 struct statfs fs;
2602 int ret;
2603
2604 do {
2605 ret = statfs(path, &fs);
2606 } while (ret != 0 && errno == EINTR);
2607
2608 if (ret != 0) {
2609 perror("statfs");
2610 return 0;
2611 }
2612
2613 if (fs.f_type != HUGETLBFS_MAGIC)
2614 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
2615
2616 return fs.f_bsize;
2617}
2618
2619static void *file_ram_alloc(ram_addr_t memory, const char *path)
2620{
2621 char *filename;
2622 void *area;
2623 int fd;
2624#ifdef MAP_POPULATE
2625 int flags;
2626#endif
2627 unsigned long hpagesize;
2628
2629 hpagesize = gethugepagesize(path);
2630 if (!hpagesize) {
2631 return NULL;
2632 }
2633
2634 if (memory < hpagesize) {
2635 return NULL;
2636 }
2637
2638 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2639 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2640 return NULL;
2641 }
2642
2643 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
2644 return NULL;
2645 }
2646
2647 fd = mkstemp(filename);
2648 if (fd < 0) {
2649 perror("mkstemp");
2650 free(filename);
2651 return NULL;
2652 }
2653 unlink(filename);
2654 free(filename);
2655
2656 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2657
2658 /*
2659 * ftruncate is not supported by hugetlbfs in older
2660 * hosts, so don't bother bailing out on errors.
2661 * If anything goes wrong with it under other filesystems,
2662 * mmap will fail.
2663 */
2664 if (ftruncate(fd, memory))
2665 perror("ftruncate");
2666
2667#ifdef MAP_POPULATE
2668 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2669 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2670 * to sidestep this quirk.
2671 */
2672 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2673 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2674#else
2675 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2676#endif
2677 if (area == MAP_FAILED) {
2678 perror("file_ram_alloc: can't mmap RAM pages");
2679 close(fd);
2680 return (NULL);
2681 }
2682 return area;
2683}
2684#endif
2685
Anthony Liguoric227f092009-10-01 16:12:16 -05002686ram_addr_t qemu_ram_alloc(ram_addr_t size)
pbrook94a6b542009-04-11 17:15:54 +00002687{
2688 RAMBlock *new_block;
2689
pbrook94a6b542009-04-11 17:15:54 +00002690 size = TARGET_PAGE_ALIGN(size);
2691 new_block = qemu_malloc(sizeof(*new_block));
2692
Marcelo Tosattic9027602010-03-01 20:25:08 -03002693 if (mem_path) {
2694#if defined (__linux__) && !defined(TARGET_S390X)
2695 new_block->host = file_ram_alloc(size, mem_path);
2696 if (!new_block->host)
2697 exit(1);
Alexander Graf6b024942009-12-05 12:44:25 +01002698#else
Marcelo Tosattic9027602010-03-01 20:25:08 -03002699 fprintf(stderr, "-mem-path option unsupported\n");
2700 exit(1);
2701#endif
2702 } else {
2703#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2704 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2705 new_block->host = mmap((void*)0x1000000, size,
2706 PROT_EXEC|PROT_READ|PROT_WRITE,
2707 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
2708#else
2709 new_block->host = qemu_vmalloc(size);
Alexander Graf6b024942009-12-05 12:44:25 +01002710#endif
Izik Eidusccb167e2009-10-08 16:39:39 +02002711#ifdef MADV_MERGEABLE
Marcelo Tosattic9027602010-03-01 20:25:08 -03002712 madvise(new_block->host, size, MADV_MERGEABLE);
Izik Eidusccb167e2009-10-08 16:39:39 +02002713#endif
Marcelo Tosattic9027602010-03-01 20:25:08 -03002714 }
pbrook94a6b542009-04-11 17:15:54 +00002715 new_block->offset = last_ram_offset;
2716 new_block->length = size;
2717
2718 new_block->next = ram_blocks;
2719 ram_blocks = new_block;
2720
2721 phys_ram_dirty = qemu_realloc(phys_ram_dirty,
2722 (last_ram_offset + size) >> TARGET_PAGE_BITS);
2723 memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
2724 0xff, size >> TARGET_PAGE_BITS);
2725
2726 last_ram_offset += size;
2727
Jan Kiszka6f0437e2009-04-26 18:03:40 +02002728 if (kvm_enabled())
2729 kvm_setup_guest_memory(new_block->host, size);
2730
pbrook94a6b542009-04-11 17:15:54 +00002731 return new_block->offset;
2732}
bellarde9a1ab12007-02-08 23:08:38 +00002733
Anthony Liguoric227f092009-10-01 16:12:16 -05002734void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00002735{
pbrook94a6b542009-04-11 17:15:54 +00002736 /* TODO: implement this. */
bellarde9a1ab12007-02-08 23:08:38 +00002737}
2738
pbrookdc828ca2009-04-09 22:21:07 +00002739/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00002740 With the exception of the softmmu code in this file, this should
2741 only be used for local memory (e.g. video ram) that the device owns,
2742 and knows it isn't going to access beyond the end of the block.
2743
2744 It should not be used for general purpose DMA.
2745 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2746 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002747void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00002748{
pbrook94a6b542009-04-11 17:15:54 +00002749 RAMBlock *prev;
2750 RAMBlock **prevp;
2751 RAMBlock *block;
2752
pbrook94a6b542009-04-11 17:15:54 +00002753 prev = NULL;
2754 prevp = &ram_blocks;
2755 block = ram_blocks;
2756 while (block && (block->offset > addr
2757 || block->offset + block->length <= addr)) {
2758 if (prev)
2759 prevp = &prev->next;
2760 prev = block;
2761 block = block->next;
2762 }
2763 if (!block) {
2764 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2765 abort();
2766 }
2767 /* Move this entry to to start of the list. */
2768 if (prev) {
2769 prev->next = block->next;
2770 block->next = *prevp;
2771 *prevp = block;
2772 }
2773 return block->host + (addr - block->offset);
pbrookdc828ca2009-04-09 22:21:07 +00002774}
2775
pbrook5579c7f2009-04-11 14:47:08 +00002776/* Some of the softmmu routines need to translate from a host pointer
2777 (typically a TLB entry) back to a ram offset. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002778ram_addr_t qemu_ram_addr_from_host(void *ptr)
pbrook5579c7f2009-04-11 14:47:08 +00002779{
pbrook94a6b542009-04-11 17:15:54 +00002780 RAMBlock *prev;
pbrook94a6b542009-04-11 17:15:54 +00002781 RAMBlock *block;
2782 uint8_t *host = ptr;
2783
pbrook94a6b542009-04-11 17:15:54 +00002784 prev = NULL;
pbrook94a6b542009-04-11 17:15:54 +00002785 block = ram_blocks;
2786 while (block && (block->host > host
2787 || block->host + block->length <= host)) {
pbrook94a6b542009-04-11 17:15:54 +00002788 prev = block;
2789 block = block->next;
2790 }
2791 if (!block) {
2792 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2793 abort();
2794 }
2795 return block->offset + (host - block->host);
pbrook5579c7f2009-04-11 14:47:08 +00002796}
2797
Anthony Liguoric227f092009-10-01 16:12:16 -05002798static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00002799{
pbrook67d3b952006-12-18 05:03:52 +00002800#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002801 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00002802#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002803#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002804 do_unassigned_access(addr, 0, 0, 0, 1);
2805#endif
2806 return 0;
2807}
2808
Anthony Liguoric227f092009-10-01 16:12:16 -05002809static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002810{
2811#ifdef DEBUG_UNASSIGNED
2812 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2813#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002814#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002815 do_unassigned_access(addr, 0, 0, 0, 2);
2816#endif
2817 return 0;
2818}
2819
Anthony Liguoric227f092009-10-01 16:12:16 -05002820static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00002821{
2822#ifdef DEBUG_UNASSIGNED
2823 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2824#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002825#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002826 do_unassigned_access(addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002827#endif
bellard33417e72003-08-10 21:47:01 +00002828 return 0;
2829}
2830
Anthony Liguoric227f092009-10-01 16:12:16 -05002831static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00002832{
pbrook67d3b952006-12-18 05:03:52 +00002833#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00002834 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00002835#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002836#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002837 do_unassigned_access(addr, 1, 0, 0, 1);
2838#endif
2839}
2840
Anthony Liguoric227f092009-10-01 16:12:16 -05002841static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00002842{
2843#ifdef DEBUG_UNASSIGNED
2844 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2845#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002846#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002847 do_unassigned_access(addr, 1, 0, 0, 2);
2848#endif
2849}
2850
Anthony Liguoric227f092009-10-01 16:12:16 -05002851static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00002852{
2853#ifdef DEBUG_UNASSIGNED
2854 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2855#endif
Edgar E. Iglesiasfaed1c22009-09-03 13:25:09 +02002856#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
blueswir1e18231a2008-10-06 18:46:28 +00002857 do_unassigned_access(addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00002858#endif
bellard33417e72003-08-10 21:47:01 +00002859}
2860
Blue Swirld60efc62009-08-25 18:29:31 +00002861static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
bellard33417e72003-08-10 21:47:01 +00002862 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00002863 unassigned_mem_readw,
2864 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00002865};
2866
Blue Swirld60efc62009-08-25 18:29:31 +00002867static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
bellard33417e72003-08-10 21:47:01 +00002868 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00002869 unassigned_mem_writew,
2870 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00002871};
2872
Anthony Liguoric227f092009-10-01 16:12:16 -05002873static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002874 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002875{
bellard3a7d9292005-08-21 09:26:42 +00002876 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002877 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2878 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2879#if !defined(CONFIG_USER_ONLY)
2880 tb_invalidate_phys_page_fast(ram_addr, 1);
2881 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2882#endif
2883 }
pbrook5579c7f2009-04-11 14:47:08 +00002884 stb_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002885 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2886 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2887 /* we remove the notdirty callback only if the code has been
2888 flushed */
2889 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002890 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002891}
2892
Anthony Liguoric227f092009-10-01 16:12:16 -05002893static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002894 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002895{
bellard3a7d9292005-08-21 09:26:42 +00002896 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002897 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2898 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2899#if !defined(CONFIG_USER_ONLY)
2900 tb_invalidate_phys_page_fast(ram_addr, 2);
2901 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2902#endif
2903 }
pbrook5579c7f2009-04-11 14:47:08 +00002904 stw_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002905 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2906 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2907 /* we remove the notdirty callback only if the code has been
2908 flushed */
2909 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002910 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002911}
2912
Anthony Liguoric227f092009-10-01 16:12:16 -05002913static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00002914 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00002915{
bellard3a7d9292005-08-21 09:26:42 +00002916 int dirty_flags;
bellard3a7d9292005-08-21 09:26:42 +00002917 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2918 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2919#if !defined(CONFIG_USER_ONLY)
2920 tb_invalidate_phys_page_fast(ram_addr, 4);
2921 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2922#endif
2923 }
pbrook5579c7f2009-04-11 14:47:08 +00002924 stl_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00002925 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2926 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2927 /* we remove the notdirty callback only if the code has been
2928 flushed */
2929 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00002930 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00002931}
2932
Blue Swirld60efc62009-08-25 18:29:31 +00002933static CPUReadMemoryFunc * const error_mem_read[3] = {
bellard3a7d9292005-08-21 09:26:42 +00002934 NULL, /* never used */
2935 NULL, /* never used */
2936 NULL, /* never used */
2937};
2938
Blue Swirld60efc62009-08-25 18:29:31 +00002939static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
bellard1ccde1c2004-02-06 19:46:14 +00002940 notdirty_mem_writeb,
2941 notdirty_mem_writew,
2942 notdirty_mem_writel,
2943};
2944
pbrook0f459d12008-06-09 00:20:13 +00002945/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00002946static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00002947{
2948 CPUState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00002949 target_ulong pc, cs_base;
2950 TranslationBlock *tb;
pbrook0f459d12008-06-09 00:20:13 +00002951 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00002952 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00002953 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00002954
aliguori06d55cc2008-11-18 20:24:06 +00002955 if (env->watchpoint_hit) {
2956 /* We re-entered the check after replacing the TB. Now raise
2957 * the debug interrupt so that is will trigger after the
2958 * current instruction. */
2959 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2960 return;
2961 }
pbrook2e70f6e2008-06-29 01:03:05 +00002962 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002963 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00002964 if ((vaddr == (wp->vaddr & len_mask) ||
2965 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00002966 wp->flags |= BP_WATCHPOINT_HIT;
2967 if (!env->watchpoint_hit) {
2968 env->watchpoint_hit = wp;
2969 tb = tb_find_pc(env->mem_io_pc);
2970 if (!tb) {
2971 cpu_abort(env, "check_watchpoint: could not find TB for "
2972 "pc=%p", (void *)env->mem_io_pc);
2973 }
2974 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
2975 tb_phys_invalidate(tb, -1);
2976 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2977 env->exception_index = EXCP_DEBUG;
2978 } else {
2979 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2980 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
2981 }
2982 cpu_resume_from_signal(env, NULL);
aliguori06d55cc2008-11-18 20:24:06 +00002983 }
aliguori6e140f22008-11-18 20:37:55 +00002984 } else {
2985 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00002986 }
2987 }
2988}
2989
pbrook6658ffb2007-03-16 23:58:11 +00002990/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2991 so these check for a hit then pass through to the normal out-of-line
2992 phys routines. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002993static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00002994{
aliguorib4051332008-11-18 20:14:20 +00002995 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00002996 return ldub_phys(addr);
2997}
2998
Anthony Liguoric227f092009-10-01 16:12:16 -05002999static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003000{
aliguorib4051332008-11-18 20:14:20 +00003001 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003002 return lduw_phys(addr);
3003}
3004
Anthony Liguoric227f092009-10-01 16:12:16 -05003005static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003006{
aliguorib4051332008-11-18 20:14:20 +00003007 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003008 return ldl_phys(addr);
3009}
3010
Anthony Liguoric227f092009-10-01 16:12:16 -05003011static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003012 uint32_t val)
3013{
aliguorib4051332008-11-18 20:14:20 +00003014 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003015 stb_phys(addr, val);
3016}
3017
Anthony Liguoric227f092009-10-01 16:12:16 -05003018static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003019 uint32_t val)
3020{
aliguorib4051332008-11-18 20:14:20 +00003021 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003022 stw_phys(addr, val);
3023}
3024
Anthony Liguoric227f092009-10-01 16:12:16 -05003025static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003026 uint32_t val)
3027{
aliguorib4051332008-11-18 20:14:20 +00003028 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003029 stl_phys(addr, val);
3030}
3031
Blue Swirld60efc62009-08-25 18:29:31 +00003032static CPUReadMemoryFunc * const watch_mem_read[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003033 watch_mem_readb,
3034 watch_mem_readw,
3035 watch_mem_readl,
3036};
3037
Blue Swirld60efc62009-08-25 18:29:31 +00003038static CPUWriteMemoryFunc * const watch_mem_write[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003039 watch_mem_writeb,
3040 watch_mem_writew,
3041 watch_mem_writel,
3042};
pbrook6658ffb2007-03-16 23:58:11 +00003043
Anthony Liguoric227f092009-10-01 16:12:16 -05003044static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003045 unsigned int len)
3046{
blueswir1db7b5422007-05-26 17:36:03 +00003047 uint32_t ret;
3048 unsigned int idx;
3049
pbrook8da3ff12008-12-01 18:59:50 +00003050 idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003051#if defined(DEBUG_SUBPAGE)
3052 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3053 mmio, len, addr, idx);
3054#endif
pbrook8da3ff12008-12-01 18:59:50 +00003055 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
3056 addr + mmio->region_offset[idx][0][len]);
blueswir1db7b5422007-05-26 17:36:03 +00003057
3058 return ret;
3059}
3060
Anthony Liguoric227f092009-10-01 16:12:16 -05003061static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003062 uint32_t value, unsigned int len)
3063{
blueswir1db7b5422007-05-26 17:36:03 +00003064 unsigned int idx;
3065
pbrook8da3ff12008-12-01 18:59:50 +00003066 idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003067#if defined(DEBUG_SUBPAGE)
3068 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
3069 mmio, len, addr, idx, value);
3070#endif
pbrook8da3ff12008-12-01 18:59:50 +00003071 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
3072 addr + mmio->region_offset[idx][1][len],
3073 value);
blueswir1db7b5422007-05-26 17:36:03 +00003074}
3075
Anthony Liguoric227f092009-10-01 16:12:16 -05003076static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003077{
3078#if defined(DEBUG_SUBPAGE)
3079 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3080#endif
3081
3082 return subpage_readlen(opaque, addr, 0);
3083}
3084
Anthony Liguoric227f092009-10-01 16:12:16 -05003085static void subpage_writeb (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003086 uint32_t value)
3087{
3088#if defined(DEBUG_SUBPAGE)
3089 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3090#endif
3091 subpage_writelen(opaque, addr, value, 0);
3092}
3093
Anthony Liguoric227f092009-10-01 16:12:16 -05003094static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003095{
3096#if defined(DEBUG_SUBPAGE)
3097 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3098#endif
3099
3100 return subpage_readlen(opaque, addr, 1);
3101}
3102
Anthony Liguoric227f092009-10-01 16:12:16 -05003103static void subpage_writew (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003104 uint32_t value)
3105{
3106#if defined(DEBUG_SUBPAGE)
3107 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3108#endif
3109 subpage_writelen(opaque, addr, value, 1);
3110}
3111
Anthony Liguoric227f092009-10-01 16:12:16 -05003112static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003113{
3114#if defined(DEBUG_SUBPAGE)
3115 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3116#endif
3117
3118 return subpage_readlen(opaque, addr, 2);
3119}
3120
3121static void subpage_writel (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -05003122 target_phys_addr_t addr, uint32_t value)
blueswir1db7b5422007-05-26 17:36:03 +00003123{
3124#if defined(DEBUG_SUBPAGE)
3125 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3126#endif
3127 subpage_writelen(opaque, addr, value, 2);
3128}
3129
Blue Swirld60efc62009-08-25 18:29:31 +00003130static CPUReadMemoryFunc * const subpage_read[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003131 &subpage_readb,
3132 &subpage_readw,
3133 &subpage_readl,
3134};
3135
Blue Swirld60efc62009-08-25 18:29:31 +00003136static CPUWriteMemoryFunc * const subpage_write[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003137 &subpage_writeb,
3138 &subpage_writew,
3139 &subpage_writel,
3140};
3141
Anthony Liguoric227f092009-10-01 16:12:16 -05003142static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3143 ram_addr_t memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003144{
3145 int idx, eidx;
blueswir14254fab2008-01-01 16:57:19 +00003146 unsigned int i;
blueswir1db7b5422007-05-26 17:36:03 +00003147
3148 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3149 return -1;
3150 idx = SUBPAGE_IDX(start);
3151 eidx = SUBPAGE_IDX(end);
3152#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00003153 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00003154 mmio, start, end, idx, eidx, memory);
3155#endif
3156 memory >>= IO_MEM_SHIFT;
3157 for (; idx <= eidx; idx++) {
blueswir14254fab2008-01-01 16:57:19 +00003158 for (i = 0; i < 4; i++) {
blueswir13ee89922008-01-02 19:45:26 +00003159 if (io_mem_read[memory][i]) {
3160 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
3161 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
pbrook8da3ff12008-12-01 18:59:50 +00003162 mmio->region_offset[idx][0][i] = region_offset;
blueswir13ee89922008-01-02 19:45:26 +00003163 }
3164 if (io_mem_write[memory][i]) {
3165 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
3166 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
pbrook8da3ff12008-12-01 18:59:50 +00003167 mmio->region_offset[idx][1][i] = region_offset;
blueswir13ee89922008-01-02 19:45:26 +00003168 }
blueswir14254fab2008-01-01 16:57:19 +00003169 }
blueswir1db7b5422007-05-26 17:36:03 +00003170 }
3171
3172 return 0;
3173}
3174
Anthony Liguoric227f092009-10-01 16:12:16 -05003175static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3176 ram_addr_t orig_memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003177{
Anthony Liguoric227f092009-10-01 16:12:16 -05003178 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00003179 int subpage_memory;
3180
Anthony Liguoric227f092009-10-01 16:12:16 -05003181 mmio = qemu_mallocz(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00003182
3183 mmio->base = base;
Avi Kivity1eed09c2009-06-14 11:38:51 +03003184 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio);
blueswir1db7b5422007-05-26 17:36:03 +00003185#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00003186 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3187 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00003188#endif
aliguori1eec6142009-02-05 22:06:18 +00003189 *phys = subpage_memory | IO_MEM_SUBPAGE;
3190 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory,
pbrook8da3ff12008-12-01 18:59:50 +00003191 region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00003192
3193 return mmio;
3194}
3195
aliguori88715652009-02-11 15:20:58 +00003196static int get_free_io_mem_idx(void)
3197{
3198 int i;
3199
3200 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3201 if (!io_mem_used[i]) {
3202 io_mem_used[i] = 1;
3203 return i;
3204 }
Riku Voipioc6703b42009-12-03 15:56:05 +02003205 fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
aliguori88715652009-02-11 15:20:58 +00003206 return -1;
3207}
3208
bellard33417e72003-08-10 21:47:01 +00003209/* mem_read and mem_write are arrays of functions containing the
3210 function to access byte (index 0), word (index 1) and dword (index
Paul Brook0b4e6e32009-04-30 18:37:55 +01003211 2). Functions can be omitted with a NULL function pointer.
blueswir13ee89922008-01-02 19:45:26 +00003212 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00003213 modified. If it is zero, a new io zone is allocated. The return
3214 value can be used with cpu_register_physical_memory(). (-1) is
3215 returned if error. */
Avi Kivity1eed09c2009-06-14 11:38:51 +03003216static int cpu_register_io_memory_fixed(int io_index,
Blue Swirld60efc62009-08-25 18:29:31 +00003217 CPUReadMemoryFunc * const *mem_read,
3218 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03003219 void *opaque)
bellard33417e72003-08-10 21:47:01 +00003220{
blueswir14254fab2008-01-01 16:57:19 +00003221 int i, subwidth = 0;
bellard33417e72003-08-10 21:47:01 +00003222
3223 if (io_index <= 0) {
aliguori88715652009-02-11 15:20:58 +00003224 io_index = get_free_io_mem_idx();
3225 if (io_index == -1)
3226 return io_index;
bellard33417e72003-08-10 21:47:01 +00003227 } else {
Avi Kivity1eed09c2009-06-14 11:38:51 +03003228 io_index >>= IO_MEM_SHIFT;
bellard33417e72003-08-10 21:47:01 +00003229 if (io_index >= IO_MEM_NB_ENTRIES)
3230 return -1;
3231 }
bellardb5ff1b32005-11-26 10:38:39 +00003232
bellard33417e72003-08-10 21:47:01 +00003233 for(i = 0;i < 3; i++) {
blueswir14254fab2008-01-01 16:57:19 +00003234 if (!mem_read[i] || !mem_write[i])
3235 subwidth = IO_MEM_SUBWIDTH;
bellard33417e72003-08-10 21:47:01 +00003236 io_mem_read[io_index][i] = mem_read[i];
3237 io_mem_write[io_index][i] = mem_write[i];
3238 }
bellarda4193c82004-06-03 14:01:43 +00003239 io_mem_opaque[io_index] = opaque;
blueswir14254fab2008-01-01 16:57:19 +00003240 return (io_index << IO_MEM_SHIFT) | subwidth;
bellard33417e72003-08-10 21:47:01 +00003241}
bellard61382a52003-10-27 21:22:23 +00003242
Blue Swirld60efc62009-08-25 18:29:31 +00003243int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
3244 CPUWriteMemoryFunc * const *mem_write,
Avi Kivity1eed09c2009-06-14 11:38:51 +03003245 void *opaque)
3246{
3247 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
3248}
3249
aliguori88715652009-02-11 15:20:58 +00003250void cpu_unregister_io_memory(int io_table_address)
3251{
3252 int i;
3253 int io_index = io_table_address >> IO_MEM_SHIFT;
3254
3255 for (i=0;i < 3; i++) {
3256 io_mem_read[io_index][i] = unassigned_mem_read[i];
3257 io_mem_write[io_index][i] = unassigned_mem_write[i];
3258 }
3259 io_mem_opaque[io_index] = NULL;
3260 io_mem_used[io_index] = 0;
3261}
3262
Avi Kivitye9179ce2009-06-14 11:38:52 +03003263static void io_mem_init(void)
3264{
3265 int i;
3266
3267 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
3268 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
3269 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
3270 for (i=0; i<5; i++)
3271 io_mem_used[i] = 1;
3272
3273 io_mem_watch = cpu_register_io_memory(watch_mem_read,
3274 watch_mem_write, NULL);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003275}
3276
pbrooke2eef172008-06-08 01:09:01 +00003277#endif /* !defined(CONFIG_USER_ONLY) */
3278
bellard13eb76e2004-01-24 15:23:36 +00003279/* physical memory access (slow version, mainly for debug) */
3280#if defined(CONFIG_USER_ONLY)
Paul Brooka68fe892010-03-01 00:08:59 +00003281int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3282 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003283{
3284 int l, flags;
3285 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00003286 void * p;
bellard13eb76e2004-01-24 15:23:36 +00003287
3288 while (len > 0) {
3289 page = addr & TARGET_PAGE_MASK;
3290 l = (page + TARGET_PAGE_SIZE) - addr;
3291 if (l > len)
3292 l = len;
3293 flags = page_get_flags(page);
3294 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00003295 return -1;
bellard13eb76e2004-01-24 15:23:36 +00003296 if (is_write) {
3297 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00003298 return -1;
bellard579a97f2007-11-11 14:26:47 +00003299 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003300 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00003301 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003302 memcpy(p, buf, l);
3303 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00003304 } else {
3305 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00003306 return -1;
bellard579a97f2007-11-11 14:26:47 +00003307 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003308 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00003309 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003310 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00003311 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00003312 }
3313 len -= l;
3314 buf += l;
3315 addr += l;
3316 }
Paul Brooka68fe892010-03-01 00:08:59 +00003317 return 0;
bellard13eb76e2004-01-24 15:23:36 +00003318}
bellard8df1cd02005-01-28 22:37:22 +00003319
bellard13eb76e2004-01-24 15:23:36 +00003320#else
Anthony Liguoric227f092009-10-01 16:12:16 -05003321void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00003322 int len, int is_write)
3323{
3324 int l, io_index;
3325 uint8_t *ptr;
3326 uint32_t val;
Anthony Liguoric227f092009-10-01 16:12:16 -05003327 target_phys_addr_t page;
bellard2e126692004-04-25 21:28:44 +00003328 unsigned long pd;
bellard92e873b2004-05-21 14:52:29 +00003329 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003330
bellard13eb76e2004-01-24 15:23:36 +00003331 while (len > 0) {
3332 page = addr & TARGET_PAGE_MASK;
3333 l = (page + TARGET_PAGE_SIZE) - addr;
3334 if (l > len)
3335 l = len;
bellard92e873b2004-05-21 14:52:29 +00003336 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00003337 if (!p) {
3338 pd = IO_MEM_UNASSIGNED;
3339 } else {
3340 pd = p->phys_offset;
3341 }
ths3b46e622007-09-17 08:09:54 +00003342
bellard13eb76e2004-01-24 15:23:36 +00003343 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00003344 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003345 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003346 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003347 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003348 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard6a00d602005-11-21 23:25:50 +00003349 /* XXX: could force cpu_single_env to NULL to avoid
3350 potential bugs */
aurel326c2934d2009-02-18 21:37:17 +00003351 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003352 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003353 val = ldl_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003354 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003355 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003356 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003357 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003358 val = lduw_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003359 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003360 l = 2;
3361 } else {
bellard1c213d12005-09-03 10:49:04 +00003362 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003363 val = ldub_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003364 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003365 l = 1;
3366 }
3367 } else {
bellardb448f2f2004-02-25 23:24:04 +00003368 unsigned long addr1;
3369 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00003370 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003371 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00003372 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00003373 if (!cpu_physical_memory_is_dirty(addr1)) {
3374 /* invalidate code */
3375 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3376 /* set dirty bit */
ths5fafdf22007-09-16 21:08:06 +00003377 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
bellardf23db162005-08-21 19:12:28 +00003378 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00003379 }
bellard13eb76e2004-01-24 15:23:36 +00003380 }
3381 } else {
ths5fafdf22007-09-16 21:08:06 +00003382 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003383 !(pd & IO_MEM_ROMD)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003384 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003385 /* I/O case */
3386 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003387 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003388 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3389 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003390 /* 32 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003391 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003392 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003393 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003394 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003395 /* 16 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003396 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003397 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003398 l = 2;
3399 } else {
bellard1c213d12005-09-03 10:49:04 +00003400 /* 8 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003401 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003402 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003403 l = 1;
3404 }
3405 } else {
3406 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003407 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard13eb76e2004-01-24 15:23:36 +00003408 (addr & ~TARGET_PAGE_MASK);
3409 memcpy(buf, ptr, l);
3410 }
3411 }
3412 len -= l;
3413 buf += l;
3414 addr += l;
3415 }
3416}
bellard8df1cd02005-01-28 22:37:22 +00003417
bellardd0ecd2a2006-04-23 17:14:48 +00003418/* used for ROM loading : can write in RAM and ROM */
Anthony Liguoric227f092009-10-01 16:12:16 -05003419void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00003420 const uint8_t *buf, int len)
3421{
3422 int l;
3423 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003424 target_phys_addr_t page;
bellardd0ecd2a2006-04-23 17:14:48 +00003425 unsigned long pd;
3426 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003427
bellardd0ecd2a2006-04-23 17:14:48 +00003428 while (len > 0) {
3429 page = addr & TARGET_PAGE_MASK;
3430 l = (page + TARGET_PAGE_SIZE) - addr;
3431 if (l > len)
3432 l = len;
3433 p = phys_page_find(page >> TARGET_PAGE_BITS);
3434 if (!p) {
3435 pd = IO_MEM_UNASSIGNED;
3436 } else {
3437 pd = p->phys_offset;
3438 }
ths3b46e622007-09-17 08:09:54 +00003439
bellardd0ecd2a2006-04-23 17:14:48 +00003440 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00003441 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3442 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00003443 /* do nothing */
3444 } else {
3445 unsigned long addr1;
3446 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3447 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003448 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00003449 memcpy(ptr, buf, l);
3450 }
3451 len -= l;
3452 buf += l;
3453 addr += l;
3454 }
3455}
3456
aliguori6d16c2f2009-01-22 16:59:11 +00003457typedef struct {
3458 void *buffer;
Anthony Liguoric227f092009-10-01 16:12:16 -05003459 target_phys_addr_t addr;
3460 target_phys_addr_t len;
aliguori6d16c2f2009-01-22 16:59:11 +00003461} BounceBuffer;
3462
3463static BounceBuffer bounce;
3464
aliguoriba223c22009-01-22 16:59:16 +00003465typedef struct MapClient {
3466 void *opaque;
3467 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00003468 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00003469} MapClient;
3470
Blue Swirl72cf2d42009-09-12 07:36:22 +00003471static QLIST_HEAD(map_client_list, MapClient) map_client_list
3472 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003473
3474void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3475{
3476 MapClient *client = qemu_malloc(sizeof(*client));
3477
3478 client->opaque = opaque;
3479 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003480 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00003481 return client;
3482}
3483
3484void cpu_unregister_map_client(void *_client)
3485{
3486 MapClient *client = (MapClient *)_client;
3487
Blue Swirl72cf2d42009-09-12 07:36:22 +00003488 QLIST_REMOVE(client, link);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003489 qemu_free(client);
aliguoriba223c22009-01-22 16:59:16 +00003490}
3491
3492static void cpu_notify_map_clients(void)
3493{
3494 MapClient *client;
3495
Blue Swirl72cf2d42009-09-12 07:36:22 +00003496 while (!QLIST_EMPTY(&map_client_list)) {
3497 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00003498 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09003499 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00003500 }
3501}
3502
aliguori6d16c2f2009-01-22 16:59:11 +00003503/* Map a physical memory region into a host virtual address.
3504 * May map a subset of the requested range, given by and returned in *plen.
3505 * May return NULL if resources needed to perform the mapping are exhausted.
3506 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00003507 * Use cpu_register_map_client() to know when retrying the map operation is
3508 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00003509 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003510void *cpu_physical_memory_map(target_phys_addr_t addr,
3511 target_phys_addr_t *plen,
aliguori6d16c2f2009-01-22 16:59:11 +00003512 int is_write)
3513{
Anthony Liguoric227f092009-10-01 16:12:16 -05003514 target_phys_addr_t len = *plen;
3515 target_phys_addr_t done = 0;
aliguori6d16c2f2009-01-22 16:59:11 +00003516 int l;
3517 uint8_t *ret = NULL;
3518 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003519 target_phys_addr_t page;
aliguori6d16c2f2009-01-22 16:59:11 +00003520 unsigned long pd;
3521 PhysPageDesc *p;
3522 unsigned long addr1;
3523
3524 while (len > 0) {
3525 page = addr & TARGET_PAGE_MASK;
3526 l = (page + TARGET_PAGE_SIZE) - addr;
3527 if (l > len)
3528 l = len;
3529 p = phys_page_find(page >> TARGET_PAGE_BITS);
3530 if (!p) {
3531 pd = IO_MEM_UNASSIGNED;
3532 } else {
3533 pd = p->phys_offset;
3534 }
3535
3536 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3537 if (done || bounce.buffer) {
3538 break;
3539 }
3540 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3541 bounce.addr = addr;
3542 bounce.len = l;
3543 if (!is_write) {
3544 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3545 }
3546 ptr = bounce.buffer;
3547 } else {
3548 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003549 ptr = qemu_get_ram_ptr(addr1);
aliguori6d16c2f2009-01-22 16:59:11 +00003550 }
3551 if (!done) {
3552 ret = ptr;
3553 } else if (ret + done != ptr) {
3554 break;
3555 }
3556
3557 len -= l;
3558 addr += l;
3559 done += l;
3560 }
3561 *plen = done;
3562 return ret;
3563}
3564
3565/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3566 * Will also mark the memory as dirty if is_write == 1. access_len gives
3567 * the amount of memory that was actually read or written by the caller.
3568 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003569void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3570 int is_write, target_phys_addr_t access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00003571{
3572 if (buffer != bounce.buffer) {
3573 if (is_write) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003574 ram_addr_t addr1 = qemu_ram_addr_from_host(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00003575 while (access_len) {
3576 unsigned l;
3577 l = TARGET_PAGE_SIZE;
3578 if (l > access_len)
3579 l = access_len;
3580 if (!cpu_physical_memory_is_dirty(addr1)) {
3581 /* invalidate code */
3582 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3583 /* set dirty bit */
3584 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3585 (0xff & ~CODE_DIRTY_FLAG);
3586 }
3587 addr1 += l;
3588 access_len -= l;
3589 }
3590 }
3591 return;
3592 }
3593 if (is_write) {
3594 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3595 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00003596 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00003597 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00003598 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00003599}
bellardd0ecd2a2006-04-23 17:14:48 +00003600
bellard8df1cd02005-01-28 22:37:22 +00003601/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003602uint32_t ldl_phys(target_phys_addr_t addr)
bellard8df1cd02005-01-28 22:37:22 +00003603{
3604 int io_index;
3605 uint8_t *ptr;
3606 uint32_t val;
3607 unsigned long pd;
3608 PhysPageDesc *p;
3609
3610 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3611 if (!p) {
3612 pd = IO_MEM_UNASSIGNED;
3613 } else {
3614 pd = p->phys_offset;
3615 }
ths3b46e622007-09-17 08:09:54 +00003616
ths5fafdf22007-09-16 21:08:06 +00003617 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003618 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00003619 /* I/O case */
3620 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003621 if (p)
3622 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003623 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3624 } else {
3625 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003626 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00003627 (addr & ~TARGET_PAGE_MASK);
3628 val = ldl_p(ptr);
3629 }
3630 return val;
3631}
3632
bellard84b7b8e2005-11-28 21:19:04 +00003633/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003634uint64_t ldq_phys(target_phys_addr_t addr)
bellard84b7b8e2005-11-28 21:19:04 +00003635{
3636 int io_index;
3637 uint8_t *ptr;
3638 uint64_t val;
3639 unsigned long pd;
3640 PhysPageDesc *p;
3641
3642 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3643 if (!p) {
3644 pd = IO_MEM_UNASSIGNED;
3645 } else {
3646 pd = p->phys_offset;
3647 }
ths3b46e622007-09-17 08:09:54 +00003648
bellard2a4188a2006-06-25 21:54:59 +00003649 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3650 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00003651 /* I/O case */
3652 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003653 if (p)
3654 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard84b7b8e2005-11-28 21:19:04 +00003655#ifdef TARGET_WORDS_BIGENDIAN
3656 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3657 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3658#else
3659 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3660 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3661#endif
3662 } else {
3663 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003664 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00003665 (addr & ~TARGET_PAGE_MASK);
3666 val = ldq_p(ptr);
3667 }
3668 return val;
3669}
3670
bellardaab33092005-10-30 20:48:42 +00003671/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003672uint32_t ldub_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003673{
3674 uint8_t val;
3675 cpu_physical_memory_read(addr, &val, 1);
3676 return val;
3677}
3678
3679/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003680uint32_t lduw_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00003681{
3682 uint16_t val;
3683 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
3684 return tswap16(val);
3685}
3686
bellard8df1cd02005-01-28 22:37:22 +00003687/* warning: addr must be aligned. The ram page is not masked as dirty
3688 and the code inside is not invalidated. It is useful if the dirty
3689 bits are used to track modified PTEs */
Anthony Liguoric227f092009-10-01 16:12:16 -05003690void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003691{
3692 int io_index;
3693 uint8_t *ptr;
3694 unsigned long pd;
3695 PhysPageDesc *p;
3696
3697 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3698 if (!p) {
3699 pd = IO_MEM_UNASSIGNED;
3700 } else {
3701 pd = p->phys_offset;
3702 }
ths3b46e622007-09-17 08:09:54 +00003703
bellard3a7d9292005-08-21 09:26:42 +00003704 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003705 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003706 if (p)
3707 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003708 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3709 } else {
aliguori74576192008-10-06 14:02:03 +00003710 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00003711 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003712 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00003713
3714 if (unlikely(in_migration)) {
3715 if (!cpu_physical_memory_is_dirty(addr1)) {
3716 /* invalidate code */
3717 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3718 /* set dirty bit */
3719 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3720 (0xff & ~CODE_DIRTY_FLAG);
3721 }
3722 }
bellard8df1cd02005-01-28 22:37:22 +00003723 }
3724}
3725
Anthony Liguoric227f092009-10-01 16:12:16 -05003726void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
j_mayerbc98a7e2007-04-04 07:55:12 +00003727{
3728 int io_index;
3729 uint8_t *ptr;
3730 unsigned long pd;
3731 PhysPageDesc *p;
3732
3733 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3734 if (!p) {
3735 pd = IO_MEM_UNASSIGNED;
3736 } else {
3737 pd = p->phys_offset;
3738 }
ths3b46e622007-09-17 08:09:54 +00003739
j_mayerbc98a7e2007-04-04 07:55:12 +00003740 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3741 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003742 if (p)
3743 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
j_mayerbc98a7e2007-04-04 07:55:12 +00003744#ifdef TARGET_WORDS_BIGENDIAN
3745 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3746 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3747#else
3748 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3749 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3750#endif
3751 } else {
pbrook5579c7f2009-04-11 14:47:08 +00003752 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00003753 (addr & ~TARGET_PAGE_MASK);
3754 stq_p(ptr, val);
3755 }
3756}
3757
bellard8df1cd02005-01-28 22:37:22 +00003758/* warning: addr must be aligned */
Anthony Liguoric227f092009-10-01 16:12:16 -05003759void stl_phys(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00003760{
3761 int io_index;
3762 uint8_t *ptr;
3763 unsigned long pd;
3764 PhysPageDesc *p;
3765
3766 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3767 if (!p) {
3768 pd = IO_MEM_UNASSIGNED;
3769 } else {
3770 pd = p->phys_offset;
3771 }
ths3b46e622007-09-17 08:09:54 +00003772
bellard3a7d9292005-08-21 09:26:42 +00003773 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00003774 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003775 if (p)
3776 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00003777 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3778 } else {
3779 unsigned long addr1;
3780 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3781 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003782 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00003783 stl_p(ptr, val);
bellard3a7d9292005-08-21 09:26:42 +00003784 if (!cpu_physical_memory_is_dirty(addr1)) {
3785 /* invalidate code */
3786 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3787 /* set dirty bit */
bellardf23db162005-08-21 19:12:28 +00003788 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3789 (0xff & ~CODE_DIRTY_FLAG);
bellard3a7d9292005-08-21 09:26:42 +00003790 }
bellard8df1cd02005-01-28 22:37:22 +00003791 }
3792}
3793
bellardaab33092005-10-30 20:48:42 +00003794/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003795void stb_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003796{
3797 uint8_t v = val;
3798 cpu_physical_memory_write(addr, &v, 1);
3799}
3800
3801/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003802void stw_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00003803{
3804 uint16_t v = tswap16(val);
3805 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3806}
3807
3808/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05003809void stq_phys(target_phys_addr_t addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00003810{
3811 val = tswap64(val);
3812 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3813}
3814
aliguori5e2972f2009-03-28 17:51:36 +00003815/* virtual memory access for debug (includes writing to ROM) */
ths5fafdf22007-09-16 21:08:06 +00003816int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00003817 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003818{
3819 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05003820 target_phys_addr_t phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00003821 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00003822
3823 while (len > 0) {
3824 page = addr & TARGET_PAGE_MASK;
3825 phys_addr = cpu_get_phys_page_debug(env, page);
3826 /* if no physical page mapped, return an error */
3827 if (phys_addr == -1)
3828 return -1;
3829 l = (page + TARGET_PAGE_SIZE) - addr;
3830 if (l > len)
3831 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00003832 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00003833 if (is_write)
3834 cpu_physical_memory_write_rom(phys_addr, buf, l);
3835 else
aliguori5e2972f2009-03-28 17:51:36 +00003836 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00003837 len -= l;
3838 buf += l;
3839 addr += l;
3840 }
3841 return 0;
3842}
Paul Brooka68fe892010-03-01 00:08:59 +00003843#endif
bellard13eb76e2004-01-24 15:23:36 +00003844
pbrook2e70f6e2008-06-29 01:03:05 +00003845/* in deterministic execution mode, instructions doing device I/Os
3846 must be at the end of the TB */
3847void cpu_io_recompile(CPUState *env, void *retaddr)
3848{
3849 TranslationBlock *tb;
3850 uint32_t n, cflags;
3851 target_ulong pc, cs_base;
3852 uint64_t flags;
3853
3854 tb = tb_find_pc((unsigned long)retaddr);
3855 if (!tb) {
3856 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3857 retaddr);
3858 }
3859 n = env->icount_decr.u16.low + tb->icount;
3860 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3861 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00003862 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00003863 n = n - env->icount_decr.u16.low;
3864 /* Generate a new TB ending on the I/O insn. */
3865 n++;
3866 /* On MIPS and SH, delay slot instructions can only be restarted if
3867 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00003868 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00003869 branch. */
3870#if defined(TARGET_MIPS)
3871 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3872 env->active_tc.PC -= 4;
3873 env->icount_decr.u16.low++;
3874 env->hflags &= ~MIPS_HFLAG_BMASK;
3875 }
3876#elif defined(TARGET_SH4)
3877 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3878 && n > 1) {
3879 env->pc -= 2;
3880 env->icount_decr.u16.low++;
3881 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3882 }
3883#endif
3884 /* This should never happen. */
3885 if (n > CF_COUNT_MASK)
3886 cpu_abort(env, "TB too big during recompile");
3887
3888 cflags = n | CF_LAST_IO;
3889 pc = tb->pc;
3890 cs_base = tb->cs_base;
3891 flags = tb->flags;
3892 tb_phys_invalidate(tb, -1);
3893 /* FIXME: In theory this could raise an exception. In practice
3894 we have already translated the block once so it's probably ok. */
3895 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00003896 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00003897 the first in the TB) then we end up generating a whole new TB and
3898 repeating the fault, which is horribly inefficient.
3899 Better would be to execute just this insn uncached, or generate a
3900 second new TB. */
3901 cpu_resume_from_signal(env, NULL);
3902}
3903
bellarde3db7222005-01-26 22:00:47 +00003904void dump_exec_info(FILE *f,
3905 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3906{
3907 int i, target_code_size, max_target_code_size;
3908 int direct_jmp_count, direct_jmp2_count, cross_page;
3909 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00003910
bellarde3db7222005-01-26 22:00:47 +00003911 target_code_size = 0;
3912 max_target_code_size = 0;
3913 cross_page = 0;
3914 direct_jmp_count = 0;
3915 direct_jmp2_count = 0;
3916 for(i = 0; i < nb_tbs; i++) {
3917 tb = &tbs[i];
3918 target_code_size += tb->size;
3919 if (tb->size > max_target_code_size)
3920 max_target_code_size = tb->size;
3921 if (tb->page_addr[1] != -1)
3922 cross_page++;
3923 if (tb->tb_next_offset[0] != 0xffff) {
3924 direct_jmp_count++;
3925 if (tb->tb_next_offset[1] != 0xffff) {
3926 direct_jmp2_count++;
3927 }
3928 }
3929 }
3930 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00003931 cpu_fprintf(f, "Translation buffer state:\n");
bellard26a5f132008-05-28 12:30:31 +00003932 cpu_fprintf(f, "gen code size %ld/%ld\n",
3933 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3934 cpu_fprintf(f, "TB count %d/%d\n",
3935 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00003936 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00003937 nb_tbs ? target_code_size / nb_tbs : 0,
3938 max_target_code_size);
ths5fafdf22007-09-16 21:08:06 +00003939 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00003940 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3941 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00003942 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3943 cross_page,
bellarde3db7222005-01-26 22:00:47 +00003944 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3945 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00003946 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00003947 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3948 direct_jmp2_count,
3949 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00003950 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00003951 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3952 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3953 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00003954 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00003955}
3956
ths5fafdf22007-09-16 21:08:06 +00003957#if !defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +00003958
3959#define MMUSUFFIX _cmmu
3960#define GETPC() NULL
3961#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00003962#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00003963
3964#define SHIFT 0
3965#include "softmmu_template.h"
3966
3967#define SHIFT 1
3968#include "softmmu_template.h"
3969
3970#define SHIFT 2
3971#include "softmmu_template.h"
3972
3973#define SHIFT 3
3974#include "softmmu_template.h"
3975
3976#undef env
3977
3978#endif