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