]> git.proxmox.com Git - mirror_qemu.git/blame - include/exec/memory.h
memory: Move assignment to ram_block to memory_region_init_*
[mirror_qemu.git] / include / exec / memory.h
CommitLineData
093bc2cd
AK
1/*
2 * Physical memory management API
3 *
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#ifndef MEMORY_H
15#define MEMORY_H
16
17#ifndef CONFIG_USER_ONLY
18
1ab4c8ce
JQ
19#define DIRTY_MEMORY_VGA 0
20#define DIRTY_MEMORY_CODE 1
21#define DIRTY_MEMORY_MIGRATION 2
22#define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
23
022c62cb 24#include "exec/cpu-common.h"
ce927ed9 25#ifndef CONFIG_USER_ONLY
022c62cb 26#include "exec/hwaddr.h"
ce927ed9 27#endif
cc05c43a 28#include "exec/memattrs.h"
1de7afc9 29#include "qemu/queue.h"
1de7afc9 30#include "qemu/int128.h"
06866575 31#include "qemu/notify.h"
b4fefef9 32#include "qom/object.h"
374f2981 33#include "qemu/rcu.h"
58eaa217 34#include "qemu/typedefs.h"
093bc2cd 35
052e87b0
PB
36#define MAX_PHYS_ADDR_SPACE_BITS 62
37#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
38
b4fefef9
PC
39#define TYPE_MEMORY_REGION "qemu:memory-region"
40#define MEMORY_REGION(obj) \
41 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
42
093bc2cd 43typedef struct MemoryRegionOps MemoryRegionOps;
74901c3b 44typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd 45
74901c3b
AK
46struct MemoryRegionMmio {
47 CPUReadMemoryFunc *read[3];
48 CPUWriteMemoryFunc *write[3];
49};
50
30951157
AK
51typedef struct IOMMUTLBEntry IOMMUTLBEntry;
52
53/* See address_space_translate: bit 0 is read, bit 1 is write. */
54typedef enum {
55 IOMMU_NONE = 0,
56 IOMMU_RO = 1,
57 IOMMU_WO = 2,
58 IOMMU_RW = 3,
59} IOMMUAccessFlags;
60
61struct IOMMUTLBEntry {
62 AddressSpace *target_as;
63 hwaddr iova;
64 hwaddr translated_addr;
65 hwaddr addr_mask; /* 0xfff = 4k translation */
66 IOMMUAccessFlags perm;
67};
68
cc05c43a
PM
69/* New-style MMIO accessors can indicate that the transaction failed.
70 * A zero (MEMTX_OK) response means success; anything else is a failure
71 * of some kind. The memory subsystem will bitwise-OR together results
72 * if it is synthesizing an operation from multiple smaller accesses.
73 */
74#define MEMTX_OK 0
75#define MEMTX_ERROR (1U << 0) /* device returned an error */
76#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
77typedef uint32_t MemTxResult;
78
093bc2cd
AK
79/*
80 * Memory region callbacks
81 */
82struct MemoryRegionOps {
83 /* Read from the memory region. @addr is relative to @mr; @size is
84 * in bytes. */
85 uint64_t (*read)(void *opaque,
a8170e5e 86 hwaddr addr,
093bc2cd
AK
87 unsigned size);
88 /* Write to the memory region. @addr is relative to @mr; @size is
89 * in bytes. */
90 void (*write)(void *opaque,
a8170e5e 91 hwaddr addr,
093bc2cd
AK
92 uint64_t data,
93 unsigned size);
94
cc05c43a
PM
95 MemTxResult (*read_with_attrs)(void *opaque,
96 hwaddr addr,
97 uint64_t *data,
98 unsigned size,
99 MemTxAttrs attrs);
100 MemTxResult (*write_with_attrs)(void *opaque,
101 hwaddr addr,
102 uint64_t data,
103 unsigned size,
104 MemTxAttrs attrs);
105
093bc2cd
AK
106 enum device_endian endianness;
107 /* Guest-visible constraints: */
108 struct {
109 /* If nonzero, specify bounds on access sizes beyond which a machine
110 * check is thrown.
111 */
112 unsigned min_access_size;
113 unsigned max_access_size;
114 /* If true, unaligned accesses are supported. Otherwise unaligned
115 * accesses throw machine checks.
116 */
117 bool unaligned;
897fa7cf
AK
118 /*
119 * If present, and returns #false, the transaction is not accepted
120 * by the device (and results in machine dependent behaviour such
121 * as a machine check exception).
122 */
a8170e5e 123 bool (*accepts)(void *opaque, hwaddr addr,
897fa7cf 124 unsigned size, bool is_write);
093bc2cd
AK
125 } valid;
126 /* Internal implementation constraints: */
127 struct {
128 /* If nonzero, specifies the minimum size implemented. Smaller sizes
129 * will be rounded upwards and a partial result will be returned.
130 */
131 unsigned min_access_size;
132 /* If nonzero, specifies the maximum size implemented. Larger sizes
133 * will be done as a series of accesses with smaller sizes.
134 */
135 unsigned max_access_size;
136 /* If true, unaligned accesses are supported. Otherwise all accesses
137 * are converted to (possibly multiple) naturally aligned accesses.
138 */
edc1ba7a 139 bool unaligned;
093bc2cd 140 } impl;
627a0e90 141
74901c3b
AK
142 /* If .read and .write are not present, old_mmio may be used for
143 * backwards compatibility with old mmio registration
144 */
145 const MemoryRegionMmio old_mmio;
093bc2cd
AK
146};
147
30951157
AK
148typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
149
150struct MemoryRegionIOMMUOps {
151 /* Return a TLB entry that contains a given address. */
8d7b8cb9 152 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
30951157
AK
153};
154
093bc2cd 155typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 156typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
157
158struct MemoryRegion {
b4fefef9 159 Object parent_obj;
a676854f 160
093bc2cd 161 /* All fields are private - violators will be prosecuted */
a676854f
PB
162
163 /* The following fields should fit in a cache line */
164 bool romd_mode;
165 bool ram;
166 bool subpage;
167 bool readonly; /* For RAM regions */
168 bool rom_device;
169 bool flush_coalesced_mmio;
170 bool global_locking;
171 uint8_t dirty_log_mask;
172 ram_addr_t ram_addr;
58eaa217 173 RAMBlock *ram_block;
612263cf 174 Object *owner;
30951157 175 const MemoryRegionIOMMUOps *iommu_ops;
a676854f
PB
176
177 const MemoryRegionOps *ops;
093bc2cd 178 void *opaque;
feca4ac1 179 MemoryRegion *container;
08dafab4 180 Int128 size;
a8170e5e 181 hwaddr addr;
545e92e0 182 void (*destructor)(MemoryRegion *mr);
a2b257d6 183 uint64_t align;
14a3c10a 184 bool terminates;
e4dc3f59 185 bool skip_dump;
6bba19ba 186 bool enabled;
1660e72d 187 bool warning_printed; /* For reservations */
deb809ed 188 uint8_t vga_logging_count;
093bc2cd 189 MemoryRegion *alias;
a8170e5e 190 hwaddr alias_offset;
d33382da 191 int32_t priority;
093bc2cd
AK
192 bool may_overlap;
193 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
194 QTAILQ_ENTRY(MemoryRegion) subregions_link;
195 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
302fa283 196 const char *name;
3e9d69e7
AK
197 unsigned ioeventfd_nb;
198 MemoryRegionIoeventfd *ioeventfds;
06866575 199 NotifierList iommu_notify;
093bc2cd
AK
200};
201
c2fc83e8
PB
202/**
203 * MemoryListener: callbacks structure for updates to the physical memory map
204 *
205 * Allows a component to adjust to changes in the guest-visible memory map.
206 * Use with memory_listener_register() and memory_listener_unregister().
207 */
208struct MemoryListener {
209 void (*begin)(MemoryListener *listener);
210 void (*commit)(MemoryListener *listener);
211 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
212 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
213 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
b2dfd71c
PB
214 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
215 int old, int new);
216 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
217 int old, int new);
c2fc83e8
PB
218 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
219 void (*log_global_start)(MemoryListener *listener);
220 void (*log_global_stop)(MemoryListener *listener);
221 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
222 bool match_data, uint64_t data, EventNotifier *e);
223 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
224 bool match_data, uint64_t data, EventNotifier *e);
225 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
226 hwaddr addr, hwaddr len);
227 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
228 hwaddr addr, hwaddr len);
229 /* Lower = earlier (during add), later (during del) */
230 unsigned priority;
231 AddressSpace *address_space_filter;
232 QTAILQ_ENTRY(MemoryListener) link;
233};
234
9ad2bbc1
AK
235/**
236 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
237 */
238struct AddressSpace {
239 /* All fields are private. */
374f2981 240 struct rcu_head rcu;
7dca8043 241 char *name;
9ad2bbc1 242 MemoryRegion *root;
f0c02d15
PC
243 int ref_count;
244 bool malloced;
374f2981
PB
245
246 /* Accessed via RCU. */
9ad2bbc1 247 struct FlatView *current_map;
374f2981 248
9ad2bbc1
AK
249 int ioeventfd_nb;
250 struct MemoryRegionIoeventfd *ioeventfds;
ac1970fb 251 struct AddressSpaceDispatch *dispatch;
00752703 252 struct AddressSpaceDispatch *next_dispatch;
89ae337a
PB
253 MemoryListener dispatch_listener;
254
0d673e36 255 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
9ad2bbc1
AK
256};
257
e2177955
AK
258/**
259 * MemoryRegionSection: describes a fragment of a #MemoryRegion
260 *
261 * @mr: the region, or %NULL if empty
7664e80c 262 * @address_space: the address space the region is mapped in
e2177955
AK
263 * @offset_within_region: the beginning of the section, relative to @mr's start
264 * @size: the size of the section; will not exceed @mr's boundaries
265 * @offset_within_address_space: the address of the first byte of the section
266 * relative to the region's address space
7a8499e8 267 * @readonly: writes to this section are ignored
e2177955
AK
268 */
269struct MemoryRegionSection {
270 MemoryRegion *mr;
f6790af6 271 AddressSpace *address_space;
a8170e5e 272 hwaddr offset_within_region;
052e87b0 273 Int128 size;
a8170e5e 274 hwaddr offset_within_address_space;
7a8499e8 275 bool readonly;
e2177955
AK
276};
277
093bc2cd
AK
278/**
279 * memory_region_init: Initialize a memory region
280 *
69ddaf66 281 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
282 * memory_region_add_subregion() to add subregions.
283 *
284 * @mr: the #MemoryRegion to be initialized
2c9b15ca 285 * @owner: the object that tracks the region's reference count
093bc2cd
AK
286 * @name: used for debugging; not visible to the user or ABI
287 * @size: size of the region; any subregions beyond this size will be clipped
288 */
289void memory_region_init(MemoryRegion *mr,
2c9b15ca 290 struct Object *owner,
093bc2cd
AK
291 const char *name,
292 uint64_t size);
46637be2
PB
293
294/**
295 * memory_region_ref: Add 1 to a memory region's reference count
296 *
297 * Whenever memory regions are accessed outside the BQL, they need to be
298 * preserved against hot-unplug. MemoryRegions actually do not have their
299 * own reference count; they piggyback on a QOM object, their "owner".
300 * This function adds a reference to the owner.
301 *
302 * All MemoryRegions must have an owner if they can disappear, even if the
303 * device they belong to operates exclusively under the BQL. This is because
304 * the region could be returned at any time by memory_region_find, and this
305 * is usually under guest control.
306 *
307 * @mr: the #MemoryRegion
308 */
309void memory_region_ref(MemoryRegion *mr);
310
311/**
312 * memory_region_unref: Remove 1 to a memory region's reference count
313 *
314 * Whenever memory regions are accessed outside the BQL, they need to be
315 * preserved against hot-unplug. MemoryRegions actually do not have their
316 * own reference count; they piggyback on a QOM object, their "owner".
317 * This function removes a reference to the owner and possibly destroys it.
318 *
319 * @mr: the #MemoryRegion
320 */
321void memory_region_unref(MemoryRegion *mr);
322
093bc2cd
AK
323/**
324 * memory_region_init_io: Initialize an I/O memory region.
325 *
69ddaf66 326 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
327 * if @size is nonzero, subregions will be clipped to @size.
328 *
329 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 330 * @owner: the object that tracks the region's reference count
093bc2cd
AK
331 * @ops: a structure containing read and write callbacks to be used when
332 * I/O is performed on the region.
b6af0975 333 * @opaque: passed to the read and write callbacks of the @ops structure.
093bc2cd
AK
334 * @name: used for debugging; not visible to the user or ABI
335 * @size: size of the region.
336 */
337void memory_region_init_io(MemoryRegion *mr,
2c9b15ca 338 struct Object *owner,
093bc2cd
AK
339 const MemoryRegionOps *ops,
340 void *opaque,
341 const char *name,
342 uint64_t size);
343
344/**
345 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
69ddaf66 346 * region will modify memory directly.
093bc2cd
AK
347 *
348 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 349 * @owner: the object that tracks the region's reference count
c5705a77 350 * @name: the name of the region.
093bc2cd 351 * @size: size of the region.
49946538 352 * @errp: pointer to Error*, to store an error if it happens.
093bc2cd
AK
353 */
354void memory_region_init_ram(MemoryRegion *mr,
2c9b15ca 355 struct Object *owner,
093bc2cd 356 const char *name,
49946538
HT
357 uint64_t size,
358 Error **errp);
093bc2cd 359
60786ef3
MT
360/**
361 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
362 * RAM. Accesses into the region will
363 * modify memory directly. Only an initial
364 * portion of this RAM is actually used.
365 * The used size can change across reboots.
366 *
367 * @mr: the #MemoryRegion to be initialized.
368 * @owner: the object that tracks the region's reference count
369 * @name: the name of the region.
370 * @size: used size of the region.
371 * @max_size: max size of the region.
372 * @resized: callback to notify owner about used size change.
373 * @errp: pointer to Error*, to store an error if it happens.
374 */
375void memory_region_init_resizeable_ram(MemoryRegion *mr,
376 struct Object *owner,
377 const char *name,
378 uint64_t size,
379 uint64_t max_size,
380 void (*resized)(const char*,
381 uint64_t length,
382 void *host),
383 Error **errp);
0b183fc8
PB
384#ifdef __linux__
385/**
386 * memory_region_init_ram_from_file: Initialize RAM memory region with a
387 * mmap-ed backend.
388 *
389 * @mr: the #MemoryRegion to be initialized.
390 * @owner: the object that tracks the region's reference count
391 * @name: the name of the region.
392 * @size: size of the region.
dbcb8981 393 * @share: %true if memory must be mmaped with the MAP_SHARED flag
0b183fc8 394 * @path: the path in which to allocate the RAM.
7f56e740 395 * @errp: pointer to Error*, to store an error if it happens.
0b183fc8
PB
396 */
397void memory_region_init_ram_from_file(MemoryRegion *mr,
398 struct Object *owner,
399 const char *name,
400 uint64_t size,
dbcb8981 401 bool share,
7f56e740
PB
402 const char *path,
403 Error **errp);
0b183fc8
PB
404#endif
405
093bc2cd 406/**
1a7e8cae
BZ
407 * memory_region_init_ram_ptr: Initialize RAM memory region from a
408 * user-provided pointer. Accesses into the
409 * region will modify memory directly.
093bc2cd
AK
410 *
411 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 412 * @owner: the object that tracks the region's reference count
c5705a77 413 * @name: the name of the region.
093bc2cd
AK
414 * @size: size of the region.
415 * @ptr: memory to be mapped; must contain at least @size bytes.
416 */
417void memory_region_init_ram_ptr(MemoryRegion *mr,
2c9b15ca 418 struct Object *owner,
093bc2cd
AK
419 const char *name,
420 uint64_t size,
421 void *ptr);
422
423/**
424 * memory_region_init_alias: Initialize a memory region that aliases all or a
425 * part of another memory region.
426 *
427 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 428 * @owner: the object that tracks the region's reference count
093bc2cd
AK
429 * @name: used for debugging; not visible to the user or ABI
430 * @orig: the region to be referenced; @mr will be equivalent to
431 * @orig between @offset and @offset + @size - 1.
432 * @offset: start of the section in @orig to be referenced.
433 * @size: size of the region.
434 */
435void memory_region_init_alias(MemoryRegion *mr,
2c9b15ca 436 struct Object *owner,
093bc2cd
AK
437 const char *name,
438 MemoryRegion *orig,
a8170e5e 439 hwaddr offset,
093bc2cd 440 uint64_t size);
d0a9b5bc
AK
441
442/**
443 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
444 * handled via callbacks.
445 *
6d6d2abf
PF
446 * If NULL callbacks pointer is given, then I/O space is not supposed to be
447 * handled by QEMU itself. Any access via the memory API will cause an abort().
448 *
d0a9b5bc 449 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 450 * @owner: the object that tracks the region's reference count
d0a9b5bc 451 * @ops: callbacks for write access handling.
c5705a77 452 * @name: the name of the region.
d0a9b5bc 453 * @size: size of the region.
33e0eb52 454 * @errp: pointer to Error*, to store an error if it happens.
d0a9b5bc
AK
455 */
456void memory_region_init_rom_device(MemoryRegion *mr,
2c9b15ca 457 struct Object *owner,
d0a9b5bc 458 const MemoryRegionOps *ops,
75f5941c 459 void *opaque,
d0a9b5bc 460 const char *name,
33e0eb52
HT
461 uint64_t size,
462 Error **errp);
d0a9b5bc 463
1660e72d
JK
464/**
465 * memory_region_init_reservation: Initialize a memory region that reserves
466 * I/O space.
467 *
468 * A reservation region primariy serves debugging purposes. It claims I/O
469 * space that is not supposed to be handled by QEMU itself. Any access via
470 * the memory API will cause an abort().
6d6d2abf
PF
471 * This function is deprecated. Use memory_region_init_io() with NULL
472 * callbacks instead.
1660e72d
JK
473 *
474 * @mr: the #MemoryRegion to be initialized
2c9b15ca 475 * @owner: the object that tracks the region's reference count
1660e72d
JK
476 * @name: used for debugging; not visible to the user or ABI
477 * @size: size of the region.
478 */
6d6d2abf
PF
479static inline void memory_region_init_reservation(MemoryRegion *mr,
480 Object *owner,
1660e72d 481 const char *name,
6d6d2abf
PF
482 uint64_t size)
483{
484 memory_region_init_io(mr, owner, NULL, mr, name, size);
485}
30951157
AK
486
487/**
488 * memory_region_init_iommu: Initialize a memory region that translates
489 * addresses
490 *
491 * An IOMMU region translates addresses and forwards accesses to a target
492 * memory region.
493 *
494 * @mr: the #MemoryRegion to be initialized
2c9b15ca 495 * @owner: the object that tracks the region's reference count
30951157
AK
496 * @ops: a function that translates addresses into the @target region
497 * @name: used for debugging; not visible to the user or ABI
498 * @size: size of the region.
499 */
500void memory_region_init_iommu(MemoryRegion *mr,
2c9b15ca 501 struct Object *owner,
30951157
AK
502 const MemoryRegionIOMMUOps *ops,
503 const char *name,
504 uint64_t size);
505
803c0816
PB
506/**
507 * memory_region_owner: get a memory region's owner.
508 *
509 * @mr: the memory region being queried.
510 */
511struct Object *memory_region_owner(MemoryRegion *mr);
512
093bc2cd
AK
513/**
514 * memory_region_size: get a memory region's size.
515 *
516 * @mr: the memory region being queried.
517 */
518uint64_t memory_region_size(MemoryRegion *mr);
519
8ea9252a
AK
520/**
521 * memory_region_is_ram: check whether a memory region is random access
522 *
523 * Returns %true is a memory region is random access.
524 *
525 * @mr: the memory region being queried
526 */
1619d1fe
PB
527static inline bool memory_region_is_ram(MemoryRegion *mr)
528{
529 return mr->ram;
530}
8ea9252a 531
e4dc3f59
ND
532/**
533 * memory_region_is_skip_dump: check whether a memory region should not be
534 * dumped
535 *
536 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
537 *
538 * @mr: the memory region being queried
539 */
540bool memory_region_is_skip_dump(MemoryRegion *mr);
541
542/**
543 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
544 * region
545 *
546 * @mr: the memory region being queried
547 */
548void memory_region_set_skip_dump(MemoryRegion *mr);
549
fd062573 550/**
5f9a5ea1 551 * memory_region_is_romd: check whether a memory region is in ROMD mode
fd062573 552 *
5f9a5ea1 553 * Returns %true if a memory region is a ROM device and currently set to allow
fd062573
BS
554 * direct reads.
555 *
556 * @mr: the memory region being queried
557 */
558static inline bool memory_region_is_romd(MemoryRegion *mr)
559{
5f9a5ea1 560 return mr->rom_device && mr->romd_mode;
fd062573
BS
561}
562
30951157
AK
563/**
564 * memory_region_is_iommu: check whether a memory region is an iommu
565 *
566 * Returns %true is a memory region is an iommu.
567 *
568 * @mr: the memory region being queried
569 */
1619d1fe
PB
570static inline bool memory_region_is_iommu(MemoryRegion *mr)
571{
572 return mr->iommu_ops;
573}
574
30951157 575
06866575
DG
576/**
577 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
578 *
579 * @mr: the memory region that was changed
580 * @entry: the new entry in the IOMMU translation table. The entry
581 * replaces all old entries for the same virtual I/O address range.
582 * Deleted entries have .@perm == 0.
583 */
584void memory_region_notify_iommu(MemoryRegion *mr,
585 IOMMUTLBEntry entry);
586
587/**
588 * memory_region_register_iommu_notifier: register a notifier for changes to
589 * IOMMU translation entries.
590 *
591 * @mr: the memory region to observe
592 * @n: the notifier to be added; the notifier receives a pointer to an
593 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
594 * valid on exit from the notifier.
595 */
596void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
597
a788f227
DG
598/**
599 * memory_region_iommu_replay: replay existing IOMMU translations to
600 * a notifier
601 *
602 * @mr: the memory region to observe
603 * @n: the notifier to which to replay iommu mappings
604 * @granularity: Minimum page granularity to replay notifications for
605 * @is_write: Whether to treat the replay as a translate "write"
606 * through the iommu
607 */
608void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
609 hwaddr granularity, bool is_write);
610
06866575
DG
611/**
612 * memory_region_unregister_iommu_notifier: unregister a notifier for
613 * changes to IOMMU translation entries.
614 *
615 * @n: the notifier to be removed.
616 */
617void memory_region_unregister_iommu_notifier(Notifier *n);
618
8991c79b
AK
619/**
620 * memory_region_name: get a memory region's name
621 *
622 * Returns the string that was used to initialize the memory region.
623 *
624 * @mr: the memory region being queried
625 */
5d546d4b 626const char *memory_region_name(const MemoryRegion *mr);
8991c79b 627
55043ba3
AK
628/**
629 * memory_region_is_logging: return whether a memory region is logging writes
630 *
2d1a35be 631 * Returns %true if the memory region is logging writes for the given client
55043ba3
AK
632 *
633 * @mr: the memory region being queried
2d1a35be 634 * @client: the client being queried
55043ba3 635 */
2d1a35be
PB
636bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
637
638/**
639 * memory_region_get_dirty_log_mask: return the clients for which a
640 * memory region is logging writes.
641 *
677e7805
PB
642 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
643 * are the bit indices.
2d1a35be
PB
644 *
645 * @mr: the memory region being queried
646 */
647uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
55043ba3 648
ce7923da
AK
649/**
650 * memory_region_is_rom: check whether a memory region is ROM
651 *
652 * Returns %true is a memory region is read-only memory.
653 *
654 * @mr: the memory region being queried
655 */
1619d1fe
PB
656static inline bool memory_region_is_rom(MemoryRegion *mr)
657{
658 return mr->ram && mr->readonly;
659}
660
ce7923da 661
a35ba7be
PB
662/**
663 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
664 *
665 * Returns a file descriptor backing a file-based RAM memory region,
666 * or -1 if the region is not a file-based RAM memory region.
667 *
668 * @mr: the RAM or alias memory region being queried.
669 */
670int memory_region_get_fd(MemoryRegion *mr);
671
093bc2cd
AK
672/**
673 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
674 *
675 * Returns a host pointer to a RAM memory region (created with
49b24afc
PB
676 * memory_region_init_ram() or memory_region_init_ram_ptr()).
677 *
678 * Use with care; by the time this function returns, the returned pointer is
679 * not protected by RCU anymore. If the caller is not within an RCU critical
680 * section and does not hold the iothread lock, it must have other means of
681 * protecting the pointer, such as a reference to the region that includes
682 * the incoming ram_addr_t.
093bc2cd
AK
683 *
684 * @mr: the memory region being queried.
685 */
686void *memory_region_get_ram_ptr(MemoryRegion *mr);
687
37d7c084
PB
688/* memory_region_ram_resize: Resize a RAM region.
689 *
690 * Only legal before guest might have detected the memory size: e.g. on
691 * incoming migration, or right after reset.
692 *
693 * @mr: a memory region created with @memory_region_init_resizeable_ram.
694 * @newsize: the new size the region
695 * @errp: pointer to Error*, to store an error if it happens.
696 */
697void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
698 Error **errp);
699
093bc2cd
AK
700/**
701 * memory_region_set_log: Turn dirty logging on or off for a region.
702 *
703 * Turns dirty logging on or off for a specified client (display, migration).
704 * Only meaningful for RAM regions.
705 *
706 * @mr: the memory region being updated.
707 * @log: whether dirty logging is to be enabled or disabled.
dbddac6d 708 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
093bc2cd
AK
709 */
710void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
711
712/**
cd7a45c9
BS
713 * memory_region_get_dirty: Check whether a range of bytes is dirty
714 * for a specified client.
093bc2cd 715 *
cd7a45c9 716 * Checks whether a range of bytes has been written to since the last
093bc2cd
AK
717 * call to memory_region_reset_dirty() with the same @client. Dirty logging
718 * must be enabled.
719 *
720 * @mr: the memory region being queried.
721 * @addr: the address (relative to the start of the region) being queried.
cd7a45c9 722 * @size: the size of the range being queried.
093bc2cd
AK
723 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
724 * %DIRTY_MEMORY_VGA.
725 */
a8170e5e
AK
726bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
727 hwaddr size, unsigned client);
093bc2cd
AK
728
729/**
fd4aa979 730 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
093bc2cd 731 *
fd4aa979
BS
732 * Marks a range of bytes as dirty, after it has been dirtied outside
733 * guest code.
093bc2cd 734 *
fd4aa979 735 * @mr: the memory region being dirtied.
093bc2cd 736 * @addr: the address (relative to the start of the region) being dirtied.
fd4aa979 737 * @size: size of the range being dirtied.
093bc2cd 738 */
a8170e5e
AK
739void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
740 hwaddr size);
093bc2cd 741
6c279db8
JQ
742/**
743 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
744 * for a specified client. It clears them.
745 *
746 * Checks whether a range of bytes has been written to since the last
747 * call to memory_region_reset_dirty() with the same @client. Dirty logging
748 * must be enabled.
749 *
750 * @mr: the memory region being queried.
751 * @addr: the address (relative to the start of the region) being queried.
752 * @size: the size of the range being queried.
753 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
754 * %DIRTY_MEMORY_VGA.
755 */
756bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
757 hwaddr size, unsigned client);
093bc2cd
AK
758/**
759 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
760 * any external TLBs (e.g. kvm)
761 *
762 * Flushes dirty information from accelerators such as kvm and vhost-net
763 * and makes it available to users of the memory API.
764 *
765 * @mr: the region being flushed.
766 */
767void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
768
769/**
770 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
771 * client.
772 *
773 * Marks a range of pages as no longer dirty.
774 *
775 * @mr: the region being updated.
776 * @addr: the start of the subrange being cleaned.
777 * @size: the size of the subrange being cleaned.
778 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
779 * %DIRTY_MEMORY_VGA.
780 */
a8170e5e
AK
781void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
782 hwaddr size, unsigned client);
093bc2cd
AK
783
784/**
785 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
786 *
787 * Allows a memory region to be marked as read-only (turning it into a ROM).
788 * only useful on RAM regions.
789 *
790 * @mr: the region being updated.
791 * @readonly: whether rhe region is to be ROM or RAM.
792 */
793void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
794
d0a9b5bc 795/**
5f9a5ea1 796 * memory_region_rom_device_set_romd: enable/disable ROMD mode
d0a9b5bc
AK
797 *
798 * Allows a ROM device (initialized with memory_region_init_rom_device() to
5f9a5ea1
JK
799 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
800 * device is mapped to guest memory and satisfies read access directly.
801 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
802 * Writes are always handled by the #MemoryRegion.write function.
d0a9b5bc
AK
803 *
804 * @mr: the memory region to be updated
5f9a5ea1 805 * @romd_mode: %true to put the region into ROMD mode
d0a9b5bc 806 */
5f9a5ea1 807void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
d0a9b5bc 808
093bc2cd
AK
809/**
810 * memory_region_set_coalescing: Enable memory coalescing for the region.
811 *
812 * Enabled writes to a region to be queued for later processing. MMIO ->write
813 * callbacks may be delayed until a non-coalesced MMIO is issued.
814 * Only useful for IO regions. Roughly similar to write-combining hardware.
815 *
816 * @mr: the memory region to be write coalesced
817 */
818void memory_region_set_coalescing(MemoryRegion *mr);
819
820/**
821 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
822 * a region.
823 *
824 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
825 * Multiple calls can be issued coalesced disjoint ranges.
826 *
827 * @mr: the memory region to be updated.
828 * @offset: the start of the range within the region to be coalesced.
829 * @size: the size of the subrange to be coalesced.
830 */
831void memory_region_add_coalescing(MemoryRegion *mr,
a8170e5e 832 hwaddr offset,
093bc2cd
AK
833 uint64_t size);
834
835/**
836 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
837 *
838 * Disables any coalescing caused by memory_region_set_coalescing() or
839 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
840 * hardware.
841 *
842 * @mr: the memory region to be updated.
843 */
844void memory_region_clear_coalescing(MemoryRegion *mr);
845
d410515e
JK
846/**
847 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
848 * accesses.
849 *
850 * Ensure that pending coalesced MMIO request are flushed before the memory
851 * region is accessed. This property is automatically enabled for all regions
852 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
853 *
854 * @mr: the memory region to be updated.
855 */
856void memory_region_set_flush_coalesced(MemoryRegion *mr);
857
858/**
859 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
860 * accesses.
861 *
862 * Clear the automatic coalesced MMIO flushing enabled via
863 * memory_region_set_flush_coalesced. Note that this service has no effect on
864 * memory regions that have MMIO coalescing enabled for themselves. For them,
865 * automatic flushing will stop once coalescing is disabled.
866 *
867 * @mr: the memory region to be updated.
868 */
869void memory_region_clear_flush_coalesced(MemoryRegion *mr);
870
196ea131
JK
871/**
872 * memory_region_set_global_locking: Declares the access processing requires
873 * QEMU's global lock.
874 *
875 * When this is invoked, accesses to the memory region will be processed while
876 * holding the global lock of QEMU. This is the default behavior of memory
877 * regions.
878 *
879 * @mr: the memory region to be updated.
880 */
881void memory_region_set_global_locking(MemoryRegion *mr);
882
883/**
884 * memory_region_clear_global_locking: Declares that access processing does
885 * not depend on the QEMU global lock.
886 *
887 * By clearing this property, accesses to the memory region will be processed
888 * outside of QEMU's global lock (unless the lock is held on when issuing the
889 * access request). In this case, the device model implementing the access
890 * handlers is responsible for synchronization of concurrency.
891 *
892 * @mr: the memory region to be updated.
893 */
894void memory_region_clear_global_locking(MemoryRegion *mr);
895
3e9d69e7
AK
896/**
897 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
898 * is written to a location.
899 *
900 * Marks a word in an IO region (initialized with memory_region_init_io())
901 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 902 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
903 * action if the callback _is_ called).
904 *
905 * @mr: the memory region being updated.
906 * @addr: the address within @mr that is to be monitored
907 * @size: the size of the access to trigger the eventfd
908 * @match_data: whether to match against @data, instead of just @addr
909 * @data: the data to match against the guest write
910 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
911 **/
912void memory_region_add_eventfd(MemoryRegion *mr,
a8170e5e 913 hwaddr addr,
3e9d69e7
AK
914 unsigned size,
915 bool match_data,
916 uint64_t data,
753d5e14 917 EventNotifier *e);
3e9d69e7
AK
918
919/**
69ddaf66 920 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 921 *
69ddaf66
ASRJ
922 * Cancels an eventfd trigger requested by a previous
923 * memory_region_add_eventfd() call.
3e9d69e7
AK
924 *
925 * @mr: the memory region being updated.
926 * @addr: the address within @mr that is to be monitored
927 * @size: the size of the access to trigger the eventfd
928 * @match_data: whether to match against @data, instead of just @addr
929 * @data: the data to match against the guest write
930 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
931 */
932void memory_region_del_eventfd(MemoryRegion *mr,
a8170e5e 933 hwaddr addr,
3e9d69e7
AK
934 unsigned size,
935 bool match_data,
936 uint64_t data,
753d5e14
PB
937 EventNotifier *e);
938
093bc2cd 939/**
69ddaf66 940 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 941 *
69ddaf66 942 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
943 * subregions (except for those explicitly marked as overlapping). A region
944 * may only be added once as a subregion (unless removed with
945 * memory_region_del_subregion()); use memory_region_init_alias() if you
946 * want a region to be a subregion in multiple locations.
947 *
948 * @mr: the region to contain the new subregion; must be a container
949 * initialized with memory_region_init().
950 * @offset: the offset relative to @mr where @subregion is added.
951 * @subregion: the subregion to be added.
952 */
953void memory_region_add_subregion(MemoryRegion *mr,
a8170e5e 954 hwaddr offset,
093bc2cd
AK
955 MemoryRegion *subregion);
956/**
1a7e8cae
BZ
957 * memory_region_add_subregion_overlap: Add a subregion to a container
958 * with overlap.
093bc2cd 959 *
69ddaf66 960 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
961 * subregions. Conflicts are resolved by having a higher @priority hide a
962 * lower @priority. Subregions without priority are taken as @priority 0.
963 * A region may only be added once as a subregion (unless removed with
964 * memory_region_del_subregion()); use memory_region_init_alias() if you
965 * want a region to be a subregion in multiple locations.
966 *
967 * @mr: the region to contain the new subregion; must be a container
968 * initialized with memory_region_init().
969 * @offset: the offset relative to @mr where @subregion is added.
970 * @subregion: the subregion to be added.
971 * @priority: used for resolving overlaps; highest priority wins.
972 */
973void memory_region_add_subregion_overlap(MemoryRegion *mr,
a8170e5e 974 hwaddr offset,
093bc2cd 975 MemoryRegion *subregion,
a1ff8ae0 976 int priority);
e34911c4
AK
977
978/**
979 * memory_region_get_ram_addr: Get the ram address associated with a memory
980 * region
981 *
dabdf394 982 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
e34911c4
AK
983 * code is being reworked.
984 */
1619d1fe
PB
985static inline ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
986{
987 return mr->ram_addr;
988}
e34911c4 989
a2b257d6 990uint64_t memory_region_get_alignment(const MemoryRegion *mr);
093bc2cd
AK
991/**
992 * memory_region_del_subregion: Remove a subregion.
993 *
994 * Removes a subregion from its container.
995 *
996 * @mr: the container to be updated.
997 * @subregion: the region being removed; must be a current subregion of @mr.
998 */
999void memory_region_del_subregion(MemoryRegion *mr,
1000 MemoryRegion *subregion);
1001
6bba19ba
AK
1002/*
1003 * memory_region_set_enabled: dynamically enable or disable a region
1004 *
1005 * Enables or disables a memory region. A disabled memory region
1006 * ignores all accesses to itself and its subregions. It does not
1007 * obscure sibling subregions with lower priority - it simply behaves as
1008 * if it was removed from the hierarchy.
1009 *
1010 * Regions default to being enabled.
1011 *
1012 * @mr: the region to be updated
1013 * @enabled: whether to enable or disable the region
1014 */
1015void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1016
2282e1af
AK
1017/*
1018 * memory_region_set_address: dynamically update the address of a region
1019 *
feca4ac1 1020 * Dynamically updates the address of a region, relative to its container.
2282e1af
AK
1021 * May be used on regions are currently part of a memory hierarchy.
1022 *
1023 * @mr: the region to be updated
feca4ac1 1024 * @addr: new address, relative to container region
2282e1af 1025 */
a8170e5e 1026void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
2282e1af 1027
e7af4c67
MT
1028/*
1029 * memory_region_set_size: dynamically update the size of a region.
1030 *
1031 * Dynamically updates the size of a region.
1032 *
1033 * @mr: the region to be updated
1034 * @size: used size of the region.
1035 */
1036void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1037
4703359e
AK
1038/*
1039 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1040 *
1041 * Dynamically updates the offset into the target region that an alias points
1042 * to, as if the fourth argument to memory_region_init_alias() has changed.
1043 *
1044 * @mr: the #MemoryRegion to be updated; should be an alias.
1045 * @offset: the new offset into the target memory region
1046 */
1047void memory_region_set_alias_offset(MemoryRegion *mr,
a8170e5e 1048 hwaddr offset);
4703359e 1049
3ce10901 1050/**
feca4ac1
PB
1051 * memory_region_present: checks if an address relative to a @container
1052 * translates into #MemoryRegion within @container
3ce10901 1053 *
feca4ac1 1054 * Answer whether a #MemoryRegion within @container covers the address
3ce10901
PB
1055 * @addr.
1056 *
feca4ac1
PB
1057 * @container: a #MemoryRegion within which @addr is a relative address
1058 * @addr: the area within @container to be searched
3ce10901 1059 */
feca4ac1 1060bool memory_region_present(MemoryRegion *container, hwaddr addr);
3ce10901 1061
eed2bacf
IM
1062/**
1063 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1064 * into any address space.
1065 *
1066 * @mr: a #MemoryRegion which should be checked if it's mapped
1067 */
1068bool memory_region_is_mapped(MemoryRegion *mr);
1069
e2177955 1070/**
73034e9e
PB
1071 * memory_region_find: translate an address/size relative to a
1072 * MemoryRegion into a #MemoryRegionSection.
e2177955 1073 *
73034e9e
PB
1074 * Locates the first #MemoryRegion within @mr that overlaps the range
1075 * given by @addr and @size.
e2177955
AK
1076 *
1077 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1078 * It will have the following characteristics:
e2177955
AK
1079 * .@size = 0 iff no overlap was found
1080 * .@mr is non-%NULL iff an overlap was found
1081 *
73034e9e
PB
1082 * Remember that in the return value the @offset_within_region is
1083 * relative to the returned region (in the .@mr field), not to the
1084 * @mr argument.
1085 *
1086 * Similarly, the .@offset_within_address_space is relative to the
1087 * address space that contains both regions, the passed and the
1088 * returned one. However, in the special case where the @mr argument
feca4ac1 1089 * has no container (and thus is the root of the address space), the
73034e9e
PB
1090 * following will hold:
1091 * .@offset_within_address_space >= @addr
1092 * .@offset_within_address_space + .@size <= @addr + @size
1093 *
1094 * @mr: a MemoryRegion within which @addr is a relative address
1095 * @addr: start of the area within @as to be searched
e2177955
AK
1096 * @size: size of the area to be searched
1097 */
73034e9e 1098MemoryRegionSection memory_region_find(MemoryRegion *mr,
a8170e5e 1099 hwaddr addr, uint64_t size);
e2177955 1100
86e775c6 1101/**
1d671369 1102 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
86e775c6
AK
1103 *
1104 * Synchronizes the dirty page log for an entire address space.
1d671369 1105 * @as: the address space that contains the memory being synchronized
86e775c6 1106 */
1d671369 1107void address_space_sync_dirty_bitmap(AddressSpace *as);
86e775c6 1108
69ddaf66
ASRJ
1109/**
1110 * memory_region_transaction_begin: Start a transaction.
1111 *
1112 * During a transaction, changes will be accumulated and made visible
dabdf394 1113 * only when the transaction ends (is committed).
4ef4db86
AK
1114 */
1115void memory_region_transaction_begin(void);
69ddaf66
ASRJ
1116
1117/**
1118 * memory_region_transaction_commit: Commit a transaction and make changes
1119 * visible to the guest.
4ef4db86
AK
1120 */
1121void memory_region_transaction_commit(void);
1122
7664e80c
AK
1123/**
1124 * memory_listener_register: register callbacks to be called when memory
1125 * sections are mapped or unmapped into an address
1126 * space
1127 *
1128 * @listener: an object containing the callbacks to be called
7376e582 1129 * @filter: if non-%NULL, only regions in this address space will be observed
7664e80c 1130 */
f6790af6 1131void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
7664e80c
AK
1132
1133/**
1134 * memory_listener_unregister: undo the effect of memory_listener_register()
1135 *
1136 * @listener: an object containing the callbacks to be removed
1137 */
1138void memory_listener_unregister(MemoryListener *listener);
1139
1140/**
1141 * memory_global_dirty_log_start: begin dirty logging for all regions
1142 */
1143void memory_global_dirty_log_start(void);
1144
1145/**
1a7e8cae 1146 * memory_global_dirty_log_stop: end dirty logging for all regions
7664e80c
AK
1147 */
1148void memory_global_dirty_log_stop(void);
1149
314e2987
BS
1150void mtree_info(fprintf_function mon_printf, void *f);
1151
3b643495
PM
1152/**
1153 * memory_region_dispatch_read: perform a read directly to the specified
1154 * MemoryRegion.
1155 *
1156 * @mr: #MemoryRegion to access
1157 * @addr: address within that region
1158 * @pval: pointer to uint64_t which the data is written to
1159 * @size: size of the access in bytes
1160 * @attrs: memory transaction attributes to use for the access
1161 */
1162MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1163 hwaddr addr,
1164 uint64_t *pval,
1165 unsigned size,
1166 MemTxAttrs attrs);
1167/**
1168 * memory_region_dispatch_write: perform a write directly to the specified
1169 * MemoryRegion.
1170 *
1171 * @mr: #MemoryRegion to access
1172 * @addr: address within that region
1173 * @data: data to write
1174 * @size: size of the access in bytes
1175 * @attrs: memory transaction attributes to use for the access
1176 */
1177MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1178 hwaddr addr,
1179 uint64_t data,
1180 unsigned size,
1181 MemTxAttrs attrs);
1182
9ad2bbc1
AK
1183/**
1184 * address_space_init: initializes an address space
1185 *
1186 * @as: an uninitialized #AddressSpace
67cc32eb 1187 * @root: a #MemoryRegion that routes addresses for the address space
7dca8043
AK
1188 * @name: an address space name. The name is only used for debugging
1189 * output.
9ad2bbc1 1190 */
7dca8043 1191void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
9ad2bbc1 1192
f0c02d15
PC
1193/**
1194 * address_space_init_shareable: return an address space for a memory region,
1195 * creating it if it does not already exist
1196 *
1197 * @root: a #MemoryRegion that routes addresses for the address space
1198 * @name: an address space name. The name is only used for debugging
1199 * output.
1200 *
1201 * This function will return a pointer to an existing AddressSpace
1202 * which was initialized with the specified MemoryRegion, or it will
1203 * create and initialize one if it does not already exist. The ASes
1204 * are reference-counted, so the memory will be freed automatically
1205 * when the AddressSpace is destroyed via address_space_destroy.
1206 */
1207AddressSpace *address_space_init_shareable(MemoryRegion *root,
1208 const char *name);
83f3c251
AK
1209
1210/**
1211 * address_space_destroy: destroy an address space
1212 *
1213 * Releases all resources associated with an address space. After an address space
1214 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1215 * as well.
1216 *
1217 * @as: address space to be destroyed
1218 */
1219void address_space_destroy(AddressSpace *as);
1220
ac1970fb
AK
1221/**
1222 * address_space_rw: read from or write to an address space.
1223 *
5c9eb028
PM
1224 * Return a MemTxResult indicating whether the operation succeeded
1225 * or failed (eg unassigned memory, device rejected the transaction,
1226 * IOMMU fault).
fd8aaa76 1227 *
ac1970fb
AK
1228 * @as: #AddressSpace to be accessed
1229 * @addr: address within that address space
5c9eb028 1230 * @attrs: memory transaction attributes
ac1970fb
AK
1231 * @buf: buffer with the data transferred
1232 * @is_write: indicates the transfer direction
1233 */
5c9eb028
PM
1234MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1235 MemTxAttrs attrs, uint8_t *buf,
1236 int len, bool is_write);
ac1970fb
AK
1237
1238/**
1239 * address_space_write: write to address space.
1240 *
5c9eb028
PM
1241 * Return a MemTxResult indicating whether the operation succeeded
1242 * or failed (eg unassigned memory, device rejected the transaction,
1243 * IOMMU fault).
fd8aaa76 1244 *
ac1970fb
AK
1245 * @as: #AddressSpace to be accessed
1246 * @addr: address within that address space
5c9eb028 1247 * @attrs: memory transaction attributes
ac1970fb
AK
1248 * @buf: buffer with the data transferred
1249 */
5c9eb028
PM
1250MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1251 MemTxAttrs attrs,
1252 const uint8_t *buf, int len);
ac1970fb 1253
3cc8f884 1254/* address_space_ld*: load from an address space
50013115
PM
1255 * address_space_st*: store to an address space
1256 *
1257 * These functions perform a load or store of the byte, word,
1258 * longword or quad to the specified address within the AddressSpace.
1259 * The _le suffixed functions treat the data as little endian;
1260 * _be indicates big endian; no suffix indicates "same endianness
1261 * as guest CPU".
1262 *
1263 * The "guest CPU endianness" accessors are deprecated for use outside
1264 * target-* code; devices should be CPU-agnostic and use either the LE
1265 * or the BE accessors.
1266 *
1267 * @as #AddressSpace to be accessed
1268 * @addr: address within that address space
1269 * @val: data value, for stores
1270 * @attrs: memory transaction attributes
1271 * @result: location to write the success/failure of the transaction;
1272 * if NULL, this information is discarded
1273 */
1274uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1275 MemTxAttrs attrs, MemTxResult *result);
1276uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1277 MemTxAttrs attrs, MemTxResult *result);
1278uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1279 MemTxAttrs attrs, MemTxResult *result);
1280uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1281 MemTxAttrs attrs, MemTxResult *result);
1282uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1283 MemTxAttrs attrs, MemTxResult *result);
1284uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1285 MemTxAttrs attrs, MemTxResult *result);
1286uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1287 MemTxAttrs attrs, MemTxResult *result);
1288void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1289 MemTxAttrs attrs, MemTxResult *result);
1290void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1291 MemTxAttrs attrs, MemTxResult *result);
1292void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1293 MemTxAttrs attrs, MemTxResult *result);
1294void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1295 MemTxAttrs attrs, MemTxResult *result);
1296void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1297 MemTxAttrs attrs, MemTxResult *result);
1298void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1299 MemTxAttrs attrs, MemTxResult *result);
1300void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1301 MemTxAttrs attrs, MemTxResult *result);
1302
1303#ifdef NEED_CPU_H
1304uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
1305 MemTxAttrs attrs, MemTxResult *result);
1306uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
1307 MemTxAttrs attrs, MemTxResult *result);
1308uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
1309 MemTxAttrs attrs, MemTxResult *result);
1310void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val,
1311 MemTxAttrs attrs, MemTxResult *result);
1312void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
1313 MemTxAttrs attrs, MemTxResult *result);
1314void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
1315 MemTxAttrs attrs, MemTxResult *result);
1316void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
1317 MemTxAttrs attrs, MemTxResult *result);
1318#endif
1319
149f54b5 1320/* address_space_translate: translate an address range into an address space
41063e1e
PB
1321 * into a MemoryRegion and an address range into that section. Should be
1322 * called from an RCU critical section, to avoid that the last reference
1323 * to the returned region disappears after address_space_translate returns.
149f54b5
PB
1324 *
1325 * @as: #AddressSpace to be accessed
1326 * @addr: address within that address space
1327 * @xlat: pointer to address within the returned memory region section's
1328 * #MemoryRegion.
1329 * @len: pointer to length
1330 * @is_write: indicates the transfer direction
1331 */
5c8a00ce
PB
1332MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1333 hwaddr *xlat, hwaddr *len,
1334 bool is_write);
149f54b5 1335
51644ab7
PB
1336/* address_space_access_valid: check for validity of accessing an address
1337 * space range
1338 *
30951157
AK
1339 * Check whether memory is assigned to the given address space range, and
1340 * access is permitted by any IOMMU regions that are active for the address
1341 * space.
51644ab7
PB
1342 *
1343 * For now, addr and len should be aligned to a page size. This limitation
1344 * will be lifted in the future.
1345 *
1346 * @as: #AddressSpace to be accessed
1347 * @addr: address within that address space
1348 * @len: length of the area to be checked
1349 * @is_write: indicates the transfer direction
1350 */
1351bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1352
ac1970fb
AK
1353/* address_space_map: map a physical memory region into a host virtual address
1354 *
1355 * May map a subset of the requested range, given by and returned in @plen.
1356 * May return %NULL if resources needed to perform the mapping are exhausted.
1357 * Use only for reads OR writes - not for read-modify-write operations.
1358 * Use cpu_register_map_client() to know when retrying the map operation is
1359 * likely to succeed.
1360 *
1361 * @as: #AddressSpace to be accessed
1362 * @addr: address within that address space
1363 * @plen: pointer to length of buffer; updated on return
1364 * @is_write: indicates the transfer direction
1365 */
a8170e5e
AK
1366void *address_space_map(AddressSpace *as, hwaddr addr,
1367 hwaddr *plen, bool is_write);
ac1970fb
AK
1368
1369/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1370 *
1371 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1372 * the amount of memory that was actually read or written by the caller.
1373 *
1374 * @as: #AddressSpace used
1375 * @addr: address within that address space
1376 * @len: buffer length as returned by address_space_map()
1377 * @access_len: amount of data actually transferred
1378 * @is_write: indicates the transfer direction
1379 */
a8170e5e
AK
1380void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1381 int is_write, hwaddr access_len);
ac1970fb
AK
1382
1383
a203ac70
PB
1384/* Internal functions, part of the implementation of address_space_read. */
1385MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1386 MemTxAttrs attrs, uint8_t *buf,
1387 int len, hwaddr addr1, hwaddr l,
1388 MemoryRegion *mr);
3cc8f884
PB
1389MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1390 MemTxAttrs attrs, uint8_t *buf, int len);
3655cb9c 1391void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
3cc8f884
PB
1392
1393static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1394{
1395 if (is_write) {
1396 return memory_region_is_ram(mr) && !mr->readonly;
1397 } else {
1398 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1399 }
3cc8f884
PB
1400}
1401
1402/**
1403 * address_space_read: read from an address space.
1404 *
1405 * Return a MemTxResult indicating whether the operation succeeded
1406 * or failed (eg unassigned memory, device rejected the transaction,
1407 * IOMMU fault).
1408 *
1409 * @as: #AddressSpace to be accessed
1410 * @addr: address within that address space
1411 * @attrs: memory transaction attributes
1412 * @buf: buffer with the data transferred
1413 */
1414static inline __attribute__((__always_inline__))
1415MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1416 uint8_t *buf, int len)
1417{
1418 MemTxResult result = MEMTX_OK;
1419 hwaddr l, addr1;
1420 void *ptr;
1421 MemoryRegion *mr;
1422
1423 if (__builtin_constant_p(len)) {
1424 if (len) {
1425 rcu_read_lock();
1426 l = len;
1427 mr = address_space_translate(as, addr, &addr1, &l, false);
1428 if (len == l && memory_access_is_direct(mr, false)) {
1429 addr1 += memory_region_get_ram_addr(mr);
3655cb9c 1430 ptr = qemu_get_ram_ptr(mr->ram_block, addr1);
3cc8f884
PB
1431 memcpy(buf, ptr, len);
1432 } else {
1433 result = address_space_read_continue(as, addr, attrs, buf, len,
1434 addr1, l, mr);
1435 }
1436 rcu_read_unlock();
1437 }
1438 } else {
1439 result = address_space_read_full(as, addr, attrs, buf, len);
1440 }
1441 return result;
1442}
a203ac70 1443
093bc2cd
AK
1444#endif
1445
1446#endif