]> git.proxmox.com Git - mirror_qemu.git/blame - exec.c
pc-bios/s390-ccw: build for z900
[mirror_qemu.git] / exec.c
CommitLineData
54936004 1/*
5b6dd868 2 * Virtual page mapping
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"
777872e5 20#ifndef _WIN32
a98d49b1 21#include <sys/types.h>
d5a8f07c
FB
22#include <sys/mman.h>
23#endif
54936004 24
055403b2 25#include "qemu-common.h"
6180a181 26#include "cpu.h"
b67d9a52 27#include "tcg.h"
b3c7724c 28#include "hw/hw.h"
4485bd26 29#if !defined(CONFIG_USER_ONLY)
47c8ca53 30#include "hw/boards.h"
4485bd26 31#endif
cc9e98cb 32#include "hw/qdev.h"
1de7afc9 33#include "qemu/osdep.h"
9c17d615 34#include "sysemu/kvm.h"
2ff3de68 35#include "sysemu/sysemu.h"
0d09e41a 36#include "hw/xen/xen.h"
1de7afc9
PB
37#include "qemu/timer.h"
38#include "qemu/config-file.h"
75a34036 39#include "qemu/error-report.h"
022c62cb 40#include "exec/memory.h"
9c17d615 41#include "sysemu/dma.h"
022c62cb 42#include "exec/address-spaces.h"
53a5960a
PB
43#if defined(CONFIG_USER_ONLY)
44#include <qemu.h>
432d268c 45#else /* !CONFIG_USER_ONLY */
9c17d615 46#include "sysemu/xen-mapcache.h"
6506e4f9 47#include "trace.h"
53a5960a 48#endif
0d6d3c87 49#include "exec/cpu-all.h"
0dc3f44a 50#include "qemu/rcu_queue.h"
4840f10e 51#include "qemu/main-loop.h"
5b6dd868 52#include "translate-all.h"
7615936e 53#include "sysemu/replay.h"
0cac1b66 54
022c62cb 55#include "exec/memory-internal.h"
220c3ebd 56#include "exec/ram_addr.h"
67d95c15 57
b35ba30f 58#include "qemu/range.h"
794e8f30
MT
59#ifndef _WIN32
60#include "qemu/mmap-alloc.h"
61#endif
b35ba30f 62
db7b5426 63//#define DEBUG_SUBPAGE
1196be37 64
e2eef170 65#if !defined(CONFIG_USER_ONLY)
0dc3f44a
MD
66/* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes
67 * are protected by the ramlist lock.
68 */
0d53d9fe 69RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
62152b8a
AK
70
71static MemoryRegion *system_memory;
309cb471 72static MemoryRegion *system_io;
62152b8a 73
f6790af6
AK
74AddressSpace address_space_io;
75AddressSpace address_space_memory;
2673a5da 76
0844e007 77MemoryRegion io_mem_rom, io_mem_notdirty;
acc9d80b 78static MemoryRegion io_mem_unassigned;
0e0df1e2 79
7bd4f430
PB
80/* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
81#define RAM_PREALLOC (1 << 0)
82
dbcb8981
PB
83/* RAM is mmap-ed with MAP_SHARED */
84#define RAM_SHARED (1 << 1)
85
62be4e3a
MT
86/* Only a portion of RAM (used_length) is actually used, and migrated.
87 * This used_length size can change across reboots.
88 */
89#define RAM_RESIZEABLE (1 << 2)
90
794e8f30 91/* RAM is backed by an mmapped file.
8561c924 92 */
794e8f30 93#define RAM_FILE (1 << 3)
e2eef170 94#endif
9fa3e853 95
bdc44640 96struct CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
6a00d601
FB
97/* current CPU in the current thread. It is only valid inside
98 cpu_exec() */
f240eb6f 99__thread CPUState *current_cpu;
2e70f6ef 100/* 0 = Do not count executed instructions.
bf20dc07 101 1 = Precise instruction counting.
2e70f6ef 102 2 = Adaptive rate instruction counting. */
5708fc66 103int use_icount;
6a00d601 104
e2eef170 105#if !defined(CONFIG_USER_ONLY)
4346ae3e 106
1db8abb1
PB
107typedef struct PhysPageEntry PhysPageEntry;
108
109struct PhysPageEntry {
9736e55b 110 /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
8b795765 111 uint32_t skip : 6;
9736e55b 112 /* index into phys_sections (!skip) or phys_map_nodes (skip) */
8b795765 113 uint32_t ptr : 26;
1db8abb1
PB
114};
115
8b795765
MT
116#define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
117
03f49957 118/* Size of the L2 (and L3, etc) page tables. */
57271d63 119#define ADDR_SPACE_BITS 64
03f49957 120
026736ce 121#define P_L2_BITS 9
03f49957
PB
122#define P_L2_SIZE (1 << P_L2_BITS)
123
124#define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
125
126typedef PhysPageEntry Node[P_L2_SIZE];
0475d94f 127
53cb28cb 128typedef struct PhysPageMap {
79e2b9ae
PB
129 struct rcu_head rcu;
130
53cb28cb
MA
131 unsigned sections_nb;
132 unsigned sections_nb_alloc;
133 unsigned nodes_nb;
134 unsigned nodes_nb_alloc;
135 Node *nodes;
136 MemoryRegionSection *sections;
137} PhysPageMap;
138
1db8abb1 139struct AddressSpaceDispatch {
79e2b9ae
PB
140 struct rcu_head rcu;
141
1db8abb1
PB
142 /* This is a multi-level map on the physical address space.
143 * The bottom level has pointers to MemoryRegionSections.
144 */
145 PhysPageEntry phys_map;
53cb28cb 146 PhysPageMap map;
acc9d80b 147 AddressSpace *as;
1db8abb1
PB
148};
149
90260c6c
JK
150#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
151typedef struct subpage_t {
152 MemoryRegion iomem;
acc9d80b 153 AddressSpace *as;
90260c6c
JK
154 hwaddr base;
155 uint16_t sub_section[TARGET_PAGE_SIZE];
156} subpage_t;
157
b41aac4f
LPF
158#define PHYS_SECTION_UNASSIGNED 0
159#define PHYS_SECTION_NOTDIRTY 1
160#define PHYS_SECTION_ROM 2
161#define PHYS_SECTION_WATCH 3
5312bd8b 162
e2eef170 163static void io_mem_init(void);
62152b8a 164static void memory_map_init(void);
09daed84 165static void tcg_commit(MemoryListener *listener);
e2eef170 166
1ec9b909 167static MemoryRegion io_mem_watch;
32857f4d
PM
168
169/**
170 * CPUAddressSpace: all the information a CPU needs about an AddressSpace
171 * @cpu: the CPU whose AddressSpace this is
172 * @as: the AddressSpace itself
173 * @memory_dispatch: its dispatch pointer (cached, RCU protected)
174 * @tcg_as_listener: listener for tracking changes to the AddressSpace
175 */
176struct CPUAddressSpace {
177 CPUState *cpu;
178 AddressSpace *as;
179 struct AddressSpaceDispatch *memory_dispatch;
180 MemoryListener tcg_as_listener;
181};
182
6658ffb8 183#endif
fd6ce8f6 184
6d9a1304 185#if !defined(CONFIG_USER_ONLY)
d6f2ea22 186
53cb28cb 187static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
d6f2ea22 188{
53cb28cb
MA
189 if (map->nodes_nb + nodes > map->nodes_nb_alloc) {
190 map->nodes_nb_alloc = MAX(map->nodes_nb_alloc * 2, 16);
191 map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, map->nodes_nb + nodes);
192 map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc);
d6f2ea22 193 }
f7bf5461
AK
194}
195
db94604b 196static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf)
f7bf5461
AK
197{
198 unsigned i;
8b795765 199 uint32_t ret;
db94604b
PB
200 PhysPageEntry e;
201 PhysPageEntry *p;
f7bf5461 202
53cb28cb 203 ret = map->nodes_nb++;
db94604b 204 p = map->nodes[ret];
f7bf5461 205 assert(ret != PHYS_MAP_NODE_NIL);
53cb28cb 206 assert(ret != map->nodes_nb_alloc);
db94604b
PB
207
208 e.skip = leaf ? 0 : 1;
209 e.ptr = leaf ? PHYS_SECTION_UNASSIGNED : PHYS_MAP_NODE_NIL;
03f49957 210 for (i = 0; i < P_L2_SIZE; ++i) {
db94604b 211 memcpy(&p[i], &e, sizeof(e));
d6f2ea22 212 }
f7bf5461 213 return ret;
d6f2ea22
AK
214}
215
53cb28cb
MA
216static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
217 hwaddr *index, hwaddr *nb, uint16_t leaf,
2999097b 218 int level)
f7bf5461
AK
219{
220 PhysPageEntry *p;
03f49957 221 hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
108c49b8 222
9736e55b 223 if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
db94604b 224 lp->ptr = phys_map_node_alloc(map, level == 0);
92e873b9 225 }
db94604b 226 p = map->nodes[lp->ptr];
03f49957 227 lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
f7bf5461 228
03f49957 229 while (*nb && lp < &p[P_L2_SIZE]) {
07f07b31 230 if ((*index & (step - 1)) == 0 && *nb >= step) {
9736e55b 231 lp->skip = 0;
c19e8800 232 lp->ptr = leaf;
07f07b31
AK
233 *index += step;
234 *nb -= step;
2999097b 235 } else {
53cb28cb 236 phys_page_set_level(map, lp, index, nb, leaf, level - 1);
2999097b
AK
237 }
238 ++lp;
f7bf5461
AK
239 }
240}
241
ac1970fb 242static void phys_page_set(AddressSpaceDispatch *d,
a8170e5e 243 hwaddr index, hwaddr nb,
2999097b 244 uint16_t leaf)
f7bf5461 245{
2999097b 246 /* Wildly overreserve - it doesn't matter much. */
53cb28cb 247 phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
5cd2c5b6 248
53cb28cb 249 phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
92e873b9
FB
250}
251
b35ba30f
MT
252/* Compact a non leaf page entry. Simply detect that the entry has a single child,
253 * and update our entry so we can skip it and go directly to the destination.
254 */
255static void phys_page_compact(PhysPageEntry *lp, Node *nodes, unsigned long *compacted)
256{
257 unsigned valid_ptr = P_L2_SIZE;
258 int valid = 0;
259 PhysPageEntry *p;
260 int i;
261
262 if (lp->ptr == PHYS_MAP_NODE_NIL) {
263 return;
264 }
265
266 p = nodes[lp->ptr];
267 for (i = 0; i < P_L2_SIZE; i++) {
268 if (p[i].ptr == PHYS_MAP_NODE_NIL) {
269 continue;
270 }
271
272 valid_ptr = i;
273 valid++;
274 if (p[i].skip) {
275 phys_page_compact(&p[i], nodes, compacted);
276 }
277 }
278
279 /* We can only compress if there's only one child. */
280 if (valid != 1) {
281 return;
282 }
283
284 assert(valid_ptr < P_L2_SIZE);
285
286 /* Don't compress if it won't fit in the # of bits we have. */
287 if (lp->skip + p[valid_ptr].skip >= (1 << 3)) {
288 return;
289 }
290
291 lp->ptr = p[valid_ptr].ptr;
292 if (!p[valid_ptr].skip) {
293 /* If our only child is a leaf, make this a leaf. */
294 /* By design, we should have made this node a leaf to begin with so we
295 * should never reach here.
296 * But since it's so simple to handle this, let's do it just in case we
297 * change this rule.
298 */
299 lp->skip = 0;
300 } else {
301 lp->skip += p[valid_ptr].skip;
302 }
303}
304
305static void phys_page_compact_all(AddressSpaceDispatch *d, int nodes_nb)
306{
307 DECLARE_BITMAP(compacted, nodes_nb);
308
309 if (d->phys_map.skip) {
53cb28cb 310 phys_page_compact(&d->phys_map, d->map.nodes, compacted);
b35ba30f
MT
311 }
312}
313
97115a8d 314static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr addr,
9affd6fc 315 Node *nodes, MemoryRegionSection *sections)
92e873b9 316{
31ab2b4a 317 PhysPageEntry *p;
97115a8d 318 hwaddr index = addr >> TARGET_PAGE_BITS;
31ab2b4a 319 int i;
f1f6e3b8 320
9736e55b 321 for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) {
c19e8800 322 if (lp.ptr == PHYS_MAP_NODE_NIL) {
9affd6fc 323 return &sections[PHYS_SECTION_UNASSIGNED];
31ab2b4a 324 }
9affd6fc 325 p = nodes[lp.ptr];
03f49957 326 lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
5312bd8b 327 }
b35ba30f
MT
328
329 if (sections[lp.ptr].size.hi ||
330 range_covers_byte(sections[lp.ptr].offset_within_address_space,
331 sections[lp.ptr].size.lo, addr)) {
332 return &sections[lp.ptr];
333 } else {
334 return &sections[PHYS_SECTION_UNASSIGNED];
335 }
f3705d53
AK
336}
337
e5548617
BS
338bool memory_region_is_unassigned(MemoryRegion *mr)
339{
2a8e7499 340 return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
5b6dd868 341 && mr != &io_mem_watch;
fd6ce8f6 342}
149f54b5 343
79e2b9ae 344/* Called from RCU critical section */
c7086b4a 345static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
90260c6c
JK
346 hwaddr addr,
347 bool resolve_subpage)
9f029603 348{
90260c6c
JK
349 MemoryRegionSection *section;
350 subpage_t *subpage;
351
53cb28cb 352 section = phys_page_find(d->phys_map, addr, d->map.nodes, d->map.sections);
90260c6c
JK
353 if (resolve_subpage && section->mr->subpage) {
354 subpage = container_of(section->mr, subpage_t, iomem);
53cb28cb 355 section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
90260c6c
JK
356 }
357 return section;
9f029603
JK
358}
359
79e2b9ae 360/* Called from RCU critical section */
90260c6c 361static MemoryRegionSection *
c7086b4a 362address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat,
90260c6c 363 hwaddr *plen, bool resolve_subpage)
149f54b5
PB
364{
365 MemoryRegionSection *section;
965eb2fc 366 MemoryRegion *mr;
a87f3954 367 Int128 diff;
149f54b5 368
c7086b4a 369 section = address_space_lookup_region(d, addr, resolve_subpage);
149f54b5
PB
370 /* Compute offset within MemoryRegionSection */
371 addr -= section->offset_within_address_space;
372
373 /* Compute offset within MemoryRegion */
374 *xlat = addr + section->offset_within_region;
375
965eb2fc 376 mr = section->mr;
b242e0e0
PB
377
378 /* MMIO registers can be expected to perform full-width accesses based only
379 * on their address, without considering adjacent registers that could
380 * decode to completely different MemoryRegions. When such registers
381 * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
382 * regions overlap wildly. For this reason we cannot clamp the accesses
383 * here.
384 *
385 * If the length is small (as is the case for address_space_ldl/stl),
386 * everything works fine. If the incoming length is large, however,
387 * the caller really has to do the clamping through memory_access_size.
388 */
965eb2fc 389 if (memory_region_is_ram(mr)) {
e4a511f8 390 diff = int128_sub(section->size, int128_make64(addr));
965eb2fc
PB
391 *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
392 }
149f54b5
PB
393 return section;
394}
90260c6c 395
a87f3954
PB
396static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
397{
398 if (memory_region_is_ram(mr)) {
399 return !(is_write && mr->readonly);
400 }
401 if (memory_region_is_romd(mr)) {
402 return !is_write;
403 }
404
405 return false;
406}
407
41063e1e 408/* Called from RCU critical section */
5c8a00ce
PB
409MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
410 hwaddr *xlat, hwaddr *plen,
411 bool is_write)
90260c6c 412{
30951157
AK
413 IOMMUTLBEntry iotlb;
414 MemoryRegionSection *section;
415 MemoryRegion *mr;
30951157
AK
416
417 for (;;) {
79e2b9ae
PB
418 AddressSpaceDispatch *d = atomic_rcu_read(&as->dispatch);
419 section = address_space_translate_internal(d, addr, &addr, plen, true);
30951157
AK
420 mr = section->mr;
421
422 if (!mr->iommu_ops) {
423 break;
424 }
425
8d7b8cb9 426 iotlb = mr->iommu_ops->translate(mr, addr, is_write);
30951157
AK
427 addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
428 | (addr & iotlb.addr_mask));
23820dbf 429 *plen = MIN(*plen, (addr | iotlb.addr_mask) - addr + 1);
30951157
AK
430 if (!(iotlb.perm & (1 << is_write))) {
431 mr = &io_mem_unassigned;
432 break;
433 }
434
435 as = iotlb.target_as;
436 }
437
fe680d0d 438 if (xen_enabled() && memory_access_is_direct(mr, is_write)) {
a87f3954 439 hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr;
23820dbf 440 *plen = MIN(page, *plen);
a87f3954
PB
441 }
442
30951157
AK
443 *xlat = addr;
444 return mr;
90260c6c
JK
445}
446
79e2b9ae 447/* Called from RCU critical section */
90260c6c 448MemoryRegionSection *
9d82b5a7
PB
449address_space_translate_for_iotlb(CPUState *cpu, hwaddr addr,
450 hwaddr *xlat, hwaddr *plen)
90260c6c 451{
30951157 452 MemoryRegionSection *section;
32857f4d 453 section = address_space_translate_internal(cpu->cpu_ases[0].memory_dispatch,
9d82b5a7 454 addr, xlat, plen, false);
30951157
AK
455
456 assert(!section->mr->iommu_ops);
457 return section;
90260c6c 458}
5b6dd868 459#endif
fd6ce8f6 460
b170fce3 461#if !defined(CONFIG_USER_ONLY)
5b6dd868
BS
462
463static int cpu_common_post_load(void *opaque, int version_id)
fd6ce8f6 464{
259186a7 465 CPUState *cpu = opaque;
a513fe19 466
5b6dd868
BS
467 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
468 version_id is increased. */
259186a7 469 cpu->interrupt_request &= ~0x01;
c01a71c1 470 tlb_flush(cpu, 1);
5b6dd868
BS
471
472 return 0;
a513fe19 473}
7501267e 474
6c3bff0e
PD
475static int cpu_common_pre_load(void *opaque)
476{
477 CPUState *cpu = opaque;
478
adee6424 479 cpu->exception_index = -1;
6c3bff0e
PD
480
481 return 0;
482}
483
484static bool cpu_common_exception_index_needed(void *opaque)
485{
486 CPUState *cpu = opaque;
487
adee6424 488 return tcg_enabled() && cpu->exception_index != -1;
6c3bff0e
PD
489}
490
491static const VMStateDescription vmstate_cpu_common_exception_index = {
492 .name = "cpu_common/exception_index",
493 .version_id = 1,
494 .minimum_version_id = 1,
5cd8cada 495 .needed = cpu_common_exception_index_needed,
6c3bff0e
PD
496 .fields = (VMStateField[]) {
497 VMSTATE_INT32(exception_index, CPUState),
498 VMSTATE_END_OF_LIST()
499 }
500};
501
bac05aa9
AS
502static bool cpu_common_crash_occurred_needed(void *opaque)
503{
504 CPUState *cpu = opaque;
505
506 return cpu->crash_occurred;
507}
508
509static const VMStateDescription vmstate_cpu_common_crash_occurred = {
510 .name = "cpu_common/crash_occurred",
511 .version_id = 1,
512 .minimum_version_id = 1,
513 .needed = cpu_common_crash_occurred_needed,
514 .fields = (VMStateField[]) {
515 VMSTATE_BOOL(crash_occurred, CPUState),
516 VMSTATE_END_OF_LIST()
517 }
518};
519
1a1562f5 520const VMStateDescription vmstate_cpu_common = {
5b6dd868
BS
521 .name = "cpu_common",
522 .version_id = 1,
523 .minimum_version_id = 1,
6c3bff0e 524 .pre_load = cpu_common_pre_load,
5b6dd868 525 .post_load = cpu_common_post_load,
35d08458 526 .fields = (VMStateField[]) {
259186a7
AF
527 VMSTATE_UINT32(halted, CPUState),
528 VMSTATE_UINT32(interrupt_request, CPUState),
5b6dd868 529 VMSTATE_END_OF_LIST()
6c3bff0e 530 },
5cd8cada
JQ
531 .subsections = (const VMStateDescription*[]) {
532 &vmstate_cpu_common_exception_index,
bac05aa9 533 &vmstate_cpu_common_crash_occurred,
5cd8cada 534 NULL
5b6dd868
BS
535 }
536};
1a1562f5 537
5b6dd868 538#endif
ea041c0e 539
38d8f5c8 540CPUState *qemu_get_cpu(int index)
ea041c0e 541{
bdc44640 542 CPUState *cpu;
ea041c0e 543
bdc44640 544 CPU_FOREACH(cpu) {
55e5c285 545 if (cpu->cpu_index == index) {
bdc44640 546 return cpu;
55e5c285 547 }
ea041c0e 548 }
5b6dd868 549
bdc44640 550 return NULL;
ea041c0e
FB
551}
552
09daed84
EI
553#if !defined(CONFIG_USER_ONLY)
554void tcg_cpu_address_space_init(CPUState *cpu, AddressSpace *as)
555{
556 /* We only support one address space per cpu at the moment. */
557 assert(cpu->as == as);
558
32857f4d
PM
559 if (cpu->cpu_ases) {
560 /* We've already registered the listener for our only AS */
561 return;
09daed84 562 }
32857f4d
PM
563
564 cpu->cpu_ases = g_new0(CPUAddressSpace, 1);
565 cpu->cpu_ases[0].cpu = cpu;
566 cpu->cpu_ases[0].as = as;
567 cpu->cpu_ases[0].tcg_as_listener.commit = tcg_commit;
568 memory_listener_register(&cpu->cpu_ases[0].tcg_as_listener, as);
09daed84
EI
569}
570#endif
571
b7bca733
BR
572#ifndef CONFIG_USER_ONLY
573static DECLARE_BITMAP(cpu_index_map, MAX_CPUMASK_BITS);
574
575static int cpu_get_free_index(Error **errp)
576{
577 int cpu = find_first_zero_bit(cpu_index_map, MAX_CPUMASK_BITS);
578
579 if (cpu >= MAX_CPUMASK_BITS) {
580 error_setg(errp, "Trying to use more CPUs than max of %d",
581 MAX_CPUMASK_BITS);
582 return -1;
583 }
584
585 bitmap_set(cpu_index_map, cpu, 1);
586 return cpu;
587}
588
589void cpu_exec_exit(CPUState *cpu)
590{
591 if (cpu->cpu_index == -1) {
592 /* cpu_index was never allocated by this @cpu or was already freed. */
593 return;
594 }
595
596 bitmap_clear(cpu_index_map, cpu->cpu_index, 1);
597 cpu->cpu_index = -1;
598}
599#else
600
601static int cpu_get_free_index(Error **errp)
602{
603 CPUState *some_cpu;
604 int cpu_index = 0;
605
606 CPU_FOREACH(some_cpu) {
607 cpu_index++;
608 }
609 return cpu_index;
610}
611
612void cpu_exec_exit(CPUState *cpu)
613{
614}
615#endif
616
4bad9e39 617void cpu_exec_init(CPUState *cpu, Error **errp)
ea041c0e 618{
b170fce3 619 CPUClass *cc = CPU_GET_CLASS(cpu);
5b6dd868 620 int cpu_index;
b7bca733 621 Error *local_err = NULL;
5b6dd868 622
291135b5
EH
623#ifndef CONFIG_USER_ONLY
624 cpu->as = &address_space_memory;
625 cpu->thread_id = qemu_get_thread_id();
291135b5
EH
626#endif
627
5b6dd868
BS
628#if defined(CONFIG_USER_ONLY)
629 cpu_list_lock();
630#endif
b7bca733
BR
631 cpu_index = cpu->cpu_index = cpu_get_free_index(&local_err);
632 if (local_err) {
633 error_propagate(errp, local_err);
634#if defined(CONFIG_USER_ONLY)
635 cpu_list_unlock();
636#endif
637 return;
5b6dd868 638 }
bdc44640 639 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
5b6dd868
BS
640#if defined(CONFIG_USER_ONLY)
641 cpu_list_unlock();
642#endif
e0d47944
AF
643 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
644 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
645 }
5b6dd868 646#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
5b6dd868 647 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
4bad9e39 648 cpu_save, cpu_load, cpu->env_ptr);
b170fce3 649 assert(cc->vmsd == NULL);
e0d47944 650 assert(qdev_get_vmsd(DEVICE(cpu)) == NULL);
5b6dd868 651#endif
b170fce3
AF
652 if (cc->vmsd != NULL) {
653 vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
654 }
ea041c0e
FB
655}
656
94df27fd 657#if defined(CONFIG_USER_ONLY)
00b941e5 658static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
94df27fd
PB
659{
660 tb_invalidate_phys_page_range(pc, pc + 1, 0);
661}
662#else
00b941e5 663static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
1e7855a5 664{
e8262a1b
MF
665 hwaddr phys = cpu_get_phys_page_debug(cpu, pc);
666 if (phys != -1) {
09daed84 667 tb_invalidate_phys_addr(cpu->as,
29d8ec7b 668 phys | (pc & ~TARGET_PAGE_MASK));
e8262a1b 669 }
1e7855a5 670}
c27004ec 671#endif
d720b93d 672
c527ee8f 673#if defined(CONFIG_USER_ONLY)
75a34036 674void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
c527ee8f
PB
675
676{
677}
678
3ee887e8
PM
679int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
680 int flags)
681{
682 return -ENOSYS;
683}
684
685void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
686{
687}
688
75a34036 689int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
c527ee8f
PB
690 int flags, CPUWatchpoint **watchpoint)
691{
692 return -ENOSYS;
693}
694#else
6658ffb8 695/* Add a watchpoint. */
75a34036 696int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
a1d1bb31 697 int flags, CPUWatchpoint **watchpoint)
6658ffb8 698{
c0ce998e 699 CPUWatchpoint *wp;
6658ffb8 700
05068c0d 701 /* forbid ranges which are empty or run off the end of the address space */
07e2863d 702 if (len == 0 || (addr + len - 1) < addr) {
75a34036
AF
703 error_report("tried to set invalid watchpoint at %"
704 VADDR_PRIx ", len=%" VADDR_PRIu, addr, len);
b4051334
AL
705 return -EINVAL;
706 }
7267c094 707 wp = g_malloc(sizeof(*wp));
a1d1bb31
AL
708
709 wp->vaddr = addr;
05068c0d 710 wp->len = len;
a1d1bb31
AL
711 wp->flags = flags;
712
2dc9f411 713 /* keep all GDB-injected watchpoints in front */
ff4700b0
AF
714 if (flags & BP_GDB) {
715 QTAILQ_INSERT_HEAD(&cpu->watchpoints, wp, entry);
716 } else {
717 QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
718 }
6658ffb8 719
31b030d4 720 tlb_flush_page(cpu, addr);
a1d1bb31
AL
721
722 if (watchpoint)
723 *watchpoint = wp;
724 return 0;
6658ffb8
PB
725}
726
a1d1bb31 727/* Remove a specific watchpoint. */
75a34036 728int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
a1d1bb31 729 int flags)
6658ffb8 730{
a1d1bb31 731 CPUWatchpoint *wp;
6658ffb8 732
ff4700b0 733 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
05068c0d 734 if (addr == wp->vaddr && len == wp->len
6e140f28 735 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
75a34036 736 cpu_watchpoint_remove_by_ref(cpu, wp);
6658ffb8
PB
737 return 0;
738 }
739 }
a1d1bb31 740 return -ENOENT;
6658ffb8
PB
741}
742
a1d1bb31 743/* Remove a specific watchpoint by reference. */
75a34036 744void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
a1d1bb31 745{
ff4700b0 746 QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry);
7d03f82f 747
31b030d4 748 tlb_flush_page(cpu, watchpoint->vaddr);
a1d1bb31 749
7267c094 750 g_free(watchpoint);
a1d1bb31
AL
751}
752
753/* Remove all matching watchpoints. */
75a34036 754void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
a1d1bb31 755{
c0ce998e 756 CPUWatchpoint *wp, *next;
a1d1bb31 757
ff4700b0 758 QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) {
75a34036
AF
759 if (wp->flags & mask) {
760 cpu_watchpoint_remove_by_ref(cpu, wp);
761 }
c0ce998e 762 }
7d03f82f 763}
05068c0d
PM
764
765/* Return true if this watchpoint address matches the specified
766 * access (ie the address range covered by the watchpoint overlaps
767 * partially or completely with the address range covered by the
768 * access).
769 */
770static inline bool cpu_watchpoint_address_matches(CPUWatchpoint *wp,
771 vaddr addr,
772 vaddr len)
773{
774 /* We know the lengths are non-zero, but a little caution is
775 * required to avoid errors in the case where the range ends
776 * exactly at the top of the address space and so addr + len
777 * wraps round to zero.
778 */
779 vaddr wpend = wp->vaddr + wp->len - 1;
780 vaddr addrend = addr + len - 1;
781
782 return !(addr > wpend || wp->vaddr > addrend);
783}
784
c527ee8f 785#endif
7d03f82f 786
a1d1bb31 787/* Add a breakpoint. */
b3310ab3 788int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
a1d1bb31 789 CPUBreakpoint **breakpoint)
4c3a88a2 790{
c0ce998e 791 CPUBreakpoint *bp;
3b46e624 792
7267c094 793 bp = g_malloc(sizeof(*bp));
4c3a88a2 794
a1d1bb31
AL
795 bp->pc = pc;
796 bp->flags = flags;
797
2dc9f411 798 /* keep all GDB-injected breakpoints in front */
00b941e5 799 if (flags & BP_GDB) {
f0c3c505 800 QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
00b941e5 801 } else {
f0c3c505 802 QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
00b941e5 803 }
3b46e624 804
f0c3c505 805 breakpoint_invalidate(cpu, pc);
a1d1bb31 806
00b941e5 807 if (breakpoint) {
a1d1bb31 808 *breakpoint = bp;
00b941e5 809 }
4c3a88a2 810 return 0;
4c3a88a2
FB
811}
812
a1d1bb31 813/* Remove a specific breakpoint. */
b3310ab3 814int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
a1d1bb31 815{
a1d1bb31
AL
816 CPUBreakpoint *bp;
817
f0c3c505 818 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
a1d1bb31 819 if (bp->pc == pc && bp->flags == flags) {
b3310ab3 820 cpu_breakpoint_remove_by_ref(cpu, bp);
a1d1bb31
AL
821 return 0;
822 }
7d03f82f 823 }
a1d1bb31 824 return -ENOENT;
7d03f82f
EI
825}
826
a1d1bb31 827/* Remove a specific breakpoint by reference. */
b3310ab3 828void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint)
4c3a88a2 829{
f0c3c505
AF
830 QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry);
831
832 breakpoint_invalidate(cpu, breakpoint->pc);
a1d1bb31 833
7267c094 834 g_free(breakpoint);
a1d1bb31
AL
835}
836
837/* Remove all matching breakpoints. */
b3310ab3 838void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
a1d1bb31 839{
c0ce998e 840 CPUBreakpoint *bp, *next;
a1d1bb31 841
f0c3c505 842 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
b3310ab3
AF
843 if (bp->flags & mask) {
844 cpu_breakpoint_remove_by_ref(cpu, bp);
845 }
c0ce998e 846 }
4c3a88a2
FB
847}
848
c33a346e
FB
849/* enable or disable single step mode. EXCP_DEBUG is returned by the
850 CPU loop after each instruction */
3825b28f 851void cpu_single_step(CPUState *cpu, int enabled)
c33a346e 852{
ed2803da
AF
853 if (cpu->singlestep_enabled != enabled) {
854 cpu->singlestep_enabled = enabled;
855 if (kvm_enabled()) {
38e478ec 856 kvm_update_guest_debug(cpu, 0);
ed2803da 857 } else {
ccbb4d44 858 /* must flush all the translated code to avoid inconsistencies */
e22a25c9 859 /* XXX: only flush what is necessary */
bbd77c18 860 tb_flush(cpu);
e22a25c9 861 }
c33a346e 862 }
c33a346e
FB
863}
864
a47dddd7 865void cpu_abort(CPUState *cpu, const char *fmt, ...)
7501267e
FB
866{
867 va_list ap;
493ae1f0 868 va_list ap2;
7501267e
FB
869
870 va_start(ap, fmt);
493ae1f0 871 va_copy(ap2, ap);
7501267e
FB
872 fprintf(stderr, "qemu: fatal: ");
873 vfprintf(stderr, fmt, ap);
874 fprintf(stderr, "\n");
878096ee 875 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
93fcfe39
AL
876 if (qemu_log_enabled()) {
877 qemu_log("qemu: fatal: ");
878 qemu_log_vprintf(fmt, ap2);
879 qemu_log("\n");
a0762859 880 log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP);
31b1a7b4 881 qemu_log_flush();
93fcfe39 882 qemu_log_close();
924edcae 883 }
493ae1f0 884 va_end(ap2);
f9373291 885 va_end(ap);
7615936e 886 replay_finish();
fd052bf6
RV
887#if defined(CONFIG_USER_ONLY)
888 {
889 struct sigaction act;
890 sigfillset(&act.sa_mask);
891 act.sa_handler = SIG_DFL;
892 sigaction(SIGABRT, &act, NULL);
893 }
894#endif
7501267e
FB
895 abort();
896}
897
0124311e 898#if !defined(CONFIG_USER_ONLY)
0dc3f44a 899/* Called from RCU critical section */
041603fe
PB
900static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
901{
902 RAMBlock *block;
903
43771539 904 block = atomic_rcu_read(&ram_list.mru_block);
9b8424d5 905 if (block && addr - block->offset < block->max_length) {
68851b98 906 return block;
041603fe 907 }
0dc3f44a 908 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
9b8424d5 909 if (addr - block->offset < block->max_length) {
041603fe
PB
910 goto found;
911 }
912 }
913
914 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
915 abort();
916
917found:
43771539
PB
918 /* It is safe to write mru_block outside the iothread lock. This
919 * is what happens:
920 *
921 * mru_block = xxx
922 * rcu_read_unlock()
923 * xxx removed from list
924 * rcu_read_lock()
925 * read mru_block
926 * mru_block = NULL;
927 * call_rcu(reclaim_ramblock, xxx);
928 * rcu_read_unlock()
929 *
930 * atomic_rcu_set is not needed here. The block was already published
931 * when it was placed into the list. Here we're just making an extra
932 * copy of the pointer.
933 */
041603fe
PB
934 ram_list.mru_block = block;
935 return block;
936}
937
a2f4d5be 938static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
d24981d3 939{
9a13565d 940 CPUState *cpu;
041603fe 941 ram_addr_t start1;
a2f4d5be
JQ
942 RAMBlock *block;
943 ram_addr_t end;
944
945 end = TARGET_PAGE_ALIGN(start + length);
946 start &= TARGET_PAGE_MASK;
d24981d3 947
0dc3f44a 948 rcu_read_lock();
041603fe
PB
949 block = qemu_get_ram_block(start);
950 assert(block == qemu_get_ram_block(end - 1));
1240be24 951 start1 = (uintptr_t)ramblock_ptr(block, start - block->offset);
9a13565d
PC
952 CPU_FOREACH(cpu) {
953 tlb_reset_dirty(cpu, start1, length);
954 }
0dc3f44a 955 rcu_read_unlock();
d24981d3
JQ
956}
957
5579c7f3 958/* Note: start and end must be within the same ram block. */
03eebc9e
SH
959bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
960 ram_addr_t length,
961 unsigned client)
1ccde1cb 962{
03eebc9e
SH
963 unsigned long end, page;
964 bool dirty;
965
966 if (length == 0) {
967 return false;
968 }
f23db169 969
03eebc9e
SH
970 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
971 page = start >> TARGET_PAGE_BITS;
972 dirty = bitmap_test_and_clear_atomic(ram_list.dirty_memory[client],
973 page, end - page);
974
975 if (dirty && tcg_enabled()) {
a2f4d5be 976 tlb_reset_dirty_range_all(start, length);
5579c7f3 977 }
03eebc9e
SH
978
979 return dirty;
1ccde1cb
FB
980}
981
79e2b9ae 982/* Called from RCU critical section */
bb0e627a 983hwaddr memory_region_section_get_iotlb(CPUState *cpu,
149f54b5
PB
984 MemoryRegionSection *section,
985 target_ulong vaddr,
986 hwaddr paddr, hwaddr xlat,
987 int prot,
988 target_ulong *address)
e5548617 989{
a8170e5e 990 hwaddr iotlb;
e5548617
BS
991 CPUWatchpoint *wp;
992
cc5bea60 993 if (memory_region_is_ram(section->mr)) {
e5548617
BS
994 /* Normal RAM. */
995 iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
149f54b5 996 + xlat;
e5548617 997 if (!section->readonly) {
b41aac4f 998 iotlb |= PHYS_SECTION_NOTDIRTY;
e5548617 999 } else {
b41aac4f 1000 iotlb |= PHYS_SECTION_ROM;
e5548617
BS
1001 }
1002 } else {
0b8e2c10
PM
1003 AddressSpaceDispatch *d;
1004
1005 d = atomic_rcu_read(&section->address_space->dispatch);
1006 iotlb = section - d->map.sections;
149f54b5 1007 iotlb += xlat;
e5548617
BS
1008 }
1009
1010 /* Make accesses to pages with watchpoints go via the
1011 watchpoint trap routines. */
ff4700b0 1012 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
05068c0d 1013 if (cpu_watchpoint_address_matches(wp, vaddr, TARGET_PAGE_SIZE)) {
e5548617
BS
1014 /* Avoid trapping reads of pages with a write breakpoint. */
1015 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
b41aac4f 1016 iotlb = PHYS_SECTION_WATCH + paddr;
e5548617
BS
1017 *address |= TLB_MMIO;
1018 break;
1019 }
1020 }
1021 }
1022
1023 return iotlb;
1024}
9fa3e853
FB
1025#endif /* defined(CONFIG_USER_ONLY) */
1026
e2eef170 1027#if !defined(CONFIG_USER_ONLY)
8da3ff18 1028
c227f099 1029static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
5312bd8b 1030 uint16_t section);
acc9d80b 1031static subpage_t *subpage_init(AddressSpace *as, hwaddr base);
54688b1e 1032
a2b257d6
IM
1033static void *(*phys_mem_alloc)(size_t size, uint64_t *align) =
1034 qemu_anon_ram_alloc;
91138037
MA
1035
1036/*
1037 * Set a custom physical guest memory alloator.
1038 * Accelerators with unusual needs may need this. Hopefully, we can
1039 * get rid of it eventually.
1040 */
a2b257d6 1041void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align))
91138037
MA
1042{
1043 phys_mem_alloc = alloc;
1044}
1045
53cb28cb
MA
1046static uint16_t phys_section_add(PhysPageMap *map,
1047 MemoryRegionSection *section)
5312bd8b 1048{
68f3f65b
PB
1049 /* The physical section number is ORed with a page-aligned
1050 * pointer to produce the iotlb entries. Thus it should
1051 * never overflow into the page-aligned value.
1052 */
53cb28cb 1053 assert(map->sections_nb < TARGET_PAGE_SIZE);
68f3f65b 1054
53cb28cb
MA
1055 if (map->sections_nb == map->sections_nb_alloc) {
1056 map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16);
1057 map->sections = g_renew(MemoryRegionSection, map->sections,
1058 map->sections_nb_alloc);
5312bd8b 1059 }
53cb28cb 1060 map->sections[map->sections_nb] = *section;
dfde4e6e 1061 memory_region_ref(section->mr);
53cb28cb 1062 return map->sections_nb++;
5312bd8b
AK
1063}
1064
058bc4b5
PB
1065static void phys_section_destroy(MemoryRegion *mr)
1066{
dfde4e6e
PB
1067 memory_region_unref(mr);
1068
058bc4b5
PB
1069 if (mr->subpage) {
1070 subpage_t *subpage = container_of(mr, subpage_t, iomem);
b4fefef9 1071 object_unref(OBJECT(&subpage->iomem));
058bc4b5
PB
1072 g_free(subpage);
1073 }
1074}
1075
6092666e 1076static void phys_sections_free(PhysPageMap *map)
5312bd8b 1077{
9affd6fc
PB
1078 while (map->sections_nb > 0) {
1079 MemoryRegionSection *section = &map->sections[--map->sections_nb];
058bc4b5
PB
1080 phys_section_destroy(section->mr);
1081 }
9affd6fc
PB
1082 g_free(map->sections);
1083 g_free(map->nodes);
5312bd8b
AK
1084}
1085
ac1970fb 1086static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
0f0cb164
AK
1087{
1088 subpage_t *subpage;
a8170e5e 1089 hwaddr base = section->offset_within_address_space
0f0cb164 1090 & TARGET_PAGE_MASK;
97115a8d 1091 MemoryRegionSection *existing = phys_page_find(d->phys_map, base,
53cb28cb 1092 d->map.nodes, d->map.sections);
0f0cb164
AK
1093 MemoryRegionSection subsection = {
1094 .offset_within_address_space = base,
052e87b0 1095 .size = int128_make64(TARGET_PAGE_SIZE),
0f0cb164 1096 };
a8170e5e 1097 hwaddr start, end;
0f0cb164 1098
f3705d53 1099 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
0f0cb164 1100
f3705d53 1101 if (!(existing->mr->subpage)) {
acc9d80b 1102 subpage = subpage_init(d->as, base);
3be91e86 1103 subsection.address_space = d->as;
0f0cb164 1104 subsection.mr = &subpage->iomem;
ac1970fb 1105 phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
53cb28cb 1106 phys_section_add(&d->map, &subsection));
0f0cb164 1107 } else {
f3705d53 1108 subpage = container_of(existing->mr, subpage_t, iomem);
0f0cb164
AK
1109 }
1110 start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
052e87b0 1111 end = start + int128_get64(section->size) - 1;
53cb28cb
MA
1112 subpage_register(subpage, start, end,
1113 phys_section_add(&d->map, section));
0f0cb164
AK
1114}
1115
1116
052e87b0
PB
1117static void register_multipage(AddressSpaceDispatch *d,
1118 MemoryRegionSection *section)
33417e70 1119{
a8170e5e 1120 hwaddr start_addr = section->offset_within_address_space;
53cb28cb 1121 uint16_t section_index = phys_section_add(&d->map, section);
052e87b0
PB
1122 uint64_t num_pages = int128_get64(int128_rshift(section->size,
1123 TARGET_PAGE_BITS));
dd81124b 1124
733d5ef5
PB
1125 assert(num_pages);
1126 phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
33417e70
FB
1127}
1128
ac1970fb 1129static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
0f0cb164 1130{
89ae337a 1131 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
00752703 1132 AddressSpaceDispatch *d = as->next_dispatch;
99b9cc06 1133 MemoryRegionSection now = *section, remain = *section;
052e87b0 1134 Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
0f0cb164 1135
733d5ef5
PB
1136 if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
1137 uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
1138 - now.offset_within_address_space;
1139
052e87b0 1140 now.size = int128_min(int128_make64(left), now.size);
ac1970fb 1141 register_subpage(d, &now);
733d5ef5 1142 } else {
052e87b0 1143 now.size = int128_zero();
733d5ef5 1144 }
052e87b0
PB
1145 while (int128_ne(remain.size, now.size)) {
1146 remain.size = int128_sub(remain.size, now.size);
1147 remain.offset_within_address_space += int128_get64(now.size);
1148 remain.offset_within_region += int128_get64(now.size);
69b67646 1149 now = remain;
052e87b0 1150 if (int128_lt(remain.size, page_size)) {
733d5ef5 1151 register_subpage(d, &now);
88266249 1152 } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
052e87b0 1153 now.size = page_size;
ac1970fb 1154 register_subpage(d, &now);
69b67646 1155 } else {
052e87b0 1156 now.size = int128_and(now.size, int128_neg(page_size));
ac1970fb 1157 register_multipage(d, &now);
69b67646 1158 }
0f0cb164
AK
1159 }
1160}
1161
62a2744c
SY
1162void qemu_flush_coalesced_mmio_buffer(void)
1163{
1164 if (kvm_enabled())
1165 kvm_flush_coalesced_mmio_buffer();
1166}
1167
b2a8658e
UD
1168void qemu_mutex_lock_ramlist(void)
1169{
1170 qemu_mutex_lock(&ram_list.mutex);
1171}
1172
1173void qemu_mutex_unlock_ramlist(void)
1174{
1175 qemu_mutex_unlock(&ram_list.mutex);
1176}
1177
e1e84ba0 1178#ifdef __linux__
c902760f
MT
1179
1180#include <sys/vfs.h>
1181
1182#define HUGETLBFS_MAGIC 0x958458f6
1183
fc7a5800 1184static long gethugepagesize(const char *path, Error **errp)
c902760f
MT
1185{
1186 struct statfs fs;
1187 int ret;
1188
1189 do {
9742bf26 1190 ret = statfs(path, &fs);
c902760f
MT
1191 } while (ret != 0 && errno == EINTR);
1192
1193 if (ret != 0) {
fc7a5800
HT
1194 error_setg_errno(errp, errno, "failed to get page size of file %s",
1195 path);
9742bf26 1196 return 0;
c902760f
MT
1197 }
1198
c902760f
MT
1199 return fs.f_bsize;
1200}
1201
04b16653
AW
1202static void *file_ram_alloc(RAMBlock *block,
1203 ram_addr_t memory,
7f56e740
PB
1204 const char *path,
1205 Error **errp)
c902760f 1206{
8d31d6b6 1207 struct stat st;
c902760f 1208 char *filename;
8ca761f6
PF
1209 char *sanitized_name;
1210 char *c;
794e8f30 1211 void *area;
c902760f 1212 int fd;
557529dd 1213 uint64_t hpagesize;
fc7a5800 1214 Error *local_err = NULL;
c902760f 1215
fc7a5800
HT
1216 hpagesize = gethugepagesize(path, &local_err);
1217 if (local_err) {
1218 error_propagate(errp, local_err);
f9a49dfa 1219 goto error;
c902760f 1220 }
a2b257d6 1221 block->mr->align = hpagesize;
c902760f
MT
1222
1223 if (memory < hpagesize) {
557529dd
HT
1224 error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
1225 "or larger than huge page size 0x%" PRIx64,
1226 memory, hpagesize);
1227 goto error;
c902760f
MT
1228 }
1229
1230 if (kvm_enabled() && !kvm_has_sync_mmu()) {
7f56e740
PB
1231 error_setg(errp,
1232 "host lacks kvm mmu notifiers, -mem-path unsupported");
f9a49dfa 1233 goto error;
c902760f
MT
1234 }
1235
8d31d6b6
PF
1236 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
1237 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1238 sanitized_name = g_strdup(memory_region_name(block->mr));
1239 for (c = sanitized_name; *c != '\0'; c++) {
1240 if (*c == '/') {
1241 *c = '_';
1242 }
1243 }
8ca761f6 1244
8d31d6b6
PF
1245 filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
1246 sanitized_name);
1247 g_free(sanitized_name);
1248
1249 fd = mkstemp(filename);
1250 if (fd >= 0) {
1251 unlink(filename);
1252 }
1253 g_free(filename);
1254 } else {
1255 fd = open(path, O_RDWR | O_CREAT, 0644);
1256 }
c902760f 1257
c902760f 1258 if (fd < 0) {
7f56e740
PB
1259 error_setg_errno(errp, errno,
1260 "unable to create backing store for hugepages");
f9a49dfa 1261 goto error;
c902760f 1262 }
c902760f 1263
9284f319 1264 memory = ROUND_UP(memory, hpagesize);
c902760f
MT
1265
1266 /*
1267 * ftruncate is not supported by hugetlbfs in older
1268 * hosts, so don't bother bailing out on errors.
1269 * If anything goes wrong with it under other filesystems,
1270 * mmap will fail.
1271 */
7f56e740 1272 if (ftruncate(fd, memory)) {
9742bf26 1273 perror("ftruncate");
7f56e740 1274 }
c902760f 1275
794e8f30 1276 area = qemu_ram_mmap(fd, memory, hpagesize, block->flags & RAM_SHARED);
c902760f 1277 if (area == MAP_FAILED) {
7f56e740
PB
1278 error_setg_errno(errp, errno,
1279 "unable to map backing store for hugepages");
9742bf26 1280 close(fd);
f9a49dfa 1281 goto error;
c902760f 1282 }
ef36fa14
MT
1283
1284 if (mem_prealloc) {
38183310 1285 os_mem_prealloc(fd, area, memory);
ef36fa14
MT
1286 }
1287
04b16653 1288 block->fd = fd;
c902760f 1289 return area;
f9a49dfa
MT
1290
1291error:
f9a49dfa 1292 return NULL;
c902760f
MT
1293}
1294#endif
1295
0dc3f44a 1296/* Called with the ramlist lock held. */
d17b5288 1297static ram_addr_t find_ram_offset(ram_addr_t size)
04b16653
AW
1298{
1299 RAMBlock *block, *next_block;
3e837b2c 1300 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
04b16653 1301
49cd9ac6
SH
1302 assert(size != 0); /* it would hand out same offset multiple times */
1303
0dc3f44a 1304 if (QLIST_EMPTY_RCU(&ram_list.blocks)) {
04b16653 1305 return 0;
0d53d9fe 1306 }
04b16653 1307
0dc3f44a 1308 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
f15fbc4b 1309 ram_addr_t end, next = RAM_ADDR_MAX;
04b16653 1310
62be4e3a 1311 end = block->offset + block->max_length;
04b16653 1312
0dc3f44a 1313 QLIST_FOREACH_RCU(next_block, &ram_list.blocks, next) {
04b16653
AW
1314 if (next_block->offset >= end) {
1315 next = MIN(next, next_block->offset);
1316 }
1317 }
1318 if (next - end >= size && next - end < mingap) {
3e837b2c 1319 offset = end;
04b16653
AW
1320 mingap = next - end;
1321 }
1322 }
3e837b2c
AW
1323
1324 if (offset == RAM_ADDR_MAX) {
1325 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1326 (uint64_t)size);
1327 abort();
1328 }
1329
04b16653
AW
1330 return offset;
1331}
1332
652d7ec2 1333ram_addr_t last_ram_offset(void)
d17b5288
AW
1334{
1335 RAMBlock *block;
1336 ram_addr_t last = 0;
1337
0dc3f44a
MD
1338 rcu_read_lock();
1339 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
62be4e3a 1340 last = MAX(last, block->offset + block->max_length);
0d53d9fe 1341 }
0dc3f44a 1342 rcu_read_unlock();
d17b5288
AW
1343 return last;
1344}
1345
ddb97f1d
JB
1346static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1347{
1348 int ret;
ddb97f1d
JB
1349
1350 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
47c8ca53 1351 if (!machine_dump_guest_core(current_machine)) {
ddb97f1d
JB
1352 ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1353 if (ret) {
1354 perror("qemu_madvise");
1355 fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1356 "but dump_guest_core=off specified\n");
1357 }
1358 }
1359}
1360
0dc3f44a
MD
1361/* Called within an RCU critical section, or while the ramlist lock
1362 * is held.
1363 */
20cfe881 1364static RAMBlock *find_ram_block(ram_addr_t addr)
84b89d78 1365{
20cfe881 1366 RAMBlock *block;
84b89d78 1367
0dc3f44a 1368 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
c5705a77 1369 if (block->offset == addr) {
20cfe881 1370 return block;
c5705a77
AK
1371 }
1372 }
20cfe881
HT
1373
1374 return NULL;
1375}
1376
422148d3
DDAG
1377const char *qemu_ram_get_idstr(RAMBlock *rb)
1378{
1379 return rb->idstr;
1380}
1381
ae3a7047 1382/* Called with iothread lock held. */
20cfe881
HT
1383void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
1384{
ae3a7047 1385 RAMBlock *new_block, *block;
20cfe881 1386
0dc3f44a 1387 rcu_read_lock();
ae3a7047 1388 new_block = find_ram_block(addr);
c5705a77
AK
1389 assert(new_block);
1390 assert(!new_block->idstr[0]);
84b89d78 1391
09e5ab63
AL
1392 if (dev) {
1393 char *id = qdev_get_dev_path(dev);
84b89d78
CM
1394 if (id) {
1395 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
7267c094 1396 g_free(id);
84b89d78
CM
1397 }
1398 }
1399 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1400
0dc3f44a 1401 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
c5705a77 1402 if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
84b89d78
CM
1403 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1404 new_block->idstr);
1405 abort();
1406 }
1407 }
0dc3f44a 1408 rcu_read_unlock();
c5705a77
AK
1409}
1410
ae3a7047 1411/* Called with iothread lock held. */
20cfe881
HT
1412void qemu_ram_unset_idstr(ram_addr_t addr)
1413{
ae3a7047 1414 RAMBlock *block;
20cfe881 1415
ae3a7047
MD
1416 /* FIXME: arch_init.c assumes that this is not called throughout
1417 * migration. Ignore the problem since hot-unplug during migration
1418 * does not work anyway.
1419 */
1420
0dc3f44a 1421 rcu_read_lock();
ae3a7047 1422 block = find_ram_block(addr);
20cfe881
HT
1423 if (block) {
1424 memset(block->idstr, 0, sizeof(block->idstr));
1425 }
0dc3f44a 1426 rcu_read_unlock();
20cfe881
HT
1427}
1428
8490fc78
LC
1429static int memory_try_enable_merging(void *addr, size_t len)
1430{
75cc7f01 1431 if (!machine_mem_merge(current_machine)) {
8490fc78
LC
1432 /* disabled by the user */
1433 return 0;
1434 }
1435
1436 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1437}
1438
62be4e3a
MT
1439/* Only legal before guest might have detected the memory size: e.g. on
1440 * incoming migration, or right after reset.
1441 *
1442 * As memory core doesn't know how is memory accessed, it is up to
1443 * resize callback to update device state and/or add assertions to detect
1444 * misuse, if necessary.
1445 */
1446int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp)
1447{
1448 RAMBlock *block = find_ram_block(base);
1449
1450 assert(block);
1451
4ed023ce 1452 newsize = HOST_PAGE_ALIGN(newsize);
129ddaf3 1453
62be4e3a
MT
1454 if (block->used_length == newsize) {
1455 return 0;
1456 }
1457
1458 if (!(block->flags & RAM_RESIZEABLE)) {
1459 error_setg_errno(errp, EINVAL,
1460 "Length mismatch: %s: 0x" RAM_ADDR_FMT
1461 " in != 0x" RAM_ADDR_FMT, block->idstr,
1462 newsize, block->used_length);
1463 return -EINVAL;
1464 }
1465
1466 if (block->max_length < newsize) {
1467 error_setg_errno(errp, EINVAL,
1468 "Length too large: %s: 0x" RAM_ADDR_FMT
1469 " > 0x" RAM_ADDR_FMT, block->idstr,
1470 newsize, block->max_length);
1471 return -EINVAL;
1472 }
1473
1474 cpu_physical_memory_clear_dirty_range(block->offset, block->used_length);
1475 block->used_length = newsize;
58d2707e
PB
1476 cpu_physical_memory_set_dirty_range(block->offset, block->used_length,
1477 DIRTY_CLIENTS_ALL);
62be4e3a
MT
1478 memory_region_set_size(block->mr, newsize);
1479 if (block->resized) {
1480 block->resized(block->idstr, newsize, block->host);
1481 }
1482 return 0;
1483}
1484
ef701d7b 1485static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
c5705a77 1486{
e1c57ab8 1487 RAMBlock *block;
0d53d9fe 1488 RAMBlock *last_block = NULL;
2152f5ca
JQ
1489 ram_addr_t old_ram_size, new_ram_size;
1490
1491 old_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
c5705a77 1492
b2a8658e 1493 qemu_mutex_lock_ramlist();
9b8424d5 1494 new_block->offset = find_ram_offset(new_block->max_length);
e1c57ab8
PB
1495
1496 if (!new_block->host) {
1497 if (xen_enabled()) {
9b8424d5
MT
1498 xen_ram_alloc(new_block->offset, new_block->max_length,
1499 new_block->mr);
e1c57ab8 1500 } else {
9b8424d5 1501 new_block->host = phys_mem_alloc(new_block->max_length,
a2b257d6 1502 &new_block->mr->align);
39228250 1503 if (!new_block->host) {
ef701d7b
HT
1504 error_setg_errno(errp, errno,
1505 "cannot set up guest memory '%s'",
1506 memory_region_name(new_block->mr));
1507 qemu_mutex_unlock_ramlist();
1508 return -1;
39228250 1509 }
9b8424d5 1510 memory_try_enable_merging(new_block->host, new_block->max_length);
6977dfe6 1511 }
c902760f 1512 }
94a6b54f 1513
dd631697
LZ
1514 new_ram_size = MAX(old_ram_size,
1515 (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
1516 if (new_ram_size > old_ram_size) {
1517 migration_bitmap_extend(old_ram_size, new_ram_size);
1518 }
0d53d9fe
MD
1519 /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ,
1520 * QLIST (which has an RCU-friendly variant) does not have insertion at
1521 * tail, so save the last element in last_block.
1522 */
0dc3f44a 1523 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
0d53d9fe 1524 last_block = block;
9b8424d5 1525 if (block->max_length < new_block->max_length) {
abb26d63
PB
1526 break;
1527 }
1528 }
1529 if (block) {
0dc3f44a 1530 QLIST_INSERT_BEFORE_RCU(block, new_block, next);
0d53d9fe 1531 } else if (last_block) {
0dc3f44a 1532 QLIST_INSERT_AFTER_RCU(last_block, new_block, next);
0d53d9fe 1533 } else { /* list is empty */
0dc3f44a 1534 QLIST_INSERT_HEAD_RCU(&ram_list.blocks, new_block, next);
abb26d63 1535 }
0d6d3c87 1536 ram_list.mru_block = NULL;
94a6b54f 1537
0dc3f44a
MD
1538 /* Write list before version */
1539 smp_wmb();
f798b07f 1540 ram_list.version++;
b2a8658e 1541 qemu_mutex_unlock_ramlist();
f798b07f 1542
2152f5ca
JQ
1543 new_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
1544
1545 if (new_ram_size > old_ram_size) {
1ab4c8ce 1546 int i;
ae3a7047
MD
1547
1548 /* ram_list.dirty_memory[] is protected by the iothread lock. */
1ab4c8ce
JQ
1549 for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
1550 ram_list.dirty_memory[i] =
1551 bitmap_zero_extend(ram_list.dirty_memory[i],
1552 old_ram_size, new_ram_size);
1553 }
2152f5ca 1554 }
9b8424d5 1555 cpu_physical_memory_set_dirty_range(new_block->offset,
58d2707e
PB
1556 new_block->used_length,
1557 DIRTY_CLIENTS_ALL);
94a6b54f 1558
a904c911
PB
1559 if (new_block->host) {
1560 qemu_ram_setup_dump(new_block->host, new_block->max_length);
1561 qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
1562 qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK);
1563 if (kvm_enabled()) {
1564 kvm_setup_guest_memory(new_block->host, new_block->max_length);
1565 }
e1c57ab8 1566 }
6f0437e8 1567
94a6b54f
PB
1568 return new_block->offset;
1569}
e9a1ab19 1570
0b183fc8 1571#ifdef __linux__
e1c57ab8 1572ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
dbcb8981 1573 bool share, const char *mem_path,
7f56e740 1574 Error **errp)
e1c57ab8
PB
1575{
1576 RAMBlock *new_block;
ef701d7b
HT
1577 ram_addr_t addr;
1578 Error *local_err = NULL;
e1c57ab8
PB
1579
1580 if (xen_enabled()) {
7f56e740
PB
1581 error_setg(errp, "-mem-path not supported with Xen");
1582 return -1;
e1c57ab8
PB
1583 }
1584
1585 if (phys_mem_alloc != qemu_anon_ram_alloc) {
1586 /*
1587 * file_ram_alloc() needs to allocate just like
1588 * phys_mem_alloc, but we haven't bothered to provide
1589 * a hook there.
1590 */
7f56e740
PB
1591 error_setg(errp,
1592 "-mem-path not supported with this accelerator");
1593 return -1;
e1c57ab8
PB
1594 }
1595
4ed023ce 1596 size = HOST_PAGE_ALIGN(size);
e1c57ab8
PB
1597 new_block = g_malloc0(sizeof(*new_block));
1598 new_block->mr = mr;
9b8424d5
MT
1599 new_block->used_length = size;
1600 new_block->max_length = size;
dbcb8981 1601 new_block->flags = share ? RAM_SHARED : 0;
794e8f30 1602 new_block->flags |= RAM_FILE;
7f56e740
PB
1603 new_block->host = file_ram_alloc(new_block, size,
1604 mem_path, errp);
1605 if (!new_block->host) {
1606 g_free(new_block);
1607 return -1;
1608 }
1609
ef701d7b
HT
1610 addr = ram_block_add(new_block, &local_err);
1611 if (local_err) {
1612 g_free(new_block);
1613 error_propagate(errp, local_err);
1614 return -1;
1615 }
1616 return addr;
e1c57ab8 1617}
0b183fc8 1618#endif
e1c57ab8 1619
62be4e3a
MT
1620static
1621ram_addr_t qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
1622 void (*resized)(const char*,
1623 uint64_t length,
1624 void *host),
1625 void *host, bool resizeable,
ef701d7b 1626 MemoryRegion *mr, Error **errp)
e1c57ab8
PB
1627{
1628 RAMBlock *new_block;
ef701d7b
HT
1629 ram_addr_t addr;
1630 Error *local_err = NULL;
e1c57ab8 1631
4ed023ce
DDAG
1632 size = HOST_PAGE_ALIGN(size);
1633 max_size = HOST_PAGE_ALIGN(max_size);
e1c57ab8
PB
1634 new_block = g_malloc0(sizeof(*new_block));
1635 new_block->mr = mr;
62be4e3a 1636 new_block->resized = resized;
9b8424d5
MT
1637 new_block->used_length = size;
1638 new_block->max_length = max_size;
62be4e3a 1639 assert(max_size >= size);
e1c57ab8
PB
1640 new_block->fd = -1;
1641 new_block->host = host;
1642 if (host) {
7bd4f430 1643 new_block->flags |= RAM_PREALLOC;
e1c57ab8 1644 }
62be4e3a
MT
1645 if (resizeable) {
1646 new_block->flags |= RAM_RESIZEABLE;
1647 }
ef701d7b
HT
1648 addr = ram_block_add(new_block, &local_err);
1649 if (local_err) {
1650 g_free(new_block);
1651 error_propagate(errp, local_err);
1652 return -1;
1653 }
1654 return addr;
e1c57ab8
PB
1655}
1656
62be4e3a
MT
1657ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1658 MemoryRegion *mr, Error **errp)
1659{
1660 return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp);
1661}
1662
ef701d7b 1663ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp)
6977dfe6 1664{
62be4e3a
MT
1665 return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp);
1666}
1667
1668ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
1669 void (*resized)(const char*,
1670 uint64_t length,
1671 void *host),
1672 MemoryRegion *mr, Error **errp)
1673{
1674 return qemu_ram_alloc_internal(size, maxsz, resized, NULL, true, mr, errp);
6977dfe6
YT
1675}
1676
1f2e98b6
AW
1677void qemu_ram_free_from_ptr(ram_addr_t addr)
1678{
1679 RAMBlock *block;
1680
b2a8658e 1681 qemu_mutex_lock_ramlist();
0dc3f44a 1682 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1f2e98b6 1683 if (addr == block->offset) {
0dc3f44a 1684 QLIST_REMOVE_RCU(block, next);
0d6d3c87 1685 ram_list.mru_block = NULL;
0dc3f44a
MD
1686 /* Write list before version */
1687 smp_wmb();
f798b07f 1688 ram_list.version++;
43771539 1689 g_free_rcu(block, rcu);
b2a8658e 1690 break;
1f2e98b6
AW
1691 }
1692 }
b2a8658e 1693 qemu_mutex_unlock_ramlist();
1f2e98b6
AW
1694}
1695
43771539
PB
1696static void reclaim_ramblock(RAMBlock *block)
1697{
1698 if (block->flags & RAM_PREALLOC) {
1699 ;
1700 } else if (xen_enabled()) {
1701 xen_invalidate_map_cache_entry(block->host);
1702#ifndef _WIN32
1703 } else if (block->fd >= 0) {
794e8f30
MT
1704 if (block->flags & RAM_FILE) {
1705 qemu_ram_munmap(block->host, block->max_length);
8561c924
MT
1706 } else {
1707 munmap(block->host, block->max_length);
1708 }
43771539
PB
1709 close(block->fd);
1710#endif
1711 } else {
1712 qemu_anon_ram_free(block->host, block->max_length);
1713 }
1714 g_free(block);
1715}
1716
c227f099 1717void qemu_ram_free(ram_addr_t addr)
e9a1ab19 1718{
04b16653
AW
1719 RAMBlock *block;
1720
b2a8658e 1721 qemu_mutex_lock_ramlist();
0dc3f44a 1722 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
04b16653 1723 if (addr == block->offset) {
0dc3f44a 1724 QLIST_REMOVE_RCU(block, next);
0d6d3c87 1725 ram_list.mru_block = NULL;
0dc3f44a
MD
1726 /* Write list before version */
1727 smp_wmb();
f798b07f 1728 ram_list.version++;
43771539 1729 call_rcu(block, reclaim_ramblock, rcu);
b2a8658e 1730 break;
04b16653
AW
1731 }
1732 }
b2a8658e 1733 qemu_mutex_unlock_ramlist();
e9a1ab19
FB
1734}
1735
cd19cfa2
HY
1736#ifndef _WIN32
1737void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1738{
1739 RAMBlock *block;
1740 ram_addr_t offset;
1741 int flags;
1742 void *area, *vaddr;
1743
0dc3f44a 1744 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
cd19cfa2 1745 offset = addr - block->offset;
9b8424d5 1746 if (offset < block->max_length) {
1240be24 1747 vaddr = ramblock_ptr(block, offset);
7bd4f430 1748 if (block->flags & RAM_PREALLOC) {
cd19cfa2 1749 ;
dfeaf2ab
MA
1750 } else if (xen_enabled()) {
1751 abort();
cd19cfa2
HY
1752 } else {
1753 flags = MAP_FIXED;
3435f395 1754 if (block->fd >= 0) {
dbcb8981
PB
1755 flags |= (block->flags & RAM_SHARED ?
1756 MAP_SHARED : MAP_PRIVATE);
3435f395
MA
1757 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1758 flags, block->fd, offset);
cd19cfa2 1759 } else {
2eb9fbaa
MA
1760 /*
1761 * Remap needs to match alloc. Accelerators that
1762 * set phys_mem_alloc never remap. If they did,
1763 * we'd need a remap hook here.
1764 */
1765 assert(phys_mem_alloc == qemu_anon_ram_alloc);
1766
cd19cfa2
HY
1767 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1768 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1769 flags, -1, 0);
cd19cfa2
HY
1770 }
1771 if (area != vaddr) {
f15fbc4b
AP
1772 fprintf(stderr, "Could not remap addr: "
1773 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
cd19cfa2
HY
1774 length, addr);
1775 exit(1);
1776 }
8490fc78 1777 memory_try_enable_merging(vaddr, length);
ddb97f1d 1778 qemu_ram_setup_dump(vaddr, length);
cd19cfa2 1779 }
cd19cfa2
HY
1780 }
1781 }
1782}
1783#endif /* !_WIN32 */
1784
a35ba7be
PB
1785int qemu_get_ram_fd(ram_addr_t addr)
1786{
ae3a7047
MD
1787 RAMBlock *block;
1788 int fd;
a35ba7be 1789
0dc3f44a 1790 rcu_read_lock();
ae3a7047
MD
1791 block = qemu_get_ram_block(addr);
1792 fd = block->fd;
0dc3f44a 1793 rcu_read_unlock();
ae3a7047 1794 return fd;
a35ba7be
PB
1795}
1796
3fd74b84
DM
1797void *qemu_get_ram_block_host_ptr(ram_addr_t addr)
1798{
ae3a7047
MD
1799 RAMBlock *block;
1800 void *ptr;
3fd74b84 1801
0dc3f44a 1802 rcu_read_lock();
ae3a7047
MD
1803 block = qemu_get_ram_block(addr);
1804 ptr = ramblock_ptr(block, 0);
0dc3f44a 1805 rcu_read_unlock();
ae3a7047 1806 return ptr;
3fd74b84
DM
1807}
1808
1b5ec234 1809/* Return a host pointer to ram allocated with qemu_ram_alloc.
ae3a7047
MD
1810 * This should not be used for general purpose DMA. Use address_space_map
1811 * or address_space_rw instead. For local memory (e.g. video ram) that the
1812 * device owns, use memory_region_get_ram_ptr.
0dc3f44a
MD
1813 *
1814 * By the time this function returns, the returned pointer is not protected
1815 * by RCU anymore. If the caller is not within an RCU critical section and
1816 * does not hold the iothread lock, it must have other means of protecting the
1817 * pointer, such as a reference to the region that includes the incoming
1818 * ram_addr_t.
1b5ec234
PB
1819 */
1820void *qemu_get_ram_ptr(ram_addr_t addr)
1821{
ae3a7047
MD
1822 RAMBlock *block;
1823 void *ptr;
1b5ec234 1824
0dc3f44a 1825 rcu_read_lock();
ae3a7047
MD
1826 block = qemu_get_ram_block(addr);
1827
1828 if (xen_enabled() && block->host == NULL) {
0d6d3c87
PB
1829 /* We need to check if the requested address is in the RAM
1830 * because we don't want to map the entire memory in QEMU.
1831 * In that case just map until the end of the page.
1832 */
1833 if (block->offset == 0) {
ae3a7047 1834 ptr = xen_map_cache(addr, 0, 0);
0dc3f44a 1835 goto unlock;
0d6d3c87 1836 }
ae3a7047
MD
1837
1838 block->host = xen_map_cache(block->offset, block->max_length, 1);
0d6d3c87 1839 }
ae3a7047
MD
1840 ptr = ramblock_ptr(block, addr - block->offset);
1841
0dc3f44a
MD
1842unlock:
1843 rcu_read_unlock();
ae3a7047 1844 return ptr;
dc828ca1
PB
1845}
1846
38bee5dc 1847/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
ae3a7047 1848 * but takes a size argument.
0dc3f44a
MD
1849 *
1850 * By the time this function returns, the returned pointer is not protected
1851 * by RCU anymore. If the caller is not within an RCU critical section and
1852 * does not hold the iothread lock, it must have other means of protecting the
1853 * pointer, such as a reference to the region that includes the incoming
1854 * ram_addr_t.
ae3a7047 1855 */
cb85f7ab 1856static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size)
38bee5dc 1857{
ae3a7047 1858 void *ptr;
8ab934f9
SS
1859 if (*size == 0) {
1860 return NULL;
1861 }
868bb33f 1862 if (xen_enabled()) {
e41d7c69 1863 return xen_map_cache(addr, *size, 1);
868bb33f 1864 } else {
38bee5dc 1865 RAMBlock *block;
0dc3f44a
MD
1866 rcu_read_lock();
1867 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
9b8424d5
MT
1868 if (addr - block->offset < block->max_length) {
1869 if (addr - block->offset + *size > block->max_length)
1870 *size = block->max_length - addr + block->offset;
ae3a7047 1871 ptr = ramblock_ptr(block, addr - block->offset);
0dc3f44a 1872 rcu_read_unlock();
ae3a7047 1873 return ptr;
38bee5dc
SS
1874 }
1875 }
1876
1877 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1878 abort();
38bee5dc
SS
1879 }
1880}
1881
422148d3
DDAG
1882/*
1883 * Translates a host ptr back to a RAMBlock, a ram_addr and an offset
1884 * in that RAMBlock.
1885 *
1886 * ptr: Host pointer to look up
1887 * round_offset: If true round the result offset down to a page boundary
1888 * *ram_addr: set to result ram_addr
1889 * *offset: set to result offset within the RAMBlock
1890 *
1891 * Returns: RAMBlock (or NULL if not found)
ae3a7047
MD
1892 *
1893 * By the time this function returns, the returned pointer is not protected
1894 * by RCU anymore. If the caller is not within an RCU critical section and
1895 * does not hold the iothread lock, it must have other means of protecting the
1896 * pointer, such as a reference to the region that includes the incoming
1897 * ram_addr_t.
1898 */
422148d3
DDAG
1899RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
1900 ram_addr_t *ram_addr,
1901 ram_addr_t *offset)
5579c7f3 1902{
94a6b54f
PB
1903 RAMBlock *block;
1904 uint8_t *host = ptr;
1905
868bb33f 1906 if (xen_enabled()) {
0dc3f44a 1907 rcu_read_lock();
e41d7c69 1908 *ram_addr = xen_ram_addr_from_mapcache(ptr);
422148d3
DDAG
1909 block = qemu_get_ram_block(*ram_addr);
1910 if (block) {
1911 *offset = (host - block->host);
1912 }
0dc3f44a 1913 rcu_read_unlock();
422148d3 1914 return block;
712c2b41
SS
1915 }
1916
0dc3f44a
MD
1917 rcu_read_lock();
1918 block = atomic_rcu_read(&ram_list.mru_block);
9b8424d5 1919 if (block && block->host && host - block->host < block->max_length) {
23887b79
PB
1920 goto found;
1921 }
1922
0dc3f44a 1923 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
432d268c
JN
1924 /* This case append when the block is not mapped. */
1925 if (block->host == NULL) {
1926 continue;
1927 }
9b8424d5 1928 if (host - block->host < block->max_length) {
23887b79 1929 goto found;
f471a17e 1930 }
94a6b54f 1931 }
432d268c 1932
0dc3f44a 1933 rcu_read_unlock();
1b5ec234 1934 return NULL;
23887b79
PB
1935
1936found:
422148d3
DDAG
1937 *offset = (host - block->host);
1938 if (round_offset) {
1939 *offset &= TARGET_PAGE_MASK;
1940 }
1941 *ram_addr = block->offset + *offset;
0dc3f44a 1942 rcu_read_unlock();
422148d3
DDAG
1943 return block;
1944}
1945
e3dd7493
DDAG
1946/*
1947 * Finds the named RAMBlock
1948 *
1949 * name: The name of RAMBlock to find
1950 *
1951 * Returns: RAMBlock (or NULL if not found)
1952 */
1953RAMBlock *qemu_ram_block_by_name(const char *name)
1954{
1955 RAMBlock *block;
1956
1957 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1958 if (!strcmp(name, block->idstr)) {
1959 return block;
1960 }
1961 }
1962
1963 return NULL;
1964}
1965
422148d3
DDAG
1966/* Some of the softmmu routines need to translate from a host pointer
1967 (typically a TLB entry) back to a ram offset. */
1968MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
1969{
1970 RAMBlock *block;
1971 ram_addr_t offset; /* Not used */
1972
1973 block = qemu_ram_block_from_host(ptr, false, ram_addr, &offset);
1974
1975 if (!block) {
1976 return NULL;
1977 }
1978
1979 return block->mr;
e890261f 1980}
f471a17e 1981
a8170e5e 1982static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
0e0df1e2 1983 uint64_t val, unsigned size)
9fa3e853 1984{
52159192 1985 if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
0e0df1e2 1986 tb_invalidate_phys_page_fast(ram_addr, size);
3a7d929e 1987 }
0e0df1e2
AK
1988 switch (size) {
1989 case 1:
1990 stb_p(qemu_get_ram_ptr(ram_addr), val);
1991 break;
1992 case 2:
1993 stw_p(qemu_get_ram_ptr(ram_addr), val);
1994 break;
1995 case 4:
1996 stl_p(qemu_get_ram_ptr(ram_addr), val);
1997 break;
1998 default:
1999 abort();
3a7d929e 2000 }
58d2707e
PB
2001 /* Set both VGA and migration bits for simplicity and to remove
2002 * the notdirty callback faster.
2003 */
2004 cpu_physical_memory_set_dirty_range(ram_addr, size,
2005 DIRTY_CLIENTS_NOCODE);
f23db169
FB
2006 /* we remove the notdirty callback only if the code has been
2007 flushed */
a2cd8c85 2008 if (!cpu_physical_memory_is_clean(ram_addr)) {
bcae01e4 2009 tlb_set_dirty(current_cpu, current_cpu->mem_io_vaddr);
4917cf44 2010 }
9fa3e853
FB
2011}
2012
b018ddf6
PB
2013static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
2014 unsigned size, bool is_write)
2015{
2016 return is_write;
2017}
2018
0e0df1e2 2019static const MemoryRegionOps notdirty_mem_ops = {
0e0df1e2 2020 .write = notdirty_mem_write,
b018ddf6 2021 .valid.accepts = notdirty_mem_accepts,
0e0df1e2 2022 .endianness = DEVICE_NATIVE_ENDIAN,
1ccde1cb
FB
2023};
2024
0f459d16 2025/* Generate a debug exception if a watchpoint has been hit. */
66b9b43c 2026static void check_watchpoint(int offset, int len, MemTxAttrs attrs, int flags)
0f459d16 2027{
93afeade
AF
2028 CPUState *cpu = current_cpu;
2029 CPUArchState *env = cpu->env_ptr;
06d55cc1 2030 target_ulong pc, cs_base;
0f459d16 2031 target_ulong vaddr;
a1d1bb31 2032 CPUWatchpoint *wp;
06d55cc1 2033 int cpu_flags;
0f459d16 2034
ff4700b0 2035 if (cpu->watchpoint_hit) {
06d55cc1
AL
2036 /* We re-entered the check after replacing the TB. Now raise
2037 * the debug interrupt so that is will trigger after the
2038 * current instruction. */
93afeade 2039 cpu_interrupt(cpu, CPU_INTERRUPT_DEBUG);
06d55cc1
AL
2040 return;
2041 }
93afeade 2042 vaddr = (cpu->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
ff4700b0 2043 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
05068c0d
PM
2044 if (cpu_watchpoint_address_matches(wp, vaddr, len)
2045 && (wp->flags & flags)) {
08225676
PM
2046 if (flags == BP_MEM_READ) {
2047 wp->flags |= BP_WATCHPOINT_HIT_READ;
2048 } else {
2049 wp->flags |= BP_WATCHPOINT_HIT_WRITE;
2050 }
2051 wp->hitaddr = vaddr;
66b9b43c 2052 wp->hitattrs = attrs;
ff4700b0
AF
2053 if (!cpu->watchpoint_hit) {
2054 cpu->watchpoint_hit = wp;
239c51a5 2055 tb_check_watchpoint(cpu);
6e140f28 2056 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
27103424 2057 cpu->exception_index = EXCP_DEBUG;
5638d180 2058 cpu_loop_exit(cpu);
6e140f28
AL
2059 } else {
2060 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
648f034c 2061 tb_gen_code(cpu, pc, cs_base, cpu_flags, 1);
0ea8cb88 2062 cpu_resume_from_signal(cpu, NULL);
6e140f28 2063 }
06d55cc1 2064 }
6e140f28
AL
2065 } else {
2066 wp->flags &= ~BP_WATCHPOINT_HIT;
0f459d16
PB
2067 }
2068 }
2069}
2070
6658ffb8
PB
2071/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2072 so these check for a hit then pass through to the normal out-of-line
2073 phys routines. */
66b9b43c
PM
2074static MemTxResult watch_mem_read(void *opaque, hwaddr addr, uint64_t *pdata,
2075 unsigned size, MemTxAttrs attrs)
6658ffb8 2076{
66b9b43c
PM
2077 MemTxResult res;
2078 uint64_t data;
2079
2080 check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_READ);
1ec9b909 2081 switch (size) {
66b9b43c
PM
2082 case 1:
2083 data = address_space_ldub(&address_space_memory, addr, attrs, &res);
2084 break;
2085 case 2:
2086 data = address_space_lduw(&address_space_memory, addr, attrs, &res);
2087 break;
2088 case 4:
2089 data = address_space_ldl(&address_space_memory, addr, attrs, &res);
2090 break;
1ec9b909
AK
2091 default: abort();
2092 }
66b9b43c
PM
2093 *pdata = data;
2094 return res;
6658ffb8
PB
2095}
2096
66b9b43c
PM
2097static MemTxResult watch_mem_write(void *opaque, hwaddr addr,
2098 uint64_t val, unsigned size,
2099 MemTxAttrs attrs)
6658ffb8 2100{
66b9b43c
PM
2101 MemTxResult res;
2102
2103 check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_WRITE);
1ec9b909 2104 switch (size) {
67364150 2105 case 1:
66b9b43c 2106 address_space_stb(&address_space_memory, addr, val, attrs, &res);
67364150
MF
2107 break;
2108 case 2:
66b9b43c 2109 address_space_stw(&address_space_memory, addr, val, attrs, &res);
67364150
MF
2110 break;
2111 case 4:
66b9b43c 2112 address_space_stl(&address_space_memory, addr, val, attrs, &res);
67364150 2113 break;
1ec9b909
AK
2114 default: abort();
2115 }
66b9b43c 2116 return res;
6658ffb8
PB
2117}
2118
1ec9b909 2119static const MemoryRegionOps watch_mem_ops = {
66b9b43c
PM
2120 .read_with_attrs = watch_mem_read,
2121 .write_with_attrs = watch_mem_write,
1ec9b909 2122 .endianness = DEVICE_NATIVE_ENDIAN,
6658ffb8 2123};
6658ffb8 2124
f25a49e0
PM
2125static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data,
2126 unsigned len, MemTxAttrs attrs)
db7b5426 2127{
acc9d80b 2128 subpage_t *subpage = opaque;
ff6cff75 2129 uint8_t buf[8];
5c9eb028 2130 MemTxResult res;
791af8c8 2131
db7b5426 2132#if defined(DEBUG_SUBPAGE)
016e9d62 2133 printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__,
acc9d80b 2134 subpage, len, addr);
db7b5426 2135#endif
5c9eb028
PM
2136 res = address_space_read(subpage->as, addr + subpage->base,
2137 attrs, buf, len);
2138 if (res) {
2139 return res;
f25a49e0 2140 }
acc9d80b
JK
2141 switch (len) {
2142 case 1:
f25a49e0
PM
2143 *data = ldub_p(buf);
2144 return MEMTX_OK;
acc9d80b 2145 case 2:
f25a49e0
PM
2146 *data = lduw_p(buf);
2147 return MEMTX_OK;
acc9d80b 2148 case 4:
f25a49e0
PM
2149 *data = ldl_p(buf);
2150 return MEMTX_OK;
ff6cff75 2151 case 8:
f25a49e0
PM
2152 *data = ldq_p(buf);
2153 return MEMTX_OK;
acc9d80b
JK
2154 default:
2155 abort();
2156 }
db7b5426
BS
2157}
2158
f25a49e0
PM
2159static MemTxResult subpage_write(void *opaque, hwaddr addr,
2160 uint64_t value, unsigned len, MemTxAttrs attrs)
db7b5426 2161{
acc9d80b 2162 subpage_t *subpage = opaque;
ff6cff75 2163 uint8_t buf[8];
acc9d80b 2164
db7b5426 2165#if defined(DEBUG_SUBPAGE)
016e9d62 2166 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
acc9d80b
JK
2167 " value %"PRIx64"\n",
2168 __func__, subpage, len, addr, value);
db7b5426 2169#endif
acc9d80b
JK
2170 switch (len) {
2171 case 1:
2172 stb_p(buf, value);
2173 break;
2174 case 2:
2175 stw_p(buf, value);
2176 break;
2177 case 4:
2178 stl_p(buf, value);
2179 break;
ff6cff75
PB
2180 case 8:
2181 stq_p(buf, value);
2182 break;
acc9d80b
JK
2183 default:
2184 abort();
2185 }
5c9eb028
PM
2186 return address_space_write(subpage->as, addr + subpage->base,
2187 attrs, buf, len);
db7b5426
BS
2188}
2189
c353e4cc 2190static bool subpage_accepts(void *opaque, hwaddr addr,
016e9d62 2191 unsigned len, bool is_write)
c353e4cc 2192{
acc9d80b 2193 subpage_t *subpage = opaque;
c353e4cc 2194#if defined(DEBUG_SUBPAGE)
016e9d62 2195 printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n",
acc9d80b 2196 __func__, subpage, is_write ? 'w' : 'r', len, addr);
c353e4cc
PB
2197#endif
2198
acc9d80b 2199 return address_space_access_valid(subpage->as, addr + subpage->base,
016e9d62 2200 len, is_write);
c353e4cc
PB
2201}
2202
70c68e44 2203static const MemoryRegionOps subpage_ops = {
f25a49e0
PM
2204 .read_with_attrs = subpage_read,
2205 .write_with_attrs = subpage_write,
ff6cff75
PB
2206 .impl.min_access_size = 1,
2207 .impl.max_access_size = 8,
2208 .valid.min_access_size = 1,
2209 .valid.max_access_size = 8,
c353e4cc 2210 .valid.accepts = subpage_accepts,
70c68e44 2211 .endianness = DEVICE_NATIVE_ENDIAN,
db7b5426
BS
2212};
2213
c227f099 2214static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
5312bd8b 2215 uint16_t section)
db7b5426
BS
2216{
2217 int idx, eidx;
2218
2219 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2220 return -1;
2221 idx = SUBPAGE_IDX(start);
2222 eidx = SUBPAGE_IDX(end);
2223#if defined(DEBUG_SUBPAGE)
016e9d62
AK
2224 printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
2225 __func__, mmio, start, end, idx, eidx, section);
db7b5426 2226#endif
db7b5426 2227 for (; idx <= eidx; idx++) {
5312bd8b 2228 mmio->sub_section[idx] = section;
db7b5426
BS
2229 }
2230
2231 return 0;
2232}
2233
acc9d80b 2234static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
db7b5426 2235{
c227f099 2236 subpage_t *mmio;
db7b5426 2237
7267c094 2238 mmio = g_malloc0(sizeof(subpage_t));
1eec614b 2239
acc9d80b 2240 mmio->as = as;
1eec614b 2241 mmio->base = base;
2c9b15ca 2242 memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
b4fefef9 2243 NULL, TARGET_PAGE_SIZE);
b3b00c78 2244 mmio->iomem.subpage = true;
db7b5426 2245#if defined(DEBUG_SUBPAGE)
016e9d62
AK
2246 printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__,
2247 mmio, base, TARGET_PAGE_SIZE);
db7b5426 2248#endif
b41aac4f 2249 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED);
db7b5426
BS
2250
2251 return mmio;
2252}
2253
a656e22f
PC
2254static uint16_t dummy_section(PhysPageMap *map, AddressSpace *as,
2255 MemoryRegion *mr)
5312bd8b 2256{
a656e22f 2257 assert(as);
5312bd8b 2258 MemoryRegionSection section = {
a656e22f 2259 .address_space = as,
5312bd8b
AK
2260 .mr = mr,
2261 .offset_within_address_space = 0,
2262 .offset_within_region = 0,
052e87b0 2263 .size = int128_2_64(),
5312bd8b
AK
2264 };
2265
53cb28cb 2266 return phys_section_add(map, &section);
5312bd8b
AK
2267}
2268
9d82b5a7 2269MemoryRegion *iotlb_to_region(CPUState *cpu, hwaddr index)
aa102231 2270{
32857f4d
PM
2271 CPUAddressSpace *cpuas = &cpu->cpu_ases[0];
2272 AddressSpaceDispatch *d = atomic_rcu_read(&cpuas->memory_dispatch);
79e2b9ae 2273 MemoryRegionSection *sections = d->map.sections;
9d82b5a7
PB
2274
2275 return sections[index & ~TARGET_PAGE_MASK].mr;
aa102231
AK
2276}
2277
e9179ce1
AK
2278static void io_mem_init(void)
2279{
1f6245e5 2280 memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, NULL, UINT64_MAX);
2c9b15ca 2281 memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
1f6245e5 2282 NULL, UINT64_MAX);
2c9b15ca 2283 memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
1f6245e5 2284 NULL, UINT64_MAX);
2c9b15ca 2285 memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL,
1f6245e5 2286 NULL, UINT64_MAX);
e9179ce1
AK
2287}
2288
ac1970fb 2289static void mem_begin(MemoryListener *listener)
00752703
PB
2290{
2291 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
53cb28cb
MA
2292 AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
2293 uint16_t n;
2294
a656e22f 2295 n = dummy_section(&d->map, as, &io_mem_unassigned);
53cb28cb 2296 assert(n == PHYS_SECTION_UNASSIGNED);
a656e22f 2297 n = dummy_section(&d->map, as, &io_mem_notdirty);
53cb28cb 2298 assert(n == PHYS_SECTION_NOTDIRTY);
a656e22f 2299 n = dummy_section(&d->map, as, &io_mem_rom);
53cb28cb 2300 assert(n == PHYS_SECTION_ROM);
a656e22f 2301 n = dummy_section(&d->map, as, &io_mem_watch);
53cb28cb 2302 assert(n == PHYS_SECTION_WATCH);
00752703 2303
9736e55b 2304 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
00752703
PB
2305 d->as = as;
2306 as->next_dispatch = d;
2307}
2308
79e2b9ae
PB
2309static void address_space_dispatch_free(AddressSpaceDispatch *d)
2310{
2311 phys_sections_free(&d->map);
2312 g_free(d);
2313}
2314
00752703 2315static void mem_commit(MemoryListener *listener)
ac1970fb 2316{
89ae337a 2317 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
0475d94f
PB
2318 AddressSpaceDispatch *cur = as->dispatch;
2319 AddressSpaceDispatch *next = as->next_dispatch;
2320
53cb28cb 2321 phys_page_compact_all(next, next->map.nodes_nb);
b35ba30f 2322
79e2b9ae 2323 atomic_rcu_set(&as->dispatch, next);
53cb28cb 2324 if (cur) {
79e2b9ae 2325 call_rcu(cur, address_space_dispatch_free, rcu);
53cb28cb 2326 }
9affd6fc
PB
2327}
2328
1d71148e 2329static void tcg_commit(MemoryListener *listener)
50c1e149 2330{
32857f4d
PM
2331 CPUAddressSpace *cpuas;
2332 AddressSpaceDispatch *d;
117712c3
AK
2333
2334 /* since each CPU stores ram addresses in its TLB cache, we must
2335 reset the modified entries */
32857f4d
PM
2336 cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener);
2337 cpu_reloading_memory_map();
2338 /* The CPU and TLB are protected by the iothread lock.
2339 * We reload the dispatch pointer now because cpu_reloading_memory_map()
2340 * may have split the RCU critical section.
2341 */
2342 d = atomic_rcu_read(&cpuas->as->dispatch);
2343 cpuas->memory_dispatch = d;
2344 tlb_flush(cpuas->cpu, 1);
50c1e149
AK
2345}
2346
ac1970fb
AK
2347void address_space_init_dispatch(AddressSpace *as)
2348{
00752703 2349 as->dispatch = NULL;
89ae337a 2350 as->dispatch_listener = (MemoryListener) {
ac1970fb 2351 .begin = mem_begin,
00752703 2352 .commit = mem_commit,
ac1970fb
AK
2353 .region_add = mem_add,
2354 .region_nop = mem_add,
2355 .priority = 0,
2356 };
89ae337a 2357 memory_listener_register(&as->dispatch_listener, as);
ac1970fb
AK
2358}
2359
6e48e8f9
PB
2360void address_space_unregister(AddressSpace *as)
2361{
2362 memory_listener_unregister(&as->dispatch_listener);
2363}
2364
83f3c251
AK
2365void address_space_destroy_dispatch(AddressSpace *as)
2366{
2367 AddressSpaceDispatch *d = as->dispatch;
2368
79e2b9ae
PB
2369 atomic_rcu_set(&as->dispatch, NULL);
2370 if (d) {
2371 call_rcu(d, address_space_dispatch_free, rcu);
2372 }
83f3c251
AK
2373}
2374
62152b8a
AK
2375static void memory_map_init(void)
2376{
7267c094 2377 system_memory = g_malloc(sizeof(*system_memory));
03f49957 2378
57271d63 2379 memory_region_init(system_memory, NULL, "system", UINT64_MAX);
7dca8043 2380 address_space_init(&address_space_memory, system_memory, "memory");
309cb471 2381
7267c094 2382 system_io = g_malloc(sizeof(*system_io));
3bb28b72
JK
2383 memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
2384 65536);
7dca8043 2385 address_space_init(&address_space_io, system_io, "I/O");
62152b8a
AK
2386}
2387
2388MemoryRegion *get_system_memory(void)
2389{
2390 return system_memory;
2391}
2392
309cb471
AK
2393MemoryRegion *get_system_io(void)
2394{
2395 return system_io;
2396}
2397
e2eef170
PB
2398#endif /* !defined(CONFIG_USER_ONLY) */
2399
13eb76e0
FB
2400/* physical memory access (slow version, mainly for debug) */
2401#if defined(CONFIG_USER_ONLY)
f17ec444 2402int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
a68fe89c 2403 uint8_t *buf, int len, int is_write)
13eb76e0
FB
2404{
2405 int l, flags;
2406 target_ulong page;
53a5960a 2407 void * p;
13eb76e0
FB
2408
2409 while (len > 0) {
2410 page = addr & TARGET_PAGE_MASK;
2411 l = (page + TARGET_PAGE_SIZE) - addr;
2412 if (l > len)
2413 l = len;
2414 flags = page_get_flags(page);
2415 if (!(flags & PAGE_VALID))
a68fe89c 2416 return -1;
13eb76e0
FB
2417 if (is_write) {
2418 if (!(flags & PAGE_WRITE))
a68fe89c 2419 return -1;
579a97f7 2420 /* XXX: this code should not depend on lock_user */
72fb7daa 2421 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
a68fe89c 2422 return -1;
72fb7daa
AJ
2423 memcpy(p, buf, l);
2424 unlock_user(p, addr, l);
13eb76e0
FB
2425 } else {
2426 if (!(flags & PAGE_READ))
a68fe89c 2427 return -1;
579a97f7 2428 /* XXX: this code should not depend on lock_user */
72fb7daa 2429 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
a68fe89c 2430 return -1;
72fb7daa 2431 memcpy(buf, p, l);
5b257578 2432 unlock_user(p, addr, 0);
13eb76e0
FB
2433 }
2434 len -= l;
2435 buf += l;
2436 addr += l;
2437 }
a68fe89c 2438 return 0;
13eb76e0 2439}
8df1cd07 2440
13eb76e0 2441#else
51d7a9eb 2442
845b6214 2443static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
a8170e5e 2444 hwaddr length)
51d7a9eb 2445{
e87f7778
PB
2446 uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr);
2447 /* No early return if dirty_log_mask is or becomes 0, because
2448 * cpu_physical_memory_set_dirty_range will still call
2449 * xen_modified_memory.
2450 */
2451 if (dirty_log_mask) {
2452 dirty_log_mask =
2453 cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask);
2454 }
2455 if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) {
2456 tb_invalidate_phys_range(addr, addr + length);
2457 dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
51d7a9eb 2458 }
e87f7778 2459 cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
51d7a9eb
AP
2460}
2461
23326164 2462static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
82f2563f 2463{
e1622f4b 2464 unsigned access_size_max = mr->ops->valid.max_access_size;
23326164
RH
2465
2466 /* Regions are assumed to support 1-4 byte accesses unless
2467 otherwise specified. */
23326164
RH
2468 if (access_size_max == 0) {
2469 access_size_max = 4;
2470 }
2471
2472 /* Bound the maximum access by the alignment of the address. */
2473 if (!mr->ops->impl.unaligned) {
2474 unsigned align_size_max = addr & -addr;
2475 if (align_size_max != 0 && align_size_max < access_size_max) {
2476 access_size_max = align_size_max;
2477 }
82f2563f 2478 }
23326164
RH
2479
2480 /* Don't attempt accesses larger than the maximum. */
2481 if (l > access_size_max) {
2482 l = access_size_max;
82f2563f 2483 }
6554f5c0 2484 l = pow2floor(l);
23326164
RH
2485
2486 return l;
82f2563f
PB
2487}
2488
4840f10e 2489static bool prepare_mmio_access(MemoryRegion *mr)
125b3806 2490{
4840f10e
JK
2491 bool unlocked = !qemu_mutex_iothread_locked();
2492 bool release_lock = false;
2493
2494 if (unlocked && mr->global_locking) {
2495 qemu_mutex_lock_iothread();
2496 unlocked = false;
2497 release_lock = true;
2498 }
125b3806 2499 if (mr->flush_coalesced_mmio) {
4840f10e
JK
2500 if (unlocked) {
2501 qemu_mutex_lock_iothread();
2502 }
125b3806 2503 qemu_flush_coalesced_mmio_buffer();
4840f10e
JK
2504 if (unlocked) {
2505 qemu_mutex_unlock_iothread();
2506 }
125b3806 2507 }
4840f10e
JK
2508
2509 return release_lock;
125b3806
PB
2510}
2511
5c9eb028
PM
2512MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2513 uint8_t *buf, int len, bool is_write)
13eb76e0 2514{
149f54b5 2515 hwaddr l;
13eb76e0 2516 uint8_t *ptr;
791af8c8 2517 uint64_t val;
149f54b5 2518 hwaddr addr1;
5c8a00ce 2519 MemoryRegion *mr;
3b643495 2520 MemTxResult result = MEMTX_OK;
4840f10e 2521 bool release_lock = false;
3b46e624 2522
41063e1e 2523 rcu_read_lock();
13eb76e0 2524 while (len > 0) {
149f54b5 2525 l = len;
5c8a00ce 2526 mr = address_space_translate(as, addr, &addr1, &l, is_write);
3b46e624 2527
13eb76e0 2528 if (is_write) {
5c8a00ce 2529 if (!memory_access_is_direct(mr, is_write)) {
4840f10e 2530 release_lock |= prepare_mmio_access(mr);
5c8a00ce 2531 l = memory_access_size(mr, l, addr1);
4917cf44 2532 /* XXX: could force current_cpu to NULL to avoid
6a00d601 2533 potential bugs */
23326164
RH
2534 switch (l) {
2535 case 8:
2536 /* 64 bit write access */
2537 val = ldq_p(buf);
3b643495
PM
2538 result |= memory_region_dispatch_write(mr, addr1, val, 8,
2539 attrs);
23326164
RH
2540 break;
2541 case 4:
1c213d19 2542 /* 32 bit write access */
c27004ec 2543 val = ldl_p(buf);
3b643495
PM
2544 result |= memory_region_dispatch_write(mr, addr1, val, 4,
2545 attrs);
23326164
RH
2546 break;
2547 case 2:
1c213d19 2548 /* 16 bit write access */
c27004ec 2549 val = lduw_p(buf);
3b643495
PM
2550 result |= memory_region_dispatch_write(mr, addr1, val, 2,
2551 attrs);
23326164
RH
2552 break;
2553 case 1:
1c213d19 2554 /* 8 bit write access */
c27004ec 2555 val = ldub_p(buf);
3b643495
PM
2556 result |= memory_region_dispatch_write(mr, addr1, val, 1,
2557 attrs);
23326164
RH
2558 break;
2559 default:
2560 abort();
13eb76e0 2561 }
2bbfa05d 2562 } else {
5c8a00ce 2563 addr1 += memory_region_get_ram_addr(mr);
13eb76e0 2564 /* RAM case */
5579c7f3 2565 ptr = qemu_get_ram_ptr(addr1);
13eb76e0 2566 memcpy(ptr, buf, l);
845b6214 2567 invalidate_and_set_dirty(mr, addr1, l);
13eb76e0
FB
2568 }
2569 } else {
5c8a00ce 2570 if (!memory_access_is_direct(mr, is_write)) {
13eb76e0 2571 /* I/O case */
4840f10e 2572 release_lock |= prepare_mmio_access(mr);
5c8a00ce 2573 l = memory_access_size(mr, l, addr1);
23326164
RH
2574 switch (l) {
2575 case 8:
2576 /* 64 bit read access */
3b643495
PM
2577 result |= memory_region_dispatch_read(mr, addr1, &val, 8,
2578 attrs);
23326164
RH
2579 stq_p(buf, val);
2580 break;
2581 case 4:
13eb76e0 2582 /* 32 bit read access */
3b643495
PM
2583 result |= memory_region_dispatch_read(mr, addr1, &val, 4,
2584 attrs);
c27004ec 2585 stl_p(buf, val);
23326164
RH
2586 break;
2587 case 2:
13eb76e0 2588 /* 16 bit read access */
3b643495
PM
2589 result |= memory_region_dispatch_read(mr, addr1, &val, 2,
2590 attrs);
c27004ec 2591 stw_p(buf, val);
23326164
RH
2592 break;
2593 case 1:
1c213d19 2594 /* 8 bit read access */
3b643495
PM
2595 result |= memory_region_dispatch_read(mr, addr1, &val, 1,
2596 attrs);
c27004ec 2597 stb_p(buf, val);
23326164
RH
2598 break;
2599 default:
2600 abort();
13eb76e0
FB
2601 }
2602 } else {
2603 /* RAM case */
5c8a00ce 2604 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
f3705d53 2605 memcpy(buf, ptr, l);
13eb76e0
FB
2606 }
2607 }
4840f10e
JK
2608
2609 if (release_lock) {
2610 qemu_mutex_unlock_iothread();
2611 release_lock = false;
2612 }
2613
13eb76e0
FB
2614 len -= l;
2615 buf += l;
2616 addr += l;
2617 }
41063e1e 2618 rcu_read_unlock();
fd8aaa76 2619
3b643495 2620 return result;
13eb76e0 2621}
8df1cd07 2622
5c9eb028
PM
2623MemTxResult address_space_write(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2624 const uint8_t *buf, int len)
ac1970fb 2625{
5c9eb028 2626 return address_space_rw(as, addr, attrs, (uint8_t *)buf, len, true);
ac1970fb
AK
2627}
2628
5c9eb028
PM
2629MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2630 uint8_t *buf, int len)
ac1970fb 2631{
5c9eb028 2632 return address_space_rw(as, addr, attrs, buf, len, false);
ac1970fb
AK
2633}
2634
2635
a8170e5e 2636void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
ac1970fb
AK
2637 int len, int is_write)
2638{
5c9eb028
PM
2639 address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED,
2640 buf, len, is_write);
ac1970fb
AK
2641}
2642
582b55a9
AG
2643enum write_rom_type {
2644 WRITE_DATA,
2645 FLUSH_CACHE,
2646};
2647
2a221651 2648static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
582b55a9 2649 hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
d0ecd2aa 2650{
149f54b5 2651 hwaddr l;
d0ecd2aa 2652 uint8_t *ptr;
149f54b5 2653 hwaddr addr1;
5c8a00ce 2654 MemoryRegion *mr;
3b46e624 2655
41063e1e 2656 rcu_read_lock();
d0ecd2aa 2657 while (len > 0) {
149f54b5 2658 l = len;
2a221651 2659 mr = address_space_translate(as, addr, &addr1, &l, true);
3b46e624 2660
5c8a00ce
PB
2661 if (!(memory_region_is_ram(mr) ||
2662 memory_region_is_romd(mr))) {
b242e0e0 2663 l = memory_access_size(mr, l, addr1);
d0ecd2aa 2664 } else {
5c8a00ce 2665 addr1 += memory_region_get_ram_addr(mr);
d0ecd2aa 2666 /* ROM/RAM case */
5579c7f3 2667 ptr = qemu_get_ram_ptr(addr1);
582b55a9
AG
2668 switch (type) {
2669 case WRITE_DATA:
2670 memcpy(ptr, buf, l);
845b6214 2671 invalidate_and_set_dirty(mr, addr1, l);
582b55a9
AG
2672 break;
2673 case FLUSH_CACHE:
2674 flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l);
2675 break;
2676 }
d0ecd2aa
FB
2677 }
2678 len -= l;
2679 buf += l;
2680 addr += l;
2681 }
41063e1e 2682 rcu_read_unlock();
d0ecd2aa
FB
2683}
2684
582b55a9 2685/* used for ROM loading : can write in RAM and ROM */
2a221651 2686void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr,
582b55a9
AG
2687 const uint8_t *buf, int len)
2688{
2a221651 2689 cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
582b55a9
AG
2690}
2691
2692void cpu_flush_icache_range(hwaddr start, int len)
2693{
2694 /*
2695 * This function should do the same thing as an icache flush that was
2696 * triggered from within the guest. For TCG we are always cache coherent,
2697 * so there is no need to flush anything. For KVM / Xen we need to flush
2698 * the host's instruction cache at least.
2699 */
2700 if (tcg_enabled()) {
2701 return;
2702 }
2703
2a221651
EI
2704 cpu_physical_memory_write_rom_internal(&address_space_memory,
2705 start, NULL, len, FLUSH_CACHE);
582b55a9
AG
2706}
2707
6d16c2f8 2708typedef struct {
d3e71559 2709 MemoryRegion *mr;
6d16c2f8 2710 void *buffer;
a8170e5e
AK
2711 hwaddr addr;
2712 hwaddr len;
c2cba0ff 2713 bool in_use;
6d16c2f8
AL
2714} BounceBuffer;
2715
2716static BounceBuffer bounce;
2717
ba223c29 2718typedef struct MapClient {
e95205e1 2719 QEMUBH *bh;
72cf2d4f 2720 QLIST_ENTRY(MapClient) link;
ba223c29
AL
2721} MapClient;
2722
38e047b5 2723QemuMutex map_client_list_lock;
72cf2d4f
BS
2724static QLIST_HEAD(map_client_list, MapClient) map_client_list
2725 = QLIST_HEAD_INITIALIZER(map_client_list);
ba223c29 2726
e95205e1
FZ
2727static void cpu_unregister_map_client_do(MapClient *client)
2728{
2729 QLIST_REMOVE(client, link);
2730 g_free(client);
2731}
2732
33b6c2ed
FZ
2733static void cpu_notify_map_clients_locked(void)
2734{
2735 MapClient *client;
2736
2737 while (!QLIST_EMPTY(&map_client_list)) {
2738 client = QLIST_FIRST(&map_client_list);
e95205e1
FZ
2739 qemu_bh_schedule(client->bh);
2740 cpu_unregister_map_client_do(client);
33b6c2ed
FZ
2741 }
2742}
2743
e95205e1 2744void cpu_register_map_client(QEMUBH *bh)
ba223c29 2745{
7267c094 2746 MapClient *client = g_malloc(sizeof(*client));
ba223c29 2747
38e047b5 2748 qemu_mutex_lock(&map_client_list_lock);
e95205e1 2749 client->bh = bh;
72cf2d4f 2750 QLIST_INSERT_HEAD(&map_client_list, client, link);
33b6c2ed
FZ
2751 if (!atomic_read(&bounce.in_use)) {
2752 cpu_notify_map_clients_locked();
2753 }
38e047b5 2754 qemu_mutex_unlock(&map_client_list_lock);
ba223c29
AL
2755}
2756
38e047b5 2757void cpu_exec_init_all(void)
ba223c29 2758{
38e047b5 2759 qemu_mutex_init(&ram_list.mutex);
38e047b5 2760 io_mem_init();
680a4783 2761 memory_map_init();
38e047b5 2762 qemu_mutex_init(&map_client_list_lock);
ba223c29
AL
2763}
2764
e95205e1 2765void cpu_unregister_map_client(QEMUBH *bh)
ba223c29
AL
2766{
2767 MapClient *client;
2768
e95205e1
FZ
2769 qemu_mutex_lock(&map_client_list_lock);
2770 QLIST_FOREACH(client, &map_client_list, link) {
2771 if (client->bh == bh) {
2772 cpu_unregister_map_client_do(client);
2773 break;
2774 }
ba223c29 2775 }
e95205e1 2776 qemu_mutex_unlock(&map_client_list_lock);
ba223c29
AL
2777}
2778
2779static void cpu_notify_map_clients(void)
2780{
38e047b5 2781 qemu_mutex_lock(&map_client_list_lock);
33b6c2ed 2782 cpu_notify_map_clients_locked();
38e047b5 2783 qemu_mutex_unlock(&map_client_list_lock);
ba223c29
AL
2784}
2785
51644ab7
PB
2786bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2787{
5c8a00ce 2788 MemoryRegion *mr;
51644ab7
PB
2789 hwaddr l, xlat;
2790
41063e1e 2791 rcu_read_lock();
51644ab7
PB
2792 while (len > 0) {
2793 l = len;
5c8a00ce
PB
2794 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2795 if (!memory_access_is_direct(mr, is_write)) {
2796 l = memory_access_size(mr, l, addr);
2797 if (!memory_region_access_valid(mr, xlat, l, is_write)) {
51644ab7
PB
2798 return false;
2799 }
2800 }
2801
2802 len -= l;
2803 addr += l;
2804 }
41063e1e 2805 rcu_read_unlock();
51644ab7
PB
2806 return true;
2807}
2808
6d16c2f8
AL
2809/* Map a physical memory region into a host virtual address.
2810 * May map a subset of the requested range, given by and returned in *plen.
2811 * May return NULL if resources needed to perform the mapping are exhausted.
2812 * Use only for reads OR writes - not for read-modify-write operations.
ba223c29
AL
2813 * Use cpu_register_map_client() to know when retrying the map operation is
2814 * likely to succeed.
6d16c2f8 2815 */
ac1970fb 2816void *address_space_map(AddressSpace *as,
a8170e5e
AK
2817 hwaddr addr,
2818 hwaddr *plen,
ac1970fb 2819 bool is_write)
6d16c2f8 2820{
a8170e5e 2821 hwaddr len = *plen;
e3127ae0
PB
2822 hwaddr done = 0;
2823 hwaddr l, xlat, base;
2824 MemoryRegion *mr, *this_mr;
2825 ram_addr_t raddr;
6d16c2f8 2826
e3127ae0
PB
2827 if (len == 0) {
2828 return NULL;
2829 }
38bee5dc 2830
e3127ae0 2831 l = len;
41063e1e 2832 rcu_read_lock();
e3127ae0 2833 mr = address_space_translate(as, addr, &xlat, &l, is_write);
41063e1e 2834
e3127ae0 2835 if (!memory_access_is_direct(mr, is_write)) {
c2cba0ff 2836 if (atomic_xchg(&bounce.in_use, true)) {
41063e1e 2837 rcu_read_unlock();
e3127ae0 2838 return NULL;
6d16c2f8 2839 }
e85d9db5
KW
2840 /* Avoid unbounded allocations */
2841 l = MIN(l, TARGET_PAGE_SIZE);
2842 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l);
e3127ae0
PB
2843 bounce.addr = addr;
2844 bounce.len = l;
d3e71559
PB
2845
2846 memory_region_ref(mr);
2847 bounce.mr = mr;
e3127ae0 2848 if (!is_write) {
5c9eb028
PM
2849 address_space_read(as, addr, MEMTXATTRS_UNSPECIFIED,
2850 bounce.buffer, l);
8ab934f9 2851 }
6d16c2f8 2852
41063e1e 2853 rcu_read_unlock();
e3127ae0
PB
2854 *plen = l;
2855 return bounce.buffer;
2856 }
2857
2858 base = xlat;
2859 raddr = memory_region_get_ram_addr(mr);
2860
2861 for (;;) {
6d16c2f8
AL
2862 len -= l;
2863 addr += l;
e3127ae0
PB
2864 done += l;
2865 if (len == 0) {
2866 break;
2867 }
2868
2869 l = len;
2870 this_mr = address_space_translate(as, addr, &xlat, &l, is_write);
2871 if (this_mr != mr || xlat != base + done) {
2872 break;
2873 }
6d16c2f8 2874 }
e3127ae0 2875
d3e71559 2876 memory_region_ref(mr);
41063e1e 2877 rcu_read_unlock();
e3127ae0
PB
2878 *plen = done;
2879 return qemu_ram_ptr_length(raddr + base, plen);
6d16c2f8
AL
2880}
2881
ac1970fb 2882/* Unmaps a memory region previously mapped by address_space_map().
6d16c2f8
AL
2883 * Will also mark the memory as dirty if is_write == 1. access_len gives
2884 * the amount of memory that was actually read or written by the caller.
2885 */
a8170e5e
AK
2886void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2887 int is_write, hwaddr access_len)
6d16c2f8
AL
2888{
2889 if (buffer != bounce.buffer) {
d3e71559
PB
2890 MemoryRegion *mr;
2891 ram_addr_t addr1;
2892
2893 mr = qemu_ram_addr_from_host(buffer, &addr1);
2894 assert(mr != NULL);
6d16c2f8 2895 if (is_write) {
845b6214 2896 invalidate_and_set_dirty(mr, addr1, access_len);
6d16c2f8 2897 }
868bb33f 2898 if (xen_enabled()) {
e41d7c69 2899 xen_invalidate_map_cache_entry(buffer);
050a0ddf 2900 }
d3e71559 2901 memory_region_unref(mr);
6d16c2f8
AL
2902 return;
2903 }
2904 if (is_write) {
5c9eb028
PM
2905 address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED,
2906 bounce.buffer, access_len);
6d16c2f8 2907 }
f8a83245 2908 qemu_vfree(bounce.buffer);
6d16c2f8 2909 bounce.buffer = NULL;
d3e71559 2910 memory_region_unref(bounce.mr);
c2cba0ff 2911 atomic_mb_set(&bounce.in_use, false);
ba223c29 2912 cpu_notify_map_clients();
6d16c2f8 2913}
d0ecd2aa 2914
a8170e5e
AK
2915void *cpu_physical_memory_map(hwaddr addr,
2916 hwaddr *plen,
ac1970fb
AK
2917 int is_write)
2918{
2919 return address_space_map(&address_space_memory, addr, plen, is_write);
2920}
2921
a8170e5e
AK
2922void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2923 int is_write, hwaddr access_len)
ac1970fb
AK
2924{
2925 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2926}
2927
8df1cd07 2928/* warning: addr must be aligned */
50013115
PM
2929static inline uint32_t address_space_ldl_internal(AddressSpace *as, hwaddr addr,
2930 MemTxAttrs attrs,
2931 MemTxResult *result,
2932 enum device_endian endian)
8df1cd07 2933{
8df1cd07 2934 uint8_t *ptr;
791af8c8 2935 uint64_t val;
5c8a00ce 2936 MemoryRegion *mr;
149f54b5
PB
2937 hwaddr l = 4;
2938 hwaddr addr1;
50013115 2939 MemTxResult r;
4840f10e 2940 bool release_lock = false;
8df1cd07 2941
41063e1e 2942 rcu_read_lock();
fdfba1a2 2943 mr = address_space_translate(as, addr, &addr1, &l, false);
5c8a00ce 2944 if (l < 4 || !memory_access_is_direct(mr, false)) {
4840f10e 2945 release_lock |= prepare_mmio_access(mr);
125b3806 2946
8df1cd07 2947 /* I/O case */
50013115 2948 r = memory_region_dispatch_read(mr, addr1, &val, 4, attrs);
1e78bcc1
AG
2949#if defined(TARGET_WORDS_BIGENDIAN)
2950 if (endian == DEVICE_LITTLE_ENDIAN) {
2951 val = bswap32(val);
2952 }
2953#else
2954 if (endian == DEVICE_BIG_ENDIAN) {
2955 val = bswap32(val);
2956 }
2957#endif
8df1cd07
FB
2958 } else {
2959 /* RAM case */
5c8a00ce 2960 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
06ef3525 2961 & TARGET_PAGE_MASK)
149f54b5 2962 + addr1);
1e78bcc1
AG
2963 switch (endian) {
2964 case DEVICE_LITTLE_ENDIAN:
2965 val = ldl_le_p(ptr);
2966 break;
2967 case DEVICE_BIG_ENDIAN:
2968 val = ldl_be_p(ptr);
2969 break;
2970 default:
2971 val = ldl_p(ptr);
2972 break;
2973 }
50013115
PM
2974 r = MEMTX_OK;
2975 }
2976 if (result) {
2977 *result = r;
8df1cd07 2978 }
4840f10e
JK
2979 if (release_lock) {
2980 qemu_mutex_unlock_iothread();
2981 }
41063e1e 2982 rcu_read_unlock();
8df1cd07
FB
2983 return val;
2984}
2985
50013115
PM
2986uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
2987 MemTxAttrs attrs, MemTxResult *result)
2988{
2989 return address_space_ldl_internal(as, addr, attrs, result,
2990 DEVICE_NATIVE_ENDIAN);
2991}
2992
2993uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
2994 MemTxAttrs attrs, MemTxResult *result)
2995{
2996 return address_space_ldl_internal(as, addr, attrs, result,
2997 DEVICE_LITTLE_ENDIAN);
2998}
2999
3000uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
3001 MemTxAttrs attrs, MemTxResult *result)
3002{
3003 return address_space_ldl_internal(as, addr, attrs, result,
3004 DEVICE_BIG_ENDIAN);
3005}
3006
fdfba1a2 3007uint32_t ldl_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3008{
50013115 3009 return address_space_ldl(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3010}
3011
fdfba1a2 3012uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3013{
50013115 3014 return address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3015}
3016
fdfba1a2 3017uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3018{
50013115 3019 return address_space_ldl_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3020}
3021
84b7b8e7 3022/* warning: addr must be aligned */
50013115
PM
3023static inline uint64_t address_space_ldq_internal(AddressSpace *as, hwaddr addr,
3024 MemTxAttrs attrs,
3025 MemTxResult *result,
3026 enum device_endian endian)
84b7b8e7 3027{
84b7b8e7
FB
3028 uint8_t *ptr;
3029 uint64_t val;
5c8a00ce 3030 MemoryRegion *mr;
149f54b5
PB
3031 hwaddr l = 8;
3032 hwaddr addr1;
50013115 3033 MemTxResult r;
4840f10e 3034 bool release_lock = false;
84b7b8e7 3035
41063e1e 3036 rcu_read_lock();
2c17449b 3037 mr = address_space_translate(as, addr, &addr1, &l,
5c8a00ce
PB
3038 false);
3039 if (l < 8 || !memory_access_is_direct(mr, false)) {
4840f10e 3040 release_lock |= prepare_mmio_access(mr);
125b3806 3041
84b7b8e7 3042 /* I/O case */
50013115 3043 r = memory_region_dispatch_read(mr, addr1, &val, 8, attrs);
968a5627
PB
3044#if defined(TARGET_WORDS_BIGENDIAN)
3045 if (endian == DEVICE_LITTLE_ENDIAN) {
3046 val = bswap64(val);
3047 }
3048#else
3049 if (endian == DEVICE_BIG_ENDIAN) {
3050 val = bswap64(val);
3051 }
84b7b8e7
FB
3052#endif
3053 } else {
3054 /* RAM case */
5c8a00ce 3055 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
06ef3525 3056 & TARGET_PAGE_MASK)
149f54b5 3057 + addr1);
1e78bcc1
AG
3058 switch (endian) {
3059 case DEVICE_LITTLE_ENDIAN:
3060 val = ldq_le_p(ptr);
3061 break;
3062 case DEVICE_BIG_ENDIAN:
3063 val = ldq_be_p(ptr);
3064 break;
3065 default:
3066 val = ldq_p(ptr);
3067 break;
3068 }
50013115
PM
3069 r = MEMTX_OK;
3070 }
3071 if (result) {
3072 *result = r;
84b7b8e7 3073 }
4840f10e
JK
3074 if (release_lock) {
3075 qemu_mutex_unlock_iothread();
3076 }
41063e1e 3077 rcu_read_unlock();
84b7b8e7
FB
3078 return val;
3079}
3080
50013115
PM
3081uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
3082 MemTxAttrs attrs, MemTxResult *result)
3083{
3084 return address_space_ldq_internal(as, addr, attrs, result,
3085 DEVICE_NATIVE_ENDIAN);
3086}
3087
3088uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
3089 MemTxAttrs attrs, MemTxResult *result)
3090{
3091 return address_space_ldq_internal(as, addr, attrs, result,
3092 DEVICE_LITTLE_ENDIAN);
3093}
3094
3095uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
3096 MemTxAttrs attrs, MemTxResult *result)
3097{
3098 return address_space_ldq_internal(as, addr, attrs, result,
3099 DEVICE_BIG_ENDIAN);
3100}
3101
2c17449b 3102uint64_t ldq_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3103{
50013115 3104 return address_space_ldq(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3105}
3106
2c17449b 3107uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3108{
50013115 3109 return address_space_ldq_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3110}
3111
2c17449b 3112uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3113{
50013115 3114 return address_space_ldq_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3115}
3116
aab33094 3117/* XXX: optimize */
50013115
PM
3118uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
3119 MemTxAttrs attrs, MemTxResult *result)
aab33094
FB
3120{
3121 uint8_t val;
50013115
PM
3122 MemTxResult r;
3123
3124 r = address_space_rw(as, addr, attrs, &val, 1, 0);
3125 if (result) {
3126 *result = r;
3127 }
aab33094
FB
3128 return val;
3129}
3130
50013115
PM
3131uint32_t ldub_phys(AddressSpace *as, hwaddr addr)
3132{
3133 return address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3134}
3135
733f0b02 3136/* warning: addr must be aligned */
50013115
PM
3137static inline uint32_t address_space_lduw_internal(AddressSpace *as,
3138 hwaddr addr,
3139 MemTxAttrs attrs,
3140 MemTxResult *result,
3141 enum device_endian endian)
aab33094 3142{
733f0b02
MT
3143 uint8_t *ptr;
3144 uint64_t val;
5c8a00ce 3145 MemoryRegion *mr;
149f54b5
PB
3146 hwaddr l = 2;
3147 hwaddr addr1;
50013115 3148 MemTxResult r;
4840f10e 3149 bool release_lock = false;
733f0b02 3150
41063e1e 3151 rcu_read_lock();
41701aa4 3152 mr = address_space_translate(as, addr, &addr1, &l,
5c8a00ce
PB
3153 false);
3154 if (l < 2 || !memory_access_is_direct(mr, false)) {
4840f10e 3155 release_lock |= prepare_mmio_access(mr);
125b3806 3156
733f0b02 3157 /* I/O case */
50013115 3158 r = memory_region_dispatch_read(mr, addr1, &val, 2, attrs);
1e78bcc1
AG
3159#if defined(TARGET_WORDS_BIGENDIAN)
3160 if (endian == DEVICE_LITTLE_ENDIAN) {
3161 val = bswap16(val);
3162 }
3163#else
3164 if (endian == DEVICE_BIG_ENDIAN) {
3165 val = bswap16(val);
3166 }
3167#endif
733f0b02
MT
3168 } else {
3169 /* RAM case */
5c8a00ce 3170 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
06ef3525 3171 & TARGET_PAGE_MASK)
149f54b5 3172 + addr1);
1e78bcc1
AG
3173 switch (endian) {
3174 case DEVICE_LITTLE_ENDIAN:
3175 val = lduw_le_p(ptr);
3176 break;
3177 case DEVICE_BIG_ENDIAN:
3178 val = lduw_be_p(ptr);
3179 break;
3180 default:
3181 val = lduw_p(ptr);
3182 break;
3183 }
50013115
PM
3184 r = MEMTX_OK;
3185 }
3186 if (result) {
3187 *result = r;
733f0b02 3188 }
4840f10e
JK
3189 if (release_lock) {
3190 qemu_mutex_unlock_iothread();
3191 }
41063e1e 3192 rcu_read_unlock();
733f0b02 3193 return val;
aab33094
FB
3194}
3195
50013115
PM
3196uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
3197 MemTxAttrs attrs, MemTxResult *result)
3198{
3199 return address_space_lduw_internal(as, addr, attrs, result,
3200 DEVICE_NATIVE_ENDIAN);
3201}
3202
3203uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
3204 MemTxAttrs attrs, MemTxResult *result)
3205{
3206 return address_space_lduw_internal(as, addr, attrs, result,
3207 DEVICE_LITTLE_ENDIAN);
3208}
3209
3210uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
3211 MemTxAttrs attrs, MemTxResult *result)
3212{
3213 return address_space_lduw_internal(as, addr, attrs, result,
3214 DEVICE_BIG_ENDIAN);
3215}
3216
41701aa4 3217uint32_t lduw_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3218{
50013115 3219 return address_space_lduw(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3220}
3221
41701aa4 3222uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3223{
50013115 3224 return address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3225}
3226
41701aa4 3227uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr)
1e78bcc1 3228{
50013115 3229 return address_space_lduw_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3230}
3231
8df1cd07
FB
3232/* warning: addr must be aligned. The ram page is not masked as dirty
3233 and the code inside is not invalidated. It is useful if the dirty
3234 bits are used to track modified PTEs */
50013115
PM
3235void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val,
3236 MemTxAttrs attrs, MemTxResult *result)
8df1cd07 3237{
8df1cd07 3238 uint8_t *ptr;
5c8a00ce 3239 MemoryRegion *mr;
149f54b5
PB
3240 hwaddr l = 4;
3241 hwaddr addr1;
50013115 3242 MemTxResult r;
845b6214 3243 uint8_t dirty_log_mask;
4840f10e 3244 bool release_lock = false;
8df1cd07 3245
41063e1e 3246 rcu_read_lock();
2198a121 3247 mr = address_space_translate(as, addr, &addr1, &l,
5c8a00ce
PB
3248 true);
3249 if (l < 4 || !memory_access_is_direct(mr, true)) {
4840f10e 3250 release_lock |= prepare_mmio_access(mr);
125b3806 3251
50013115 3252 r = memory_region_dispatch_write(mr, addr1, val, 4, attrs);
8df1cd07 3253 } else {
5c8a00ce 3254 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
5579c7f3 3255 ptr = qemu_get_ram_ptr(addr1);
8df1cd07 3256 stl_p(ptr, val);
74576198 3257
845b6214
PB
3258 dirty_log_mask = memory_region_get_dirty_log_mask(mr);
3259 dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
58d2707e 3260 cpu_physical_memory_set_dirty_range(addr1, 4, dirty_log_mask);
50013115
PM
3261 r = MEMTX_OK;
3262 }
3263 if (result) {
3264 *result = r;
8df1cd07 3265 }
4840f10e
JK
3266 if (release_lock) {
3267 qemu_mutex_unlock_iothread();
3268 }
41063e1e 3269 rcu_read_unlock();
8df1cd07
FB
3270}
3271
50013115
PM
3272void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val)
3273{
3274 address_space_stl_notdirty(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3275}
3276
8df1cd07 3277/* warning: addr must be aligned */
50013115
PM
3278static inline void address_space_stl_internal(AddressSpace *as,
3279 hwaddr addr, uint32_t val,
3280 MemTxAttrs attrs,
3281 MemTxResult *result,
3282 enum device_endian endian)
8df1cd07 3283{
8df1cd07 3284 uint8_t *ptr;
5c8a00ce 3285 MemoryRegion *mr;
149f54b5
PB
3286 hwaddr l = 4;
3287 hwaddr addr1;
50013115 3288 MemTxResult r;
4840f10e 3289 bool release_lock = false;
8df1cd07 3290
41063e1e 3291 rcu_read_lock();
ab1da857 3292 mr = address_space_translate(as, addr, &addr1, &l,
5c8a00ce
PB
3293 true);
3294 if (l < 4 || !memory_access_is_direct(mr, true)) {
4840f10e 3295 release_lock |= prepare_mmio_access(mr);
125b3806 3296
1e78bcc1
AG
3297#if defined(TARGET_WORDS_BIGENDIAN)
3298 if (endian == DEVICE_LITTLE_ENDIAN) {
3299 val = bswap32(val);
3300 }
3301#else
3302 if (endian == DEVICE_BIG_ENDIAN) {
3303 val = bswap32(val);
3304 }
3305#endif
50013115 3306 r = memory_region_dispatch_write(mr, addr1, val, 4, attrs);
8df1cd07 3307 } else {
8df1cd07 3308 /* RAM case */
5c8a00ce 3309 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
5579c7f3 3310 ptr = qemu_get_ram_ptr(addr1);
1e78bcc1
AG
3311 switch (endian) {
3312 case DEVICE_LITTLE_ENDIAN:
3313 stl_le_p(ptr, val);
3314 break;
3315 case DEVICE_BIG_ENDIAN:
3316 stl_be_p(ptr, val);
3317 break;
3318 default:
3319 stl_p(ptr, val);
3320 break;
3321 }
845b6214 3322 invalidate_and_set_dirty(mr, addr1, 4);
50013115
PM
3323 r = MEMTX_OK;
3324 }
3325 if (result) {
3326 *result = r;
8df1cd07 3327 }
4840f10e
JK
3328 if (release_lock) {
3329 qemu_mutex_unlock_iothread();
3330 }
41063e1e 3331 rcu_read_unlock();
8df1cd07
FB
3332}
3333
50013115
PM
3334void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
3335 MemTxAttrs attrs, MemTxResult *result)
3336{
3337 address_space_stl_internal(as, addr, val, attrs, result,
3338 DEVICE_NATIVE_ENDIAN);
3339}
3340
3341void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
3342 MemTxAttrs attrs, MemTxResult *result)
3343{
3344 address_space_stl_internal(as, addr, val, attrs, result,
3345 DEVICE_LITTLE_ENDIAN);
3346}
3347
3348void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
3349 MemTxAttrs attrs, MemTxResult *result)
3350{
3351 address_space_stl_internal(as, addr, val, attrs, result,
3352 DEVICE_BIG_ENDIAN);
3353}
3354
ab1da857 3355void stl_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3356{
50013115 3357 address_space_stl(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3358}
3359
ab1da857 3360void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3361{
50013115 3362 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3363}
3364
ab1da857 3365void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3366{
50013115 3367 address_space_stl_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3368}
3369
aab33094 3370/* XXX: optimize */
50013115
PM
3371void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
3372 MemTxAttrs attrs, MemTxResult *result)
aab33094
FB
3373{
3374 uint8_t v = val;
50013115
PM
3375 MemTxResult r;
3376
3377 r = address_space_rw(as, addr, attrs, &v, 1, 1);
3378 if (result) {
3379 *result = r;
3380 }
3381}
3382
3383void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3384{
3385 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
aab33094
FB
3386}
3387
733f0b02 3388/* warning: addr must be aligned */
50013115
PM
3389static inline void address_space_stw_internal(AddressSpace *as,
3390 hwaddr addr, uint32_t val,
3391 MemTxAttrs attrs,
3392 MemTxResult *result,
3393 enum device_endian endian)
aab33094 3394{
733f0b02 3395 uint8_t *ptr;
5c8a00ce 3396 MemoryRegion *mr;
149f54b5
PB
3397 hwaddr l = 2;
3398 hwaddr addr1;
50013115 3399 MemTxResult r;
4840f10e 3400 bool release_lock = false;
733f0b02 3401
41063e1e 3402 rcu_read_lock();
5ce5944d 3403 mr = address_space_translate(as, addr, &addr1, &l, true);
5c8a00ce 3404 if (l < 2 || !memory_access_is_direct(mr, true)) {
4840f10e 3405 release_lock |= prepare_mmio_access(mr);
125b3806 3406
1e78bcc1
AG
3407#if defined(TARGET_WORDS_BIGENDIAN)
3408 if (endian == DEVICE_LITTLE_ENDIAN) {
3409 val = bswap16(val);
3410 }
3411#else
3412 if (endian == DEVICE_BIG_ENDIAN) {
3413 val = bswap16(val);
3414 }
3415#endif
50013115 3416 r = memory_region_dispatch_write(mr, addr1, val, 2, attrs);
733f0b02 3417 } else {
733f0b02 3418 /* RAM case */
5c8a00ce 3419 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
733f0b02 3420 ptr = qemu_get_ram_ptr(addr1);
1e78bcc1
AG
3421 switch (endian) {
3422 case DEVICE_LITTLE_ENDIAN:
3423 stw_le_p(ptr, val);
3424 break;
3425 case DEVICE_BIG_ENDIAN:
3426 stw_be_p(ptr, val);
3427 break;
3428 default:
3429 stw_p(ptr, val);
3430 break;
3431 }
845b6214 3432 invalidate_and_set_dirty(mr, addr1, 2);
50013115
PM
3433 r = MEMTX_OK;
3434 }
3435 if (result) {
3436 *result = r;
733f0b02 3437 }
4840f10e
JK
3438 if (release_lock) {
3439 qemu_mutex_unlock_iothread();
3440 }
41063e1e 3441 rcu_read_unlock();
aab33094
FB
3442}
3443
50013115
PM
3444void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
3445 MemTxAttrs attrs, MemTxResult *result)
3446{
3447 address_space_stw_internal(as, addr, val, attrs, result,
3448 DEVICE_NATIVE_ENDIAN);
3449}
3450
3451void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
3452 MemTxAttrs attrs, MemTxResult *result)
3453{
3454 address_space_stw_internal(as, addr, val, attrs, result,
3455 DEVICE_LITTLE_ENDIAN);
3456}
3457
3458void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
3459 MemTxAttrs attrs, MemTxResult *result)
3460{
3461 address_space_stw_internal(as, addr, val, attrs, result,
3462 DEVICE_BIG_ENDIAN);
3463}
3464
5ce5944d 3465void stw_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3466{
50013115 3467 address_space_stw(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3468}
3469
5ce5944d 3470void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3471{
50013115 3472 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3473}
3474
5ce5944d 3475void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
1e78bcc1 3476{
50013115 3477 address_space_stw_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3478}
3479
aab33094 3480/* XXX: optimize */
50013115
PM
3481void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
3482 MemTxAttrs attrs, MemTxResult *result)
aab33094 3483{
50013115 3484 MemTxResult r;
aab33094 3485 val = tswap64(val);
50013115
PM
3486 r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3487 if (result) {
3488 *result = r;
3489 }
aab33094
FB
3490}
3491
50013115
PM
3492void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
3493 MemTxAttrs attrs, MemTxResult *result)
1e78bcc1 3494{
50013115 3495 MemTxResult r;
1e78bcc1 3496 val = cpu_to_le64(val);
50013115
PM
3497 r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3498 if (result) {
3499 *result = r;
3500 }
3501}
3502void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
3503 MemTxAttrs attrs, MemTxResult *result)
3504{
3505 MemTxResult r;
3506 val = cpu_to_be64(val);
3507 r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3508 if (result) {
3509 *result = r;
3510 }
3511}
3512
3513void stq_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3514{
3515 address_space_stq(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3516}
3517
3518void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3519{
3520 address_space_stq_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3521}
3522
f606604f 3523void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val)
1e78bcc1 3524{
50013115 3525 address_space_stq_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
1e78bcc1
AG
3526}
3527
5e2972fd 3528/* virtual memory access for debug (includes writing to ROM) */
f17ec444 3529int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
b448f2f3 3530 uint8_t *buf, int len, int is_write)
13eb76e0
FB
3531{
3532 int l;
a8170e5e 3533 hwaddr phys_addr;
9b3c35e0 3534 target_ulong page;
13eb76e0
FB
3535
3536 while (len > 0) {
3537 page = addr & TARGET_PAGE_MASK;
f17ec444 3538 phys_addr = cpu_get_phys_page_debug(cpu, page);
13eb76e0
FB
3539 /* if no physical page mapped, return an error */
3540 if (phys_addr == -1)
3541 return -1;
3542 l = (page + TARGET_PAGE_SIZE) - addr;
3543 if (l > len)
3544 l = len;
5e2972fd 3545 phys_addr += (addr & ~TARGET_PAGE_MASK);
2e38847b
EI
3546 if (is_write) {
3547 cpu_physical_memory_write_rom(cpu->as, phys_addr, buf, l);
3548 } else {
5c9eb028
PM
3549 address_space_rw(cpu->as, phys_addr, MEMTXATTRS_UNSPECIFIED,
3550 buf, l, 0);
2e38847b 3551 }
13eb76e0
FB
3552 len -= l;
3553 buf += l;
3554 addr += l;
3555 }
3556 return 0;
3557}
038629a6
DDAG
3558
3559/*
3560 * Allows code that needs to deal with migration bitmaps etc to still be built
3561 * target independent.
3562 */
3563size_t qemu_target_page_bits(void)
3564{
3565 return TARGET_PAGE_BITS;
3566}
3567
a68fe89c 3568#endif
13eb76e0 3569
8e4a424b
BS
3570/*
3571 * A helper function for the _utterly broken_ virtio device model to find out if
3572 * it's running on a big endian machine. Don't do this at home kids!
3573 */
98ed8ecf
GK
3574bool target_words_bigendian(void);
3575bool target_words_bigendian(void)
8e4a424b
BS
3576{
3577#if defined(TARGET_WORDS_BIGENDIAN)
3578 return true;
3579#else
3580 return false;
3581#endif
3582}
3583
76f35538 3584#ifndef CONFIG_USER_ONLY
a8170e5e 3585bool cpu_physical_memory_is_io(hwaddr phys_addr)
76f35538 3586{
5c8a00ce 3587 MemoryRegion*mr;
149f54b5 3588 hwaddr l = 1;
41063e1e 3589 bool res;
76f35538 3590
41063e1e 3591 rcu_read_lock();
5c8a00ce
PB
3592 mr = address_space_translate(&address_space_memory,
3593 phys_addr, &phys_addr, &l, false);
76f35538 3594
41063e1e
PB
3595 res = !(memory_region_is_ram(mr) || memory_region_is_romd(mr));
3596 rcu_read_unlock();
3597 return res;
76f35538 3598}
bd2fa51f 3599
e3807054 3600int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
bd2fa51f
MH
3601{
3602 RAMBlock *block;
e3807054 3603 int ret = 0;
bd2fa51f 3604
0dc3f44a
MD
3605 rcu_read_lock();
3606 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
e3807054
DDAG
3607 ret = func(block->idstr, block->host, block->offset,
3608 block->used_length, opaque);
3609 if (ret) {
3610 break;
3611 }
bd2fa51f 3612 }
0dc3f44a 3613 rcu_read_unlock();
e3807054 3614 return ret;
bd2fa51f 3615}
ec3f8c99 3616#endif