blob: d8b21801708f8069d42edaf449db17f8fdf75b60 [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
Stefan Weil055403b2010-10-22 23:03:32 +020027#include "qemu-common.h"
bellard6180a182003-09-30 21:04:53 +000028#include "cpu.h"
bellardb67d9a52008-05-23 09:57:34 +000029#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000030#include "hw/hw.h"
Alex Williamsoncc9e98c2010-06-25 11:09:43 -060031#include "hw/qdev.h"
aliguori74576192008-10-06 14:02:03 +000032#include "osdep.h"
aliguori7ba1e612008-11-05 16:04:33 +000033#include "kvm.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010034#include "hw/xen.h"
Blue Swirl29e922b2010-03-29 19:24:00 +000035#include "qemu-timer.h"
Avi Kivity62152b82011-07-26 14:26:14 +030036#include "memory.h"
37#include "exec-memory.h"
pbrook53a59602006-03-25 19:31:22 +000038#if defined(CONFIG_USER_ONLY)
39#include <qemu.h>
Juergen Lockf01576f2010-03-25 22:32:16 +010040#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
41#include <sys/param.h>
42#if __FreeBSD_version >= 700104
43#define HAVE_KINFO_GETVMMAP
44#define sigqueue sigqueue_freebsd /* avoid redefinition */
45#include <sys/time.h>
46#include <sys/proc.h>
47#include <machine/profile.h>
48#define _KERNEL
49#include <sys/user.h>
50#undef _KERNEL
51#undef sigqueue
52#include <libutil.h>
53#endif
54#endif
Jun Nakajima432d2682010-08-31 16:41:25 +010055#else /* !CONFIG_USER_ONLY */
56#include "xen-mapcache.h"
Stefano Stabellini6506e4f2011-05-19 18:35:44 +010057#include "trace.h"
pbrook53a59602006-03-25 19:31:22 +000058#endif
bellard54936002003-05-13 00:25:15 +000059
bellardfd6ce8f2003-05-14 19:00:11 +000060//#define DEBUG_TB_INVALIDATE
bellard66e85a22003-06-24 13:28:12 +000061//#define DEBUG_FLUSH
bellard9fa3e852004-01-04 18:06:42 +000062//#define DEBUG_TLB
pbrook67d3b952006-12-18 05:03:52 +000063//#define DEBUG_UNASSIGNED
bellardfd6ce8f2003-05-14 19:00:11 +000064
65/* make various TB consistency checks */
ths5fafdf22007-09-16 21:08:06 +000066//#define DEBUG_TB_CHECK
67//#define DEBUG_TLB_CHECK
bellardfd6ce8f2003-05-14 19:00:11 +000068
ths1196be32007-03-17 15:17:58 +000069//#define DEBUG_IOPORT
blueswir1db7b5422007-05-26 17:36:03 +000070//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000071
pbrook99773bd2006-04-16 15:14:59 +000072#if !defined(CONFIG_USER_ONLY)
73/* TB consistency checks only implemented for usermode emulation. */
74#undef DEBUG_TB_CHECK
75#endif
76
bellard9fa3e852004-01-04 18:06:42 +000077#define SMC_BITMAP_USE_THRESHOLD 10
78
blueswir1bdaf78e2008-10-04 07:24:27 +000079static TranslationBlock *tbs;
Stefan Weil24ab68a2010-07-19 18:23:17 +020080static int code_gen_max_blocks;
bellard9fa3e852004-01-04 18:06:42 +000081TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
blueswir1bdaf78e2008-10-04 07:24:27 +000082static int nb_tbs;
bellardeb51d102003-05-14 21:51:13 +000083/* any access to the tbs or the page table must use this lock */
Anthony Liguoric227f092009-10-01 16:12:16 -050084spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
bellardfd6ce8f2003-05-14 19:00:11 +000085
blueswir1141ac462008-07-26 15:05:57 +000086#if defined(__arm__) || defined(__sparc_v9__)
87/* The prologue must be reachable with a direct jump. ARM and Sparc64
88 have limited branch ranges (possibly also PPC) so place it in a
blueswir1d03d8602008-07-10 17:21:31 +000089 section close to code segment. */
90#define code_gen_section \
91 __attribute__((__section__(".gen_code"))) \
92 __attribute__((aligned (32)))
Stefan Weilf8e2af12009-06-18 23:04:48 +020093#elif defined(_WIN32)
94/* Maximum alignment for Win32 is 16. */
95#define code_gen_section \
96 __attribute__((aligned (16)))
blueswir1d03d8602008-07-10 17:21:31 +000097#else
98#define code_gen_section \
99 __attribute__((aligned (32)))
100#endif
101
102uint8_t code_gen_prologue[1024] code_gen_section;
blueswir1bdaf78e2008-10-04 07:24:27 +0000103static uint8_t *code_gen_buffer;
104static unsigned long code_gen_buffer_size;
bellard26a5f132008-05-28 12:30:31 +0000105/* threshold to flush the translated code buffer */
blueswir1bdaf78e2008-10-04 07:24:27 +0000106static unsigned long code_gen_buffer_max_size;
Stefan Weil24ab68a2010-07-19 18:23:17 +0200107static uint8_t *code_gen_ptr;
bellardfd6ce8f2003-05-14 19:00:11 +0000108
pbrooke2eef172008-06-08 01:09:01 +0000109#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +0000110int phys_ram_fd;
aliguori74576192008-10-06 14:02:03 +0000111static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +0000112
Paolo Bonzini85d59fe2011-08-12 13:18:14 +0200113RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
Avi Kivity62152b82011-07-26 14:26:14 +0300114
115static MemoryRegion *system_memory;
Avi Kivity309cb472011-08-08 16:09:03 +0300116static MemoryRegion *system_io;
Avi Kivity62152b82011-07-26 14:26:14 +0300117
pbrooke2eef172008-06-08 01:09:01 +0000118#endif
bellard9fa3e852004-01-04 18:06:42 +0000119
bellard6a00d602005-11-21 23:25:50 +0000120CPUState *first_cpu;
121/* current CPU in the current thread. It is only valid inside
122 cpu_exec() */
Paolo Bonzinib3c4bbe2011-10-28 10:52:42 +0100123DEFINE_TLS(CPUState *,cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +0000124/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +0000125 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +0000126 2 = Adaptive rate instruction counting. */
127int use_icount = 0;
bellard6a00d602005-11-21 23:25:50 +0000128
bellard54936002003-05-13 00:25:15 +0000129typedef struct PageDesc {
bellard92e873b2004-05-21 14:52:29 +0000130 /* list of TBs intersecting this ram page */
bellardfd6ce8f2003-05-14 19:00:11 +0000131 TranslationBlock *first_tb;
bellard9fa3e852004-01-04 18:06:42 +0000132 /* in order to optimize self modifying code, we count the number
133 of lookups we do to a given page to use a bitmap */
134 unsigned int code_write_count;
135 uint8_t *code_bitmap;
136#if defined(CONFIG_USER_ONLY)
137 unsigned long flags;
138#endif
bellard54936002003-05-13 00:25:15 +0000139} PageDesc;
140
Paul Brook41c1b1c2010-03-12 16:54:58 +0000141/* In system mode we want L1_MAP to be based on ram offsets,
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800142 while in user mode we want it to be based on virtual addresses. */
143#if !defined(CONFIG_USER_ONLY)
Paul Brook41c1b1c2010-03-12 16:54:58 +0000144#if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
145# define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
146#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800147# define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
Paul Brook41c1b1c2010-03-12 16:54:58 +0000148#endif
j_mayerbedb69e2007-04-05 20:08:21 +0000149#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800150# define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
j_mayerbedb69e2007-04-05 20:08:21 +0000151#endif
bellard54936002003-05-13 00:25:15 +0000152
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800153/* Size of the L2 (and L3, etc) page tables. */
154#define L2_BITS 10
bellard54936002003-05-13 00:25:15 +0000155#define L2_SIZE (1 << L2_BITS)
156
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800157/* The bits remaining after N lower levels of page tables. */
158#define P_L1_BITS_REM \
159 ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
160#define V_L1_BITS_REM \
161 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
162
163/* Size of the L1 page table. Avoid silly small sizes. */
164#if P_L1_BITS_REM < 4
165#define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
166#else
167#define P_L1_BITS P_L1_BITS_REM
168#endif
169
170#if V_L1_BITS_REM < 4
171#define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
172#else
173#define V_L1_BITS V_L1_BITS_REM
174#endif
175
176#define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
177#define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
178
179#define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
180#define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
181
bellard83fb7ad2004-07-05 21:25:26 +0000182unsigned long qemu_real_host_page_size;
bellard83fb7ad2004-07-05 21:25:26 +0000183unsigned 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)
Paul Brook41c1b1c2010-03-12 16:54:58 +0000191typedef struct PhysPageDesc {
192 /* offset in host memory of the page + io_index in the low bits */
193 ram_addr_t phys_offset;
194 ram_addr_t region_offset;
195} PhysPageDesc;
196
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800197/* This is a multi-level map on the physical address space.
198 The bottom level has pointers to PhysPageDesc. */
199static void *l1_phys_map[P_L1_SIZE];
Paul Brook6d9a1302010-02-28 23:55:53 +0000200
pbrooke2eef172008-06-08 01:09:01 +0000201static void io_mem_init(void);
Avi Kivity62152b82011-07-26 14:26:14 +0300202static void memory_map_init(void);
pbrooke2eef172008-06-08 01:09:01 +0000203
bellard33417e72003-08-10 21:47:01 +0000204/* io memory support */
bellard33417e72003-08-10 21:47:01 +0000205CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
206CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
bellarda4193c82004-06-03 14:01:43 +0000207void *io_mem_opaque[IO_MEM_NB_ENTRIES];
blueswir1511d2b12009-03-07 15:32:56 +0000208static char io_mem_used[IO_MEM_NB_ENTRIES];
pbrook6658ffb2007-03-16 23:58:11 +0000209static int io_mem_watch;
210#endif
bellard33417e72003-08-10 21:47:01 +0000211
bellard34865132003-10-05 14:28:56 +0000212/* log support */
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200213#ifdef WIN32
214static const char *logfilename = "qemu.log";
215#else
blueswir1d9b630f2008-10-05 09:57:08 +0000216static const char *logfilename = "/tmp/qemu.log";
Juha Riihimäki1e8b27c2009-12-03 15:56:02 +0200217#endif
bellard34865132003-10-05 14:28:56 +0000218FILE *logfile;
219int loglevel;
pbrooke735b912007-06-30 13:53:24 +0000220static int log_append = 0;
bellard34865132003-10-05 14:28:56 +0000221
bellarde3db7222005-01-26 22:00:47 +0000222/* statistics */
Paul Brookb3755a92010-03-12 16:54:58 +0000223#if !defined(CONFIG_USER_ONLY)
bellarde3db7222005-01-26 22:00:47 +0000224static int tlb_flush_count;
Paul Brookb3755a92010-03-12 16:54:58 +0000225#endif
bellarde3db7222005-01-26 22:00:47 +0000226static int tb_flush_count;
227static int tb_phys_invalidate_count;
228
bellard7cb69ca2008-05-10 10:55:51 +0000229#ifdef _WIN32
230static void map_exec(void *addr, long size)
231{
232 DWORD old_protect;
233 VirtualProtect(addr, size,
234 PAGE_EXECUTE_READWRITE, &old_protect);
235
236}
237#else
238static void map_exec(void *addr, long size)
239{
bellard43694152008-05-29 09:35:57 +0000240 unsigned long start, end, page_size;
bellard7cb69ca2008-05-10 10:55:51 +0000241
bellard43694152008-05-29 09:35:57 +0000242 page_size = getpagesize();
bellard7cb69ca2008-05-10 10:55:51 +0000243 start = (unsigned long)addr;
bellard43694152008-05-29 09:35:57 +0000244 start &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000245
246 end = (unsigned long)addr + size;
bellard43694152008-05-29 09:35:57 +0000247 end += page_size - 1;
248 end &= ~(page_size - 1);
bellard7cb69ca2008-05-10 10:55:51 +0000249
250 mprotect((void *)start, end - start,
251 PROT_READ | PROT_WRITE | PROT_EXEC);
252}
253#endif
254
bellardb346ff42003-06-15 20:05:50 +0000255static void page_init(void)
bellard54936002003-05-13 00:25:15 +0000256{
bellard83fb7ad2004-07-05 21:25:26 +0000257 /* NOTE: we can always suppose that qemu_host_page_size >=
bellard54936002003-05-13 00:25:15 +0000258 TARGET_PAGE_SIZE */
aliguoric2b48b62008-11-11 22:06:42 +0000259#ifdef _WIN32
260 {
261 SYSTEM_INFO system_info;
262
263 GetSystemInfo(&system_info);
264 qemu_real_host_page_size = system_info.dwPageSize;
265 }
266#else
267 qemu_real_host_page_size = getpagesize();
268#endif
bellard83fb7ad2004-07-05 21:25:26 +0000269 if (qemu_host_page_size == 0)
270 qemu_host_page_size = qemu_real_host_page_size;
271 if (qemu_host_page_size < TARGET_PAGE_SIZE)
272 qemu_host_page_size = TARGET_PAGE_SIZE;
bellard83fb7ad2004-07-05 21:25:26 +0000273 qemu_host_page_mask = ~(qemu_host_page_size - 1);
balrog50a95692007-12-12 01:16:23 +0000274
Paul Brook2e9a5712010-05-05 16:32:59 +0100275#if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
balrog50a95692007-12-12 01:16:23 +0000276 {
Juergen Lockf01576f2010-03-25 22:32:16 +0100277#ifdef HAVE_KINFO_GETVMMAP
278 struct kinfo_vmentry *freep;
279 int i, cnt;
280
281 freep = kinfo_getvmmap(getpid(), &cnt);
282 if (freep) {
283 mmap_lock();
284 for (i = 0; i < cnt; i++) {
285 unsigned long startaddr, endaddr;
286
287 startaddr = freep[i].kve_start;
288 endaddr = freep[i].kve_end;
289 if (h2g_valid(startaddr)) {
290 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
291
292 if (h2g_valid(endaddr)) {
293 endaddr = h2g(endaddr);
Aurelien Jarnofd436902010-04-10 17:20:36 +0200294 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
Juergen Lockf01576f2010-03-25 22:32:16 +0100295 } else {
296#if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
297 endaddr = ~0ul;
Aurelien Jarnofd436902010-04-10 17:20:36 +0200298 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
Juergen Lockf01576f2010-03-25 22:32:16 +0100299#endif
300 }
301 }
302 }
303 free(freep);
304 mmap_unlock();
305 }
306#else
balrog50a95692007-12-12 01:16:23 +0000307 FILE *f;
balrog50a95692007-12-12 01:16:23 +0000308
pbrook07765902008-05-31 16:33:53 +0000309 last_brk = (unsigned long)sbrk(0);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800310
Aurelien Jarnofd436902010-04-10 17:20:36 +0200311 f = fopen("/compat/linux/proc/self/maps", "r");
balrog50a95692007-12-12 01:16:23 +0000312 if (f) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800313 mmap_lock();
314
balrog50a95692007-12-12 01:16:23 +0000315 do {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800316 unsigned long startaddr, endaddr;
317 int n;
318
319 n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
320
321 if (n == 2 && h2g_valid(startaddr)) {
322 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
323
324 if (h2g_valid(endaddr)) {
325 endaddr = h2g(endaddr);
326 } else {
327 endaddr = ~0ul;
328 }
329 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
balrog50a95692007-12-12 01:16:23 +0000330 }
331 } while (!feof(f));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800332
balrog50a95692007-12-12 01:16:23 +0000333 fclose(f);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800334 mmap_unlock();
balrog50a95692007-12-12 01:16:23 +0000335 }
Juergen Lockf01576f2010-03-25 22:32:16 +0100336#endif
balrog50a95692007-12-12 01:16:23 +0000337 }
338#endif
bellard54936002003-05-13 00:25:15 +0000339}
340
Paul Brook41c1b1c2010-03-12 16:54:58 +0000341static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
bellard54936002003-05-13 00:25:15 +0000342{
Paul Brook41c1b1c2010-03-12 16:54:58 +0000343 PageDesc *pd;
344 void **lp;
345 int i;
346
pbrook17e23772008-06-09 13:47:45 +0000347#if defined(CONFIG_USER_ONLY)
Anthony Liguori7267c092011-08-20 22:09:37 -0500348 /* We can't use g_malloc because it may recurse into a locked mutex. */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800349# define ALLOC(P, SIZE) \
350 do { \
351 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
352 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800353 } while (0)
pbrook17e23772008-06-09 13:47:45 +0000354#else
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800355# define ALLOC(P, SIZE) \
Anthony Liguori7267c092011-08-20 22:09:37 -0500356 do { P = g_malloc0(SIZE); } while (0)
pbrook17e23772008-06-09 13:47:45 +0000357#endif
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800358
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800359 /* Level 1. Always allocated. */
360 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
361
362 /* Level 2..N-1. */
363 for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
364 void **p = *lp;
365
366 if (p == NULL) {
367 if (!alloc) {
368 return NULL;
369 }
370 ALLOC(p, sizeof(void *) * L2_SIZE);
371 *lp = p;
372 }
373
374 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000375 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800376
377 pd = *lp;
378 if (pd == NULL) {
379 if (!alloc) {
380 return NULL;
381 }
382 ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
383 *lp = pd;
384 }
385
386#undef ALLOC
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800387
388 return pd + (index & (L2_SIZE - 1));
bellard54936002003-05-13 00:25:15 +0000389}
390
Paul Brook41c1b1c2010-03-12 16:54:58 +0000391static inline PageDesc *page_find(tb_page_addr_t index)
bellard54936002003-05-13 00:25:15 +0000392{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800393 return page_find_alloc(index, 0);
bellard54936002003-05-13 00:25:15 +0000394}
395
Paul Brook6d9a1302010-02-28 23:55:53 +0000396#if !defined(CONFIG_USER_ONLY)
Anthony Liguoric227f092009-10-01 16:12:16 -0500397static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
bellard92e873b2004-05-21 14:52:29 +0000398{
pbrooke3f4e2a2006-04-08 20:02:06 +0000399 PhysPageDesc *pd;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800400 void **lp;
401 int i;
bellard92e873b2004-05-21 14:52:29 +0000402
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800403 /* Level 1. Always allocated. */
404 lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000405
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800406 /* Level 2..N-1. */
407 for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
408 void **p = *lp;
409 if (p == NULL) {
410 if (!alloc) {
411 return NULL;
412 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500413 *lp = p = g_malloc0(sizeof(void *) * L2_SIZE);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800414 }
415 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
bellard108c49b2005-07-24 12:55:09 +0000416 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800417
pbrooke3f4e2a2006-04-08 20:02:06 +0000418 pd = *lp;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800419 if (pd == NULL) {
pbrooke3f4e2a2006-04-08 20:02:06 +0000420 int i;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800421
422 if (!alloc) {
bellard108c49b2005-07-24 12:55:09 +0000423 return NULL;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800424 }
425
Anthony Liguori7267c092011-08-20 22:09:37 -0500426 *lp = pd = g_malloc(sizeof(PhysPageDesc) * L2_SIZE);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800427
pbrook67c4d232009-02-23 13:16:07 +0000428 for (i = 0; i < L2_SIZE; i++) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800429 pd[i].phys_offset = IO_MEM_UNASSIGNED;
430 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
pbrook67c4d232009-02-23 13:16:07 +0000431 }
bellard92e873b2004-05-21 14:52:29 +0000432 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800433
434 return pd + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000435}
436
Anthony Liguoric227f092009-10-01 16:12:16 -0500437static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000438{
bellard108c49b2005-07-24 12:55:09 +0000439 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000440}
441
Anthony Liguoric227f092009-10-01 16:12:16 -0500442static void tlb_protect_code(ram_addr_t ram_addr);
443static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000444 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000445#define mmap_lock() do { } while(0)
446#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000447#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000448
bellard43694152008-05-29 09:35:57 +0000449#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
450
451#if defined(CONFIG_USER_ONLY)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100452/* Currently it is not recommended to allocate big chunks of data in
bellard43694152008-05-29 09:35:57 +0000453 user mode. It will change when a dedicated libc will be used */
454#define USE_STATIC_CODE_GEN_BUFFER
455#endif
456
457#ifdef USE_STATIC_CODE_GEN_BUFFER
Aurelien Jarnoebf50fb2010-03-29 02:12:51 +0200458static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
459 __attribute__((aligned (CODE_GEN_ALIGN)));
bellard43694152008-05-29 09:35:57 +0000460#endif
461
blueswir18fcd3692008-08-17 20:26:25 +0000462static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000463{
bellard43694152008-05-29 09:35:57 +0000464#ifdef USE_STATIC_CODE_GEN_BUFFER
465 code_gen_buffer = static_code_gen_buffer;
466 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
467 map_exec(code_gen_buffer, code_gen_buffer_size);
468#else
bellard26a5f132008-05-28 12:30:31 +0000469 code_gen_buffer_size = tb_size;
470 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000471#if defined(CONFIG_USER_ONLY)
bellard43694152008-05-29 09:35:57 +0000472 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
473#else
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100474 /* XXX: needs adjustments */
pbrook94a6b542009-04-11 17:15:54 +0000475 code_gen_buffer_size = (unsigned long)(ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000476#endif
bellard26a5f132008-05-28 12:30:31 +0000477 }
478 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
479 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
480 /* The code gen buffer location may have constraints depending on
481 the host cpu and OS */
482#if defined(__linux__)
483 {
484 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000485 void *start = NULL;
486
bellard26a5f132008-05-28 12:30:31 +0000487 flags = MAP_PRIVATE | MAP_ANONYMOUS;
488#if defined(__x86_64__)
489 flags |= MAP_32BIT;
490 /* Cannot map more than that */
491 if (code_gen_buffer_size > (800 * 1024 * 1024))
492 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000493#elif defined(__sparc_v9__)
494 // Map the buffer below 2G, so we can use direct calls and branches
495 flags |= MAP_FIXED;
496 start = (void *) 0x60000000UL;
497 if (code_gen_buffer_size > (512 * 1024 * 1024))
498 code_gen_buffer_size = (512 * 1024 * 1024);
balrog1cb06612008-12-01 02:10:17 +0000499#elif defined(__arm__)
balrog63d41242008-12-01 02:19:41 +0000500 /* Map the buffer below 32M, so we can use direct calls and branches */
balrog1cb06612008-12-01 02:10:17 +0000501 flags |= MAP_FIXED;
502 start = (void *) 0x01000000UL;
503 if (code_gen_buffer_size > 16 * 1024 * 1024)
504 code_gen_buffer_size = 16 * 1024 * 1024;
Richard Hendersoneba0b892010-06-04 12:14:14 -0700505#elif defined(__s390x__)
506 /* Map the buffer so that we can use direct calls and branches. */
507 /* We have a +- 4GB range on the branches; leave some slop. */
508 if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) {
509 code_gen_buffer_size = 3ul * 1024 * 1024 * 1024;
510 }
511 start = (void *)0x90000000UL;
bellard26a5f132008-05-28 12:30:31 +0000512#endif
blueswir1141ac462008-07-26 15:05:57 +0000513 code_gen_buffer = mmap(start, code_gen_buffer_size,
514 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000515 flags, -1, 0);
516 if (code_gen_buffer == MAP_FAILED) {
517 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
518 exit(1);
519 }
520 }
Bradcbb608a2010-12-20 21:25:40 -0500521#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
Tobias Nygren9f4b09a2011-08-07 09:57:05 +0000522 || defined(__DragonFly__) || defined(__OpenBSD__) \
523 || defined(__NetBSD__)
aliguori06e67a82008-09-27 15:32:41 +0000524 {
525 int flags;
526 void *addr = NULL;
527 flags = MAP_PRIVATE | MAP_ANONYMOUS;
528#if defined(__x86_64__)
529 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
530 * 0x40000000 is free */
531 flags |= MAP_FIXED;
532 addr = (void *)0x40000000;
533 /* Cannot map more than that */
534 if (code_gen_buffer_size > (800 * 1024 * 1024))
535 code_gen_buffer_size = (800 * 1024 * 1024);
Blue Swirl4cd31ad2011-01-16 08:32:27 +0000536#elif defined(__sparc_v9__)
537 // Map the buffer below 2G, so we can use direct calls and branches
538 flags |= MAP_FIXED;
539 addr = (void *) 0x60000000UL;
540 if (code_gen_buffer_size > (512 * 1024 * 1024)) {
541 code_gen_buffer_size = (512 * 1024 * 1024);
542 }
aliguori06e67a82008-09-27 15:32:41 +0000543#endif
544 code_gen_buffer = mmap(addr, code_gen_buffer_size,
545 PROT_WRITE | PROT_READ | PROT_EXEC,
546 flags, -1, 0);
547 if (code_gen_buffer == MAP_FAILED) {
548 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
549 exit(1);
550 }
551 }
bellard26a5f132008-05-28 12:30:31 +0000552#else
Anthony Liguori7267c092011-08-20 22:09:37 -0500553 code_gen_buffer = g_malloc(code_gen_buffer_size);
bellard26a5f132008-05-28 12:30:31 +0000554 map_exec(code_gen_buffer, code_gen_buffer_size);
555#endif
bellard43694152008-05-29 09:35:57 +0000556#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000557 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
Peter Maydella884da82011-06-22 11:58:25 +0100558 code_gen_buffer_max_size = code_gen_buffer_size -
559 (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
bellard26a5f132008-05-28 12:30:31 +0000560 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
Anthony Liguori7267c092011-08-20 22:09:37 -0500561 tbs = g_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
bellard26a5f132008-05-28 12:30:31 +0000562}
563
564/* Must be called before using the QEMU cpus. 'tb_size' is the size
565 (in bytes) allocated to the translation buffer. Zero means default
566 size. */
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200567void tcg_exec_init(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000568{
bellard26a5f132008-05-28 12:30:31 +0000569 cpu_gen_init();
570 code_gen_alloc(tb_size);
571 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000572 page_init();
Richard Henderson9002ec72010-05-06 08:50:41 -0700573#if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
574 /* There's no guest base to take into account, so go ahead and
575 initialize the prologue now. */
576 tcg_prologue_init(&tcg_ctx);
577#endif
bellard26a5f132008-05-28 12:30:31 +0000578}
579
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200580bool tcg_enabled(void)
581{
582 return code_gen_buffer != NULL;
583}
584
585void cpu_exec_init_all(void)
586{
587#if !defined(CONFIG_USER_ONLY)
588 memory_map_init();
589 io_mem_init();
590#endif
591}
592
pbrook9656f322008-07-01 20:01:19 +0000593#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
594
Juan Quintelae59fb372009-09-29 22:48:21 +0200595static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200596{
597 CPUState *env = opaque;
598
aurel323098dba2009-03-07 21:28:24 +0000599 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
600 version_id is increased. */
601 env->interrupt_request &= ~0x01;
pbrook9656f322008-07-01 20:01:19 +0000602 tlb_flush(env, 1);
603
604 return 0;
605}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200606
607static const VMStateDescription vmstate_cpu_common = {
608 .name = "cpu_common",
609 .version_id = 1,
610 .minimum_version_id = 1,
611 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200612 .post_load = cpu_common_post_load,
613 .fields = (VMStateField []) {
614 VMSTATE_UINT32(halted, CPUState),
615 VMSTATE_UINT32(interrupt_request, CPUState),
616 VMSTATE_END_OF_LIST()
617 }
618};
pbrook9656f322008-07-01 20:01:19 +0000619#endif
620
Glauber Costa950f1472009-06-09 12:15:18 -0400621CPUState *qemu_get_cpu(int cpu)
622{
623 CPUState *env = first_cpu;
624
625 while (env) {
626 if (env->cpu_index == cpu)
627 break;
628 env = env->next_cpu;
629 }
630
631 return env;
632}
633
bellard6a00d602005-11-21 23:25:50 +0000634void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000635{
bellard6a00d602005-11-21 23:25:50 +0000636 CPUState **penv;
637 int cpu_index;
638
pbrookc2764712009-03-07 15:24:59 +0000639#if defined(CONFIG_USER_ONLY)
640 cpu_list_lock();
641#endif
bellard6a00d602005-11-21 23:25:50 +0000642 env->next_cpu = NULL;
643 penv = &first_cpu;
644 cpu_index = 0;
645 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700646 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000647 cpu_index++;
648 }
649 env->cpu_index = cpu_index;
aliguori268a3622009-04-21 22:30:27 +0000650 env->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000651 QTAILQ_INIT(&env->breakpoints);
652 QTAILQ_INIT(&env->watchpoints);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100653#ifndef CONFIG_USER_ONLY
654 env->thread_id = qemu_get_thread_id();
655#endif
bellard6a00d602005-11-21 23:25:50 +0000656 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000657#if defined(CONFIG_USER_ONLY)
658 cpu_list_unlock();
659#endif
pbrookb3c77242008-06-30 16:31:04 +0000660#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600661 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
662 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000663 cpu_save, cpu_load, env);
664#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000665}
666
Tristan Gingoldd1a1eb72011-02-10 10:04:57 +0100667/* Allocate a new translation block. Flush the translation buffer if
668 too many translation blocks or too much generated code. */
669static TranslationBlock *tb_alloc(target_ulong pc)
670{
671 TranslationBlock *tb;
672
673 if (nb_tbs >= code_gen_max_blocks ||
674 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
675 return NULL;
676 tb = &tbs[nb_tbs++];
677 tb->pc = pc;
678 tb->cflags = 0;
679 return tb;
680}
681
682void tb_free(TranslationBlock *tb)
683{
684 /* In practice this is mostly used for single use temporary TB
685 Ignore the hard cases and just back up if this TB happens to
686 be the last one generated. */
687 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
688 code_gen_ptr = tb->tc_ptr;
689 nb_tbs--;
690 }
691}
692
bellard9fa3e852004-01-04 18:06:42 +0000693static inline void invalidate_page_bitmap(PageDesc *p)
694{
695 if (p->code_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500696 g_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000697 p->code_bitmap = NULL;
698 }
699 p->code_write_count = 0;
700}
701
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800702/* Set to NULL all the 'first_tb' fields in all PageDescs. */
703
704static void page_flush_tb_1 (int level, void **lp)
705{
706 int i;
707
708 if (*lp == NULL) {
709 return;
710 }
711 if (level == 0) {
712 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000713 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800714 pd[i].first_tb = NULL;
715 invalidate_page_bitmap(pd + i);
716 }
717 } else {
718 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000719 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800720 page_flush_tb_1 (level - 1, pp + i);
721 }
722 }
723}
724
bellardfd6ce8f2003-05-14 19:00:11 +0000725static void page_flush_tb(void)
726{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800727 int i;
728 for (i = 0; i < V_L1_SIZE; i++) {
729 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
bellardfd6ce8f2003-05-14 19:00:11 +0000730 }
731}
732
733/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000734/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000735void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000736{
bellard6a00d602005-11-21 23:25:50 +0000737 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000738#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000739 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
740 (unsigned long)(code_gen_ptr - code_gen_buffer),
741 nb_tbs, nb_tbs > 0 ?
742 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000743#endif
bellard26a5f132008-05-28 12:30:31 +0000744 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000745 cpu_abort(env1, "Internal error: code buffer overflow\n");
746
bellardfd6ce8f2003-05-14 19:00:11 +0000747 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000748
bellard6a00d602005-11-21 23:25:50 +0000749 for(env = first_cpu; env != NULL; env = env->next_cpu) {
750 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
751 }
bellard9fa3e852004-01-04 18:06:42 +0000752
bellard8a8a6082004-10-03 13:36:49 +0000753 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000754 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000755
bellardfd6ce8f2003-05-14 19:00:11 +0000756 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000757 /* XXX: flush processor icache at this point if cache flush is
758 expensive */
bellarde3db7222005-01-26 22:00:47 +0000759 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000760}
761
762#ifdef DEBUG_TB_CHECK
763
j_mayerbc98a7e2007-04-04 07:55:12 +0000764static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000765{
766 TranslationBlock *tb;
767 int i;
768 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000769 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
770 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000771 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
772 address >= tb->pc + tb->size)) {
Blue Swirl0bf9e312009-07-20 17:19:25 +0000773 printf("ERROR invalidate: address=" TARGET_FMT_lx
774 " PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000775 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000776 }
777 }
778 }
779}
780
781/* verify that all the pages have correct rights for code */
782static void tb_page_check(void)
783{
784 TranslationBlock *tb;
785 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000786
pbrook99773bd2006-04-16 15:14:59 +0000787 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
788 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000789 flags1 = page_get_flags(tb->pc);
790 flags2 = page_get_flags(tb->pc + tb->size - 1);
791 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
792 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000793 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000794 }
795 }
796 }
797}
798
799#endif
800
801/* invalidate one TB */
802static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
803 int next_offset)
804{
805 TranslationBlock *tb1;
806 for(;;) {
807 tb1 = *ptb;
808 if (tb1 == tb) {
809 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
810 break;
811 }
812 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
813 }
814}
815
bellard9fa3e852004-01-04 18:06:42 +0000816static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
817{
818 TranslationBlock *tb1;
819 unsigned int n1;
820
821 for(;;) {
822 tb1 = *ptb;
823 n1 = (long)tb1 & 3;
824 tb1 = (TranslationBlock *)((long)tb1 & ~3);
825 if (tb1 == tb) {
826 *ptb = tb1->page_next[n1];
827 break;
828 }
829 ptb = &tb1->page_next[n1];
830 }
831}
832
bellardd4e81642003-05-25 16:46:15 +0000833static inline void tb_jmp_remove(TranslationBlock *tb, int n)
834{
835 TranslationBlock *tb1, **ptb;
836 unsigned int n1;
837
838 ptb = &tb->jmp_next[n];
839 tb1 = *ptb;
840 if (tb1) {
841 /* find tb(n) in circular list */
842 for(;;) {
843 tb1 = *ptb;
844 n1 = (long)tb1 & 3;
845 tb1 = (TranslationBlock *)((long)tb1 & ~3);
846 if (n1 == n && tb1 == tb)
847 break;
848 if (n1 == 2) {
849 ptb = &tb1->jmp_first;
850 } else {
851 ptb = &tb1->jmp_next[n1];
852 }
853 }
854 /* now we can suppress tb(n) from the list */
855 *ptb = tb->jmp_next[n];
856
857 tb->jmp_next[n] = NULL;
858 }
859}
860
861/* reset the jump entry 'n' of a TB so that it is not chained to
862 another TB */
863static inline void tb_reset_jump(TranslationBlock *tb, int n)
864{
865 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
866}
867
Paul Brook41c1b1c2010-03-12 16:54:58 +0000868void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000869{
bellard6a00d602005-11-21 23:25:50 +0000870 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000871 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000872 unsigned int h, n1;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000873 tb_page_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000874 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000875
bellard9fa3e852004-01-04 18:06:42 +0000876 /* remove the TB from the hash list */
877 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
878 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000879 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000880 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000881
bellard9fa3e852004-01-04 18:06:42 +0000882 /* remove the TB from the page list */
883 if (tb->page_addr[0] != page_addr) {
884 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
885 tb_page_remove(&p->first_tb, tb);
886 invalidate_page_bitmap(p);
887 }
888 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
889 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
890 tb_page_remove(&p->first_tb, tb);
891 invalidate_page_bitmap(p);
892 }
893
bellard8a40a182005-11-20 10:35:40 +0000894 tb_invalidated_flag = 1;
895
896 /* remove the TB from the hash list */
897 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000898 for(env = first_cpu; env != NULL; env = env->next_cpu) {
899 if (env->tb_jmp_cache[h] == tb)
900 env->tb_jmp_cache[h] = NULL;
901 }
bellard8a40a182005-11-20 10:35:40 +0000902
903 /* suppress this TB from the two jump lists */
904 tb_jmp_remove(tb, 0);
905 tb_jmp_remove(tb, 1);
906
907 /* suppress any remaining jumps to this TB */
908 tb1 = tb->jmp_first;
909 for(;;) {
910 n1 = (long)tb1 & 3;
911 if (n1 == 2)
912 break;
913 tb1 = (TranslationBlock *)((long)tb1 & ~3);
914 tb2 = tb1->jmp_next[n1];
915 tb_reset_jump(tb1, n1);
916 tb1->jmp_next[n1] = NULL;
917 tb1 = tb2;
918 }
919 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
920
bellarde3db7222005-01-26 22:00:47 +0000921 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000922}
923
924static inline void set_bits(uint8_t *tab, int start, int len)
925{
926 int end, mask, end1;
927
928 end = start + len;
929 tab += start >> 3;
930 mask = 0xff << (start & 7);
931 if ((start & ~7) == (end & ~7)) {
932 if (start < end) {
933 mask &= ~(0xff << (end & 7));
934 *tab |= mask;
935 }
936 } else {
937 *tab++ |= mask;
938 start = (start + 8) & ~7;
939 end1 = end & ~7;
940 while (start < end1) {
941 *tab++ = 0xff;
942 start += 8;
943 }
944 if (start < end) {
945 mask = ~(0xff << (end & 7));
946 *tab |= mask;
947 }
948 }
949}
950
951static void build_page_bitmap(PageDesc *p)
952{
953 int n, tb_start, tb_end;
954 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000955
Anthony Liguori7267c092011-08-20 22:09:37 -0500956 p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000957
958 tb = p->first_tb;
959 while (tb != NULL) {
960 n = (long)tb & 3;
961 tb = (TranslationBlock *)((long)tb & ~3);
962 /* NOTE: this is subtle as a TB may span two physical pages */
963 if (n == 0) {
964 /* NOTE: tb_end may be after the end of the page, but
965 it is not a problem */
966 tb_start = tb->pc & ~TARGET_PAGE_MASK;
967 tb_end = tb_start + tb->size;
968 if (tb_end > TARGET_PAGE_SIZE)
969 tb_end = TARGET_PAGE_SIZE;
970 } else {
971 tb_start = 0;
972 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
973 }
974 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
975 tb = tb->page_next[n];
976 }
977}
978
pbrook2e70f6e2008-06-29 01:03:05 +0000979TranslationBlock *tb_gen_code(CPUState *env,
980 target_ulong pc, target_ulong cs_base,
981 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000982{
983 TranslationBlock *tb;
984 uint8_t *tc_ptr;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000985 tb_page_addr_t phys_pc, phys_page2;
986 target_ulong virt_page2;
bellardd720b932004-04-25 17:57:43 +0000987 int code_gen_size;
988
Paul Brook41c1b1c2010-03-12 16:54:58 +0000989 phys_pc = get_page_addr_code(env, pc);
bellardc27004e2005-01-03 23:35:10 +0000990 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000991 if (!tb) {
992 /* flush must be done */
993 tb_flush(env);
994 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000995 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000996 /* Don't forget to invalidate previous TB info. */
997 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000998 }
999 tc_ptr = code_gen_ptr;
1000 tb->tc_ptr = tc_ptr;
1001 tb->cs_base = cs_base;
1002 tb->flags = flags;
1003 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +00001004 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +00001005 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 +00001006
bellardd720b932004-04-25 17:57:43 +00001007 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +00001008 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +00001009 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +00001010 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
Paul Brook41c1b1c2010-03-12 16:54:58 +00001011 phys_page2 = get_page_addr_code(env, virt_page2);
bellardd720b932004-04-25 17:57:43 +00001012 }
Paul Brook41c1b1c2010-03-12 16:54:58 +00001013 tb_link_page(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +00001014 return tb;
bellardd720b932004-04-25 17:57:43 +00001015}
ths3b46e622007-09-17 08:09:54 +00001016
bellard9fa3e852004-01-04 18:06:42 +00001017/* invalidate all TBs which intersect with the target physical page
1018 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +00001019 the same physical page. 'is_cpu_write_access' should be true if called
1020 from a real cpu write access: the virtual CPU will exit the current
1021 TB if code is modified inside this TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001022void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
bellardd720b932004-04-25 17:57:43 +00001023 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +00001024{
aliguori6b917542008-11-18 19:46:41 +00001025 TranslationBlock *tb, *tb_next, *saved_tb;
bellardd720b932004-04-25 17:57:43 +00001026 CPUState *env = cpu_single_env;
Paul Brook41c1b1c2010-03-12 16:54:58 +00001027 tb_page_addr_t tb_start, tb_end;
aliguori6b917542008-11-18 19:46:41 +00001028 PageDesc *p;
1029 int n;
1030#ifdef TARGET_HAS_PRECISE_SMC
1031 int current_tb_not_found = is_cpu_write_access;
1032 TranslationBlock *current_tb = NULL;
1033 int current_tb_modified = 0;
1034 target_ulong current_pc = 0;
1035 target_ulong current_cs_base = 0;
1036 int current_flags = 0;
1037#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001038
1039 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001040 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001041 return;
ths5fafdf22007-09-16 21:08:06 +00001042 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +00001043 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1044 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +00001045 /* build code bitmap */
1046 build_page_bitmap(p);
1047 }
1048
1049 /* we remove all the TBs in the range [start, end[ */
1050 /* XXX: see if in some cases it could be faster to invalidate all the code */
1051 tb = p->first_tb;
1052 while (tb != NULL) {
1053 n = (long)tb & 3;
1054 tb = (TranslationBlock *)((long)tb & ~3);
1055 tb_next = tb->page_next[n];
1056 /* NOTE: this is subtle as a TB may span two physical pages */
1057 if (n == 0) {
1058 /* NOTE: tb_end may be after the end of the page, but
1059 it is not a problem */
1060 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1061 tb_end = tb_start + tb->size;
1062 } else {
1063 tb_start = tb->page_addr[1];
1064 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1065 }
1066 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +00001067#ifdef TARGET_HAS_PRECISE_SMC
1068 if (current_tb_not_found) {
1069 current_tb_not_found = 0;
1070 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001071 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +00001072 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +00001073 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +00001074 }
1075 }
1076 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001077 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001078 /* If we are modifying the current TB, we must stop
1079 its execution. We could be more precise by checking
1080 that the modification is after the current PC, but it
1081 would require a specialized function to partially
1082 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001083
bellardd720b932004-04-25 17:57:43 +00001084 current_tb_modified = 1;
Stefan Weil618ba8e2011-04-18 06:39:53 +00001085 cpu_restore_state(current_tb, env, env->mem_io_pc);
aliguori6b917542008-11-18 19:46:41 +00001086 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1087 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001088 }
1089#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +00001090 /* we need to do that to handle the case where a signal
1091 occurs while doing tb_phys_invalidate() */
1092 saved_tb = NULL;
1093 if (env) {
1094 saved_tb = env->current_tb;
1095 env->current_tb = NULL;
1096 }
bellard9fa3e852004-01-04 18:06:42 +00001097 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +00001098 if (env) {
1099 env->current_tb = saved_tb;
1100 if (env->interrupt_request && env->current_tb)
1101 cpu_interrupt(env, env->interrupt_request);
1102 }
bellard9fa3e852004-01-04 18:06:42 +00001103 }
1104 tb = tb_next;
1105 }
1106#if !defined(CONFIG_USER_ONLY)
1107 /* if no code remaining, no need to continue to use slow writes */
1108 if (!p->first_tb) {
1109 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +00001110 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +00001111 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +00001112 }
1113 }
1114#endif
1115#ifdef TARGET_HAS_PRECISE_SMC
1116 if (current_tb_modified) {
1117 /* we generate a block containing just the instruction
1118 modifying the memory. It will ensure that it cannot modify
1119 itself */
bellardea1c1802004-06-14 18:56:36 +00001120 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001121 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001122 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001123 }
1124#endif
1125}
1126
1127/* len must be <= 8 and start must be a multiple of len */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001128static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +00001129{
1130 PageDesc *p;
1131 int offset, b;
bellard59817cc2004-02-16 22:01:13 +00001132#if 0
bellarda4193c82004-06-03 14:01:43 +00001133 if (1) {
aliguori93fcfe32009-01-15 22:34:14 +00001134 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1135 cpu_single_env->mem_io_vaddr, len,
1136 cpu_single_env->eip,
1137 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
bellard59817cc2004-02-16 22:01:13 +00001138 }
1139#endif
bellard9fa3e852004-01-04 18:06:42 +00001140 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001141 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001142 return;
1143 if (p->code_bitmap) {
1144 offset = start & ~TARGET_PAGE_MASK;
1145 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1146 if (b & ((1 << len) - 1))
1147 goto do_invalidate;
1148 } else {
1149 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001150 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001151 }
1152}
1153
bellard9fa3e852004-01-04 18:06:42 +00001154#if !defined(CONFIG_SOFTMMU)
Paul Brook41c1b1c2010-03-12 16:54:58 +00001155static void tb_invalidate_phys_page(tb_page_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001156 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001157{
aliguori6b917542008-11-18 19:46:41 +00001158 TranslationBlock *tb;
bellard9fa3e852004-01-04 18:06:42 +00001159 PageDesc *p;
aliguori6b917542008-11-18 19:46:41 +00001160 int n;
bellardd720b932004-04-25 17:57:43 +00001161#ifdef TARGET_HAS_PRECISE_SMC
aliguori6b917542008-11-18 19:46:41 +00001162 TranslationBlock *current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001163 CPUState *env = cpu_single_env;
aliguori6b917542008-11-18 19:46:41 +00001164 int current_tb_modified = 0;
1165 target_ulong current_pc = 0;
1166 target_ulong current_cs_base = 0;
1167 int current_flags = 0;
bellardd720b932004-04-25 17:57:43 +00001168#endif
bellard9fa3e852004-01-04 18:06:42 +00001169
1170 addr &= TARGET_PAGE_MASK;
1171 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001172 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001173 return;
1174 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001175#ifdef TARGET_HAS_PRECISE_SMC
1176 if (tb && pc != 0) {
1177 current_tb = tb_find_pc(pc);
1178 }
1179#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001180 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001181 n = (long)tb & 3;
1182 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001183#ifdef TARGET_HAS_PRECISE_SMC
1184 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001185 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001186 /* If we are modifying the current TB, we must stop
1187 its execution. We could be more precise by checking
1188 that the modification is after the current PC, but it
1189 would require a specialized function to partially
1190 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001191
bellardd720b932004-04-25 17:57:43 +00001192 current_tb_modified = 1;
Stefan Weil618ba8e2011-04-18 06:39:53 +00001193 cpu_restore_state(current_tb, env, pc);
aliguori6b917542008-11-18 19:46:41 +00001194 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1195 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001196 }
1197#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001198 tb_phys_invalidate(tb, addr);
1199 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001200 }
1201 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001202#ifdef TARGET_HAS_PRECISE_SMC
1203 if (current_tb_modified) {
1204 /* we generate a block containing just the instruction
1205 modifying the memory. It will ensure that it cannot modify
1206 itself */
bellardea1c1802004-06-14 18:56:36 +00001207 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001208 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001209 cpu_resume_from_signal(env, puc);
1210 }
1211#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001212}
bellard9fa3e852004-01-04 18:06:42 +00001213#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001214
1215/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001216static inline void tb_alloc_page(TranslationBlock *tb,
Paul Brook41c1b1c2010-03-12 16:54:58 +00001217 unsigned int n, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001218{
1219 PageDesc *p;
Juan Quintela4429ab42011-06-02 01:53:44 +00001220#ifndef CONFIG_USER_ONLY
1221 bool page_already_protected;
1222#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001223
bellard9fa3e852004-01-04 18:06:42 +00001224 tb->page_addr[n] = page_addr;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001225 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
bellard9fa3e852004-01-04 18:06:42 +00001226 tb->page_next[n] = p->first_tb;
Juan Quintela4429ab42011-06-02 01:53:44 +00001227#ifndef CONFIG_USER_ONLY
1228 page_already_protected = p->first_tb != NULL;
1229#endif
bellard9fa3e852004-01-04 18:06:42 +00001230 p->first_tb = (TranslationBlock *)((long)tb | n);
1231 invalidate_page_bitmap(p);
1232
bellard107db442004-06-22 18:48:46 +00001233#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001234
bellard9fa3e852004-01-04 18:06:42 +00001235#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001236 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001237 target_ulong addr;
1238 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001239 int prot;
1240
bellardfd6ce8f2003-05-14 19:00:11 +00001241 /* force the host page as non writable (writes will have a
1242 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001243 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001244 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001245 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1246 addr += TARGET_PAGE_SIZE) {
1247
1248 p2 = page_find (addr >> TARGET_PAGE_BITS);
1249 if (!p2)
1250 continue;
1251 prot |= p2->flags;
1252 p2->flags &= ~PAGE_WRITE;
pbrook53a59602006-03-25 19:31:22 +00001253 }
ths5fafdf22007-09-16 21:08:06 +00001254 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001255 (prot & PAGE_BITS) & ~PAGE_WRITE);
1256#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001257 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001258 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001259#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001260 }
bellard9fa3e852004-01-04 18:06:42 +00001261#else
1262 /* if some code is already present, then the pages are already
1263 protected. So we handle the case where only the first TB is
1264 allocated in a physical page */
Juan Quintela4429ab42011-06-02 01:53:44 +00001265 if (!page_already_protected) {
bellard6a00d602005-11-21 23:25:50 +00001266 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001267 }
1268#endif
bellardd720b932004-04-25 17:57:43 +00001269
1270#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001271}
1272
bellard9fa3e852004-01-04 18:06:42 +00001273/* add a new TB and link it to the physical page tables. phys_page2 is
1274 (-1) to indicate that only one page contains the TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001275void tb_link_page(TranslationBlock *tb,
1276 tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001277{
bellard9fa3e852004-01-04 18:06:42 +00001278 unsigned int h;
1279 TranslationBlock **ptb;
1280
pbrookc8a706f2008-06-02 16:16:42 +00001281 /* Grab the mmap lock to stop another thread invalidating this TB
1282 before we are done. */
1283 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001284 /* add in the physical hash table */
1285 h = tb_phys_hash_func(phys_pc);
1286 ptb = &tb_phys_hash[h];
1287 tb->phys_hash_next = *ptb;
1288 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001289
1290 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001291 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1292 if (phys_page2 != -1)
1293 tb_alloc_page(tb, 1, phys_page2);
1294 else
1295 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001296
bellardd4e81642003-05-25 16:46:15 +00001297 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1298 tb->jmp_next[0] = NULL;
1299 tb->jmp_next[1] = NULL;
1300
1301 /* init original jump addresses */
1302 if (tb->tb_next_offset[0] != 0xffff)
1303 tb_reset_jump(tb, 0);
1304 if (tb->tb_next_offset[1] != 0xffff)
1305 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001306
1307#ifdef DEBUG_TB_CHECK
1308 tb_page_check();
1309#endif
pbrookc8a706f2008-06-02 16:16:42 +00001310 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001311}
1312
bellarda513fe12003-05-27 23:29:48 +00001313/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1314 tb[1].tc_ptr. Return NULL if not found */
1315TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1316{
1317 int m_min, m_max, m;
1318 unsigned long v;
1319 TranslationBlock *tb;
1320
1321 if (nb_tbs <= 0)
1322 return NULL;
1323 if (tc_ptr < (unsigned long)code_gen_buffer ||
1324 tc_ptr >= (unsigned long)code_gen_ptr)
1325 return NULL;
1326 /* binary search (cf Knuth) */
1327 m_min = 0;
1328 m_max = nb_tbs - 1;
1329 while (m_min <= m_max) {
1330 m = (m_min + m_max) >> 1;
1331 tb = &tbs[m];
1332 v = (unsigned long)tb->tc_ptr;
1333 if (v == tc_ptr)
1334 return tb;
1335 else if (tc_ptr < v) {
1336 m_max = m - 1;
1337 } else {
1338 m_min = m + 1;
1339 }
ths5fafdf22007-09-16 21:08:06 +00001340 }
bellarda513fe12003-05-27 23:29:48 +00001341 return &tbs[m_max];
1342}
bellard75012672003-06-21 13:11:07 +00001343
bellardea041c02003-06-25 16:16:50 +00001344static void tb_reset_jump_recursive(TranslationBlock *tb);
1345
1346static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1347{
1348 TranslationBlock *tb1, *tb_next, **ptb;
1349 unsigned int n1;
1350
1351 tb1 = tb->jmp_next[n];
1352 if (tb1 != NULL) {
1353 /* find head of list */
1354 for(;;) {
1355 n1 = (long)tb1 & 3;
1356 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1357 if (n1 == 2)
1358 break;
1359 tb1 = tb1->jmp_next[n1];
1360 }
1361 /* we are now sure now that tb jumps to tb1 */
1362 tb_next = tb1;
1363
1364 /* remove tb from the jmp_first list */
1365 ptb = &tb_next->jmp_first;
1366 for(;;) {
1367 tb1 = *ptb;
1368 n1 = (long)tb1 & 3;
1369 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1370 if (n1 == n && tb1 == tb)
1371 break;
1372 ptb = &tb1->jmp_next[n1];
1373 }
1374 *ptb = tb->jmp_next[n];
1375 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001376
bellardea041c02003-06-25 16:16:50 +00001377 /* suppress the jump to next tb in generated code */
1378 tb_reset_jump(tb, n);
1379
bellard01243112004-01-04 15:48:17 +00001380 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001381 tb_reset_jump_recursive(tb_next);
1382 }
1383}
1384
1385static void tb_reset_jump_recursive(TranslationBlock *tb)
1386{
1387 tb_reset_jump_recursive2(tb, 0);
1388 tb_reset_jump_recursive2(tb, 1);
1389}
1390
bellard1fddef42005-04-17 19:16:13 +00001391#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +00001392#if defined(CONFIG_USER_ONLY)
1393static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1394{
1395 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1396}
1397#else
bellardd720b932004-04-25 17:57:43 +00001398static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1399{
Anthony Liguoric227f092009-10-01 16:12:16 -05001400 target_phys_addr_t addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00001401 target_ulong pd;
Anthony Liguoric227f092009-10-01 16:12:16 -05001402 ram_addr_t ram_addr;
pbrookc2f07f82006-04-08 17:14:56 +00001403 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001404
pbrookc2f07f82006-04-08 17:14:56 +00001405 addr = cpu_get_phys_page_debug(env, pc);
1406 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1407 if (!p) {
1408 pd = IO_MEM_UNASSIGNED;
1409 } else {
1410 pd = p->phys_offset;
1411 }
1412 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001413 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001414}
bellardc27004e2005-01-03 23:35:10 +00001415#endif
Paul Brook94df27f2010-02-28 23:47:45 +00001416#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +00001417
Paul Brookc527ee82010-03-01 03:31:14 +00001418#if defined(CONFIG_USER_ONLY)
1419void cpu_watchpoint_remove_all(CPUState *env, int mask)
1420
1421{
1422}
1423
1424int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1425 int flags, CPUWatchpoint **watchpoint)
1426{
1427 return -ENOSYS;
1428}
1429#else
pbrook6658ffb2007-03-16 23:58:11 +00001430/* Add a watchpoint. */
aliguoria1d1bb32008-11-18 20:07:32 +00001431int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1432 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +00001433{
aliguorib4051332008-11-18 20:14:20 +00001434 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +00001435 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001436
aliguorib4051332008-11-18 20:14:20 +00001437 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1438 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1439 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1440 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1441 return -EINVAL;
1442 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001443 wp = g_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +00001444
aliguoria1d1bb32008-11-18 20:07:32 +00001445 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +00001446 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +00001447 wp->flags = flags;
1448
aliguori2dc9f412008-11-18 20:56:59 +00001449 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001450 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001451 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001452 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001453 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001454
pbrook6658ffb2007-03-16 23:58:11 +00001455 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +00001456
1457 if (watchpoint)
1458 *watchpoint = wp;
1459 return 0;
pbrook6658ffb2007-03-16 23:58:11 +00001460}
1461
aliguoria1d1bb32008-11-18 20:07:32 +00001462/* Remove a specific watchpoint. */
1463int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1464 int flags)
pbrook6658ffb2007-03-16 23:58:11 +00001465{
aliguorib4051332008-11-18 20:14:20 +00001466 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +00001467 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001468
Blue Swirl72cf2d42009-09-12 07:36:22 +00001469 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001470 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +00001471 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +00001472 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +00001473 return 0;
1474 }
1475 }
aliguoria1d1bb32008-11-18 20:07:32 +00001476 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +00001477}
1478
aliguoria1d1bb32008-11-18 20:07:32 +00001479/* Remove a specific watchpoint by reference. */
1480void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1481{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001482 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +00001483
aliguoria1d1bb32008-11-18 20:07:32 +00001484 tlb_flush_page(env, watchpoint->vaddr);
1485
Anthony Liguori7267c092011-08-20 22:09:37 -05001486 g_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +00001487}
1488
aliguoria1d1bb32008-11-18 20:07:32 +00001489/* Remove all matching watchpoints. */
1490void cpu_watchpoint_remove_all(CPUState *env, int mask)
1491{
aliguoric0ce9982008-11-25 22:13:57 +00001492 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001493
Blue Swirl72cf2d42009-09-12 07:36:22 +00001494 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001495 if (wp->flags & mask)
1496 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +00001497 }
aliguoria1d1bb32008-11-18 20:07:32 +00001498}
Paul Brookc527ee82010-03-01 03:31:14 +00001499#endif
aliguoria1d1bb32008-11-18 20:07:32 +00001500
1501/* Add a breakpoint. */
1502int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1503 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001504{
bellard1fddef42005-04-17 19:16:13 +00001505#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001506 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +00001507
Anthony Liguori7267c092011-08-20 22:09:37 -05001508 bp = g_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +00001509
1510 bp->pc = pc;
1511 bp->flags = flags;
1512
aliguori2dc9f412008-11-18 20:56:59 +00001513 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001514 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001515 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001516 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001517 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001518
1519 breakpoint_invalidate(env, pc);
1520
1521 if (breakpoint)
1522 *breakpoint = bp;
1523 return 0;
1524#else
1525 return -ENOSYS;
1526#endif
1527}
1528
1529/* Remove a specific breakpoint. */
1530int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1531{
1532#if defined(TARGET_HAS_ICE)
1533 CPUBreakpoint *bp;
1534
Blue Swirl72cf2d42009-09-12 07:36:22 +00001535 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00001536 if (bp->pc == pc && bp->flags == flags) {
1537 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +00001538 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +00001539 }
bellard4c3a88a2003-07-26 12:06:08 +00001540 }
aliguoria1d1bb32008-11-18 20:07:32 +00001541 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +00001542#else
aliguoria1d1bb32008-11-18 20:07:32 +00001543 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +00001544#endif
1545}
1546
aliguoria1d1bb32008-11-18 20:07:32 +00001547/* Remove a specific breakpoint by reference. */
1548void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001549{
bellard1fddef42005-04-17 19:16:13 +00001550#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001551 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +00001552
aliguoria1d1bb32008-11-18 20:07:32 +00001553 breakpoint_invalidate(env, breakpoint->pc);
1554
Anthony Liguori7267c092011-08-20 22:09:37 -05001555 g_free(breakpoint);
aliguoria1d1bb32008-11-18 20:07:32 +00001556#endif
1557}
1558
1559/* Remove all matching breakpoints. */
1560void cpu_breakpoint_remove_all(CPUState *env, int mask)
1561{
1562#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001563 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001564
Blue Swirl72cf2d42009-09-12 07:36:22 +00001565 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001566 if (bp->flags & mask)
1567 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +00001568 }
bellard4c3a88a2003-07-26 12:06:08 +00001569#endif
1570}
1571
bellardc33a3462003-07-29 20:50:33 +00001572/* enable or disable single step mode. EXCP_DEBUG is returned by the
1573 CPU loop after each instruction */
1574void cpu_single_step(CPUState *env, int enabled)
1575{
bellard1fddef42005-04-17 19:16:13 +00001576#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001577 if (env->singlestep_enabled != enabled) {
1578 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +00001579 if (kvm_enabled())
1580 kvm_update_guest_debug(env, 0);
1581 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001582 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +00001583 /* XXX: only flush what is necessary */
1584 tb_flush(env);
1585 }
bellardc33a3462003-07-29 20:50:33 +00001586 }
1587#endif
1588}
1589
bellard34865132003-10-05 14:28:56 +00001590/* enable or disable low levels log */
1591void cpu_set_log(int log_flags)
1592{
1593 loglevel = log_flags;
1594 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001595 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001596 if (!logfile) {
1597 perror(logfilename);
1598 _exit(1);
1599 }
bellard9fa3e852004-01-04 18:06:42 +00001600#if !defined(CONFIG_SOFTMMU)
1601 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1602 {
blueswir1b55266b2008-09-20 08:07:15 +00001603 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001604 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1605 }
Stefan Weildaf767b2011-12-03 22:32:37 +01001606#elif defined(_WIN32)
1607 /* Win32 doesn't support line-buffering, so use unbuffered output. */
1608 setvbuf(logfile, NULL, _IONBF, 0);
1609#else
bellard34865132003-10-05 14:28:56 +00001610 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001611#endif
pbrooke735b912007-06-30 13:53:24 +00001612 log_append = 1;
1613 }
1614 if (!loglevel && logfile) {
1615 fclose(logfile);
1616 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001617 }
1618}
1619
1620void cpu_set_log_filename(const char *filename)
1621{
1622 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001623 if (logfile) {
1624 fclose(logfile);
1625 logfile = NULL;
1626 }
1627 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001628}
bellardc33a3462003-07-29 20:50:33 +00001629
aurel323098dba2009-03-07 21:28:24 +00001630static void cpu_unlink_tb(CPUState *env)
bellardea041c02003-06-25 16:16:50 +00001631{
pbrookd5975362008-06-07 20:50:51 +00001632 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1633 problem and hope the cpu will stop of its own accord. For userspace
1634 emulation this often isn't actually as bad as it sounds. Often
1635 signals are used primarily to interrupt blocking syscalls. */
aurel323098dba2009-03-07 21:28:24 +00001636 TranslationBlock *tb;
Anthony Liguoric227f092009-10-01 16:12:16 -05001637 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
aurel323098dba2009-03-07 21:28:24 +00001638
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001639 spin_lock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001640 tb = env->current_tb;
1641 /* if the cpu is currently executing code, we must unlink it and
1642 all the potentially executing TB */
Riku Voipiof76cfe52009-12-04 15:16:30 +02001643 if (tb) {
aurel323098dba2009-03-07 21:28:24 +00001644 env->current_tb = NULL;
1645 tb_reset_jump_recursive(tb);
aurel323098dba2009-03-07 21:28:24 +00001646 }
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001647 spin_unlock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001648}
1649
Jan Kiszka97ffbd82011-04-13 01:32:56 +02001650#ifndef CONFIG_USER_ONLY
aurel323098dba2009-03-07 21:28:24 +00001651/* mask must never be zero, except for A20 change call */
Jan Kiszkaec6959d2011-04-13 01:32:56 +02001652static void tcg_handle_interrupt(CPUState *env, int mask)
aurel323098dba2009-03-07 21:28:24 +00001653{
1654 int old_mask;
1655
1656 old_mask = env->interrupt_request;
1657 env->interrupt_request |= mask;
1658
aliguori8edac962009-04-24 18:03:45 +00001659 /*
1660 * If called from iothread context, wake the target cpu in
1661 * case its halted.
1662 */
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001663 if (!qemu_cpu_is_self(env)) {
aliguori8edac962009-04-24 18:03:45 +00001664 qemu_cpu_kick(env);
1665 return;
1666 }
aliguori8edac962009-04-24 18:03:45 +00001667
pbrook2e70f6e2008-06-29 01:03:05 +00001668 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001669 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001670 if (!can_do_io(env)
aurel32be214e62009-03-06 21:48:00 +00001671 && (mask & ~old_mask) != 0) {
pbrook2e70f6e2008-06-29 01:03:05 +00001672 cpu_abort(env, "Raised interrupt while not in I/O function");
1673 }
pbrook2e70f6e2008-06-29 01:03:05 +00001674 } else {
aurel323098dba2009-03-07 21:28:24 +00001675 cpu_unlink_tb(env);
bellardea041c02003-06-25 16:16:50 +00001676 }
1677}
1678
Jan Kiszkaec6959d2011-04-13 01:32:56 +02001679CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
1680
Jan Kiszka97ffbd82011-04-13 01:32:56 +02001681#else /* CONFIG_USER_ONLY */
1682
1683void cpu_interrupt(CPUState *env, int mask)
1684{
1685 env->interrupt_request |= mask;
1686 cpu_unlink_tb(env);
1687}
1688#endif /* CONFIG_USER_ONLY */
1689
bellardb54ad042004-05-20 13:42:52 +00001690void cpu_reset_interrupt(CPUState *env, int mask)
1691{
1692 env->interrupt_request &= ~mask;
1693}
1694
aurel323098dba2009-03-07 21:28:24 +00001695void cpu_exit(CPUState *env)
1696{
1697 env->exit_request = 1;
1698 cpu_unlink_tb(env);
1699}
1700
blueswir1c7cd6a32008-10-02 18:27:46 +00001701const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001702 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001703 "show generated host assembly code for each compiled TB" },
1704 { CPU_LOG_TB_IN_ASM, "in_asm",
1705 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001706 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001707 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001708 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001709 "show micro ops "
1710#ifdef TARGET_I386
1711 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001712#endif
blueswir1e01a1152008-03-14 17:37:11 +00001713 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001714 { CPU_LOG_INT, "int",
1715 "show interrupts/exceptions in short format" },
1716 { CPU_LOG_EXEC, "exec",
1717 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001718 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001719 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001720#ifdef TARGET_I386
1721 { CPU_LOG_PCALL, "pcall",
1722 "show protected mode far calls/returns/exceptions" },
aliguorieca1bdf2009-01-26 19:54:31 +00001723 { CPU_LOG_RESET, "cpu_reset",
1724 "show CPU state before CPU resets" },
bellardf193c792004-03-21 17:06:25 +00001725#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001726#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001727 { CPU_LOG_IOPORT, "ioport",
1728 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001729#endif
bellardf193c792004-03-21 17:06:25 +00001730 { 0, NULL, NULL },
1731};
1732
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001733#ifndef CONFIG_USER_ONLY
1734static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
1735 = QLIST_HEAD_INITIALIZER(memory_client_list);
1736
1737static void cpu_notify_set_memory(target_phys_addr_t start_addr,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001738 ram_addr_t size,
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03001739 ram_addr_t phys_offset,
1740 bool log_dirty)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001741{
1742 CPUPhysMemoryClient *client;
1743 QLIST_FOREACH(client, &memory_client_list, list) {
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03001744 client->set_memory(client, start_addr, size, phys_offset, log_dirty);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001745 }
1746}
1747
1748static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001749 target_phys_addr_t end)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001750{
1751 CPUPhysMemoryClient *client;
1752 QLIST_FOREACH(client, &memory_client_list, list) {
1753 int r = client->sync_dirty_bitmap(client, start, end);
1754 if (r < 0)
1755 return r;
1756 }
1757 return 0;
1758}
1759
1760static int cpu_notify_migration_log(int enable)
1761{
1762 CPUPhysMemoryClient *client;
1763 QLIST_FOREACH(client, &memory_client_list, list) {
1764 int r = client->migration_log(client, enable);
1765 if (r < 0)
1766 return r;
1767 }
1768 return 0;
1769}
1770
Alex Williamson2173a752011-05-03 12:36:58 -06001771struct last_map {
1772 target_phys_addr_t start_addr;
1773 ram_addr_t size;
1774 ram_addr_t phys_offset;
1775};
1776
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001777/* The l1_phys_map provides the upper P_L1_BITs of the guest physical
1778 * address. Each intermediate table provides the next L2_BITs of guest
1779 * physical address space. The number of levels vary based on host and
1780 * guest configuration, making it efficient to build the final guest
1781 * physical address by seeding the L1 offset and shifting and adding in
1782 * each L2 offset as we recurse through them. */
Alex Williamson2173a752011-05-03 12:36:58 -06001783static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
1784 void **lp, target_phys_addr_t addr,
1785 struct last_map *map)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001786{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001787 int i;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001788
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001789 if (*lp == NULL) {
1790 return;
1791 }
1792 if (level == 0) {
1793 PhysPageDesc *pd = *lp;
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001794 addr <<= L2_BITS + TARGET_PAGE_BITS;
Paul Brook7296aba2010-03-14 14:58:46 +00001795 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001796 if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
Alex Williamson2173a752011-05-03 12:36:58 -06001797 target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
1798
1799 if (map->size &&
1800 start_addr == map->start_addr + map->size &&
1801 pd[i].phys_offset == map->phys_offset + map->size) {
1802
1803 map->size += TARGET_PAGE_SIZE;
1804 continue;
1805 } else if (map->size) {
1806 client->set_memory(client, map->start_addr,
1807 map->size, map->phys_offset, false);
1808 }
1809
1810 map->start_addr = start_addr;
1811 map->size = TARGET_PAGE_SIZE;
1812 map->phys_offset = pd[i].phys_offset;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001813 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001814 }
1815 } else {
1816 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00001817 for (i = 0; i < L2_SIZE; ++i) {
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001818 phys_page_for_each_1(client, level - 1, pp + i,
Alex Williamson2173a752011-05-03 12:36:58 -06001819 (addr << L2_BITS) | i, map);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001820 }
1821 }
1822}
1823
1824static void phys_page_for_each(CPUPhysMemoryClient *client)
1825{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001826 int i;
Alex Williamson2173a752011-05-03 12:36:58 -06001827 struct last_map map = { };
1828
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001829 for (i = 0; i < P_L1_SIZE; ++i) {
1830 phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
Alex Williamson2173a752011-05-03 12:36:58 -06001831 l1_phys_map + i, i, &map);
1832 }
1833 if (map.size) {
1834 client->set_memory(client, map.start_addr, map.size, map.phys_offset,
1835 false);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001836 }
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001837}
1838
1839void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
1840{
1841 QLIST_INSERT_HEAD(&memory_client_list, client, list);
1842 phys_page_for_each(client);
1843}
1844
1845void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
1846{
1847 QLIST_REMOVE(client, list);
1848}
1849#endif
1850
bellardf193c792004-03-21 17:06:25 +00001851static int cmp1(const char *s1, int n, const char *s2)
1852{
1853 if (strlen(s2) != n)
1854 return 0;
1855 return memcmp(s1, s2, n) == 0;
1856}
ths3b46e622007-09-17 08:09:54 +00001857
bellardf193c792004-03-21 17:06:25 +00001858/* takes a comma separated list of log masks. Return 0 if error. */
1859int cpu_str_to_log_mask(const char *str)
1860{
blueswir1c7cd6a32008-10-02 18:27:46 +00001861 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001862 int mask;
1863 const char *p, *p1;
1864
1865 p = str;
1866 mask = 0;
1867 for(;;) {
1868 p1 = strchr(p, ',');
1869 if (!p1)
1870 p1 = p + strlen(p);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001871 if(cmp1(p,p1-p,"all")) {
1872 for(item = cpu_log_items; item->mask != 0; item++) {
1873 mask |= item->mask;
1874 }
1875 } else {
1876 for(item = cpu_log_items; item->mask != 0; item++) {
1877 if (cmp1(p, p1 - p, item->name))
1878 goto found;
1879 }
1880 return 0;
bellardf193c792004-03-21 17:06:25 +00001881 }
bellardf193c792004-03-21 17:06:25 +00001882 found:
1883 mask |= item->mask;
1884 if (*p1 != ',')
1885 break;
1886 p = p1 + 1;
1887 }
1888 return mask;
1889}
bellardea041c02003-06-25 16:16:50 +00001890
bellard75012672003-06-21 13:11:07 +00001891void cpu_abort(CPUState *env, const char *fmt, ...)
1892{
1893 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001894 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001895
1896 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001897 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001898 fprintf(stderr, "qemu: fatal: ");
1899 vfprintf(stderr, fmt, ap);
1900 fprintf(stderr, "\n");
1901#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001902 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1903#else
1904 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001905#endif
aliguori93fcfe32009-01-15 22:34:14 +00001906 if (qemu_log_enabled()) {
1907 qemu_log("qemu: fatal: ");
1908 qemu_log_vprintf(fmt, ap2);
1909 qemu_log("\n");
j_mayerf9373292007-09-29 12:18:20 +00001910#ifdef TARGET_I386
aliguori93fcfe32009-01-15 22:34:14 +00001911 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
j_mayerf9373292007-09-29 12:18:20 +00001912#else
aliguori93fcfe32009-01-15 22:34:14 +00001913 log_cpu_state(env, 0);
j_mayerf9373292007-09-29 12:18:20 +00001914#endif
aliguori31b1a7b2009-01-15 22:35:09 +00001915 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +00001916 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +00001917 }
pbrook493ae1f2007-11-23 16:53:59 +00001918 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001919 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +02001920#if defined(CONFIG_USER_ONLY)
1921 {
1922 struct sigaction act;
1923 sigfillset(&act.sa_mask);
1924 act.sa_handler = SIG_DFL;
1925 sigaction(SIGABRT, &act, NULL);
1926 }
1927#endif
bellard75012672003-06-21 13:11:07 +00001928 abort();
1929}
1930
thsc5be9f02007-02-28 20:20:53 +00001931CPUState *cpu_copy(CPUState *env)
1932{
ths01ba9812007-12-09 02:22:57 +00001933 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001934 CPUState *next_cpu = new_env->next_cpu;
1935 int cpu_index = new_env->cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001936#if defined(TARGET_HAS_ICE)
1937 CPUBreakpoint *bp;
1938 CPUWatchpoint *wp;
1939#endif
1940
thsc5be9f02007-02-28 20:20:53 +00001941 memcpy(new_env, env, sizeof(CPUState));
aliguori5a38f082009-01-15 20:16:51 +00001942
1943 /* Preserve chaining and index. */
thsc5be9f02007-02-28 20:20:53 +00001944 new_env->next_cpu = next_cpu;
1945 new_env->cpu_index = cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001946
1947 /* Clone all break/watchpoints.
1948 Note: Once we support ptrace with hw-debug register access, make sure
1949 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001950 QTAILQ_INIT(&env->breakpoints);
1951 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +00001952#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001953 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001954 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1955 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001956 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001957 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1958 wp->flags, NULL);
1959 }
1960#endif
1961
thsc5be9f02007-02-28 20:20:53 +00001962 return new_env;
1963}
1964
bellard01243112004-01-04 15:48:17 +00001965#if !defined(CONFIG_USER_ONLY)
1966
edgar_igl5c751e92008-05-06 08:44:21 +00001967static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1968{
1969 unsigned int i;
1970
1971 /* Discard jump cache entries for any tb which might potentially
1972 overlap the flushed page. */
1973 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1974 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001975 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001976
1977 i = tb_jmp_cache_hash_page(addr);
1978 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001979 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001980}
1981
Igor Kovalenko08738982009-07-12 02:15:40 +04001982static CPUTLBEntry s_cputlb_empty_entry = {
1983 .addr_read = -1,
1984 .addr_write = -1,
1985 .addr_code = -1,
1986 .addend = -1,
1987};
1988
bellardee8b7022004-02-03 23:35:10 +00001989/* NOTE: if flush_global is true, also flush global entries (not
1990 implemented yet) */
1991void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001992{
bellard33417e72003-08-10 21:47:01 +00001993 int i;
bellard01243112004-01-04 15:48:17 +00001994
bellard9fa3e852004-01-04 18:06:42 +00001995#if defined(DEBUG_TLB)
1996 printf("tlb_flush:\n");
1997#endif
bellard01243112004-01-04 15:48:17 +00001998 /* must reset current TB so that interrupts cannot modify the
1999 links while we are modifying them */
2000 env->current_tb = NULL;
2001
bellard33417e72003-08-10 21:47:01 +00002002 for(i = 0; i < CPU_TLB_SIZE; i++) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002003 int mmu_idx;
2004 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
Igor Kovalenko08738982009-07-12 02:15:40 +04002005 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002006 }
bellard33417e72003-08-10 21:47:01 +00002007 }
bellard9fa3e852004-01-04 18:06:42 +00002008
bellard8a40a182005-11-20 10:35:40 +00002009 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00002010
Paul Brookd4c430a2010-03-17 02:14:28 +00002011 env->tlb_flush_addr = -1;
2012 env->tlb_flush_mask = 0;
bellarde3db7222005-01-26 22:00:47 +00002013 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00002014}
2015
bellard274da6b2004-05-20 21:56:27 +00002016static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00002017{
ths5fafdf22007-09-16 21:08:06 +00002018 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00002019 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00002020 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00002021 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00002022 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00002023 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Igor Kovalenko08738982009-07-12 02:15:40 +04002024 *tlb_entry = s_cputlb_empty_entry;
bellard84b7b8e2005-11-28 21:19:04 +00002025 }
bellard61382a52003-10-27 21:22:23 +00002026}
2027
bellard2e126692004-04-25 21:28:44 +00002028void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00002029{
bellard8a40a182005-11-20 10:35:40 +00002030 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002031 int mmu_idx;
bellard01243112004-01-04 15:48:17 +00002032
bellard9fa3e852004-01-04 18:06:42 +00002033#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00002034 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00002035#endif
Paul Brookd4c430a2010-03-17 02:14:28 +00002036 /* Check if we need to flush due to large pages. */
2037 if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
2038#if defined(DEBUG_TLB)
2039 printf("tlb_flush_page: forced full flush ("
2040 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
2041 env->tlb_flush_addr, env->tlb_flush_mask);
2042#endif
2043 tlb_flush(env, 1);
2044 return;
2045 }
bellard01243112004-01-04 15:48:17 +00002046 /* must reset current TB so that interrupts cannot modify the
2047 links while we are modifying them */
2048 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00002049
bellard61382a52003-10-27 21:22:23 +00002050 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00002051 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002052 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2053 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
bellard01243112004-01-04 15:48:17 +00002054
edgar_igl5c751e92008-05-06 08:44:21 +00002055 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00002056}
2057
bellard9fa3e852004-01-04 18:06:42 +00002058/* update the TLBs so that writes to code in the virtual page 'addr'
2059 can be detected */
Anthony Liguoric227f092009-10-01 16:12:16 -05002060static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00002061{
ths5fafdf22007-09-16 21:08:06 +00002062 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00002063 ram_addr + TARGET_PAGE_SIZE,
2064 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00002065}
2066
bellard9fa3e852004-01-04 18:06:42 +00002067/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00002068 tested for self modifying code */
Anthony Liguoric227f092009-10-01 16:12:16 -05002069static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00002070 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00002071{
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002072 cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG);
bellard1ccde1c2004-02-06 19:46:14 +00002073}
2074
ths5fafdf22007-09-16 21:08:06 +00002075static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00002076 unsigned long start, unsigned long length)
2077{
2078 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00002079 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
2080 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00002081 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00002082 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00002083 }
2084 }
2085}
2086
pbrook5579c7f2009-04-11 14:47:08 +00002087/* Note: start and end must be within the same ram block. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002088void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00002089 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00002090{
2091 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00002092 unsigned long length, start1;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002093 int i;
bellard1ccde1c2004-02-06 19:46:14 +00002094
2095 start &= TARGET_PAGE_MASK;
2096 end = TARGET_PAGE_ALIGN(end);
2097
2098 length = end - start;
2099 if (length == 0)
2100 return;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002101 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00002102
bellard1ccde1c2004-02-06 19:46:14 +00002103 /* we modify the TLB cache so that the dirty bit will be set again
2104 when accessing the range */
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02002105 start1 = (unsigned long)qemu_safe_ram_ptr(start);
Stefan Weila57d23e2011-04-30 22:49:26 +02002106 /* Check that we don't span multiple blocks - this breaks the
pbrook5579c7f2009-04-11 14:47:08 +00002107 address comparisons below. */
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02002108 if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
pbrook5579c7f2009-04-11 14:47:08 +00002109 != (end - 1) - start) {
2110 abort();
2111 }
2112
bellard6a00d602005-11-21 23:25:50 +00002113 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002114 int mmu_idx;
2115 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2116 for(i = 0; i < CPU_TLB_SIZE; i++)
2117 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
2118 start1, length);
2119 }
bellard6a00d602005-11-21 23:25:50 +00002120 }
bellard1ccde1c2004-02-06 19:46:14 +00002121}
2122
aliguori74576192008-10-06 14:02:03 +00002123int cpu_physical_memory_set_dirty_tracking(int enable)
2124{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002125 int ret = 0;
aliguori74576192008-10-06 14:02:03 +00002126 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002127 ret = cpu_notify_migration_log(!!enable);
2128 return ret;
aliguori74576192008-10-06 14:02:03 +00002129}
2130
2131int cpu_physical_memory_get_dirty_tracking(void)
2132{
2133 return in_migration;
2134}
2135
Anthony Liguoric227f092009-10-01 16:12:16 -05002136int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
2137 target_phys_addr_t end_addr)
aliguori2bec46d2008-11-24 20:21:41 +00002138{
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +02002139 int ret;
Jan Kiszka151f7742009-05-01 20:52:47 +02002140
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002141 ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
Jan Kiszka151f7742009-05-01 20:52:47 +02002142 return ret;
aliguori2bec46d2008-11-24 20:21:41 +00002143}
2144
Anthony PERARDe5896b12011-02-07 12:19:23 +01002145int cpu_physical_log_start(target_phys_addr_t start_addr,
2146 ram_addr_t size)
2147{
2148 CPUPhysMemoryClient *client;
2149 QLIST_FOREACH(client, &memory_client_list, list) {
2150 if (client->log_start) {
2151 int r = client->log_start(client, start_addr, size);
2152 if (r < 0) {
2153 return r;
2154 }
2155 }
2156 }
2157 return 0;
2158}
2159
2160int cpu_physical_log_stop(target_phys_addr_t start_addr,
2161 ram_addr_t size)
2162{
2163 CPUPhysMemoryClient *client;
2164 QLIST_FOREACH(client, &memory_client_list, list) {
2165 if (client->log_stop) {
2166 int r = client->log_stop(client, start_addr, size);
2167 if (r < 0) {
2168 return r;
2169 }
2170 }
2171 }
2172 return 0;
2173}
2174
bellard3a7d9292005-08-21 09:26:42 +00002175static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
2176{
Anthony Liguoric227f092009-10-01 16:12:16 -05002177 ram_addr_t ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00002178 void *p;
bellard3a7d9292005-08-21 09:26:42 +00002179
bellard84b7b8e2005-11-28 21:19:04 +00002180 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
pbrook5579c7f2009-04-11 14:47:08 +00002181 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
2182 + tlb_entry->addend);
Marcelo Tosattie8902612010-10-11 15:31:19 -03002183 ram_addr = qemu_ram_addr_from_host_nofail(p);
bellard3a7d9292005-08-21 09:26:42 +00002184 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00002185 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00002186 }
2187 }
2188}
2189
2190/* update the TLB according to the current state of the dirty bits */
2191void cpu_tlb_update_dirty(CPUState *env)
2192{
2193 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002194 int mmu_idx;
2195 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2196 for(i = 0; i < CPU_TLB_SIZE; i++)
2197 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
2198 }
bellard3a7d9292005-08-21 09:26:42 +00002199}
2200
pbrook0f459d12008-06-09 00:20:13 +00002201static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002202{
pbrook0f459d12008-06-09 00:20:13 +00002203 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
2204 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00002205}
2206
pbrook0f459d12008-06-09 00:20:13 +00002207/* update the TLB corresponding to virtual page vaddr
2208 so that it is no longer dirty */
2209static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002210{
bellard1ccde1c2004-02-06 19:46:14 +00002211 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002212 int mmu_idx;
bellard1ccde1c2004-02-06 19:46:14 +00002213
pbrook0f459d12008-06-09 00:20:13 +00002214 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00002215 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002216 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2217 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
bellard9fa3e852004-01-04 18:06:42 +00002218}
2219
Paul Brookd4c430a2010-03-17 02:14:28 +00002220/* Our TLB does not support large pages, so remember the area covered by
2221 large pages and trigger a full TLB flush if these are invalidated. */
2222static void tlb_add_large_page(CPUState *env, target_ulong vaddr,
2223 target_ulong size)
2224{
2225 target_ulong mask = ~(size - 1);
2226
2227 if (env->tlb_flush_addr == (target_ulong)-1) {
2228 env->tlb_flush_addr = vaddr & mask;
2229 env->tlb_flush_mask = mask;
2230 return;
2231 }
2232 /* Extend the existing region to include the new page.
2233 This is a compromise between unnecessary flushes and the cost
2234 of maintaining a full variable size TLB. */
2235 mask &= env->tlb_flush_mask;
2236 while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
2237 mask <<= 1;
2238 }
2239 env->tlb_flush_addr &= mask;
2240 env->tlb_flush_mask = mask;
2241}
2242
2243/* Add a new TLB entry. At most one entry for a given virtual address
2244 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
2245 supplied size is only used by tlb_flush_page. */
2246void tlb_set_page(CPUState *env, target_ulong vaddr,
2247 target_phys_addr_t paddr, int prot,
2248 int mmu_idx, target_ulong size)
bellard9fa3e852004-01-04 18:06:42 +00002249{
bellard92e873b2004-05-21 14:52:29 +00002250 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00002251 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00002252 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00002253 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00002254 target_ulong code_address;
Paul Brook355b1942010-04-05 00:28:53 +01002255 unsigned long addend;
bellard84b7b8e2005-11-28 21:19:04 +00002256 CPUTLBEntry *te;
aliguoria1d1bb32008-11-18 20:07:32 +00002257 CPUWatchpoint *wp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002258 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00002259
Paul Brookd4c430a2010-03-17 02:14:28 +00002260 assert(size >= TARGET_PAGE_SIZE);
2261 if (size != TARGET_PAGE_SIZE) {
2262 tlb_add_large_page(env, vaddr, size);
2263 }
bellard92e873b2004-05-21 14:52:29 +00002264 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00002265 if (!p) {
2266 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00002267 } else {
2268 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00002269 }
2270#if defined(DEBUG_TLB)
Stefan Weil7fd3f492010-09-30 22:39:51 +02002271 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
2272 " prot=%x idx=%d pd=0x%08lx\n",
2273 vaddr, paddr, prot, mmu_idx, pd);
bellard9fa3e852004-01-04 18:06:42 +00002274#endif
2275
pbrook0f459d12008-06-09 00:20:13 +00002276 address = vaddr;
2277 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2278 /* IO memory case (romd handled later) */
2279 address |= TLB_MMIO;
2280 }
pbrook5579c7f2009-04-11 14:47:08 +00002281 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
pbrook0f459d12008-06-09 00:20:13 +00002282 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2283 /* Normal RAM. */
2284 iotlb = pd & TARGET_PAGE_MASK;
2285 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2286 iotlb |= IO_MEM_NOTDIRTY;
2287 else
2288 iotlb |= IO_MEM_ROM;
2289 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002290 /* IO handlers are currently passed a physical address.
pbrook0f459d12008-06-09 00:20:13 +00002291 It would be nice to pass an offset from the base address
2292 of that region. This would avoid having to special case RAM,
2293 and avoid full address decoding in every device.
2294 We can't use the high bits of pd for this because
2295 IO_MEM_ROMD uses these as a ram address. */
pbrook8da3ff12008-12-01 18:59:50 +00002296 iotlb = (pd & ~TARGET_PAGE_MASK);
2297 if (p) {
pbrook8da3ff12008-12-01 18:59:50 +00002298 iotlb += p->region_offset;
2299 } else {
2300 iotlb += paddr;
2301 }
pbrook0f459d12008-06-09 00:20:13 +00002302 }
pbrook6658ffb2007-03-16 23:58:11 +00002303
pbrook0f459d12008-06-09 00:20:13 +00002304 code_address = address;
2305 /* Make accesses to pages with watchpoints go via the
2306 watchpoint trap routines. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002307 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00002308 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
Jun Koibf298f82010-05-06 14:36:59 +09002309 /* Avoid trapping reads of pages with a write breakpoint. */
2310 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
2311 iotlb = io_mem_watch + paddr;
2312 address |= TLB_MMIO;
2313 break;
2314 }
pbrook6658ffb2007-03-16 23:58:11 +00002315 }
pbrook0f459d12008-06-09 00:20:13 +00002316 }
balrogd79acba2007-06-26 20:01:13 +00002317
pbrook0f459d12008-06-09 00:20:13 +00002318 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2319 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2320 te = &env->tlb_table[mmu_idx][index];
2321 te->addend = addend - vaddr;
2322 if (prot & PAGE_READ) {
2323 te->addr_read = address;
2324 } else {
2325 te->addr_read = -1;
2326 }
edgar_igl5c751e92008-05-06 08:44:21 +00002327
pbrook0f459d12008-06-09 00:20:13 +00002328 if (prot & PAGE_EXEC) {
2329 te->addr_code = code_address;
2330 } else {
2331 te->addr_code = -1;
2332 }
2333 if (prot & PAGE_WRITE) {
2334 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2335 (pd & IO_MEM_ROMD)) {
2336 /* Write access calls the I/O callback. */
2337 te->addr_write = address | TLB_MMIO;
2338 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2339 !cpu_physical_memory_is_dirty(pd)) {
2340 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00002341 } else {
pbrook0f459d12008-06-09 00:20:13 +00002342 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00002343 }
pbrook0f459d12008-06-09 00:20:13 +00002344 } else {
2345 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00002346 }
bellard9fa3e852004-01-04 18:06:42 +00002347}
2348
bellard01243112004-01-04 15:48:17 +00002349#else
2350
bellardee8b7022004-02-03 23:35:10 +00002351void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00002352{
2353}
2354
bellard2e126692004-04-25 21:28:44 +00002355void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00002356{
2357}
2358
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002359/*
2360 * Walks guest process memory "regions" one by one
2361 * and calls callback function 'fn' for each region.
2362 */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002363
2364struct walk_memory_regions_data
bellard9fa3e852004-01-04 18:06:42 +00002365{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002366 walk_memory_regions_fn fn;
2367 void *priv;
2368 unsigned long start;
2369 int prot;
2370};
bellard9fa3e852004-01-04 18:06:42 +00002371
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002372static int walk_memory_regions_end(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002373 abi_ulong end, int new_prot)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002374{
2375 if (data->start != -1ul) {
2376 int rc = data->fn(data->priv, data->start, end, data->prot);
2377 if (rc != 0) {
2378 return rc;
bellard9fa3e852004-01-04 18:06:42 +00002379 }
bellard33417e72003-08-10 21:47:01 +00002380 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002381
2382 data->start = (new_prot ? end : -1ul);
2383 data->prot = new_prot;
2384
2385 return 0;
2386}
2387
2388static int walk_memory_regions_1(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002389 abi_ulong base, int level, void **lp)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002390{
Paul Brookb480d9b2010-03-12 23:23:29 +00002391 abi_ulong pa;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002392 int i, rc;
2393
2394 if (*lp == NULL) {
2395 return walk_memory_regions_end(data, base, 0);
2396 }
2397
2398 if (level == 0) {
2399 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002400 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002401 int prot = pd[i].flags;
2402
2403 pa = base | (i << TARGET_PAGE_BITS);
2404 if (prot != data->prot) {
2405 rc = walk_memory_regions_end(data, pa, prot);
2406 if (rc != 0) {
2407 return rc;
2408 }
2409 }
2410 }
2411 } else {
2412 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002413 for (i = 0; i < L2_SIZE; ++i) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002414 pa = base | ((abi_ulong)i <<
2415 (TARGET_PAGE_BITS + L2_BITS * level));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002416 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2417 if (rc != 0) {
2418 return rc;
2419 }
2420 }
2421 }
2422
2423 return 0;
2424}
2425
2426int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2427{
2428 struct walk_memory_regions_data data;
2429 unsigned long i;
2430
2431 data.fn = fn;
2432 data.priv = priv;
2433 data.start = -1ul;
2434 data.prot = 0;
2435
2436 for (i = 0; i < V_L1_SIZE; i++) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002437 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002438 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
2439 if (rc != 0) {
2440 return rc;
2441 }
2442 }
2443
2444 return walk_memory_regions_end(&data, 0, 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002445}
2446
Paul Brookb480d9b2010-03-12 23:23:29 +00002447static int dump_region(void *priv, abi_ulong start,
2448 abi_ulong end, unsigned long prot)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002449{
2450 FILE *f = (FILE *)priv;
2451
Paul Brookb480d9b2010-03-12 23:23:29 +00002452 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
2453 " "TARGET_ABI_FMT_lx" %c%c%c\n",
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002454 start, end, end - start,
2455 ((prot & PAGE_READ) ? 'r' : '-'),
2456 ((prot & PAGE_WRITE) ? 'w' : '-'),
2457 ((prot & PAGE_EXEC) ? 'x' : '-'));
2458
2459 return (0);
2460}
2461
2462/* dump memory mappings */
2463void page_dump(FILE *f)
2464{
2465 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2466 "start", "end", "size", "prot");
2467 walk_memory_regions(f, dump_region);
bellard33417e72003-08-10 21:47:01 +00002468}
2469
pbrook53a59602006-03-25 19:31:22 +00002470int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002471{
bellard9fa3e852004-01-04 18:06:42 +00002472 PageDesc *p;
2473
2474 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002475 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002476 return 0;
2477 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002478}
2479
Richard Henderson376a7902010-03-10 15:57:04 -08002480/* Modify the flags of a page and invalidate the code if necessary.
2481 The flag PAGE_WRITE_ORG is positioned automatically depending
2482 on PAGE_WRITE. The mmap_lock should already be held. */
pbrook53a59602006-03-25 19:31:22 +00002483void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002484{
Richard Henderson376a7902010-03-10 15:57:04 -08002485 target_ulong addr, len;
bellard9fa3e852004-01-04 18:06:42 +00002486
Richard Henderson376a7902010-03-10 15:57:04 -08002487 /* This function should never be called with addresses outside the
2488 guest address space. If this assert fires, it probably indicates
2489 a missing call to h2g_valid. */
Paul Brookb480d9b2010-03-12 23:23:29 +00002490#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2491 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002492#endif
2493 assert(start < end);
2494
bellard9fa3e852004-01-04 18:06:42 +00002495 start = start & TARGET_PAGE_MASK;
2496 end = TARGET_PAGE_ALIGN(end);
Richard Henderson376a7902010-03-10 15:57:04 -08002497
2498 if (flags & PAGE_WRITE) {
bellard9fa3e852004-01-04 18:06:42 +00002499 flags |= PAGE_WRITE_ORG;
Richard Henderson376a7902010-03-10 15:57:04 -08002500 }
2501
2502 for (addr = start, len = end - start;
2503 len != 0;
2504 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2505 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2506
2507 /* If the write protection bit is set, then we invalidate
2508 the code inside. */
ths5fafdf22007-09-16 21:08:06 +00002509 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002510 (flags & PAGE_WRITE) &&
2511 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002512 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002513 }
2514 p->flags = flags;
2515 }
bellard9fa3e852004-01-04 18:06:42 +00002516}
2517
ths3d97b402007-11-02 19:02:07 +00002518int page_check_range(target_ulong start, target_ulong len, int flags)
2519{
2520 PageDesc *p;
2521 target_ulong end;
2522 target_ulong addr;
2523
Richard Henderson376a7902010-03-10 15:57:04 -08002524 /* This function should never be called with addresses outside the
2525 guest address space. If this assert fires, it probably indicates
2526 a missing call to h2g_valid. */
Blue Swirl338e9e62010-03-13 09:48:08 +00002527#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2528 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002529#endif
2530
Richard Henderson3e0650a2010-03-29 10:54:42 -07002531 if (len == 0) {
2532 return 0;
2533 }
Richard Henderson376a7902010-03-10 15:57:04 -08002534 if (start + len - 1 < start) {
2535 /* We've wrapped around. */
balrog55f280c2008-10-28 10:24:11 +00002536 return -1;
Richard Henderson376a7902010-03-10 15:57:04 -08002537 }
balrog55f280c2008-10-28 10:24:11 +00002538
ths3d97b402007-11-02 19:02:07 +00002539 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2540 start = start & TARGET_PAGE_MASK;
2541
Richard Henderson376a7902010-03-10 15:57:04 -08002542 for (addr = start, len = end - start;
2543 len != 0;
2544 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
ths3d97b402007-11-02 19:02:07 +00002545 p = page_find(addr >> TARGET_PAGE_BITS);
2546 if( !p )
2547 return -1;
2548 if( !(p->flags & PAGE_VALID) )
2549 return -1;
2550
bellarddae32702007-11-14 10:51:00 +00002551 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002552 return -1;
bellarddae32702007-11-14 10:51:00 +00002553 if (flags & PAGE_WRITE) {
2554 if (!(p->flags & PAGE_WRITE_ORG))
2555 return -1;
2556 /* unprotect the page if it was put read-only because it
2557 contains translated code */
2558 if (!(p->flags & PAGE_WRITE)) {
2559 if (!page_unprotect(addr, 0, NULL))
2560 return -1;
2561 }
2562 return 0;
2563 }
ths3d97b402007-11-02 19:02:07 +00002564 }
2565 return 0;
2566}
2567
bellard9fa3e852004-01-04 18:06:42 +00002568/* called from signal handler: invalidate the code and unprotect the
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002569 page. Return TRUE if the fault was successfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002570int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002571{
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002572 unsigned int prot;
2573 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002574 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002575
pbrookc8a706f2008-06-02 16:16:42 +00002576 /* Technically this isn't safe inside a signal handler. However we
2577 know this only ever happens in a synchronous SEGV handler, so in
2578 practice it seems to be ok. */
2579 mmap_lock();
2580
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002581 p = page_find(address >> TARGET_PAGE_BITS);
2582 if (!p) {
pbrookc8a706f2008-06-02 16:16:42 +00002583 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002584 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002585 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002586
bellard9fa3e852004-01-04 18:06:42 +00002587 /* if the page was really writable, then we change its
2588 protection back to writable */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002589 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2590 host_start = address & qemu_host_page_mask;
2591 host_end = host_start + qemu_host_page_size;
2592
2593 prot = 0;
2594 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2595 p = page_find(addr >> TARGET_PAGE_BITS);
2596 p->flags |= PAGE_WRITE;
2597 prot |= p->flags;
2598
bellard9fa3e852004-01-04 18:06:42 +00002599 /* and since the content will be modified, we must invalidate
2600 the corresponding translated code. */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002601 tb_invalidate_phys_page(addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002602#ifdef DEBUG_TB_CHECK
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002603 tb_invalidate_check(addr);
bellard9fa3e852004-01-04 18:06:42 +00002604#endif
bellard9fa3e852004-01-04 18:06:42 +00002605 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002606 mprotect((void *)g2h(host_start), qemu_host_page_size,
2607 prot & PAGE_BITS);
2608
2609 mmap_unlock();
2610 return 1;
bellard9fa3e852004-01-04 18:06:42 +00002611 }
pbrookc8a706f2008-06-02 16:16:42 +00002612 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002613 return 0;
2614}
2615
bellard6a00d602005-11-21 23:25:50 +00002616static inline void tlb_set_dirty(CPUState *env,
2617 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002618{
2619}
bellard9fa3e852004-01-04 18:06:42 +00002620#endif /* defined(CONFIG_USER_ONLY) */
2621
pbrooke2eef172008-06-08 01:09:01 +00002622#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +00002623
Paul Brookc04b2b72010-03-01 03:31:14 +00002624#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2625typedef struct subpage_t {
2626 target_phys_addr_t base;
Richard Hendersonf6405242010-04-22 16:47:31 -07002627 ram_addr_t sub_io_index[TARGET_PAGE_SIZE];
2628 ram_addr_t region_offset[TARGET_PAGE_SIZE];
Paul Brookc04b2b72010-03-01 03:31:14 +00002629} subpage_t;
2630
Anthony Liguoric227f092009-10-01 16:12:16 -05002631static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2632 ram_addr_t memory, ram_addr_t region_offset);
Richard Hendersonf6405242010-04-22 16:47:31 -07002633static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2634 ram_addr_t orig_memory,
2635 ram_addr_t region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002636#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2637 need_subpage) \
2638 do { \
2639 if (addr > start_addr) \
2640 start_addr2 = 0; \
2641 else { \
2642 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2643 if (start_addr2 > 0) \
2644 need_subpage = 1; \
2645 } \
2646 \
blueswir149e9fba2007-05-30 17:25:06 +00002647 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002648 end_addr2 = TARGET_PAGE_SIZE - 1; \
2649 else { \
2650 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2651 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2652 need_subpage = 1; \
2653 } \
2654 } while (0)
2655
Michael S. Tsirkin8f2498f2009-09-29 18:53:16 +02002656/* register physical memory.
2657 For RAM, 'size' must be a multiple of the target page size.
2658 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
pbrook8da3ff12008-12-01 18:59:50 +00002659 io memory page. The address used when calling the IO function is
2660 the offset from the start of the region, plus region_offset. Both
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002661 start_addr and region_offset are rounded down to a page boundary
pbrook8da3ff12008-12-01 18:59:50 +00002662 before calculating this offset. This should not be a problem unless
2663 the low bits of start_addr and region_offset differ. */
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002664void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
Anthony Liguoric227f092009-10-01 16:12:16 -05002665 ram_addr_t size,
2666 ram_addr_t phys_offset,
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002667 ram_addr_t region_offset,
2668 bool log_dirty)
bellard33417e72003-08-10 21:47:01 +00002669{
Anthony Liguoric227f092009-10-01 16:12:16 -05002670 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002671 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002672 CPUState *env;
Anthony Liguoric227f092009-10-01 16:12:16 -05002673 ram_addr_t orig_size = size;
Richard Hendersonf6405242010-04-22 16:47:31 -07002674 subpage_t *subpage;
bellard33417e72003-08-10 21:47:01 +00002675
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002676 assert(size);
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002677 cpu_notify_set_memory(start_addr, size, phys_offset, log_dirty);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002678
pbrook67c4d232009-02-23 13:16:07 +00002679 if (phys_offset == IO_MEM_UNASSIGNED) {
2680 region_offset = start_addr;
2681 }
pbrook8da3ff12008-12-01 18:59:50 +00002682 region_offset &= TARGET_PAGE_MASK;
bellard5fd386f2004-05-23 21:11:22 +00002683 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
Anthony Liguoric227f092009-10-01 16:12:16 -05002684 end_addr = start_addr + (target_phys_addr_t)size;
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002685
2686 addr = start_addr;
2687 do {
blueswir1db7b5422007-05-26 17:36:03 +00002688 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2689 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002690 ram_addr_t orig_memory = p->phys_offset;
2691 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002692 int need_subpage = 0;
2693
2694 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2695 need_subpage);
Richard Hendersonf6405242010-04-22 16:47:31 -07002696 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002697 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2698 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002699 &p->phys_offset, orig_memory,
2700 p->region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002701 } else {
2702 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2703 >> IO_MEM_SHIFT];
2704 }
pbrook8da3ff12008-12-01 18:59:50 +00002705 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2706 region_offset);
2707 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002708 } else {
2709 p->phys_offset = phys_offset;
2710 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2711 (phys_offset & IO_MEM_ROMD))
2712 phys_offset += TARGET_PAGE_SIZE;
2713 }
2714 } else {
2715 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2716 p->phys_offset = phys_offset;
pbrook8da3ff12008-12-01 18:59:50 +00002717 p->region_offset = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002718 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
pbrook8da3ff12008-12-01 18:59:50 +00002719 (phys_offset & IO_MEM_ROMD)) {
blueswir1db7b5422007-05-26 17:36:03 +00002720 phys_offset += TARGET_PAGE_SIZE;
pbrook0e8f0962008-12-02 09:02:15 +00002721 } else {
Anthony Liguoric227f092009-10-01 16:12:16 -05002722 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002723 int need_subpage = 0;
2724
2725 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2726 end_addr2, need_subpage);
2727
Richard Hendersonf6405242010-04-22 16:47:31 -07002728 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002729 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002730 &p->phys_offset, IO_MEM_UNASSIGNED,
pbrook67c4d232009-02-23 13:16:07 +00002731 addr & TARGET_PAGE_MASK);
blueswir1db7b5422007-05-26 17:36:03 +00002732 subpage_register(subpage, start_addr2, end_addr2,
pbrook8da3ff12008-12-01 18:59:50 +00002733 phys_offset, region_offset);
2734 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002735 }
2736 }
2737 }
pbrook8da3ff12008-12-01 18:59:50 +00002738 region_offset += TARGET_PAGE_SIZE;
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002739 addr += TARGET_PAGE_SIZE;
2740 } while (addr != end_addr);
ths3b46e622007-09-17 08:09:54 +00002741
bellard9d420372006-06-25 22:25:22 +00002742 /* since each CPU stores ram addresses in its TLB cache, we must
2743 reset the modified entries */
2744 /* XXX: slow ! */
2745 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2746 tlb_flush(env, 1);
2747 }
bellard33417e72003-08-10 21:47:01 +00002748}
2749
bellardba863452006-09-24 18:41:10 +00002750/* XXX: temporary until new memory mapping API */
Anthony Liguoric227f092009-10-01 16:12:16 -05002751ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002752{
2753 PhysPageDesc *p;
2754
2755 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2756 if (!p)
2757 return IO_MEM_UNASSIGNED;
2758 return p->phys_offset;
2759}
2760
Anthony Liguoric227f092009-10-01 16:12:16 -05002761void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002762{
2763 if (kvm_enabled())
2764 kvm_coalesce_mmio_region(addr, size);
2765}
2766
Anthony Liguoric227f092009-10-01 16:12:16 -05002767void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002768{
2769 if (kvm_enabled())
2770 kvm_uncoalesce_mmio_region(addr, size);
2771}
2772
Sheng Yang62a27442010-01-26 19:21:16 +08002773void qemu_flush_coalesced_mmio_buffer(void)
2774{
2775 if (kvm_enabled())
2776 kvm_flush_coalesced_mmio_buffer();
2777}
2778
Marcelo Tosattic9027602010-03-01 20:25:08 -03002779#if defined(__linux__) && !defined(TARGET_S390X)
2780
2781#include <sys/vfs.h>
2782
2783#define HUGETLBFS_MAGIC 0x958458f6
2784
2785static long gethugepagesize(const char *path)
2786{
2787 struct statfs fs;
2788 int ret;
2789
2790 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002791 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002792 } while (ret != 0 && errno == EINTR);
2793
2794 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002795 perror(path);
2796 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002797 }
2798
2799 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002800 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002801
2802 return fs.f_bsize;
2803}
2804
Alex Williamson04b16652010-07-02 11:13:17 -06002805static void *file_ram_alloc(RAMBlock *block,
2806 ram_addr_t memory,
2807 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -03002808{
2809 char *filename;
2810 void *area;
2811 int fd;
2812#ifdef MAP_POPULATE
2813 int flags;
2814#endif
2815 unsigned long hpagesize;
2816
2817 hpagesize = gethugepagesize(path);
2818 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002819 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002820 }
2821
2822 if (memory < hpagesize) {
2823 return NULL;
2824 }
2825
2826 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2827 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2828 return NULL;
2829 }
2830
2831 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002832 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002833 }
2834
2835 fd = mkstemp(filename);
2836 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002837 perror("unable to create backing store for hugepages");
2838 free(filename);
2839 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002840 }
2841 unlink(filename);
2842 free(filename);
2843
2844 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2845
2846 /*
2847 * ftruncate is not supported by hugetlbfs in older
2848 * hosts, so don't bother bailing out on errors.
2849 * If anything goes wrong with it under other filesystems,
2850 * mmap will fail.
2851 */
2852 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002853 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -03002854
2855#ifdef MAP_POPULATE
2856 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2857 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2858 * to sidestep this quirk.
2859 */
2860 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2861 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2862#else
2863 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2864#endif
2865 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002866 perror("file_ram_alloc: can't mmap RAM pages");
2867 close(fd);
2868 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002869 }
Alex Williamson04b16652010-07-02 11:13:17 -06002870 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002871 return area;
2872}
2873#endif
2874
Alex Williamsond17b5282010-06-25 11:08:38 -06002875static ram_addr_t find_ram_offset(ram_addr_t size)
2876{
Alex Williamson04b16652010-07-02 11:13:17 -06002877 RAMBlock *block, *next_block;
Alex Williamson3e837b22011-10-31 08:54:09 -06002878 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06002879
2880 if (QLIST_EMPTY(&ram_list.blocks))
2881 return 0;
2882
2883 QLIST_FOREACH(block, &ram_list.blocks, next) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00002884 ram_addr_t end, next = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06002885
2886 end = block->offset + block->length;
2887
2888 QLIST_FOREACH(next_block, &ram_list.blocks, next) {
2889 if (next_block->offset >= end) {
2890 next = MIN(next, next_block->offset);
2891 }
2892 }
2893 if (next - end >= size && next - end < mingap) {
Alex Williamson3e837b22011-10-31 08:54:09 -06002894 offset = end;
Alex Williamson04b16652010-07-02 11:13:17 -06002895 mingap = next - end;
2896 }
2897 }
Alex Williamson3e837b22011-10-31 08:54:09 -06002898
2899 if (offset == RAM_ADDR_MAX) {
2900 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
2901 (uint64_t)size);
2902 abort();
2903 }
2904
Alex Williamson04b16652010-07-02 11:13:17 -06002905 return offset;
2906}
2907
2908static ram_addr_t last_ram_offset(void)
2909{
Alex Williamsond17b5282010-06-25 11:08:38 -06002910 RAMBlock *block;
2911 ram_addr_t last = 0;
2912
2913 QLIST_FOREACH(block, &ram_list.blocks, next)
2914 last = MAX(last, block->offset + block->length);
2915
2916 return last;
2917}
2918
Cam Macdonell84b89d72010-07-26 18:10:57 -06002919ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002920 ram_addr_t size, void *host)
Cam Macdonell84b89d72010-07-26 18:10:57 -06002921{
2922 RAMBlock *new_block, *block;
2923
2924 size = TARGET_PAGE_ALIGN(size);
Anthony Liguori7267c092011-08-20 22:09:37 -05002925 new_block = g_malloc0(sizeof(*new_block));
Cam Macdonell84b89d72010-07-26 18:10:57 -06002926
2927 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
2928 char *id = dev->parent_bus->info->get_dev_path(dev);
2929 if (id) {
2930 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
Anthony Liguori7267c092011-08-20 22:09:37 -05002931 g_free(id);
Cam Macdonell84b89d72010-07-26 18:10:57 -06002932 }
2933 }
2934 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
2935
2936 QLIST_FOREACH(block, &ram_list.blocks, next) {
2937 if (!strcmp(block->idstr, new_block->idstr)) {
2938 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
2939 new_block->idstr);
2940 abort();
2941 }
2942 }
2943
Jun Nakajima432d2682010-08-31 16:41:25 +01002944 new_block->offset = find_ram_offset(size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002945 if (host) {
2946 new_block->host = host;
Huang Yingcd19cfa2011-03-02 08:56:19 +01002947 new_block->flags |= RAM_PREALLOC_MASK;
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002948 } else {
2949 if (mem_path) {
2950#if defined (__linux__) && !defined(TARGET_S390X)
2951 new_block->host = file_ram_alloc(new_block, size, mem_path);
2952 if (!new_block->host) {
2953 new_block->host = qemu_vmalloc(size);
Andreas Färbere78815a2010-09-25 11:26:05 +00002954 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002955 }
2956#else
2957 fprintf(stderr, "-mem-path option unsupported\n");
2958 exit(1);
2959#endif
2960 } else {
2961#if defined(TARGET_S390X) && defined(CONFIG_KVM)
Christian Borntraegerff836782011-05-10 14:49:10 +02002962 /* S390 KVM requires the topmost vma of the RAM to be smaller than
2963 an system defined value, which is at least 256GB. Larger systems
2964 have larger values. We put the guest between the end of data
2965 segment (system break) and this value. We use 32GB as a base to
2966 have enough room for the system break to grow. */
2967 new_block->host = mmap((void*)0x800000000, size,
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002968 PROT_EXEC|PROT_READ|PROT_WRITE,
Christian Borntraegerff836782011-05-10 14:49:10 +02002969 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
Alexander Graffb8b2732011-05-20 17:33:28 +02002970 if (new_block->host == MAP_FAILED) {
2971 fprintf(stderr, "Allocating RAM failed\n");
2972 abort();
2973 }
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002974#else
Jan Kiszka868bb332011-06-21 22:59:09 +02002975 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01002976 xen_ram_alloc(new_block->offset, size);
2977 } else {
2978 new_block->host = qemu_vmalloc(size);
2979 }
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002980#endif
Andreas Färbere78815a2010-09-25 11:26:05 +00002981 qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09002982 }
2983 }
Cam Macdonell84b89d72010-07-26 18:10:57 -06002984 new_block->length = size;
2985
2986 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
2987
Anthony Liguori7267c092011-08-20 22:09:37 -05002988 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
Cam Macdonell84b89d72010-07-26 18:10:57 -06002989 last_ram_offset() >> TARGET_PAGE_BITS);
2990 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
2991 0xff, size >> TARGET_PAGE_BITS);
2992
2993 if (kvm_enabled())
2994 kvm_setup_guest_memory(new_block->host, size);
2995
2996 return new_block->offset;
2997}
2998
Alex Williamson1724f042010-06-25 11:09:35 -06002999ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
pbrook94a6b542009-04-11 17:15:54 +00003000{
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09003001 return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
pbrook94a6b542009-04-11 17:15:54 +00003002}
bellarde9a1ab12007-02-08 23:08:38 +00003003
Alex Williamson1f2e98b2011-05-03 12:48:09 -06003004void qemu_ram_free_from_ptr(ram_addr_t addr)
3005{
3006 RAMBlock *block;
3007
3008 QLIST_FOREACH(block, &ram_list.blocks, next) {
3009 if (addr == block->offset) {
3010 QLIST_REMOVE(block, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05003011 g_free(block);
Alex Williamson1f2e98b2011-05-03 12:48:09 -06003012 return;
3013 }
3014 }
3015}
3016
Anthony Liguoric227f092009-10-01 16:12:16 -05003017void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00003018{
Alex Williamson04b16652010-07-02 11:13:17 -06003019 RAMBlock *block;
3020
3021 QLIST_FOREACH(block, &ram_list.blocks, next) {
3022 if (addr == block->offset) {
3023 QLIST_REMOVE(block, next);
Huang Yingcd19cfa2011-03-02 08:56:19 +01003024 if (block->flags & RAM_PREALLOC_MASK) {
3025 ;
3026 } else if (mem_path) {
Alex Williamson04b16652010-07-02 11:13:17 -06003027#if defined (__linux__) && !defined(TARGET_S390X)
3028 if (block->fd) {
3029 munmap(block->host, block->length);
3030 close(block->fd);
3031 } else {
3032 qemu_vfree(block->host);
3033 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01003034#else
3035 abort();
Alex Williamson04b16652010-07-02 11:13:17 -06003036#endif
3037 } else {
3038#if defined(TARGET_S390X) && defined(CONFIG_KVM)
3039 munmap(block->host, block->length);
3040#else
Jan Kiszka868bb332011-06-21 22:59:09 +02003041 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003042 xen_invalidate_map_cache_entry(block->host);
Jun Nakajima432d2682010-08-31 16:41:25 +01003043 } else {
3044 qemu_vfree(block->host);
3045 }
Alex Williamson04b16652010-07-02 11:13:17 -06003046#endif
3047 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003048 g_free(block);
Alex Williamson04b16652010-07-02 11:13:17 -06003049 return;
3050 }
3051 }
3052
bellarde9a1ab12007-02-08 23:08:38 +00003053}
3054
Huang Yingcd19cfa2011-03-02 08:56:19 +01003055#ifndef _WIN32
3056void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
3057{
3058 RAMBlock *block;
3059 ram_addr_t offset;
3060 int flags;
3061 void *area, *vaddr;
3062
3063 QLIST_FOREACH(block, &ram_list.blocks, next) {
3064 offset = addr - block->offset;
3065 if (offset < block->length) {
3066 vaddr = block->host + offset;
3067 if (block->flags & RAM_PREALLOC_MASK) {
3068 ;
3069 } else {
3070 flags = MAP_FIXED;
3071 munmap(vaddr, length);
3072 if (mem_path) {
3073#if defined(__linux__) && !defined(TARGET_S390X)
3074 if (block->fd) {
3075#ifdef MAP_POPULATE
3076 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
3077 MAP_PRIVATE;
3078#else
3079 flags |= MAP_PRIVATE;
3080#endif
3081 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3082 flags, block->fd, offset);
3083 } else {
3084 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
3085 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3086 flags, -1, 0);
3087 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01003088#else
3089 abort();
Huang Yingcd19cfa2011-03-02 08:56:19 +01003090#endif
3091 } else {
3092#if defined(TARGET_S390X) && defined(CONFIG_KVM)
3093 flags |= MAP_SHARED | MAP_ANONYMOUS;
3094 area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
3095 flags, -1, 0);
3096#else
3097 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
3098 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3099 flags, -1, 0);
3100#endif
3101 }
3102 if (area != vaddr) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00003103 fprintf(stderr, "Could not remap addr: "
3104 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
Huang Yingcd19cfa2011-03-02 08:56:19 +01003105 length, addr);
3106 exit(1);
3107 }
3108 qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
3109 }
3110 return;
3111 }
3112 }
3113}
3114#endif /* !_WIN32 */
3115
pbrookdc828ca2009-04-09 22:21:07 +00003116/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00003117 With the exception of the softmmu code in this file, this should
3118 only be used for local memory (e.g. video ram) that the device owns,
3119 and knows it isn't going to access beyond the end of the block.
3120
3121 It should not be used for general purpose DMA.
3122 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
3123 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003124void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00003125{
pbrook94a6b542009-04-11 17:15:54 +00003126 RAMBlock *block;
3127
Alex Williamsonf471a172010-06-11 11:11:42 -06003128 QLIST_FOREACH(block, &ram_list.blocks, next) {
3129 if (addr - block->offset < block->length) {
Vincent Palatin7d82af32011-03-10 15:47:46 -05003130 /* Move this entry to to start of the list. */
3131 if (block != QLIST_FIRST(&ram_list.blocks)) {
3132 QLIST_REMOVE(block, next);
3133 QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
3134 }
Jan Kiszka868bb332011-06-21 22:59:09 +02003135 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003136 /* We need to check if the requested address is in the RAM
3137 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003138 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01003139 */
3140 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003141 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01003142 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003143 block->host =
3144 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01003145 }
3146 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003147 return block->host + (addr - block->offset);
3148 }
pbrook94a6b542009-04-11 17:15:54 +00003149 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003150
3151 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3152 abort();
3153
3154 return NULL;
pbrookdc828ca2009-04-09 22:21:07 +00003155}
3156
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02003157/* Return a host pointer to ram allocated with qemu_ram_alloc.
3158 * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
3159 */
3160void *qemu_safe_ram_ptr(ram_addr_t addr)
3161{
3162 RAMBlock *block;
3163
3164 QLIST_FOREACH(block, &ram_list.blocks, next) {
3165 if (addr - block->offset < block->length) {
Jan Kiszka868bb332011-06-21 22:59:09 +02003166 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003167 /* We need to check if the requested address is in the RAM
3168 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003169 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01003170 */
3171 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003172 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01003173 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003174 block->host =
3175 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01003176 }
3177 }
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02003178 return block->host + (addr - block->offset);
3179 }
3180 }
3181
3182 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3183 abort();
3184
3185 return NULL;
3186}
3187
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003188/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
3189 * but takes a size argument */
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01003190void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003191{
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01003192 if (*size == 0) {
3193 return NULL;
3194 }
Jan Kiszka868bb332011-06-21 22:59:09 +02003195 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003196 return xen_map_cache(addr, *size, 1);
Jan Kiszka868bb332011-06-21 22:59:09 +02003197 } else {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003198 RAMBlock *block;
3199
3200 QLIST_FOREACH(block, &ram_list.blocks, next) {
3201 if (addr - block->offset < block->length) {
3202 if (addr - block->offset + *size > block->length)
3203 *size = block->length - addr + block->offset;
3204 return block->host + (addr - block->offset);
3205 }
3206 }
3207
3208 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3209 abort();
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003210 }
3211}
3212
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003213void qemu_put_ram_ptr(void *addr)
3214{
3215 trace_qemu_put_ram_ptr(addr);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003216}
3217
Marcelo Tosattie8902612010-10-11 15:31:19 -03003218int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00003219{
pbrook94a6b542009-04-11 17:15:54 +00003220 RAMBlock *block;
3221 uint8_t *host = ptr;
3222
Jan Kiszka868bb332011-06-21 22:59:09 +02003223 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003224 *ram_addr = xen_ram_addr_from_mapcache(ptr);
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003225 return 0;
3226 }
3227
Alex Williamsonf471a172010-06-11 11:11:42 -06003228 QLIST_FOREACH(block, &ram_list.blocks, next) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003229 /* This case append when the block is not mapped. */
3230 if (block->host == NULL) {
3231 continue;
3232 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003233 if (host - block->host < block->length) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03003234 *ram_addr = block->offset + (host - block->host);
3235 return 0;
Alex Williamsonf471a172010-06-11 11:11:42 -06003236 }
pbrook94a6b542009-04-11 17:15:54 +00003237 }
Jun Nakajima432d2682010-08-31 16:41:25 +01003238
Marcelo Tosattie8902612010-10-11 15:31:19 -03003239 return -1;
3240}
Alex Williamsonf471a172010-06-11 11:11:42 -06003241
Marcelo Tosattie8902612010-10-11 15:31:19 -03003242/* Some of the softmmu routines need to translate from a host pointer
3243 (typically a TLB entry) back to a ram offset. */
3244ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
3245{
3246 ram_addr_t ram_addr;
Alex Williamsonf471a172010-06-11 11:11:42 -06003247
Marcelo Tosattie8902612010-10-11 15:31:19 -03003248 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
3249 fprintf(stderr, "Bad ram pointer %p\n", ptr);
3250 abort();
3251 }
3252 return ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00003253}
3254
Anthony Liguoric227f092009-10-01 16:12:16 -05003255static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00003256{
pbrook67d3b952006-12-18 05:03:52 +00003257#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00003258 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00003259#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003260#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003261 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 1);
blueswir1e18231a2008-10-06 18:46:28 +00003262#endif
3263 return 0;
3264}
3265
Anthony Liguoric227f092009-10-01 16:12:16 -05003266static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00003267{
3268#ifdef DEBUG_UNASSIGNED
3269 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3270#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003271#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003272 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 2);
blueswir1e18231a2008-10-06 18:46:28 +00003273#endif
3274 return 0;
3275}
3276
Anthony Liguoric227f092009-10-01 16:12:16 -05003277static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00003278{
3279#ifdef DEBUG_UNASSIGNED
3280 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3281#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003282#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003283 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00003284#endif
bellard33417e72003-08-10 21:47:01 +00003285 return 0;
3286}
3287
Anthony Liguoric227f092009-10-01 16:12:16 -05003288static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00003289{
pbrook67d3b952006-12-18 05:03:52 +00003290#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00003291 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00003292#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003293#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003294 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 1);
blueswir1e18231a2008-10-06 18:46:28 +00003295#endif
3296}
3297
Anthony Liguoric227f092009-10-01 16:12:16 -05003298static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003299{
3300#ifdef DEBUG_UNASSIGNED
3301 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3302#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003303#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003304 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 2);
blueswir1e18231a2008-10-06 18:46:28 +00003305#endif
3306}
3307
Anthony Liguoric227f092009-10-01 16:12:16 -05003308static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003309{
3310#ifdef DEBUG_UNASSIGNED
3311 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3312#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003313#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003314 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00003315#endif
bellard33417e72003-08-10 21:47:01 +00003316}
3317
Blue Swirld60efc62009-08-25 18:29:31 +00003318static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
bellard33417e72003-08-10 21:47:01 +00003319 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00003320 unassigned_mem_readw,
3321 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00003322};
3323
Blue Swirld60efc62009-08-25 18:29:31 +00003324static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
bellard33417e72003-08-10 21:47:01 +00003325 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00003326 unassigned_mem_writew,
3327 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00003328};
3329
Anthony Liguoric227f092009-10-01 16:12:16 -05003330static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003331 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003332{
bellard3a7d9292005-08-21 09:26:42 +00003333 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003334 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003335 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3336#if !defined(CONFIG_USER_ONLY)
3337 tb_invalidate_phys_page_fast(ram_addr, 1);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003338 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003339#endif
3340 }
pbrook5579c7f2009-04-11 14:47:08 +00003341 stb_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003342 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003343 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003344 /* we remove the notdirty callback only if the code has been
3345 flushed */
3346 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003347 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003348}
3349
Anthony Liguoric227f092009-10-01 16:12:16 -05003350static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003351 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003352{
bellard3a7d9292005-08-21 09:26:42 +00003353 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003354 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003355 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3356#if !defined(CONFIG_USER_ONLY)
3357 tb_invalidate_phys_page_fast(ram_addr, 2);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003358 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003359#endif
3360 }
pbrook5579c7f2009-04-11 14:47:08 +00003361 stw_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003362 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003363 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003364 /* we remove the notdirty callback only if the code has been
3365 flushed */
3366 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003367 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003368}
3369
Anthony Liguoric227f092009-10-01 16:12:16 -05003370static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003371 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003372{
bellard3a7d9292005-08-21 09:26:42 +00003373 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003374 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003375 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3376#if !defined(CONFIG_USER_ONLY)
3377 tb_invalidate_phys_page_fast(ram_addr, 4);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003378 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003379#endif
3380 }
pbrook5579c7f2009-04-11 14:47:08 +00003381 stl_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003382 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003383 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003384 /* we remove the notdirty callback only if the code has been
3385 flushed */
3386 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003387 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003388}
3389
Blue Swirld60efc62009-08-25 18:29:31 +00003390static CPUReadMemoryFunc * const error_mem_read[3] = {
bellard3a7d9292005-08-21 09:26:42 +00003391 NULL, /* never used */
3392 NULL, /* never used */
3393 NULL, /* never used */
3394};
3395
Blue Swirld60efc62009-08-25 18:29:31 +00003396static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
bellard1ccde1c2004-02-06 19:46:14 +00003397 notdirty_mem_writeb,
3398 notdirty_mem_writew,
3399 notdirty_mem_writel,
3400};
3401
pbrook0f459d12008-06-09 00:20:13 +00003402/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00003403static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00003404{
3405 CPUState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00003406 target_ulong pc, cs_base;
3407 TranslationBlock *tb;
pbrook0f459d12008-06-09 00:20:13 +00003408 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00003409 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00003410 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00003411
aliguori06d55cc2008-11-18 20:24:06 +00003412 if (env->watchpoint_hit) {
3413 /* We re-entered the check after replacing the TB. Now raise
3414 * the debug interrupt so that is will trigger after the
3415 * current instruction. */
3416 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
3417 return;
3418 }
pbrook2e70f6e2008-06-29 01:03:05 +00003419 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003420 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00003421 if ((vaddr == (wp->vaddr & len_mask) ||
3422 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00003423 wp->flags |= BP_WATCHPOINT_HIT;
3424 if (!env->watchpoint_hit) {
3425 env->watchpoint_hit = wp;
3426 tb = tb_find_pc(env->mem_io_pc);
3427 if (!tb) {
3428 cpu_abort(env, "check_watchpoint: could not find TB for "
3429 "pc=%p", (void *)env->mem_io_pc);
3430 }
Stefan Weil618ba8e2011-04-18 06:39:53 +00003431 cpu_restore_state(tb, env, env->mem_io_pc);
aliguori6e140f22008-11-18 20:37:55 +00003432 tb_phys_invalidate(tb, -1);
3433 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
3434 env->exception_index = EXCP_DEBUG;
3435 } else {
3436 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
3437 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
3438 }
3439 cpu_resume_from_signal(env, NULL);
aliguori06d55cc2008-11-18 20:24:06 +00003440 }
aliguori6e140f22008-11-18 20:37:55 +00003441 } else {
3442 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00003443 }
3444 }
3445}
3446
pbrook6658ffb2007-03-16 23:58:11 +00003447/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
3448 so these check for a hit then pass through to the normal out-of-line
3449 phys routines. */
Anthony Liguoric227f092009-10-01 16:12:16 -05003450static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003451{
aliguorib4051332008-11-18 20:14:20 +00003452 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003453 return ldub_phys(addr);
3454}
3455
Anthony Liguoric227f092009-10-01 16:12:16 -05003456static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003457{
aliguorib4051332008-11-18 20:14:20 +00003458 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003459 return lduw_phys(addr);
3460}
3461
Anthony Liguoric227f092009-10-01 16:12:16 -05003462static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003463{
aliguorib4051332008-11-18 20:14:20 +00003464 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003465 return ldl_phys(addr);
3466}
3467
Anthony Liguoric227f092009-10-01 16:12:16 -05003468static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003469 uint32_t val)
3470{
aliguorib4051332008-11-18 20:14:20 +00003471 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003472 stb_phys(addr, val);
3473}
3474
Anthony Liguoric227f092009-10-01 16:12:16 -05003475static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003476 uint32_t val)
3477{
aliguorib4051332008-11-18 20:14:20 +00003478 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003479 stw_phys(addr, val);
3480}
3481
Anthony Liguoric227f092009-10-01 16:12:16 -05003482static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003483 uint32_t val)
3484{
aliguorib4051332008-11-18 20:14:20 +00003485 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003486 stl_phys(addr, val);
3487}
3488
Blue Swirld60efc62009-08-25 18:29:31 +00003489static CPUReadMemoryFunc * const watch_mem_read[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003490 watch_mem_readb,
3491 watch_mem_readw,
3492 watch_mem_readl,
3493};
3494
Blue Swirld60efc62009-08-25 18:29:31 +00003495static CPUWriteMemoryFunc * const watch_mem_write[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003496 watch_mem_writeb,
3497 watch_mem_writew,
3498 watch_mem_writel,
3499};
pbrook6658ffb2007-03-16 23:58:11 +00003500
Richard Hendersonf6405242010-04-22 16:47:31 -07003501static inline uint32_t subpage_readlen (subpage_t *mmio,
3502 target_phys_addr_t addr,
3503 unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003504{
Richard Hendersonf6405242010-04-22 16:47:31 -07003505 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003506#if defined(DEBUG_SUBPAGE)
3507 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3508 mmio, len, addr, idx);
3509#endif
blueswir1db7b5422007-05-26 17:36:03 +00003510
Richard Hendersonf6405242010-04-22 16:47:31 -07003511 addr += mmio->region_offset[idx];
3512 idx = mmio->sub_io_index[idx];
3513 return io_mem_read[idx][len](io_mem_opaque[idx], addr);
blueswir1db7b5422007-05-26 17:36:03 +00003514}
3515
Anthony Liguoric227f092009-10-01 16:12:16 -05003516static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
Richard Hendersonf6405242010-04-22 16:47:31 -07003517 uint32_t value, unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003518{
Richard Hendersonf6405242010-04-22 16:47:31 -07003519 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003520#if defined(DEBUG_SUBPAGE)
Richard Hendersonf6405242010-04-22 16:47:31 -07003521 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n",
3522 __func__, mmio, len, addr, idx, value);
blueswir1db7b5422007-05-26 17:36:03 +00003523#endif
Richard Hendersonf6405242010-04-22 16:47:31 -07003524
3525 addr += mmio->region_offset[idx];
3526 idx = mmio->sub_io_index[idx];
3527 io_mem_write[idx][len](io_mem_opaque[idx], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00003528}
3529
Anthony Liguoric227f092009-10-01 16:12:16 -05003530static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003531{
blueswir1db7b5422007-05-26 17:36:03 +00003532 return subpage_readlen(opaque, addr, 0);
3533}
3534
Anthony Liguoric227f092009-10-01 16:12:16 -05003535static void subpage_writeb (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003536 uint32_t value)
3537{
blueswir1db7b5422007-05-26 17:36:03 +00003538 subpage_writelen(opaque, addr, value, 0);
3539}
3540
Anthony Liguoric227f092009-10-01 16:12:16 -05003541static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003542{
blueswir1db7b5422007-05-26 17:36:03 +00003543 return subpage_readlen(opaque, addr, 1);
3544}
3545
Anthony Liguoric227f092009-10-01 16:12:16 -05003546static void subpage_writew (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003547 uint32_t value)
3548{
blueswir1db7b5422007-05-26 17:36:03 +00003549 subpage_writelen(opaque, addr, value, 1);
3550}
3551
Anthony Liguoric227f092009-10-01 16:12:16 -05003552static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003553{
blueswir1db7b5422007-05-26 17:36:03 +00003554 return subpage_readlen(opaque, addr, 2);
3555}
3556
Richard Hendersonf6405242010-04-22 16:47:31 -07003557static void subpage_writel (void *opaque, target_phys_addr_t addr,
3558 uint32_t value)
blueswir1db7b5422007-05-26 17:36:03 +00003559{
blueswir1db7b5422007-05-26 17:36:03 +00003560 subpage_writelen(opaque, addr, value, 2);
3561}
3562
Blue Swirld60efc62009-08-25 18:29:31 +00003563static CPUReadMemoryFunc * const subpage_read[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003564 &subpage_readb,
3565 &subpage_readw,
3566 &subpage_readl,
3567};
3568
Blue Swirld60efc62009-08-25 18:29:31 +00003569static CPUWriteMemoryFunc * const subpage_write[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003570 &subpage_writeb,
3571 &subpage_writew,
3572 &subpage_writel,
3573};
3574
Anthony Liguoric227f092009-10-01 16:12:16 -05003575static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3576 ram_addr_t memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003577{
3578 int idx, eidx;
3579
3580 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3581 return -1;
3582 idx = SUBPAGE_IDX(start);
3583 eidx = SUBPAGE_IDX(end);
3584#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00003585 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00003586 mmio, start, end, idx, eidx, memory);
3587#endif
Gleb Natapov95c318f2010-07-29 10:41:45 +03003588 if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
3589 memory = IO_MEM_UNASSIGNED;
Richard Hendersonf6405242010-04-22 16:47:31 -07003590 memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
blueswir1db7b5422007-05-26 17:36:03 +00003591 for (; idx <= eidx; idx++) {
Richard Hendersonf6405242010-04-22 16:47:31 -07003592 mmio->sub_io_index[idx] = memory;
3593 mmio->region_offset[idx] = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00003594 }
3595
3596 return 0;
3597}
3598
Richard Hendersonf6405242010-04-22 16:47:31 -07003599static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3600 ram_addr_t orig_memory,
3601 ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003602{
Anthony Liguoric227f092009-10-01 16:12:16 -05003603 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00003604 int subpage_memory;
3605
Anthony Liguori7267c092011-08-20 22:09:37 -05003606 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00003607
3608 mmio->base = base;
Alexander Graf2507c122010-12-08 12:05:37 +01003609 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio,
3610 DEVICE_NATIVE_ENDIAN);
blueswir1db7b5422007-05-26 17:36:03 +00003611#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00003612 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3613 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00003614#endif
aliguori1eec6142009-02-05 22:06:18 +00003615 *phys = subpage_memory | IO_MEM_SUBPAGE;
Richard Hendersonf6405242010-04-22 16:47:31 -07003616 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00003617
3618 return mmio;
3619}
3620
aliguori88715652009-02-11 15:20:58 +00003621static int get_free_io_mem_idx(void)
3622{
3623 int i;
3624
3625 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3626 if (!io_mem_used[i]) {
3627 io_mem_used[i] = 1;
3628 return i;
3629 }
Riku Voipioc6703b42009-12-03 15:56:05 +02003630 fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
aliguori88715652009-02-11 15:20:58 +00003631 return -1;
3632}
3633
Alexander Grafdd310532010-12-08 12:05:36 +01003634/*
3635 * Usually, devices operate in little endian mode. There are devices out
3636 * there that operate in big endian too. Each device gets byte swapped
3637 * mmio if plugged onto a CPU that does the other endianness.
3638 *
3639 * CPU Device swap?
3640 *
3641 * little little no
3642 * little big yes
3643 * big little yes
3644 * big big no
3645 */
3646
3647typedef struct SwapEndianContainer {
3648 CPUReadMemoryFunc *read[3];
3649 CPUWriteMemoryFunc *write[3];
3650 void *opaque;
3651} SwapEndianContainer;
3652
3653static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr)
3654{
3655 uint32_t val;
3656 SwapEndianContainer *c = opaque;
3657 val = c->read[0](c->opaque, addr);
3658 return val;
3659}
3660
3661static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr)
3662{
3663 uint32_t val;
3664 SwapEndianContainer *c = opaque;
3665 val = bswap16(c->read[1](c->opaque, addr));
3666 return val;
3667}
3668
3669static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr)
3670{
3671 uint32_t val;
3672 SwapEndianContainer *c = opaque;
3673 val = bswap32(c->read[2](c->opaque, addr));
3674 return val;
3675}
3676
3677static CPUReadMemoryFunc * const swapendian_readfn[3]={
3678 swapendian_mem_readb,
3679 swapendian_mem_readw,
3680 swapendian_mem_readl
3681};
3682
3683static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr,
3684 uint32_t val)
3685{
3686 SwapEndianContainer *c = opaque;
3687 c->write[0](c->opaque, addr, val);
3688}
3689
3690static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr,
3691 uint32_t val)
3692{
3693 SwapEndianContainer *c = opaque;
3694 c->write[1](c->opaque, addr, bswap16(val));
3695}
3696
3697static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr,
3698 uint32_t val)
3699{
3700 SwapEndianContainer *c = opaque;
3701 c->write[2](c->opaque, addr, bswap32(val));
3702}
3703
3704static CPUWriteMemoryFunc * const swapendian_writefn[3]={
3705 swapendian_mem_writeb,
3706 swapendian_mem_writew,
3707 swapendian_mem_writel
3708};
3709
3710static void swapendian_init(int io_index)
3711{
Anthony Liguori7267c092011-08-20 22:09:37 -05003712 SwapEndianContainer *c = g_malloc(sizeof(SwapEndianContainer));
Alexander Grafdd310532010-12-08 12:05:36 +01003713 int i;
3714
3715 /* Swap mmio for big endian targets */
3716 c->opaque = io_mem_opaque[io_index];
3717 for (i = 0; i < 3; i++) {
3718 c->read[i] = io_mem_read[io_index][i];
3719 c->write[i] = io_mem_write[io_index][i];
3720
3721 io_mem_read[io_index][i] = swapendian_readfn[i];
3722 io_mem_write[io_index][i] = swapendian_writefn[i];
3723 }
3724 io_mem_opaque[io_index] = c;
3725}
3726
3727static void swapendian_del(int io_index)
3728{
3729 if (io_mem_read[io_index][0] == swapendian_readfn[0]) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003730 g_free(io_mem_opaque[io_index]);
Alexander Grafdd310532010-12-08 12:05:36 +01003731 }
3732}
3733
bellard33417e72003-08-10 21:47:01 +00003734/* mem_read and mem_write are arrays of functions containing the
3735 function to access byte (index 0), word (index 1) and dword (index
Paul Brook0b4e6e32009-04-30 18:37:55 +01003736 2). Functions can be omitted with a NULL function pointer.
blueswir13ee89922008-01-02 19:45:26 +00003737 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00003738 modified. If it is zero, a new io zone is allocated. The return
3739 value can be used with cpu_register_physical_memory(). (-1) is
3740 returned if error. */
Avi Kivity1eed09c2009-06-14 11:38:51 +03003741static int cpu_register_io_memory_fixed(int io_index,
Blue Swirld60efc62009-08-25 18:29:31 +00003742 CPUReadMemoryFunc * const *mem_read,
3743 CPUWriteMemoryFunc * const *mem_write,
Alexander Grafdd310532010-12-08 12:05:36 +01003744 void *opaque, enum device_endian endian)
bellard33417e72003-08-10 21:47:01 +00003745{
Richard Henderson3cab7212010-05-07 09:52:51 -07003746 int i;
3747
bellard33417e72003-08-10 21:47:01 +00003748 if (io_index <= 0) {
aliguori88715652009-02-11 15:20:58 +00003749 io_index = get_free_io_mem_idx();
3750 if (io_index == -1)
3751 return io_index;
bellard33417e72003-08-10 21:47:01 +00003752 } else {
Avi Kivity1eed09c2009-06-14 11:38:51 +03003753 io_index >>= IO_MEM_SHIFT;
bellard33417e72003-08-10 21:47:01 +00003754 if (io_index >= IO_MEM_NB_ENTRIES)
3755 return -1;
3756 }
bellardb5ff1b32005-11-26 10:38:39 +00003757
Richard Henderson3cab7212010-05-07 09:52:51 -07003758 for (i = 0; i < 3; ++i) {
3759 io_mem_read[io_index][i]
3760 = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]);
3761 }
3762 for (i = 0; i < 3; ++i) {
3763 io_mem_write[io_index][i]
3764 = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]);
3765 }
bellarda4193c82004-06-03 14:01:43 +00003766 io_mem_opaque[io_index] = opaque;
Richard Hendersonf6405242010-04-22 16:47:31 -07003767
Alexander Grafdd310532010-12-08 12:05:36 +01003768 switch (endian) {
3769 case DEVICE_BIG_ENDIAN:
3770#ifndef TARGET_WORDS_BIGENDIAN
3771 swapendian_init(io_index);
3772#endif
3773 break;
3774 case DEVICE_LITTLE_ENDIAN:
3775#ifdef TARGET_WORDS_BIGENDIAN
3776 swapendian_init(io_index);
3777#endif
3778 break;
3779 case DEVICE_NATIVE_ENDIAN:
3780 default:
3781 break;
3782 }
3783
Richard Hendersonf6405242010-04-22 16:47:31 -07003784 return (io_index << IO_MEM_SHIFT);
bellard33417e72003-08-10 21:47:01 +00003785}
bellard61382a52003-10-27 21:22:23 +00003786
Blue Swirld60efc62009-08-25 18:29:31 +00003787int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
3788 CPUWriteMemoryFunc * const *mem_write,
Alexander Grafdd310532010-12-08 12:05:36 +01003789 void *opaque, enum device_endian endian)
Avi Kivity1eed09c2009-06-14 11:38:51 +03003790{
Alexander Graf2507c122010-12-08 12:05:37 +01003791 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian);
Avi Kivity1eed09c2009-06-14 11:38:51 +03003792}
3793
aliguori88715652009-02-11 15:20:58 +00003794void cpu_unregister_io_memory(int io_table_address)
3795{
3796 int i;
3797 int io_index = io_table_address >> IO_MEM_SHIFT;
3798
Alexander Grafdd310532010-12-08 12:05:36 +01003799 swapendian_del(io_index);
3800
aliguori88715652009-02-11 15:20:58 +00003801 for (i=0;i < 3; i++) {
3802 io_mem_read[io_index][i] = unassigned_mem_read[i];
3803 io_mem_write[io_index][i] = unassigned_mem_write[i];
3804 }
3805 io_mem_opaque[io_index] = NULL;
3806 io_mem_used[io_index] = 0;
3807}
3808
Avi Kivitye9179ce2009-06-14 11:38:52 +03003809static void io_mem_init(void)
3810{
3811 int i;
3812
Alexander Graf2507c122010-12-08 12:05:37 +01003813 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read,
3814 unassigned_mem_write, NULL,
3815 DEVICE_NATIVE_ENDIAN);
3816 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read,
3817 unassigned_mem_write, NULL,
3818 DEVICE_NATIVE_ENDIAN);
3819 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read,
3820 notdirty_mem_write, NULL,
3821 DEVICE_NATIVE_ENDIAN);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003822 for (i=0; i<5; i++)
3823 io_mem_used[i] = 1;
3824
3825 io_mem_watch = cpu_register_io_memory(watch_mem_read,
Alexander Graf2507c122010-12-08 12:05:37 +01003826 watch_mem_write, NULL,
3827 DEVICE_NATIVE_ENDIAN);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003828}
3829
Avi Kivity62152b82011-07-26 14:26:14 +03003830static void memory_map_init(void)
3831{
Anthony Liguori7267c092011-08-20 22:09:37 -05003832 system_memory = g_malloc(sizeof(*system_memory));
Avi Kivity8417ceb2011-08-03 11:56:14 +03003833 memory_region_init(system_memory, "system", INT64_MAX);
Avi Kivity62152b82011-07-26 14:26:14 +03003834 set_system_memory_map(system_memory);
Avi Kivity309cb472011-08-08 16:09:03 +03003835
Anthony Liguori7267c092011-08-20 22:09:37 -05003836 system_io = g_malloc(sizeof(*system_io));
Avi Kivity309cb472011-08-08 16:09:03 +03003837 memory_region_init(system_io, "io", 65536);
3838 set_system_io_map(system_io);
Avi Kivity62152b82011-07-26 14:26:14 +03003839}
3840
3841MemoryRegion *get_system_memory(void)
3842{
3843 return system_memory;
3844}
3845
Avi Kivity309cb472011-08-08 16:09:03 +03003846MemoryRegion *get_system_io(void)
3847{
3848 return system_io;
3849}
3850
pbrooke2eef172008-06-08 01:09:01 +00003851#endif /* !defined(CONFIG_USER_ONLY) */
3852
bellard13eb76e2004-01-24 15:23:36 +00003853/* physical memory access (slow version, mainly for debug) */
3854#if defined(CONFIG_USER_ONLY)
Paul Brooka68fe892010-03-01 00:08:59 +00003855int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3856 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003857{
3858 int l, flags;
3859 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00003860 void * p;
bellard13eb76e2004-01-24 15:23:36 +00003861
3862 while (len > 0) {
3863 page = addr & TARGET_PAGE_MASK;
3864 l = (page + TARGET_PAGE_SIZE) - addr;
3865 if (l > len)
3866 l = len;
3867 flags = page_get_flags(page);
3868 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00003869 return -1;
bellard13eb76e2004-01-24 15:23:36 +00003870 if (is_write) {
3871 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00003872 return -1;
bellard579a97f2007-11-11 14:26:47 +00003873 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003874 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00003875 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003876 memcpy(p, buf, l);
3877 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00003878 } else {
3879 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00003880 return -1;
bellard579a97f2007-11-11 14:26:47 +00003881 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003882 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00003883 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003884 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00003885 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00003886 }
3887 len -= l;
3888 buf += l;
3889 addr += l;
3890 }
Paul Brooka68fe892010-03-01 00:08:59 +00003891 return 0;
bellard13eb76e2004-01-24 15:23:36 +00003892}
bellard8df1cd02005-01-28 22:37:22 +00003893
bellard13eb76e2004-01-24 15:23:36 +00003894#else
Anthony Liguoric227f092009-10-01 16:12:16 -05003895void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00003896 int len, int is_write)
3897{
3898 int l, io_index;
3899 uint8_t *ptr;
3900 uint32_t val;
Anthony Liguoric227f092009-10-01 16:12:16 -05003901 target_phys_addr_t page;
Anthony PERARD8ca56922011-07-15 04:32:53 +00003902 ram_addr_t pd;
bellard92e873b2004-05-21 14:52:29 +00003903 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003904
bellard13eb76e2004-01-24 15:23:36 +00003905 while (len > 0) {
3906 page = addr & TARGET_PAGE_MASK;
3907 l = (page + TARGET_PAGE_SIZE) - addr;
3908 if (l > len)
3909 l = len;
bellard92e873b2004-05-21 14:52:29 +00003910 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00003911 if (!p) {
3912 pd = IO_MEM_UNASSIGNED;
3913 } else {
3914 pd = p->phys_offset;
3915 }
ths3b46e622007-09-17 08:09:54 +00003916
bellard13eb76e2004-01-24 15:23:36 +00003917 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00003918 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003919 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003920 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003921 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003922 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard6a00d602005-11-21 23:25:50 +00003923 /* XXX: could force cpu_single_env to NULL to avoid
3924 potential bugs */
aurel326c2934d2009-02-18 21:37:17 +00003925 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003926 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003927 val = ldl_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003928 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003929 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003930 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003931 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003932 val = lduw_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003933 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003934 l = 2;
3935 } else {
bellard1c213d12005-09-03 10:49:04 +00003936 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003937 val = ldub_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003938 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003939 l = 1;
3940 }
3941 } else {
Anthony PERARD8ca56922011-07-15 04:32:53 +00003942 ram_addr_t addr1;
bellardb448f2f2004-02-25 23:24:04 +00003943 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00003944 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00003945 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00003946 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00003947 if (!cpu_physical_memory_is_dirty(addr1)) {
3948 /* invalidate code */
3949 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3950 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003951 cpu_physical_memory_set_dirty_flags(
3952 addr1, (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00003953 }
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003954 qemu_put_ram_ptr(ptr);
bellard13eb76e2004-01-24 15:23:36 +00003955 }
3956 } else {
ths5fafdf22007-09-16 21:08:06 +00003957 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00003958 !(pd & IO_MEM_ROMD)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003959 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003960 /* I/O case */
3961 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003962 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003963 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3964 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003965 /* 32 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003966 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003967 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003968 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003969 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00003970 /* 16 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003971 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003972 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003973 l = 2;
3974 } else {
bellard1c213d12005-09-03 10:49:04 +00003975 /* 8 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00003976 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00003977 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00003978 l = 1;
3979 }
3980 } else {
3981 /* RAM case */
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003982 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
3983 memcpy(buf, ptr + (addr & ~TARGET_PAGE_MASK), l);
3984 qemu_put_ram_ptr(ptr);
bellard13eb76e2004-01-24 15:23:36 +00003985 }
3986 }
3987 len -= l;
3988 buf += l;
3989 addr += l;
3990 }
3991}
bellard8df1cd02005-01-28 22:37:22 +00003992
bellardd0ecd2a2006-04-23 17:14:48 +00003993/* used for ROM loading : can write in RAM and ROM */
Anthony Liguoric227f092009-10-01 16:12:16 -05003994void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00003995 const uint8_t *buf, int len)
3996{
3997 int l;
3998 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003999 target_phys_addr_t page;
bellardd0ecd2a2006-04-23 17:14:48 +00004000 unsigned long pd;
4001 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00004002
bellardd0ecd2a2006-04-23 17:14:48 +00004003 while (len > 0) {
4004 page = addr & TARGET_PAGE_MASK;
4005 l = (page + TARGET_PAGE_SIZE) - addr;
4006 if (l > len)
4007 l = len;
4008 p = phys_page_find(page >> TARGET_PAGE_BITS);
4009 if (!p) {
4010 pd = IO_MEM_UNASSIGNED;
4011 } else {
4012 pd = p->phys_offset;
4013 }
ths3b46e622007-09-17 08:09:54 +00004014
bellardd0ecd2a2006-04-23 17:14:48 +00004015 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00004016 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
4017 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00004018 /* do nothing */
4019 } else {
4020 unsigned long addr1;
4021 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4022 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004023 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00004024 memcpy(ptr, buf, l);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004025 qemu_put_ram_ptr(ptr);
bellardd0ecd2a2006-04-23 17:14:48 +00004026 }
4027 len -= l;
4028 buf += l;
4029 addr += l;
4030 }
4031}
4032
aliguori6d16c2f2009-01-22 16:59:11 +00004033typedef struct {
4034 void *buffer;
Anthony Liguoric227f092009-10-01 16:12:16 -05004035 target_phys_addr_t addr;
4036 target_phys_addr_t len;
aliguori6d16c2f2009-01-22 16:59:11 +00004037} BounceBuffer;
4038
4039static BounceBuffer bounce;
4040
aliguoriba223c22009-01-22 16:59:16 +00004041typedef struct MapClient {
4042 void *opaque;
4043 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004044 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00004045} MapClient;
4046
Blue Swirl72cf2d42009-09-12 07:36:22 +00004047static QLIST_HEAD(map_client_list, MapClient) map_client_list
4048 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00004049
4050void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
4051{
Anthony Liguori7267c092011-08-20 22:09:37 -05004052 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00004053
4054 client->opaque = opaque;
4055 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004056 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00004057 return client;
4058}
4059
4060void cpu_unregister_map_client(void *_client)
4061{
4062 MapClient *client = (MapClient *)_client;
4063
Blue Swirl72cf2d42009-09-12 07:36:22 +00004064 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05004065 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00004066}
4067
4068static void cpu_notify_map_clients(void)
4069{
4070 MapClient *client;
4071
Blue Swirl72cf2d42009-09-12 07:36:22 +00004072 while (!QLIST_EMPTY(&map_client_list)) {
4073 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00004074 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09004075 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00004076 }
4077}
4078
aliguori6d16c2f2009-01-22 16:59:11 +00004079/* Map a physical memory region into a host virtual address.
4080 * May map a subset of the requested range, given by and returned in *plen.
4081 * May return NULL if resources needed to perform the mapping are exhausted.
4082 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00004083 * Use cpu_register_map_client() to know when retrying the map operation is
4084 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00004085 */
Anthony Liguoric227f092009-10-01 16:12:16 -05004086void *cpu_physical_memory_map(target_phys_addr_t addr,
4087 target_phys_addr_t *plen,
aliguori6d16c2f2009-01-22 16:59:11 +00004088 int is_write)
4089{
Anthony Liguoric227f092009-10-01 16:12:16 -05004090 target_phys_addr_t len = *plen;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004091 target_phys_addr_t todo = 0;
aliguori6d16c2f2009-01-22 16:59:11 +00004092 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05004093 target_phys_addr_t page;
aliguori6d16c2f2009-01-22 16:59:11 +00004094 unsigned long pd;
4095 PhysPageDesc *p;
Anthony PERARDf15fbc42011-07-20 08:17:42 +00004096 ram_addr_t raddr = RAM_ADDR_MAX;
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004097 ram_addr_t rlen;
4098 void *ret;
aliguori6d16c2f2009-01-22 16:59:11 +00004099
4100 while (len > 0) {
4101 page = addr & TARGET_PAGE_MASK;
4102 l = (page + TARGET_PAGE_SIZE) - addr;
4103 if (l > len)
4104 l = len;
4105 p = phys_page_find(page >> TARGET_PAGE_BITS);
4106 if (!p) {
4107 pd = IO_MEM_UNASSIGNED;
4108 } else {
4109 pd = p->phys_offset;
4110 }
4111
4112 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004113 if (todo || bounce.buffer) {
aliguori6d16c2f2009-01-22 16:59:11 +00004114 break;
4115 }
4116 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
4117 bounce.addr = addr;
4118 bounce.len = l;
4119 if (!is_write) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02004120 cpu_physical_memory_read(addr, bounce.buffer, l);
aliguori6d16c2f2009-01-22 16:59:11 +00004121 }
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004122
4123 *plen = l;
4124 return bounce.buffer;
aliguori6d16c2f2009-01-22 16:59:11 +00004125 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004126 if (!todo) {
4127 raddr = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4128 }
aliguori6d16c2f2009-01-22 16:59:11 +00004129
4130 len -= l;
4131 addr += l;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004132 todo += l;
aliguori6d16c2f2009-01-22 16:59:11 +00004133 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004134 rlen = todo;
4135 ret = qemu_ram_ptr_length(raddr, &rlen);
4136 *plen = rlen;
4137 return ret;
aliguori6d16c2f2009-01-22 16:59:11 +00004138}
4139
4140/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
4141 * Will also mark the memory as dirty if is_write == 1. access_len gives
4142 * the amount of memory that was actually read or written by the caller.
4143 */
Anthony Liguoric227f092009-10-01 16:12:16 -05004144void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
4145 int is_write, target_phys_addr_t access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00004146{
4147 if (buffer != bounce.buffer) {
4148 if (is_write) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03004149 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00004150 while (access_len) {
4151 unsigned l;
4152 l = TARGET_PAGE_SIZE;
4153 if (l > access_len)
4154 l = access_len;
4155 if (!cpu_physical_memory_is_dirty(addr1)) {
4156 /* invalidate code */
4157 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
4158 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004159 cpu_physical_memory_set_dirty_flags(
4160 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori6d16c2f2009-01-22 16:59:11 +00004161 }
4162 addr1 += l;
4163 access_len -= l;
4164 }
4165 }
Jan Kiszka868bb332011-06-21 22:59:09 +02004166 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02004167 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004168 }
aliguori6d16c2f2009-01-22 16:59:11 +00004169 return;
4170 }
4171 if (is_write) {
4172 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
4173 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00004174 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00004175 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00004176 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00004177}
bellardd0ecd2a2006-04-23 17:14:48 +00004178
bellard8df1cd02005-01-28 22:37:22 +00004179/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004180static inline uint32_t ldl_phys_internal(target_phys_addr_t addr,
4181 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00004182{
4183 int io_index;
4184 uint8_t *ptr;
4185 uint32_t val;
4186 unsigned long pd;
4187 PhysPageDesc *p;
4188
4189 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4190 if (!p) {
4191 pd = IO_MEM_UNASSIGNED;
4192 } else {
4193 pd = p->phys_offset;
4194 }
ths3b46e622007-09-17 08:09:54 +00004195
ths5fafdf22007-09-16 21:08:06 +00004196 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00004197 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00004198 /* I/O case */
4199 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004200 if (p)
4201 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00004202 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004203#if defined(TARGET_WORDS_BIGENDIAN)
4204 if (endian == DEVICE_LITTLE_ENDIAN) {
4205 val = bswap32(val);
4206 }
4207#else
4208 if (endian == DEVICE_BIG_ENDIAN) {
4209 val = bswap32(val);
4210 }
4211#endif
bellard8df1cd02005-01-28 22:37:22 +00004212 } else {
4213 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004214 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00004215 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004216 switch (endian) {
4217 case DEVICE_LITTLE_ENDIAN:
4218 val = ldl_le_p(ptr);
4219 break;
4220 case DEVICE_BIG_ENDIAN:
4221 val = ldl_be_p(ptr);
4222 break;
4223 default:
4224 val = ldl_p(ptr);
4225 break;
4226 }
bellard8df1cd02005-01-28 22:37:22 +00004227 }
4228 return val;
4229}
4230
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004231uint32_t ldl_phys(target_phys_addr_t addr)
4232{
4233 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4234}
4235
4236uint32_t ldl_le_phys(target_phys_addr_t addr)
4237{
4238 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4239}
4240
4241uint32_t ldl_be_phys(target_phys_addr_t addr)
4242{
4243 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
4244}
4245
bellard84b7b8e2005-11-28 21:19:04 +00004246/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004247static inline uint64_t ldq_phys_internal(target_phys_addr_t addr,
4248 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00004249{
4250 int io_index;
4251 uint8_t *ptr;
4252 uint64_t val;
4253 unsigned long pd;
4254 PhysPageDesc *p;
4255
4256 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4257 if (!p) {
4258 pd = IO_MEM_UNASSIGNED;
4259 } else {
4260 pd = p->phys_offset;
4261 }
ths3b46e622007-09-17 08:09:54 +00004262
bellard2a4188a2006-06-25 21:54:59 +00004263 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
4264 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00004265 /* I/O case */
4266 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004267 if (p)
4268 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004269
4270 /* XXX This is broken when device endian != cpu endian.
4271 Fix and add "endian" variable check */
bellard84b7b8e2005-11-28 21:19:04 +00004272#ifdef TARGET_WORDS_BIGENDIAN
4273 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
4274 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
4275#else
4276 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
4277 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
4278#endif
4279 } else {
4280 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004281 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00004282 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004283 switch (endian) {
4284 case DEVICE_LITTLE_ENDIAN:
4285 val = ldq_le_p(ptr);
4286 break;
4287 case DEVICE_BIG_ENDIAN:
4288 val = ldq_be_p(ptr);
4289 break;
4290 default:
4291 val = ldq_p(ptr);
4292 break;
4293 }
bellard84b7b8e2005-11-28 21:19:04 +00004294 }
4295 return val;
4296}
4297
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004298uint64_t ldq_phys(target_phys_addr_t addr)
4299{
4300 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4301}
4302
4303uint64_t ldq_le_phys(target_phys_addr_t addr)
4304{
4305 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4306}
4307
4308uint64_t ldq_be_phys(target_phys_addr_t addr)
4309{
4310 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
4311}
4312
bellardaab33092005-10-30 20:48:42 +00004313/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004314uint32_t ldub_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00004315{
4316 uint8_t val;
4317 cpu_physical_memory_read(addr, &val, 1);
4318 return val;
4319}
4320
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004321/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004322static inline uint32_t lduw_phys_internal(target_phys_addr_t addr,
4323 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00004324{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004325 int io_index;
4326 uint8_t *ptr;
4327 uint64_t val;
4328 unsigned long pd;
4329 PhysPageDesc *p;
4330
4331 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4332 if (!p) {
4333 pd = IO_MEM_UNASSIGNED;
4334 } else {
4335 pd = p->phys_offset;
4336 }
4337
4338 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
4339 !(pd & IO_MEM_ROMD)) {
4340 /* I/O case */
4341 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4342 if (p)
4343 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4344 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004345#if defined(TARGET_WORDS_BIGENDIAN)
4346 if (endian == DEVICE_LITTLE_ENDIAN) {
4347 val = bswap16(val);
4348 }
4349#else
4350 if (endian == DEVICE_BIG_ENDIAN) {
4351 val = bswap16(val);
4352 }
4353#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004354 } else {
4355 /* RAM case */
4356 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
4357 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004358 switch (endian) {
4359 case DEVICE_LITTLE_ENDIAN:
4360 val = lduw_le_p(ptr);
4361 break;
4362 case DEVICE_BIG_ENDIAN:
4363 val = lduw_be_p(ptr);
4364 break;
4365 default:
4366 val = lduw_p(ptr);
4367 break;
4368 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004369 }
4370 return val;
bellardaab33092005-10-30 20:48:42 +00004371}
4372
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004373uint32_t lduw_phys(target_phys_addr_t addr)
4374{
4375 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4376}
4377
4378uint32_t lduw_le_phys(target_phys_addr_t addr)
4379{
4380 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4381}
4382
4383uint32_t lduw_be_phys(target_phys_addr_t addr)
4384{
4385 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
4386}
4387
bellard8df1cd02005-01-28 22:37:22 +00004388/* warning: addr must be aligned. The ram page is not masked as dirty
4389 and the code inside is not invalidated. It is useful if the dirty
4390 bits are used to track modified PTEs */
Anthony Liguoric227f092009-10-01 16:12:16 -05004391void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00004392{
4393 int io_index;
4394 uint8_t *ptr;
4395 unsigned long pd;
4396 PhysPageDesc *p;
4397
4398 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4399 if (!p) {
4400 pd = IO_MEM_UNASSIGNED;
4401 } else {
4402 pd = p->phys_offset;
4403 }
ths3b46e622007-09-17 08:09:54 +00004404
bellard3a7d9292005-08-21 09:26:42 +00004405 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00004406 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004407 if (p)
4408 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00004409 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4410 } else {
aliguori74576192008-10-06 14:02:03 +00004411 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00004412 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00004413 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00004414
4415 if (unlikely(in_migration)) {
4416 if (!cpu_physical_memory_is_dirty(addr1)) {
4417 /* invalidate code */
4418 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4419 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004420 cpu_physical_memory_set_dirty_flags(
4421 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00004422 }
4423 }
bellard8df1cd02005-01-28 22:37:22 +00004424 }
4425}
4426
Anthony Liguoric227f092009-10-01 16:12:16 -05004427void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
j_mayerbc98a7e2007-04-04 07:55:12 +00004428{
4429 int io_index;
4430 uint8_t *ptr;
4431 unsigned long pd;
4432 PhysPageDesc *p;
4433
4434 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4435 if (!p) {
4436 pd = IO_MEM_UNASSIGNED;
4437 } else {
4438 pd = p->phys_offset;
4439 }
ths3b46e622007-09-17 08:09:54 +00004440
j_mayerbc98a7e2007-04-04 07:55:12 +00004441 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4442 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004443 if (p)
4444 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
j_mayerbc98a7e2007-04-04 07:55:12 +00004445#ifdef TARGET_WORDS_BIGENDIAN
4446 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
4447 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
4448#else
4449 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4450 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
4451#endif
4452 } else {
pbrook5579c7f2009-04-11 14:47:08 +00004453 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00004454 (addr & ~TARGET_PAGE_MASK);
4455 stq_p(ptr, val);
4456 }
4457}
4458
bellard8df1cd02005-01-28 22:37:22 +00004459/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004460static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val,
4461 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00004462{
4463 int io_index;
4464 uint8_t *ptr;
4465 unsigned long pd;
4466 PhysPageDesc *p;
4467
4468 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4469 if (!p) {
4470 pd = IO_MEM_UNASSIGNED;
4471 } else {
4472 pd = p->phys_offset;
4473 }
ths3b46e622007-09-17 08:09:54 +00004474
bellard3a7d9292005-08-21 09:26:42 +00004475 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00004476 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004477 if (p)
4478 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004479#if defined(TARGET_WORDS_BIGENDIAN)
4480 if (endian == DEVICE_LITTLE_ENDIAN) {
4481 val = bswap32(val);
4482 }
4483#else
4484 if (endian == DEVICE_BIG_ENDIAN) {
4485 val = bswap32(val);
4486 }
4487#endif
bellard8df1cd02005-01-28 22:37:22 +00004488 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4489 } else {
4490 unsigned long addr1;
4491 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4492 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004493 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004494 switch (endian) {
4495 case DEVICE_LITTLE_ENDIAN:
4496 stl_le_p(ptr, val);
4497 break;
4498 case DEVICE_BIG_ENDIAN:
4499 stl_be_p(ptr, val);
4500 break;
4501 default:
4502 stl_p(ptr, val);
4503 break;
4504 }
bellard3a7d9292005-08-21 09:26:42 +00004505 if (!cpu_physical_memory_is_dirty(addr1)) {
4506 /* invalidate code */
4507 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4508 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004509 cpu_physical_memory_set_dirty_flags(addr1,
4510 (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00004511 }
bellard8df1cd02005-01-28 22:37:22 +00004512 }
4513}
4514
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004515void stl_phys(target_phys_addr_t addr, uint32_t val)
4516{
4517 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
4518}
4519
4520void stl_le_phys(target_phys_addr_t addr, uint32_t val)
4521{
4522 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
4523}
4524
4525void stl_be_phys(target_phys_addr_t addr, uint32_t val)
4526{
4527 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
4528}
4529
bellardaab33092005-10-30 20:48:42 +00004530/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004531void stb_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00004532{
4533 uint8_t v = val;
4534 cpu_physical_memory_write(addr, &v, 1);
4535}
4536
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004537/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004538static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val,
4539 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00004540{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004541 int io_index;
4542 uint8_t *ptr;
4543 unsigned long pd;
4544 PhysPageDesc *p;
4545
4546 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4547 if (!p) {
4548 pd = IO_MEM_UNASSIGNED;
4549 } else {
4550 pd = p->phys_offset;
4551 }
4552
4553 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4554 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4555 if (p)
4556 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004557#if defined(TARGET_WORDS_BIGENDIAN)
4558 if (endian == DEVICE_LITTLE_ENDIAN) {
4559 val = bswap16(val);
4560 }
4561#else
4562 if (endian == DEVICE_BIG_ENDIAN) {
4563 val = bswap16(val);
4564 }
4565#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004566 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
4567 } else {
4568 unsigned long addr1;
4569 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4570 /* RAM case */
4571 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004572 switch (endian) {
4573 case DEVICE_LITTLE_ENDIAN:
4574 stw_le_p(ptr, val);
4575 break;
4576 case DEVICE_BIG_ENDIAN:
4577 stw_be_p(ptr, val);
4578 break;
4579 default:
4580 stw_p(ptr, val);
4581 break;
4582 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004583 if (!cpu_physical_memory_is_dirty(addr1)) {
4584 /* invalidate code */
4585 tb_invalidate_phys_page_range(addr1, addr1 + 2, 0);
4586 /* set dirty bit */
4587 cpu_physical_memory_set_dirty_flags(addr1,
4588 (0xff & ~CODE_DIRTY_FLAG));
4589 }
4590 }
bellardaab33092005-10-30 20:48:42 +00004591}
4592
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004593void stw_phys(target_phys_addr_t addr, uint32_t val)
4594{
4595 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
4596}
4597
4598void stw_le_phys(target_phys_addr_t addr, uint32_t val)
4599{
4600 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
4601}
4602
4603void stw_be_phys(target_phys_addr_t addr, uint32_t val)
4604{
4605 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
4606}
4607
bellardaab33092005-10-30 20:48:42 +00004608/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004609void stq_phys(target_phys_addr_t addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00004610{
4611 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01004612 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00004613}
4614
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004615void stq_le_phys(target_phys_addr_t addr, uint64_t val)
4616{
4617 val = cpu_to_le64(val);
4618 cpu_physical_memory_write(addr, &val, 8);
4619}
4620
4621void stq_be_phys(target_phys_addr_t addr, uint64_t val)
4622{
4623 val = cpu_to_be64(val);
4624 cpu_physical_memory_write(addr, &val, 8);
4625}
4626
aliguori5e2972f2009-03-28 17:51:36 +00004627/* virtual memory access for debug (includes writing to ROM) */
ths5fafdf22007-09-16 21:08:06 +00004628int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00004629 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00004630{
4631 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05004632 target_phys_addr_t phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00004633 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00004634
4635 while (len > 0) {
4636 page = addr & TARGET_PAGE_MASK;
4637 phys_addr = cpu_get_phys_page_debug(env, page);
4638 /* if no physical page mapped, return an error */
4639 if (phys_addr == -1)
4640 return -1;
4641 l = (page + TARGET_PAGE_SIZE) - addr;
4642 if (l > len)
4643 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00004644 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00004645 if (is_write)
4646 cpu_physical_memory_write_rom(phys_addr, buf, l);
4647 else
aliguori5e2972f2009-03-28 17:51:36 +00004648 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00004649 len -= l;
4650 buf += l;
4651 addr += l;
4652 }
4653 return 0;
4654}
Paul Brooka68fe892010-03-01 00:08:59 +00004655#endif
bellard13eb76e2004-01-24 15:23:36 +00004656
pbrook2e70f6e2008-06-29 01:03:05 +00004657/* in deterministic execution mode, instructions doing device I/Os
4658 must be at the end of the TB */
4659void cpu_io_recompile(CPUState *env, void *retaddr)
4660{
4661 TranslationBlock *tb;
4662 uint32_t n, cflags;
4663 target_ulong pc, cs_base;
4664 uint64_t flags;
4665
4666 tb = tb_find_pc((unsigned long)retaddr);
4667 if (!tb) {
4668 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
4669 retaddr);
4670 }
4671 n = env->icount_decr.u16.low + tb->icount;
Stefan Weil618ba8e2011-04-18 06:39:53 +00004672 cpu_restore_state(tb, env, (unsigned long)retaddr);
pbrook2e70f6e2008-06-29 01:03:05 +00004673 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00004674 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00004675 n = n - env->icount_decr.u16.low;
4676 /* Generate a new TB ending on the I/O insn. */
4677 n++;
4678 /* On MIPS and SH, delay slot instructions can only be restarted if
4679 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00004680 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00004681 branch. */
4682#if defined(TARGET_MIPS)
4683 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
4684 env->active_tc.PC -= 4;
4685 env->icount_decr.u16.low++;
4686 env->hflags &= ~MIPS_HFLAG_BMASK;
4687 }
4688#elif defined(TARGET_SH4)
4689 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
4690 && n > 1) {
4691 env->pc -= 2;
4692 env->icount_decr.u16.low++;
4693 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
4694 }
4695#endif
4696 /* This should never happen. */
4697 if (n > CF_COUNT_MASK)
4698 cpu_abort(env, "TB too big during recompile");
4699
4700 cflags = n | CF_LAST_IO;
4701 pc = tb->pc;
4702 cs_base = tb->cs_base;
4703 flags = tb->flags;
4704 tb_phys_invalidate(tb, -1);
4705 /* FIXME: In theory this could raise an exception. In practice
4706 we have already translated the block once so it's probably ok. */
4707 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00004708 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00004709 the first in the TB) then we end up generating a whole new TB and
4710 repeating the fault, which is horribly inefficient.
4711 Better would be to execute just this insn uncached, or generate a
4712 second new TB. */
4713 cpu_resume_from_signal(env, NULL);
4714}
4715
Paul Brookb3755a92010-03-12 16:54:58 +00004716#if !defined(CONFIG_USER_ONLY)
4717
Stefan Weil055403b2010-10-22 23:03:32 +02004718void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
bellarde3db7222005-01-26 22:00:47 +00004719{
4720 int i, target_code_size, max_target_code_size;
4721 int direct_jmp_count, direct_jmp2_count, cross_page;
4722 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00004723
bellarde3db7222005-01-26 22:00:47 +00004724 target_code_size = 0;
4725 max_target_code_size = 0;
4726 cross_page = 0;
4727 direct_jmp_count = 0;
4728 direct_jmp2_count = 0;
4729 for(i = 0; i < nb_tbs; i++) {
4730 tb = &tbs[i];
4731 target_code_size += tb->size;
4732 if (tb->size > max_target_code_size)
4733 max_target_code_size = tb->size;
4734 if (tb->page_addr[1] != -1)
4735 cross_page++;
4736 if (tb->tb_next_offset[0] != 0xffff) {
4737 direct_jmp_count++;
4738 if (tb->tb_next_offset[1] != 0xffff) {
4739 direct_jmp2_count++;
4740 }
4741 }
4742 }
4743 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00004744 cpu_fprintf(f, "Translation buffer state:\n");
Stefan Weil055403b2010-10-22 23:03:32 +02004745 cpu_fprintf(f, "gen code size %td/%ld\n",
bellard26a5f132008-05-28 12:30:31 +00004746 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
4747 cpu_fprintf(f, "TB count %d/%d\n",
4748 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00004749 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00004750 nb_tbs ? target_code_size / nb_tbs : 0,
4751 max_target_code_size);
Stefan Weil055403b2010-10-22 23:03:32 +02004752 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00004753 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
4754 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00004755 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
4756 cross_page,
bellarde3db7222005-01-26 22:00:47 +00004757 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
4758 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00004759 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00004760 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
4761 direct_jmp2_count,
4762 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00004763 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00004764 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
4765 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
4766 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00004767 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00004768}
4769
bellard61382a52003-10-27 21:22:23 +00004770#define MMUSUFFIX _cmmu
Blue Swirl39171492011-09-21 18:13:16 +00004771#undef GETPC
bellard61382a52003-10-27 21:22:23 +00004772#define GETPC() NULL
4773#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00004774#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00004775
4776#define SHIFT 0
4777#include "softmmu_template.h"
4778
4779#define SHIFT 1
4780#include "softmmu_template.h"
4781
4782#define SHIFT 2
4783#include "softmmu_template.h"
4784
4785#define SHIFT 3
4786#include "softmmu_template.h"
4787
4788#undef env
4789
4790#endif