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