]> Git Repo - qemu.git/blame - exec.c
exec: Split up and tidy code_gen_buffer
[qemu.git] / exec.c
CommitLineData
54936004 1/*
fd6ce8f6 2 * virtual page mapping and translated block handling
5fafdf24 3 *
54936004
FB
4 * 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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
54936004 18 */
67b915a5 19#include "config.h"
d5a8f07c
FB
20#ifdef _WIN32
21#include <windows.h>
22#else
a98d49b1 23#include <sys/types.h>
d5a8f07c
FB
24#include <sys/mman.h>
25#endif
54936004 26
055403b2 27#include "qemu-common.h"
6180a181 28#include "cpu.h"
b67d9a52 29#include "tcg.h"
b3c7724c 30#include "hw/hw.h"
cc9e98cb 31#include "hw/qdev.h"
74576198 32#include "osdep.h"
7ba1e619 33#include "kvm.h"
432d268c 34#include "hw/xen.h"
29e922b6 35#include "qemu-timer.h"
62152b8a
AK
36#include "memory.h"
37#include "exec-memory.h"
53a5960a
PB
38#if defined(CONFIG_USER_ONLY)
39#include <qemu.h>
f01576f1
JL
40#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
432d268c
JN
55#else /* !CONFIG_USER_ONLY */
56#include "xen-mapcache.h"
6506e4f9 57#include "trace.h"
53a5960a 58#endif
54936004 59
0cac1b66
BS
60#include "cputlb.h"
61
67d95c15
AK
62#define WANT_EXEC_OBSOLETE
63#include "exec-obsolete.h"
64
fd6ce8f6 65//#define DEBUG_TB_INVALIDATE
66e85a21 66//#define DEBUG_FLUSH
67d3b957 67//#define DEBUG_UNASSIGNED
fd6ce8f6
FB
68
69/* make various TB consistency checks */
5fafdf24 70//#define DEBUG_TB_CHECK
fd6ce8f6 71
1196be37 72//#define DEBUG_IOPORT
db7b5426 73//#define DEBUG_SUBPAGE
1196be37 74
99773bd4
PB
75#if !defined(CONFIG_USER_ONLY)
76/* TB consistency checks only implemented for usermode emulation. */
77#undef DEBUG_TB_CHECK
78#endif
79
9fa3e853
FB
80#define SMC_BITMAP_USE_THRESHOLD 10
81
bdaf78e0 82static TranslationBlock *tbs;
24ab68ac 83static int code_gen_max_blocks;
9fa3e853 84TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
bdaf78e0 85static int nb_tbs;
eb51d102 86/* any access to the tbs or the page table must use this lock */
c227f099 87spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
fd6ce8f6 88
9b9c37c3 89#if defined(__arm__) || defined(__sparc__)
141ac468
BS
90/* The prologue must be reachable with a direct jump. ARM and Sparc64
91 have limited branch ranges (possibly also PPC) so place it in a
d03d860b
BS
92 section close to code segment. */
93#define code_gen_section \
94 __attribute__((__section__(".gen_code"))) \
95 __attribute__((aligned (32)))
6840981d 96#elif defined(_WIN32) && !defined(_WIN64)
f8e2af11
SW
97#define code_gen_section \
98 __attribute__((aligned (16)))
d03d860b
BS
99#else
100#define code_gen_section \
101 __attribute__((aligned (32)))
102#endif
103
104uint8_t code_gen_prologue[1024] code_gen_section;
bdaf78e0 105static uint8_t *code_gen_buffer;
f1bc0bcc 106static size_t code_gen_buffer_size;
26a5f13b 107/* threshold to flush the translated code buffer */
f1bc0bcc 108static size_t code_gen_buffer_max_size;
24ab68ac 109static uint8_t *code_gen_ptr;
fd6ce8f6 110
e2eef170 111#if !defined(CONFIG_USER_ONLY)
9fa3e853 112int phys_ram_fd;
74576198 113static int in_migration;
94a6b54f 114
85d59fef 115RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
62152b8a
AK
116
117static MemoryRegion *system_memory;
309cb471 118static MemoryRegion *system_io;
62152b8a 119
0e0df1e2 120MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
de712f94 121static MemoryRegion io_mem_subpage_ram;
0e0df1e2 122
e2eef170 123#endif
9fa3e853 124
9349b4f9 125CPUArchState *first_cpu;
6a00d601
FB
126/* current CPU in the current thread. It is only valid inside
127 cpu_exec() */
9349b4f9 128DEFINE_TLS(CPUArchState *,cpu_single_env);
2e70f6ef 129/* 0 = Do not count executed instructions.
bf20dc07 130 1 = Precise instruction counting.
2e70f6ef
PB
131 2 = Adaptive rate instruction counting. */
132int use_icount = 0;
6a00d601 133
54936004 134typedef struct PageDesc {
92e873b9 135 /* list of TBs intersecting this ram page */
fd6ce8f6 136 TranslationBlock *first_tb;
9fa3e853
FB
137 /* in order to optimize self modifying code, we count the number
138 of lookups we do to a given page to use a bitmap */
139 unsigned int code_write_count;
140 uint8_t *code_bitmap;
141#if defined(CONFIG_USER_ONLY)
142 unsigned long flags;
143#endif
54936004
FB
144} PageDesc;
145
41c1b1c9 146/* In system mode we want L1_MAP to be based on ram offsets,
5cd2c5b6
RH
147 while in user mode we want it to be based on virtual addresses. */
148#if !defined(CONFIG_USER_ONLY)
41c1b1c9
PB
149#if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
150# define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
151#else
5cd2c5b6 152# define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
41c1b1c9 153#endif
bedb69ea 154#else
5cd2c5b6 155# define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
bedb69ea 156#endif
54936004 157
5cd2c5b6
RH
158/* Size of the L2 (and L3, etc) page tables. */
159#define L2_BITS 10
54936004
FB
160#define L2_SIZE (1 << L2_BITS)
161
3eef53df
AK
162#define P_L2_LEVELS \
163 (((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / L2_BITS) + 1)
164
5cd2c5b6 165/* The bits remaining after N lower levels of page tables. */
5cd2c5b6
RH
166#define V_L1_BITS_REM \
167 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
168
5cd2c5b6
RH
169#if V_L1_BITS_REM < 4
170#define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
171#else
172#define V_L1_BITS V_L1_BITS_REM
173#endif
174
5cd2c5b6
RH
175#define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
176
5cd2c5b6
RH
177#define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
178
c6d50674
SW
179uintptr_t qemu_real_host_page_size;
180uintptr_t qemu_host_page_size;
181uintptr_t qemu_host_page_mask;
54936004 182
5cd2c5b6
RH
183/* This is a multi-level map on the virtual address space.
184 The bottom level has pointers to PageDesc. */
185static void *l1_map[V_L1_SIZE];
54936004 186
e2eef170 187#if !defined(CONFIG_USER_ONLY)
4346ae3e
AK
188typedef struct PhysPageEntry PhysPageEntry;
189
5312bd8b
AK
190static MemoryRegionSection *phys_sections;
191static unsigned phys_sections_nb, phys_sections_nb_alloc;
192static uint16_t phys_section_unassigned;
aa102231
AK
193static uint16_t phys_section_notdirty;
194static uint16_t phys_section_rom;
195static uint16_t phys_section_watch;
5312bd8b 196
4346ae3e 197struct PhysPageEntry {
07f07b31
AK
198 uint16_t is_leaf : 1;
199 /* index into phys_sections (is_leaf) or phys_map_nodes (!is_leaf) */
200 uint16_t ptr : 15;
4346ae3e
AK
201};
202
d6f2ea22
AK
203/* Simple allocator for PhysPageEntry nodes */
204static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
205static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;
206
07f07b31 207#define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
d6f2ea22 208
5cd2c5b6 209/* This is a multi-level map on the physical address space.
06ef3525 210 The bottom level has pointers to MemoryRegionSections. */
07f07b31 211static PhysPageEntry phys_map = { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
6d9a1304 212
e2eef170 213static void io_mem_init(void);
62152b8a 214static void memory_map_init(void);
e2eef170 215
1ec9b909 216static MemoryRegion io_mem_watch;
6658ffb8 217#endif
33417e70 218
e3db7226 219/* statistics */
e3db7226
FB
220static int tb_flush_count;
221static int tb_phys_invalidate_count;
222
7cb69cae
FB
223#ifdef _WIN32
224static void map_exec(void *addr, long size)
225{
226 DWORD old_protect;
227 VirtualProtect(addr, size,
228 PAGE_EXECUTE_READWRITE, &old_protect);
229
230}
231#else
232static void map_exec(void *addr, long size)
233{
4369415f 234 unsigned long start, end, page_size;
7cb69cae 235
4369415f 236 page_size = getpagesize();
7cb69cae 237 start = (unsigned long)addr;
4369415f 238 start &= ~(page_size - 1);
7cb69cae
FB
239
240 end = (unsigned long)addr + size;
4369415f
FB
241 end += page_size - 1;
242 end &= ~(page_size - 1);
7cb69cae
FB
243
244 mprotect((void *)start, end - start,
245 PROT_READ | PROT_WRITE | PROT_EXEC);
246}
247#endif
248
b346ff46 249static void page_init(void)
54936004 250{
83fb7adf 251 /* NOTE: we can always suppose that qemu_host_page_size >=
54936004 252 TARGET_PAGE_SIZE */
c2b48b69
AL
253#ifdef _WIN32
254 {
255 SYSTEM_INFO system_info;
256
257 GetSystemInfo(&system_info);
258 qemu_real_host_page_size = system_info.dwPageSize;
259 }
260#else
261 qemu_real_host_page_size = getpagesize();
262#endif
83fb7adf
FB
263 if (qemu_host_page_size == 0)
264 qemu_host_page_size = qemu_real_host_page_size;
265 if (qemu_host_page_size < TARGET_PAGE_SIZE)
266 qemu_host_page_size = TARGET_PAGE_SIZE;
83fb7adf 267 qemu_host_page_mask = ~(qemu_host_page_size - 1);
50a9569b 268
2e9a5713 269#if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
50a9569b 270 {
f01576f1
JL
271#ifdef HAVE_KINFO_GETVMMAP
272 struct kinfo_vmentry *freep;
273 int i, cnt;
274
275 freep = kinfo_getvmmap(getpid(), &cnt);
276 if (freep) {
277 mmap_lock();
278 for (i = 0; i < cnt; i++) {
279 unsigned long startaddr, endaddr;
280
281 startaddr = freep[i].kve_start;
282 endaddr = freep[i].kve_end;
283 if (h2g_valid(startaddr)) {
284 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
285
286 if (h2g_valid(endaddr)) {
287 endaddr = h2g(endaddr);
fd436907 288 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
f01576f1
JL
289 } else {
290#if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
291 endaddr = ~0ul;
fd436907 292 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
f01576f1
JL
293#endif
294 }
295 }
296 }
297 free(freep);
298 mmap_unlock();
299 }
300#else
50a9569b 301 FILE *f;
50a9569b 302
0776590d 303 last_brk = (unsigned long)sbrk(0);
5cd2c5b6 304
fd436907 305 f = fopen("/compat/linux/proc/self/maps", "r");
50a9569b 306 if (f) {
5cd2c5b6
RH
307 mmap_lock();
308
50a9569b 309 do {
5cd2c5b6
RH
310 unsigned long startaddr, endaddr;
311 int n;
312
313 n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
314
315 if (n == 2 && h2g_valid(startaddr)) {
316 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
317
318 if (h2g_valid(endaddr)) {
319 endaddr = h2g(endaddr);
320 } else {
321 endaddr = ~0ul;
322 }
323 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
50a9569b
AZ
324 }
325 } while (!feof(f));
5cd2c5b6 326
50a9569b 327 fclose(f);
5cd2c5b6 328 mmap_unlock();
50a9569b 329 }
f01576f1 330#endif
50a9569b
AZ
331 }
332#endif
54936004
FB
333}
334
41c1b1c9 335static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
54936004 336{
41c1b1c9
PB
337 PageDesc *pd;
338 void **lp;
339 int i;
340
5cd2c5b6 341#if defined(CONFIG_USER_ONLY)
7267c094 342 /* We can't use g_malloc because it may recurse into a locked mutex. */
5cd2c5b6
RH
343# define ALLOC(P, SIZE) \
344 do { \
345 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
346 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
5cd2c5b6
RH
347 } while (0)
348#else
349# define ALLOC(P, SIZE) \
7267c094 350 do { P = g_malloc0(SIZE); } while (0)
17e2377a 351#endif
434929bf 352
5cd2c5b6
RH
353 /* Level 1. Always allocated. */
354 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
355
356 /* Level 2..N-1. */
357 for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
358 void **p = *lp;
359
360 if (p == NULL) {
361 if (!alloc) {
362 return NULL;
363 }
364 ALLOC(p, sizeof(void *) * L2_SIZE);
365 *lp = p;
17e2377a 366 }
5cd2c5b6
RH
367
368 lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
369 }
370
371 pd = *lp;
372 if (pd == NULL) {
373 if (!alloc) {
374 return NULL;
375 }
376 ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
377 *lp = pd;
54936004 378 }
5cd2c5b6
RH
379
380#undef ALLOC
5cd2c5b6
RH
381
382 return pd + (index & (L2_SIZE - 1));
54936004
FB
383}
384
41c1b1c9 385static inline PageDesc *page_find(tb_page_addr_t index)
54936004 386{
5cd2c5b6 387 return page_find_alloc(index, 0);
fd6ce8f6
FB
388}
389
6d9a1304 390#if !defined(CONFIG_USER_ONLY)
d6f2ea22 391
f7bf5461 392static void phys_map_node_reserve(unsigned nodes)
d6f2ea22 393{
f7bf5461 394 if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
d6f2ea22
AK
395 typedef PhysPageEntry Node[L2_SIZE];
396 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
f7bf5461
AK
397 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
398 phys_map_nodes_nb + nodes);
d6f2ea22
AK
399 phys_map_nodes = g_renew(Node, phys_map_nodes,
400 phys_map_nodes_nb_alloc);
401 }
f7bf5461
AK
402}
403
404static uint16_t phys_map_node_alloc(void)
405{
406 unsigned i;
407 uint16_t ret;
408
409 ret = phys_map_nodes_nb++;
410 assert(ret != PHYS_MAP_NODE_NIL);
411 assert(ret != phys_map_nodes_nb_alloc);
d6f2ea22 412 for (i = 0; i < L2_SIZE; ++i) {
07f07b31 413 phys_map_nodes[ret][i].is_leaf = 0;
c19e8800 414 phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
d6f2ea22 415 }
f7bf5461 416 return ret;
d6f2ea22
AK
417}
418
419static void phys_map_nodes_reset(void)
420{
421 phys_map_nodes_nb = 0;
422}
423
92e873b9 424
2999097b
AK
425static void phys_page_set_level(PhysPageEntry *lp, target_phys_addr_t *index,
426 target_phys_addr_t *nb, uint16_t leaf,
427 int level)
f7bf5461
AK
428{
429 PhysPageEntry *p;
430 int i;
07f07b31 431 target_phys_addr_t step = (target_phys_addr_t)1 << (level * L2_BITS);
108c49b8 432
07f07b31 433 if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
c19e8800
AK
434 lp->ptr = phys_map_node_alloc();
435 p = phys_map_nodes[lp->ptr];
f7bf5461
AK
436 if (level == 0) {
437 for (i = 0; i < L2_SIZE; i++) {
07f07b31 438 p[i].is_leaf = 1;
c19e8800 439 p[i].ptr = phys_section_unassigned;
4346ae3e 440 }
67c4d23c 441 }
f7bf5461 442 } else {
c19e8800 443 p = phys_map_nodes[lp->ptr];
92e873b9 444 }
2999097b 445 lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
f7bf5461 446
2999097b 447 while (*nb && lp < &p[L2_SIZE]) {
07f07b31
AK
448 if ((*index & (step - 1)) == 0 && *nb >= step) {
449 lp->is_leaf = true;
c19e8800 450 lp->ptr = leaf;
07f07b31
AK
451 *index += step;
452 *nb -= step;
2999097b
AK
453 } else {
454 phys_page_set_level(lp, index, nb, leaf, level - 1);
455 }
456 ++lp;
f7bf5461
AK
457 }
458}
459
2999097b
AK
460static void phys_page_set(target_phys_addr_t index, target_phys_addr_t nb,
461 uint16_t leaf)
f7bf5461 462{
2999097b 463 /* Wildly overreserve - it doesn't matter much. */
07f07b31 464 phys_map_node_reserve(3 * P_L2_LEVELS);
5cd2c5b6 465
2999097b 466 phys_page_set_level(&phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
92e873b9
FB
467}
468
0cac1b66 469MemoryRegionSection *phys_page_find(target_phys_addr_t index)
92e873b9 470{
31ab2b4a
AK
471 PhysPageEntry lp = phys_map;
472 PhysPageEntry *p;
473 int i;
31ab2b4a 474 uint16_t s_index = phys_section_unassigned;
f1f6e3b8 475
07f07b31 476 for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
c19e8800 477 if (lp.ptr == PHYS_MAP_NODE_NIL) {
31ab2b4a
AK
478 goto not_found;
479 }
c19e8800 480 p = phys_map_nodes[lp.ptr];
31ab2b4a 481 lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
5312bd8b 482 }
31ab2b4a 483
c19e8800 484 s_index = lp.ptr;
31ab2b4a 485not_found:
f3705d53
AK
486 return &phys_sections[s_index];
487}
488
e5548617
BS
489bool memory_region_is_unassigned(MemoryRegion *mr)
490{
491 return mr != &io_mem_ram && mr != &io_mem_rom
492 && mr != &io_mem_notdirty && !mr->rom_device
493 && mr != &io_mem_watch;
494}
495
c8a706fe
PB
496#define mmap_lock() do { } while(0)
497#define mmap_unlock() do { } while(0)
9fa3e853 498#endif
fd6ce8f6 499
4369415f 500#if defined(CONFIG_USER_ONLY)
ccbb4d44 501/* Currently it is not recommended to allocate big chunks of data in
f1bc0bcc
RH
502 user mode. It will change when a dedicated libc will be used. */
503/* ??? 64-bit hosts ought to have no problem mmaping data outside the
504 region in which the guest needs to run. Revisit this. */
4369415f
FB
505#define USE_STATIC_CODE_GEN_BUFFER
506#endif
507
f1bc0bcc
RH
508/* ??? Should configure for this, not list operating systems here. */
509#if (defined(__linux__) \
510 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
511 || defined(__DragonFly__) || defined(__OpenBSD__) \
512 || defined(__NetBSD__))
513# define USE_MMAP
4369415f
FB
514#endif
515
f1bc0bcc
RH
516/* Maximum size of the code gen buffer we'd like to use. Unless otherwise
517 indicated, this is constrained by the range of direct branches on the
518 host cpu, as used by the TCG implementation of goto_tb. */
519#if defined(__x86_64__)
520# define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
521#elif defined(__sparc__)
522# define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
523#elif defined(__arm__)
524# define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024)
525#elif defined(__s390x__)
526 /* We have a +- 4GB range on the branches; leave some slop. */
527# define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
528#else
529# define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
530#endif
531
532#define DEFAULT_CODE_GEN_BUFFER_SIZE (32u * 1024 * 1024)
533
534static inline size_t size_code_gen_buffer(size_t tb_size)
26a5f13b 535{
f1bc0bcc
RH
536 /* Size the buffer. */
537 if (tb_size == 0) {
4369415f 538#ifdef USE_STATIC_CODE_GEN_BUFFER
f1bc0bcc 539 tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
4369415f 540#else
f1bc0bcc
RH
541 /* ??? Needs adjustments. */
542 /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
543 static buffer, we could size this on RESERVED_VA, on the text
544 segment size of the executable, or continue to use the default. */
545 tb_size = (unsigned long)(ram_size / 4);
4369415f 546#endif
26a5f13b 547 }
f1bc0bcc
RH
548 if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
549 tb_size = MIN_CODE_GEN_BUFFER_SIZE;
26a5f13b 550 }
f1bc0bcc
RH
551 if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
552 tb_size = MAX_CODE_GEN_BUFFER_SIZE;
06e67a82 553 }
f1bc0bcc
RH
554 code_gen_buffer_size = tb_size;
555 return tb_size;
556}
557
558#ifdef USE_STATIC_CODE_GEN_BUFFER
559static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
560 __attribute__((aligned(CODE_GEN_ALIGN)));
561
562static inline void *alloc_code_gen_buffer(void)
563{
564 map_exec(static_code_gen_buffer, code_gen_buffer_size);
565 return static_code_gen_buffer;
566}
567#elif defined(USE_MMAP)
568static inline void *alloc_code_gen_buffer(void)
569{
570 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
571 uintptr_t start = 0;
572 void *buf;
573
574 /* Constrain the position of the buffer based on the host cpu.
575 Note that these addresses are chosen in concert with the
576 addresses assigned in the relevant linker script file. */
577# if defined(__x86_64__) && defined(MAP_32BIT)
578 /* Force the memory down into low memory with the executable.
579 Leave the choice of exact location with the kernel. */
580 flags |= MAP_32BIT;
581 /* Cannot expect to map more than 800MB in low memory. */
582 if (code_gen_buffer_size > 800u * 1024 * 1024) {
583 code_gen_buffer_size = 800u * 1024 * 1024;
584 }
585# elif defined(__sparc__)
586 start = 0x40000000ul;
587# elif defined(__s390x__)
588 start = 0x90000000ul;
589# endif
590
591 buf = mmap((void *)start, code_gen_buffer_size,
592 PROT_WRITE | PROT_READ | PROT_EXEC, flags, -1, 0);
593 return buf == MAP_FAILED ? NULL : buf;
594}
26a5f13b 595#else
f1bc0bcc
RH
596static inline void *alloc_code_gen_buffer(void)
597{
598 void *buf = g_malloc(code_gen_buffer_size);
599 if (buf) {
600 map_exec(buf, code_gen_buffer_size);
601 }
602 return buf;
603}
604#endif /* USE_STATIC_CODE_GEN_BUFFER, USE_MMAP */
605
606static inline void code_gen_alloc(size_t tb_size)
607{
608 code_gen_buffer_size = size_code_gen_buffer(tb_size);
609 code_gen_buffer = alloc_code_gen_buffer();
610 if (code_gen_buffer == NULL) {
611 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
612 exit(1);
613 }
614
26a5f13b 615 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
a884da8a
PM
616 code_gen_buffer_max_size = code_gen_buffer_size -
617 (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
26a5f13b 618 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
7267c094 619 tbs = g_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
26a5f13b
FB
620}
621
622/* Must be called before using the QEMU cpus. 'tb_size' is the size
623 (in bytes) allocated to the translation buffer. Zero means default
624 size. */
d5ab9713 625void tcg_exec_init(unsigned long tb_size)
26a5f13b 626{
26a5f13b
FB
627 cpu_gen_init();
628 code_gen_alloc(tb_size);
629 code_gen_ptr = code_gen_buffer;
813da627 630 tcg_register_jit(code_gen_buffer, code_gen_buffer_size);
4369415f 631 page_init();
9002ec79
RH
632#if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
633 /* There's no guest base to take into account, so go ahead and
634 initialize the prologue now. */
635 tcg_prologue_init(&tcg_ctx);
636#endif
26a5f13b
FB
637}
638
d5ab9713
JK
639bool tcg_enabled(void)
640{
641 return code_gen_buffer != NULL;
642}
643
644void cpu_exec_init_all(void)
645{
646#if !defined(CONFIG_USER_ONLY)
647 memory_map_init();
648 io_mem_init();
649#endif
650}
651
9656f324
PB
652#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
653
e59fb374 654static int cpu_common_post_load(void *opaque, int version_id)
e7f4eff7 655{
9349b4f9 656 CPUArchState *env = opaque;
9656f324 657
3098dba0
AJ
658 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
659 version_id is increased. */
660 env->interrupt_request &= ~0x01;
9656f324
PB
661 tlb_flush(env, 1);
662
663 return 0;
664}
e7f4eff7
JQ
665
666static const VMStateDescription vmstate_cpu_common = {
667 .name = "cpu_common",
668 .version_id = 1,
669 .minimum_version_id = 1,
670 .minimum_version_id_old = 1,
e7f4eff7
JQ
671 .post_load = cpu_common_post_load,
672 .fields = (VMStateField []) {
9349b4f9
AF
673 VMSTATE_UINT32(halted, CPUArchState),
674 VMSTATE_UINT32(interrupt_request, CPUArchState),
e7f4eff7
JQ
675 VMSTATE_END_OF_LIST()
676 }
677};
9656f324
PB
678#endif
679
9349b4f9 680CPUArchState *qemu_get_cpu(int cpu)
950f1472 681{
9349b4f9 682 CPUArchState *env = first_cpu;
950f1472
GC
683
684 while (env) {
685 if (env->cpu_index == cpu)
686 break;
687 env = env->next_cpu;
688 }
689
690 return env;
691}
692
9349b4f9 693void cpu_exec_init(CPUArchState *env)
fd6ce8f6 694{
9349b4f9 695 CPUArchState **penv;
6a00d601
FB
696 int cpu_index;
697
c2764719
PB
698#if defined(CONFIG_USER_ONLY)
699 cpu_list_lock();
700#endif
6a00d601
FB
701 env->next_cpu = NULL;
702 penv = &first_cpu;
703 cpu_index = 0;
704 while (*penv != NULL) {
1e9fa730 705 penv = &(*penv)->next_cpu;
6a00d601
FB
706 cpu_index++;
707 }
708 env->cpu_index = cpu_index;
268a362c 709 env->numa_node = 0;
72cf2d4f
BS
710 QTAILQ_INIT(&env->breakpoints);
711 QTAILQ_INIT(&env->watchpoints);
dc7a09cf
JK
712#ifndef CONFIG_USER_ONLY
713 env->thread_id = qemu_get_thread_id();
714#endif
6a00d601 715 *penv = env;
c2764719
PB
716#if defined(CONFIG_USER_ONLY)
717 cpu_list_unlock();
718#endif
b3c7724c 719#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
0be71e32
AW
720 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
721 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
b3c7724c
PB
722 cpu_save, cpu_load, env);
723#endif
fd6ce8f6
FB
724}
725
d1a1eb74
TG
726/* Allocate a new translation block. Flush the translation buffer if
727 too many translation blocks or too much generated code. */
728static TranslationBlock *tb_alloc(target_ulong pc)
729{
730 TranslationBlock *tb;
731
732 if (nb_tbs >= code_gen_max_blocks ||
733 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
734 return NULL;
735 tb = &tbs[nb_tbs++];
736 tb->pc = pc;
737 tb->cflags = 0;
738 return tb;
739}
740
741void tb_free(TranslationBlock *tb)
742{
743 /* In practice this is mostly used for single use temporary TB
744 Ignore the hard cases and just back up if this TB happens to
745 be the last one generated. */
746 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
747 code_gen_ptr = tb->tc_ptr;
748 nb_tbs--;
749 }
750}
751
9fa3e853
FB
752static inline void invalidate_page_bitmap(PageDesc *p)
753{
754 if (p->code_bitmap) {
7267c094 755 g_free(p->code_bitmap);
9fa3e853
FB
756 p->code_bitmap = NULL;
757 }
758 p->code_write_count = 0;
759}
760
5cd2c5b6
RH
761/* Set to NULL all the 'first_tb' fields in all PageDescs. */
762
763static void page_flush_tb_1 (int level, void **lp)
fd6ce8f6 764{
5cd2c5b6 765 int i;
fd6ce8f6 766
5cd2c5b6
RH
767 if (*lp == NULL) {
768 return;
769 }
770 if (level == 0) {
771 PageDesc *pd = *lp;
7296abac 772 for (i = 0; i < L2_SIZE; ++i) {
5cd2c5b6
RH
773 pd[i].first_tb = NULL;
774 invalidate_page_bitmap(pd + i);
fd6ce8f6 775 }
5cd2c5b6
RH
776 } else {
777 void **pp = *lp;
7296abac 778 for (i = 0; i < L2_SIZE; ++i) {
5cd2c5b6
RH
779 page_flush_tb_1 (level - 1, pp + i);
780 }
781 }
782}
783
784static void page_flush_tb(void)
785{
786 int i;
787 for (i = 0; i < V_L1_SIZE; i++) {
788 page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
fd6ce8f6
FB
789 }
790}
791
792/* flush all the translation blocks */
d4e8164f 793/* XXX: tb_flush is currently not thread safe */
9349b4f9 794void tb_flush(CPUArchState *env1)
fd6ce8f6 795{
9349b4f9 796 CPUArchState *env;
0124311e 797#if defined(DEBUG_FLUSH)
ab3d1727
BS
798 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
799 (unsigned long)(code_gen_ptr - code_gen_buffer),
800 nb_tbs, nb_tbs > 0 ?
801 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
fd6ce8f6 802#endif
26a5f13b 803 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
a208e54a
PB
804 cpu_abort(env1, "Internal error: code buffer overflow\n");
805
fd6ce8f6 806 nb_tbs = 0;
3b46e624 807
6a00d601
FB
808 for(env = first_cpu; env != NULL; env = env->next_cpu) {
809 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
810 }
9fa3e853 811
8a8a608f 812 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
fd6ce8f6 813 page_flush_tb();
9fa3e853 814
fd6ce8f6 815 code_gen_ptr = code_gen_buffer;
d4e8164f
FB
816 /* XXX: flush processor icache at this point if cache flush is
817 expensive */
e3db7226 818 tb_flush_count++;
fd6ce8f6
FB
819}
820
821#ifdef DEBUG_TB_CHECK
822
bc98a7ef 823static void tb_invalidate_check(target_ulong address)
fd6ce8f6
FB
824{
825 TranslationBlock *tb;
826 int i;
827 address &= TARGET_PAGE_MASK;
99773bd4
PB
828 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
829 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
fd6ce8f6
FB
830 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
831 address >= tb->pc + tb->size)) {
0bf9e31a
BS
832 printf("ERROR invalidate: address=" TARGET_FMT_lx
833 " PC=%08lx size=%04x\n",
99773bd4 834 address, (long)tb->pc, tb->size);
fd6ce8f6
FB
835 }
836 }
837 }
838}
839
840/* verify that all the pages have correct rights for code */
841static void tb_page_check(void)
842{
843 TranslationBlock *tb;
844 int i, flags1, flags2;
3b46e624 845
99773bd4
PB
846 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
847 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
fd6ce8f6
FB
848 flags1 = page_get_flags(tb->pc);
849 flags2 = page_get_flags(tb->pc + tb->size - 1);
850 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
851 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
99773bd4 852 (long)tb->pc, tb->size, flags1, flags2);
fd6ce8f6
FB
853 }
854 }
855 }
856}
857
858#endif
859
860/* invalidate one TB */
861static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
862 int next_offset)
863{
864 TranslationBlock *tb1;
865 for(;;) {
866 tb1 = *ptb;
867 if (tb1 == tb) {
868 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
869 break;
870 }
871 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
872 }
873}
874
9fa3e853
FB
875static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
876{
877 TranslationBlock *tb1;
878 unsigned int n1;
879
880 for(;;) {
881 tb1 = *ptb;
8efe0ca8
SW
882 n1 = (uintptr_t)tb1 & 3;
883 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
9fa3e853
FB
884 if (tb1 == tb) {
885 *ptb = tb1->page_next[n1];
886 break;
887 }
888 ptb = &tb1->page_next[n1];
889 }
890}
891
d4e8164f
FB
892static inline void tb_jmp_remove(TranslationBlock *tb, int n)
893{
894 TranslationBlock *tb1, **ptb;
895 unsigned int n1;
896
897 ptb = &tb->jmp_next[n];
898 tb1 = *ptb;
899 if (tb1) {
900 /* find tb(n) in circular list */
901 for(;;) {
902 tb1 = *ptb;
8efe0ca8
SW
903 n1 = (uintptr_t)tb1 & 3;
904 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
d4e8164f
FB
905 if (n1 == n && tb1 == tb)
906 break;
907 if (n1 == 2) {
908 ptb = &tb1->jmp_first;
909 } else {
910 ptb = &tb1->jmp_next[n1];
911 }
912 }
913 /* now we can suppress tb(n) from the list */
914 *ptb = tb->jmp_next[n];
915
916 tb->jmp_next[n] = NULL;
917 }
918}
919
920/* reset the jump entry 'n' of a TB so that it is not chained to
921 another TB */
922static inline void tb_reset_jump(TranslationBlock *tb, int n)
923{
8efe0ca8 924 tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
d4e8164f
FB
925}
926
41c1b1c9 927void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
fd6ce8f6 928{
9349b4f9 929 CPUArchState *env;
8a40a180 930 PageDesc *p;
d4e8164f 931 unsigned int h, n1;
41c1b1c9 932 tb_page_addr_t phys_pc;
8a40a180 933 TranslationBlock *tb1, *tb2;
3b46e624 934
8a40a180
FB
935 /* remove the TB from the hash list */
936 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
937 h = tb_phys_hash_func(phys_pc);
5fafdf24 938 tb_remove(&tb_phys_hash[h], tb,
8a40a180
FB
939 offsetof(TranslationBlock, phys_hash_next));
940
941 /* remove the TB from the page list */
942 if (tb->page_addr[0] != page_addr) {
943 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
944 tb_page_remove(&p->first_tb, tb);
945 invalidate_page_bitmap(p);
946 }
947 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
948 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
949 tb_page_remove(&p->first_tb, tb);
950 invalidate_page_bitmap(p);
951 }
952
36bdbe54 953 tb_invalidated_flag = 1;
59817ccb 954
fd6ce8f6 955 /* remove the TB from the hash list */
8a40a180 956 h = tb_jmp_cache_hash_func(tb->pc);
6a00d601
FB
957 for(env = first_cpu; env != NULL; env = env->next_cpu) {
958 if (env->tb_jmp_cache[h] == tb)
959 env->tb_jmp_cache[h] = NULL;
960 }
d4e8164f
FB
961
962 /* suppress this TB from the two jump lists */
963 tb_jmp_remove(tb, 0);
964 tb_jmp_remove(tb, 1);
965
966 /* suppress any remaining jumps to this TB */
967 tb1 = tb->jmp_first;
968 for(;;) {
8efe0ca8 969 n1 = (uintptr_t)tb1 & 3;
d4e8164f
FB
970 if (n1 == 2)
971 break;
8efe0ca8 972 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
d4e8164f
FB
973 tb2 = tb1->jmp_next[n1];
974 tb_reset_jump(tb1, n1);
975 tb1->jmp_next[n1] = NULL;
976 tb1 = tb2;
977 }
8efe0ca8 978 tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2); /* fail safe */
9fa3e853 979
e3db7226 980 tb_phys_invalidate_count++;
9fa3e853
FB
981}
982
983static inline void set_bits(uint8_t *tab, int start, int len)
984{
985 int end, mask, end1;
986
987 end = start + len;
988 tab += start >> 3;
989 mask = 0xff << (start & 7);
990 if ((start & ~7) == (end & ~7)) {
991 if (start < end) {
992 mask &= ~(0xff << (end & 7));
993 *tab |= mask;
994 }
995 } else {
996 *tab++ |= mask;
997 start = (start + 8) & ~7;
998 end1 = end & ~7;
999 while (start < end1) {
1000 *tab++ = 0xff;
1001 start += 8;
1002 }
1003 if (start < end) {
1004 mask = ~(0xff << (end & 7));
1005 *tab |= mask;
1006 }
1007 }
1008}
1009
1010static void build_page_bitmap(PageDesc *p)
1011{
1012 int n, tb_start, tb_end;
1013 TranslationBlock *tb;
3b46e624 1014
7267c094 1015 p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
9fa3e853
FB
1016
1017 tb = p->first_tb;
1018 while (tb != NULL) {
8efe0ca8
SW
1019 n = (uintptr_t)tb & 3;
1020 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
9fa3e853
FB
1021 /* NOTE: this is subtle as a TB may span two physical pages */
1022 if (n == 0) {
1023 /* NOTE: tb_end may be after the end of the page, but
1024 it is not a problem */
1025 tb_start = tb->pc & ~TARGET_PAGE_MASK;
1026 tb_end = tb_start + tb->size;
1027 if (tb_end > TARGET_PAGE_SIZE)
1028 tb_end = TARGET_PAGE_SIZE;
1029 } else {
1030 tb_start = 0;
1031 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1032 }
1033 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
1034 tb = tb->page_next[n];
1035 }
1036}
1037
9349b4f9 1038TranslationBlock *tb_gen_code(CPUArchState *env,
2e70f6ef
PB
1039 target_ulong pc, target_ulong cs_base,
1040 int flags, int cflags)
d720b93d
FB
1041{
1042 TranslationBlock *tb;
1043 uint8_t *tc_ptr;
41c1b1c9
PB
1044 tb_page_addr_t phys_pc, phys_page2;
1045 target_ulong virt_page2;
d720b93d
FB
1046 int code_gen_size;
1047
41c1b1c9 1048 phys_pc = get_page_addr_code(env, pc);
c27004ec 1049 tb = tb_alloc(pc);
d720b93d
FB
1050 if (!tb) {
1051 /* flush must be done */
1052 tb_flush(env);
1053 /* cannot fail at this point */
c27004ec 1054 tb = tb_alloc(pc);
2e70f6ef
PB
1055 /* Don't forget to invalidate previous TB info. */
1056 tb_invalidated_flag = 1;
d720b93d
FB
1057 }
1058 tc_ptr = code_gen_ptr;
1059 tb->tc_ptr = tc_ptr;
1060 tb->cs_base = cs_base;
1061 tb->flags = flags;
1062 tb->cflags = cflags;
d07bde88 1063 cpu_gen_code(env, tb, &code_gen_size);
8efe0ca8
SW
1064 code_gen_ptr = (void *)(((uintptr_t)code_gen_ptr + code_gen_size +
1065 CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
3b46e624 1066
d720b93d 1067 /* check next page if needed */
c27004ec 1068 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
d720b93d 1069 phys_page2 = -1;
c27004ec 1070 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
41c1b1c9 1071 phys_page2 = get_page_addr_code(env, virt_page2);
d720b93d 1072 }
41c1b1c9 1073 tb_link_page(tb, phys_pc, phys_page2);
2e70f6ef 1074 return tb;
d720b93d 1075}
3b46e624 1076
77a8f1a5 1077/*
8e0fdce3
JK
1078 * Invalidate all TBs which intersect with the target physical address range
1079 * [start;end[. NOTE: start and end may refer to *different* physical pages.
1080 * 'is_cpu_write_access' should be true if called from a real cpu write
1081 * access: the virtual CPU will exit the current TB if code is modified inside
1082 * this TB.
77a8f1a5
AG
1083 */
1084void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
1085 int is_cpu_write_access)
1086{
1087 while (start < end) {
1088 tb_invalidate_phys_page_range(start, end, is_cpu_write_access);
1089 start &= TARGET_PAGE_MASK;
1090 start += TARGET_PAGE_SIZE;
1091 }
1092}
1093
8e0fdce3
JK
1094/*
1095 * Invalidate all TBs which intersect with the target physical address range
1096 * [start;end[. NOTE: start and end must refer to the *same* physical page.
1097 * 'is_cpu_write_access' should be true if called from a real cpu write
1098 * access: the virtual CPU will exit the current TB if code is modified inside
1099 * this TB.
1100 */
41c1b1c9 1101void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
d720b93d
FB
1102 int is_cpu_write_access)
1103{
6b917547 1104 TranslationBlock *tb, *tb_next, *saved_tb;
9349b4f9 1105 CPUArchState *env = cpu_single_env;
41c1b1c9 1106 tb_page_addr_t tb_start, tb_end;
6b917547
AL
1107 PageDesc *p;
1108 int n;
1109#ifdef TARGET_HAS_PRECISE_SMC
1110 int current_tb_not_found = is_cpu_write_access;
1111 TranslationBlock *current_tb = NULL;
1112 int current_tb_modified = 0;
1113 target_ulong current_pc = 0;
1114 target_ulong current_cs_base = 0;
1115 int current_flags = 0;
1116#endif /* TARGET_HAS_PRECISE_SMC */
9fa3e853
FB
1117
1118 p = page_find(start >> TARGET_PAGE_BITS);
5fafdf24 1119 if (!p)
9fa3e853 1120 return;
5fafdf24 1121 if (!p->code_bitmap &&
d720b93d
FB
1122 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1123 is_cpu_write_access) {
9fa3e853
FB
1124 /* build code bitmap */
1125 build_page_bitmap(p);
1126 }
1127
1128 /* we remove all the TBs in the range [start, end[ */
1129 /* XXX: see if in some cases it could be faster to invalidate all the code */
1130 tb = p->first_tb;
1131 while (tb != NULL) {
8efe0ca8
SW
1132 n = (uintptr_t)tb & 3;
1133 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
9fa3e853
FB
1134 tb_next = tb->page_next[n];
1135 /* NOTE: this is subtle as a TB may span two physical pages */
1136 if (n == 0) {
1137 /* NOTE: tb_end may be after the end of the page, but
1138 it is not a problem */
1139 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1140 tb_end = tb_start + tb->size;
1141 } else {
1142 tb_start = tb->page_addr[1];
1143 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1144 }
1145 if (!(tb_end <= start || tb_start >= end)) {
d720b93d
FB
1146#ifdef TARGET_HAS_PRECISE_SMC
1147 if (current_tb_not_found) {
1148 current_tb_not_found = 0;
1149 current_tb = NULL;
2e70f6ef 1150 if (env->mem_io_pc) {
d720b93d 1151 /* now we have a real cpu fault */
2e70f6ef 1152 current_tb = tb_find_pc(env->mem_io_pc);
d720b93d
FB
1153 }
1154 }
1155 if (current_tb == tb &&
2e70f6ef 1156 (current_tb->cflags & CF_COUNT_MASK) != 1) {
d720b93d
FB
1157 /* If we are modifying the current TB, we must stop
1158 its execution. We could be more precise by checking
1159 that the modification is after the current PC, but it
1160 would require a specialized function to partially
1161 restore the CPU state */
3b46e624 1162
d720b93d 1163 current_tb_modified = 1;
618ba8e6 1164 cpu_restore_state(current_tb, env, env->mem_io_pc);
6b917547
AL
1165 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1166 &current_flags);
d720b93d
FB
1167 }
1168#endif /* TARGET_HAS_PRECISE_SMC */
6f5a9f7e
FB
1169 /* we need to do that to handle the case where a signal
1170 occurs while doing tb_phys_invalidate() */
1171 saved_tb = NULL;
1172 if (env) {
1173 saved_tb = env->current_tb;
1174 env->current_tb = NULL;
1175 }
9fa3e853 1176 tb_phys_invalidate(tb, -1);
6f5a9f7e
FB
1177 if (env) {
1178 env->current_tb = saved_tb;
1179 if (env->interrupt_request && env->current_tb)
1180 cpu_interrupt(env, env->interrupt_request);
1181 }
9fa3e853
FB
1182 }
1183 tb = tb_next;
1184 }
1185#if !defined(CONFIG_USER_ONLY)
1186 /* if no code remaining, no need to continue to use slow writes */
1187 if (!p->first_tb) {
1188 invalidate_page_bitmap(p);
d720b93d 1189 if (is_cpu_write_access) {
2e70f6ef 1190 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
d720b93d
FB
1191 }
1192 }
1193#endif
1194#ifdef TARGET_HAS_PRECISE_SMC
1195 if (current_tb_modified) {
1196 /* we generate a block containing just the instruction
1197 modifying the memory. It will ensure that it cannot modify
1198 itself */
ea1c1802 1199 env->current_tb = NULL;
2e70f6ef 1200 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
d720b93d 1201 cpu_resume_from_signal(env, NULL);
9fa3e853 1202 }
fd6ce8f6 1203#endif
9fa3e853 1204}
fd6ce8f6 1205
9fa3e853 1206/* len must be <= 8 and start must be a multiple of len */
41c1b1c9 1207static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
9fa3e853
FB
1208{
1209 PageDesc *p;
1210 int offset, b;
59817ccb 1211#if 0
a4193c8a 1212 if (1) {
93fcfe39
AL
1213 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1214 cpu_single_env->mem_io_vaddr, len,
1215 cpu_single_env->eip,
8efe0ca8
SW
1216 cpu_single_env->eip +
1217 (intptr_t)cpu_single_env->segs[R_CS].base);
59817ccb
FB
1218 }
1219#endif
9fa3e853 1220 p = page_find(start >> TARGET_PAGE_BITS);
5fafdf24 1221 if (!p)
9fa3e853
FB
1222 return;
1223 if (p->code_bitmap) {
1224 offset = start & ~TARGET_PAGE_MASK;
1225 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1226 if (b & ((1 << len) - 1))
1227 goto do_invalidate;
1228 } else {
1229 do_invalidate:
d720b93d 1230 tb_invalidate_phys_page_range(start, start + len, 1);
9fa3e853
FB
1231 }
1232}
1233
9fa3e853 1234#if !defined(CONFIG_SOFTMMU)
41c1b1c9 1235static void tb_invalidate_phys_page(tb_page_addr_t addr,
20503968 1236 uintptr_t pc, void *puc)
9fa3e853 1237{
6b917547 1238 TranslationBlock *tb;
9fa3e853 1239 PageDesc *p;
6b917547 1240 int n;
d720b93d 1241#ifdef TARGET_HAS_PRECISE_SMC
6b917547 1242 TranslationBlock *current_tb = NULL;
9349b4f9 1243 CPUArchState *env = cpu_single_env;
6b917547
AL
1244 int current_tb_modified = 0;
1245 target_ulong current_pc = 0;
1246 target_ulong current_cs_base = 0;
1247 int current_flags = 0;
d720b93d 1248#endif
9fa3e853
FB
1249
1250 addr &= TARGET_PAGE_MASK;
1251 p = page_find(addr >> TARGET_PAGE_BITS);
5fafdf24 1252 if (!p)
9fa3e853
FB
1253 return;
1254 tb = p->first_tb;
d720b93d
FB
1255#ifdef TARGET_HAS_PRECISE_SMC
1256 if (tb && pc != 0) {
1257 current_tb = tb_find_pc(pc);
1258 }
1259#endif
9fa3e853 1260 while (tb != NULL) {
8efe0ca8
SW
1261 n = (uintptr_t)tb & 3;
1262 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
d720b93d
FB
1263#ifdef TARGET_HAS_PRECISE_SMC
1264 if (current_tb == tb &&
2e70f6ef 1265 (current_tb->cflags & CF_COUNT_MASK) != 1) {
d720b93d
FB
1266 /* If we are modifying the current TB, we must stop
1267 its execution. We could be more precise by checking
1268 that the modification is after the current PC, but it
1269 would require a specialized function to partially
1270 restore the CPU state */
3b46e624 1271
d720b93d 1272 current_tb_modified = 1;
618ba8e6 1273 cpu_restore_state(current_tb, env, pc);
6b917547
AL
1274 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1275 &current_flags);
d720b93d
FB
1276 }
1277#endif /* TARGET_HAS_PRECISE_SMC */
9fa3e853
FB
1278 tb_phys_invalidate(tb, addr);
1279 tb = tb->page_next[n];
1280 }
fd6ce8f6 1281 p->first_tb = NULL;
d720b93d
FB
1282#ifdef TARGET_HAS_PRECISE_SMC
1283 if (current_tb_modified) {
1284 /* we generate a block containing just the instruction
1285 modifying the memory. It will ensure that it cannot modify
1286 itself */
ea1c1802 1287 env->current_tb = NULL;
2e70f6ef 1288 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
d720b93d
FB
1289 cpu_resume_from_signal(env, puc);
1290 }
1291#endif
fd6ce8f6 1292}
9fa3e853 1293#endif
fd6ce8f6
FB
1294
1295/* add the tb in the target page and protect it if necessary */
5fafdf24 1296static inline void tb_alloc_page(TranslationBlock *tb,
41c1b1c9 1297 unsigned int n, tb_page_addr_t page_addr)
fd6ce8f6
FB
1298{
1299 PageDesc *p;
4429ab44
JQ
1300#ifndef CONFIG_USER_ONLY
1301 bool page_already_protected;
1302#endif
9fa3e853
FB
1303
1304 tb->page_addr[n] = page_addr;
5cd2c5b6 1305 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
9fa3e853 1306 tb->page_next[n] = p->first_tb;
4429ab44
JQ
1307#ifndef CONFIG_USER_ONLY
1308 page_already_protected = p->first_tb != NULL;
1309#endif
8efe0ca8 1310 p->first_tb = (TranslationBlock *)((uintptr_t)tb | n);
9fa3e853 1311 invalidate_page_bitmap(p);
fd6ce8f6 1312
107db443 1313#if defined(TARGET_HAS_SMC) || 1
d720b93d 1314
9fa3e853 1315#if defined(CONFIG_USER_ONLY)
fd6ce8f6 1316 if (p->flags & PAGE_WRITE) {
53a5960a
PB
1317 target_ulong addr;
1318 PageDesc *p2;
9fa3e853
FB
1319 int prot;
1320
fd6ce8f6
FB
1321 /* force the host page as non writable (writes will have a
1322 page fault + mprotect overhead) */
53a5960a 1323 page_addr &= qemu_host_page_mask;
fd6ce8f6 1324 prot = 0;
53a5960a
PB
1325 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1326 addr += TARGET_PAGE_SIZE) {
1327
1328 p2 = page_find (addr >> TARGET_PAGE_BITS);
1329 if (!p2)
1330 continue;
1331 prot |= p2->flags;
1332 p2->flags &= ~PAGE_WRITE;
53a5960a 1333 }
5fafdf24 1334 mprotect(g2h(page_addr), qemu_host_page_size,
fd6ce8f6
FB
1335 (prot & PAGE_BITS) & ~PAGE_WRITE);
1336#ifdef DEBUG_TB_INVALIDATE
ab3d1727 1337 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
53a5960a 1338 page_addr);
fd6ce8f6 1339#endif
fd6ce8f6 1340 }
9fa3e853
FB
1341#else
1342 /* if some code is already present, then the pages are already
1343 protected. So we handle the case where only the first TB is
1344 allocated in a physical page */
4429ab44 1345 if (!page_already_protected) {
6a00d601 1346 tlb_protect_code(page_addr);
9fa3e853
FB
1347 }
1348#endif
d720b93d
FB
1349
1350#endif /* TARGET_HAS_SMC */
fd6ce8f6
FB
1351}
1352
9fa3e853
FB
1353/* add a new TB and link it to the physical page tables. phys_page2 is
1354 (-1) to indicate that only one page contains the TB. */
41c1b1c9
PB
1355void tb_link_page(TranslationBlock *tb,
1356 tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
d4e8164f 1357{
9fa3e853
FB
1358 unsigned int h;
1359 TranslationBlock **ptb;
1360
c8a706fe
PB
1361 /* Grab the mmap lock to stop another thread invalidating this TB
1362 before we are done. */
1363 mmap_lock();
9fa3e853
FB
1364 /* add in the physical hash table */
1365 h = tb_phys_hash_func(phys_pc);
1366 ptb = &tb_phys_hash[h];
1367 tb->phys_hash_next = *ptb;
1368 *ptb = tb;
fd6ce8f6
FB
1369
1370 /* add in the page list */
9fa3e853
FB
1371 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1372 if (phys_page2 != -1)
1373 tb_alloc_page(tb, 1, phys_page2);
1374 else
1375 tb->page_addr[1] = -1;
9fa3e853 1376
8efe0ca8 1377 tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2);
d4e8164f
FB
1378 tb->jmp_next[0] = NULL;
1379 tb->jmp_next[1] = NULL;
1380
1381 /* init original jump addresses */
1382 if (tb->tb_next_offset[0] != 0xffff)
1383 tb_reset_jump(tb, 0);
1384 if (tb->tb_next_offset[1] != 0xffff)
1385 tb_reset_jump(tb, 1);
8a40a180
FB
1386
1387#ifdef DEBUG_TB_CHECK
1388 tb_page_check();
1389#endif
c8a706fe 1390 mmap_unlock();
fd6ce8f6
FB
1391}
1392
9fa3e853
FB
1393/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1394 tb[1].tc_ptr. Return NULL if not found */
6375e09e 1395TranslationBlock *tb_find_pc(uintptr_t tc_ptr)
fd6ce8f6 1396{
9fa3e853 1397 int m_min, m_max, m;
8efe0ca8 1398 uintptr_t v;
9fa3e853 1399 TranslationBlock *tb;
a513fe19
FB
1400
1401 if (nb_tbs <= 0)
1402 return NULL;
8efe0ca8
SW
1403 if (tc_ptr < (uintptr_t)code_gen_buffer ||
1404 tc_ptr >= (uintptr_t)code_gen_ptr) {
a513fe19 1405 return NULL;
8efe0ca8 1406 }
a513fe19
FB
1407 /* binary search (cf Knuth) */
1408 m_min = 0;
1409 m_max = nb_tbs - 1;
1410 while (m_min <= m_max) {
1411 m = (m_min + m_max) >> 1;
1412 tb = &tbs[m];
8efe0ca8 1413 v = (uintptr_t)tb->tc_ptr;
a513fe19
FB
1414 if (v == tc_ptr)
1415 return tb;
1416 else if (tc_ptr < v) {
1417 m_max = m - 1;
1418 } else {
1419 m_min = m + 1;
1420 }
5fafdf24 1421 }
a513fe19
FB
1422 return &tbs[m_max];
1423}
7501267e 1424
ea041c0e
FB
1425static void tb_reset_jump_recursive(TranslationBlock *tb);
1426
1427static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1428{
1429 TranslationBlock *tb1, *tb_next, **ptb;
1430 unsigned int n1;
1431
1432 tb1 = tb->jmp_next[n];
1433 if (tb1 != NULL) {
1434 /* find head of list */
1435 for(;;) {
8efe0ca8
SW
1436 n1 = (uintptr_t)tb1 & 3;
1437 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
ea041c0e
FB
1438 if (n1 == 2)
1439 break;
1440 tb1 = tb1->jmp_next[n1];
1441 }
1442 /* we are now sure now that tb jumps to tb1 */
1443 tb_next = tb1;
1444
1445 /* remove tb from the jmp_first list */
1446 ptb = &tb_next->jmp_first;
1447 for(;;) {
1448 tb1 = *ptb;
8efe0ca8
SW
1449 n1 = (uintptr_t)tb1 & 3;
1450 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
ea041c0e
FB
1451 if (n1 == n && tb1 == tb)
1452 break;
1453 ptb = &tb1->jmp_next[n1];
1454 }
1455 *ptb = tb->jmp_next[n];
1456 tb->jmp_next[n] = NULL;
3b46e624 1457
ea041c0e
FB
1458 /* suppress the jump to next tb in generated code */
1459 tb_reset_jump(tb, n);
1460
0124311e 1461 /* suppress jumps in the tb on which we could have jumped */
ea041c0e
FB
1462 tb_reset_jump_recursive(tb_next);
1463 }
1464}
1465
1466static void tb_reset_jump_recursive(TranslationBlock *tb)
1467{
1468 tb_reset_jump_recursive2(tb, 0);
1469 tb_reset_jump_recursive2(tb, 1);
1470}
1471
1fddef4b 1472#if defined(TARGET_HAS_ICE)
94df27fd 1473#if defined(CONFIG_USER_ONLY)
9349b4f9 1474static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
94df27fd
PB
1475{
1476 tb_invalidate_phys_page_range(pc, pc + 1, 0);
1477}
1478#else
1e7855a5 1479void tb_invalidate_phys_addr(target_phys_addr_t addr)
d720b93d 1480{
c227f099 1481 ram_addr_t ram_addr;
f3705d53 1482 MemoryRegionSection *section;
d720b93d 1483
06ef3525 1484 section = phys_page_find(addr >> TARGET_PAGE_BITS);
f3705d53
AK
1485 if (!(memory_region_is_ram(section->mr)
1486 || (section->mr->rom_device && section->mr->readable))) {
06ef3525
AK
1487 return;
1488 }
f3705d53 1489 ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
cc5bea60 1490 + memory_region_section_addr(section, addr);
706cd4b5 1491 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
d720b93d 1492}
1e7855a5
MF
1493
1494static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
1495{
9d70c4b7
MF
1496 tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
1497 (pc & ~TARGET_PAGE_MASK));
1e7855a5 1498}
c27004ec 1499#endif
94df27fd 1500#endif /* TARGET_HAS_ICE */
d720b93d 1501
c527ee8f 1502#if defined(CONFIG_USER_ONLY)
9349b4f9 1503void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
c527ee8f
PB
1504
1505{
1506}
1507
9349b4f9 1508int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
c527ee8f
PB
1509 int flags, CPUWatchpoint **watchpoint)
1510{
1511 return -ENOSYS;
1512}
1513#else
6658ffb8 1514/* Add a watchpoint. */
9349b4f9 1515int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
a1d1bb31 1516 int flags, CPUWatchpoint **watchpoint)
6658ffb8 1517{
b4051334 1518 target_ulong len_mask = ~(len - 1);
c0ce998e 1519 CPUWatchpoint *wp;
6658ffb8 1520
b4051334 1521 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
0dc23828
MF
1522 if ((len & (len - 1)) || (addr & ~len_mask) ||
1523 len == 0 || len > TARGET_PAGE_SIZE) {
b4051334
AL
1524 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1525 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1526 return -EINVAL;
1527 }
7267c094 1528 wp = g_malloc(sizeof(*wp));
a1d1bb31
AL
1529
1530 wp->vaddr = addr;
b4051334 1531 wp->len_mask = len_mask;
a1d1bb31
AL
1532 wp->flags = flags;
1533
2dc9f411 1534 /* keep all GDB-injected watchpoints in front */
c0ce998e 1535 if (flags & BP_GDB)
72cf2d4f 1536 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
c0ce998e 1537 else
72cf2d4f 1538 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
6658ffb8 1539
6658ffb8 1540 tlb_flush_page(env, addr);
a1d1bb31
AL
1541
1542 if (watchpoint)
1543 *watchpoint = wp;
1544 return 0;
6658ffb8
PB
1545}
1546
a1d1bb31 1547/* Remove a specific watchpoint. */
9349b4f9 1548int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
a1d1bb31 1549 int flags)
6658ffb8 1550{
b4051334 1551 target_ulong len_mask = ~(len - 1);
a1d1bb31 1552 CPUWatchpoint *wp;
6658ffb8 1553
72cf2d4f 1554 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
b4051334 1555 if (addr == wp->vaddr && len_mask == wp->len_mask
6e140f28 1556 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
a1d1bb31 1557 cpu_watchpoint_remove_by_ref(env, wp);
6658ffb8
PB
1558 return 0;
1559 }
1560 }
a1d1bb31 1561 return -ENOENT;
6658ffb8
PB
1562}
1563
a1d1bb31 1564/* Remove a specific watchpoint by reference. */
9349b4f9 1565void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
a1d1bb31 1566{
72cf2d4f 1567 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
7d03f82f 1568
a1d1bb31
AL
1569 tlb_flush_page(env, watchpoint->vaddr);
1570
7267c094 1571 g_free(watchpoint);
a1d1bb31
AL
1572}
1573
1574/* Remove all matching watchpoints. */
9349b4f9 1575void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
a1d1bb31 1576{
c0ce998e 1577 CPUWatchpoint *wp, *next;
a1d1bb31 1578
72cf2d4f 1579 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
a1d1bb31
AL
1580 if (wp->flags & mask)
1581 cpu_watchpoint_remove_by_ref(env, wp);
c0ce998e 1582 }
7d03f82f 1583}
c527ee8f 1584#endif
7d03f82f 1585
a1d1bb31 1586/* Add a breakpoint. */
9349b4f9 1587int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
a1d1bb31 1588 CPUBreakpoint **breakpoint)
4c3a88a2 1589{
1fddef4b 1590#if defined(TARGET_HAS_ICE)
c0ce998e 1591 CPUBreakpoint *bp;
3b46e624 1592
7267c094 1593 bp = g_malloc(sizeof(*bp));
4c3a88a2 1594
a1d1bb31
AL
1595 bp->pc = pc;
1596 bp->flags = flags;
1597
2dc9f411 1598 /* keep all GDB-injected breakpoints in front */
c0ce998e 1599 if (flags & BP_GDB)
72cf2d4f 1600 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
c0ce998e 1601 else
72cf2d4f 1602 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
3b46e624 1603
d720b93d 1604 breakpoint_invalidate(env, pc);
a1d1bb31
AL
1605
1606 if (breakpoint)
1607 *breakpoint = bp;
4c3a88a2
FB
1608 return 0;
1609#else
a1d1bb31 1610 return -ENOSYS;
4c3a88a2
FB
1611#endif
1612}
1613
a1d1bb31 1614/* Remove a specific breakpoint. */
9349b4f9 1615int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
a1d1bb31 1616{
7d03f82f 1617#if defined(TARGET_HAS_ICE)
a1d1bb31
AL
1618 CPUBreakpoint *bp;
1619
72cf2d4f 1620 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a1d1bb31
AL
1621 if (bp->pc == pc && bp->flags == flags) {
1622 cpu_breakpoint_remove_by_ref(env, bp);
1623 return 0;
1624 }
7d03f82f 1625 }
a1d1bb31
AL
1626 return -ENOENT;
1627#else
1628 return -ENOSYS;
7d03f82f
EI
1629#endif
1630}
1631
a1d1bb31 1632/* Remove a specific breakpoint by reference. */
9349b4f9 1633void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
4c3a88a2 1634{
1fddef4b 1635#if defined(TARGET_HAS_ICE)
72cf2d4f 1636 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
d720b93d 1637
a1d1bb31
AL
1638 breakpoint_invalidate(env, breakpoint->pc);
1639
7267c094 1640 g_free(breakpoint);
a1d1bb31
AL
1641#endif
1642}
1643
1644/* Remove all matching breakpoints. */
9349b4f9 1645void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
a1d1bb31
AL
1646{
1647#if defined(TARGET_HAS_ICE)
c0ce998e 1648 CPUBreakpoint *bp, *next;
a1d1bb31 1649
72cf2d4f 1650 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
a1d1bb31
AL
1651 if (bp->flags & mask)
1652 cpu_breakpoint_remove_by_ref(env, bp);
c0ce998e 1653 }
4c3a88a2
FB
1654#endif
1655}
1656
c33a346e
FB
1657/* enable or disable single step mode. EXCP_DEBUG is returned by the
1658 CPU loop after each instruction */
9349b4f9 1659void cpu_single_step(CPUArchState *env, int enabled)
c33a346e 1660{
1fddef4b 1661#if defined(TARGET_HAS_ICE)
c33a346e
FB
1662 if (env->singlestep_enabled != enabled) {
1663 env->singlestep_enabled = enabled;
e22a25c9
AL
1664 if (kvm_enabled())
1665 kvm_update_guest_debug(env, 0);
1666 else {
ccbb4d44 1667 /* must flush all the translated code to avoid inconsistencies */
e22a25c9
AL
1668 /* XXX: only flush what is necessary */
1669 tb_flush(env);
1670 }
c33a346e
FB
1671 }
1672#endif
1673}
1674
9349b4f9 1675static void cpu_unlink_tb(CPUArchState *env)
ea041c0e 1676{
3098dba0
AJ
1677 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1678 problem and hope the cpu will stop of its own accord. For userspace
1679 emulation this often isn't actually as bad as it sounds. Often
1680 signals are used primarily to interrupt blocking syscalls. */
ea041c0e 1681 TranslationBlock *tb;
c227f099 1682 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
59817ccb 1683
cab1b4bd 1684 spin_lock(&interrupt_lock);
3098dba0
AJ
1685 tb = env->current_tb;
1686 /* if the cpu is currently executing code, we must unlink it and
1687 all the potentially executing TB */
f76cfe56 1688 if (tb) {
3098dba0
AJ
1689 env->current_tb = NULL;
1690 tb_reset_jump_recursive(tb);
be214e6c 1691 }
cab1b4bd 1692 spin_unlock(&interrupt_lock);
3098dba0
AJ
1693}
1694
97ffbd8d 1695#ifndef CONFIG_USER_ONLY
3098dba0 1696/* mask must never be zero, except for A20 change call */
9349b4f9 1697static void tcg_handle_interrupt(CPUArchState *env, int mask)
3098dba0
AJ
1698{
1699 int old_mask;
be214e6c 1700
2e70f6ef 1701 old_mask = env->interrupt_request;
68a79315 1702 env->interrupt_request |= mask;
3098dba0 1703
8edac960
AL
1704 /*
1705 * If called from iothread context, wake the target cpu in
1706 * case its halted.
1707 */
b7680cb6 1708 if (!qemu_cpu_is_self(env)) {
8edac960
AL
1709 qemu_cpu_kick(env);
1710 return;
1711 }
8edac960 1712
2e70f6ef 1713 if (use_icount) {
266910c4 1714 env->icount_decr.u16.high = 0xffff;
2e70f6ef 1715 if (!can_do_io(env)
be214e6c 1716 && (mask & ~old_mask) != 0) {
2e70f6ef
PB
1717 cpu_abort(env, "Raised interrupt while not in I/O function");
1718 }
2e70f6ef 1719 } else {
3098dba0 1720 cpu_unlink_tb(env);
ea041c0e
FB
1721 }
1722}
1723
ec6959d0
JK
1724CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
1725
97ffbd8d
JK
1726#else /* CONFIG_USER_ONLY */
1727
9349b4f9 1728void cpu_interrupt(CPUArchState *env, int mask)
97ffbd8d
JK
1729{
1730 env->interrupt_request |= mask;
1731 cpu_unlink_tb(env);
1732}
1733#endif /* CONFIG_USER_ONLY */
1734
9349b4f9 1735void cpu_reset_interrupt(CPUArchState *env, int mask)
b54ad049
FB
1736{
1737 env->interrupt_request &= ~mask;
1738}
1739
9349b4f9 1740void cpu_exit(CPUArchState *env)
3098dba0
AJ
1741{
1742 env->exit_request = 1;
1743 cpu_unlink_tb(env);
1744}
1745
9349b4f9 1746void cpu_abort(CPUArchState *env, const char *fmt, ...)
7501267e
FB
1747{
1748 va_list ap;
493ae1f0 1749 va_list ap2;
7501267e
FB
1750
1751 va_start(ap, fmt);
493ae1f0 1752 va_copy(ap2, ap);
7501267e
FB
1753 fprintf(stderr, "qemu: fatal: ");
1754 vfprintf(stderr, fmt, ap);
1755 fprintf(stderr, "\n");
6fd2a026 1756 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
93fcfe39
AL
1757 if (qemu_log_enabled()) {
1758 qemu_log("qemu: fatal: ");
1759 qemu_log_vprintf(fmt, ap2);
1760 qemu_log("\n");
6fd2a026 1761 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
31b1a7b4 1762 qemu_log_flush();
93fcfe39 1763 qemu_log_close();
924edcae 1764 }
493ae1f0 1765 va_end(ap2);
f9373291 1766 va_end(ap);
fd052bf6
RV
1767#if defined(CONFIG_USER_ONLY)
1768 {
1769 struct sigaction act;
1770 sigfillset(&act.sa_mask);
1771 act.sa_handler = SIG_DFL;
1772 sigaction(SIGABRT, &act, NULL);
1773 }
1774#endif
7501267e
FB
1775 abort();
1776}
1777
9349b4f9 1778CPUArchState *cpu_copy(CPUArchState *env)
c5be9f08 1779{
9349b4f9
AF
1780 CPUArchState *new_env = cpu_init(env->cpu_model_str);
1781 CPUArchState *next_cpu = new_env->next_cpu;
c5be9f08 1782 int cpu_index = new_env->cpu_index;
5a38f081
AL
1783#if defined(TARGET_HAS_ICE)
1784 CPUBreakpoint *bp;
1785 CPUWatchpoint *wp;
1786#endif
1787
9349b4f9 1788 memcpy(new_env, env, sizeof(CPUArchState));
5a38f081
AL
1789
1790 /* Preserve chaining and index. */
c5be9f08
TS
1791 new_env->next_cpu = next_cpu;
1792 new_env->cpu_index = cpu_index;
5a38f081
AL
1793
1794 /* Clone all break/watchpoints.
1795 Note: Once we support ptrace with hw-debug register access, make sure
1796 BP_CPU break/watchpoints are handled correctly on clone. */
72cf2d4f
BS
1797 QTAILQ_INIT(&env->breakpoints);
1798 QTAILQ_INIT(&env->watchpoints);
5a38f081 1799#if defined(TARGET_HAS_ICE)
72cf2d4f 1800 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
5a38f081
AL
1801 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1802 }
72cf2d4f 1803 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
5a38f081
AL
1804 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1805 wp->flags, NULL);
1806 }
1807#endif
1808
c5be9f08
TS
1809 return new_env;
1810}
1811
0124311e 1812#if !defined(CONFIG_USER_ONLY)
0cac1b66 1813void tb_flush_jmp_cache(CPUArchState *env, target_ulong addr)
5c751e99
EI
1814{
1815 unsigned int i;
1816
1817 /* Discard jump cache entries for any tb which might potentially
1818 overlap the flushed page. */
1819 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1820 memset (&env->tb_jmp_cache[i], 0,
9742bf26 1821 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
5c751e99
EI
1822
1823 i = tb_jmp_cache_hash_page(addr);
1824 memset (&env->tb_jmp_cache[i], 0,
9742bf26 1825 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
5c751e99
EI
1826}
1827
d24981d3
JQ
1828static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
1829 uintptr_t length)
1830{
1831 uintptr_t start1;
1832
1833 /* we modify the TLB cache so that the dirty bit will be set again
1834 when accessing the range */
1835 start1 = (uintptr_t)qemu_safe_ram_ptr(start);
1836 /* Check that we don't span multiple blocks - this breaks the
1837 address comparisons below. */
1838 if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
1839 != (end - 1) - start) {
1840 abort();
1841 }
1842 cpu_tlb_reset_dirty_all(start1, length);
1843
1844}
1845
5579c7f3 1846/* Note: start and end must be within the same ram block. */
c227f099 1847void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
0a962c02 1848 int dirty_flags)
1ccde1cb 1849{
d24981d3 1850 uintptr_t length;
1ccde1cb
FB
1851
1852 start &= TARGET_PAGE_MASK;
1853 end = TARGET_PAGE_ALIGN(end);
1854
1855 length = end - start;
1856 if (length == 0)
1857 return;
f7c11b53 1858 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
f23db169 1859
d24981d3
JQ
1860 if (tcg_enabled()) {
1861 tlb_reset_dirty_range_all(start, end, length);
5579c7f3 1862 }
1ccde1cb
FB
1863}
1864
74576198
AL
1865int cpu_physical_memory_set_dirty_tracking(int enable)
1866{
f6f3fbca 1867 int ret = 0;
74576198 1868 in_migration = enable;
f6f3fbca 1869 return ret;
74576198
AL
1870}
1871
e5548617
BS
1872target_phys_addr_t memory_region_section_get_iotlb(CPUArchState *env,
1873 MemoryRegionSection *section,
1874 target_ulong vaddr,
1875 target_phys_addr_t paddr,
1876 int prot,
1877 target_ulong *address)
1878{
1879 target_phys_addr_t iotlb;
1880 CPUWatchpoint *wp;
1881
cc5bea60 1882 if (memory_region_is_ram(section->mr)) {
e5548617
BS
1883 /* Normal RAM. */
1884 iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
cc5bea60 1885 + memory_region_section_addr(section, paddr);
e5548617
BS
1886 if (!section->readonly) {
1887 iotlb |= phys_section_notdirty;
1888 } else {
1889 iotlb |= phys_section_rom;
1890 }
1891 } else {
1892 /* IO handlers are currently passed a physical address.
1893 It would be nice to pass an offset from the base address
1894 of that region. This would avoid having to special case RAM,
1895 and avoid full address decoding in every device.
1896 We can't use the high bits of pd for this because
1897 IO_MEM_ROMD uses these as a ram address. */
1898 iotlb = section - phys_sections;
cc5bea60 1899 iotlb += memory_region_section_addr(section, paddr);
e5548617
BS
1900 }
1901
1902 /* Make accesses to pages with watchpoints go via the
1903 watchpoint trap routines. */
1904 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1905 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
1906 /* Avoid trapping reads of pages with a write breakpoint. */
1907 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
1908 iotlb = phys_section_watch + paddr;
1909 *address |= TLB_MMIO;
1910 break;
1911 }
1912 }
1913 }
1914
1915 return iotlb;
1916}
1917
0124311e 1918#else
edf8e2af
MW
1919/*
1920 * Walks guest process memory "regions" one by one
1921 * and calls callback function 'fn' for each region.
1922 */
5cd2c5b6
RH
1923
1924struct walk_memory_regions_data
1925{
1926 walk_memory_regions_fn fn;
1927 void *priv;
8efe0ca8 1928 uintptr_t start;
5cd2c5b6
RH
1929 int prot;
1930};
1931
1932static int walk_memory_regions_end(struct walk_memory_regions_data *data,
b480d9b7 1933 abi_ulong end, int new_prot)
5cd2c5b6
RH
1934{
1935 if (data->start != -1ul) {
1936 int rc = data->fn(data->priv, data->start, end, data->prot);
1937 if (rc != 0) {
1938 return rc;
1939 }
1940 }
1941
1942 data->start = (new_prot ? end : -1ul);
1943 data->prot = new_prot;
1944
1945 return 0;
1946}
1947
1948static int walk_memory_regions_1(struct walk_memory_regions_data *data,
b480d9b7 1949 abi_ulong base, int level, void **lp)
5cd2c5b6 1950{
b480d9b7 1951 abi_ulong pa;
5cd2c5b6
RH
1952 int i, rc;
1953
1954 if (*lp == NULL) {
1955 return walk_memory_regions_end(data, base, 0);
1956 }
1957
1958 if (level == 0) {
1959 PageDesc *pd = *lp;
7296abac 1960 for (i = 0; i < L2_SIZE; ++i) {
5cd2c5b6
RH
1961 int prot = pd[i].flags;
1962
1963 pa = base | (i << TARGET_PAGE_BITS);
1964 if (prot != data->prot) {
1965 rc = walk_memory_regions_end(data, pa, prot);
1966 if (rc != 0) {
1967 return rc;
9fa3e853 1968 }
9fa3e853 1969 }
5cd2c5b6
RH
1970 }
1971 } else {
1972 void **pp = *lp;
7296abac 1973 for (i = 0; i < L2_SIZE; ++i) {
b480d9b7
PB
1974 pa = base | ((abi_ulong)i <<
1975 (TARGET_PAGE_BITS + L2_BITS * level));
5cd2c5b6
RH
1976 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
1977 if (rc != 0) {
1978 return rc;
1979 }
1980 }
1981 }
1982
1983 return 0;
1984}
1985
1986int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
1987{
1988 struct walk_memory_regions_data data;
8efe0ca8 1989 uintptr_t i;
5cd2c5b6
RH
1990
1991 data.fn = fn;
1992 data.priv = priv;
1993 data.start = -1ul;
1994 data.prot = 0;
1995
1996 for (i = 0; i < V_L1_SIZE; i++) {
b480d9b7 1997 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
5cd2c5b6
RH
1998 V_L1_SHIFT / L2_BITS - 1, l1_map + i);
1999 if (rc != 0) {
2000 return rc;
9fa3e853 2001 }
33417e70 2002 }
5cd2c5b6
RH
2003
2004 return walk_memory_regions_end(&data, 0, 0);
edf8e2af
MW
2005}
2006
b480d9b7
PB
2007static int dump_region(void *priv, abi_ulong start,
2008 abi_ulong end, unsigned long prot)
edf8e2af
MW
2009{
2010 FILE *f = (FILE *)priv;
2011
b480d9b7
PB
2012 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
2013 " "TARGET_ABI_FMT_lx" %c%c%c\n",
edf8e2af
MW
2014 start, end, end - start,
2015 ((prot & PAGE_READ) ? 'r' : '-'),
2016 ((prot & PAGE_WRITE) ? 'w' : '-'),
2017 ((prot & PAGE_EXEC) ? 'x' : '-'));
2018
2019 return (0);
2020}
2021
2022/* dump memory mappings */
2023void page_dump(FILE *f)
2024{
2025 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2026 "start", "end", "size", "prot");
2027 walk_memory_regions(f, dump_region);
33417e70
FB
2028}
2029
53a5960a 2030int page_get_flags(target_ulong address)
33417e70 2031{
9fa3e853
FB
2032 PageDesc *p;
2033
2034 p = page_find(address >> TARGET_PAGE_BITS);
33417e70 2035 if (!p)
9fa3e853
FB
2036 return 0;
2037 return p->flags;
2038}
2039
376a7909
RH
2040/* Modify the flags of a page and invalidate the code if necessary.
2041 The flag PAGE_WRITE_ORG is positioned automatically depending
2042 on PAGE_WRITE. The mmap_lock should already be held. */
53a5960a 2043void page_set_flags(target_ulong start, target_ulong end, int flags)
9fa3e853 2044{
376a7909
RH
2045 target_ulong addr, len;
2046
2047 /* This function should never be called with addresses outside the
2048 guest address space. If this assert fires, it probably indicates
2049 a missing call to h2g_valid. */
b480d9b7
PB
2050#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2051 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
376a7909
RH
2052#endif
2053 assert(start < end);
9fa3e853
FB
2054
2055 start = start & TARGET_PAGE_MASK;
2056 end = TARGET_PAGE_ALIGN(end);
376a7909
RH
2057
2058 if (flags & PAGE_WRITE) {
9fa3e853 2059 flags |= PAGE_WRITE_ORG;
376a7909
RH
2060 }
2061
2062 for (addr = start, len = end - start;
2063 len != 0;
2064 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2065 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2066
2067 /* If the write protection bit is set, then we invalidate
2068 the code inside. */
5fafdf24 2069 if (!(p->flags & PAGE_WRITE) &&
9fa3e853
FB
2070 (flags & PAGE_WRITE) &&
2071 p->first_tb) {
d720b93d 2072 tb_invalidate_phys_page(addr, 0, NULL);
9fa3e853
FB
2073 }
2074 p->flags = flags;
2075 }
33417e70
FB
2076}
2077
3d97b40b
TS
2078int page_check_range(target_ulong start, target_ulong len, int flags)
2079{
2080 PageDesc *p;
2081 target_ulong end;
2082 target_ulong addr;
2083
376a7909
RH
2084 /* This function should never be called with addresses outside the
2085 guest address space. If this assert fires, it probably indicates
2086 a missing call to h2g_valid. */
338e9e6c
BS
2087#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2088 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
376a7909
RH
2089#endif
2090
3e0650a9
RH
2091 if (len == 0) {
2092 return 0;
2093 }
376a7909
RH
2094 if (start + len - 1 < start) {
2095 /* We've wrapped around. */
55f280c9 2096 return -1;
376a7909 2097 }
55f280c9 2098
3d97b40b
TS
2099 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2100 start = start & TARGET_PAGE_MASK;
2101
376a7909
RH
2102 for (addr = start, len = end - start;
2103 len != 0;
2104 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
3d97b40b
TS
2105 p = page_find(addr >> TARGET_PAGE_BITS);
2106 if( !p )
2107 return -1;
2108 if( !(p->flags & PAGE_VALID) )
2109 return -1;
2110
dae3270c 2111 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
3d97b40b 2112 return -1;
dae3270c
FB
2113 if (flags & PAGE_WRITE) {
2114 if (!(p->flags & PAGE_WRITE_ORG))
2115 return -1;
2116 /* unprotect the page if it was put read-only because it
2117 contains translated code */
2118 if (!(p->flags & PAGE_WRITE)) {
2119 if (!page_unprotect(addr, 0, NULL))
2120 return -1;
2121 }
2122 return 0;
2123 }
3d97b40b
TS
2124 }
2125 return 0;
2126}
2127
9fa3e853 2128/* called from signal handler: invalidate the code and unprotect the
ccbb4d44 2129 page. Return TRUE if the fault was successfully handled. */
6375e09e 2130int page_unprotect(target_ulong address, uintptr_t pc, void *puc)
9fa3e853 2131{
45d679d6
AJ
2132 unsigned int prot;
2133 PageDesc *p;
53a5960a 2134 target_ulong host_start, host_end, addr;
9fa3e853 2135
c8a706fe
PB
2136 /* Technically this isn't safe inside a signal handler. However we
2137 know this only ever happens in a synchronous SEGV handler, so in
2138 practice it seems to be ok. */
2139 mmap_lock();
2140
45d679d6
AJ
2141 p = page_find(address >> TARGET_PAGE_BITS);
2142 if (!p) {
c8a706fe 2143 mmap_unlock();
9fa3e853 2144 return 0;
c8a706fe 2145 }
45d679d6 2146
9fa3e853
FB
2147 /* if the page was really writable, then we change its
2148 protection back to writable */
45d679d6
AJ
2149 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2150 host_start = address & qemu_host_page_mask;
2151 host_end = host_start + qemu_host_page_size;
2152
2153 prot = 0;
2154 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2155 p = page_find(addr >> TARGET_PAGE_BITS);
2156 p->flags |= PAGE_WRITE;
2157 prot |= p->flags;
2158
9fa3e853
FB
2159 /* and since the content will be modified, we must invalidate
2160 the corresponding translated code. */
45d679d6 2161 tb_invalidate_phys_page(addr, pc, puc);
9fa3e853 2162#ifdef DEBUG_TB_CHECK
45d679d6 2163 tb_invalidate_check(addr);
9fa3e853 2164#endif
9fa3e853 2165 }
45d679d6
AJ
2166 mprotect((void *)g2h(host_start), qemu_host_page_size,
2167 prot & PAGE_BITS);
2168
2169 mmap_unlock();
2170 return 1;
9fa3e853 2171 }
c8a706fe 2172 mmap_unlock();
9fa3e853
FB
2173 return 0;
2174}
9fa3e853
FB
2175#endif /* defined(CONFIG_USER_ONLY) */
2176
e2eef170 2177#if !defined(CONFIG_USER_ONLY)
8da3ff18 2178
c04b2b78
PB
2179#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2180typedef struct subpage_t {
70c68e44 2181 MemoryRegion iomem;
c04b2b78 2182 target_phys_addr_t base;
5312bd8b 2183 uint16_t sub_section[TARGET_PAGE_SIZE];
c04b2b78
PB
2184} subpage_t;
2185
c227f099 2186static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
5312bd8b 2187 uint16_t section);
0f0cb164 2188static subpage_t *subpage_init(target_phys_addr_t base);
5312bd8b 2189static void destroy_page_desc(uint16_t section_index)
54688b1e 2190{
5312bd8b
AK
2191 MemoryRegionSection *section = &phys_sections[section_index];
2192 MemoryRegion *mr = section->mr;
54688b1e
AK
2193
2194 if (mr->subpage) {
2195 subpage_t *subpage = container_of(mr, subpage_t, iomem);
2196 memory_region_destroy(&subpage->iomem);
2197 g_free(subpage);
2198 }
2199}
2200
4346ae3e 2201static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
54688b1e
AK
2202{
2203 unsigned i;
d6f2ea22 2204 PhysPageEntry *p;
54688b1e 2205
c19e8800 2206 if (lp->ptr == PHYS_MAP_NODE_NIL) {
54688b1e
AK
2207 return;
2208 }
2209
c19e8800 2210 p = phys_map_nodes[lp->ptr];
4346ae3e 2211 for (i = 0; i < L2_SIZE; ++i) {
07f07b31 2212 if (!p[i].is_leaf) {
54688b1e 2213 destroy_l2_mapping(&p[i], level - 1);
4346ae3e 2214 } else {
c19e8800 2215 destroy_page_desc(p[i].ptr);
54688b1e 2216 }
54688b1e 2217 }
07f07b31 2218 lp->is_leaf = 0;
c19e8800 2219 lp->ptr = PHYS_MAP_NODE_NIL;
54688b1e
AK
2220}
2221
2222static void destroy_all_mappings(void)
2223{
3eef53df 2224 destroy_l2_mapping(&phys_map, P_L2_LEVELS - 1);
d6f2ea22 2225 phys_map_nodes_reset();
54688b1e
AK
2226}
2227
5312bd8b
AK
2228static uint16_t phys_section_add(MemoryRegionSection *section)
2229{
2230 if (phys_sections_nb == phys_sections_nb_alloc) {
2231 phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
2232 phys_sections = g_renew(MemoryRegionSection, phys_sections,
2233 phys_sections_nb_alloc);
2234 }
2235 phys_sections[phys_sections_nb] = *section;
2236 return phys_sections_nb++;
2237}
2238
2239static void phys_sections_clear(void)
2240{
2241 phys_sections_nb = 0;
2242}
2243
0f0cb164
AK
2244static void register_subpage(MemoryRegionSection *section)
2245{
2246 subpage_t *subpage;
2247 target_phys_addr_t base = section->offset_within_address_space
2248 & TARGET_PAGE_MASK;
f3705d53 2249 MemoryRegionSection *existing = phys_page_find(base >> TARGET_PAGE_BITS);
0f0cb164
AK
2250 MemoryRegionSection subsection = {
2251 .offset_within_address_space = base,
2252 .size = TARGET_PAGE_SIZE,
2253 };
0f0cb164
AK
2254 target_phys_addr_t start, end;
2255
f3705d53 2256 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
0f0cb164 2257
f3705d53 2258 if (!(existing->mr->subpage)) {
0f0cb164
AK
2259 subpage = subpage_init(base);
2260 subsection.mr = &subpage->iomem;
2999097b
AK
2261 phys_page_set(base >> TARGET_PAGE_BITS, 1,
2262 phys_section_add(&subsection));
0f0cb164 2263 } else {
f3705d53 2264 subpage = container_of(existing->mr, subpage_t, iomem);
0f0cb164
AK
2265 }
2266 start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
adb2a9b5 2267 end = start + section->size - 1;
0f0cb164
AK
2268 subpage_register(subpage, start, end, phys_section_add(section));
2269}
2270
2271
2272static void register_multipage(MemoryRegionSection *section)
33417e70 2273{
dd81124b
AK
2274 target_phys_addr_t start_addr = section->offset_within_address_space;
2275 ram_addr_t size = section->size;
2999097b 2276 target_phys_addr_t addr;
5312bd8b 2277 uint16_t section_index = phys_section_add(section);
dd81124b 2278
3b8e6a2d 2279 assert(size);
f6f3fbca 2280
3b8e6a2d 2281 addr = start_addr;
2999097b
AK
2282 phys_page_set(addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
2283 section_index);
33417e70
FB
2284}
2285
0f0cb164
AK
2286void cpu_register_physical_memory_log(MemoryRegionSection *section,
2287 bool readonly)
2288{
2289 MemoryRegionSection now = *section, remain = *section;
2290
2291 if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
2292 || (now.size < TARGET_PAGE_SIZE)) {
2293 now.size = MIN(TARGET_PAGE_ALIGN(now.offset_within_address_space)
2294 - now.offset_within_address_space,
2295 now.size);
2296 register_subpage(&now);
2297 remain.size -= now.size;
2298 remain.offset_within_address_space += now.size;
2299 remain.offset_within_region += now.size;
2300 }
69b67646
TH
2301 while (remain.size >= TARGET_PAGE_SIZE) {
2302 now = remain;
2303 if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
2304 now.size = TARGET_PAGE_SIZE;
2305 register_subpage(&now);
2306 } else {
2307 now.size &= TARGET_PAGE_MASK;
2308 register_multipage(&now);
2309 }
0f0cb164
AK
2310 remain.size -= now.size;
2311 remain.offset_within_address_space += now.size;
2312 remain.offset_within_region += now.size;
2313 }
2314 now = remain;
2315 if (now.size) {
2316 register_subpage(&now);
2317 }
2318}
2319
2320
c227f099 2321void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
f65ed4c1
AL
2322{
2323 if (kvm_enabled())
2324 kvm_coalesce_mmio_region(addr, size);
2325}
2326
c227f099 2327void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
f65ed4c1
AL
2328{
2329 if (kvm_enabled())
2330 kvm_uncoalesce_mmio_region(addr, size);
2331}
2332
62a2744c
SY
2333void qemu_flush_coalesced_mmio_buffer(void)
2334{
2335 if (kvm_enabled())
2336 kvm_flush_coalesced_mmio_buffer();
2337}
2338
c902760f
MT
2339#if defined(__linux__) && !defined(TARGET_S390X)
2340
2341#include <sys/vfs.h>
2342
2343#define HUGETLBFS_MAGIC 0x958458f6
2344
2345static long gethugepagesize(const char *path)
2346{
2347 struct statfs fs;
2348 int ret;
2349
2350 do {
9742bf26 2351 ret = statfs(path, &fs);
c902760f
MT
2352 } while (ret != 0 && errno == EINTR);
2353
2354 if (ret != 0) {
9742bf26
YT
2355 perror(path);
2356 return 0;
c902760f
MT
2357 }
2358
2359 if (fs.f_type != HUGETLBFS_MAGIC)
9742bf26 2360 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
c902760f
MT
2361
2362 return fs.f_bsize;
2363}
2364
04b16653
AW
2365static void *file_ram_alloc(RAMBlock *block,
2366 ram_addr_t memory,
2367 const char *path)
c902760f
MT
2368{
2369 char *filename;
2370 void *area;
2371 int fd;
2372#ifdef MAP_POPULATE
2373 int flags;
2374#endif
2375 unsigned long hpagesize;
2376
2377 hpagesize = gethugepagesize(path);
2378 if (!hpagesize) {
9742bf26 2379 return NULL;
c902760f
MT
2380 }
2381
2382 if (memory < hpagesize) {
2383 return NULL;
2384 }
2385
2386 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2387 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2388 return NULL;
2389 }
2390
2391 if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
9742bf26 2392 return NULL;
c902760f
MT
2393 }
2394
2395 fd = mkstemp(filename);
2396 if (fd < 0) {
9742bf26
YT
2397 perror("unable to create backing store for hugepages");
2398 free(filename);
2399 return NULL;
c902760f
MT
2400 }
2401 unlink(filename);
2402 free(filename);
2403
2404 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2405
2406 /*
2407 * ftruncate is not supported by hugetlbfs in older
2408 * hosts, so don't bother bailing out on errors.
2409 * If anything goes wrong with it under other filesystems,
2410 * mmap will fail.
2411 */
2412 if (ftruncate(fd, memory))
9742bf26 2413 perror("ftruncate");
c902760f
MT
2414
2415#ifdef MAP_POPULATE
2416 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2417 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2418 * to sidestep this quirk.
2419 */
2420 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
2421 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
2422#else
2423 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
2424#endif
2425 if (area == MAP_FAILED) {
9742bf26
YT
2426 perror("file_ram_alloc: can't mmap RAM pages");
2427 close(fd);
2428 return (NULL);
c902760f 2429 }
04b16653 2430 block->fd = fd;
c902760f
MT
2431 return area;
2432}
2433#endif
2434
d17b5288 2435static ram_addr_t find_ram_offset(ram_addr_t size)
04b16653
AW
2436{
2437 RAMBlock *block, *next_block;
3e837b2c 2438 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
04b16653
AW
2439
2440 if (QLIST_EMPTY(&ram_list.blocks))
2441 return 0;
2442
2443 QLIST_FOREACH(block, &ram_list.blocks, next) {
f15fbc4b 2444 ram_addr_t end, next = RAM_ADDR_MAX;
04b16653
AW
2445
2446 end = block->offset + block->length;
2447
2448 QLIST_FOREACH(next_block, &ram_list.blocks, next) {
2449 if (next_block->offset >= end) {
2450 next = MIN(next, next_block->offset);
2451 }
2452 }
2453 if (next - end >= size && next - end < mingap) {
3e837b2c 2454 offset = end;
04b16653
AW
2455 mingap = next - end;
2456 }
2457 }
3e837b2c
AW
2458
2459 if (offset == RAM_ADDR_MAX) {
2460 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
2461 (uint64_t)size);
2462 abort();
2463 }
2464
04b16653
AW
2465 return offset;
2466}
2467
2468static ram_addr_t last_ram_offset(void)
d17b5288
AW
2469{
2470 RAMBlock *block;
2471 ram_addr_t last = 0;
2472
2473 QLIST_FOREACH(block, &ram_list.blocks, next)
2474 last = MAX(last, block->offset + block->length);
2475
2476 return last;
2477}
2478
ddb97f1d
JB
2479static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
2480{
2481 int ret;
2482 QemuOpts *machine_opts;
2483
2484 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
2485 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
2486 if (machine_opts &&
2487 !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
2488 ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
2489 if (ret) {
2490 perror("qemu_madvise");
2491 fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
2492 "but dump_guest_core=off specified\n");
2493 }
2494 }
2495}
2496
c5705a77 2497void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
84b89d78
CM
2498{
2499 RAMBlock *new_block, *block;
2500
c5705a77
AK
2501 new_block = NULL;
2502 QLIST_FOREACH(block, &ram_list.blocks, next) {
2503 if (block->offset == addr) {
2504 new_block = block;
2505 break;
2506 }
2507 }
2508 assert(new_block);
2509 assert(!new_block->idstr[0]);
84b89d78 2510
09e5ab63
AL
2511 if (dev) {
2512 char *id = qdev_get_dev_path(dev);
84b89d78
CM
2513 if (id) {
2514 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
7267c094 2515 g_free(id);
84b89d78
CM
2516 }
2517 }
2518 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
2519
2520 QLIST_FOREACH(block, &ram_list.blocks, next) {
c5705a77 2521 if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
84b89d78
CM
2522 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
2523 new_block->idstr);
2524 abort();
2525 }
2526 }
c5705a77
AK
2527}
2528
8490fc78
LC
2529static int memory_try_enable_merging(void *addr, size_t len)
2530{
2531 QemuOpts *opts;
2532
2533 opts = qemu_opts_find(qemu_find_opts("machine"), 0);
2534 if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
2535 /* disabled by the user */
2536 return 0;
2537 }
2538
2539 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
2540}
2541
c5705a77
AK
2542ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
2543 MemoryRegion *mr)
2544{
2545 RAMBlock *new_block;
2546
2547 size = TARGET_PAGE_ALIGN(size);
2548 new_block = g_malloc0(sizeof(*new_block));
84b89d78 2549
7c637366 2550 new_block->mr = mr;
432d268c 2551 new_block->offset = find_ram_offset(size);
6977dfe6
YT
2552 if (host) {
2553 new_block->host = host;
cd19cfa2 2554 new_block->flags |= RAM_PREALLOC_MASK;
6977dfe6
YT
2555 } else {
2556 if (mem_path) {
c902760f 2557#if defined (__linux__) && !defined(TARGET_S390X)
6977dfe6
YT
2558 new_block->host = file_ram_alloc(new_block, size, mem_path);
2559 if (!new_block->host) {
2560 new_block->host = qemu_vmalloc(size);
8490fc78 2561 memory_try_enable_merging(new_block->host, size);
6977dfe6 2562 }
c902760f 2563#else
6977dfe6
YT
2564 fprintf(stderr, "-mem-path option unsupported\n");
2565 exit(1);
c902760f 2566#endif
6977dfe6 2567 } else {
868bb33f 2568 if (xen_enabled()) {
fce537d4 2569 xen_ram_alloc(new_block->offset, size, mr);
fdec9918
CB
2570 } else if (kvm_enabled()) {
2571 /* some s390/kvm configurations have special constraints */
2572 new_block->host = kvm_vmalloc(size);
432d268c
JN
2573 } else {
2574 new_block->host = qemu_vmalloc(size);
2575 }
8490fc78 2576 memory_try_enable_merging(new_block->host, size);
6977dfe6 2577 }
c902760f 2578 }
94a6b54f
PB
2579 new_block->length = size;
2580
f471a17e 2581 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
94a6b54f 2582
7267c094 2583 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
04b16653 2584 last_ram_offset() >> TARGET_PAGE_BITS);
5fda043f
IM
2585 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
2586 0, size >> TARGET_PAGE_BITS);
1720aeee 2587 cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
94a6b54f 2588
ddb97f1d
JB
2589 qemu_ram_setup_dump(new_block->host, size);
2590
6f0437e8
JK
2591 if (kvm_enabled())
2592 kvm_setup_guest_memory(new_block->host, size);
2593
94a6b54f
PB
2594 return new_block->offset;
2595}
e9a1ab19 2596
c5705a77 2597ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
6977dfe6 2598{
c5705a77 2599 return qemu_ram_alloc_from_ptr(size, NULL, mr);
6977dfe6
YT
2600}
2601
1f2e98b6
AW
2602void qemu_ram_free_from_ptr(ram_addr_t addr)
2603{
2604 RAMBlock *block;
2605
2606 QLIST_FOREACH(block, &ram_list.blocks, next) {
2607 if (addr == block->offset) {
2608 QLIST_REMOVE(block, next);
7267c094 2609 g_free(block);
1f2e98b6
AW
2610 return;
2611 }
2612 }
2613}
2614
c227f099 2615void qemu_ram_free(ram_addr_t addr)
e9a1ab19 2616{
04b16653
AW
2617 RAMBlock *block;
2618
2619 QLIST_FOREACH(block, &ram_list.blocks, next) {
2620 if (addr == block->offset) {
2621 QLIST_REMOVE(block, next);
cd19cfa2
HY
2622 if (block->flags & RAM_PREALLOC_MASK) {
2623 ;
2624 } else if (mem_path) {
04b16653
AW
2625#if defined (__linux__) && !defined(TARGET_S390X)
2626 if (block->fd) {
2627 munmap(block->host, block->length);
2628 close(block->fd);
2629 } else {
2630 qemu_vfree(block->host);
2631 }
fd28aa13
JK
2632#else
2633 abort();
04b16653
AW
2634#endif
2635 } else {
2636#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2637 munmap(block->host, block->length);
2638#else
868bb33f 2639 if (xen_enabled()) {
e41d7c69 2640 xen_invalidate_map_cache_entry(block->host);
432d268c
JN
2641 } else {
2642 qemu_vfree(block->host);
2643 }
04b16653
AW
2644#endif
2645 }
7267c094 2646 g_free(block);
04b16653
AW
2647 return;
2648 }
2649 }
2650
e9a1ab19
FB
2651}
2652
cd19cfa2
HY
2653#ifndef _WIN32
2654void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
2655{
2656 RAMBlock *block;
2657 ram_addr_t offset;
2658 int flags;
2659 void *area, *vaddr;
2660
2661 QLIST_FOREACH(block, &ram_list.blocks, next) {
2662 offset = addr - block->offset;
2663 if (offset < block->length) {
2664 vaddr = block->host + offset;
2665 if (block->flags & RAM_PREALLOC_MASK) {
2666 ;
2667 } else {
2668 flags = MAP_FIXED;
2669 munmap(vaddr, length);
2670 if (mem_path) {
2671#if defined(__linux__) && !defined(TARGET_S390X)
2672 if (block->fd) {
2673#ifdef MAP_POPULATE
2674 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
2675 MAP_PRIVATE;
2676#else
2677 flags |= MAP_PRIVATE;
2678#endif
2679 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
2680 flags, block->fd, offset);
2681 } else {
2682 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
2683 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
2684 flags, -1, 0);
2685 }
fd28aa13
JK
2686#else
2687 abort();
cd19cfa2
HY
2688#endif
2689 } else {
2690#if defined(TARGET_S390X) && defined(CONFIG_KVM)
2691 flags |= MAP_SHARED | MAP_ANONYMOUS;
2692 area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
2693 flags, -1, 0);
2694#else
2695 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
2696 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
2697 flags, -1, 0);
2698#endif
2699 }
2700 if (area != vaddr) {
f15fbc4b
AP
2701 fprintf(stderr, "Could not remap addr: "
2702 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
cd19cfa2
HY
2703 length, addr);
2704 exit(1);
2705 }
8490fc78 2706 memory_try_enable_merging(vaddr, length);
ddb97f1d 2707 qemu_ram_setup_dump(vaddr, length);
cd19cfa2
HY
2708 }
2709 return;
2710 }
2711 }
2712}
2713#endif /* !_WIN32 */
2714
dc828ca1 2715/* Return a host pointer to ram allocated with qemu_ram_alloc.
5579c7f3
PB
2716 With the exception of the softmmu code in this file, this should
2717 only be used for local memory (e.g. video ram) that the device owns,
2718 and knows it isn't going to access beyond the end of the block.
2719
2720 It should not be used for general purpose DMA.
2721 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2722 */
c227f099 2723void *qemu_get_ram_ptr(ram_addr_t addr)
dc828ca1 2724{
94a6b54f
PB
2725 RAMBlock *block;
2726
f471a17e
AW
2727 QLIST_FOREACH(block, &ram_list.blocks, next) {
2728 if (addr - block->offset < block->length) {
7d82af38
VP
2729 /* Move this entry to to start of the list. */
2730 if (block != QLIST_FIRST(&ram_list.blocks)) {
2731 QLIST_REMOVE(block, next);
2732 QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
2733 }
868bb33f 2734 if (xen_enabled()) {
432d268c
JN
2735 /* We need to check if the requested address is in the RAM
2736 * because we don't want to map the entire memory in QEMU.
712c2b41 2737 * In that case just map until the end of the page.
432d268c
JN
2738 */
2739 if (block->offset == 0) {
e41d7c69 2740 return xen_map_cache(addr, 0, 0);
432d268c 2741 } else if (block->host == NULL) {
e41d7c69
JK
2742 block->host =
2743 xen_map_cache(block->offset, block->length, 1);
432d268c
JN
2744 }
2745 }
f471a17e
AW
2746 return block->host + (addr - block->offset);
2747 }
94a6b54f 2748 }
f471a17e
AW
2749
2750 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2751 abort();
2752
2753 return NULL;
dc828ca1
PB
2754}
2755
b2e0a138
MT
2756/* Return a host pointer to ram allocated with qemu_ram_alloc.
2757 * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
2758 */
2759void *qemu_safe_ram_ptr(ram_addr_t addr)
2760{
2761 RAMBlock *block;
2762
2763 QLIST_FOREACH(block, &ram_list.blocks, next) {
2764 if (addr - block->offset < block->length) {
868bb33f 2765 if (xen_enabled()) {
432d268c
JN
2766 /* We need to check if the requested address is in the RAM
2767 * because we don't want to map the entire memory in QEMU.
712c2b41 2768 * In that case just map until the end of the page.
432d268c
JN
2769 */
2770 if (block->offset == 0) {
e41d7c69 2771 return xen_map_cache(addr, 0, 0);
432d268c 2772 } else if (block->host == NULL) {
e41d7c69
JK
2773 block->host =
2774 xen_map_cache(block->offset, block->length, 1);
432d268c
JN
2775 }
2776 }
b2e0a138
MT
2777 return block->host + (addr - block->offset);
2778 }
2779 }
2780
2781 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2782 abort();
2783
2784 return NULL;
2785}
2786
38bee5dc
SS
2787/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
2788 * but takes a size argument */
8ab934f9 2789void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
38bee5dc 2790{
8ab934f9
SS
2791 if (*size == 0) {
2792 return NULL;
2793 }
868bb33f 2794 if (xen_enabled()) {
e41d7c69 2795 return xen_map_cache(addr, *size, 1);
868bb33f 2796 } else {
38bee5dc
SS
2797 RAMBlock *block;
2798
2799 QLIST_FOREACH(block, &ram_list.blocks, next) {
2800 if (addr - block->offset < block->length) {
2801 if (addr - block->offset + *size > block->length)
2802 *size = block->length - addr + block->offset;
2803 return block->host + (addr - block->offset);
2804 }
2805 }
2806
2807 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2808 abort();
38bee5dc
SS
2809 }
2810}
2811
050a0ddf
AP
2812void qemu_put_ram_ptr(void *addr)
2813{
2814 trace_qemu_put_ram_ptr(addr);
050a0ddf
AP
2815}
2816
e890261f 2817int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
5579c7f3 2818{
94a6b54f
PB
2819 RAMBlock *block;
2820 uint8_t *host = ptr;
2821
868bb33f 2822 if (xen_enabled()) {
e41d7c69 2823 *ram_addr = xen_ram_addr_from_mapcache(ptr);
712c2b41
SS
2824 return 0;
2825 }
2826
f471a17e 2827 QLIST_FOREACH(block, &ram_list.blocks, next) {
432d268c
JN
2828 /* This case append when the block is not mapped. */
2829 if (block->host == NULL) {
2830 continue;
2831 }
f471a17e 2832 if (host - block->host < block->length) {
e890261f
MT
2833 *ram_addr = block->offset + (host - block->host);
2834 return 0;
f471a17e 2835 }
94a6b54f 2836 }
432d268c 2837
e890261f
MT
2838 return -1;
2839}
f471a17e 2840
e890261f
MT
2841/* Some of the softmmu routines need to translate from a host pointer
2842 (typically a TLB entry) back to a ram offset. */
2843ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
2844{
2845 ram_addr_t ram_addr;
f471a17e 2846
e890261f
MT
2847 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
2848 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2849 abort();
2850 }
2851 return ram_addr;
5579c7f3
PB
2852}
2853
0e0df1e2
AK
2854static uint64_t unassigned_mem_read(void *opaque, target_phys_addr_t addr,
2855 unsigned size)
e18231a3
BS
2856{
2857#ifdef DEBUG_UNASSIGNED
2858 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2859#endif
5b450407 2860#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
0e0df1e2 2861 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
e18231a3
BS
2862#endif
2863 return 0;
2864}
2865
0e0df1e2
AK
2866static void unassigned_mem_write(void *opaque, target_phys_addr_t addr,
2867 uint64_t val, unsigned size)
e18231a3
BS
2868{
2869#ifdef DEBUG_UNASSIGNED
0e0df1e2 2870 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
e18231a3 2871#endif
5b450407 2872#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
0e0df1e2 2873 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
67d3b957 2874#endif
33417e70
FB
2875}
2876
0e0df1e2
AK
2877static const MemoryRegionOps unassigned_mem_ops = {
2878 .read = unassigned_mem_read,
2879 .write = unassigned_mem_write,
2880 .endianness = DEVICE_NATIVE_ENDIAN,
2881};
e18231a3 2882
0e0df1e2
AK
2883static uint64_t error_mem_read(void *opaque, target_phys_addr_t addr,
2884 unsigned size)
e18231a3 2885{
0e0df1e2 2886 abort();
e18231a3
BS
2887}
2888
0e0df1e2
AK
2889static void error_mem_write(void *opaque, target_phys_addr_t addr,
2890 uint64_t value, unsigned size)
e18231a3 2891{
0e0df1e2 2892 abort();
33417e70
FB
2893}
2894
0e0df1e2
AK
2895static const MemoryRegionOps error_mem_ops = {
2896 .read = error_mem_read,
2897 .write = error_mem_write,
2898 .endianness = DEVICE_NATIVE_ENDIAN,
33417e70
FB
2899};
2900
0e0df1e2
AK
2901static const MemoryRegionOps rom_mem_ops = {
2902 .read = error_mem_read,
2903 .write = unassigned_mem_write,
2904 .endianness = DEVICE_NATIVE_ENDIAN,
33417e70
FB
2905};
2906
0e0df1e2
AK
2907static void notdirty_mem_write(void *opaque, target_phys_addr_t ram_addr,
2908 uint64_t val, unsigned size)
9fa3e853 2909{
3a7d929e 2910 int dirty_flags;
f7c11b53 2911 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
3a7d929e 2912 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
9fa3e853 2913#if !defined(CONFIG_USER_ONLY)
0e0df1e2 2914 tb_invalidate_phys_page_fast(ram_addr, size);
f7c11b53 2915 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
9fa3e853 2916#endif
3a7d929e 2917 }
0e0df1e2
AK
2918 switch (size) {
2919 case 1:
2920 stb_p(qemu_get_ram_ptr(ram_addr), val);
2921 break;
2922 case 2:
2923 stw_p(qemu_get_ram_ptr(ram_addr), val);
2924 break;
2925 case 4:
2926 stl_p(qemu_get_ram_ptr(ram_addr), val);
2927 break;
2928 default:
2929 abort();
3a7d929e 2930 }
f23db169 2931 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
f7c11b53 2932 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
f23db169
FB
2933 /* we remove the notdirty callback only if the code has been
2934 flushed */
2935 if (dirty_flags == 0xff)
2e70f6ef 2936 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
9fa3e853
FB
2937}
2938
0e0df1e2
AK
2939static const MemoryRegionOps notdirty_mem_ops = {
2940 .read = error_mem_read,
2941 .write = notdirty_mem_write,
2942 .endianness = DEVICE_NATIVE_ENDIAN,
1ccde1cb
FB
2943};
2944
0f459d16 2945/* Generate a debug exception if a watchpoint has been hit. */
b4051334 2946static void check_watchpoint(int offset, int len_mask, int flags)
0f459d16 2947{
9349b4f9 2948 CPUArchState *env = cpu_single_env;
06d55cc1
AL
2949 target_ulong pc, cs_base;
2950 TranslationBlock *tb;
0f459d16 2951 target_ulong vaddr;
a1d1bb31 2952 CPUWatchpoint *wp;
06d55cc1 2953 int cpu_flags;
0f459d16 2954
06d55cc1
AL
2955 if (env->watchpoint_hit) {
2956 /* We re-entered the check after replacing the TB. Now raise
2957 * the debug interrupt so that is will trigger after the
2958 * current instruction. */
2959 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2960 return;
2961 }
2e70f6ef 2962 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
72cf2d4f 2963 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
b4051334
AL
2964 if ((vaddr == (wp->vaddr & len_mask) ||
2965 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
6e140f28
AL
2966 wp->flags |= BP_WATCHPOINT_HIT;
2967 if (!env->watchpoint_hit) {
2968 env->watchpoint_hit = wp;
2969 tb = tb_find_pc(env->mem_io_pc);
2970 if (!tb) {
2971 cpu_abort(env, "check_watchpoint: could not find TB for "
2972 "pc=%p", (void *)env->mem_io_pc);
2973 }
618ba8e6 2974 cpu_restore_state(tb, env, env->mem_io_pc);
6e140f28
AL
2975 tb_phys_invalidate(tb, -1);
2976 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2977 env->exception_index = EXCP_DEBUG;
488d6577 2978 cpu_loop_exit(env);
6e140f28
AL
2979 } else {
2980 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2981 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
488d6577 2982 cpu_resume_from_signal(env, NULL);
6e140f28 2983 }
06d55cc1 2984 }
6e140f28
AL
2985 } else {
2986 wp->flags &= ~BP_WATCHPOINT_HIT;
0f459d16
PB
2987 }
2988 }
2989}
2990
6658ffb8
PB
2991/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2992 so these check for a hit then pass through to the normal out-of-line
2993 phys routines. */
1ec9b909
AK
2994static uint64_t watch_mem_read(void *opaque, target_phys_addr_t addr,
2995 unsigned size)
6658ffb8 2996{
1ec9b909
AK
2997 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
2998 switch (size) {
2999 case 1: return ldub_phys(addr);
3000 case 2: return lduw_phys(addr);
3001 case 4: return ldl_phys(addr);
3002 default: abort();
3003 }
6658ffb8
PB
3004}
3005
1ec9b909
AK
3006static void watch_mem_write(void *opaque, target_phys_addr_t addr,
3007 uint64_t val, unsigned size)
6658ffb8 3008{
1ec9b909
AK
3009 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
3010 switch (size) {
67364150
MF
3011 case 1:
3012 stb_phys(addr, val);
3013 break;
3014 case 2:
3015 stw_phys(addr, val);
3016 break;
3017 case 4:
3018 stl_phys(addr, val);
3019 break;
1ec9b909
AK
3020 default: abort();
3021 }
6658ffb8
PB
3022}
3023
1ec9b909
AK
3024static const MemoryRegionOps watch_mem_ops = {
3025 .read = watch_mem_read,
3026 .write = watch_mem_write,
3027 .endianness = DEVICE_NATIVE_ENDIAN,
6658ffb8 3028};
6658ffb8 3029
70c68e44
AK
3030static uint64_t subpage_read(void *opaque, target_phys_addr_t addr,
3031 unsigned len)
db7b5426 3032{
70c68e44 3033 subpage_t *mmio = opaque;
f6405247 3034 unsigned int idx = SUBPAGE_IDX(addr);
5312bd8b 3035 MemoryRegionSection *section;
db7b5426
BS
3036#if defined(DEBUG_SUBPAGE)
3037 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
3038 mmio, len, addr, idx);
3039#endif
db7b5426 3040
5312bd8b
AK
3041 section = &phys_sections[mmio->sub_section[idx]];
3042 addr += mmio->base;
3043 addr -= section->offset_within_address_space;
3044 addr += section->offset_within_region;
37ec01d4 3045 return io_mem_read(section->mr, addr, len);
db7b5426
BS
3046}
3047
70c68e44
AK
3048static void subpage_write(void *opaque, target_phys_addr_t addr,
3049 uint64_t value, unsigned len)
db7b5426 3050{
70c68e44 3051 subpage_t *mmio = opaque;
f6405247 3052 unsigned int idx = SUBPAGE_IDX(addr);
5312bd8b 3053 MemoryRegionSection *section;
db7b5426 3054#if defined(DEBUG_SUBPAGE)
70c68e44
AK
3055 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
3056 " idx %d value %"PRIx64"\n",
f6405247 3057 __func__, mmio, len, addr, idx, value);
db7b5426 3058#endif
f6405247 3059
5312bd8b
AK
3060 section = &phys_sections[mmio->sub_section[idx]];
3061 addr += mmio->base;
3062 addr -= section->offset_within_address_space;
3063 addr += section->offset_within_region;
37ec01d4 3064 io_mem_write(section->mr, addr, value, len);
db7b5426
BS
3065}
3066
70c68e44
AK
3067static const MemoryRegionOps subpage_ops = {
3068 .read = subpage_read,
3069 .write = subpage_write,
3070 .endianness = DEVICE_NATIVE_ENDIAN,
db7b5426
BS
3071};
3072
de712f94
AK
3073static uint64_t subpage_ram_read(void *opaque, target_phys_addr_t addr,
3074 unsigned size)
56384e8b
AF
3075{
3076 ram_addr_t raddr = addr;
3077 void *ptr = qemu_get_ram_ptr(raddr);
de712f94
AK
3078 switch (size) {
3079 case 1: return ldub_p(ptr);
3080 case 2: return lduw_p(ptr);
3081 case 4: return ldl_p(ptr);
3082 default: abort();
3083 }
56384e8b
AF
3084}
3085
de712f94
AK
3086static void subpage_ram_write(void *opaque, target_phys_addr_t addr,
3087 uint64_t value, unsigned size)
56384e8b
AF
3088{
3089 ram_addr_t raddr = addr;
3090 void *ptr = qemu_get_ram_ptr(raddr);
de712f94
AK
3091 switch (size) {
3092 case 1: return stb_p(ptr, value);
3093 case 2: return stw_p(ptr, value);
3094 case 4: return stl_p(ptr, value);
3095 default: abort();
3096 }
56384e8b
AF
3097}
3098
de712f94
AK
3099static const MemoryRegionOps subpage_ram_ops = {
3100 .read = subpage_ram_read,
3101 .write = subpage_ram_write,
3102 .endianness = DEVICE_NATIVE_ENDIAN,
56384e8b
AF
3103};
3104
c227f099 3105static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
5312bd8b 3106 uint16_t section)
db7b5426
BS
3107{
3108 int idx, eidx;
3109
3110 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3111 return -1;
3112 idx = SUBPAGE_IDX(start);
3113 eidx = SUBPAGE_IDX(end);
3114#if defined(DEBUG_SUBPAGE)
0bf9e31a 3115 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
db7b5426
BS
3116 mmio, start, end, idx, eidx, memory);
3117#endif
5312bd8b
AK
3118 if (memory_region_is_ram(phys_sections[section].mr)) {
3119 MemoryRegionSection new_section = phys_sections[section];
3120 new_section.mr = &io_mem_subpage_ram;
3121 section = phys_section_add(&new_section);
56384e8b 3122 }
db7b5426 3123 for (; idx <= eidx; idx++) {
5312bd8b 3124 mmio->sub_section[idx] = section;
db7b5426
BS
3125 }
3126
3127 return 0;
3128}
3129
0f0cb164 3130static subpage_t *subpage_init(target_phys_addr_t base)
db7b5426 3131{
c227f099 3132 subpage_t *mmio;
db7b5426 3133
7267c094 3134 mmio = g_malloc0(sizeof(subpage_t));
1eec614b
AL
3135
3136 mmio->base = base;
70c68e44
AK
3137 memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
3138 "subpage", TARGET_PAGE_SIZE);
b3b00c78 3139 mmio->iomem.subpage = true;
db7b5426 3140#if defined(DEBUG_SUBPAGE)
1eec614b
AL
3141 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3142 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
db7b5426 3143#endif
0f0cb164 3144 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
db7b5426
BS
3145
3146 return mmio;
3147}
3148
5312bd8b
AK
3149static uint16_t dummy_section(MemoryRegion *mr)
3150{
3151 MemoryRegionSection section = {
3152 .mr = mr,
3153 .offset_within_address_space = 0,
3154 .offset_within_region = 0,
3155 .size = UINT64_MAX,
3156 };
3157
3158 return phys_section_add(&section);
3159}
3160
37ec01d4 3161MemoryRegion *iotlb_to_region(target_phys_addr_t index)
aa102231 3162{
37ec01d4 3163 return phys_sections[index & ~TARGET_PAGE_MASK].mr;
aa102231
AK
3164}
3165
e9179ce1
AK
3166static void io_mem_init(void)
3167{
0e0df1e2 3168 memory_region_init_io(&io_mem_ram, &error_mem_ops, NULL, "ram", UINT64_MAX);
0e0df1e2
AK
3169 memory_region_init_io(&io_mem_rom, &rom_mem_ops, NULL, "rom", UINT64_MAX);
3170 memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
3171 "unassigned", UINT64_MAX);
3172 memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
3173 "notdirty", UINT64_MAX);
de712f94
AK
3174 memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
3175 "subpage-ram", UINT64_MAX);
1ec9b909
AK
3176 memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
3177 "watch", UINT64_MAX);
e9179ce1
AK
3178}
3179
50c1e149
AK
3180static void core_begin(MemoryListener *listener)
3181{
54688b1e 3182 destroy_all_mappings();
5312bd8b 3183 phys_sections_clear();
c19e8800 3184 phys_map.ptr = PHYS_MAP_NODE_NIL;
5312bd8b 3185 phys_section_unassigned = dummy_section(&io_mem_unassigned);
aa102231
AK
3186 phys_section_notdirty = dummy_section(&io_mem_notdirty);
3187 phys_section_rom = dummy_section(&io_mem_rom);
3188 phys_section_watch = dummy_section(&io_mem_watch);
50c1e149
AK
3189}
3190
3191static void core_commit(MemoryListener *listener)
3192{
9349b4f9 3193 CPUArchState *env;
117712c3
AK
3194
3195 /* since each CPU stores ram addresses in its TLB cache, we must
3196 reset the modified entries */
3197 /* XXX: slow ! */
3198 for(env = first_cpu; env != NULL; env = env->next_cpu) {
3199 tlb_flush(env, 1);
3200 }
50c1e149
AK
3201}
3202
93632747
AK
3203static void core_region_add(MemoryListener *listener,
3204 MemoryRegionSection *section)
3205{
4855d41a 3206 cpu_register_physical_memory_log(section, section->readonly);
93632747
AK
3207}
3208
3209static void core_region_del(MemoryListener *listener,
3210 MemoryRegionSection *section)
3211{
93632747
AK
3212}
3213
50c1e149
AK
3214static void core_region_nop(MemoryListener *listener,
3215 MemoryRegionSection *section)
3216{
54688b1e 3217 cpu_register_physical_memory_log(section, section->readonly);
50c1e149
AK
3218}
3219
93632747
AK
3220static void core_log_start(MemoryListener *listener,
3221 MemoryRegionSection *section)
3222{
3223}
3224
3225static void core_log_stop(MemoryListener *listener,
3226 MemoryRegionSection *section)
3227{
3228}
3229
3230static void core_log_sync(MemoryListener *listener,
3231 MemoryRegionSection *section)
3232{
3233}
3234
3235static void core_log_global_start(MemoryListener *listener)
3236{
3237 cpu_physical_memory_set_dirty_tracking(1);
3238}
3239
3240static void core_log_global_stop(MemoryListener *listener)
3241{
3242 cpu_physical_memory_set_dirty_tracking(0);
3243}
3244
3245static void core_eventfd_add(MemoryListener *listener,
3246 MemoryRegionSection *section,
753d5e14 3247 bool match_data, uint64_t data, EventNotifier *e)
93632747
AK
3248{
3249}
3250
3251static void core_eventfd_del(MemoryListener *listener,
3252 MemoryRegionSection *section,
753d5e14 3253 bool match_data, uint64_t data, EventNotifier *e)
93632747
AK
3254{
3255}
3256
50c1e149
AK
3257static void io_begin(MemoryListener *listener)
3258{
3259}
3260
3261static void io_commit(MemoryListener *listener)
3262{
3263}
3264
4855d41a
AK
3265static void io_region_add(MemoryListener *listener,
3266 MemoryRegionSection *section)
3267{
a2d33521
AK
3268 MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);
3269
3270 mrio->mr = section->mr;
3271 mrio->offset = section->offset_within_region;
3272 iorange_init(&mrio->iorange, &memory_region_iorange_ops,
4855d41a 3273 section->offset_within_address_space, section->size);
a2d33521 3274 ioport_register(&mrio->iorange);
4855d41a
AK
3275}
3276
3277static void io_region_del(MemoryListener *listener,
3278 MemoryRegionSection *section)
3279{
3280 isa_unassign_ioport(section->offset_within_address_space, section->size);
3281}
3282
50c1e149
AK
3283static void io_region_nop(MemoryListener *listener,
3284 MemoryRegionSection *section)
3285{
3286}
3287
4855d41a
AK
3288static void io_log_start(MemoryListener *listener,
3289 MemoryRegionSection *section)
3290{
3291}
3292
3293static void io_log_stop(MemoryListener *listener,
3294 MemoryRegionSection *section)
3295{
3296}
3297
3298static void io_log_sync(MemoryListener *listener,
3299 MemoryRegionSection *section)
3300{
3301}
3302
3303static void io_log_global_start(MemoryListener *listener)
3304{
3305}
3306
3307static void io_log_global_stop(MemoryListener *listener)
3308{
3309}
3310
3311static void io_eventfd_add(MemoryListener *listener,
3312 MemoryRegionSection *section,
753d5e14 3313 bool match_data, uint64_t data, EventNotifier *e)
4855d41a
AK
3314{
3315}
3316
3317static void io_eventfd_del(MemoryListener *listener,
3318 MemoryRegionSection *section,
753d5e14 3319 bool match_data, uint64_t data, EventNotifier *e)
4855d41a
AK
3320{
3321}
3322
93632747 3323static MemoryListener core_memory_listener = {
50c1e149
AK
3324 .begin = core_begin,
3325 .commit = core_commit,
93632747
AK
3326 .region_add = core_region_add,
3327 .region_del = core_region_del,
50c1e149 3328 .region_nop = core_region_nop,
93632747
AK
3329 .log_start = core_log_start,
3330 .log_stop = core_log_stop,
3331 .log_sync = core_log_sync,
3332 .log_global_start = core_log_global_start,
3333 .log_global_stop = core_log_global_stop,
3334 .eventfd_add = core_eventfd_add,
3335 .eventfd_del = core_eventfd_del,
3336 .priority = 0,
3337};
3338
4855d41a 3339static MemoryListener io_memory_listener = {
50c1e149
AK
3340 .begin = io_begin,
3341 .commit = io_commit,
4855d41a
AK
3342 .region_add = io_region_add,
3343 .region_del = io_region_del,
50c1e149 3344 .region_nop = io_region_nop,
4855d41a
AK
3345 .log_start = io_log_start,
3346 .log_stop = io_log_stop,
3347 .log_sync = io_log_sync,
3348 .log_global_start = io_log_global_start,
3349 .log_global_stop = io_log_global_stop,
3350 .eventfd_add = io_eventfd_add,
3351 .eventfd_del = io_eventfd_del,
3352 .priority = 0,
3353};
3354
62152b8a
AK
3355static void memory_map_init(void)
3356{
7267c094 3357 system_memory = g_malloc(sizeof(*system_memory));
8417cebf 3358 memory_region_init(system_memory, "system", INT64_MAX);
62152b8a 3359 set_system_memory_map(system_memory);
309cb471 3360
7267c094 3361 system_io = g_malloc(sizeof(*system_io));
309cb471
AK
3362 memory_region_init(system_io, "io", 65536);
3363 set_system_io_map(system_io);
93632747 3364
4855d41a
AK
3365 memory_listener_register(&core_memory_listener, system_memory);
3366 memory_listener_register(&io_memory_listener, system_io);
62152b8a
AK
3367}
3368
3369MemoryRegion *get_system_memory(void)
3370{
3371 return system_memory;
3372}
3373
309cb471
AK
3374MemoryRegion *get_system_io(void)
3375{
3376 return system_io;
3377}
3378
e2eef170
PB
3379#endif /* !defined(CONFIG_USER_ONLY) */
3380
13eb76e0
FB
3381/* physical memory access (slow version, mainly for debug) */
3382#if defined(CONFIG_USER_ONLY)
9349b4f9 3383int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
a68fe89c 3384 uint8_t *buf, int len, int is_write)
13eb76e0
FB
3385{
3386 int l, flags;
3387 target_ulong page;
53a5960a 3388 void * p;
13eb76e0
FB
3389
3390 while (len > 0) {
3391 page = addr & TARGET_PAGE_MASK;
3392 l = (page + TARGET_PAGE_SIZE) - addr;
3393 if (l > len)
3394 l = len;
3395 flags = page_get_flags(page);
3396 if (!(flags & PAGE_VALID))
a68fe89c 3397 return -1;
13eb76e0
FB
3398 if (is_write) {
3399 if (!(flags & PAGE_WRITE))
a68fe89c 3400 return -1;
579a97f7 3401 /* XXX: this code should not depend on lock_user */
72fb7daa 3402 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
a68fe89c 3403 return -1;
72fb7daa
AJ
3404 memcpy(p, buf, l);
3405 unlock_user(p, addr, l);
13eb76e0
FB
3406 } else {
3407 if (!(flags & PAGE_READ))
a68fe89c 3408 return -1;
579a97f7 3409 /* XXX: this code should not depend on lock_user */
72fb7daa 3410 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
a68fe89c 3411 return -1;
72fb7daa 3412 memcpy(buf, p, l);
5b257578 3413 unlock_user(p, addr, 0);
13eb76e0
FB
3414 }
3415 len -= l;
3416 buf += l;
3417 addr += l;
3418 }
a68fe89c 3419 return 0;
13eb76e0 3420}
8df1cd07 3421
13eb76e0 3422#else
51d7a9eb
AP
3423
3424static void invalidate_and_set_dirty(target_phys_addr_t addr,
3425 target_phys_addr_t length)
3426{
3427 if (!cpu_physical_memory_is_dirty(addr)) {
3428 /* invalidate code */
3429 tb_invalidate_phys_page_range(addr, addr + length, 0);
3430 /* set dirty bit */
3431 cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
3432 }
e226939d 3433 xen_modified_memory(addr, length);
51d7a9eb
AP
3434}
3435
c227f099 3436void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
13eb76e0
FB
3437 int len, int is_write)
3438{
37ec01d4 3439 int l;
13eb76e0
FB
3440 uint8_t *ptr;
3441 uint32_t val;
c227f099 3442 target_phys_addr_t page;
f3705d53 3443 MemoryRegionSection *section;
3b46e624 3444
13eb76e0
FB
3445 while (len > 0) {
3446 page = addr & TARGET_PAGE_MASK;
3447 l = (page + TARGET_PAGE_SIZE) - addr;
3448 if (l > len)
3449 l = len;
06ef3525 3450 section = phys_page_find(page >> TARGET_PAGE_BITS);
3b46e624 3451
13eb76e0 3452 if (is_write) {
f3705d53 3453 if (!memory_region_is_ram(section->mr)) {
f1f6e3b8 3454 target_phys_addr_t addr1;
cc5bea60 3455 addr1 = memory_region_section_addr(section, addr);
6a00d601
FB
3456 /* XXX: could force cpu_single_env to NULL to avoid
3457 potential bugs */
6c2934db 3458 if (l >= 4 && ((addr1 & 3) == 0)) {
1c213d19 3459 /* 32 bit write access */
c27004ec 3460 val = ldl_p(buf);
37ec01d4 3461 io_mem_write(section->mr, addr1, val, 4);
13eb76e0 3462 l = 4;
6c2934db 3463 } else if (l >= 2 && ((addr1 & 1) == 0)) {
1c213d19 3464 /* 16 bit write access */
c27004ec 3465 val = lduw_p(buf);
37ec01d4 3466 io_mem_write(section->mr, addr1, val, 2);
13eb76e0
FB
3467 l = 2;
3468 } else {
1c213d19 3469 /* 8 bit write access */
c27004ec 3470 val = ldub_p(buf);
37ec01d4 3471 io_mem_write(section->mr, addr1, val, 1);
13eb76e0
FB
3472 l = 1;
3473 }
f3705d53 3474 } else if (!section->readonly) {
8ca5692d 3475 ram_addr_t addr1;
f3705d53 3476 addr1 = memory_region_get_ram_addr(section->mr)
cc5bea60 3477 + memory_region_section_addr(section, addr);
13eb76e0 3478 /* RAM case */
5579c7f3 3479 ptr = qemu_get_ram_ptr(addr1);
13eb76e0 3480 memcpy(ptr, buf, l);
51d7a9eb 3481 invalidate_and_set_dirty(addr1, l);
050a0ddf 3482 qemu_put_ram_ptr(ptr);
13eb76e0
FB
3483 }
3484 } else {
cc5bea60
BS
3485 if (!(memory_region_is_ram(section->mr) ||
3486 memory_region_is_romd(section->mr))) {
f1f6e3b8 3487 target_phys_addr_t addr1;
13eb76e0 3488 /* I/O case */
cc5bea60 3489 addr1 = memory_region_section_addr(section, addr);
6c2934db 3490 if (l >= 4 && ((addr1 & 3) == 0)) {
13eb76e0 3491 /* 32 bit read access */
37ec01d4 3492 val = io_mem_read(section->mr, addr1, 4);
c27004ec 3493 stl_p(buf, val);
13eb76e0 3494 l = 4;
6c2934db 3495 } else if (l >= 2 && ((addr1 & 1) == 0)) {
13eb76e0 3496 /* 16 bit read access */
37ec01d4 3497 val = io_mem_read(section->mr, addr1, 2);
c27004ec 3498 stw_p(buf, val);
13eb76e0
FB
3499 l = 2;
3500 } else {
1c213d19 3501 /* 8 bit read access */
37ec01d4 3502 val = io_mem_read(section->mr, addr1, 1);
c27004ec 3503 stb_p(buf, val);
13eb76e0
FB
3504 l = 1;
3505 }
3506 } else {
3507 /* RAM case */
0a1b357f 3508 ptr = qemu_get_ram_ptr(section->mr->ram_addr
cc5bea60
BS
3509 + memory_region_section_addr(section,
3510 addr));
f3705d53 3511 memcpy(buf, ptr, l);
050a0ddf 3512 qemu_put_ram_ptr(ptr);
13eb76e0
FB
3513 }
3514 }
3515 len -= l;
3516 buf += l;
3517 addr += l;
3518 }
3519}
8df1cd07 3520
d0ecd2aa 3521/* used for ROM loading : can write in RAM and ROM */
c227f099 3522void cpu_physical_memory_write_rom(target_phys_addr_t addr,
d0ecd2aa
FB
3523 const uint8_t *buf, int len)
3524{
3525 int l;
3526 uint8_t *ptr;
c227f099 3527 target_phys_addr_t page;
f3705d53 3528 MemoryRegionSection *section;
3b46e624 3529
d0ecd2aa
FB
3530 while (len > 0) {
3531 page = addr & TARGET_PAGE_MASK;
3532 l = (page + TARGET_PAGE_SIZE) - addr;
3533 if (l > len)
3534 l = len;
06ef3525 3535 section = phys_page_find(page >> TARGET_PAGE_BITS);
3b46e624 3536
cc5bea60
BS
3537 if (!(memory_region_is_ram(section->mr) ||
3538 memory_region_is_romd(section->mr))) {
d0ecd2aa
FB
3539 /* do nothing */
3540 } else {
3541 unsigned long addr1;
f3705d53 3542 addr1 = memory_region_get_ram_addr(section->mr)
cc5bea60 3543 + memory_region_section_addr(section, addr);
d0ecd2aa 3544 /* ROM/RAM case */
5579c7f3 3545 ptr = qemu_get_ram_ptr(addr1);
d0ecd2aa 3546 memcpy(ptr, buf, l);
51d7a9eb 3547 invalidate_and_set_dirty(addr1, l);
050a0ddf 3548 qemu_put_ram_ptr(ptr);
d0ecd2aa
FB
3549 }
3550 len -= l;
3551 buf += l;
3552 addr += l;
3553 }
3554}
3555
6d16c2f8
AL
3556typedef struct {
3557 void *buffer;
c227f099
AL
3558 target_phys_addr_t addr;
3559 target_phys_addr_t len;
6d16c2f8
AL
3560} BounceBuffer;
3561
3562static BounceBuffer bounce;
3563
ba223c29
AL
3564typedef struct MapClient {
3565 void *opaque;
3566 void (*callback)(void *opaque);
72cf2d4f 3567 QLIST_ENTRY(MapClient) link;
ba223c29
AL
3568} MapClient;
3569
72cf2d4f
BS
3570static QLIST_HEAD(map_client_list, MapClient) map_client_list
3571 = QLIST_HEAD_INITIALIZER(map_client_list);
ba223c29
AL
3572
3573void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3574{
7267c094 3575 MapClient *client = g_malloc(sizeof(*client));
ba223c29
AL
3576
3577 client->opaque = opaque;
3578 client->callback = callback;
72cf2d4f 3579 QLIST_INSERT_HEAD(&map_client_list, client, link);
ba223c29
AL
3580 return client;
3581}
3582
3583void cpu_unregister_map_client(void *_client)
3584{
3585 MapClient *client = (MapClient *)_client;
3586
72cf2d4f 3587 QLIST_REMOVE(client, link);
7267c094 3588 g_free(client);
ba223c29
AL
3589}
3590
3591static void cpu_notify_map_clients(void)
3592{
3593 MapClient *client;
3594
72cf2d4f
BS
3595 while (!QLIST_EMPTY(&map_client_list)) {
3596 client = QLIST_FIRST(&map_client_list);
ba223c29 3597 client->callback(client->opaque);
34d5e948 3598 cpu_unregister_map_client(client);
ba223c29
AL
3599 }
3600}
3601
6d16c2f8
AL
3602/* Map a physical memory region into a host virtual address.
3603 * May map a subset of the requested range, given by and returned in *plen.
3604 * May return NULL if resources needed to perform the mapping are exhausted.
3605 * Use only for reads OR writes - not for read-modify-write operations.
ba223c29
AL
3606 * Use cpu_register_map_client() to know when retrying the map operation is
3607 * likely to succeed.
6d16c2f8 3608 */
c227f099
AL
3609void *cpu_physical_memory_map(target_phys_addr_t addr,
3610 target_phys_addr_t *plen,
6d16c2f8
AL
3611 int is_write)
3612{
c227f099 3613 target_phys_addr_t len = *plen;
38bee5dc 3614 target_phys_addr_t todo = 0;
6d16c2f8 3615 int l;
c227f099 3616 target_phys_addr_t page;
f3705d53 3617 MemoryRegionSection *section;
f15fbc4b 3618 ram_addr_t raddr = RAM_ADDR_MAX;
8ab934f9
SS
3619 ram_addr_t rlen;
3620 void *ret;
6d16c2f8
AL
3621
3622 while (len > 0) {
3623 page = addr & TARGET_PAGE_MASK;
3624 l = (page + TARGET_PAGE_SIZE) - addr;
3625 if (l > len)
3626 l = len;
06ef3525 3627 section = phys_page_find(page >> TARGET_PAGE_BITS);
6d16c2f8 3628
f3705d53 3629 if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
38bee5dc 3630 if (todo || bounce.buffer) {
6d16c2f8
AL
3631 break;
3632 }
3633 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3634 bounce.addr = addr;
3635 bounce.len = l;
3636 if (!is_write) {
54f7b4a3 3637 cpu_physical_memory_read(addr, bounce.buffer, l);
6d16c2f8 3638 }
38bee5dc
SS
3639
3640 *plen = l;
3641 return bounce.buffer;
6d16c2f8 3642 }
8ab934f9 3643 if (!todo) {
f3705d53 3644 raddr = memory_region_get_ram_addr(section->mr)
cc5bea60 3645 + memory_region_section_addr(section, addr);
8ab934f9 3646 }
6d16c2f8
AL
3647
3648 len -= l;
3649 addr += l;
38bee5dc 3650 todo += l;
6d16c2f8 3651 }
8ab934f9
SS
3652 rlen = todo;
3653 ret = qemu_ram_ptr_length(raddr, &rlen);
3654 *plen = rlen;
3655 return ret;
6d16c2f8
AL
3656}
3657
3658/* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3659 * Will also mark the memory as dirty if is_write == 1. access_len gives
3660 * the amount of memory that was actually read or written by the caller.
3661 */
c227f099
AL
3662void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3663 int is_write, target_phys_addr_t access_len)
6d16c2f8
AL
3664{
3665 if (buffer != bounce.buffer) {
3666 if (is_write) {
e890261f 3667 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
6d16c2f8
AL
3668 while (access_len) {
3669 unsigned l;
3670 l = TARGET_PAGE_SIZE;
3671 if (l > access_len)
3672 l = access_len;
51d7a9eb 3673 invalidate_and_set_dirty(addr1, l);
6d16c2f8
AL
3674 addr1 += l;
3675 access_len -= l;
3676 }
3677 }
868bb33f 3678 if (xen_enabled()) {
e41d7c69 3679 xen_invalidate_map_cache_entry(buffer);
050a0ddf 3680 }
6d16c2f8
AL
3681 return;
3682 }
3683 if (is_write) {
3684 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3685 }
f8a83245 3686 qemu_vfree(bounce.buffer);
6d16c2f8 3687 bounce.buffer = NULL;
ba223c29 3688 cpu_notify_map_clients();
6d16c2f8 3689}
d0ecd2aa 3690
8df1cd07 3691/* warning: addr must be aligned */
1e78bcc1
AG
3692static inline uint32_t ldl_phys_internal(target_phys_addr_t addr,
3693 enum device_endian endian)
8df1cd07 3694{
8df1cd07
FB
3695 uint8_t *ptr;
3696 uint32_t val;
f3705d53 3697 MemoryRegionSection *section;
8df1cd07 3698
06ef3525 3699 section = phys_page_find(addr >> TARGET_PAGE_BITS);
3b46e624 3700
cc5bea60
BS
3701 if (!(memory_region_is_ram(section->mr) ||
3702 memory_region_is_romd(section->mr))) {
8df1cd07 3703 /* I/O case */
cc5bea60 3704 addr = memory_region_section_addr(section, addr);
37ec01d4 3705 val = io_mem_read(section->mr, addr, 4);
1e78bcc1
AG
3706#if defined(TARGET_WORDS_BIGENDIAN)
3707 if (endian == DEVICE_LITTLE_ENDIAN) {
3708 val = bswap32(val);
3709 }
3710#else
3711 if (endian == DEVICE_BIG_ENDIAN) {
3712 val = bswap32(val);
3713 }
3714#endif
8df1cd07
FB
3715 } else {
3716 /* RAM case */
f3705d53 3717 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
06ef3525 3718 & TARGET_PAGE_MASK)
cc5bea60 3719 + memory_region_section_addr(section, addr));
1e78bcc1
AG
3720 switch (endian) {
3721 case DEVICE_LITTLE_ENDIAN:
3722 val = ldl_le_p(ptr);
3723 break;
3724 case DEVICE_BIG_ENDIAN:
3725 val = ldl_be_p(ptr);
3726 break;
3727 default:
3728 val = ldl_p(ptr);
3729 break;
3730 }
8df1cd07
FB
3731 }
3732 return val;
3733}
3734
1e78bcc1
AG
3735uint32_t ldl_phys(target_phys_addr_t addr)
3736{
3737 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
3738}
3739
3740uint32_t ldl_le_phys(target_phys_addr_t addr)
3741{
3742 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
3743}
3744
3745uint32_t ldl_be_phys(target_phys_addr_t addr)
3746{
3747 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
3748}
3749
84b7b8e7 3750/* warning: addr must be aligned */
1e78bcc1
AG
3751static inline uint64_t ldq_phys_internal(target_phys_addr_t addr,
3752 enum device_endian endian)
84b7b8e7 3753{
84b7b8e7
FB
3754 uint8_t *ptr;
3755 uint64_t val;
f3705d53 3756 MemoryRegionSection *section;
84b7b8e7 3757
06ef3525 3758 section = phys_page_find(addr >> TARGET_PAGE_BITS);
3b46e624 3759
cc5bea60
BS
3760 if (!(memory_region_is_ram(section->mr) ||
3761 memory_region_is_romd(section->mr))) {
84b7b8e7 3762 /* I/O case */
cc5bea60 3763 addr = memory_region_section_addr(section, addr);
1e78bcc1
AG
3764
3765 /* XXX This is broken when device endian != cpu endian.
3766 Fix and add "endian" variable check */
84b7b8e7 3767#ifdef TARGET_WORDS_BIGENDIAN
37ec01d4
AK
3768 val = io_mem_read(section->mr, addr, 4) << 32;
3769 val |= io_mem_read(section->mr, addr + 4, 4);
84b7b8e7 3770#else
37ec01d4
AK
3771 val = io_mem_read(section->mr, addr, 4);
3772 val |= io_mem_read(section->mr, addr + 4, 4) << 32;
84b7b8e7
FB
3773#endif
3774 } else {
3775 /* RAM case */
f3705d53 3776 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
06ef3525 3777 & TARGET_PAGE_MASK)
cc5bea60 3778 + memory_region_section_addr(section, addr));
1e78bcc1
AG
3779 switch (endian) {
3780 case DEVICE_LITTLE_ENDIAN:
3781 val = ldq_le_p(ptr);
3782 break;
3783 case DEVICE_BIG_ENDIAN:
3784 val = ldq_be_p(ptr);
3785 break;
3786 default:
3787 val = ldq_p(ptr);
3788 break;
3789 }
84b7b8e7
FB
3790 }
3791 return val;
3792}
3793
1e78bcc1
AG
3794uint64_t ldq_phys(target_phys_addr_t addr)
3795{
3796 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
3797}
3798
3799uint64_t ldq_le_phys(target_phys_addr_t addr)
3800{
3801 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
3802}
3803
3804uint64_t ldq_be_phys(target_phys_addr_t addr)
3805{
3806 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
3807}
3808
aab33094 3809/* XXX: optimize */
c227f099 3810uint32_t ldub_phys(target_phys_addr_t addr)
aab33094
FB
3811{
3812 uint8_t val;
3813 cpu_physical_memory_read(addr, &val, 1);
3814 return val;
3815}
3816
733f0b02 3817/* warning: addr must be aligned */
1e78bcc1
AG
3818static inline uint32_t lduw_phys_internal(target_phys_addr_t addr,
3819 enum device_endian endian)
aab33094 3820{
733f0b02
MT
3821 uint8_t *ptr;
3822 uint64_t val;
f3705d53 3823 MemoryRegionSection *section;
733f0b02 3824
06ef3525 3825 section = phys_page_find(addr >> TARGET_PAGE_BITS);
733f0b02 3826
cc5bea60
BS
3827 if (!(memory_region_is_ram(section->mr) ||
3828 memory_region_is_romd(section->mr))) {
733f0b02 3829 /* I/O case */
cc5bea60 3830 addr = memory_region_section_addr(section, addr);
37ec01d4 3831 val = io_mem_read(section->mr, addr, 2);
1e78bcc1
AG
3832#if defined(TARGET_WORDS_BIGENDIAN)
3833 if (endian == DEVICE_LITTLE_ENDIAN) {
3834 val = bswap16(val);
3835 }
3836#else
3837 if (endian == DEVICE_BIG_ENDIAN) {
3838 val = bswap16(val);
3839 }
3840#endif
733f0b02
MT
3841 } else {
3842 /* RAM case */
f3705d53 3843 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
06ef3525 3844 & TARGET_PAGE_MASK)
cc5bea60 3845 + memory_region_section_addr(section, addr));
1e78bcc1
AG
3846 switch (endian) {
3847 case DEVICE_LITTLE_ENDIAN:
3848 val = lduw_le_p(ptr);
3849 break;
3850 case DEVICE_BIG_ENDIAN:
3851 val = lduw_be_p(ptr);
3852 break;
3853 default:
3854 val = lduw_p(ptr);
3855 break;
3856 }
733f0b02
MT
3857 }
3858 return val;
aab33094
FB
3859}
3860
1e78bcc1
AG
3861uint32_t lduw_phys(target_phys_addr_t addr)
3862{
3863 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
3864}
3865
3866uint32_t lduw_le_phys(target_phys_addr_t addr)
3867{
3868 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
3869}
3870
3871uint32_t lduw_be_phys(target_phys_addr_t addr)
3872{
3873 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
3874}
3875
8df1cd07
FB
3876/* warning: addr must be aligned. The ram page is not masked as dirty
3877 and the code inside is not invalidated. It is useful if the dirty
3878 bits are used to track modified PTEs */
c227f099 3879void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
8df1cd07 3880{
8df1cd07 3881 uint8_t *ptr;
f3705d53 3882 MemoryRegionSection *section;
8df1cd07 3883
06ef3525 3884 section = phys_page_find(addr >> TARGET_PAGE_BITS);
3b46e624 3885
f3705d53 3886 if (!memory_region_is_ram(section->mr) || section->readonly) {
cc5bea60 3887 addr = memory_region_section_addr(section, addr);
f3705d53 3888 if (memory_region_is_ram(section->mr)) {
37ec01d4 3889 section = &phys_sections[phys_section_rom];
06ef3525 3890 }
37ec01d4 3891 io_mem_write(section->mr, addr, val, 4);
8df1cd07 3892 } else {
f3705d53 3893 unsigned long addr1 = (memory_region_get_ram_addr(section->mr)
06ef3525 3894 & TARGET_PAGE_MASK)
cc5bea60 3895 + memory_region_section_addr(section, addr);
5579c7f3 3896 ptr = qemu_get_ram_ptr(addr1);
8df1cd07 3897 stl_p(ptr, val);
74576198
AL
3898
3899 if (unlikely(in_migration)) {
3900 if (!cpu_physical_memory_is_dirty(addr1)) {
3901 /* invalidate code */
3902 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3903 /* set dirty bit */
f7c11b53
YT
3904 cpu_physical_memory_set_dirty_flags(
3905 addr1, (0xff & ~CODE_DIRTY_FLAG));
74576198
AL
3906 }
3907 }
8df1cd07
FB
3908 }
3909}
3910
c227f099 3911void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
bc98a7ef 3912{
bc98a7ef 3913 uint8_t *ptr;
f3705d53 3914 MemoryRegionSection *section;
bc98a7ef 3915
06ef3525 3916 section = phys_page_find(addr >> TARGET_PAGE_BITS);
3b46e624 3917
f3705d53 3918 if (!memory_region_is_ram(section->mr) || section->readonly) {
cc5bea60 3919 addr = memory_region_section_addr(section, addr);
f3705d53 3920 if (memory_region_is_ram(section->mr)) {
37ec01d4 3921 section = &phys_sections[phys_section_rom];
06ef3525 3922 }
bc98a7ef 3923#ifdef TARGET_WORDS_BIGENDIAN
37ec01d4
AK
3924 io_mem_write(section->mr, addr, val >> 32, 4);
3925 io_mem_write(section->mr, addr + 4, (uint32_t)val, 4);
bc98a7ef 3926#else
37ec01d4
AK
3927 io_mem_write(section->mr, addr, (uint32_t)val, 4);
3928 io_mem_write(section->mr, addr + 4, val >> 32, 4);
bc98a7ef
JM
3929#endif
3930 } else {
f3705d53 3931 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
06ef3525 3932 & TARGET_PAGE_MASK)
cc5bea60 3933 + memory_region_section_addr(section, addr));
bc98a7ef
JM
3934 stq_p(ptr, val);
3935 }
3936}
3937
8df1cd07 3938/* warning: addr must be aligned */
1e78bcc1
AG
3939static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val,
3940 enum device_endian endian)
8df1cd07 3941{
8df1cd07 3942 uint8_t *ptr;
f3705d53 3943 MemoryRegionSection *section;
8df1cd07 3944
06ef3525 3945 section = phys_page_find(addr >> TARGET_PAGE_BITS);
3b46e624 3946
f3705d53 3947 if (!memory_region_is_ram(section->mr) || section->readonly) {
cc5bea60 3948 addr = memory_region_section_addr(section, addr);
f3705d53 3949 if (memory_region_is_ram(section->mr)) {
37ec01d4 3950 section = &phys_sections[phys_section_rom];
06ef3525 3951 }
1e78bcc1
AG
3952#if defined(TARGET_WORDS_BIGENDIAN)
3953 if (endian == DEVICE_LITTLE_ENDIAN) {
3954 val = bswap32(val);
3955 }
3956#else
3957 if (endian == DEVICE_BIG_ENDIAN) {
3958 val = bswap32(val);
3959 }
3960#endif
37ec01d4 3961 io_mem_write(section->mr, addr, val, 4);
8df1cd07
FB
3962 } else {
3963 unsigned long addr1;
f3705d53 3964 addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
cc5bea60 3965 + memory_region_section_addr(section, addr);
8df1cd07 3966 /* RAM case */
5579c7f3 3967 ptr = qemu_get_ram_ptr(addr1);
1e78bcc1
AG
3968 switch (endian) {
3969 case DEVICE_LITTLE_ENDIAN:
3970 stl_le_p(ptr, val);
3971 break;
3972 case DEVICE_BIG_ENDIAN:
3973 stl_be_p(ptr, val);
3974 break;
3975 default:
3976 stl_p(ptr, val);
3977 break;
3978 }
51d7a9eb 3979 invalidate_and_set_dirty(addr1, 4);
8df1cd07
FB
3980 }
3981}
3982
1e78bcc1
AG
3983void stl_phys(target_phys_addr_t addr, uint32_t val)
3984{
3985 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
3986}
3987
3988void stl_le_phys(target_phys_addr_t addr, uint32_t val)
3989{
3990 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
3991}
3992
3993void stl_be_phys(target_phys_addr_t addr, uint32_t val)
3994{
3995 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
3996}
3997
aab33094 3998/* XXX: optimize */
c227f099 3999void stb_phys(target_phys_addr_t addr, uint32_t val)
aab33094
FB
4000{
4001 uint8_t v = val;
4002 cpu_physical_memory_write(addr, &v, 1);
4003}
4004
733f0b02 4005/* warning: addr must be aligned */
1e78bcc1
AG
4006static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val,
4007 enum device_endian endian)
aab33094 4008{
733f0b02 4009 uint8_t *ptr;
f3705d53 4010 MemoryRegionSection *section;
733f0b02 4011
06ef3525 4012 section = phys_page_find(addr >> TARGET_PAGE_BITS);
733f0b02 4013
f3705d53 4014 if (!memory_region_is_ram(section->mr) || section->readonly) {
cc5bea60 4015 addr = memory_region_section_addr(section, addr);
f3705d53 4016 if (memory_region_is_ram(section->mr)) {
37ec01d4 4017 section = &phys_sections[phys_section_rom];
06ef3525 4018 }
1e78bcc1
AG
4019#if defined(TARGET_WORDS_BIGENDIAN)
4020 if (endian == DEVICE_LITTLE_ENDIAN) {
4021 val = bswap16(val);
4022 }
4023#else
4024 if (endian == DEVICE_BIG_ENDIAN) {
4025 val = bswap16(val);
4026 }
4027#endif
37ec01d4 4028 io_mem_write(section->mr, addr, val, 2);
733f0b02
MT
4029 } else {
4030 unsigned long addr1;
f3705d53 4031 addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
cc5bea60 4032 + memory_region_section_addr(section, addr);
733f0b02
MT
4033 /* RAM case */
4034 ptr = qemu_get_ram_ptr(addr1);
1e78bcc1
AG
4035 switch (endian) {
4036 case DEVICE_LITTLE_ENDIAN:
4037 stw_le_p(ptr, val);
4038 break;
4039 case DEVICE_BIG_ENDIAN:
4040 stw_be_p(ptr, val);
4041 break;
4042 default:
4043 stw_p(ptr, val);
4044 break;
4045 }
51d7a9eb 4046 invalidate_and_set_dirty(addr1, 2);
733f0b02 4047 }
aab33094
FB
4048}
4049
1e78bcc1
AG
4050void stw_phys(target_phys_addr_t addr, uint32_t val)
4051{
4052 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
4053}
4054
4055void stw_le_phys(target_phys_addr_t addr, uint32_t val)
4056{
4057 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
4058}
4059
4060void stw_be_phys(target_phys_addr_t addr, uint32_t val)
4061{
4062 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
4063}
4064
aab33094 4065/* XXX: optimize */
c227f099 4066void stq_phys(target_phys_addr_t addr, uint64_t val)
aab33094
FB
4067{
4068 val = tswap64(val);
71d2b725 4069 cpu_physical_memory_write(addr, &val, 8);
aab33094
FB
4070}
4071
1e78bcc1
AG
4072void stq_le_phys(target_phys_addr_t addr, uint64_t val)
4073{
4074 val = cpu_to_le64(val);
4075 cpu_physical_memory_write(addr, &val, 8);
4076}
4077
4078void stq_be_phys(target_phys_addr_t addr, uint64_t val)
4079{
4080 val = cpu_to_be64(val);
4081 cpu_physical_memory_write(addr, &val, 8);
4082}
4083
5e2972fd 4084/* virtual memory access for debug (includes writing to ROM) */
9349b4f9 4085int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
b448f2f3 4086 uint8_t *buf, int len, int is_write)
13eb76e0
FB
4087{
4088 int l;
c227f099 4089 target_phys_addr_t phys_addr;
9b3c35e0 4090 target_ulong page;
13eb76e0
FB
4091
4092 while (len > 0) {
4093 page = addr & TARGET_PAGE_MASK;
4094 phys_addr = cpu_get_phys_page_debug(env, page);
4095 /* if no physical page mapped, return an error */
4096 if (phys_addr == -1)
4097 return -1;
4098 l = (page + TARGET_PAGE_SIZE) - addr;
4099 if (l > len)
4100 l = len;
5e2972fd 4101 phys_addr += (addr & ~TARGET_PAGE_MASK);
5e2972fd
AL
4102 if (is_write)
4103 cpu_physical_memory_write_rom(phys_addr, buf, l);
4104 else
5e2972fd 4105 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
13eb76e0
FB
4106 len -= l;
4107 buf += l;
4108 addr += l;
4109 }
4110 return 0;
4111}
a68fe89c 4112#endif
13eb76e0 4113
2e70f6ef
PB
4114/* in deterministic execution mode, instructions doing device I/Os
4115 must be at the end of the TB */
20503968 4116void cpu_io_recompile(CPUArchState *env, uintptr_t retaddr)
2e70f6ef
PB
4117{
4118 TranslationBlock *tb;
4119 uint32_t n, cflags;
4120 target_ulong pc, cs_base;
4121 uint64_t flags;
4122
20503968 4123 tb = tb_find_pc(retaddr);
2e70f6ef
PB
4124 if (!tb) {
4125 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
20503968 4126 (void *)retaddr);
2e70f6ef
PB
4127 }
4128 n = env->icount_decr.u16.low + tb->icount;
20503968 4129 cpu_restore_state(tb, env, retaddr);
2e70f6ef 4130 /* Calculate how many instructions had been executed before the fault
bf20dc07 4131 occurred. */
2e70f6ef
PB
4132 n = n - env->icount_decr.u16.low;
4133 /* Generate a new TB ending on the I/O insn. */
4134 n++;
4135 /* On MIPS and SH, delay slot instructions can only be restarted if
4136 they were already the first instruction in the TB. If this is not
bf20dc07 4137 the first instruction in a TB then re-execute the preceding
2e70f6ef
PB
4138 branch. */
4139#if defined(TARGET_MIPS)
4140 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
4141 env->active_tc.PC -= 4;
4142 env->icount_decr.u16.low++;
4143 env->hflags &= ~MIPS_HFLAG_BMASK;
4144 }
4145#elif defined(TARGET_SH4)
4146 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
4147 && n > 1) {
4148 env->pc -= 2;
4149 env->icount_decr.u16.low++;
4150 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
4151 }
4152#endif
4153 /* This should never happen. */
4154 if (n > CF_COUNT_MASK)
4155 cpu_abort(env, "TB too big during recompile");
4156
4157 cflags = n | CF_LAST_IO;
4158 pc = tb->pc;
4159 cs_base = tb->cs_base;
4160 flags = tb->flags;
4161 tb_phys_invalidate(tb, -1);
4162 /* FIXME: In theory this could raise an exception. In practice
4163 we have already translated the block once so it's probably ok. */
4164 tb_gen_code(env, pc, cs_base, flags, cflags);
bf20dc07 4165 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
2e70f6ef
PB
4166 the first in the TB) then we end up generating a whole new TB and
4167 repeating the fault, which is horribly inefficient.
4168 Better would be to execute just this insn uncached, or generate a
4169 second new TB. */
4170 cpu_resume_from_signal(env, NULL);
4171}
4172
b3755a91
PB
4173#if !defined(CONFIG_USER_ONLY)
4174
055403b2 4175void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
e3db7226
FB
4176{
4177 int i, target_code_size, max_target_code_size;
4178 int direct_jmp_count, direct_jmp2_count, cross_page;
4179 TranslationBlock *tb;
3b46e624 4180
e3db7226
FB
4181 target_code_size = 0;
4182 max_target_code_size = 0;
4183 cross_page = 0;
4184 direct_jmp_count = 0;
4185 direct_jmp2_count = 0;
4186 for(i = 0; i < nb_tbs; i++) {
4187 tb = &tbs[i];
4188 target_code_size += tb->size;
4189 if (tb->size > max_target_code_size)
4190 max_target_code_size = tb->size;
4191 if (tb->page_addr[1] != -1)
4192 cross_page++;
4193 if (tb->tb_next_offset[0] != 0xffff) {
4194 direct_jmp_count++;
4195 if (tb->tb_next_offset[1] != 0xffff) {
4196 direct_jmp2_count++;
4197 }
4198 }
4199 }
4200 /* XXX: avoid using doubles ? */
57fec1fe 4201 cpu_fprintf(f, "Translation buffer state:\n");
f1bc0bcc 4202 cpu_fprintf(f, "gen code size %td/%zd\n",
26a5f13b
FB
4203 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
4204 cpu_fprintf(f, "TB count %d/%d\n",
4205 nb_tbs, code_gen_max_blocks);
5fafdf24 4206 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
e3db7226
FB
4207 nb_tbs ? target_code_size / nb_tbs : 0,
4208 max_target_code_size);
055403b2 4209 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
e3db7226
FB
4210 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
4211 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
5fafdf24
TS
4212 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
4213 cross_page,
e3db7226
FB
4214 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
4215 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
5fafdf24 4216 direct_jmp_count,
e3db7226
FB
4217 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
4218 direct_jmp2_count,
4219 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
57fec1fe 4220 cpu_fprintf(f, "\nStatistics:\n");
e3db7226
FB
4221 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
4222 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
4223 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
b67d9a52 4224 tcg_dump_info(f, cpu_fprintf);
e3db7226
FB
4225}
4226
82afa586
BH
4227/*
4228 * A helper function for the _utterly broken_ virtio device model to find out if
4229 * it's running on a big endian machine. Don't do this at home kids!
4230 */
4231bool virtio_is_big_endian(void);
4232bool virtio_is_big_endian(void)
4233{
4234#if defined(TARGET_WORDS_BIGENDIAN)
4235 return true;
4236#else
4237 return false;
4238#endif
4239}
4240
61382a50 4241#endif
76f35538
WC
4242
4243#ifndef CONFIG_USER_ONLY
4244bool cpu_physical_memory_is_io(target_phys_addr_t phys_addr)
4245{
4246 MemoryRegionSection *section;
4247
4248 section = phys_page_find(phys_addr >> TARGET_PAGE_BITS);
4249
4250 return !(memory_region_is_ram(section->mr) ||
4251 memory_region_is_romd(section->mr));
4252}
4253#endif
This page took 1.355707 seconds and 4 git commands to generate.