blob: fbcb1bf02810e82e55e53d49d79be7f3432e108c [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;
Alex Rozenman5ab97b72011-12-13 12:52:08 +0200421 int first_index = index & ~(L2_SIZE - 1);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800422
423 if (!alloc) {
bellard108c49b2005-07-24 12:55:09 +0000424 return NULL;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800425 }
426
Anthony Liguori7267c092011-08-20 22:09:37 -0500427 *lp = pd = g_malloc(sizeof(PhysPageDesc) * L2_SIZE);
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800428
pbrook67c4d232009-02-23 13:16:07 +0000429 for (i = 0; i < L2_SIZE; i++) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800430 pd[i].phys_offset = IO_MEM_UNASSIGNED;
Alex Rozenman5ab97b72011-12-13 12:52:08 +0200431 pd[i].region_offset = (first_index + i) << TARGET_PAGE_BITS;
pbrook67c4d232009-02-23 13:16:07 +0000432 }
bellard92e873b2004-05-21 14:52:29 +0000433 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800434
435 return pd + (index & (L2_SIZE - 1));
bellard92e873b2004-05-21 14:52:29 +0000436}
437
Anthony Liguoric227f092009-10-01 16:12:16 -0500438static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
bellard92e873b2004-05-21 14:52:29 +0000439{
bellard108c49b2005-07-24 12:55:09 +0000440 return phys_page_find_alloc(index, 0);
bellard92e873b2004-05-21 14:52:29 +0000441}
442
Anthony Liguoric227f092009-10-01 16:12:16 -0500443static void tlb_protect_code(ram_addr_t ram_addr);
444static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +0000445 target_ulong vaddr);
pbrookc8a706f2008-06-02 16:16:42 +0000446#define mmap_lock() do { } while(0)
447#define mmap_unlock() do { } while(0)
bellard9fa3e852004-01-04 18:06:42 +0000448#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000449
bellard43694152008-05-29 09:35:57 +0000450#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
451
452#if defined(CONFIG_USER_ONLY)
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100453/* Currently it is not recommended to allocate big chunks of data in
bellard43694152008-05-29 09:35:57 +0000454 user mode. It will change when a dedicated libc will be used */
455#define USE_STATIC_CODE_GEN_BUFFER
456#endif
457
458#ifdef USE_STATIC_CODE_GEN_BUFFER
Aurelien Jarnoebf50fb2010-03-29 02:12:51 +0200459static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
460 __attribute__((aligned (CODE_GEN_ALIGN)));
bellard43694152008-05-29 09:35:57 +0000461#endif
462
blueswir18fcd3692008-08-17 20:26:25 +0000463static void code_gen_alloc(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000464{
bellard43694152008-05-29 09:35:57 +0000465#ifdef USE_STATIC_CODE_GEN_BUFFER
466 code_gen_buffer = static_code_gen_buffer;
467 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
468 map_exec(code_gen_buffer, code_gen_buffer_size);
469#else
bellard26a5f132008-05-28 12:30:31 +0000470 code_gen_buffer_size = tb_size;
471 if (code_gen_buffer_size == 0) {
bellard43694152008-05-29 09:35:57 +0000472#if defined(CONFIG_USER_ONLY)
bellard43694152008-05-29 09:35:57 +0000473 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
474#else
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100475 /* XXX: needs adjustments */
pbrook94a6b542009-04-11 17:15:54 +0000476 code_gen_buffer_size = (unsigned long)(ram_size / 4);
bellard43694152008-05-29 09:35:57 +0000477#endif
bellard26a5f132008-05-28 12:30:31 +0000478 }
479 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
480 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
481 /* The code gen buffer location may have constraints depending on
482 the host cpu and OS */
483#if defined(__linux__)
484 {
485 int flags;
blueswir1141ac462008-07-26 15:05:57 +0000486 void *start = NULL;
487
bellard26a5f132008-05-28 12:30:31 +0000488 flags = MAP_PRIVATE | MAP_ANONYMOUS;
489#if defined(__x86_64__)
490 flags |= MAP_32BIT;
491 /* Cannot map more than that */
492 if (code_gen_buffer_size > (800 * 1024 * 1024))
493 code_gen_buffer_size = (800 * 1024 * 1024);
blueswir1141ac462008-07-26 15:05:57 +0000494#elif defined(__sparc_v9__)
495 // Map the buffer below 2G, so we can use direct calls and branches
496 flags |= MAP_FIXED;
497 start = (void *) 0x60000000UL;
498 if (code_gen_buffer_size > (512 * 1024 * 1024))
499 code_gen_buffer_size = (512 * 1024 * 1024);
balrog1cb06612008-12-01 02:10:17 +0000500#elif defined(__arm__)
Dr. David Alan Gilbert222f23f2011-12-12 16:37:31 +0100501 /* Keep the buffer no bigger than 16GB to branch between blocks */
balrog1cb06612008-12-01 02:10:17 +0000502 if (code_gen_buffer_size > 16 * 1024 * 1024)
503 code_gen_buffer_size = 16 * 1024 * 1024;
Richard Hendersoneba0b892010-06-04 12:14:14 -0700504#elif defined(__s390x__)
505 /* Map the buffer so that we can use direct calls and branches. */
506 /* We have a +- 4GB range on the branches; leave some slop. */
507 if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) {
508 code_gen_buffer_size = 3ul * 1024 * 1024 * 1024;
509 }
510 start = (void *)0x90000000UL;
bellard26a5f132008-05-28 12:30:31 +0000511#endif
blueswir1141ac462008-07-26 15:05:57 +0000512 code_gen_buffer = mmap(start, code_gen_buffer_size,
513 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard26a5f132008-05-28 12:30:31 +0000514 flags, -1, 0);
515 if (code_gen_buffer == MAP_FAILED) {
516 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
517 exit(1);
518 }
519 }
Bradcbb608a2010-12-20 21:25:40 -0500520#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
Tobias Nygren9f4b09a2011-08-07 09:57:05 +0000521 || defined(__DragonFly__) || defined(__OpenBSD__) \
522 || defined(__NetBSD__)
aliguori06e67a82008-09-27 15:32:41 +0000523 {
524 int flags;
525 void *addr = NULL;
526 flags = MAP_PRIVATE | MAP_ANONYMOUS;
527#if defined(__x86_64__)
528 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
529 * 0x40000000 is free */
530 flags |= MAP_FIXED;
531 addr = (void *)0x40000000;
532 /* Cannot map more than that */
533 if (code_gen_buffer_size > (800 * 1024 * 1024))
534 code_gen_buffer_size = (800 * 1024 * 1024);
Blue Swirl4cd31ad2011-01-16 08:32:27 +0000535#elif defined(__sparc_v9__)
536 // Map the buffer below 2G, so we can use direct calls and branches
537 flags |= MAP_FIXED;
538 addr = (void *) 0x60000000UL;
539 if (code_gen_buffer_size > (512 * 1024 * 1024)) {
540 code_gen_buffer_size = (512 * 1024 * 1024);
541 }
aliguori06e67a82008-09-27 15:32:41 +0000542#endif
543 code_gen_buffer = mmap(addr, code_gen_buffer_size,
544 PROT_WRITE | PROT_READ | PROT_EXEC,
545 flags, -1, 0);
546 if (code_gen_buffer == MAP_FAILED) {
547 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
548 exit(1);
549 }
550 }
bellard26a5f132008-05-28 12:30:31 +0000551#else
Anthony Liguori7267c092011-08-20 22:09:37 -0500552 code_gen_buffer = g_malloc(code_gen_buffer_size);
bellard26a5f132008-05-28 12:30:31 +0000553 map_exec(code_gen_buffer, code_gen_buffer_size);
554#endif
bellard43694152008-05-29 09:35:57 +0000555#endif /* !USE_STATIC_CODE_GEN_BUFFER */
bellard26a5f132008-05-28 12:30:31 +0000556 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
Peter Maydella884da82011-06-22 11:58:25 +0100557 code_gen_buffer_max_size = code_gen_buffer_size -
558 (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
bellard26a5f132008-05-28 12:30:31 +0000559 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
Anthony Liguori7267c092011-08-20 22:09:37 -0500560 tbs = g_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
bellard26a5f132008-05-28 12:30:31 +0000561}
562
563/* Must be called before using the QEMU cpus. 'tb_size' is the size
564 (in bytes) allocated to the translation buffer. Zero means default
565 size. */
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200566void tcg_exec_init(unsigned long tb_size)
bellard26a5f132008-05-28 12:30:31 +0000567{
bellard26a5f132008-05-28 12:30:31 +0000568 cpu_gen_init();
569 code_gen_alloc(tb_size);
570 code_gen_ptr = code_gen_buffer;
bellard43694152008-05-29 09:35:57 +0000571 page_init();
Richard Henderson9002ec72010-05-06 08:50:41 -0700572#if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
573 /* There's no guest base to take into account, so go ahead and
574 initialize the prologue now. */
575 tcg_prologue_init(&tcg_ctx);
576#endif
bellard26a5f132008-05-28 12:30:31 +0000577}
578
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200579bool tcg_enabled(void)
580{
581 return code_gen_buffer != NULL;
582}
583
584void cpu_exec_init_all(void)
585{
586#if !defined(CONFIG_USER_ONLY)
587 memory_map_init();
588 io_mem_init();
589#endif
590}
591
pbrook9656f322008-07-01 20:01:19 +0000592#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
593
Juan Quintelae59fb372009-09-29 22:48:21 +0200594static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200595{
596 CPUState *env = opaque;
597
aurel323098dba2009-03-07 21:28:24 +0000598 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
599 version_id is increased. */
600 env->interrupt_request &= ~0x01;
pbrook9656f322008-07-01 20:01:19 +0000601 tlb_flush(env, 1);
602
603 return 0;
604}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200605
606static const VMStateDescription vmstate_cpu_common = {
607 .name = "cpu_common",
608 .version_id = 1,
609 .minimum_version_id = 1,
610 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200611 .post_load = cpu_common_post_load,
612 .fields = (VMStateField []) {
613 VMSTATE_UINT32(halted, CPUState),
614 VMSTATE_UINT32(interrupt_request, CPUState),
615 VMSTATE_END_OF_LIST()
616 }
617};
pbrook9656f322008-07-01 20:01:19 +0000618#endif
619
Glauber Costa950f1472009-06-09 12:15:18 -0400620CPUState *qemu_get_cpu(int cpu)
621{
622 CPUState *env = first_cpu;
623
624 while (env) {
625 if (env->cpu_index == cpu)
626 break;
627 env = env->next_cpu;
628 }
629
630 return env;
631}
632
bellard6a00d602005-11-21 23:25:50 +0000633void cpu_exec_init(CPUState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000634{
bellard6a00d602005-11-21 23:25:50 +0000635 CPUState **penv;
636 int cpu_index;
637
pbrookc2764712009-03-07 15:24:59 +0000638#if defined(CONFIG_USER_ONLY)
639 cpu_list_lock();
640#endif
bellard6a00d602005-11-21 23:25:50 +0000641 env->next_cpu = NULL;
642 penv = &first_cpu;
643 cpu_index = 0;
644 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700645 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000646 cpu_index++;
647 }
648 env->cpu_index = cpu_index;
aliguori268a3622009-04-21 22:30:27 +0000649 env->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000650 QTAILQ_INIT(&env->breakpoints);
651 QTAILQ_INIT(&env->watchpoints);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100652#ifndef CONFIG_USER_ONLY
653 env->thread_id = qemu_get_thread_id();
654#endif
bellard6a00d602005-11-21 23:25:50 +0000655 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000656#if defined(CONFIG_USER_ONLY)
657 cpu_list_unlock();
658#endif
pbrookb3c77242008-06-30 16:31:04 +0000659#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600660 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
661 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000662 cpu_save, cpu_load, env);
663#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000664}
665
Tristan Gingoldd1a1eb72011-02-10 10:04:57 +0100666/* Allocate a new translation block. Flush the translation buffer if
667 too many translation blocks or too much generated code. */
668static TranslationBlock *tb_alloc(target_ulong pc)
669{
670 TranslationBlock *tb;
671
672 if (nb_tbs >= code_gen_max_blocks ||
673 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
674 return NULL;
675 tb = &tbs[nb_tbs++];
676 tb->pc = pc;
677 tb->cflags = 0;
678 return tb;
679}
680
681void tb_free(TranslationBlock *tb)
682{
683 /* In practice this is mostly used for single use temporary TB
684 Ignore the hard cases and just back up if this TB happens to
685 be the last one generated. */
686 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
687 code_gen_ptr = tb->tc_ptr;
688 nb_tbs--;
689 }
690}
691
bellard9fa3e852004-01-04 18:06:42 +0000692static inline void invalidate_page_bitmap(PageDesc *p)
693{
694 if (p->code_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500695 g_free(p->code_bitmap);
bellard9fa3e852004-01-04 18:06:42 +0000696 p->code_bitmap = NULL;
697 }
698 p->code_write_count = 0;
699}
700
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800701/* Set to NULL all the 'first_tb' fields in all PageDescs. */
702
703static void page_flush_tb_1 (int level, void **lp)
704{
705 int i;
706
707 if (*lp == NULL) {
708 return;
709 }
710 if (level == 0) {
711 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000712 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800713 pd[i].first_tb = NULL;
714 invalidate_page_bitmap(pd + i);
715 }
716 } else {
717 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +0000718 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800719 page_flush_tb_1 (level - 1, pp + i);
720 }
721 }
722}
723
bellardfd6ce8f2003-05-14 19:00:11 +0000724static void page_flush_tb(void)
725{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -0800726 int i;
727 for (i = 0; i < V_L1_SIZE; i++) {
728 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
bellardfd6ce8f2003-05-14 19:00:11 +0000729 }
730}
731
732/* flush all the translation blocks */
bellardd4e81642003-05-25 16:46:15 +0000733/* XXX: tb_flush is currently not thread safe */
bellard6a00d602005-11-21 23:25:50 +0000734void tb_flush(CPUState *env1)
bellardfd6ce8f2003-05-14 19:00:11 +0000735{
bellard6a00d602005-11-21 23:25:50 +0000736 CPUState *env;
bellard01243112004-01-04 15:48:17 +0000737#if defined(DEBUG_FLUSH)
blueswir1ab3d1722007-11-04 07:31:40 +0000738 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
739 (unsigned long)(code_gen_ptr - code_gen_buffer),
740 nb_tbs, nb_tbs > 0 ?
741 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
bellardfd6ce8f2003-05-14 19:00:11 +0000742#endif
bellard26a5f132008-05-28 12:30:31 +0000743 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
pbrooka208e542008-03-31 17:07:36 +0000744 cpu_abort(env1, "Internal error: code buffer overflow\n");
745
bellardfd6ce8f2003-05-14 19:00:11 +0000746 nb_tbs = 0;
ths3b46e622007-09-17 08:09:54 +0000747
bellard6a00d602005-11-21 23:25:50 +0000748 for(env = first_cpu; env != NULL; env = env->next_cpu) {
749 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
750 }
bellard9fa3e852004-01-04 18:06:42 +0000751
bellard8a8a6082004-10-03 13:36:49 +0000752 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
bellardfd6ce8f2003-05-14 19:00:11 +0000753 page_flush_tb();
bellard9fa3e852004-01-04 18:06:42 +0000754
bellardfd6ce8f2003-05-14 19:00:11 +0000755 code_gen_ptr = code_gen_buffer;
bellardd4e81642003-05-25 16:46:15 +0000756 /* XXX: flush processor icache at this point if cache flush is
757 expensive */
bellarde3db7222005-01-26 22:00:47 +0000758 tb_flush_count++;
bellardfd6ce8f2003-05-14 19:00:11 +0000759}
760
761#ifdef DEBUG_TB_CHECK
762
j_mayerbc98a7e2007-04-04 07:55:12 +0000763static void tb_invalidate_check(target_ulong address)
bellardfd6ce8f2003-05-14 19:00:11 +0000764{
765 TranslationBlock *tb;
766 int i;
767 address &= TARGET_PAGE_MASK;
pbrook99773bd2006-04-16 15:14:59 +0000768 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
769 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000770 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
771 address >= tb->pc + tb->size)) {
Blue Swirl0bf9e312009-07-20 17:19:25 +0000772 printf("ERROR invalidate: address=" TARGET_FMT_lx
773 " PC=%08lx size=%04x\n",
pbrook99773bd2006-04-16 15:14:59 +0000774 address, (long)tb->pc, tb->size);
bellardfd6ce8f2003-05-14 19:00:11 +0000775 }
776 }
777 }
778}
779
780/* verify that all the pages have correct rights for code */
781static void tb_page_check(void)
782{
783 TranslationBlock *tb;
784 int i, flags1, flags2;
ths3b46e622007-09-17 08:09:54 +0000785
pbrook99773bd2006-04-16 15:14:59 +0000786 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
787 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
bellardfd6ce8f2003-05-14 19:00:11 +0000788 flags1 = page_get_flags(tb->pc);
789 flags2 = page_get_flags(tb->pc + tb->size - 1);
790 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
791 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
pbrook99773bd2006-04-16 15:14:59 +0000792 (long)tb->pc, tb->size, flags1, flags2);
bellardfd6ce8f2003-05-14 19:00:11 +0000793 }
794 }
795 }
796}
797
798#endif
799
800/* invalidate one TB */
801static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
802 int next_offset)
803{
804 TranslationBlock *tb1;
805 for(;;) {
806 tb1 = *ptb;
807 if (tb1 == tb) {
808 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
809 break;
810 }
811 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
812 }
813}
814
bellard9fa3e852004-01-04 18:06:42 +0000815static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
816{
817 TranslationBlock *tb1;
818 unsigned int n1;
819
820 for(;;) {
821 tb1 = *ptb;
822 n1 = (long)tb1 & 3;
823 tb1 = (TranslationBlock *)((long)tb1 & ~3);
824 if (tb1 == tb) {
825 *ptb = tb1->page_next[n1];
826 break;
827 }
828 ptb = &tb1->page_next[n1];
829 }
830}
831
bellardd4e81642003-05-25 16:46:15 +0000832static inline void tb_jmp_remove(TranslationBlock *tb, int n)
833{
834 TranslationBlock *tb1, **ptb;
835 unsigned int n1;
836
837 ptb = &tb->jmp_next[n];
838 tb1 = *ptb;
839 if (tb1) {
840 /* find tb(n) in circular list */
841 for(;;) {
842 tb1 = *ptb;
843 n1 = (long)tb1 & 3;
844 tb1 = (TranslationBlock *)((long)tb1 & ~3);
845 if (n1 == n && tb1 == tb)
846 break;
847 if (n1 == 2) {
848 ptb = &tb1->jmp_first;
849 } else {
850 ptb = &tb1->jmp_next[n1];
851 }
852 }
853 /* now we can suppress tb(n) from the list */
854 *ptb = tb->jmp_next[n];
855
856 tb->jmp_next[n] = NULL;
857 }
858}
859
860/* reset the jump entry 'n' of a TB so that it is not chained to
861 another TB */
862static inline void tb_reset_jump(TranslationBlock *tb, int n)
863{
864 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
865}
866
Paul Brook41c1b1c2010-03-12 16:54:58 +0000867void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +0000868{
bellard6a00d602005-11-21 23:25:50 +0000869 CPUState *env;
bellardfd6ce8f2003-05-14 19:00:11 +0000870 PageDesc *p;
bellard8a40a182005-11-20 10:35:40 +0000871 unsigned int h, n1;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000872 tb_page_addr_t phys_pc;
bellard8a40a182005-11-20 10:35:40 +0000873 TranslationBlock *tb1, *tb2;
ths3b46e622007-09-17 08:09:54 +0000874
bellard9fa3e852004-01-04 18:06:42 +0000875 /* remove the TB from the hash list */
876 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
877 h = tb_phys_hash_func(phys_pc);
ths5fafdf22007-09-16 21:08:06 +0000878 tb_remove(&tb_phys_hash[h], tb,
bellard9fa3e852004-01-04 18:06:42 +0000879 offsetof(TranslationBlock, phys_hash_next));
bellardfd6ce8f2003-05-14 19:00:11 +0000880
bellard9fa3e852004-01-04 18:06:42 +0000881 /* remove the TB from the page list */
882 if (tb->page_addr[0] != page_addr) {
883 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
884 tb_page_remove(&p->first_tb, tb);
885 invalidate_page_bitmap(p);
886 }
887 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
888 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
889 tb_page_remove(&p->first_tb, tb);
890 invalidate_page_bitmap(p);
891 }
892
bellard8a40a182005-11-20 10:35:40 +0000893 tb_invalidated_flag = 1;
894
895 /* remove the TB from the hash list */
896 h = tb_jmp_cache_hash_func(tb->pc);
bellard6a00d602005-11-21 23:25:50 +0000897 for(env = first_cpu; env != NULL; env = env->next_cpu) {
898 if (env->tb_jmp_cache[h] == tb)
899 env->tb_jmp_cache[h] = NULL;
900 }
bellard8a40a182005-11-20 10:35:40 +0000901
902 /* suppress this TB from the two jump lists */
903 tb_jmp_remove(tb, 0);
904 tb_jmp_remove(tb, 1);
905
906 /* suppress any remaining jumps to this TB */
907 tb1 = tb->jmp_first;
908 for(;;) {
909 n1 = (long)tb1 & 3;
910 if (n1 == 2)
911 break;
912 tb1 = (TranslationBlock *)((long)tb1 & ~3);
913 tb2 = tb1->jmp_next[n1];
914 tb_reset_jump(tb1, n1);
915 tb1->jmp_next[n1] = NULL;
916 tb1 = tb2;
917 }
918 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
919
bellarde3db7222005-01-26 22:00:47 +0000920 tb_phys_invalidate_count++;
bellard9fa3e852004-01-04 18:06:42 +0000921}
922
923static inline void set_bits(uint8_t *tab, int start, int len)
924{
925 int end, mask, end1;
926
927 end = start + len;
928 tab += start >> 3;
929 mask = 0xff << (start & 7);
930 if ((start & ~7) == (end & ~7)) {
931 if (start < end) {
932 mask &= ~(0xff << (end & 7));
933 *tab |= mask;
934 }
935 } else {
936 *tab++ |= mask;
937 start = (start + 8) & ~7;
938 end1 = end & ~7;
939 while (start < end1) {
940 *tab++ = 0xff;
941 start += 8;
942 }
943 if (start < end) {
944 mask = ~(0xff << (end & 7));
945 *tab |= mask;
946 }
947 }
948}
949
950static void build_page_bitmap(PageDesc *p)
951{
952 int n, tb_start, tb_end;
953 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +0000954
Anthony Liguori7267c092011-08-20 22:09:37 -0500955 p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
bellard9fa3e852004-01-04 18:06:42 +0000956
957 tb = p->first_tb;
958 while (tb != NULL) {
959 n = (long)tb & 3;
960 tb = (TranslationBlock *)((long)tb & ~3);
961 /* NOTE: this is subtle as a TB may span two physical pages */
962 if (n == 0) {
963 /* NOTE: tb_end may be after the end of the page, but
964 it is not a problem */
965 tb_start = tb->pc & ~TARGET_PAGE_MASK;
966 tb_end = tb_start + tb->size;
967 if (tb_end > TARGET_PAGE_SIZE)
968 tb_end = TARGET_PAGE_SIZE;
969 } else {
970 tb_start = 0;
971 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
972 }
973 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
974 tb = tb->page_next[n];
975 }
976}
977
pbrook2e70f6e2008-06-29 01:03:05 +0000978TranslationBlock *tb_gen_code(CPUState *env,
979 target_ulong pc, target_ulong cs_base,
980 int flags, int cflags)
bellardd720b932004-04-25 17:57:43 +0000981{
982 TranslationBlock *tb;
983 uint8_t *tc_ptr;
Paul Brook41c1b1c2010-03-12 16:54:58 +0000984 tb_page_addr_t phys_pc, phys_page2;
985 target_ulong virt_page2;
bellardd720b932004-04-25 17:57:43 +0000986 int code_gen_size;
987
Paul Brook41c1b1c2010-03-12 16:54:58 +0000988 phys_pc = get_page_addr_code(env, pc);
bellardc27004e2005-01-03 23:35:10 +0000989 tb = tb_alloc(pc);
bellardd720b932004-04-25 17:57:43 +0000990 if (!tb) {
991 /* flush must be done */
992 tb_flush(env);
993 /* cannot fail at this point */
bellardc27004e2005-01-03 23:35:10 +0000994 tb = tb_alloc(pc);
pbrook2e70f6e2008-06-29 01:03:05 +0000995 /* Don't forget to invalidate previous TB info. */
996 tb_invalidated_flag = 1;
bellardd720b932004-04-25 17:57:43 +0000997 }
998 tc_ptr = code_gen_ptr;
999 tb->tc_ptr = tc_ptr;
1000 tb->cs_base = cs_base;
1001 tb->flags = flags;
1002 tb->cflags = cflags;
blueswir1d07bde82007-12-11 19:35:45 +00001003 cpu_gen_code(env, tb, &code_gen_size);
bellardd720b932004-04-25 17:57:43 +00001004 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 +00001005
bellardd720b932004-04-25 17:57:43 +00001006 /* check next page if needed */
bellardc27004e2005-01-03 23:35:10 +00001007 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
bellardd720b932004-04-25 17:57:43 +00001008 phys_page2 = -1;
bellardc27004e2005-01-03 23:35:10 +00001009 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
Paul Brook41c1b1c2010-03-12 16:54:58 +00001010 phys_page2 = get_page_addr_code(env, virt_page2);
bellardd720b932004-04-25 17:57:43 +00001011 }
Paul Brook41c1b1c2010-03-12 16:54:58 +00001012 tb_link_page(tb, phys_pc, phys_page2);
pbrook2e70f6e2008-06-29 01:03:05 +00001013 return tb;
bellardd720b932004-04-25 17:57:43 +00001014}
ths3b46e622007-09-17 08:09:54 +00001015
bellard9fa3e852004-01-04 18:06:42 +00001016/* invalidate all TBs which intersect with the target physical page
1017 starting in range [start;end[. NOTE: start and end must refer to
bellardd720b932004-04-25 17:57:43 +00001018 the same physical page. 'is_cpu_write_access' should be true if called
1019 from a real cpu write access: the virtual CPU will exit the current
1020 TB if code is modified inside this TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001021void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
bellardd720b932004-04-25 17:57:43 +00001022 int is_cpu_write_access)
bellard9fa3e852004-01-04 18:06:42 +00001023{
aliguori6b917542008-11-18 19:46:41 +00001024 TranslationBlock *tb, *tb_next, *saved_tb;
bellardd720b932004-04-25 17:57:43 +00001025 CPUState *env = cpu_single_env;
Paul Brook41c1b1c2010-03-12 16:54:58 +00001026 tb_page_addr_t tb_start, tb_end;
aliguori6b917542008-11-18 19:46:41 +00001027 PageDesc *p;
1028 int n;
1029#ifdef TARGET_HAS_PRECISE_SMC
1030 int current_tb_not_found = is_cpu_write_access;
1031 TranslationBlock *current_tb = NULL;
1032 int current_tb_modified = 0;
1033 target_ulong current_pc = 0;
1034 target_ulong current_cs_base = 0;
1035 int current_flags = 0;
1036#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001037
1038 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001039 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001040 return;
ths5fafdf22007-09-16 21:08:06 +00001041 if (!p->code_bitmap &&
bellardd720b932004-04-25 17:57:43 +00001042 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1043 is_cpu_write_access) {
bellard9fa3e852004-01-04 18:06:42 +00001044 /* build code bitmap */
1045 build_page_bitmap(p);
1046 }
1047
1048 /* we remove all the TBs in the range [start, end[ */
1049 /* XXX: see if in some cases it could be faster to invalidate all the code */
1050 tb = p->first_tb;
1051 while (tb != NULL) {
1052 n = (long)tb & 3;
1053 tb = (TranslationBlock *)((long)tb & ~3);
1054 tb_next = tb->page_next[n];
1055 /* NOTE: this is subtle as a TB may span two physical pages */
1056 if (n == 0) {
1057 /* NOTE: tb_end may be after the end of the page, but
1058 it is not a problem */
1059 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1060 tb_end = tb_start + tb->size;
1061 } else {
1062 tb_start = tb->page_addr[1];
1063 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1064 }
1065 if (!(tb_end <= start || tb_start >= end)) {
bellardd720b932004-04-25 17:57:43 +00001066#ifdef TARGET_HAS_PRECISE_SMC
1067 if (current_tb_not_found) {
1068 current_tb_not_found = 0;
1069 current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001070 if (env->mem_io_pc) {
bellardd720b932004-04-25 17:57:43 +00001071 /* now we have a real cpu fault */
pbrook2e70f6e2008-06-29 01:03:05 +00001072 current_tb = tb_find_pc(env->mem_io_pc);
bellardd720b932004-04-25 17:57:43 +00001073 }
1074 }
1075 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001076 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001077 /* If we are modifying the current TB, we must stop
1078 its execution. We could be more precise by checking
1079 that the modification is after the current PC, but it
1080 would require a specialized function to partially
1081 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001082
bellardd720b932004-04-25 17:57:43 +00001083 current_tb_modified = 1;
Stefan Weil618ba8e2011-04-18 06:39:53 +00001084 cpu_restore_state(current_tb, env, env->mem_io_pc);
aliguori6b917542008-11-18 19:46:41 +00001085 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1086 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001087 }
1088#endif /* TARGET_HAS_PRECISE_SMC */
bellard6f5a9f72005-11-26 20:12:28 +00001089 /* we need to do that to handle the case where a signal
1090 occurs while doing tb_phys_invalidate() */
1091 saved_tb = NULL;
1092 if (env) {
1093 saved_tb = env->current_tb;
1094 env->current_tb = NULL;
1095 }
bellard9fa3e852004-01-04 18:06:42 +00001096 tb_phys_invalidate(tb, -1);
bellard6f5a9f72005-11-26 20:12:28 +00001097 if (env) {
1098 env->current_tb = saved_tb;
1099 if (env->interrupt_request && env->current_tb)
1100 cpu_interrupt(env, env->interrupt_request);
1101 }
bellard9fa3e852004-01-04 18:06:42 +00001102 }
1103 tb = tb_next;
1104 }
1105#if !defined(CONFIG_USER_ONLY)
1106 /* if no code remaining, no need to continue to use slow writes */
1107 if (!p->first_tb) {
1108 invalidate_page_bitmap(p);
bellardd720b932004-04-25 17:57:43 +00001109 if (is_cpu_write_access) {
pbrook2e70f6e2008-06-29 01:03:05 +00001110 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
bellardd720b932004-04-25 17:57:43 +00001111 }
1112 }
1113#endif
1114#ifdef TARGET_HAS_PRECISE_SMC
1115 if (current_tb_modified) {
1116 /* we generate a block containing just the instruction
1117 modifying the memory. It will ensure that it cannot modify
1118 itself */
bellardea1c1802004-06-14 18:56:36 +00001119 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001120 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001121 cpu_resume_from_signal(env, NULL);
bellard9fa3e852004-01-04 18:06:42 +00001122 }
1123#endif
1124}
1125
1126/* len must be <= 8 and start must be a multiple of len */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001127static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
bellard9fa3e852004-01-04 18:06:42 +00001128{
1129 PageDesc *p;
1130 int offset, b;
bellard59817cc2004-02-16 22:01:13 +00001131#if 0
bellarda4193c82004-06-03 14:01:43 +00001132 if (1) {
aliguori93fcfe32009-01-15 22:34:14 +00001133 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1134 cpu_single_env->mem_io_vaddr, len,
1135 cpu_single_env->eip,
1136 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
bellard59817cc2004-02-16 22:01:13 +00001137 }
1138#endif
bellard9fa3e852004-01-04 18:06:42 +00001139 p = page_find(start >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001140 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00001141 return;
1142 if (p->code_bitmap) {
1143 offset = start & ~TARGET_PAGE_MASK;
1144 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1145 if (b & ((1 << len) - 1))
1146 goto do_invalidate;
1147 } else {
1148 do_invalidate:
bellardd720b932004-04-25 17:57:43 +00001149 tb_invalidate_phys_page_range(start, start + len, 1);
bellard9fa3e852004-01-04 18:06:42 +00001150 }
1151}
1152
bellard9fa3e852004-01-04 18:06:42 +00001153#if !defined(CONFIG_SOFTMMU)
Paul Brook41c1b1c2010-03-12 16:54:58 +00001154static void tb_invalidate_phys_page(tb_page_addr_t addr,
bellardd720b932004-04-25 17:57:43 +00001155 unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00001156{
aliguori6b917542008-11-18 19:46:41 +00001157 TranslationBlock *tb;
bellard9fa3e852004-01-04 18:06:42 +00001158 PageDesc *p;
aliguori6b917542008-11-18 19:46:41 +00001159 int n;
bellardd720b932004-04-25 17:57:43 +00001160#ifdef TARGET_HAS_PRECISE_SMC
aliguori6b917542008-11-18 19:46:41 +00001161 TranslationBlock *current_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001162 CPUState *env = cpu_single_env;
aliguori6b917542008-11-18 19:46:41 +00001163 int current_tb_modified = 0;
1164 target_ulong current_pc = 0;
1165 target_ulong current_cs_base = 0;
1166 int current_flags = 0;
bellardd720b932004-04-25 17:57:43 +00001167#endif
bellard9fa3e852004-01-04 18:06:42 +00001168
1169 addr &= TARGET_PAGE_MASK;
1170 p = page_find(addr >> TARGET_PAGE_BITS);
ths5fafdf22007-09-16 21:08:06 +00001171 if (!p)
bellardfd6ce8f2003-05-14 19:00:11 +00001172 return;
1173 tb = p->first_tb;
bellardd720b932004-04-25 17:57:43 +00001174#ifdef TARGET_HAS_PRECISE_SMC
1175 if (tb && pc != 0) {
1176 current_tb = tb_find_pc(pc);
1177 }
1178#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001179 while (tb != NULL) {
bellard9fa3e852004-01-04 18:06:42 +00001180 n = (long)tb & 3;
1181 tb = (TranslationBlock *)((long)tb & ~3);
bellardd720b932004-04-25 17:57:43 +00001182#ifdef TARGET_HAS_PRECISE_SMC
1183 if (current_tb == tb &&
pbrook2e70f6e2008-06-29 01:03:05 +00001184 (current_tb->cflags & CF_COUNT_MASK) != 1) {
bellardd720b932004-04-25 17:57:43 +00001185 /* If we are modifying the current TB, we must stop
1186 its execution. We could be more precise by checking
1187 that the modification is after the current PC, but it
1188 would require a specialized function to partially
1189 restore the CPU state */
ths3b46e622007-09-17 08:09:54 +00001190
bellardd720b932004-04-25 17:57:43 +00001191 current_tb_modified = 1;
Stefan Weil618ba8e2011-04-18 06:39:53 +00001192 cpu_restore_state(current_tb, env, pc);
aliguori6b917542008-11-18 19:46:41 +00001193 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1194 &current_flags);
bellardd720b932004-04-25 17:57:43 +00001195 }
1196#endif /* TARGET_HAS_PRECISE_SMC */
bellard9fa3e852004-01-04 18:06:42 +00001197 tb_phys_invalidate(tb, addr);
1198 tb = tb->page_next[n];
bellardfd6ce8f2003-05-14 19:00:11 +00001199 }
1200 p->first_tb = NULL;
bellardd720b932004-04-25 17:57:43 +00001201#ifdef TARGET_HAS_PRECISE_SMC
1202 if (current_tb_modified) {
1203 /* we generate a block containing just the instruction
1204 modifying the memory. It will ensure that it cannot modify
1205 itself */
bellardea1c1802004-06-14 18:56:36 +00001206 env->current_tb = NULL;
pbrook2e70f6e2008-06-29 01:03:05 +00001207 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
bellardd720b932004-04-25 17:57:43 +00001208 cpu_resume_from_signal(env, puc);
1209 }
1210#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001211}
bellard9fa3e852004-01-04 18:06:42 +00001212#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001213
1214/* add the tb in the target page and protect it if necessary */
ths5fafdf22007-09-16 21:08:06 +00001215static inline void tb_alloc_page(TranslationBlock *tb,
Paul Brook41c1b1c2010-03-12 16:54:58 +00001216 unsigned int n, tb_page_addr_t page_addr)
bellardfd6ce8f2003-05-14 19:00:11 +00001217{
1218 PageDesc *p;
Juan Quintela4429ab42011-06-02 01:53:44 +00001219#ifndef CONFIG_USER_ONLY
1220 bool page_already_protected;
1221#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001222
bellard9fa3e852004-01-04 18:06:42 +00001223 tb->page_addr[n] = page_addr;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001224 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
bellard9fa3e852004-01-04 18:06:42 +00001225 tb->page_next[n] = p->first_tb;
Juan Quintela4429ab42011-06-02 01:53:44 +00001226#ifndef CONFIG_USER_ONLY
1227 page_already_protected = p->first_tb != NULL;
1228#endif
bellard9fa3e852004-01-04 18:06:42 +00001229 p->first_tb = (TranslationBlock *)((long)tb | n);
1230 invalidate_page_bitmap(p);
1231
bellard107db442004-06-22 18:48:46 +00001232#if defined(TARGET_HAS_SMC) || 1
bellardd720b932004-04-25 17:57:43 +00001233
bellard9fa3e852004-01-04 18:06:42 +00001234#if defined(CONFIG_USER_ONLY)
bellardfd6ce8f2003-05-14 19:00:11 +00001235 if (p->flags & PAGE_WRITE) {
pbrook53a59602006-03-25 19:31:22 +00001236 target_ulong addr;
1237 PageDesc *p2;
bellard9fa3e852004-01-04 18:06:42 +00001238 int prot;
1239
bellardfd6ce8f2003-05-14 19:00:11 +00001240 /* force the host page as non writable (writes will have a
1241 page fault + mprotect overhead) */
pbrook53a59602006-03-25 19:31:22 +00001242 page_addr &= qemu_host_page_mask;
bellardfd6ce8f2003-05-14 19:00:11 +00001243 prot = 0;
pbrook53a59602006-03-25 19:31:22 +00001244 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1245 addr += TARGET_PAGE_SIZE) {
1246
1247 p2 = page_find (addr >> TARGET_PAGE_BITS);
1248 if (!p2)
1249 continue;
1250 prot |= p2->flags;
1251 p2->flags &= ~PAGE_WRITE;
pbrook53a59602006-03-25 19:31:22 +00001252 }
ths5fafdf22007-09-16 21:08:06 +00001253 mprotect(g2h(page_addr), qemu_host_page_size,
bellardfd6ce8f2003-05-14 19:00:11 +00001254 (prot & PAGE_BITS) & ~PAGE_WRITE);
1255#ifdef DEBUG_TB_INVALIDATE
blueswir1ab3d1722007-11-04 07:31:40 +00001256 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
pbrook53a59602006-03-25 19:31:22 +00001257 page_addr);
bellardfd6ce8f2003-05-14 19:00:11 +00001258#endif
bellardfd6ce8f2003-05-14 19:00:11 +00001259 }
bellard9fa3e852004-01-04 18:06:42 +00001260#else
1261 /* if some code is already present, then the pages are already
1262 protected. So we handle the case where only the first TB is
1263 allocated in a physical page */
Juan Quintela4429ab42011-06-02 01:53:44 +00001264 if (!page_already_protected) {
bellard6a00d602005-11-21 23:25:50 +00001265 tlb_protect_code(page_addr);
bellard9fa3e852004-01-04 18:06:42 +00001266 }
1267#endif
bellardd720b932004-04-25 17:57:43 +00001268
1269#endif /* TARGET_HAS_SMC */
bellardfd6ce8f2003-05-14 19:00:11 +00001270}
1271
bellard9fa3e852004-01-04 18:06:42 +00001272/* add a new TB and link it to the physical page tables. phys_page2 is
1273 (-1) to indicate that only one page contains the TB. */
Paul Brook41c1b1c2010-03-12 16:54:58 +00001274void tb_link_page(TranslationBlock *tb,
1275 tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
bellardd4e81642003-05-25 16:46:15 +00001276{
bellard9fa3e852004-01-04 18:06:42 +00001277 unsigned int h;
1278 TranslationBlock **ptb;
1279
pbrookc8a706f2008-06-02 16:16:42 +00001280 /* Grab the mmap lock to stop another thread invalidating this TB
1281 before we are done. */
1282 mmap_lock();
bellard9fa3e852004-01-04 18:06:42 +00001283 /* add in the physical hash table */
1284 h = tb_phys_hash_func(phys_pc);
1285 ptb = &tb_phys_hash[h];
1286 tb->phys_hash_next = *ptb;
1287 *ptb = tb;
bellardfd6ce8f2003-05-14 19:00:11 +00001288
1289 /* add in the page list */
bellard9fa3e852004-01-04 18:06:42 +00001290 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1291 if (phys_page2 != -1)
1292 tb_alloc_page(tb, 1, phys_page2);
1293 else
1294 tb->page_addr[1] = -1;
bellard9fa3e852004-01-04 18:06:42 +00001295
bellardd4e81642003-05-25 16:46:15 +00001296 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1297 tb->jmp_next[0] = NULL;
1298 tb->jmp_next[1] = NULL;
1299
1300 /* init original jump addresses */
1301 if (tb->tb_next_offset[0] != 0xffff)
1302 tb_reset_jump(tb, 0);
1303 if (tb->tb_next_offset[1] != 0xffff)
1304 tb_reset_jump(tb, 1);
bellard8a40a182005-11-20 10:35:40 +00001305
1306#ifdef DEBUG_TB_CHECK
1307 tb_page_check();
1308#endif
pbrookc8a706f2008-06-02 16:16:42 +00001309 mmap_unlock();
bellardfd6ce8f2003-05-14 19:00:11 +00001310}
1311
bellarda513fe12003-05-27 23:29:48 +00001312/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1313 tb[1].tc_ptr. Return NULL if not found */
1314TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1315{
1316 int m_min, m_max, m;
1317 unsigned long v;
1318 TranslationBlock *tb;
1319
1320 if (nb_tbs <= 0)
1321 return NULL;
1322 if (tc_ptr < (unsigned long)code_gen_buffer ||
1323 tc_ptr >= (unsigned long)code_gen_ptr)
1324 return NULL;
1325 /* binary search (cf Knuth) */
1326 m_min = 0;
1327 m_max = nb_tbs - 1;
1328 while (m_min <= m_max) {
1329 m = (m_min + m_max) >> 1;
1330 tb = &tbs[m];
1331 v = (unsigned long)tb->tc_ptr;
1332 if (v == tc_ptr)
1333 return tb;
1334 else if (tc_ptr < v) {
1335 m_max = m - 1;
1336 } else {
1337 m_min = m + 1;
1338 }
ths5fafdf22007-09-16 21:08:06 +00001339 }
bellarda513fe12003-05-27 23:29:48 +00001340 return &tbs[m_max];
1341}
bellard75012672003-06-21 13:11:07 +00001342
bellardea041c02003-06-25 16:16:50 +00001343static void tb_reset_jump_recursive(TranslationBlock *tb);
1344
1345static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1346{
1347 TranslationBlock *tb1, *tb_next, **ptb;
1348 unsigned int n1;
1349
1350 tb1 = tb->jmp_next[n];
1351 if (tb1 != NULL) {
1352 /* find head of list */
1353 for(;;) {
1354 n1 = (long)tb1 & 3;
1355 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1356 if (n1 == 2)
1357 break;
1358 tb1 = tb1->jmp_next[n1];
1359 }
1360 /* we are now sure now that tb jumps to tb1 */
1361 tb_next = tb1;
1362
1363 /* remove tb from the jmp_first list */
1364 ptb = &tb_next->jmp_first;
1365 for(;;) {
1366 tb1 = *ptb;
1367 n1 = (long)tb1 & 3;
1368 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1369 if (n1 == n && tb1 == tb)
1370 break;
1371 ptb = &tb1->jmp_next[n1];
1372 }
1373 *ptb = tb->jmp_next[n];
1374 tb->jmp_next[n] = NULL;
ths3b46e622007-09-17 08:09:54 +00001375
bellardea041c02003-06-25 16:16:50 +00001376 /* suppress the jump to next tb in generated code */
1377 tb_reset_jump(tb, n);
1378
bellard01243112004-01-04 15:48:17 +00001379 /* suppress jumps in the tb on which we could have jumped */
bellardea041c02003-06-25 16:16:50 +00001380 tb_reset_jump_recursive(tb_next);
1381 }
1382}
1383
1384static void tb_reset_jump_recursive(TranslationBlock *tb)
1385{
1386 tb_reset_jump_recursive2(tb, 0);
1387 tb_reset_jump_recursive2(tb, 1);
1388}
1389
bellard1fddef42005-04-17 19:16:13 +00001390#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +00001391#if defined(CONFIG_USER_ONLY)
1392static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1393{
1394 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1395}
1396#else
bellardd720b932004-04-25 17:57:43 +00001397static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1398{
Anthony Liguoric227f092009-10-01 16:12:16 -05001399 target_phys_addr_t addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00001400 target_ulong pd;
Anthony Liguoric227f092009-10-01 16:12:16 -05001401 ram_addr_t ram_addr;
pbrookc2f07f82006-04-08 17:14:56 +00001402 PhysPageDesc *p;
bellardd720b932004-04-25 17:57:43 +00001403
pbrookc2f07f82006-04-08 17:14:56 +00001404 addr = cpu_get_phys_page_debug(env, pc);
1405 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1406 if (!p) {
1407 pd = IO_MEM_UNASSIGNED;
1408 } else {
1409 pd = p->phys_offset;
1410 }
1411 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
pbrook706cd4b2006-04-08 17:36:21 +00001412 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
bellardd720b932004-04-25 17:57:43 +00001413}
bellardc27004e2005-01-03 23:35:10 +00001414#endif
Paul Brook94df27f2010-02-28 23:47:45 +00001415#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +00001416
Paul Brookc527ee82010-03-01 03:31:14 +00001417#if defined(CONFIG_USER_ONLY)
1418void cpu_watchpoint_remove_all(CPUState *env, int mask)
1419
1420{
1421}
1422
1423int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1424 int flags, CPUWatchpoint **watchpoint)
1425{
1426 return -ENOSYS;
1427}
1428#else
pbrook6658ffb2007-03-16 23:58:11 +00001429/* Add a watchpoint. */
aliguoria1d1bb32008-11-18 20:07:32 +00001430int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1431 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +00001432{
aliguorib4051332008-11-18 20:14:20 +00001433 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +00001434 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001435
aliguorib4051332008-11-18 20:14:20 +00001436 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1437 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1438 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1439 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1440 return -EINVAL;
1441 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001442 wp = g_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +00001443
aliguoria1d1bb32008-11-18 20:07:32 +00001444 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +00001445 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +00001446 wp->flags = flags;
1447
aliguori2dc9f412008-11-18 20:56:59 +00001448 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001449 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001450 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001451 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001452 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001453
pbrook6658ffb2007-03-16 23:58:11 +00001454 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +00001455
1456 if (watchpoint)
1457 *watchpoint = wp;
1458 return 0;
pbrook6658ffb2007-03-16 23:58:11 +00001459}
1460
aliguoria1d1bb32008-11-18 20:07:32 +00001461/* Remove a specific watchpoint. */
1462int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1463 int flags)
pbrook6658ffb2007-03-16 23:58:11 +00001464{
aliguorib4051332008-11-18 20:14:20 +00001465 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +00001466 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +00001467
Blue Swirl72cf2d42009-09-12 07:36:22 +00001468 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001469 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +00001470 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +00001471 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +00001472 return 0;
1473 }
1474 }
aliguoria1d1bb32008-11-18 20:07:32 +00001475 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +00001476}
1477
aliguoria1d1bb32008-11-18 20:07:32 +00001478/* Remove a specific watchpoint by reference. */
1479void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1480{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001481 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +00001482
aliguoria1d1bb32008-11-18 20:07:32 +00001483 tlb_flush_page(env, watchpoint->vaddr);
1484
Anthony Liguori7267c092011-08-20 22:09:37 -05001485 g_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +00001486}
1487
aliguoria1d1bb32008-11-18 20:07:32 +00001488/* Remove all matching watchpoints. */
1489void cpu_watchpoint_remove_all(CPUState *env, int mask)
1490{
aliguoric0ce9982008-11-25 22:13:57 +00001491 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001492
Blue Swirl72cf2d42009-09-12 07:36:22 +00001493 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001494 if (wp->flags & mask)
1495 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +00001496 }
aliguoria1d1bb32008-11-18 20:07:32 +00001497}
Paul Brookc527ee82010-03-01 03:31:14 +00001498#endif
aliguoria1d1bb32008-11-18 20:07:32 +00001499
1500/* Add a breakpoint. */
1501int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1502 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001503{
bellard1fddef42005-04-17 19:16:13 +00001504#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001505 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +00001506
Anthony Liguori7267c092011-08-20 22:09:37 -05001507 bp = g_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +00001508
1509 bp->pc = pc;
1510 bp->flags = flags;
1511
aliguori2dc9f412008-11-18 20:56:59 +00001512 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +00001513 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001514 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +00001515 else
Blue Swirl72cf2d42009-09-12 07:36:22 +00001516 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +00001517
1518 breakpoint_invalidate(env, pc);
1519
1520 if (breakpoint)
1521 *breakpoint = bp;
1522 return 0;
1523#else
1524 return -ENOSYS;
1525#endif
1526}
1527
1528/* Remove a specific breakpoint. */
1529int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1530{
1531#if defined(TARGET_HAS_ICE)
1532 CPUBreakpoint *bp;
1533
Blue Swirl72cf2d42009-09-12 07:36:22 +00001534 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00001535 if (bp->pc == pc && bp->flags == flags) {
1536 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +00001537 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +00001538 }
bellard4c3a88a2003-07-26 12:06:08 +00001539 }
aliguoria1d1bb32008-11-18 20:07:32 +00001540 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +00001541#else
aliguoria1d1bb32008-11-18 20:07:32 +00001542 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +00001543#endif
1544}
1545
aliguoria1d1bb32008-11-18 20:07:32 +00001546/* Remove a specific breakpoint by reference. */
1547void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +00001548{
bellard1fddef42005-04-17 19:16:13 +00001549#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001550 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +00001551
aliguoria1d1bb32008-11-18 20:07:32 +00001552 breakpoint_invalidate(env, breakpoint->pc);
1553
Anthony Liguori7267c092011-08-20 22:09:37 -05001554 g_free(breakpoint);
aliguoria1d1bb32008-11-18 20:07:32 +00001555#endif
1556}
1557
1558/* Remove all matching breakpoints. */
1559void cpu_breakpoint_remove_all(CPUState *env, int mask)
1560{
1561#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +00001562 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +00001563
Blue Swirl72cf2d42009-09-12 07:36:22 +00001564 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +00001565 if (bp->flags & mask)
1566 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +00001567 }
bellard4c3a88a2003-07-26 12:06:08 +00001568#endif
1569}
1570
bellardc33a3462003-07-29 20:50:33 +00001571/* enable or disable single step mode. EXCP_DEBUG is returned by the
1572 CPU loop after each instruction */
1573void cpu_single_step(CPUState *env, int enabled)
1574{
bellard1fddef42005-04-17 19:16:13 +00001575#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +00001576 if (env->singlestep_enabled != enabled) {
1577 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +00001578 if (kvm_enabled())
1579 kvm_update_guest_debug(env, 0);
1580 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01001581 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +00001582 /* XXX: only flush what is necessary */
1583 tb_flush(env);
1584 }
bellardc33a3462003-07-29 20:50:33 +00001585 }
1586#endif
1587}
1588
bellard34865132003-10-05 14:28:56 +00001589/* enable or disable low levels log */
1590void cpu_set_log(int log_flags)
1591{
1592 loglevel = log_flags;
1593 if (loglevel && !logfile) {
pbrook11fcfab2007-07-01 18:21:11 +00001594 logfile = fopen(logfilename, log_append ? "a" : "w");
bellard34865132003-10-05 14:28:56 +00001595 if (!logfile) {
1596 perror(logfilename);
1597 _exit(1);
1598 }
bellard9fa3e852004-01-04 18:06:42 +00001599#if !defined(CONFIG_SOFTMMU)
1600 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1601 {
blueswir1b55266b2008-09-20 08:07:15 +00001602 static char logfile_buf[4096];
bellard9fa3e852004-01-04 18:06:42 +00001603 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1604 }
Stefan Weildaf767b2011-12-03 22:32:37 +01001605#elif defined(_WIN32)
1606 /* Win32 doesn't support line-buffering, so use unbuffered output. */
1607 setvbuf(logfile, NULL, _IONBF, 0);
1608#else
bellard34865132003-10-05 14:28:56 +00001609 setvbuf(logfile, NULL, _IOLBF, 0);
bellard9fa3e852004-01-04 18:06:42 +00001610#endif
pbrooke735b912007-06-30 13:53:24 +00001611 log_append = 1;
1612 }
1613 if (!loglevel && logfile) {
1614 fclose(logfile);
1615 logfile = NULL;
bellard34865132003-10-05 14:28:56 +00001616 }
1617}
1618
1619void cpu_set_log_filename(const char *filename)
1620{
1621 logfilename = strdup(filename);
pbrooke735b912007-06-30 13:53:24 +00001622 if (logfile) {
1623 fclose(logfile);
1624 logfile = NULL;
1625 }
1626 cpu_set_log(loglevel);
bellard34865132003-10-05 14:28:56 +00001627}
bellardc33a3462003-07-29 20:50:33 +00001628
aurel323098dba2009-03-07 21:28:24 +00001629static void cpu_unlink_tb(CPUState *env)
bellardea041c02003-06-25 16:16:50 +00001630{
pbrookd5975362008-06-07 20:50:51 +00001631 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1632 problem and hope the cpu will stop of its own accord. For userspace
1633 emulation this often isn't actually as bad as it sounds. Often
1634 signals are used primarily to interrupt blocking syscalls. */
aurel323098dba2009-03-07 21:28:24 +00001635 TranslationBlock *tb;
Anthony Liguoric227f092009-10-01 16:12:16 -05001636 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
aurel323098dba2009-03-07 21:28:24 +00001637
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001638 spin_lock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001639 tb = env->current_tb;
1640 /* if the cpu is currently executing code, we must unlink it and
1641 all the potentially executing TB */
Riku Voipiof76cfe52009-12-04 15:16:30 +02001642 if (tb) {
aurel323098dba2009-03-07 21:28:24 +00001643 env->current_tb = NULL;
1644 tb_reset_jump_recursive(tb);
aurel323098dba2009-03-07 21:28:24 +00001645 }
Riku Voipiocab1b4b2010-01-20 12:56:27 +02001646 spin_unlock(&interrupt_lock);
aurel323098dba2009-03-07 21:28:24 +00001647}
1648
Jan Kiszka97ffbd82011-04-13 01:32:56 +02001649#ifndef CONFIG_USER_ONLY
aurel323098dba2009-03-07 21:28:24 +00001650/* mask must never be zero, except for A20 change call */
Jan Kiszkaec6959d2011-04-13 01:32:56 +02001651static void tcg_handle_interrupt(CPUState *env, int mask)
aurel323098dba2009-03-07 21:28:24 +00001652{
1653 int old_mask;
1654
1655 old_mask = env->interrupt_request;
1656 env->interrupt_request |= mask;
1657
aliguori8edac962009-04-24 18:03:45 +00001658 /*
1659 * If called from iothread context, wake the target cpu in
1660 * case its halted.
1661 */
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001662 if (!qemu_cpu_is_self(env)) {
aliguori8edac962009-04-24 18:03:45 +00001663 qemu_cpu_kick(env);
1664 return;
1665 }
aliguori8edac962009-04-24 18:03:45 +00001666
pbrook2e70f6e2008-06-29 01:03:05 +00001667 if (use_icount) {
pbrook266910c2008-07-09 15:31:50 +00001668 env->icount_decr.u16.high = 0xffff;
pbrook2e70f6e2008-06-29 01:03:05 +00001669 if (!can_do_io(env)
aurel32be214e62009-03-06 21:48:00 +00001670 && (mask & ~old_mask) != 0) {
pbrook2e70f6e2008-06-29 01:03:05 +00001671 cpu_abort(env, "Raised interrupt while not in I/O function");
1672 }
pbrook2e70f6e2008-06-29 01:03:05 +00001673 } else {
aurel323098dba2009-03-07 21:28:24 +00001674 cpu_unlink_tb(env);
bellardea041c02003-06-25 16:16:50 +00001675 }
1676}
1677
Jan Kiszkaec6959d2011-04-13 01:32:56 +02001678CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
1679
Jan Kiszka97ffbd82011-04-13 01:32:56 +02001680#else /* CONFIG_USER_ONLY */
1681
1682void cpu_interrupt(CPUState *env, int mask)
1683{
1684 env->interrupt_request |= mask;
1685 cpu_unlink_tb(env);
1686}
1687#endif /* CONFIG_USER_ONLY */
1688
bellardb54ad042004-05-20 13:42:52 +00001689void cpu_reset_interrupt(CPUState *env, int mask)
1690{
1691 env->interrupt_request &= ~mask;
1692}
1693
aurel323098dba2009-03-07 21:28:24 +00001694void cpu_exit(CPUState *env)
1695{
1696 env->exit_request = 1;
1697 cpu_unlink_tb(env);
1698}
1699
blueswir1c7cd6a32008-10-02 18:27:46 +00001700const CPULogItem cpu_log_items[] = {
ths5fafdf22007-09-16 21:08:06 +00001701 { CPU_LOG_TB_OUT_ASM, "out_asm",
bellardf193c792004-03-21 17:06:25 +00001702 "show generated host assembly code for each compiled TB" },
1703 { CPU_LOG_TB_IN_ASM, "in_asm",
1704 "show target assembly code for each compiled TB" },
ths5fafdf22007-09-16 21:08:06 +00001705 { CPU_LOG_TB_OP, "op",
bellard57fec1f2008-02-01 10:50:11 +00001706 "show micro ops for each compiled TB" },
bellardf193c792004-03-21 17:06:25 +00001707 { CPU_LOG_TB_OP_OPT, "op_opt",
blueswir1e01a1152008-03-14 17:37:11 +00001708 "show micro ops "
1709#ifdef TARGET_I386
1710 "before eflags optimization and "
bellardf193c792004-03-21 17:06:25 +00001711#endif
blueswir1e01a1152008-03-14 17:37:11 +00001712 "after liveness analysis" },
bellardf193c792004-03-21 17:06:25 +00001713 { CPU_LOG_INT, "int",
1714 "show interrupts/exceptions in short format" },
1715 { CPU_LOG_EXEC, "exec",
1716 "show trace before each executed TB (lots of logs)" },
bellard9fddaa02004-05-21 12:59:32 +00001717 { CPU_LOG_TB_CPU, "cpu",
thse91c8a72007-06-03 13:35:16 +00001718 "show CPU state before block translation" },
bellardf193c792004-03-21 17:06:25 +00001719#ifdef TARGET_I386
1720 { CPU_LOG_PCALL, "pcall",
1721 "show protected mode far calls/returns/exceptions" },
aliguorieca1bdf2009-01-26 19:54:31 +00001722 { CPU_LOG_RESET, "cpu_reset",
1723 "show CPU state before CPU resets" },
bellardf193c792004-03-21 17:06:25 +00001724#endif
bellard8e3a9fd2004-10-09 17:32:58 +00001725#ifdef DEBUG_IOPORT
bellardfd872592004-05-12 19:11:15 +00001726 { CPU_LOG_IOPORT, "ioport",
1727 "show all i/o ports accesses" },
bellard8e3a9fd2004-10-09 17:32:58 +00001728#endif
bellardf193c792004-03-21 17:06:25 +00001729 { 0, NULL, NULL },
1730};
1731
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001732#ifndef CONFIG_USER_ONLY
1733static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
1734 = QLIST_HEAD_INITIALIZER(memory_client_list);
1735
1736static void cpu_notify_set_memory(target_phys_addr_t start_addr,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001737 ram_addr_t size,
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03001738 ram_addr_t phys_offset,
1739 bool log_dirty)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001740{
1741 CPUPhysMemoryClient *client;
1742 QLIST_FOREACH(client, &memory_client_list, list) {
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03001743 client->set_memory(client, start_addr, size, phys_offset, log_dirty);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001744 }
1745}
1746
1747static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001748 target_phys_addr_t end)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001749{
1750 CPUPhysMemoryClient *client;
1751 QLIST_FOREACH(client, &memory_client_list, list) {
1752 int r = client->sync_dirty_bitmap(client, start, end);
1753 if (r < 0)
1754 return r;
1755 }
1756 return 0;
1757}
1758
1759static int cpu_notify_migration_log(int enable)
1760{
1761 CPUPhysMemoryClient *client;
1762 QLIST_FOREACH(client, &memory_client_list, list) {
1763 int r = client->migration_log(client, enable);
1764 if (r < 0)
1765 return r;
1766 }
1767 return 0;
1768}
1769
Alex Williamson2173a752011-05-03 12:36:58 -06001770struct last_map {
1771 target_phys_addr_t start_addr;
1772 ram_addr_t size;
1773 ram_addr_t phys_offset;
1774};
1775
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001776/* The l1_phys_map provides the upper P_L1_BITs of the guest physical
1777 * address. Each intermediate table provides the next L2_BITs of guest
1778 * physical address space. The number of levels vary based on host and
1779 * guest configuration, making it efficient to build the final guest
1780 * physical address by seeding the L1 offset and shifting and adding in
1781 * each L2 offset as we recurse through them. */
Alex Williamson2173a752011-05-03 12:36:58 -06001782static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
1783 void **lp, target_phys_addr_t addr,
1784 struct last_map *map)
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001785{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001786 int i;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001787
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001788 if (*lp == NULL) {
1789 return;
1790 }
1791 if (level == 0) {
1792 PhysPageDesc *pd = *lp;
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001793 addr <<= L2_BITS + TARGET_PAGE_BITS;
Paul Brook7296aba2010-03-14 14:58:46 +00001794 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001795 if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
Alex Williamson2173a752011-05-03 12:36:58 -06001796 target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
1797
1798 if (map->size &&
1799 start_addr == map->start_addr + map->size &&
1800 pd[i].phys_offset == map->phys_offset + map->size) {
1801
1802 map->size += TARGET_PAGE_SIZE;
1803 continue;
1804 } else if (map->size) {
1805 client->set_memory(client, map->start_addr,
1806 map->size, map->phys_offset, false);
1807 }
1808
1809 map->start_addr = start_addr;
1810 map->size = TARGET_PAGE_SIZE;
1811 map->phys_offset = pd[i].phys_offset;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001812 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001813 }
1814 } else {
1815 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00001816 for (i = 0; i < L2_SIZE; ++i) {
Alex Williamson8d4c78e2011-05-03 12:36:46 -06001817 phys_page_for_each_1(client, level - 1, pp + i,
Alex Williamson2173a752011-05-03 12:36:58 -06001818 (addr << L2_BITS) | i, map);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001819 }
1820 }
1821}
1822
1823static void phys_page_for_each(CPUPhysMemoryClient *client)
1824{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001825 int i;
Alex Williamson2173a752011-05-03 12:36:58 -06001826 struct last_map map = { };
1827
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08001828 for (i = 0; i < P_L1_SIZE; ++i) {
1829 phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
Alex Williamson2173a752011-05-03 12:36:58 -06001830 l1_phys_map + i, i, &map);
1831 }
1832 if (map.size) {
1833 client->set_memory(client, map.start_addr, map.size, map.phys_offset,
1834 false);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001835 }
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02001836}
1837
1838void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
1839{
1840 QLIST_INSERT_HEAD(&memory_client_list, client, list);
1841 phys_page_for_each(client);
1842}
1843
1844void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
1845{
1846 QLIST_REMOVE(client, list);
1847}
1848#endif
1849
bellardf193c792004-03-21 17:06:25 +00001850static int cmp1(const char *s1, int n, const char *s2)
1851{
1852 if (strlen(s2) != n)
1853 return 0;
1854 return memcmp(s1, s2, n) == 0;
1855}
ths3b46e622007-09-17 08:09:54 +00001856
bellardf193c792004-03-21 17:06:25 +00001857/* takes a comma separated list of log masks. Return 0 if error. */
1858int cpu_str_to_log_mask(const char *str)
1859{
blueswir1c7cd6a32008-10-02 18:27:46 +00001860 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +00001861 int mask;
1862 const char *p, *p1;
1863
1864 p = str;
1865 mask = 0;
1866 for(;;) {
1867 p1 = strchr(p, ',');
1868 if (!p1)
1869 p1 = p + strlen(p);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001870 if(cmp1(p,p1-p,"all")) {
1871 for(item = cpu_log_items; item->mask != 0; item++) {
1872 mask |= item->mask;
1873 }
1874 } else {
1875 for(item = cpu_log_items; item->mask != 0; item++) {
1876 if (cmp1(p, p1 - p, item->name))
1877 goto found;
1878 }
1879 return 0;
bellardf193c792004-03-21 17:06:25 +00001880 }
bellardf193c792004-03-21 17:06:25 +00001881 found:
1882 mask |= item->mask;
1883 if (*p1 != ',')
1884 break;
1885 p = p1 + 1;
1886 }
1887 return mask;
1888}
bellardea041c02003-06-25 16:16:50 +00001889
bellard75012672003-06-21 13:11:07 +00001890void cpu_abort(CPUState *env, const char *fmt, ...)
1891{
1892 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +00001893 va_list ap2;
bellard75012672003-06-21 13:11:07 +00001894
1895 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +00001896 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +00001897 fprintf(stderr, "qemu: fatal: ");
1898 vfprintf(stderr, fmt, ap);
1899 fprintf(stderr, "\n");
1900#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +00001901 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1902#else
1903 cpu_dump_state(env, stderr, fprintf, 0);
bellard75012672003-06-21 13:11:07 +00001904#endif
aliguori93fcfe32009-01-15 22:34:14 +00001905 if (qemu_log_enabled()) {
1906 qemu_log("qemu: fatal: ");
1907 qemu_log_vprintf(fmt, ap2);
1908 qemu_log("\n");
j_mayerf9373292007-09-29 12:18:20 +00001909#ifdef TARGET_I386
aliguori93fcfe32009-01-15 22:34:14 +00001910 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
j_mayerf9373292007-09-29 12:18:20 +00001911#else
aliguori93fcfe32009-01-15 22:34:14 +00001912 log_cpu_state(env, 0);
j_mayerf9373292007-09-29 12:18:20 +00001913#endif
aliguori31b1a7b2009-01-15 22:35:09 +00001914 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +00001915 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +00001916 }
pbrook493ae1f2007-11-23 16:53:59 +00001917 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +00001918 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +02001919#if defined(CONFIG_USER_ONLY)
1920 {
1921 struct sigaction act;
1922 sigfillset(&act.sa_mask);
1923 act.sa_handler = SIG_DFL;
1924 sigaction(SIGABRT, &act, NULL);
1925 }
1926#endif
bellard75012672003-06-21 13:11:07 +00001927 abort();
1928}
1929
thsc5be9f02007-02-28 20:20:53 +00001930CPUState *cpu_copy(CPUState *env)
1931{
ths01ba9812007-12-09 02:22:57 +00001932 CPUState *new_env = cpu_init(env->cpu_model_str);
thsc5be9f02007-02-28 20:20:53 +00001933 CPUState *next_cpu = new_env->next_cpu;
1934 int cpu_index = new_env->cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001935#if defined(TARGET_HAS_ICE)
1936 CPUBreakpoint *bp;
1937 CPUWatchpoint *wp;
1938#endif
1939
thsc5be9f02007-02-28 20:20:53 +00001940 memcpy(new_env, env, sizeof(CPUState));
aliguori5a38f082009-01-15 20:16:51 +00001941
1942 /* Preserve chaining and index. */
thsc5be9f02007-02-28 20:20:53 +00001943 new_env->next_cpu = next_cpu;
1944 new_env->cpu_index = cpu_index;
aliguori5a38f082009-01-15 20:16:51 +00001945
1946 /* Clone all break/watchpoints.
1947 Note: Once we support ptrace with hw-debug register access, make sure
1948 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001949 QTAILQ_INIT(&env->breakpoints);
1950 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +00001951#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +00001952 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001953 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1954 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001955 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +00001956 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1957 wp->flags, NULL);
1958 }
1959#endif
1960
thsc5be9f02007-02-28 20:20:53 +00001961 return new_env;
1962}
1963
bellard01243112004-01-04 15:48:17 +00001964#if !defined(CONFIG_USER_ONLY)
1965
edgar_igl5c751e92008-05-06 08:44:21 +00001966static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1967{
1968 unsigned int i;
1969
1970 /* Discard jump cache entries for any tb which might potentially
1971 overlap the flushed page. */
1972 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1973 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001974 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001975
1976 i = tb_jmp_cache_hash_page(addr);
1977 memset (&env->tb_jmp_cache[i], 0,
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001978 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
edgar_igl5c751e92008-05-06 08:44:21 +00001979}
1980
Igor Kovalenko08738982009-07-12 02:15:40 +04001981static CPUTLBEntry s_cputlb_empty_entry = {
1982 .addr_read = -1,
1983 .addr_write = -1,
1984 .addr_code = -1,
1985 .addend = -1,
1986};
1987
bellardee8b7022004-02-03 23:35:10 +00001988/* NOTE: if flush_global is true, also flush global entries (not
1989 implemented yet) */
1990void tlb_flush(CPUState *env, int flush_global)
bellard33417e72003-08-10 21:47:01 +00001991{
bellard33417e72003-08-10 21:47:01 +00001992 int i;
bellard01243112004-01-04 15:48:17 +00001993
bellard9fa3e852004-01-04 18:06:42 +00001994#if defined(DEBUG_TLB)
1995 printf("tlb_flush:\n");
1996#endif
bellard01243112004-01-04 15:48:17 +00001997 /* must reset current TB so that interrupts cannot modify the
1998 links while we are modifying them */
1999 env->current_tb = NULL;
2000
bellard33417e72003-08-10 21:47:01 +00002001 for(i = 0; i < CPU_TLB_SIZE; i++) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002002 int mmu_idx;
2003 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
Igor Kovalenko08738982009-07-12 02:15:40 +04002004 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002005 }
bellard33417e72003-08-10 21:47:01 +00002006 }
bellard9fa3e852004-01-04 18:06:42 +00002007
bellard8a40a182005-11-20 10:35:40 +00002008 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
bellard9fa3e852004-01-04 18:06:42 +00002009
Paul Brookd4c430a2010-03-17 02:14:28 +00002010 env->tlb_flush_addr = -1;
2011 env->tlb_flush_mask = 0;
bellarde3db7222005-01-26 22:00:47 +00002012 tlb_flush_count++;
bellard33417e72003-08-10 21:47:01 +00002013}
2014
bellard274da6b2004-05-20 21:56:27 +00002015static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
bellard61382a52003-10-27 21:22:23 +00002016{
ths5fafdf22007-09-16 21:08:06 +00002017 if (addr == (tlb_entry->addr_read &
bellard84b7b8e2005-11-28 21:19:04 +00002018 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00002019 addr == (tlb_entry->addr_write &
bellard84b7b8e2005-11-28 21:19:04 +00002020 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
ths5fafdf22007-09-16 21:08:06 +00002021 addr == (tlb_entry->addr_code &
bellard84b7b8e2005-11-28 21:19:04 +00002022 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Igor Kovalenko08738982009-07-12 02:15:40 +04002023 *tlb_entry = s_cputlb_empty_entry;
bellard84b7b8e2005-11-28 21:19:04 +00002024 }
bellard61382a52003-10-27 21:22:23 +00002025}
2026
bellard2e126692004-04-25 21:28:44 +00002027void tlb_flush_page(CPUState *env, target_ulong addr)
bellard33417e72003-08-10 21:47:01 +00002028{
bellard8a40a182005-11-20 10:35:40 +00002029 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002030 int mmu_idx;
bellard01243112004-01-04 15:48:17 +00002031
bellard9fa3e852004-01-04 18:06:42 +00002032#if defined(DEBUG_TLB)
bellard108c49b2005-07-24 12:55:09 +00002033 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
bellard9fa3e852004-01-04 18:06:42 +00002034#endif
Paul Brookd4c430a2010-03-17 02:14:28 +00002035 /* Check if we need to flush due to large pages. */
2036 if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
2037#if defined(DEBUG_TLB)
2038 printf("tlb_flush_page: forced full flush ("
2039 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
2040 env->tlb_flush_addr, env->tlb_flush_mask);
2041#endif
2042 tlb_flush(env, 1);
2043 return;
2044 }
bellard01243112004-01-04 15:48:17 +00002045 /* must reset current TB so that interrupts cannot modify the
2046 links while we are modifying them */
2047 env->current_tb = NULL;
bellard33417e72003-08-10 21:47:01 +00002048
bellard61382a52003-10-27 21:22:23 +00002049 addr &= TARGET_PAGE_MASK;
bellard33417e72003-08-10 21:47:01 +00002050 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002051 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2052 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
bellard01243112004-01-04 15:48:17 +00002053
edgar_igl5c751e92008-05-06 08:44:21 +00002054 tlb_flush_jmp_cache(env, addr);
bellard9fa3e852004-01-04 18:06:42 +00002055}
2056
bellard9fa3e852004-01-04 18:06:42 +00002057/* update the TLBs so that writes to code in the virtual page 'addr'
2058 can be detected */
Anthony Liguoric227f092009-10-01 16:12:16 -05002059static void tlb_protect_code(ram_addr_t ram_addr)
bellard61382a52003-10-27 21:22:23 +00002060{
ths5fafdf22007-09-16 21:08:06 +00002061 cpu_physical_memory_reset_dirty(ram_addr,
bellard6a00d602005-11-21 23:25:50 +00002062 ram_addr + TARGET_PAGE_SIZE,
2063 CODE_DIRTY_FLAG);
bellard9fa3e852004-01-04 18:06:42 +00002064}
2065
bellard9fa3e852004-01-04 18:06:42 +00002066/* update the TLB so that writes in physical page 'phys_addr' are no longer
bellard3a7d9292005-08-21 09:26:42 +00002067 tested for self modifying code */
Anthony Liguoric227f092009-10-01 16:12:16 -05002068static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
bellard3a7d9292005-08-21 09:26:42 +00002069 target_ulong vaddr)
bellard9fa3e852004-01-04 18:06:42 +00002070{
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002071 cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG);
bellard1ccde1c2004-02-06 19:46:14 +00002072}
2073
ths5fafdf22007-09-16 21:08:06 +00002074static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
bellard1ccde1c2004-02-06 19:46:14 +00002075 unsigned long start, unsigned long length)
2076{
2077 unsigned long addr;
bellard84b7b8e2005-11-28 21:19:04 +00002078 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
2079 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
bellard1ccde1c2004-02-06 19:46:14 +00002080 if ((addr - start) < length) {
pbrook0f459d12008-06-09 00:20:13 +00002081 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
bellard1ccde1c2004-02-06 19:46:14 +00002082 }
2083 }
2084}
2085
pbrook5579c7f2009-04-11 14:47:08 +00002086/* Note: start and end must be within the same ram block. */
Anthony Liguoric227f092009-10-01 16:12:16 -05002087void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +00002088 int dirty_flags)
bellard1ccde1c2004-02-06 19:46:14 +00002089{
2090 CPUState *env;
bellard4f2ac232004-04-26 19:44:02 +00002091 unsigned long length, start1;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002092 int i;
bellard1ccde1c2004-02-06 19:46:14 +00002093
2094 start &= TARGET_PAGE_MASK;
2095 end = TARGET_PAGE_ALIGN(end);
2096
2097 length = end - start;
2098 if (length == 0)
2099 return;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002100 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00002101
bellard1ccde1c2004-02-06 19:46:14 +00002102 /* we modify the TLB cache so that the dirty bit will be set again
2103 when accessing the range */
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02002104 start1 = (unsigned long)qemu_safe_ram_ptr(start);
Stefan Weila57d23e2011-04-30 22:49:26 +02002105 /* Check that we don't span multiple blocks - this breaks the
pbrook5579c7f2009-04-11 14:47:08 +00002106 address comparisons below. */
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02002107 if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
pbrook5579c7f2009-04-11 14:47:08 +00002108 != (end - 1) - start) {
2109 abort();
2110 }
2111
bellard6a00d602005-11-21 23:25:50 +00002112 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002113 int mmu_idx;
2114 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2115 for(i = 0; i < CPU_TLB_SIZE; i++)
2116 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
2117 start1, length);
2118 }
bellard6a00d602005-11-21 23:25:50 +00002119 }
bellard1ccde1c2004-02-06 19:46:14 +00002120}
2121
aliguori74576192008-10-06 14:02:03 +00002122int cpu_physical_memory_set_dirty_tracking(int enable)
2123{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002124 int ret = 0;
aliguori74576192008-10-06 14:02:03 +00002125 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002126 ret = cpu_notify_migration_log(!!enable);
2127 return ret;
aliguori74576192008-10-06 14:02:03 +00002128}
2129
2130int cpu_physical_memory_get_dirty_tracking(void)
2131{
2132 return in_migration;
2133}
2134
Anthony Liguoric227f092009-10-01 16:12:16 -05002135int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
2136 target_phys_addr_t end_addr)
aliguori2bec46d2008-11-24 20:21:41 +00002137{
Michael S. Tsirkin7b8f3b72010-01-27 22:07:21 +02002138 int ret;
Jan Kiszka151f7742009-05-01 20:52:47 +02002139
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002140 ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
Jan Kiszka151f7742009-05-01 20:52:47 +02002141 return ret;
aliguori2bec46d2008-11-24 20:21:41 +00002142}
2143
Anthony PERARDe5896b12011-02-07 12:19:23 +01002144int cpu_physical_log_start(target_phys_addr_t start_addr,
2145 ram_addr_t size)
2146{
2147 CPUPhysMemoryClient *client;
2148 QLIST_FOREACH(client, &memory_client_list, list) {
2149 if (client->log_start) {
2150 int r = client->log_start(client, start_addr, size);
2151 if (r < 0) {
2152 return r;
2153 }
2154 }
2155 }
2156 return 0;
2157}
2158
2159int cpu_physical_log_stop(target_phys_addr_t start_addr,
2160 ram_addr_t size)
2161{
2162 CPUPhysMemoryClient *client;
2163 QLIST_FOREACH(client, &memory_client_list, list) {
2164 if (client->log_stop) {
2165 int r = client->log_stop(client, start_addr, size);
2166 if (r < 0) {
2167 return r;
2168 }
2169 }
2170 }
2171 return 0;
2172}
2173
bellard3a7d9292005-08-21 09:26:42 +00002174static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
2175{
Anthony Liguoric227f092009-10-01 16:12:16 -05002176 ram_addr_t ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00002177 void *p;
bellard3a7d9292005-08-21 09:26:42 +00002178
bellard84b7b8e2005-11-28 21:19:04 +00002179 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
pbrook5579c7f2009-04-11 14:47:08 +00002180 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
2181 + tlb_entry->addend);
Marcelo Tosattie8902612010-10-11 15:31:19 -03002182 ram_addr = qemu_ram_addr_from_host_nofail(p);
bellard3a7d9292005-08-21 09:26:42 +00002183 if (!cpu_physical_memory_is_dirty(ram_addr)) {
pbrook0f459d12008-06-09 00:20:13 +00002184 tlb_entry->addr_write |= TLB_NOTDIRTY;
bellard3a7d9292005-08-21 09:26:42 +00002185 }
2186 }
2187}
2188
2189/* update the TLB according to the current state of the dirty bits */
2190void cpu_tlb_update_dirty(CPUState *env)
2191{
2192 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002193 int mmu_idx;
2194 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
2195 for(i = 0; i < CPU_TLB_SIZE; i++)
2196 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
2197 }
bellard3a7d9292005-08-21 09:26:42 +00002198}
2199
pbrook0f459d12008-06-09 00:20:13 +00002200static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002201{
pbrook0f459d12008-06-09 00:20:13 +00002202 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
2203 tlb_entry->addr_write = vaddr;
bellard1ccde1c2004-02-06 19:46:14 +00002204}
2205
pbrook0f459d12008-06-09 00:20:13 +00002206/* update the TLB corresponding to virtual page vaddr
2207 so that it is no longer dirty */
2208static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002209{
bellard1ccde1c2004-02-06 19:46:14 +00002210 int i;
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002211 int mmu_idx;
bellard1ccde1c2004-02-06 19:46:14 +00002212
pbrook0f459d12008-06-09 00:20:13 +00002213 vaddr &= TARGET_PAGE_MASK;
bellard1ccde1c2004-02-06 19:46:14 +00002214 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
Isaku Yamahatacfde4bd2009-05-20 11:31:43 +09002215 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
2216 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
bellard9fa3e852004-01-04 18:06:42 +00002217}
2218
Paul Brookd4c430a2010-03-17 02:14:28 +00002219/* Our TLB does not support large pages, so remember the area covered by
2220 large pages and trigger a full TLB flush if these are invalidated. */
2221static void tlb_add_large_page(CPUState *env, target_ulong vaddr,
2222 target_ulong size)
2223{
2224 target_ulong mask = ~(size - 1);
2225
2226 if (env->tlb_flush_addr == (target_ulong)-1) {
2227 env->tlb_flush_addr = vaddr & mask;
2228 env->tlb_flush_mask = mask;
2229 return;
2230 }
2231 /* Extend the existing region to include the new page.
2232 This is a compromise between unnecessary flushes and the cost
2233 of maintaining a full variable size TLB. */
2234 mask &= env->tlb_flush_mask;
2235 while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
2236 mask <<= 1;
2237 }
2238 env->tlb_flush_addr &= mask;
2239 env->tlb_flush_mask = mask;
2240}
2241
2242/* Add a new TLB entry. At most one entry for a given virtual address
2243 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
2244 supplied size is only used by tlb_flush_page. */
2245void tlb_set_page(CPUState *env, target_ulong vaddr,
2246 target_phys_addr_t paddr, int prot,
2247 int mmu_idx, target_ulong size)
bellard9fa3e852004-01-04 18:06:42 +00002248{
bellard92e873b2004-05-21 14:52:29 +00002249 PhysPageDesc *p;
bellard4f2ac232004-04-26 19:44:02 +00002250 unsigned long pd;
bellard9fa3e852004-01-04 18:06:42 +00002251 unsigned int index;
bellard4f2ac232004-04-26 19:44:02 +00002252 target_ulong address;
pbrook0f459d12008-06-09 00:20:13 +00002253 target_ulong code_address;
Paul Brook355b1942010-04-05 00:28:53 +01002254 unsigned long addend;
bellard84b7b8e2005-11-28 21:19:04 +00002255 CPUTLBEntry *te;
aliguoria1d1bb32008-11-18 20:07:32 +00002256 CPUWatchpoint *wp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002257 target_phys_addr_t iotlb;
bellard9fa3e852004-01-04 18:06:42 +00002258
Paul Brookd4c430a2010-03-17 02:14:28 +00002259 assert(size >= TARGET_PAGE_SIZE);
2260 if (size != TARGET_PAGE_SIZE) {
2261 tlb_add_large_page(env, vaddr, size);
2262 }
bellard92e873b2004-05-21 14:52:29 +00002263 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
bellard9fa3e852004-01-04 18:06:42 +00002264 if (!p) {
2265 pd = IO_MEM_UNASSIGNED;
bellard9fa3e852004-01-04 18:06:42 +00002266 } else {
2267 pd = p->phys_offset;
bellard9fa3e852004-01-04 18:06:42 +00002268 }
2269#if defined(DEBUG_TLB)
Stefan Weil7fd3f492010-09-30 22:39:51 +02002270 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
2271 " prot=%x idx=%d pd=0x%08lx\n",
2272 vaddr, paddr, prot, mmu_idx, pd);
bellard9fa3e852004-01-04 18:06:42 +00002273#endif
2274
pbrook0f459d12008-06-09 00:20:13 +00002275 address = vaddr;
2276 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2277 /* IO memory case (romd handled later) */
2278 address |= TLB_MMIO;
2279 }
pbrook5579c7f2009-04-11 14:47:08 +00002280 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
pbrook0f459d12008-06-09 00:20:13 +00002281 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2282 /* Normal RAM. */
2283 iotlb = pd & TARGET_PAGE_MASK;
2284 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2285 iotlb |= IO_MEM_NOTDIRTY;
2286 else
2287 iotlb |= IO_MEM_ROM;
2288 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002289 /* IO handlers are currently passed a physical address.
pbrook0f459d12008-06-09 00:20:13 +00002290 It would be nice to pass an offset from the base address
2291 of that region. This would avoid having to special case RAM,
2292 and avoid full address decoding in every device.
2293 We can't use the high bits of pd for this because
2294 IO_MEM_ROMD uses these as a ram address. */
pbrook8da3ff12008-12-01 18:59:50 +00002295 iotlb = (pd & ~TARGET_PAGE_MASK);
2296 if (p) {
pbrook8da3ff12008-12-01 18:59:50 +00002297 iotlb += p->region_offset;
2298 } else {
2299 iotlb += paddr;
2300 }
pbrook0f459d12008-06-09 00:20:13 +00002301 }
pbrook6658ffb2007-03-16 23:58:11 +00002302
pbrook0f459d12008-06-09 00:20:13 +00002303 code_address = address;
2304 /* Make accesses to pages with watchpoints go via the
2305 watchpoint trap routines. */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002306 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +00002307 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
Jun Koibf298f82010-05-06 14:36:59 +09002308 /* Avoid trapping reads of pages with a write breakpoint. */
2309 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
2310 iotlb = io_mem_watch + paddr;
2311 address |= TLB_MMIO;
2312 break;
2313 }
pbrook6658ffb2007-03-16 23:58:11 +00002314 }
pbrook0f459d12008-06-09 00:20:13 +00002315 }
balrogd79acba2007-06-26 20:01:13 +00002316
pbrook0f459d12008-06-09 00:20:13 +00002317 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2318 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2319 te = &env->tlb_table[mmu_idx][index];
2320 te->addend = addend - vaddr;
2321 if (prot & PAGE_READ) {
2322 te->addr_read = address;
2323 } else {
2324 te->addr_read = -1;
2325 }
edgar_igl5c751e92008-05-06 08:44:21 +00002326
pbrook0f459d12008-06-09 00:20:13 +00002327 if (prot & PAGE_EXEC) {
2328 te->addr_code = code_address;
2329 } else {
2330 te->addr_code = -1;
2331 }
2332 if (prot & PAGE_WRITE) {
2333 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2334 (pd & IO_MEM_ROMD)) {
2335 /* Write access calls the I/O callback. */
2336 te->addr_write = address | TLB_MMIO;
2337 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2338 !cpu_physical_memory_is_dirty(pd)) {
2339 te->addr_write = address | TLB_NOTDIRTY;
bellard84b7b8e2005-11-28 21:19:04 +00002340 } else {
pbrook0f459d12008-06-09 00:20:13 +00002341 te->addr_write = address;
bellard9fa3e852004-01-04 18:06:42 +00002342 }
pbrook0f459d12008-06-09 00:20:13 +00002343 } else {
2344 te->addr_write = -1;
bellard9fa3e852004-01-04 18:06:42 +00002345 }
bellard9fa3e852004-01-04 18:06:42 +00002346}
2347
bellard01243112004-01-04 15:48:17 +00002348#else
2349
bellardee8b7022004-02-03 23:35:10 +00002350void tlb_flush(CPUState *env, int flush_global)
bellard01243112004-01-04 15:48:17 +00002351{
2352}
2353
bellard2e126692004-04-25 21:28:44 +00002354void tlb_flush_page(CPUState *env, target_ulong addr)
bellard01243112004-01-04 15:48:17 +00002355{
2356}
2357
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002358/*
2359 * Walks guest process memory "regions" one by one
2360 * and calls callback function 'fn' for each region.
2361 */
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002362
2363struct walk_memory_regions_data
bellard9fa3e852004-01-04 18:06:42 +00002364{
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002365 walk_memory_regions_fn fn;
2366 void *priv;
2367 unsigned long start;
2368 int prot;
2369};
bellard9fa3e852004-01-04 18:06:42 +00002370
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002371static int walk_memory_regions_end(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002372 abi_ulong end, int new_prot)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002373{
2374 if (data->start != -1ul) {
2375 int rc = data->fn(data->priv, data->start, end, data->prot);
2376 if (rc != 0) {
2377 return rc;
bellard9fa3e852004-01-04 18:06:42 +00002378 }
bellard33417e72003-08-10 21:47:01 +00002379 }
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002380
2381 data->start = (new_prot ? end : -1ul);
2382 data->prot = new_prot;
2383
2384 return 0;
2385}
2386
2387static int walk_memory_regions_1(struct walk_memory_regions_data *data,
Paul Brookb480d9b2010-03-12 23:23:29 +00002388 abi_ulong base, int level, void **lp)
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002389{
Paul Brookb480d9b2010-03-12 23:23:29 +00002390 abi_ulong pa;
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002391 int i, rc;
2392
2393 if (*lp == NULL) {
2394 return walk_memory_regions_end(data, base, 0);
2395 }
2396
2397 if (level == 0) {
2398 PageDesc *pd = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002399 for (i = 0; i < L2_SIZE; ++i) {
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002400 int prot = pd[i].flags;
2401
2402 pa = base | (i << TARGET_PAGE_BITS);
2403 if (prot != data->prot) {
2404 rc = walk_memory_regions_end(data, pa, prot);
2405 if (rc != 0) {
2406 return rc;
2407 }
2408 }
2409 }
2410 } else {
2411 void **pp = *lp;
Paul Brook7296aba2010-03-14 14:58:46 +00002412 for (i = 0; i < L2_SIZE; ++i) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002413 pa = base | ((abi_ulong)i <<
2414 (TARGET_PAGE_BITS + L2_BITS * level));
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002415 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2416 if (rc != 0) {
2417 return rc;
2418 }
2419 }
2420 }
2421
2422 return 0;
2423}
2424
2425int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2426{
2427 struct walk_memory_regions_data data;
2428 unsigned long i;
2429
2430 data.fn = fn;
2431 data.priv = priv;
2432 data.start = -1ul;
2433 data.prot = 0;
2434
2435 for (i = 0; i < V_L1_SIZE; i++) {
Paul Brookb480d9b2010-03-12 23:23:29 +00002436 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
Richard Henderson5cd2c5b2010-03-10 15:53:37 -08002437 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
2438 if (rc != 0) {
2439 return rc;
2440 }
2441 }
2442
2443 return walk_memory_regions_end(&data, 0, 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002444}
2445
Paul Brookb480d9b2010-03-12 23:23:29 +00002446static int dump_region(void *priv, abi_ulong start,
2447 abi_ulong end, unsigned long prot)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002448{
2449 FILE *f = (FILE *)priv;
2450
Paul Brookb480d9b2010-03-12 23:23:29 +00002451 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
2452 " "TARGET_ABI_FMT_lx" %c%c%c\n",
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002453 start, end, end - start,
2454 ((prot & PAGE_READ) ? 'r' : '-'),
2455 ((prot & PAGE_WRITE) ? 'w' : '-'),
2456 ((prot & PAGE_EXEC) ? 'x' : '-'));
2457
2458 return (0);
2459}
2460
2461/* dump memory mappings */
2462void page_dump(FILE *f)
2463{
2464 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2465 "start", "end", "size", "prot");
2466 walk_memory_regions(f, dump_region);
bellard33417e72003-08-10 21:47:01 +00002467}
2468
pbrook53a59602006-03-25 19:31:22 +00002469int page_get_flags(target_ulong address)
bellard33417e72003-08-10 21:47:01 +00002470{
bellard9fa3e852004-01-04 18:06:42 +00002471 PageDesc *p;
2472
2473 p = page_find(address >> TARGET_PAGE_BITS);
bellard33417e72003-08-10 21:47:01 +00002474 if (!p)
bellard9fa3e852004-01-04 18:06:42 +00002475 return 0;
2476 return p->flags;
bellard33417e72003-08-10 21:47:01 +00002477}
2478
Richard Henderson376a7902010-03-10 15:57:04 -08002479/* Modify the flags of a page and invalidate the code if necessary.
2480 The flag PAGE_WRITE_ORG is positioned automatically depending
2481 on PAGE_WRITE. The mmap_lock should already be held. */
pbrook53a59602006-03-25 19:31:22 +00002482void page_set_flags(target_ulong start, target_ulong end, int flags)
bellard9fa3e852004-01-04 18:06:42 +00002483{
Richard Henderson376a7902010-03-10 15:57:04 -08002484 target_ulong addr, len;
bellard9fa3e852004-01-04 18:06:42 +00002485
Richard Henderson376a7902010-03-10 15:57:04 -08002486 /* This function should never be called with addresses outside the
2487 guest address space. If this assert fires, it probably indicates
2488 a missing call to h2g_valid. */
Paul Brookb480d9b2010-03-12 23:23:29 +00002489#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2490 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002491#endif
2492 assert(start < end);
2493
bellard9fa3e852004-01-04 18:06:42 +00002494 start = start & TARGET_PAGE_MASK;
2495 end = TARGET_PAGE_ALIGN(end);
Richard Henderson376a7902010-03-10 15:57:04 -08002496
2497 if (flags & PAGE_WRITE) {
bellard9fa3e852004-01-04 18:06:42 +00002498 flags |= PAGE_WRITE_ORG;
Richard Henderson376a7902010-03-10 15:57:04 -08002499 }
2500
2501 for (addr = start, len = end - start;
2502 len != 0;
2503 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2504 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2505
2506 /* If the write protection bit is set, then we invalidate
2507 the code inside. */
ths5fafdf22007-09-16 21:08:06 +00002508 if (!(p->flags & PAGE_WRITE) &&
bellard9fa3e852004-01-04 18:06:42 +00002509 (flags & PAGE_WRITE) &&
2510 p->first_tb) {
bellardd720b932004-04-25 17:57:43 +00002511 tb_invalidate_phys_page(addr, 0, NULL);
bellard9fa3e852004-01-04 18:06:42 +00002512 }
2513 p->flags = flags;
2514 }
bellard9fa3e852004-01-04 18:06:42 +00002515}
2516
ths3d97b402007-11-02 19:02:07 +00002517int page_check_range(target_ulong start, target_ulong len, int flags)
2518{
2519 PageDesc *p;
2520 target_ulong end;
2521 target_ulong addr;
2522
Richard Henderson376a7902010-03-10 15:57:04 -08002523 /* This function should never be called with addresses outside the
2524 guest address space. If this assert fires, it probably indicates
2525 a missing call to h2g_valid. */
Blue Swirl338e9e62010-03-13 09:48:08 +00002526#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2527 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
Richard Henderson376a7902010-03-10 15:57:04 -08002528#endif
2529
Richard Henderson3e0650a2010-03-29 10:54:42 -07002530 if (len == 0) {
2531 return 0;
2532 }
Richard Henderson376a7902010-03-10 15:57:04 -08002533 if (start + len - 1 < start) {
2534 /* We've wrapped around. */
balrog55f280c2008-10-28 10:24:11 +00002535 return -1;
Richard Henderson376a7902010-03-10 15:57:04 -08002536 }
balrog55f280c2008-10-28 10:24:11 +00002537
ths3d97b402007-11-02 19:02:07 +00002538 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2539 start = start & TARGET_PAGE_MASK;
2540
Richard Henderson376a7902010-03-10 15:57:04 -08002541 for (addr = start, len = end - start;
2542 len != 0;
2543 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
ths3d97b402007-11-02 19:02:07 +00002544 p = page_find(addr >> TARGET_PAGE_BITS);
2545 if( !p )
2546 return -1;
2547 if( !(p->flags & PAGE_VALID) )
2548 return -1;
2549
bellarddae32702007-11-14 10:51:00 +00002550 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
ths3d97b402007-11-02 19:02:07 +00002551 return -1;
bellarddae32702007-11-14 10:51:00 +00002552 if (flags & PAGE_WRITE) {
2553 if (!(p->flags & PAGE_WRITE_ORG))
2554 return -1;
2555 /* unprotect the page if it was put read-only because it
2556 contains translated code */
2557 if (!(p->flags & PAGE_WRITE)) {
2558 if (!page_unprotect(addr, 0, NULL))
2559 return -1;
2560 }
2561 return 0;
2562 }
ths3d97b402007-11-02 19:02:07 +00002563 }
2564 return 0;
2565}
2566
bellard9fa3e852004-01-04 18:06:42 +00002567/* called from signal handler: invalidate the code and unprotect the
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002568 page. Return TRUE if the fault was successfully handled. */
pbrook53a59602006-03-25 19:31:22 +00002569int page_unprotect(target_ulong address, unsigned long pc, void *puc)
bellard9fa3e852004-01-04 18:06:42 +00002570{
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002571 unsigned int prot;
2572 PageDesc *p;
pbrook53a59602006-03-25 19:31:22 +00002573 target_ulong host_start, host_end, addr;
bellard9fa3e852004-01-04 18:06:42 +00002574
pbrookc8a706f2008-06-02 16:16:42 +00002575 /* Technically this isn't safe inside a signal handler. However we
2576 know this only ever happens in a synchronous SEGV handler, so in
2577 practice it seems to be ok. */
2578 mmap_lock();
2579
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002580 p = page_find(address >> TARGET_PAGE_BITS);
2581 if (!p) {
pbrookc8a706f2008-06-02 16:16:42 +00002582 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002583 return 0;
pbrookc8a706f2008-06-02 16:16:42 +00002584 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002585
bellard9fa3e852004-01-04 18:06:42 +00002586 /* if the page was really writable, then we change its
2587 protection back to writable */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002588 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2589 host_start = address & qemu_host_page_mask;
2590 host_end = host_start + qemu_host_page_size;
2591
2592 prot = 0;
2593 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2594 p = page_find(addr >> TARGET_PAGE_BITS);
2595 p->flags |= PAGE_WRITE;
2596 prot |= p->flags;
2597
bellard9fa3e852004-01-04 18:06:42 +00002598 /* and since the content will be modified, we must invalidate
2599 the corresponding translated code. */
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002600 tb_invalidate_phys_page(addr, pc, puc);
bellard9fa3e852004-01-04 18:06:42 +00002601#ifdef DEBUG_TB_CHECK
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002602 tb_invalidate_check(addr);
bellard9fa3e852004-01-04 18:06:42 +00002603#endif
bellard9fa3e852004-01-04 18:06:42 +00002604 }
Aurelien Jarno45d679d2010-03-29 02:12:51 +02002605 mprotect((void *)g2h(host_start), qemu_host_page_size,
2606 prot & PAGE_BITS);
2607
2608 mmap_unlock();
2609 return 1;
bellard9fa3e852004-01-04 18:06:42 +00002610 }
pbrookc8a706f2008-06-02 16:16:42 +00002611 mmap_unlock();
bellard9fa3e852004-01-04 18:06:42 +00002612 return 0;
2613}
2614
bellard6a00d602005-11-21 23:25:50 +00002615static inline void tlb_set_dirty(CPUState *env,
2616 unsigned long addr, target_ulong vaddr)
bellard1ccde1c2004-02-06 19:46:14 +00002617{
2618}
bellard9fa3e852004-01-04 18:06:42 +00002619#endif /* defined(CONFIG_USER_ONLY) */
2620
pbrooke2eef172008-06-08 01:09:01 +00002621#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +00002622
Paul Brookc04b2b72010-03-01 03:31:14 +00002623#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2624typedef struct subpage_t {
2625 target_phys_addr_t base;
Richard Hendersonf6405242010-04-22 16:47:31 -07002626 ram_addr_t sub_io_index[TARGET_PAGE_SIZE];
2627 ram_addr_t region_offset[TARGET_PAGE_SIZE];
Paul Brookc04b2b72010-03-01 03:31:14 +00002628} subpage_t;
2629
Anthony Liguoric227f092009-10-01 16:12:16 -05002630static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2631 ram_addr_t memory, ram_addr_t region_offset);
Richard Hendersonf6405242010-04-22 16:47:31 -07002632static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2633 ram_addr_t orig_memory,
2634 ram_addr_t region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002635#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2636 need_subpage) \
2637 do { \
2638 if (addr > start_addr) \
2639 start_addr2 = 0; \
2640 else { \
2641 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2642 if (start_addr2 > 0) \
2643 need_subpage = 1; \
2644 } \
2645 \
blueswir149e9fba2007-05-30 17:25:06 +00002646 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
blueswir1db7b5422007-05-26 17:36:03 +00002647 end_addr2 = TARGET_PAGE_SIZE - 1; \
2648 else { \
2649 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2650 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2651 need_subpage = 1; \
2652 } \
2653 } while (0)
2654
Michael S. Tsirkin8f2498f2009-09-29 18:53:16 +02002655/* register physical memory.
2656 For RAM, 'size' must be a multiple of the target page size.
2657 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
pbrook8da3ff12008-12-01 18:59:50 +00002658 io memory page. The address used when calling the IO function is
2659 the offset from the start of the region, plus region_offset. Both
Stuart Bradyccbb4d42009-05-03 12:15:06 +01002660 start_addr and region_offset are rounded down to a page boundary
pbrook8da3ff12008-12-01 18:59:50 +00002661 before calculating this offset. This should not be a problem unless
2662 the low bits of start_addr and region_offset differ. */
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002663void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
Anthony Liguoric227f092009-10-01 16:12:16 -05002664 ram_addr_t size,
2665 ram_addr_t phys_offset,
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002666 ram_addr_t region_offset,
2667 bool log_dirty)
bellard33417e72003-08-10 21:47:01 +00002668{
Anthony Liguoric227f092009-10-01 16:12:16 -05002669 target_phys_addr_t addr, end_addr;
bellard92e873b2004-05-21 14:52:29 +00002670 PhysPageDesc *p;
bellard9d420372006-06-25 22:25:22 +00002671 CPUState *env;
Anthony Liguoric227f092009-10-01 16:12:16 -05002672 ram_addr_t orig_size = size;
Richard Hendersonf6405242010-04-22 16:47:31 -07002673 subpage_t *subpage;
bellard33417e72003-08-10 21:47:01 +00002674
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002675 assert(size);
Michael S. Tsirkin0fd542f2011-04-06 22:25:38 +03002676 cpu_notify_set_memory(start_addr, size, phys_offset, log_dirty);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +02002677
pbrook67c4d232009-02-23 13:16:07 +00002678 if (phys_offset == IO_MEM_UNASSIGNED) {
2679 region_offset = start_addr;
2680 }
pbrook8da3ff12008-12-01 18:59:50 +00002681 region_offset &= TARGET_PAGE_MASK;
bellard5fd386f2004-05-23 21:11:22 +00002682 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
Anthony Liguoric227f092009-10-01 16:12:16 -05002683 end_addr = start_addr + (target_phys_addr_t)size;
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002684
2685 addr = start_addr;
2686 do {
blueswir1db7b5422007-05-26 17:36:03 +00002687 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2688 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002689 ram_addr_t orig_memory = p->phys_offset;
2690 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002691 int need_subpage = 0;
2692
2693 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2694 need_subpage);
Richard Hendersonf6405242010-04-22 16:47:31 -07002695 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002696 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2697 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002698 &p->phys_offset, orig_memory,
2699 p->region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00002700 } else {
2701 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2702 >> IO_MEM_SHIFT];
2703 }
pbrook8da3ff12008-12-01 18:59:50 +00002704 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2705 region_offset);
2706 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002707 } else {
2708 p->phys_offset = phys_offset;
2709 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2710 (phys_offset & IO_MEM_ROMD))
2711 phys_offset += TARGET_PAGE_SIZE;
2712 }
2713 } else {
2714 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2715 p->phys_offset = phys_offset;
pbrook8da3ff12008-12-01 18:59:50 +00002716 p->region_offset = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00002717 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
pbrook8da3ff12008-12-01 18:59:50 +00002718 (phys_offset & IO_MEM_ROMD)) {
blueswir1db7b5422007-05-26 17:36:03 +00002719 phys_offset += TARGET_PAGE_SIZE;
pbrook0e8f0962008-12-02 09:02:15 +00002720 } else {
Anthony Liguoric227f092009-10-01 16:12:16 -05002721 target_phys_addr_t start_addr2, end_addr2;
blueswir1db7b5422007-05-26 17:36:03 +00002722 int need_subpage = 0;
2723
2724 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2725 end_addr2, need_subpage);
2726
Richard Hendersonf6405242010-04-22 16:47:31 -07002727 if (need_subpage) {
blueswir1db7b5422007-05-26 17:36:03 +00002728 subpage = subpage_init((addr & TARGET_PAGE_MASK),
pbrook8da3ff12008-12-01 18:59:50 +00002729 &p->phys_offset, IO_MEM_UNASSIGNED,
pbrook67c4d232009-02-23 13:16:07 +00002730 addr & TARGET_PAGE_MASK);
blueswir1db7b5422007-05-26 17:36:03 +00002731 subpage_register(subpage, start_addr2, end_addr2,
pbrook8da3ff12008-12-01 18:59:50 +00002732 phys_offset, region_offset);
2733 p->region_offset = 0;
blueswir1db7b5422007-05-26 17:36:03 +00002734 }
2735 }
2736 }
pbrook8da3ff12008-12-01 18:59:50 +00002737 region_offset += TARGET_PAGE_SIZE;
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +02002738 addr += TARGET_PAGE_SIZE;
2739 } while (addr != end_addr);
ths3b46e622007-09-17 08:09:54 +00002740
bellard9d420372006-06-25 22:25:22 +00002741 /* since each CPU stores ram addresses in its TLB cache, we must
2742 reset the modified entries */
2743 /* XXX: slow ! */
2744 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2745 tlb_flush(env, 1);
2746 }
bellard33417e72003-08-10 21:47:01 +00002747}
2748
bellardba863452006-09-24 18:41:10 +00002749/* XXX: temporary until new memory mapping API */
Anthony Liguoric227f092009-10-01 16:12:16 -05002750ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
bellardba863452006-09-24 18:41:10 +00002751{
2752 PhysPageDesc *p;
2753
2754 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2755 if (!p)
2756 return IO_MEM_UNASSIGNED;
2757 return p->phys_offset;
2758}
2759
Anthony Liguoric227f092009-10-01 16:12:16 -05002760void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002761{
2762 if (kvm_enabled())
2763 kvm_coalesce_mmio_region(addr, size);
2764}
2765
Anthony Liguoric227f092009-10-01 16:12:16 -05002766void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
aliguorif65ed4c2008-12-09 20:09:57 +00002767{
2768 if (kvm_enabled())
2769 kvm_uncoalesce_mmio_region(addr, size);
2770}
2771
Sheng Yang62a27442010-01-26 19:21:16 +08002772void qemu_flush_coalesced_mmio_buffer(void)
2773{
2774 if (kvm_enabled())
2775 kvm_flush_coalesced_mmio_buffer();
2776}
2777
Marcelo Tosattic9027602010-03-01 20:25:08 -03002778#if defined(__linux__) && !defined(TARGET_S390X)
2779
2780#include <sys/vfs.h>
2781
2782#define HUGETLBFS_MAGIC 0x958458f6
2783
2784static long gethugepagesize(const char *path)
2785{
2786 struct statfs fs;
2787 int ret;
2788
2789 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002790 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002791 } while (ret != 0 && errno == EINTR);
2792
2793 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002794 perror(path);
2795 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002796 }
2797
2798 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002799 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002800
2801 return fs.f_bsize;
2802}
2803
Alex Williamson04b16652010-07-02 11:13:17 -06002804static void *file_ram_alloc(RAMBlock *block,
2805 ram_addr_t memory,
2806 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -03002807{
2808 char *filename;
2809 void *area;
2810 int fd;
2811#ifdef MAP_POPULATE
2812 int flags;
2813#endif
2814 unsigned long hpagesize;
2815
2816 hpagesize = gethugepagesize(path);
2817 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002818 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002819 }
2820
2821 if (memory < hpagesize) {
2822 return NULL;
2823 }
2824
2825 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2826 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2827 return NULL;
2828 }
2829
2830 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002831 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002832 }
2833
2834 fd = mkstemp(filename);
2835 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002836 perror("unable to create backing store for hugepages");
2837 free(filename);
2838 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002839 }
2840 unlink(filename);
2841 free(filename);
2842
2843 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2844
2845 /*
2846 * ftruncate is not supported by hugetlbfs in older
2847 * hosts, so don't bother bailing out on errors.
2848 * If anything goes wrong with it under other filesystems,
2849 * mmap will fail.
2850 */
2851 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002852 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -03002853
2854#ifdef MAP_POPULATE
2855 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2856 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2857 * to sidestep this quirk.
2858 */
2859 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2860 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2861#else
2862 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2863#endif
2864 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09002865 perror("file_ram_alloc: can't mmap RAM pages");
2866 close(fd);
2867 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -03002868 }
Alex Williamson04b16652010-07-02 11:13:17 -06002869 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -03002870 return area;
2871}
2872#endif
2873
Alex Williamsond17b5282010-06-25 11:08:38 -06002874static ram_addr_t find_ram_offset(ram_addr_t size)
2875{
Alex Williamson04b16652010-07-02 11:13:17 -06002876 RAMBlock *block, *next_block;
Alex Williamson3e837b22011-10-31 08:54:09 -06002877 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06002878
2879 if (QLIST_EMPTY(&ram_list.blocks))
2880 return 0;
2881
2882 QLIST_FOREACH(block, &ram_list.blocks, next) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00002883 ram_addr_t end, next = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06002884
2885 end = block->offset + block->length;
2886
2887 QLIST_FOREACH(next_block, &ram_list.blocks, next) {
2888 if (next_block->offset >= end) {
2889 next = MIN(next, next_block->offset);
2890 }
2891 }
2892 if (next - end >= size && next - end < mingap) {
Alex Williamson3e837b22011-10-31 08:54:09 -06002893 offset = end;
Alex Williamson04b16652010-07-02 11:13:17 -06002894 mingap = next - end;
2895 }
2896 }
Alex Williamson3e837b22011-10-31 08:54:09 -06002897
2898 if (offset == RAM_ADDR_MAX) {
2899 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
2900 (uint64_t)size);
2901 abort();
2902 }
2903
Alex Williamson04b16652010-07-02 11:13:17 -06002904 return offset;
2905}
2906
2907static ram_addr_t last_ram_offset(void)
2908{
Alex Williamsond17b5282010-06-25 11:08:38 -06002909 RAMBlock *block;
2910 ram_addr_t last = 0;
2911
2912 QLIST_FOREACH(block, &ram_list.blocks, next)
2913 last = MAX(last, block->offset + block->length);
2914
2915 return last;
2916}
2917
Cam Macdonell84b89d72010-07-26 18:10:57 -06002918ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
Avi Kivityfce537d2011-12-18 15:48:55 +02002919 ram_addr_t size, void *host,
2920 MemoryRegion *mr)
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()) {
Avi Kivityfce537d2011-12-18 15:48:55 +02002976 xen_ram_alloc(new_block->offset, size, mr);
Jun Nakajima432d2682010-08-31 16:41:25 +01002977 } 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
Avi Kivityfce537d2011-12-18 15:48:55 +02002999ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size,
3000 MemoryRegion *mr)
pbrook94a6b542009-04-11 17:15:54 +00003001{
Avi Kivityfce537d2011-12-18 15:48:55 +02003002 return qemu_ram_alloc_from_ptr(dev, name, size, NULL, mr);
pbrook94a6b542009-04-11 17:15:54 +00003003}
bellarde9a1ab12007-02-08 23:08:38 +00003004
Alex Williamson1f2e98b2011-05-03 12:48:09 -06003005void qemu_ram_free_from_ptr(ram_addr_t addr)
3006{
3007 RAMBlock *block;
3008
3009 QLIST_FOREACH(block, &ram_list.blocks, next) {
3010 if (addr == block->offset) {
3011 QLIST_REMOVE(block, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05003012 g_free(block);
Alex Williamson1f2e98b2011-05-03 12:48:09 -06003013 return;
3014 }
3015 }
3016}
3017
Anthony Liguoric227f092009-10-01 16:12:16 -05003018void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00003019{
Alex Williamson04b16652010-07-02 11:13:17 -06003020 RAMBlock *block;
3021
3022 QLIST_FOREACH(block, &ram_list.blocks, next) {
3023 if (addr == block->offset) {
3024 QLIST_REMOVE(block, next);
Huang Yingcd19cfa2011-03-02 08:56:19 +01003025 if (block->flags & RAM_PREALLOC_MASK) {
3026 ;
3027 } else if (mem_path) {
Alex Williamson04b16652010-07-02 11:13:17 -06003028#if defined (__linux__) && !defined(TARGET_S390X)
3029 if (block->fd) {
3030 munmap(block->host, block->length);
3031 close(block->fd);
3032 } else {
3033 qemu_vfree(block->host);
3034 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01003035#else
3036 abort();
Alex Williamson04b16652010-07-02 11:13:17 -06003037#endif
3038 } else {
3039#if defined(TARGET_S390X) && defined(CONFIG_KVM)
3040 munmap(block->host, block->length);
3041#else
Jan Kiszka868bb332011-06-21 22:59:09 +02003042 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003043 xen_invalidate_map_cache_entry(block->host);
Jun Nakajima432d2682010-08-31 16:41:25 +01003044 } else {
3045 qemu_vfree(block->host);
3046 }
Alex Williamson04b16652010-07-02 11:13:17 -06003047#endif
3048 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003049 g_free(block);
Alex Williamson04b16652010-07-02 11:13:17 -06003050 return;
3051 }
3052 }
3053
bellarde9a1ab12007-02-08 23:08:38 +00003054}
3055
Huang Yingcd19cfa2011-03-02 08:56:19 +01003056#ifndef _WIN32
3057void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
3058{
3059 RAMBlock *block;
3060 ram_addr_t offset;
3061 int flags;
3062 void *area, *vaddr;
3063
3064 QLIST_FOREACH(block, &ram_list.blocks, next) {
3065 offset = addr - block->offset;
3066 if (offset < block->length) {
3067 vaddr = block->host + offset;
3068 if (block->flags & RAM_PREALLOC_MASK) {
3069 ;
3070 } else {
3071 flags = MAP_FIXED;
3072 munmap(vaddr, length);
3073 if (mem_path) {
3074#if defined(__linux__) && !defined(TARGET_S390X)
3075 if (block->fd) {
3076#ifdef MAP_POPULATE
3077 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
3078 MAP_PRIVATE;
3079#else
3080 flags |= MAP_PRIVATE;
3081#endif
3082 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3083 flags, block->fd, offset);
3084 } else {
3085 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
3086 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3087 flags, -1, 0);
3088 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01003089#else
3090 abort();
Huang Yingcd19cfa2011-03-02 08:56:19 +01003091#endif
3092 } else {
3093#if defined(TARGET_S390X) && defined(CONFIG_KVM)
3094 flags |= MAP_SHARED | MAP_ANONYMOUS;
3095 area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
3096 flags, -1, 0);
3097#else
3098 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
3099 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
3100 flags, -1, 0);
3101#endif
3102 }
3103 if (area != vaddr) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00003104 fprintf(stderr, "Could not remap addr: "
3105 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
Huang Yingcd19cfa2011-03-02 08:56:19 +01003106 length, addr);
3107 exit(1);
3108 }
3109 qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
3110 }
3111 return;
3112 }
3113 }
3114}
3115#endif /* !_WIN32 */
3116
pbrookdc828ca2009-04-09 22:21:07 +00003117/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00003118 With the exception of the softmmu code in this file, this should
3119 only be used for local memory (e.g. video ram) that the device owns,
3120 and knows it isn't going to access beyond the end of the block.
3121
3122 It should not be used for general purpose DMA.
3123 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
3124 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003125void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00003126{
pbrook94a6b542009-04-11 17:15:54 +00003127 RAMBlock *block;
3128
Alex Williamsonf471a172010-06-11 11:11:42 -06003129 QLIST_FOREACH(block, &ram_list.blocks, next) {
3130 if (addr - block->offset < block->length) {
Vincent Palatin7d82af32011-03-10 15:47:46 -05003131 /* Move this entry to to start of the list. */
3132 if (block != QLIST_FIRST(&ram_list.blocks)) {
3133 QLIST_REMOVE(block, next);
3134 QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
3135 }
Jan Kiszka868bb332011-06-21 22:59:09 +02003136 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003137 /* We need to check if the requested address is in the RAM
3138 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003139 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01003140 */
3141 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003142 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01003143 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003144 block->host =
3145 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01003146 }
3147 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003148 return block->host + (addr - block->offset);
3149 }
pbrook94a6b542009-04-11 17:15:54 +00003150 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003151
3152 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3153 abort();
3154
3155 return NULL;
pbrookdc828ca2009-04-09 22:21:07 +00003156}
3157
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02003158/* Return a host pointer to ram allocated with qemu_ram_alloc.
3159 * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
3160 */
3161void *qemu_safe_ram_ptr(ram_addr_t addr)
3162{
3163 RAMBlock *block;
3164
3165 QLIST_FOREACH(block, &ram_list.blocks, next) {
3166 if (addr - block->offset < block->length) {
Jan Kiszka868bb332011-06-21 22:59:09 +02003167 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003168 /* We need to check if the requested address is in the RAM
3169 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003170 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01003171 */
3172 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003173 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01003174 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003175 block->host =
3176 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01003177 }
3178 }
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02003179 return block->host + (addr - block->offset);
3180 }
3181 }
3182
3183 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3184 abort();
3185
3186 return NULL;
3187}
3188
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003189/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
3190 * but takes a size argument */
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01003191void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003192{
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01003193 if (*size == 0) {
3194 return NULL;
3195 }
Jan Kiszka868bb332011-06-21 22:59:09 +02003196 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003197 return xen_map_cache(addr, *size, 1);
Jan Kiszka868bb332011-06-21 22:59:09 +02003198 } else {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003199 RAMBlock *block;
3200
3201 QLIST_FOREACH(block, &ram_list.blocks, next) {
3202 if (addr - block->offset < block->length) {
3203 if (addr - block->offset + *size > block->length)
3204 *size = block->length - addr + block->offset;
3205 return block->host + (addr - block->offset);
3206 }
3207 }
3208
3209 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
3210 abort();
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01003211 }
3212}
3213
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003214void qemu_put_ram_ptr(void *addr)
3215{
3216 trace_qemu_put_ram_ptr(addr);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01003217}
3218
Marcelo Tosattie8902612010-10-11 15:31:19 -03003219int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00003220{
pbrook94a6b542009-04-11 17:15:54 +00003221 RAMBlock *block;
3222 uint8_t *host = ptr;
3223
Jan Kiszka868bb332011-06-21 22:59:09 +02003224 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02003225 *ram_addr = xen_ram_addr_from_mapcache(ptr);
Stefano Stabellini712c2b42011-05-19 18:35:46 +01003226 return 0;
3227 }
3228
Alex Williamsonf471a172010-06-11 11:11:42 -06003229 QLIST_FOREACH(block, &ram_list.blocks, next) {
Jun Nakajima432d2682010-08-31 16:41:25 +01003230 /* This case append when the block is not mapped. */
3231 if (block->host == NULL) {
3232 continue;
3233 }
Alex Williamsonf471a172010-06-11 11:11:42 -06003234 if (host - block->host < block->length) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03003235 *ram_addr = block->offset + (host - block->host);
3236 return 0;
Alex Williamsonf471a172010-06-11 11:11:42 -06003237 }
pbrook94a6b542009-04-11 17:15:54 +00003238 }
Jun Nakajima432d2682010-08-31 16:41:25 +01003239
Marcelo Tosattie8902612010-10-11 15:31:19 -03003240 return -1;
3241}
Alex Williamsonf471a172010-06-11 11:11:42 -06003242
Marcelo Tosattie8902612010-10-11 15:31:19 -03003243/* Some of the softmmu routines need to translate from a host pointer
3244 (typically a TLB entry) back to a ram offset. */
3245ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
3246{
3247 ram_addr_t ram_addr;
Alex Williamsonf471a172010-06-11 11:11:42 -06003248
Marcelo Tosattie8902612010-10-11 15:31:19 -03003249 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
3250 fprintf(stderr, "Bad ram pointer %p\n", ptr);
3251 abort();
3252 }
3253 return ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00003254}
3255
Anthony Liguoric227f092009-10-01 16:12:16 -05003256static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
bellard33417e72003-08-10 21:47:01 +00003257{
pbrook67d3b952006-12-18 05:03:52 +00003258#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00003259 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
pbrook67d3b952006-12-18 05:03:52 +00003260#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003261#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003262 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 1);
blueswir1e18231a2008-10-06 18:46:28 +00003263#endif
3264 return 0;
3265}
3266
Anthony Liguoric227f092009-10-01 16:12:16 -05003267static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00003268{
3269#ifdef DEBUG_UNASSIGNED
3270 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3271#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003272#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003273 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 2);
blueswir1e18231a2008-10-06 18:46:28 +00003274#endif
3275 return 0;
3276}
3277
Anthony Liguoric227f092009-10-01 16:12:16 -05003278static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
blueswir1e18231a2008-10-06 18:46:28 +00003279{
3280#ifdef DEBUG_UNASSIGNED
3281 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
3282#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003283#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003284 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00003285#endif
bellard33417e72003-08-10 21:47:01 +00003286 return 0;
3287}
3288
Anthony Liguoric227f092009-10-01 16:12:16 -05003289static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard33417e72003-08-10 21:47:01 +00003290{
pbrook67d3b952006-12-18 05:03:52 +00003291#ifdef DEBUG_UNASSIGNED
blueswir1ab3d1722007-11-04 07:31:40 +00003292 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
pbrook67d3b952006-12-18 05:03:52 +00003293#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003294#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003295 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 1);
blueswir1e18231a2008-10-06 18:46:28 +00003296#endif
3297}
3298
Anthony Liguoric227f092009-10-01 16:12:16 -05003299static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003300{
3301#ifdef DEBUG_UNASSIGNED
3302 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3303#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003304#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003305 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 2);
blueswir1e18231a2008-10-06 18:46:28 +00003306#endif
3307}
3308
Anthony Liguoric227f092009-10-01 16:12:16 -05003309static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
blueswir1e18231a2008-10-06 18:46:28 +00003310{
3311#ifdef DEBUG_UNASSIGNED
3312 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
3313#endif
Richard Henderson5b450402011-04-18 16:13:12 -07003314#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
Blue Swirlb14ef7c2011-07-03 08:53:46 +00003315 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 4);
blueswir1b4f0a312007-05-06 17:59:24 +00003316#endif
bellard33417e72003-08-10 21:47:01 +00003317}
3318
Blue Swirld60efc62009-08-25 18:29:31 +00003319static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
bellard33417e72003-08-10 21:47:01 +00003320 unassigned_mem_readb,
blueswir1e18231a2008-10-06 18:46:28 +00003321 unassigned_mem_readw,
3322 unassigned_mem_readl,
bellard33417e72003-08-10 21:47:01 +00003323};
3324
Blue Swirld60efc62009-08-25 18:29:31 +00003325static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
bellard33417e72003-08-10 21:47:01 +00003326 unassigned_mem_writeb,
blueswir1e18231a2008-10-06 18:46:28 +00003327 unassigned_mem_writew,
3328 unassigned_mem_writel,
bellard33417e72003-08-10 21:47:01 +00003329};
3330
Anthony Liguoric227f092009-10-01 16:12:16 -05003331static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003332 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003333{
bellard3a7d9292005-08-21 09:26:42 +00003334 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003335 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003336 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3337#if !defined(CONFIG_USER_ONLY)
3338 tb_invalidate_phys_page_fast(ram_addr, 1);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003339 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003340#endif
3341 }
pbrook5579c7f2009-04-11 14:47:08 +00003342 stb_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003343 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003344 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003345 /* we remove the notdirty callback only if the code has been
3346 flushed */
3347 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003348 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003349}
3350
Anthony Liguoric227f092009-10-01 16:12:16 -05003351static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003352 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003353{
bellard3a7d9292005-08-21 09:26:42 +00003354 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003355 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003356 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3357#if !defined(CONFIG_USER_ONLY)
3358 tb_invalidate_phys_page_fast(ram_addr, 2);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003359 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003360#endif
3361 }
pbrook5579c7f2009-04-11 14:47:08 +00003362 stw_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003363 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003364 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003365 /* we remove the notdirty callback only if the code has been
3366 flushed */
3367 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003368 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003369}
3370
Anthony Liguoric227f092009-10-01 16:12:16 -05003371static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
pbrook0f459d12008-06-09 00:20:13 +00003372 uint32_t val)
bellard1ccde1c2004-02-06 19:46:14 +00003373{
bellard3a7d9292005-08-21 09:26:42 +00003374 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003375 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003376 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
3377#if !defined(CONFIG_USER_ONLY)
3378 tb_invalidate_phys_page_fast(ram_addr, 4);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003379 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00003380#endif
3381 }
pbrook5579c7f2009-04-11 14:47:08 +00003382 stl_p(qemu_get_ram_ptr(ram_addr), val);
bellardf23db162005-08-21 19:12:28 +00003383 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09003384 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00003385 /* we remove the notdirty callback only if the code has been
3386 flushed */
3387 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00003388 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00003389}
3390
Blue Swirld60efc62009-08-25 18:29:31 +00003391static CPUReadMemoryFunc * const error_mem_read[3] = {
bellard3a7d9292005-08-21 09:26:42 +00003392 NULL, /* never used */
3393 NULL, /* never used */
3394 NULL, /* never used */
3395};
3396
Blue Swirld60efc62009-08-25 18:29:31 +00003397static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
bellard1ccde1c2004-02-06 19:46:14 +00003398 notdirty_mem_writeb,
3399 notdirty_mem_writew,
3400 notdirty_mem_writel,
3401};
3402
pbrook0f459d12008-06-09 00:20:13 +00003403/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00003404static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00003405{
3406 CPUState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00003407 target_ulong pc, cs_base;
3408 TranslationBlock *tb;
pbrook0f459d12008-06-09 00:20:13 +00003409 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00003410 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00003411 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00003412
aliguori06d55cc2008-11-18 20:24:06 +00003413 if (env->watchpoint_hit) {
3414 /* We re-entered the check after replacing the TB. Now raise
3415 * the debug interrupt so that is will trigger after the
3416 * current instruction. */
3417 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
3418 return;
3419 }
pbrook2e70f6e2008-06-29 01:03:05 +00003420 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003421 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00003422 if ((vaddr == (wp->vaddr & len_mask) ||
3423 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00003424 wp->flags |= BP_WATCHPOINT_HIT;
3425 if (!env->watchpoint_hit) {
3426 env->watchpoint_hit = wp;
3427 tb = tb_find_pc(env->mem_io_pc);
3428 if (!tb) {
3429 cpu_abort(env, "check_watchpoint: could not find TB for "
3430 "pc=%p", (void *)env->mem_io_pc);
3431 }
Stefan Weil618ba8e2011-04-18 06:39:53 +00003432 cpu_restore_state(tb, env, env->mem_io_pc);
aliguori6e140f22008-11-18 20:37:55 +00003433 tb_phys_invalidate(tb, -1);
3434 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
3435 env->exception_index = EXCP_DEBUG;
3436 } else {
3437 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
3438 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
3439 }
3440 cpu_resume_from_signal(env, NULL);
aliguori06d55cc2008-11-18 20:24:06 +00003441 }
aliguori6e140f22008-11-18 20:37:55 +00003442 } else {
3443 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00003444 }
3445 }
3446}
3447
pbrook6658ffb2007-03-16 23:58:11 +00003448/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
3449 so these check for a hit then pass through to the normal out-of-line
3450 phys routines. */
Anthony Liguoric227f092009-10-01 16:12:16 -05003451static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003452{
aliguorib4051332008-11-18 20:14:20 +00003453 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003454 return ldub_phys(addr);
3455}
3456
Anthony Liguoric227f092009-10-01 16:12:16 -05003457static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003458{
aliguorib4051332008-11-18 20:14:20 +00003459 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003460 return lduw_phys(addr);
3461}
3462
Anthony Liguoric227f092009-10-01 16:12:16 -05003463static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
pbrook6658ffb2007-03-16 23:58:11 +00003464{
aliguorib4051332008-11-18 20:14:20 +00003465 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
pbrook6658ffb2007-03-16 23:58:11 +00003466 return ldl_phys(addr);
3467}
3468
Anthony Liguoric227f092009-10-01 16:12:16 -05003469static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003470 uint32_t val)
3471{
aliguorib4051332008-11-18 20:14:20 +00003472 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003473 stb_phys(addr, val);
3474}
3475
Anthony Liguoric227f092009-10-01 16:12:16 -05003476static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003477 uint32_t val)
3478{
aliguorib4051332008-11-18 20:14:20 +00003479 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003480 stw_phys(addr, val);
3481}
3482
Anthony Liguoric227f092009-10-01 16:12:16 -05003483static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
pbrook6658ffb2007-03-16 23:58:11 +00003484 uint32_t val)
3485{
aliguorib4051332008-11-18 20:14:20 +00003486 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
pbrook6658ffb2007-03-16 23:58:11 +00003487 stl_phys(addr, val);
3488}
3489
Blue Swirld60efc62009-08-25 18:29:31 +00003490static CPUReadMemoryFunc * const watch_mem_read[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003491 watch_mem_readb,
3492 watch_mem_readw,
3493 watch_mem_readl,
3494};
3495
Blue Swirld60efc62009-08-25 18:29:31 +00003496static CPUWriteMemoryFunc * const watch_mem_write[3] = {
pbrook6658ffb2007-03-16 23:58:11 +00003497 watch_mem_writeb,
3498 watch_mem_writew,
3499 watch_mem_writel,
3500};
pbrook6658ffb2007-03-16 23:58:11 +00003501
Richard Hendersonf6405242010-04-22 16:47:31 -07003502static inline uint32_t subpage_readlen (subpage_t *mmio,
3503 target_phys_addr_t addr,
3504 unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003505{
Richard Hendersonf6405242010-04-22 16:47:31 -07003506 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003507#if defined(DEBUG_SUBPAGE)
3508 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3509 mmio, len, addr, idx);
3510#endif
blueswir1db7b5422007-05-26 17:36:03 +00003511
Richard Hendersonf6405242010-04-22 16:47:31 -07003512 addr += mmio->region_offset[idx];
3513 idx = mmio->sub_io_index[idx];
3514 return io_mem_read[idx][len](io_mem_opaque[idx], addr);
blueswir1db7b5422007-05-26 17:36:03 +00003515}
3516
Anthony Liguoric227f092009-10-01 16:12:16 -05003517static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
Richard Hendersonf6405242010-04-22 16:47:31 -07003518 uint32_t value, unsigned int len)
blueswir1db7b5422007-05-26 17:36:03 +00003519{
Richard Hendersonf6405242010-04-22 16:47:31 -07003520 unsigned int idx = SUBPAGE_IDX(addr);
blueswir1db7b5422007-05-26 17:36:03 +00003521#if defined(DEBUG_SUBPAGE)
Richard Hendersonf6405242010-04-22 16:47:31 -07003522 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n",
3523 __func__, mmio, len, addr, idx, value);
blueswir1db7b5422007-05-26 17:36:03 +00003524#endif
Richard Hendersonf6405242010-04-22 16:47:31 -07003525
3526 addr += mmio->region_offset[idx];
3527 idx = mmio->sub_io_index[idx];
3528 io_mem_write[idx][len](io_mem_opaque[idx], addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00003529}
3530
Anthony Liguoric227f092009-10-01 16:12:16 -05003531static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003532{
blueswir1db7b5422007-05-26 17:36:03 +00003533 return subpage_readlen(opaque, addr, 0);
3534}
3535
Anthony Liguoric227f092009-10-01 16:12:16 -05003536static void subpage_writeb (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003537 uint32_t value)
3538{
blueswir1db7b5422007-05-26 17:36:03 +00003539 subpage_writelen(opaque, addr, value, 0);
3540}
3541
Anthony Liguoric227f092009-10-01 16:12:16 -05003542static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003543{
blueswir1db7b5422007-05-26 17:36:03 +00003544 return subpage_readlen(opaque, addr, 1);
3545}
3546
Anthony Liguoric227f092009-10-01 16:12:16 -05003547static void subpage_writew (void *opaque, target_phys_addr_t addr,
blueswir1db7b5422007-05-26 17:36:03 +00003548 uint32_t value)
3549{
blueswir1db7b5422007-05-26 17:36:03 +00003550 subpage_writelen(opaque, addr, value, 1);
3551}
3552
Anthony Liguoric227f092009-10-01 16:12:16 -05003553static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
blueswir1db7b5422007-05-26 17:36:03 +00003554{
blueswir1db7b5422007-05-26 17:36:03 +00003555 return subpage_readlen(opaque, addr, 2);
3556}
3557
Richard Hendersonf6405242010-04-22 16:47:31 -07003558static void subpage_writel (void *opaque, target_phys_addr_t addr,
3559 uint32_t value)
blueswir1db7b5422007-05-26 17:36:03 +00003560{
blueswir1db7b5422007-05-26 17:36:03 +00003561 subpage_writelen(opaque, addr, value, 2);
3562}
3563
Blue Swirld60efc62009-08-25 18:29:31 +00003564static CPUReadMemoryFunc * const subpage_read[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003565 &subpage_readb,
3566 &subpage_readw,
3567 &subpage_readl,
3568};
3569
Blue Swirld60efc62009-08-25 18:29:31 +00003570static CPUWriteMemoryFunc * const subpage_write[] = {
blueswir1db7b5422007-05-26 17:36:03 +00003571 &subpage_writeb,
3572 &subpage_writew,
3573 &subpage_writel,
3574};
3575
Andreas Färber56384e82011-11-30 16:26:21 +01003576static uint32_t subpage_ram_readb(void *opaque, target_phys_addr_t addr)
3577{
3578 ram_addr_t raddr = addr;
3579 void *ptr = qemu_get_ram_ptr(raddr);
3580 return ldub_p(ptr);
3581}
3582
3583static void subpage_ram_writeb(void *opaque, target_phys_addr_t addr,
3584 uint32_t value)
3585{
3586 ram_addr_t raddr = addr;
3587 void *ptr = qemu_get_ram_ptr(raddr);
3588 stb_p(ptr, value);
3589}
3590
3591static uint32_t subpage_ram_readw(void *opaque, target_phys_addr_t addr)
3592{
3593 ram_addr_t raddr = addr;
3594 void *ptr = qemu_get_ram_ptr(raddr);
3595 return lduw_p(ptr);
3596}
3597
3598static void subpage_ram_writew(void *opaque, target_phys_addr_t addr,
3599 uint32_t value)
3600{
3601 ram_addr_t raddr = addr;
3602 void *ptr = qemu_get_ram_ptr(raddr);
3603 stw_p(ptr, value);
3604}
3605
3606static uint32_t subpage_ram_readl(void *opaque, target_phys_addr_t addr)
3607{
3608 ram_addr_t raddr = addr;
3609 void *ptr = qemu_get_ram_ptr(raddr);
3610 return ldl_p(ptr);
3611}
3612
3613static void subpage_ram_writel(void *opaque, target_phys_addr_t addr,
3614 uint32_t value)
3615{
3616 ram_addr_t raddr = addr;
3617 void *ptr = qemu_get_ram_ptr(raddr);
3618 stl_p(ptr, value);
3619}
3620
3621static CPUReadMemoryFunc * const subpage_ram_read[] = {
3622 &subpage_ram_readb,
3623 &subpage_ram_readw,
3624 &subpage_ram_readl,
3625};
3626
3627static CPUWriteMemoryFunc * const subpage_ram_write[] = {
3628 &subpage_ram_writeb,
3629 &subpage_ram_writew,
3630 &subpage_ram_writel,
3631};
3632
Anthony Liguoric227f092009-10-01 16:12:16 -05003633static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3634 ram_addr_t memory, ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003635{
3636 int idx, eidx;
3637
3638 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3639 return -1;
3640 idx = SUBPAGE_IDX(start);
3641 eidx = SUBPAGE_IDX(end);
3642#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00003643 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00003644 mmio, start, end, idx, eidx, memory);
3645#endif
Andreas Färber56384e82011-11-30 16:26:21 +01003646 if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
3647 memory = IO_MEM_SUBPAGE_RAM;
3648 }
Richard Hendersonf6405242010-04-22 16:47:31 -07003649 memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
blueswir1db7b5422007-05-26 17:36:03 +00003650 for (; idx <= eidx; idx++) {
Richard Hendersonf6405242010-04-22 16:47:31 -07003651 mmio->sub_io_index[idx] = memory;
3652 mmio->region_offset[idx] = region_offset;
blueswir1db7b5422007-05-26 17:36:03 +00003653 }
3654
3655 return 0;
3656}
3657
Richard Hendersonf6405242010-04-22 16:47:31 -07003658static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3659 ram_addr_t orig_memory,
3660 ram_addr_t region_offset)
blueswir1db7b5422007-05-26 17:36:03 +00003661{
Anthony Liguoric227f092009-10-01 16:12:16 -05003662 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00003663 int subpage_memory;
3664
Anthony Liguori7267c092011-08-20 22:09:37 -05003665 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00003666
3667 mmio->base = base;
Alexander Graf2507c122010-12-08 12:05:37 +01003668 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio,
3669 DEVICE_NATIVE_ENDIAN);
blueswir1db7b5422007-05-26 17:36:03 +00003670#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00003671 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3672 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00003673#endif
aliguori1eec6142009-02-05 22:06:18 +00003674 *phys = subpage_memory | IO_MEM_SUBPAGE;
Richard Hendersonf6405242010-04-22 16:47:31 -07003675 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset);
blueswir1db7b5422007-05-26 17:36:03 +00003676
3677 return mmio;
3678}
3679
aliguori88715652009-02-11 15:20:58 +00003680static int get_free_io_mem_idx(void)
3681{
3682 int i;
3683
3684 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3685 if (!io_mem_used[i]) {
3686 io_mem_used[i] = 1;
3687 return i;
3688 }
Riku Voipioc6703b42009-12-03 15:56:05 +02003689 fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
aliguori88715652009-02-11 15:20:58 +00003690 return -1;
3691}
3692
Alexander Grafdd310532010-12-08 12:05:36 +01003693/*
3694 * Usually, devices operate in little endian mode. There are devices out
3695 * there that operate in big endian too. Each device gets byte swapped
3696 * mmio if plugged onto a CPU that does the other endianness.
3697 *
3698 * CPU Device swap?
3699 *
3700 * little little no
3701 * little big yes
3702 * big little yes
3703 * big big no
3704 */
3705
3706typedef struct SwapEndianContainer {
3707 CPUReadMemoryFunc *read[3];
3708 CPUWriteMemoryFunc *write[3];
3709 void *opaque;
3710} SwapEndianContainer;
3711
3712static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr)
3713{
3714 uint32_t val;
3715 SwapEndianContainer *c = opaque;
3716 val = c->read[0](c->opaque, addr);
3717 return val;
3718}
3719
3720static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr)
3721{
3722 uint32_t val;
3723 SwapEndianContainer *c = opaque;
3724 val = bswap16(c->read[1](c->opaque, addr));
3725 return val;
3726}
3727
3728static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr)
3729{
3730 uint32_t val;
3731 SwapEndianContainer *c = opaque;
3732 val = bswap32(c->read[2](c->opaque, addr));
3733 return val;
3734}
3735
3736static CPUReadMemoryFunc * const swapendian_readfn[3]={
3737 swapendian_mem_readb,
3738 swapendian_mem_readw,
3739 swapendian_mem_readl
3740};
3741
3742static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr,
3743 uint32_t val)
3744{
3745 SwapEndianContainer *c = opaque;
3746 c->write[0](c->opaque, addr, val);
3747}
3748
3749static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr,
3750 uint32_t val)
3751{
3752 SwapEndianContainer *c = opaque;
3753 c->write[1](c->opaque, addr, bswap16(val));
3754}
3755
3756static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr,
3757 uint32_t val)
3758{
3759 SwapEndianContainer *c = opaque;
3760 c->write[2](c->opaque, addr, bswap32(val));
3761}
3762
3763static CPUWriteMemoryFunc * const swapendian_writefn[3]={
3764 swapendian_mem_writeb,
3765 swapendian_mem_writew,
3766 swapendian_mem_writel
3767};
3768
3769static void swapendian_init(int io_index)
3770{
Anthony Liguori7267c092011-08-20 22:09:37 -05003771 SwapEndianContainer *c = g_malloc(sizeof(SwapEndianContainer));
Alexander Grafdd310532010-12-08 12:05:36 +01003772 int i;
3773
3774 /* Swap mmio for big endian targets */
3775 c->opaque = io_mem_opaque[io_index];
3776 for (i = 0; i < 3; i++) {
3777 c->read[i] = io_mem_read[io_index][i];
3778 c->write[i] = io_mem_write[io_index][i];
3779
3780 io_mem_read[io_index][i] = swapendian_readfn[i];
3781 io_mem_write[io_index][i] = swapendian_writefn[i];
3782 }
3783 io_mem_opaque[io_index] = c;
3784}
3785
3786static void swapendian_del(int io_index)
3787{
3788 if (io_mem_read[io_index][0] == swapendian_readfn[0]) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003789 g_free(io_mem_opaque[io_index]);
Alexander Grafdd310532010-12-08 12:05:36 +01003790 }
3791}
3792
bellard33417e72003-08-10 21:47:01 +00003793/* mem_read and mem_write are arrays of functions containing the
3794 function to access byte (index 0), word (index 1) and dword (index
Paul Brook0b4e6e32009-04-30 18:37:55 +01003795 2). Functions can be omitted with a NULL function pointer.
blueswir13ee89922008-01-02 19:45:26 +00003796 If io_index is non zero, the corresponding io zone is
blueswir14254fab2008-01-01 16:57:19 +00003797 modified. If it is zero, a new io zone is allocated. The return
3798 value can be used with cpu_register_physical_memory(). (-1) is
3799 returned if error. */
Avi Kivity1eed09c2009-06-14 11:38:51 +03003800static int cpu_register_io_memory_fixed(int io_index,
Blue Swirld60efc62009-08-25 18:29:31 +00003801 CPUReadMemoryFunc * const *mem_read,
3802 CPUWriteMemoryFunc * const *mem_write,
Alexander Grafdd310532010-12-08 12:05:36 +01003803 void *opaque, enum device_endian endian)
bellard33417e72003-08-10 21:47:01 +00003804{
Richard Henderson3cab7212010-05-07 09:52:51 -07003805 int i;
3806
bellard33417e72003-08-10 21:47:01 +00003807 if (io_index <= 0) {
aliguori88715652009-02-11 15:20:58 +00003808 io_index = get_free_io_mem_idx();
3809 if (io_index == -1)
3810 return io_index;
bellard33417e72003-08-10 21:47:01 +00003811 } else {
Avi Kivity1eed09c2009-06-14 11:38:51 +03003812 io_index >>= IO_MEM_SHIFT;
bellard33417e72003-08-10 21:47:01 +00003813 if (io_index >= IO_MEM_NB_ENTRIES)
3814 return -1;
3815 }
bellardb5ff1b32005-11-26 10:38:39 +00003816
Richard Henderson3cab7212010-05-07 09:52:51 -07003817 for (i = 0; i < 3; ++i) {
3818 io_mem_read[io_index][i]
3819 = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]);
3820 }
3821 for (i = 0; i < 3; ++i) {
3822 io_mem_write[io_index][i]
3823 = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]);
3824 }
bellarda4193c82004-06-03 14:01:43 +00003825 io_mem_opaque[io_index] = opaque;
Richard Hendersonf6405242010-04-22 16:47:31 -07003826
Alexander Grafdd310532010-12-08 12:05:36 +01003827 switch (endian) {
3828 case DEVICE_BIG_ENDIAN:
3829#ifndef TARGET_WORDS_BIGENDIAN
3830 swapendian_init(io_index);
3831#endif
3832 break;
3833 case DEVICE_LITTLE_ENDIAN:
3834#ifdef TARGET_WORDS_BIGENDIAN
3835 swapendian_init(io_index);
3836#endif
3837 break;
3838 case DEVICE_NATIVE_ENDIAN:
3839 default:
3840 break;
3841 }
3842
Richard Hendersonf6405242010-04-22 16:47:31 -07003843 return (io_index << IO_MEM_SHIFT);
bellard33417e72003-08-10 21:47:01 +00003844}
bellard61382a52003-10-27 21:22:23 +00003845
Blue Swirld60efc62009-08-25 18:29:31 +00003846int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
3847 CPUWriteMemoryFunc * const *mem_write,
Alexander Grafdd310532010-12-08 12:05:36 +01003848 void *opaque, enum device_endian endian)
Avi Kivity1eed09c2009-06-14 11:38:51 +03003849{
Alexander Graf2507c122010-12-08 12:05:37 +01003850 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian);
Avi Kivity1eed09c2009-06-14 11:38:51 +03003851}
3852
aliguori88715652009-02-11 15:20:58 +00003853void cpu_unregister_io_memory(int io_table_address)
3854{
3855 int i;
3856 int io_index = io_table_address >> IO_MEM_SHIFT;
3857
Alexander Grafdd310532010-12-08 12:05:36 +01003858 swapendian_del(io_index);
3859
aliguori88715652009-02-11 15:20:58 +00003860 for (i=0;i < 3; i++) {
3861 io_mem_read[io_index][i] = unassigned_mem_read[i];
3862 io_mem_write[io_index][i] = unassigned_mem_write[i];
3863 }
3864 io_mem_opaque[io_index] = NULL;
3865 io_mem_used[io_index] = 0;
3866}
3867
Avi Kivitye9179ce2009-06-14 11:38:52 +03003868static void io_mem_init(void)
3869{
3870 int i;
3871
Alexander Graf2507c122010-12-08 12:05:37 +01003872 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read,
3873 unassigned_mem_write, NULL,
3874 DEVICE_NATIVE_ENDIAN);
3875 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read,
3876 unassigned_mem_write, NULL,
3877 DEVICE_NATIVE_ENDIAN);
3878 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read,
3879 notdirty_mem_write, NULL,
3880 DEVICE_NATIVE_ENDIAN);
Andreas Färber56384e82011-11-30 16:26:21 +01003881 cpu_register_io_memory_fixed(IO_MEM_SUBPAGE_RAM, subpage_ram_read,
3882 subpage_ram_write, NULL,
3883 DEVICE_NATIVE_ENDIAN);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003884 for (i=0; i<5; i++)
3885 io_mem_used[i] = 1;
3886
3887 io_mem_watch = cpu_register_io_memory(watch_mem_read,
Alexander Graf2507c122010-12-08 12:05:37 +01003888 watch_mem_write, NULL,
3889 DEVICE_NATIVE_ENDIAN);
Avi Kivitye9179ce2009-06-14 11:38:52 +03003890}
3891
Avi Kivity62152b82011-07-26 14:26:14 +03003892static void memory_map_init(void)
3893{
Anthony Liguori7267c092011-08-20 22:09:37 -05003894 system_memory = g_malloc(sizeof(*system_memory));
Avi Kivity8417ceb2011-08-03 11:56:14 +03003895 memory_region_init(system_memory, "system", INT64_MAX);
Avi Kivity62152b82011-07-26 14:26:14 +03003896 set_system_memory_map(system_memory);
Avi Kivity309cb472011-08-08 16:09:03 +03003897
Anthony Liguori7267c092011-08-20 22:09:37 -05003898 system_io = g_malloc(sizeof(*system_io));
Avi Kivity309cb472011-08-08 16:09:03 +03003899 memory_region_init(system_io, "io", 65536);
3900 set_system_io_map(system_io);
Avi Kivity62152b82011-07-26 14:26:14 +03003901}
3902
3903MemoryRegion *get_system_memory(void)
3904{
3905 return system_memory;
3906}
3907
Avi Kivity309cb472011-08-08 16:09:03 +03003908MemoryRegion *get_system_io(void)
3909{
3910 return system_io;
3911}
3912
pbrooke2eef172008-06-08 01:09:01 +00003913#endif /* !defined(CONFIG_USER_ONLY) */
3914
bellard13eb76e2004-01-24 15:23:36 +00003915/* physical memory access (slow version, mainly for debug) */
3916#if defined(CONFIG_USER_ONLY)
Paul Brooka68fe892010-03-01 00:08:59 +00003917int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3918 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00003919{
3920 int l, flags;
3921 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00003922 void * p;
bellard13eb76e2004-01-24 15:23:36 +00003923
3924 while (len > 0) {
3925 page = addr & TARGET_PAGE_MASK;
3926 l = (page + TARGET_PAGE_SIZE) - addr;
3927 if (l > len)
3928 l = len;
3929 flags = page_get_flags(page);
3930 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00003931 return -1;
bellard13eb76e2004-01-24 15:23:36 +00003932 if (is_write) {
3933 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00003934 return -1;
bellard579a97f2007-11-11 14:26:47 +00003935 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003936 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00003937 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003938 memcpy(p, buf, l);
3939 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00003940 } else {
3941 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00003942 return -1;
bellard579a97f2007-11-11 14:26:47 +00003943 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00003944 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00003945 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00003946 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00003947 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00003948 }
3949 len -= l;
3950 buf += l;
3951 addr += l;
3952 }
Paul Brooka68fe892010-03-01 00:08:59 +00003953 return 0;
bellard13eb76e2004-01-24 15:23:36 +00003954}
bellard8df1cd02005-01-28 22:37:22 +00003955
bellard13eb76e2004-01-24 15:23:36 +00003956#else
Anthony Liguoric227f092009-10-01 16:12:16 -05003957void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +00003958 int len, int is_write)
3959{
3960 int l, io_index;
3961 uint8_t *ptr;
3962 uint32_t val;
Anthony Liguoric227f092009-10-01 16:12:16 -05003963 target_phys_addr_t page;
Anthony PERARD8ca56922011-07-15 04:32:53 +00003964 ram_addr_t pd;
bellard92e873b2004-05-21 14:52:29 +00003965 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00003966
bellard13eb76e2004-01-24 15:23:36 +00003967 while (len > 0) {
3968 page = addr & TARGET_PAGE_MASK;
3969 l = (page + TARGET_PAGE_SIZE) - addr;
3970 if (l > len)
3971 l = len;
bellard92e873b2004-05-21 14:52:29 +00003972 p = phys_page_find(page >> TARGET_PAGE_BITS);
bellard13eb76e2004-01-24 15:23:36 +00003973 if (!p) {
3974 pd = IO_MEM_UNASSIGNED;
3975 } else {
3976 pd = p->phys_offset;
3977 }
ths3b46e622007-09-17 08:09:54 +00003978
bellard13eb76e2004-01-24 15:23:36 +00003979 if (is_write) {
bellard3a7d9292005-08-21 09:26:42 +00003980 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003981 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00003982 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00003983 if (p)
aurel326c2934d2009-02-18 21:37:17 +00003984 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard6a00d602005-11-21 23:25:50 +00003985 /* XXX: could force cpu_single_env to NULL to avoid
3986 potential bugs */
aurel326c2934d2009-02-18 21:37:17 +00003987 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003988 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003989 val = ldl_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003990 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003991 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00003992 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard1c213d12005-09-03 10:49:04 +00003993 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003994 val = lduw_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00003995 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00003996 l = 2;
3997 } else {
bellard1c213d12005-09-03 10:49:04 +00003998 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00003999 val = ldub_p(buf);
aurel326c2934d2009-02-18 21:37:17 +00004000 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
bellard13eb76e2004-01-24 15:23:36 +00004001 l = 1;
4002 }
4003 } else {
Anthony PERARD8ca56922011-07-15 04:32:53 +00004004 ram_addr_t addr1;
bellardb448f2f2004-02-25 23:24:04 +00004005 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
bellard13eb76e2004-01-24 15:23:36 +00004006 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004007 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00004008 memcpy(ptr, buf, l);
bellard3a7d9292005-08-21 09:26:42 +00004009 if (!cpu_physical_memory_is_dirty(addr1)) {
4010 /* invalidate code */
4011 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
4012 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004013 cpu_physical_memory_set_dirty_flags(
4014 addr1, (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00004015 }
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004016 qemu_put_ram_ptr(ptr);
bellard13eb76e2004-01-24 15:23:36 +00004017 }
4018 } else {
ths5fafdf22007-09-16 21:08:06 +00004019 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00004020 !(pd & IO_MEM_ROMD)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004021 target_phys_addr_t addr1 = addr;
bellard13eb76e2004-01-24 15:23:36 +00004022 /* I/O case */
4023 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004024 if (p)
aurel326c2934d2009-02-18 21:37:17 +00004025 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4026 if (l >= 4 && ((addr1 & 3) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00004027 /* 32 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00004028 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00004029 stl_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00004030 l = 4;
aurel326c2934d2009-02-18 21:37:17 +00004031 } else if (l >= 2 && ((addr1 & 1) == 0)) {
bellard13eb76e2004-01-24 15:23:36 +00004032 /* 16 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00004033 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00004034 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00004035 l = 2;
4036 } else {
bellard1c213d12005-09-03 10:49:04 +00004037 /* 8 bit read access */
aurel326c2934d2009-02-18 21:37:17 +00004038 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
bellardc27004e2005-01-03 23:35:10 +00004039 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00004040 l = 1;
4041 }
4042 } else {
4043 /* RAM case */
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004044 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
4045 memcpy(buf, ptr + (addr & ~TARGET_PAGE_MASK), l);
4046 qemu_put_ram_ptr(ptr);
bellard13eb76e2004-01-24 15:23:36 +00004047 }
4048 }
4049 len -= l;
4050 buf += l;
4051 addr += l;
4052 }
4053}
bellard8df1cd02005-01-28 22:37:22 +00004054
bellardd0ecd2a2006-04-23 17:14:48 +00004055/* used for ROM loading : can write in RAM and ROM */
Anthony Liguoric227f092009-10-01 16:12:16 -05004056void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +00004057 const uint8_t *buf, int len)
4058{
4059 int l;
4060 uint8_t *ptr;
Anthony Liguoric227f092009-10-01 16:12:16 -05004061 target_phys_addr_t page;
bellardd0ecd2a2006-04-23 17:14:48 +00004062 unsigned long pd;
4063 PhysPageDesc *p;
ths3b46e622007-09-17 08:09:54 +00004064
bellardd0ecd2a2006-04-23 17:14:48 +00004065 while (len > 0) {
4066 page = addr & TARGET_PAGE_MASK;
4067 l = (page + TARGET_PAGE_SIZE) - addr;
4068 if (l > len)
4069 l = len;
4070 p = phys_page_find(page >> TARGET_PAGE_BITS);
4071 if (!p) {
4072 pd = IO_MEM_UNASSIGNED;
4073 } else {
4074 pd = p->phys_offset;
4075 }
ths3b46e622007-09-17 08:09:54 +00004076
bellardd0ecd2a2006-04-23 17:14:48 +00004077 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
bellard2a4188a2006-06-25 21:54:59 +00004078 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
4079 !(pd & IO_MEM_ROMD)) {
bellardd0ecd2a2006-04-23 17:14:48 +00004080 /* do nothing */
4081 } else {
4082 unsigned long addr1;
4083 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4084 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004085 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00004086 memcpy(ptr, buf, l);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004087 qemu_put_ram_ptr(ptr);
bellardd0ecd2a2006-04-23 17:14:48 +00004088 }
4089 len -= l;
4090 buf += l;
4091 addr += l;
4092 }
4093}
4094
aliguori6d16c2f2009-01-22 16:59:11 +00004095typedef struct {
4096 void *buffer;
Anthony Liguoric227f092009-10-01 16:12:16 -05004097 target_phys_addr_t addr;
4098 target_phys_addr_t len;
aliguori6d16c2f2009-01-22 16:59:11 +00004099} BounceBuffer;
4100
4101static BounceBuffer bounce;
4102
aliguoriba223c22009-01-22 16:59:16 +00004103typedef struct MapClient {
4104 void *opaque;
4105 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004106 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00004107} MapClient;
4108
Blue Swirl72cf2d42009-09-12 07:36:22 +00004109static QLIST_HEAD(map_client_list, MapClient) map_client_list
4110 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00004111
4112void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
4113{
Anthony Liguori7267c092011-08-20 22:09:37 -05004114 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00004115
4116 client->opaque = opaque;
4117 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004118 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00004119 return client;
4120}
4121
4122void cpu_unregister_map_client(void *_client)
4123{
4124 MapClient *client = (MapClient *)_client;
4125
Blue Swirl72cf2d42009-09-12 07:36:22 +00004126 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05004127 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00004128}
4129
4130static void cpu_notify_map_clients(void)
4131{
4132 MapClient *client;
4133
Blue Swirl72cf2d42009-09-12 07:36:22 +00004134 while (!QLIST_EMPTY(&map_client_list)) {
4135 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00004136 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09004137 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00004138 }
4139}
4140
aliguori6d16c2f2009-01-22 16:59:11 +00004141/* Map a physical memory region into a host virtual address.
4142 * May map a subset of the requested range, given by and returned in *plen.
4143 * May return NULL if resources needed to perform the mapping are exhausted.
4144 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00004145 * Use cpu_register_map_client() to know when retrying the map operation is
4146 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00004147 */
Anthony Liguoric227f092009-10-01 16:12:16 -05004148void *cpu_physical_memory_map(target_phys_addr_t addr,
4149 target_phys_addr_t *plen,
aliguori6d16c2f2009-01-22 16:59:11 +00004150 int is_write)
4151{
Anthony Liguoric227f092009-10-01 16:12:16 -05004152 target_phys_addr_t len = *plen;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004153 target_phys_addr_t todo = 0;
aliguori6d16c2f2009-01-22 16:59:11 +00004154 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05004155 target_phys_addr_t page;
aliguori6d16c2f2009-01-22 16:59:11 +00004156 unsigned long pd;
4157 PhysPageDesc *p;
Anthony PERARDf15fbc42011-07-20 08:17:42 +00004158 ram_addr_t raddr = RAM_ADDR_MAX;
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004159 ram_addr_t rlen;
4160 void *ret;
aliguori6d16c2f2009-01-22 16:59:11 +00004161
4162 while (len > 0) {
4163 page = addr & TARGET_PAGE_MASK;
4164 l = (page + TARGET_PAGE_SIZE) - addr;
4165 if (l > len)
4166 l = len;
4167 p = phys_page_find(page >> TARGET_PAGE_BITS);
4168 if (!p) {
4169 pd = IO_MEM_UNASSIGNED;
4170 } else {
4171 pd = p->phys_offset;
4172 }
4173
4174 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004175 if (todo || bounce.buffer) {
aliguori6d16c2f2009-01-22 16:59:11 +00004176 break;
4177 }
4178 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
4179 bounce.addr = addr;
4180 bounce.len = l;
4181 if (!is_write) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02004182 cpu_physical_memory_read(addr, bounce.buffer, l);
aliguori6d16c2f2009-01-22 16:59:11 +00004183 }
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004184
4185 *plen = l;
4186 return bounce.buffer;
aliguori6d16c2f2009-01-22 16:59:11 +00004187 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004188 if (!todo) {
4189 raddr = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4190 }
aliguori6d16c2f2009-01-22 16:59:11 +00004191
4192 len -= l;
4193 addr += l;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01004194 todo += l;
aliguori6d16c2f2009-01-22 16:59:11 +00004195 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01004196 rlen = todo;
4197 ret = qemu_ram_ptr_length(raddr, &rlen);
4198 *plen = rlen;
4199 return ret;
aliguori6d16c2f2009-01-22 16:59:11 +00004200}
4201
4202/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
4203 * Will also mark the memory as dirty if is_write == 1. access_len gives
4204 * the amount of memory that was actually read or written by the caller.
4205 */
Anthony Liguoric227f092009-10-01 16:12:16 -05004206void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
4207 int is_write, target_phys_addr_t access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00004208{
4209 if (buffer != bounce.buffer) {
4210 if (is_write) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03004211 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00004212 while (access_len) {
4213 unsigned l;
4214 l = TARGET_PAGE_SIZE;
4215 if (l > access_len)
4216 l = access_len;
4217 if (!cpu_physical_memory_is_dirty(addr1)) {
4218 /* invalidate code */
4219 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
4220 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004221 cpu_physical_memory_set_dirty_flags(
4222 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori6d16c2f2009-01-22 16:59:11 +00004223 }
4224 addr1 += l;
4225 access_len -= l;
4226 }
4227 }
Jan Kiszka868bb332011-06-21 22:59:09 +02004228 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02004229 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01004230 }
aliguori6d16c2f2009-01-22 16:59:11 +00004231 return;
4232 }
4233 if (is_write) {
4234 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
4235 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00004236 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00004237 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00004238 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00004239}
bellardd0ecd2a2006-04-23 17:14:48 +00004240
bellard8df1cd02005-01-28 22:37:22 +00004241/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004242static inline uint32_t ldl_phys_internal(target_phys_addr_t addr,
4243 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00004244{
4245 int io_index;
4246 uint8_t *ptr;
4247 uint32_t val;
4248 unsigned long pd;
4249 PhysPageDesc *p;
4250
4251 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4252 if (!p) {
4253 pd = IO_MEM_UNASSIGNED;
4254 } else {
4255 pd = p->phys_offset;
4256 }
ths3b46e622007-09-17 08:09:54 +00004257
ths5fafdf22007-09-16 21:08:06 +00004258 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
bellard2a4188a2006-06-25 21:54:59 +00004259 !(pd & IO_MEM_ROMD)) {
bellard8df1cd02005-01-28 22:37:22 +00004260 /* I/O case */
4261 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004262 if (p)
4263 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00004264 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004265#if defined(TARGET_WORDS_BIGENDIAN)
4266 if (endian == DEVICE_LITTLE_ENDIAN) {
4267 val = bswap32(val);
4268 }
4269#else
4270 if (endian == DEVICE_BIG_ENDIAN) {
4271 val = bswap32(val);
4272 }
4273#endif
bellard8df1cd02005-01-28 22:37:22 +00004274 } else {
4275 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004276 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard8df1cd02005-01-28 22:37:22 +00004277 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004278 switch (endian) {
4279 case DEVICE_LITTLE_ENDIAN:
4280 val = ldl_le_p(ptr);
4281 break;
4282 case DEVICE_BIG_ENDIAN:
4283 val = ldl_be_p(ptr);
4284 break;
4285 default:
4286 val = ldl_p(ptr);
4287 break;
4288 }
bellard8df1cd02005-01-28 22:37:22 +00004289 }
4290 return val;
4291}
4292
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004293uint32_t ldl_phys(target_phys_addr_t addr)
4294{
4295 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4296}
4297
4298uint32_t ldl_le_phys(target_phys_addr_t addr)
4299{
4300 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4301}
4302
4303uint32_t ldl_be_phys(target_phys_addr_t addr)
4304{
4305 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
4306}
4307
bellard84b7b8e2005-11-28 21:19:04 +00004308/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004309static inline uint64_t ldq_phys_internal(target_phys_addr_t addr,
4310 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00004311{
4312 int io_index;
4313 uint8_t *ptr;
4314 uint64_t val;
4315 unsigned long pd;
4316 PhysPageDesc *p;
4317
4318 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4319 if (!p) {
4320 pd = IO_MEM_UNASSIGNED;
4321 } else {
4322 pd = p->phys_offset;
4323 }
ths3b46e622007-09-17 08:09:54 +00004324
bellard2a4188a2006-06-25 21:54:59 +00004325 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
4326 !(pd & IO_MEM_ROMD)) {
bellard84b7b8e2005-11-28 21:19:04 +00004327 /* I/O case */
4328 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004329 if (p)
4330 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004331
4332 /* XXX This is broken when device endian != cpu endian.
4333 Fix and add "endian" variable check */
bellard84b7b8e2005-11-28 21:19:04 +00004334#ifdef TARGET_WORDS_BIGENDIAN
4335 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
4336 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
4337#else
4338 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
4339 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
4340#endif
4341 } else {
4342 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004343 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
bellard84b7b8e2005-11-28 21:19:04 +00004344 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004345 switch (endian) {
4346 case DEVICE_LITTLE_ENDIAN:
4347 val = ldq_le_p(ptr);
4348 break;
4349 case DEVICE_BIG_ENDIAN:
4350 val = ldq_be_p(ptr);
4351 break;
4352 default:
4353 val = ldq_p(ptr);
4354 break;
4355 }
bellard84b7b8e2005-11-28 21:19:04 +00004356 }
4357 return val;
4358}
4359
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004360uint64_t ldq_phys(target_phys_addr_t addr)
4361{
4362 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4363}
4364
4365uint64_t ldq_le_phys(target_phys_addr_t addr)
4366{
4367 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4368}
4369
4370uint64_t ldq_be_phys(target_phys_addr_t addr)
4371{
4372 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
4373}
4374
bellardaab33092005-10-30 20:48:42 +00004375/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004376uint32_t ldub_phys(target_phys_addr_t addr)
bellardaab33092005-10-30 20:48:42 +00004377{
4378 uint8_t val;
4379 cpu_physical_memory_read(addr, &val, 1);
4380 return val;
4381}
4382
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004383/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004384static inline uint32_t lduw_phys_internal(target_phys_addr_t addr,
4385 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00004386{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004387 int io_index;
4388 uint8_t *ptr;
4389 uint64_t val;
4390 unsigned long pd;
4391 PhysPageDesc *p;
4392
4393 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4394 if (!p) {
4395 pd = IO_MEM_UNASSIGNED;
4396 } else {
4397 pd = p->phys_offset;
4398 }
4399
4400 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
4401 !(pd & IO_MEM_ROMD)) {
4402 /* I/O case */
4403 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4404 if (p)
4405 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
4406 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004407#if defined(TARGET_WORDS_BIGENDIAN)
4408 if (endian == DEVICE_LITTLE_ENDIAN) {
4409 val = bswap16(val);
4410 }
4411#else
4412 if (endian == DEVICE_BIG_ENDIAN) {
4413 val = bswap16(val);
4414 }
4415#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004416 } else {
4417 /* RAM case */
4418 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
4419 (addr & ~TARGET_PAGE_MASK);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004420 switch (endian) {
4421 case DEVICE_LITTLE_ENDIAN:
4422 val = lduw_le_p(ptr);
4423 break;
4424 case DEVICE_BIG_ENDIAN:
4425 val = lduw_be_p(ptr);
4426 break;
4427 default:
4428 val = lduw_p(ptr);
4429 break;
4430 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004431 }
4432 return val;
bellardaab33092005-10-30 20:48:42 +00004433}
4434
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004435uint32_t lduw_phys(target_phys_addr_t addr)
4436{
4437 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
4438}
4439
4440uint32_t lduw_le_phys(target_phys_addr_t addr)
4441{
4442 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
4443}
4444
4445uint32_t lduw_be_phys(target_phys_addr_t addr)
4446{
4447 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
4448}
4449
bellard8df1cd02005-01-28 22:37:22 +00004450/* warning: addr must be aligned. The ram page is not masked as dirty
4451 and the code inside is not invalidated. It is useful if the dirty
4452 bits are used to track modified PTEs */
Anthony Liguoric227f092009-10-01 16:12:16 -05004453void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00004454{
4455 int io_index;
4456 uint8_t *ptr;
4457 unsigned long pd;
4458 PhysPageDesc *p;
4459
4460 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4461 if (!p) {
4462 pd = IO_MEM_UNASSIGNED;
4463 } else {
4464 pd = p->phys_offset;
4465 }
ths3b46e622007-09-17 08:09:54 +00004466
bellard3a7d9292005-08-21 09:26:42 +00004467 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00004468 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004469 if (p)
4470 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
bellard8df1cd02005-01-28 22:37:22 +00004471 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4472 } else {
aliguori74576192008-10-06 14:02:03 +00004473 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
pbrook5579c7f2009-04-11 14:47:08 +00004474 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00004475 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00004476
4477 if (unlikely(in_migration)) {
4478 if (!cpu_physical_memory_is_dirty(addr1)) {
4479 /* invalidate code */
4480 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4481 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004482 cpu_physical_memory_set_dirty_flags(
4483 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00004484 }
4485 }
bellard8df1cd02005-01-28 22:37:22 +00004486 }
4487}
4488
Anthony Liguoric227f092009-10-01 16:12:16 -05004489void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
j_mayerbc98a7e2007-04-04 07:55:12 +00004490{
4491 int io_index;
4492 uint8_t *ptr;
4493 unsigned long pd;
4494 PhysPageDesc *p;
4495
4496 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4497 if (!p) {
4498 pd = IO_MEM_UNASSIGNED;
4499 } else {
4500 pd = p->phys_offset;
4501 }
ths3b46e622007-09-17 08:09:54 +00004502
j_mayerbc98a7e2007-04-04 07:55:12 +00004503 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4504 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004505 if (p)
4506 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
j_mayerbc98a7e2007-04-04 07:55:12 +00004507#ifdef TARGET_WORDS_BIGENDIAN
4508 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
4509 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
4510#else
4511 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4512 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
4513#endif
4514 } else {
pbrook5579c7f2009-04-11 14:47:08 +00004515 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
j_mayerbc98a7e2007-04-04 07:55:12 +00004516 (addr & ~TARGET_PAGE_MASK);
4517 stq_p(ptr, val);
4518 }
4519}
4520
bellard8df1cd02005-01-28 22:37:22 +00004521/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004522static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val,
4523 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00004524{
4525 int io_index;
4526 uint8_t *ptr;
4527 unsigned long pd;
4528 PhysPageDesc *p;
4529
4530 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4531 if (!p) {
4532 pd = IO_MEM_UNASSIGNED;
4533 } else {
4534 pd = p->phys_offset;
4535 }
ths3b46e622007-09-17 08:09:54 +00004536
bellard3a7d9292005-08-21 09:26:42 +00004537 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
bellard8df1cd02005-01-28 22:37:22 +00004538 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
pbrook8da3ff12008-12-01 18:59:50 +00004539 if (p)
4540 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004541#if defined(TARGET_WORDS_BIGENDIAN)
4542 if (endian == DEVICE_LITTLE_ENDIAN) {
4543 val = bswap32(val);
4544 }
4545#else
4546 if (endian == DEVICE_BIG_ENDIAN) {
4547 val = bswap32(val);
4548 }
4549#endif
bellard8df1cd02005-01-28 22:37:22 +00004550 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
4551 } else {
4552 unsigned long addr1;
4553 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4554 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00004555 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004556 switch (endian) {
4557 case DEVICE_LITTLE_ENDIAN:
4558 stl_le_p(ptr, val);
4559 break;
4560 case DEVICE_BIG_ENDIAN:
4561 stl_be_p(ptr, val);
4562 break;
4563 default:
4564 stl_p(ptr, val);
4565 break;
4566 }
bellard3a7d9292005-08-21 09:26:42 +00004567 if (!cpu_physical_memory_is_dirty(addr1)) {
4568 /* invalidate code */
4569 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
4570 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09004571 cpu_physical_memory_set_dirty_flags(addr1,
4572 (0xff & ~CODE_DIRTY_FLAG));
bellard3a7d9292005-08-21 09:26:42 +00004573 }
bellard8df1cd02005-01-28 22:37:22 +00004574 }
4575}
4576
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004577void stl_phys(target_phys_addr_t addr, uint32_t val)
4578{
4579 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
4580}
4581
4582void stl_le_phys(target_phys_addr_t addr, uint32_t val)
4583{
4584 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
4585}
4586
4587void stl_be_phys(target_phys_addr_t addr, uint32_t val)
4588{
4589 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
4590}
4591
bellardaab33092005-10-30 20:48:42 +00004592/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004593void stb_phys(target_phys_addr_t addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00004594{
4595 uint8_t v = val;
4596 cpu_physical_memory_write(addr, &v, 1);
4597}
4598
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004599/* warning: addr must be aligned */
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004600static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val,
4601 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00004602{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004603 int io_index;
4604 uint8_t *ptr;
4605 unsigned long pd;
4606 PhysPageDesc *p;
4607
4608 p = phys_page_find(addr >> TARGET_PAGE_BITS);
4609 if (!p) {
4610 pd = IO_MEM_UNASSIGNED;
4611 } else {
4612 pd = p->phys_offset;
4613 }
4614
4615 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
4616 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
4617 if (p)
4618 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004619#if defined(TARGET_WORDS_BIGENDIAN)
4620 if (endian == DEVICE_LITTLE_ENDIAN) {
4621 val = bswap16(val);
4622 }
4623#else
4624 if (endian == DEVICE_BIG_ENDIAN) {
4625 val = bswap16(val);
4626 }
4627#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004628 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
4629 } else {
4630 unsigned long addr1;
4631 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
4632 /* RAM case */
4633 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004634 switch (endian) {
4635 case DEVICE_LITTLE_ENDIAN:
4636 stw_le_p(ptr, val);
4637 break;
4638 case DEVICE_BIG_ENDIAN:
4639 stw_be_p(ptr, val);
4640 break;
4641 default:
4642 stw_p(ptr, val);
4643 break;
4644 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03004645 if (!cpu_physical_memory_is_dirty(addr1)) {
4646 /* invalidate code */
4647 tb_invalidate_phys_page_range(addr1, addr1 + 2, 0);
4648 /* set dirty bit */
4649 cpu_physical_memory_set_dirty_flags(addr1,
4650 (0xff & ~CODE_DIRTY_FLAG));
4651 }
4652 }
bellardaab33092005-10-30 20:48:42 +00004653}
4654
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004655void stw_phys(target_phys_addr_t addr, uint32_t val)
4656{
4657 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
4658}
4659
4660void stw_le_phys(target_phys_addr_t addr, uint32_t val)
4661{
4662 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
4663}
4664
4665void stw_be_phys(target_phys_addr_t addr, uint32_t val)
4666{
4667 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
4668}
4669
bellardaab33092005-10-30 20:48:42 +00004670/* XXX: optimize */
Anthony Liguoric227f092009-10-01 16:12:16 -05004671void stq_phys(target_phys_addr_t addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00004672{
4673 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01004674 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00004675}
4676
Alexander Graf1e78bcc2011-07-06 09:09:23 +02004677void stq_le_phys(target_phys_addr_t addr, uint64_t val)
4678{
4679 val = cpu_to_le64(val);
4680 cpu_physical_memory_write(addr, &val, 8);
4681}
4682
4683void stq_be_phys(target_phys_addr_t addr, uint64_t val)
4684{
4685 val = cpu_to_be64(val);
4686 cpu_physical_memory_write(addr, &val, 8);
4687}
4688
aliguori5e2972f2009-03-28 17:51:36 +00004689/* virtual memory access for debug (includes writing to ROM) */
ths5fafdf22007-09-16 21:08:06 +00004690int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00004691 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00004692{
4693 int l;
Anthony Liguoric227f092009-10-01 16:12:16 -05004694 target_phys_addr_t phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00004695 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00004696
4697 while (len > 0) {
4698 page = addr & TARGET_PAGE_MASK;
4699 phys_addr = cpu_get_phys_page_debug(env, page);
4700 /* if no physical page mapped, return an error */
4701 if (phys_addr == -1)
4702 return -1;
4703 l = (page + TARGET_PAGE_SIZE) - addr;
4704 if (l > len)
4705 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00004706 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00004707 if (is_write)
4708 cpu_physical_memory_write_rom(phys_addr, buf, l);
4709 else
aliguori5e2972f2009-03-28 17:51:36 +00004710 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00004711 len -= l;
4712 buf += l;
4713 addr += l;
4714 }
4715 return 0;
4716}
Paul Brooka68fe892010-03-01 00:08:59 +00004717#endif
bellard13eb76e2004-01-24 15:23:36 +00004718
pbrook2e70f6e2008-06-29 01:03:05 +00004719/* in deterministic execution mode, instructions doing device I/Os
4720 must be at the end of the TB */
4721void cpu_io_recompile(CPUState *env, void *retaddr)
4722{
4723 TranslationBlock *tb;
4724 uint32_t n, cflags;
4725 target_ulong pc, cs_base;
4726 uint64_t flags;
4727
4728 tb = tb_find_pc((unsigned long)retaddr);
4729 if (!tb) {
4730 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
4731 retaddr);
4732 }
4733 n = env->icount_decr.u16.low + tb->icount;
Stefan Weil618ba8e2011-04-18 06:39:53 +00004734 cpu_restore_state(tb, env, (unsigned long)retaddr);
pbrook2e70f6e2008-06-29 01:03:05 +00004735 /* Calculate how many instructions had been executed before the fault
thsbf20dc02008-06-30 17:22:19 +00004736 occurred. */
pbrook2e70f6e2008-06-29 01:03:05 +00004737 n = n - env->icount_decr.u16.low;
4738 /* Generate a new TB ending on the I/O insn. */
4739 n++;
4740 /* On MIPS and SH, delay slot instructions can only be restarted if
4741 they were already the first instruction in the TB. If this is not
thsbf20dc02008-06-30 17:22:19 +00004742 the first instruction in a TB then re-execute the preceding
pbrook2e70f6e2008-06-29 01:03:05 +00004743 branch. */
4744#if defined(TARGET_MIPS)
4745 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
4746 env->active_tc.PC -= 4;
4747 env->icount_decr.u16.low++;
4748 env->hflags &= ~MIPS_HFLAG_BMASK;
4749 }
4750#elif defined(TARGET_SH4)
4751 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
4752 && n > 1) {
4753 env->pc -= 2;
4754 env->icount_decr.u16.low++;
4755 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
4756 }
4757#endif
4758 /* This should never happen. */
4759 if (n > CF_COUNT_MASK)
4760 cpu_abort(env, "TB too big during recompile");
4761
4762 cflags = n | CF_LAST_IO;
4763 pc = tb->pc;
4764 cs_base = tb->cs_base;
4765 flags = tb->flags;
4766 tb_phys_invalidate(tb, -1);
4767 /* FIXME: In theory this could raise an exception. In practice
4768 we have already translated the block once so it's probably ok. */
4769 tb_gen_code(env, pc, cs_base, flags, cflags);
thsbf20dc02008-06-30 17:22:19 +00004770 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
pbrook2e70f6e2008-06-29 01:03:05 +00004771 the first in the TB) then we end up generating a whole new TB and
4772 repeating the fault, which is horribly inefficient.
4773 Better would be to execute just this insn uncached, or generate a
4774 second new TB. */
4775 cpu_resume_from_signal(env, NULL);
4776}
4777
Paul Brookb3755a92010-03-12 16:54:58 +00004778#if !defined(CONFIG_USER_ONLY)
4779
Stefan Weil055403b2010-10-22 23:03:32 +02004780void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
bellarde3db7222005-01-26 22:00:47 +00004781{
4782 int i, target_code_size, max_target_code_size;
4783 int direct_jmp_count, direct_jmp2_count, cross_page;
4784 TranslationBlock *tb;
ths3b46e622007-09-17 08:09:54 +00004785
bellarde3db7222005-01-26 22:00:47 +00004786 target_code_size = 0;
4787 max_target_code_size = 0;
4788 cross_page = 0;
4789 direct_jmp_count = 0;
4790 direct_jmp2_count = 0;
4791 for(i = 0; i < nb_tbs; i++) {
4792 tb = &tbs[i];
4793 target_code_size += tb->size;
4794 if (tb->size > max_target_code_size)
4795 max_target_code_size = tb->size;
4796 if (tb->page_addr[1] != -1)
4797 cross_page++;
4798 if (tb->tb_next_offset[0] != 0xffff) {
4799 direct_jmp_count++;
4800 if (tb->tb_next_offset[1] != 0xffff) {
4801 direct_jmp2_count++;
4802 }
4803 }
4804 }
4805 /* XXX: avoid using doubles ? */
bellard57fec1f2008-02-01 10:50:11 +00004806 cpu_fprintf(f, "Translation buffer state:\n");
Stefan Weil055403b2010-10-22 23:03:32 +02004807 cpu_fprintf(f, "gen code size %td/%ld\n",
bellard26a5f132008-05-28 12:30:31 +00004808 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
4809 cpu_fprintf(f, "TB count %d/%d\n",
4810 nb_tbs, code_gen_max_blocks);
ths5fafdf22007-09-16 21:08:06 +00004811 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
bellarde3db7222005-01-26 22:00:47 +00004812 nb_tbs ? target_code_size / nb_tbs : 0,
4813 max_target_code_size);
Stefan Weil055403b2010-10-22 23:03:32 +02004814 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
bellarde3db7222005-01-26 22:00:47 +00004815 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
4816 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
ths5fafdf22007-09-16 21:08:06 +00004817 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
4818 cross_page,
bellarde3db7222005-01-26 22:00:47 +00004819 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
4820 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
ths5fafdf22007-09-16 21:08:06 +00004821 direct_jmp_count,
bellarde3db7222005-01-26 22:00:47 +00004822 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
4823 direct_jmp2_count,
4824 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
bellard57fec1f2008-02-01 10:50:11 +00004825 cpu_fprintf(f, "\nStatistics:\n");
bellarde3db7222005-01-26 22:00:47 +00004826 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
4827 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
4828 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
bellardb67d9a52008-05-23 09:57:34 +00004829 tcg_dump_info(f, cpu_fprintf);
bellarde3db7222005-01-26 22:00:47 +00004830}
4831
bellard61382a52003-10-27 21:22:23 +00004832#define MMUSUFFIX _cmmu
Blue Swirl39171492011-09-21 18:13:16 +00004833#undef GETPC
bellard61382a52003-10-27 21:22:23 +00004834#define GETPC() NULL
4835#define env cpu_single_env
bellardb769d8f2004-10-03 15:07:13 +00004836#define SOFTMMU_CODE_ACCESS
bellard61382a52003-10-27 21:22:23 +00004837
4838#define SHIFT 0
4839#include "softmmu_template.h"
4840
4841#define SHIFT 1
4842#include "softmmu_template.h"
4843
4844#define SHIFT 2
4845#include "softmmu_template.h"
4846
4847#define SHIFT 3
4848#include "softmmu_template.h"
4849
4850#undef env
4851
4852#endif