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