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