]> git.proxmox.com Git - mirror_qemu.git/blob - include/exec/memory.h
memory: Rework "info mtree" to print flat views and dispatch trees
[mirror_qemu.git] / include / exec / memory.h
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
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/ramlist.h"
23 #include "qemu/queue.h"
24 #include "qemu/int128.h"
25 #include "qemu/notify.h"
26 #include "qom/object.h"
27 #include "qemu/rcu.h"
28 #include "hw/qdev-core.h"
29
30 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
31
32 #define MAX_PHYS_ADDR_SPACE_BITS 62
33 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
34
35 #define TYPE_MEMORY_REGION "qemu:memory-region"
36 #define MEMORY_REGION(obj) \
37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
38
39 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
40 #define IOMMU_MEMORY_REGION(obj) \
41 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
42 #define IOMMU_MEMORY_REGION_CLASS(klass) \
43 OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \
44 TYPE_IOMMU_MEMORY_REGION)
45 #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \
46 OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \
47 TYPE_IOMMU_MEMORY_REGION)
48
49 typedef struct MemoryRegionOps MemoryRegionOps;
50 typedef struct MemoryRegionMmio MemoryRegionMmio;
51 typedef struct FlatView FlatView;
52
53 struct MemoryRegionMmio {
54 CPUReadMemoryFunc *read[3];
55 CPUWriteMemoryFunc *write[3];
56 };
57
58 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
59
60 /* See address_space_translate: bit 0 is read, bit 1 is write. */
61 typedef enum {
62 IOMMU_NONE = 0,
63 IOMMU_RO = 1,
64 IOMMU_WO = 2,
65 IOMMU_RW = 3,
66 } IOMMUAccessFlags;
67
68 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
69
70 struct IOMMUTLBEntry {
71 AddressSpace *target_as;
72 hwaddr iova;
73 hwaddr translated_addr;
74 hwaddr addr_mask; /* 0xfff = 4k translation */
75 IOMMUAccessFlags perm;
76 };
77
78 /*
79 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
80 * register with one or multiple IOMMU Notifier capability bit(s).
81 */
82 typedef enum {
83 IOMMU_NOTIFIER_NONE = 0,
84 /* Notify cache invalidations */
85 IOMMU_NOTIFIER_UNMAP = 0x1,
86 /* Notify entry changes (newly created entries) */
87 IOMMU_NOTIFIER_MAP = 0x2,
88 } IOMMUNotifierFlag;
89
90 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
91
92 struct IOMMUNotifier;
93 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
94 IOMMUTLBEntry *data);
95
96 struct IOMMUNotifier {
97 IOMMUNotify notify;
98 IOMMUNotifierFlag notifier_flags;
99 /* Notify for address space range start <= addr <= end */
100 hwaddr start;
101 hwaddr end;
102 QLIST_ENTRY(IOMMUNotifier) node;
103 };
104 typedef struct IOMMUNotifier IOMMUNotifier;
105
106 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
107 IOMMUNotifierFlag flags,
108 hwaddr start, hwaddr end)
109 {
110 n->notify = fn;
111 n->notifier_flags = flags;
112 n->start = start;
113 n->end = end;
114 }
115
116 /*
117 * Memory region callbacks
118 */
119 struct MemoryRegionOps {
120 /* Read from the memory region. @addr is relative to @mr; @size is
121 * in bytes. */
122 uint64_t (*read)(void *opaque,
123 hwaddr addr,
124 unsigned size);
125 /* Write to the memory region. @addr is relative to @mr; @size is
126 * in bytes. */
127 void (*write)(void *opaque,
128 hwaddr addr,
129 uint64_t data,
130 unsigned size);
131
132 MemTxResult (*read_with_attrs)(void *opaque,
133 hwaddr addr,
134 uint64_t *data,
135 unsigned size,
136 MemTxAttrs attrs);
137 MemTxResult (*write_with_attrs)(void *opaque,
138 hwaddr addr,
139 uint64_t data,
140 unsigned size,
141 MemTxAttrs attrs);
142 /* Instruction execution pre-callback:
143 * @addr is the address of the access relative to the @mr.
144 * @size is the size of the area returned by the callback.
145 * @offset is the location of the pointer inside @mr.
146 *
147 * Returns a pointer to a location which contains guest code.
148 */
149 void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size,
150 unsigned *offset);
151
152 enum device_endian endianness;
153 /* Guest-visible constraints: */
154 struct {
155 /* If nonzero, specify bounds on access sizes beyond which a machine
156 * check is thrown.
157 */
158 unsigned min_access_size;
159 unsigned max_access_size;
160 /* If true, unaligned accesses are supported. Otherwise unaligned
161 * accesses throw machine checks.
162 */
163 bool unaligned;
164 /*
165 * If present, and returns #false, the transaction is not accepted
166 * by the device (and results in machine dependent behaviour such
167 * as a machine check exception).
168 */
169 bool (*accepts)(void *opaque, hwaddr addr,
170 unsigned size, bool is_write);
171 } valid;
172 /* Internal implementation constraints: */
173 struct {
174 /* If nonzero, specifies the minimum size implemented. Smaller sizes
175 * will be rounded upwards and a partial result will be returned.
176 */
177 unsigned min_access_size;
178 /* If nonzero, specifies the maximum size implemented. Larger sizes
179 * will be done as a series of accesses with smaller sizes.
180 */
181 unsigned max_access_size;
182 /* If true, unaligned accesses are supported. Otherwise all accesses
183 * are converted to (possibly multiple) naturally aligned accesses.
184 */
185 bool unaligned;
186 } impl;
187
188 /* If .read and .write are not present, old_mmio may be used for
189 * backwards compatibility with old mmio registration
190 */
191 const MemoryRegionMmio old_mmio;
192 };
193
194 typedef struct IOMMUMemoryRegionClass {
195 /* private */
196 struct DeviceClass parent_class;
197
198 /*
199 * Return a TLB entry that contains a given address. Flag should
200 * be the access permission of this translation operation. We can
201 * set flag to IOMMU_NONE to mean that we don't need any
202 * read/write permission checks, like, when for region replay.
203 */
204 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
205 IOMMUAccessFlags flag);
206 /* Returns minimum supported page size */
207 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
208 /* Called when IOMMU Notifier flag changed */
209 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
210 IOMMUNotifierFlag old_flags,
211 IOMMUNotifierFlag new_flags);
212 /* Set this up to provide customized IOMMU replay function */
213 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
214 } IOMMUMemoryRegionClass;
215
216 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
217 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
218
219 struct MemoryRegion {
220 Object parent_obj;
221
222 /* All fields are private - violators will be prosecuted */
223
224 /* The following fields should fit in a cache line */
225 bool romd_mode;
226 bool ram;
227 bool subpage;
228 bool readonly; /* For RAM regions */
229 bool rom_device;
230 bool flush_coalesced_mmio;
231 bool global_locking;
232 uint8_t dirty_log_mask;
233 bool is_iommu;
234 RAMBlock *ram_block;
235 Object *owner;
236
237 const MemoryRegionOps *ops;
238 void *opaque;
239 MemoryRegion *container;
240 Int128 size;
241 hwaddr addr;
242 void (*destructor)(MemoryRegion *mr);
243 uint64_t align;
244 bool terminates;
245 bool ram_device;
246 bool enabled;
247 bool warning_printed; /* For reservations */
248 uint8_t vga_logging_count;
249 MemoryRegion *alias;
250 hwaddr alias_offset;
251 int32_t priority;
252 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
253 QTAILQ_ENTRY(MemoryRegion) subregions_link;
254 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
255 const char *name;
256 unsigned ioeventfd_nb;
257 MemoryRegionIoeventfd *ioeventfds;
258 };
259
260 struct IOMMUMemoryRegion {
261 MemoryRegion parent_obj;
262
263 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
264 IOMMUNotifierFlag iommu_notify_flags;
265 };
266
267 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
268 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
269
270 /**
271 * MemoryListener: callbacks structure for updates to the physical memory map
272 *
273 * Allows a component to adjust to changes in the guest-visible memory map.
274 * Use with memory_listener_register() and memory_listener_unregister().
275 */
276 struct MemoryListener {
277 void (*begin)(MemoryListener *listener);
278 void (*commit)(MemoryListener *listener);
279 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
280 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
281 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
282 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
283 int old, int new);
284 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
285 int old, int new);
286 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
287 void (*log_global_start)(MemoryListener *listener);
288 void (*log_global_stop)(MemoryListener *listener);
289 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
290 bool match_data, uint64_t data, EventNotifier *e);
291 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
292 bool match_data, uint64_t data, EventNotifier *e);
293 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
294 hwaddr addr, hwaddr len);
295 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
296 hwaddr addr, hwaddr len);
297 /* Lower = earlier (during add), later (during del) */
298 unsigned priority;
299 AddressSpace *address_space;
300 QTAILQ_ENTRY(MemoryListener) link;
301 QTAILQ_ENTRY(MemoryListener) link_as;
302 };
303
304 /**
305 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
306 */
307 struct AddressSpace {
308 /* All fields are private. */
309 struct rcu_head rcu;
310 char *name;
311 MemoryRegion *root;
312 int ref_count;
313 bool malloced;
314
315 /* Accessed via RCU. */
316 struct FlatView *current_map;
317
318 int ioeventfd_nb;
319 struct MemoryRegionIoeventfd *ioeventfds;
320 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
321 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
322 };
323
324 FlatView *address_space_to_flatview(AddressSpace *as);
325
326 /**
327 * MemoryRegionSection: describes a fragment of a #MemoryRegion
328 *
329 * @mr: the region, or %NULL if empty
330 * @address_space: the address space the region is mapped in
331 * @offset_within_region: the beginning of the section, relative to @mr's start
332 * @size: the size of the section; will not exceed @mr's boundaries
333 * @offset_within_address_space: the address of the first byte of the section
334 * relative to the region's address space
335 * @readonly: writes to this section are ignored
336 */
337 struct MemoryRegionSection {
338 MemoryRegion *mr;
339 FlatView *fv;
340 hwaddr offset_within_region;
341 Int128 size;
342 hwaddr offset_within_address_space;
343 bool readonly;
344 };
345
346 /**
347 * memory_region_init: Initialize a memory region
348 *
349 * The region typically acts as a container for other memory regions. Use
350 * memory_region_add_subregion() to add subregions.
351 *
352 * @mr: the #MemoryRegion to be initialized
353 * @owner: the object that tracks the region's reference count
354 * @name: used for debugging; not visible to the user or ABI
355 * @size: size of the region; any subregions beyond this size will be clipped
356 */
357 void memory_region_init(MemoryRegion *mr,
358 struct Object *owner,
359 const char *name,
360 uint64_t size);
361
362 /**
363 * memory_region_ref: Add 1 to a memory region's reference count
364 *
365 * Whenever memory regions are accessed outside the BQL, they need to be
366 * preserved against hot-unplug. MemoryRegions actually do not have their
367 * own reference count; they piggyback on a QOM object, their "owner".
368 * This function adds a reference to the owner.
369 *
370 * All MemoryRegions must have an owner if they can disappear, even if the
371 * device they belong to operates exclusively under the BQL. This is because
372 * the region could be returned at any time by memory_region_find, and this
373 * is usually under guest control.
374 *
375 * @mr: the #MemoryRegion
376 */
377 void memory_region_ref(MemoryRegion *mr);
378
379 /**
380 * memory_region_unref: Remove 1 to a memory region's reference count
381 *
382 * Whenever memory regions are accessed outside the BQL, they need to be
383 * preserved against hot-unplug. MemoryRegions actually do not have their
384 * own reference count; they piggyback on a QOM object, their "owner".
385 * This function removes a reference to the owner and possibly destroys it.
386 *
387 * @mr: the #MemoryRegion
388 */
389 void memory_region_unref(MemoryRegion *mr);
390
391 /**
392 * memory_region_init_io: Initialize an I/O memory region.
393 *
394 * Accesses into the region will cause the callbacks in @ops to be called.
395 * if @size is nonzero, subregions will be clipped to @size.
396 *
397 * @mr: the #MemoryRegion to be initialized.
398 * @owner: the object that tracks the region's reference count
399 * @ops: a structure containing read and write callbacks to be used when
400 * I/O is performed on the region.
401 * @opaque: passed to the read and write callbacks of the @ops structure.
402 * @name: used for debugging; not visible to the user or ABI
403 * @size: size of the region.
404 */
405 void memory_region_init_io(MemoryRegion *mr,
406 struct Object *owner,
407 const MemoryRegionOps *ops,
408 void *opaque,
409 const char *name,
410 uint64_t size);
411
412 /**
413 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
414 * into the region will modify memory
415 * directly.
416 *
417 * @mr: the #MemoryRegion to be initialized.
418 * @owner: the object that tracks the region's reference count
419 * @name: Region name, becomes part of RAMBlock name used in migration stream
420 * must be unique within any device
421 * @size: size of the region.
422 * @errp: pointer to Error*, to store an error if it happens.
423 *
424 * Note that this function does not do anything to cause the data in the
425 * RAM memory region to be migrated; that is the responsibility of the caller.
426 */
427 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
428 struct Object *owner,
429 const char *name,
430 uint64_t size,
431 Error **errp);
432
433 /**
434 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
435 * RAM. Accesses into the region will
436 * modify memory directly. Only an initial
437 * portion of this RAM is actually used.
438 * The used size can change across reboots.
439 *
440 * @mr: the #MemoryRegion to be initialized.
441 * @owner: the object that tracks the region's reference count
442 * @name: Region name, becomes part of RAMBlock name used in migration stream
443 * must be unique within any device
444 * @size: used size of the region.
445 * @max_size: max size of the region.
446 * @resized: callback to notify owner about used size change.
447 * @errp: pointer to Error*, to store an error if it happens.
448 *
449 * Note that this function does not do anything to cause the data in the
450 * RAM memory region to be migrated; that is the responsibility of the caller.
451 */
452 void memory_region_init_resizeable_ram(MemoryRegion *mr,
453 struct Object *owner,
454 const char *name,
455 uint64_t size,
456 uint64_t max_size,
457 void (*resized)(const char*,
458 uint64_t length,
459 void *host),
460 Error **errp);
461 #ifdef __linux__
462 /**
463 * memory_region_init_ram_from_file: Initialize RAM memory region with a
464 * mmap-ed backend.
465 *
466 * @mr: the #MemoryRegion to be initialized.
467 * @owner: the object that tracks the region's reference count
468 * @name: Region name, becomes part of RAMBlock name used in migration stream
469 * must be unique within any device
470 * @size: size of the region.
471 * @share: %true if memory must be mmaped with the MAP_SHARED flag
472 * @path: the path in which to allocate the RAM.
473 * @errp: pointer to Error*, to store an error if it happens.
474 *
475 * Note that this function does not do anything to cause the data in the
476 * RAM memory region to be migrated; that is the responsibility of the caller.
477 */
478 void memory_region_init_ram_from_file(MemoryRegion *mr,
479 struct Object *owner,
480 const char *name,
481 uint64_t size,
482 bool share,
483 const char *path,
484 Error **errp);
485
486 /**
487 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
488 * mmap-ed backend.
489 *
490 * @mr: the #MemoryRegion to be initialized.
491 * @owner: the object that tracks the region's reference count
492 * @name: the name of the region.
493 * @size: size of the region.
494 * @share: %true if memory must be mmaped with the MAP_SHARED flag
495 * @fd: the fd to mmap.
496 * @errp: pointer to Error*, to store an error if it happens.
497 *
498 * Note that this function does not do anything to cause the data in the
499 * RAM memory region to be migrated; that is the responsibility of the caller.
500 */
501 void memory_region_init_ram_from_fd(MemoryRegion *mr,
502 struct Object *owner,
503 const char *name,
504 uint64_t size,
505 bool share,
506 int fd,
507 Error **errp);
508 #endif
509
510 /**
511 * memory_region_init_ram_ptr: Initialize RAM memory region from a
512 * user-provided pointer. Accesses into the
513 * region will modify memory directly.
514 *
515 * @mr: the #MemoryRegion to be initialized.
516 * @owner: the object that tracks the region's reference count
517 * @name: Region name, becomes part of RAMBlock name used in migration stream
518 * must be unique within any device
519 * @size: size of the region.
520 * @ptr: memory to be mapped; must contain at least @size bytes.
521 *
522 * Note that this function does not do anything to cause the data in the
523 * RAM memory region to be migrated; that is the responsibility of the caller.
524 */
525 void memory_region_init_ram_ptr(MemoryRegion *mr,
526 struct Object *owner,
527 const char *name,
528 uint64_t size,
529 void *ptr);
530
531 /**
532 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
533 * a user-provided pointer.
534 *
535 * A RAM device represents a mapping to a physical device, such as to a PCI
536 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
537 * into the VM address space and access to the region will modify memory
538 * directly. However, the memory region should not be included in a memory
539 * dump (device may not be enabled/mapped at the time of the dump), and
540 * operations incompatible with manipulating MMIO should be avoided. Replaces
541 * skip_dump flag.
542 *
543 * @mr: the #MemoryRegion to be initialized.
544 * @owner: the object that tracks the region's reference count
545 * @name: the name of the region.
546 * @size: size of the region.
547 * @ptr: memory to be mapped; must contain at least @size bytes.
548 *
549 * Note that this function does not do anything to cause the data in the
550 * RAM memory region to be migrated; that is the responsibility of the caller.
551 * (For RAM device memory regions, migrating the contents rarely makes sense.)
552 */
553 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
554 struct Object *owner,
555 const char *name,
556 uint64_t size,
557 void *ptr);
558
559 /**
560 * memory_region_init_alias: Initialize a memory region that aliases all or a
561 * part of another memory region.
562 *
563 * @mr: the #MemoryRegion to be initialized.
564 * @owner: the object that tracks the region's reference count
565 * @name: used for debugging; not visible to the user or ABI
566 * @orig: the region to be referenced; @mr will be equivalent to
567 * @orig between @offset and @offset + @size - 1.
568 * @offset: start of the section in @orig to be referenced.
569 * @size: size of the region.
570 */
571 void memory_region_init_alias(MemoryRegion *mr,
572 struct Object *owner,
573 const char *name,
574 MemoryRegion *orig,
575 hwaddr offset,
576 uint64_t size);
577
578 /**
579 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
580 *
581 * This has the same effect as calling memory_region_init_ram_nomigrate()
582 * and then marking the resulting region read-only with
583 * memory_region_set_readonly().
584 *
585 * Note that this function does not do anything to cause the data in the
586 * RAM side of the memory region to be migrated; that is the responsibility
587 * of the caller.
588 *
589 * @mr: the #MemoryRegion to be initialized.
590 * @owner: the object that tracks the region's reference count
591 * @name: Region name, becomes part of RAMBlock name used in migration stream
592 * must be unique within any device
593 * @size: size of the region.
594 * @errp: pointer to Error*, to store an error if it happens.
595 */
596 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
597 struct Object *owner,
598 const char *name,
599 uint64_t size,
600 Error **errp);
601
602 /**
603 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
604 * Writes are handled via callbacks.
605 *
606 * Note that this function does not do anything to cause the data in the
607 * RAM side of the memory region to be migrated; that is the responsibility
608 * of the caller.
609 *
610 * @mr: the #MemoryRegion to be initialized.
611 * @owner: the object that tracks the region's reference count
612 * @ops: callbacks for write access handling (must not be NULL).
613 * @name: Region name, becomes part of RAMBlock name used in migration stream
614 * must be unique within any device
615 * @size: size of the region.
616 * @errp: pointer to Error*, to store an error if it happens.
617 */
618 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
619 struct Object *owner,
620 const MemoryRegionOps *ops,
621 void *opaque,
622 const char *name,
623 uint64_t size,
624 Error **errp);
625
626 /**
627 * memory_region_init_reservation: Initialize a memory region that reserves
628 * I/O space.
629 *
630 * A reservation region primariy serves debugging purposes. It claims I/O
631 * space that is not supposed to be handled by QEMU itself. Any access via
632 * the memory API will cause an abort().
633 * This function is deprecated. Use memory_region_init_io() with NULL
634 * callbacks instead.
635 *
636 * @mr: the #MemoryRegion to be initialized
637 * @owner: the object that tracks the region's reference count
638 * @name: used for debugging; not visible to the user or ABI
639 * @size: size of the region.
640 */
641 static inline void memory_region_init_reservation(MemoryRegion *mr,
642 Object *owner,
643 const char *name,
644 uint64_t size)
645 {
646 memory_region_init_io(mr, owner, NULL, mr, name, size);
647 }
648
649 /**
650 * memory_region_init_iommu: Initialize a memory region of a custom type
651 * that translates addresses
652 *
653 * An IOMMU region translates addresses and forwards accesses to a target
654 * memory region.
655 *
656 * @typename: QOM class name
657 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
658 * @instance_size: the IOMMUMemoryRegion subclass instance size
659 * @owner: the object that tracks the region's reference count
660 * @ops: a function that translates addresses into the @target region
661 * @name: used for debugging; not visible to the user or ABI
662 * @size: size of the region.
663 */
664 void memory_region_init_iommu(void *_iommu_mr,
665 size_t instance_size,
666 const char *mrtypename,
667 Object *owner,
668 const char *name,
669 uint64_t size);
670
671 /**
672 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
673 * region will modify memory directly.
674 *
675 * @mr: the #MemoryRegion to be initialized
676 * @owner: the object that tracks the region's reference count (must be
677 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
678 * @name: name of the memory region
679 * @size: size of the region in bytes
680 * @errp: pointer to Error*, to store an error if it happens.
681 *
682 * This function allocates RAM for a board model or device, and
683 * arranges for it to be migrated (by calling vmstate_register_ram()
684 * if @owner is a DeviceState, or vmstate_register_ram_global() if
685 * @owner is NULL).
686 *
687 * TODO: Currently we restrict @owner to being either NULL (for
688 * global RAM regions with no owner) or devices, so that we can
689 * give the RAM block a unique name for migration purposes.
690 * We should lift this restriction and allow arbitrary Objects.
691 * If you pass a non-NULL non-device @owner then we will assert.
692 */
693 void memory_region_init_ram(MemoryRegion *mr,
694 struct Object *owner,
695 const char *name,
696 uint64_t size,
697 Error **errp);
698
699 /**
700 * memory_region_init_rom: Initialize a ROM memory region.
701 *
702 * This has the same effect as calling memory_region_init_ram()
703 * and then marking the resulting region read-only with
704 * memory_region_set_readonly(). This includes arranging for the
705 * contents to be migrated.
706 *
707 * TODO: Currently we restrict @owner to being either NULL (for
708 * global RAM regions with no owner) or devices, so that we can
709 * give the RAM block a unique name for migration purposes.
710 * We should lift this restriction and allow arbitrary Objects.
711 * If you pass a non-NULL non-device @owner then we will assert.
712 *
713 * @mr: the #MemoryRegion to be initialized.
714 * @owner: the object that tracks the region's reference count
715 * @name: Region name, becomes part of RAMBlock name used in migration stream
716 * must be unique within any device
717 * @size: size of the region.
718 * @errp: pointer to Error*, to store an error if it happens.
719 */
720 void memory_region_init_rom(MemoryRegion *mr,
721 struct Object *owner,
722 const char *name,
723 uint64_t size,
724 Error **errp);
725
726 /**
727 * memory_region_init_rom_device: Initialize a ROM memory region.
728 * Writes are handled via callbacks.
729 *
730 * This function initializes a memory region backed by RAM for reads
731 * and callbacks for writes, and arranges for the RAM backing to
732 * be migrated (by calling vmstate_register_ram()
733 * if @owner is a DeviceState, or vmstate_register_ram_global() if
734 * @owner is NULL).
735 *
736 * TODO: Currently we restrict @owner to being either NULL (for
737 * global RAM regions with no owner) or devices, so that we can
738 * give the RAM block a unique name for migration purposes.
739 * We should lift this restriction and allow arbitrary Objects.
740 * If you pass a non-NULL non-device @owner then we will assert.
741 *
742 * @mr: the #MemoryRegion to be initialized.
743 * @owner: the object that tracks the region's reference count
744 * @ops: callbacks for write access handling (must not be NULL).
745 * @name: Region name, becomes part of RAMBlock name used in migration stream
746 * must be unique within any device
747 * @size: size of the region.
748 * @errp: pointer to Error*, to store an error if it happens.
749 */
750 void memory_region_init_rom_device(MemoryRegion *mr,
751 struct Object *owner,
752 const MemoryRegionOps *ops,
753 void *opaque,
754 const char *name,
755 uint64_t size,
756 Error **errp);
757
758
759 /**
760 * memory_region_owner: get a memory region's owner.
761 *
762 * @mr: the memory region being queried.
763 */
764 struct Object *memory_region_owner(MemoryRegion *mr);
765
766 /**
767 * memory_region_size: get a memory region's size.
768 *
769 * @mr: the memory region being queried.
770 */
771 uint64_t memory_region_size(MemoryRegion *mr);
772
773 /**
774 * memory_region_is_ram: check whether a memory region is random access
775 *
776 * Returns %true is a memory region is random access.
777 *
778 * @mr: the memory region being queried
779 */
780 static inline bool memory_region_is_ram(MemoryRegion *mr)
781 {
782 return mr->ram;
783 }
784
785 /**
786 * memory_region_is_ram_device: check whether a memory region is a ram device
787 *
788 * Returns %true is a memory region is a device backed ram region
789 *
790 * @mr: the memory region being queried
791 */
792 bool memory_region_is_ram_device(MemoryRegion *mr);
793
794 /**
795 * memory_region_is_romd: check whether a memory region is in ROMD mode
796 *
797 * Returns %true if a memory region is a ROM device and currently set to allow
798 * direct reads.
799 *
800 * @mr: the memory region being queried
801 */
802 static inline bool memory_region_is_romd(MemoryRegion *mr)
803 {
804 return mr->rom_device && mr->romd_mode;
805 }
806
807 /**
808 * memory_region_get_iommu: check whether a memory region is an iommu
809 *
810 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
811 * otherwise NULL.
812 *
813 * @mr: the memory region being queried
814 */
815 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
816 {
817 if (mr->alias) {
818 return memory_region_get_iommu(mr->alias);
819 }
820 if (mr->is_iommu) {
821 return (IOMMUMemoryRegion *) mr;
822 }
823 return NULL;
824 }
825
826 /**
827 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
828 * if an iommu or NULL if not
829 *
830 * Returns pointer to IOMMUMemoryRegioniClass if a memory region is an iommu,
831 * otherwise NULL. This is fast path avoinding QOM checking, use with caution.
832 *
833 * @mr: the memory region being queried
834 */
835 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
836 IOMMUMemoryRegion *iommu_mr)
837 {
838 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
839 }
840
841 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
842
843 /**
844 * memory_region_iommu_get_min_page_size: get minimum supported page size
845 * for an iommu
846 *
847 * Returns minimum supported page size for an iommu.
848 *
849 * @iommu_mr: the memory region being queried
850 */
851 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
852
853 /**
854 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
855 *
856 * The notification type will be decided by entry.perm bits:
857 *
858 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
859 * - For MAP (newly added entry) notifies: set entry.perm to the
860 * permission of the page (which is definitely !IOMMU_NONE).
861 *
862 * Note: for any IOMMU implementation, an in-place mapping change
863 * should be notified with an UNMAP followed by a MAP.
864 *
865 * @iommu_mr: the memory region that was changed
866 * @entry: the new entry in the IOMMU translation table. The entry
867 * replaces all old entries for the same virtual I/O address range.
868 * Deleted entries have .@perm == 0.
869 */
870 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
871 IOMMUTLBEntry entry);
872
873 /**
874 * memory_region_notify_one: notify a change in an IOMMU translation
875 * entry to a single notifier
876 *
877 * This works just like memory_region_notify_iommu(), but it only
878 * notifies a specific notifier, not all of them.
879 *
880 * @notifier: the notifier to be notified
881 * @entry: the new entry in the IOMMU translation table. The entry
882 * replaces all old entries for the same virtual I/O address range.
883 * Deleted entries have .@perm == 0.
884 */
885 void memory_region_notify_one(IOMMUNotifier *notifier,
886 IOMMUTLBEntry *entry);
887
888 /**
889 * memory_region_register_iommu_notifier: register a notifier for changes to
890 * IOMMU translation entries.
891 *
892 * @mr: the memory region to observe
893 * @n: the IOMMUNotifier to be added; the notify callback receives a
894 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
895 * ceases to be valid on exit from the notifier.
896 */
897 void memory_region_register_iommu_notifier(MemoryRegion *mr,
898 IOMMUNotifier *n);
899
900 /**
901 * memory_region_iommu_replay: replay existing IOMMU translations to
902 * a notifier with the minimum page granularity returned by
903 * mr->iommu_ops->get_page_size().
904 *
905 * @iommu_mr: the memory region to observe
906 * @n: the notifier to which to replay iommu mappings
907 */
908 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
909
910 /**
911 * memory_region_iommu_replay_all: replay existing IOMMU translations
912 * to all the notifiers registered.
913 *
914 * @iommu_mr: the memory region to observe
915 */
916 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
917
918 /**
919 * memory_region_unregister_iommu_notifier: unregister a notifier for
920 * changes to IOMMU translation entries.
921 *
922 * @mr: the memory region which was observed and for which notity_stopped()
923 * needs to be called
924 * @n: the notifier to be removed.
925 */
926 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
927 IOMMUNotifier *n);
928
929 /**
930 * memory_region_name: get a memory region's name
931 *
932 * Returns the string that was used to initialize the memory region.
933 *
934 * @mr: the memory region being queried
935 */
936 const char *memory_region_name(const MemoryRegion *mr);
937
938 /**
939 * memory_region_is_logging: return whether a memory region is logging writes
940 *
941 * Returns %true if the memory region is logging writes for the given client
942 *
943 * @mr: the memory region being queried
944 * @client: the client being queried
945 */
946 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
947
948 /**
949 * memory_region_get_dirty_log_mask: return the clients for which a
950 * memory region is logging writes.
951 *
952 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
953 * are the bit indices.
954 *
955 * @mr: the memory region being queried
956 */
957 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
958
959 /**
960 * memory_region_is_rom: check whether a memory region is ROM
961 *
962 * Returns %true is a memory region is read-only memory.
963 *
964 * @mr: the memory region being queried
965 */
966 static inline bool memory_region_is_rom(MemoryRegion *mr)
967 {
968 return mr->ram && mr->readonly;
969 }
970
971
972 /**
973 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
974 *
975 * Returns a file descriptor backing a file-based RAM memory region,
976 * or -1 if the region is not a file-based RAM memory region.
977 *
978 * @mr: the RAM or alias memory region being queried.
979 */
980 int memory_region_get_fd(MemoryRegion *mr);
981
982 /**
983 * memory_region_from_host: Convert a pointer into a RAM memory region
984 * and an offset within it.
985 *
986 * Given a host pointer inside a RAM memory region (created with
987 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
988 * the MemoryRegion and the offset within it.
989 *
990 * Use with care; by the time this function returns, the returned pointer is
991 * not protected by RCU anymore. If the caller is not within an RCU critical
992 * section and does not hold the iothread lock, it must have other means of
993 * protecting the pointer, such as a reference to the region that includes
994 * the incoming ram_addr_t.
995 *
996 * @mr: the memory region being queried.
997 */
998 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
999
1000 /**
1001 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1002 *
1003 * Returns a host pointer to a RAM memory region (created with
1004 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1005 *
1006 * Use with care; by the time this function returns, the returned pointer is
1007 * not protected by RCU anymore. If the caller is not within an RCU critical
1008 * section and does not hold the iothread lock, it must have other means of
1009 * protecting the pointer, such as a reference to the region that includes
1010 * the incoming ram_addr_t.
1011 *
1012 * @mr: the memory region being queried.
1013 */
1014 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1015
1016 /* memory_region_ram_resize: Resize a RAM region.
1017 *
1018 * Only legal before guest might have detected the memory size: e.g. on
1019 * incoming migration, or right after reset.
1020 *
1021 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1022 * @newsize: the new size the region
1023 * @errp: pointer to Error*, to store an error if it happens.
1024 */
1025 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1026 Error **errp);
1027
1028 /**
1029 * memory_region_set_log: Turn dirty logging on or off for a region.
1030 *
1031 * Turns dirty logging on or off for a specified client (display, migration).
1032 * Only meaningful for RAM regions.
1033 *
1034 * @mr: the memory region being updated.
1035 * @log: whether dirty logging is to be enabled or disabled.
1036 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1037 */
1038 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1039
1040 /**
1041 * memory_region_get_dirty: Check whether a range of bytes is dirty
1042 * for a specified client.
1043 *
1044 * Checks whether a range of bytes has been written to since the last
1045 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1046 * must be enabled.
1047 *
1048 * @mr: the memory region being queried.
1049 * @addr: the address (relative to the start of the region) being queried.
1050 * @size: the size of the range being queried.
1051 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1052 * %DIRTY_MEMORY_VGA.
1053 */
1054 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1055 hwaddr size, unsigned client);
1056
1057 /**
1058 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1059 *
1060 * Marks a range of bytes as dirty, after it has been dirtied outside
1061 * guest code.
1062 *
1063 * @mr: the memory region being dirtied.
1064 * @addr: the address (relative to the start of the region) being dirtied.
1065 * @size: size of the range being dirtied.
1066 */
1067 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1068 hwaddr size);
1069
1070 /**
1071 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
1072 * for a specified client. It clears them.
1073 *
1074 * Checks whether a range of bytes has been written to since the last
1075 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1076 * must be enabled.
1077 *
1078 * @mr: the memory region being queried.
1079 * @addr: the address (relative to the start of the region) being queried.
1080 * @size: the size of the range being queried.
1081 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1082 * %DIRTY_MEMORY_VGA.
1083 */
1084 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1085 hwaddr size, unsigned client);
1086
1087 /**
1088 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1089 * bitmap and clear it.
1090 *
1091 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1092 * returns the snapshot. The snapshot can then be used to query dirty
1093 * status, using memory_region_snapshot_get_dirty. Unlike
1094 * memory_region_test_and_clear_dirty this allows to query the same
1095 * page multiple times, which is especially useful for display updates
1096 * where the scanlines often are not page aligned.
1097 *
1098 * The dirty bitmap region which gets copyed into the snapshot (and
1099 * cleared afterwards) can be larger than requested. The boundaries
1100 * are rounded up/down so complete bitmap longs (covering 64 pages on
1101 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1102 * isn't a problem for display updates as the extra pages are outside
1103 * the visible area, and in case the visible area changes a full
1104 * display redraw is due anyway. Should other use cases for this
1105 * function emerge we might have to revisit this implementation
1106 * detail.
1107 *
1108 * Use g_free to release DirtyBitmapSnapshot.
1109 *
1110 * @mr: the memory region being queried.
1111 * @addr: the address (relative to the start of the region) being queried.
1112 * @size: the size of the range being queried.
1113 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1114 */
1115 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1116 hwaddr addr,
1117 hwaddr size,
1118 unsigned client);
1119
1120 /**
1121 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1122 * in the specified dirty bitmap snapshot.
1123 *
1124 * @mr: the memory region being queried.
1125 * @snap: the dirty bitmap snapshot
1126 * @addr: the address (relative to the start of the region) being queried.
1127 * @size: the size of the range being queried.
1128 */
1129 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1130 DirtyBitmapSnapshot *snap,
1131 hwaddr addr, hwaddr size);
1132
1133 /**
1134 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
1135 * any external TLBs (e.g. kvm)
1136 *
1137 * Flushes dirty information from accelerators such as kvm and vhost-net
1138 * and makes it available to users of the memory API.
1139 *
1140 * @mr: the region being flushed.
1141 */
1142 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
1143
1144 /**
1145 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1146 * client.
1147 *
1148 * Marks a range of pages as no longer dirty.
1149 *
1150 * @mr: the region being updated.
1151 * @addr: the start of the subrange being cleaned.
1152 * @size: the size of the subrange being cleaned.
1153 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1154 * %DIRTY_MEMORY_VGA.
1155 */
1156 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1157 hwaddr size, unsigned client);
1158
1159 /**
1160 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1161 *
1162 * Allows a memory region to be marked as read-only (turning it into a ROM).
1163 * only useful on RAM regions.
1164 *
1165 * @mr: the region being updated.
1166 * @readonly: whether rhe region is to be ROM or RAM.
1167 */
1168 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1169
1170 /**
1171 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1172 *
1173 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1174 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1175 * device is mapped to guest memory and satisfies read access directly.
1176 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1177 * Writes are always handled by the #MemoryRegion.write function.
1178 *
1179 * @mr: the memory region to be updated
1180 * @romd_mode: %true to put the region into ROMD mode
1181 */
1182 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1183
1184 /**
1185 * memory_region_set_coalescing: Enable memory coalescing for the region.
1186 *
1187 * Enabled writes to a region to be queued for later processing. MMIO ->write
1188 * callbacks may be delayed until a non-coalesced MMIO is issued.
1189 * Only useful for IO regions. Roughly similar to write-combining hardware.
1190 *
1191 * @mr: the memory region to be write coalesced
1192 */
1193 void memory_region_set_coalescing(MemoryRegion *mr);
1194
1195 /**
1196 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1197 * a region.
1198 *
1199 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1200 * Multiple calls can be issued coalesced disjoint ranges.
1201 *
1202 * @mr: the memory region to be updated.
1203 * @offset: the start of the range within the region to be coalesced.
1204 * @size: the size of the subrange to be coalesced.
1205 */
1206 void memory_region_add_coalescing(MemoryRegion *mr,
1207 hwaddr offset,
1208 uint64_t size);
1209
1210 /**
1211 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1212 *
1213 * Disables any coalescing caused by memory_region_set_coalescing() or
1214 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1215 * hardware.
1216 *
1217 * @mr: the memory region to be updated.
1218 */
1219 void memory_region_clear_coalescing(MemoryRegion *mr);
1220
1221 /**
1222 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1223 * accesses.
1224 *
1225 * Ensure that pending coalesced MMIO request are flushed before the memory
1226 * region is accessed. This property is automatically enabled for all regions
1227 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1228 *
1229 * @mr: the memory region to be updated.
1230 */
1231 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1232
1233 /**
1234 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1235 * accesses.
1236 *
1237 * Clear the automatic coalesced MMIO flushing enabled via
1238 * memory_region_set_flush_coalesced. Note that this service has no effect on
1239 * memory regions that have MMIO coalescing enabled for themselves. For them,
1240 * automatic flushing will stop once coalescing is disabled.
1241 *
1242 * @mr: the memory region to be updated.
1243 */
1244 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1245
1246 /**
1247 * memory_region_set_global_locking: Declares the access processing requires
1248 * QEMU's global lock.
1249 *
1250 * When this is invoked, accesses to the memory region will be processed while
1251 * holding the global lock of QEMU. This is the default behavior of memory
1252 * regions.
1253 *
1254 * @mr: the memory region to be updated.
1255 */
1256 void memory_region_set_global_locking(MemoryRegion *mr);
1257
1258 /**
1259 * memory_region_clear_global_locking: Declares that access processing does
1260 * not depend on the QEMU global lock.
1261 *
1262 * By clearing this property, accesses to the memory region will be processed
1263 * outside of QEMU's global lock (unless the lock is held on when issuing the
1264 * access request). In this case, the device model implementing the access
1265 * handlers is responsible for synchronization of concurrency.
1266 *
1267 * @mr: the memory region to be updated.
1268 */
1269 void memory_region_clear_global_locking(MemoryRegion *mr);
1270
1271 /**
1272 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1273 * is written to a location.
1274 *
1275 * Marks a word in an IO region (initialized with memory_region_init_io())
1276 * as a trigger for an eventfd event. The I/O callback will not be called.
1277 * The caller must be prepared to handle failure (that is, take the required
1278 * action if the callback _is_ called).
1279 *
1280 * @mr: the memory region being updated.
1281 * @addr: the address within @mr that is to be monitored
1282 * @size: the size of the access to trigger the eventfd
1283 * @match_data: whether to match against @data, instead of just @addr
1284 * @data: the data to match against the guest write
1285 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1286 **/
1287 void memory_region_add_eventfd(MemoryRegion *mr,
1288 hwaddr addr,
1289 unsigned size,
1290 bool match_data,
1291 uint64_t data,
1292 EventNotifier *e);
1293
1294 /**
1295 * memory_region_del_eventfd: Cancel an eventfd.
1296 *
1297 * Cancels an eventfd trigger requested by a previous
1298 * memory_region_add_eventfd() call.
1299 *
1300 * @mr: the memory region being updated.
1301 * @addr: the address within @mr that is to be monitored
1302 * @size: the size of the access to trigger the eventfd
1303 * @match_data: whether to match against @data, instead of just @addr
1304 * @data: the data to match against the guest write
1305 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1306 */
1307 void memory_region_del_eventfd(MemoryRegion *mr,
1308 hwaddr addr,
1309 unsigned size,
1310 bool match_data,
1311 uint64_t data,
1312 EventNotifier *e);
1313
1314 /**
1315 * memory_region_add_subregion: Add a subregion to a container.
1316 *
1317 * Adds a subregion at @offset. The subregion may not overlap with other
1318 * subregions (except for those explicitly marked as overlapping). A region
1319 * may only be added once as a subregion (unless removed with
1320 * memory_region_del_subregion()); use memory_region_init_alias() if you
1321 * want a region to be a subregion in multiple locations.
1322 *
1323 * @mr: the region to contain the new subregion; must be a container
1324 * initialized with memory_region_init().
1325 * @offset: the offset relative to @mr where @subregion is added.
1326 * @subregion: the subregion to be added.
1327 */
1328 void memory_region_add_subregion(MemoryRegion *mr,
1329 hwaddr offset,
1330 MemoryRegion *subregion);
1331 /**
1332 * memory_region_add_subregion_overlap: Add a subregion to a container
1333 * with overlap.
1334 *
1335 * Adds a subregion at @offset. The subregion may overlap with other
1336 * subregions. Conflicts are resolved by having a higher @priority hide a
1337 * lower @priority. Subregions without priority are taken as @priority 0.
1338 * A region may only be added once as a subregion (unless removed with
1339 * memory_region_del_subregion()); use memory_region_init_alias() if you
1340 * want a region to be a subregion in multiple locations.
1341 *
1342 * @mr: the region to contain the new subregion; must be a container
1343 * initialized with memory_region_init().
1344 * @offset: the offset relative to @mr where @subregion is added.
1345 * @subregion: the subregion to be added.
1346 * @priority: used for resolving overlaps; highest priority wins.
1347 */
1348 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1349 hwaddr offset,
1350 MemoryRegion *subregion,
1351 int priority);
1352
1353 /**
1354 * memory_region_get_ram_addr: Get the ram address associated with a memory
1355 * region
1356 */
1357 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1358
1359 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1360 /**
1361 * memory_region_del_subregion: Remove a subregion.
1362 *
1363 * Removes a subregion from its container.
1364 *
1365 * @mr: the container to be updated.
1366 * @subregion: the region being removed; must be a current subregion of @mr.
1367 */
1368 void memory_region_del_subregion(MemoryRegion *mr,
1369 MemoryRegion *subregion);
1370
1371 /*
1372 * memory_region_set_enabled: dynamically enable or disable a region
1373 *
1374 * Enables or disables a memory region. A disabled memory region
1375 * ignores all accesses to itself and its subregions. It does not
1376 * obscure sibling subregions with lower priority - it simply behaves as
1377 * if it was removed from the hierarchy.
1378 *
1379 * Regions default to being enabled.
1380 *
1381 * @mr: the region to be updated
1382 * @enabled: whether to enable or disable the region
1383 */
1384 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1385
1386 /*
1387 * memory_region_set_address: dynamically update the address of a region
1388 *
1389 * Dynamically updates the address of a region, relative to its container.
1390 * May be used on regions are currently part of a memory hierarchy.
1391 *
1392 * @mr: the region to be updated
1393 * @addr: new address, relative to container region
1394 */
1395 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1396
1397 /*
1398 * memory_region_set_size: dynamically update the size of a region.
1399 *
1400 * Dynamically updates the size of a region.
1401 *
1402 * @mr: the region to be updated
1403 * @size: used size of the region.
1404 */
1405 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1406
1407 /*
1408 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1409 *
1410 * Dynamically updates the offset into the target region that an alias points
1411 * to, as if the fourth argument to memory_region_init_alias() has changed.
1412 *
1413 * @mr: the #MemoryRegion to be updated; should be an alias.
1414 * @offset: the new offset into the target memory region
1415 */
1416 void memory_region_set_alias_offset(MemoryRegion *mr,
1417 hwaddr offset);
1418
1419 /**
1420 * memory_region_present: checks if an address relative to a @container
1421 * translates into #MemoryRegion within @container
1422 *
1423 * Answer whether a #MemoryRegion within @container covers the address
1424 * @addr.
1425 *
1426 * @container: a #MemoryRegion within which @addr is a relative address
1427 * @addr: the area within @container to be searched
1428 */
1429 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1430
1431 /**
1432 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1433 * into any address space.
1434 *
1435 * @mr: a #MemoryRegion which should be checked if it's mapped
1436 */
1437 bool memory_region_is_mapped(MemoryRegion *mr);
1438
1439 /**
1440 * memory_region_find: translate an address/size relative to a
1441 * MemoryRegion into a #MemoryRegionSection.
1442 *
1443 * Locates the first #MemoryRegion within @mr that overlaps the range
1444 * given by @addr and @size.
1445 *
1446 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1447 * It will have the following characteristics:
1448 * .@size = 0 iff no overlap was found
1449 * .@mr is non-%NULL iff an overlap was found
1450 *
1451 * Remember that in the return value the @offset_within_region is
1452 * relative to the returned region (in the .@mr field), not to the
1453 * @mr argument.
1454 *
1455 * Similarly, the .@offset_within_address_space is relative to the
1456 * address space that contains both regions, the passed and the
1457 * returned one. However, in the special case where the @mr argument
1458 * has no container (and thus is the root of the address space), the
1459 * following will hold:
1460 * .@offset_within_address_space >= @addr
1461 * .@offset_within_address_space + .@size <= @addr + @size
1462 *
1463 * @mr: a MemoryRegion within which @addr is a relative address
1464 * @addr: start of the area within @as to be searched
1465 * @size: size of the area to be searched
1466 */
1467 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1468 hwaddr addr, uint64_t size);
1469
1470 /**
1471 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1472 *
1473 * Synchronizes the dirty page log for all address spaces.
1474 */
1475 void memory_global_dirty_log_sync(void);
1476
1477 /**
1478 * memory_region_transaction_begin: Start a transaction.
1479 *
1480 * During a transaction, changes will be accumulated and made visible
1481 * only when the transaction ends (is committed).
1482 */
1483 void memory_region_transaction_begin(void);
1484
1485 /**
1486 * memory_region_transaction_commit: Commit a transaction and make changes
1487 * visible to the guest.
1488 */
1489 void memory_region_transaction_commit(void);
1490
1491 /**
1492 * memory_listener_register: register callbacks to be called when memory
1493 * sections are mapped or unmapped into an address
1494 * space
1495 *
1496 * @listener: an object containing the callbacks to be called
1497 * @filter: if non-%NULL, only regions in this address space will be observed
1498 */
1499 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1500
1501 /**
1502 * memory_listener_unregister: undo the effect of memory_listener_register()
1503 *
1504 * @listener: an object containing the callbacks to be removed
1505 */
1506 void memory_listener_unregister(MemoryListener *listener);
1507
1508 /**
1509 * memory_global_dirty_log_start: begin dirty logging for all regions
1510 */
1511 void memory_global_dirty_log_start(void);
1512
1513 /**
1514 * memory_global_dirty_log_stop: end dirty logging for all regions
1515 */
1516 void memory_global_dirty_log_stop(void);
1517
1518 void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
1519 bool dispatch_tree);
1520
1521 /**
1522 * memory_region_request_mmio_ptr: request a pointer to an mmio
1523 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1524 * When the device wants to invalidate the pointer it will call
1525 * memory_region_invalidate_mmio_ptr.
1526 *
1527 * @mr: #MemoryRegion to check
1528 * @addr: address within that region
1529 *
1530 * Returns true on success, false otherwise.
1531 */
1532 bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr);
1533
1534 /**
1535 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1536 * previously requested.
1537 * In the end that means that if something wants to execute from this area it
1538 * will need to request the pointer again.
1539 *
1540 * @mr: #MemoryRegion associated to the pointer.
1541 * @addr: address within that region
1542 * @size: size of that area.
1543 */
1544 void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
1545 unsigned size);
1546
1547 /**
1548 * memory_region_dispatch_read: perform a read directly to the specified
1549 * MemoryRegion.
1550 *
1551 * @mr: #MemoryRegion to access
1552 * @addr: address within that region
1553 * @pval: pointer to uint64_t which the data is written to
1554 * @size: size of the access in bytes
1555 * @attrs: memory transaction attributes to use for the access
1556 */
1557 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1558 hwaddr addr,
1559 uint64_t *pval,
1560 unsigned size,
1561 MemTxAttrs attrs);
1562 /**
1563 * memory_region_dispatch_write: perform a write directly to the specified
1564 * MemoryRegion.
1565 *
1566 * @mr: #MemoryRegion to access
1567 * @addr: address within that region
1568 * @data: data to write
1569 * @size: size of the access in bytes
1570 * @attrs: memory transaction attributes to use for the access
1571 */
1572 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1573 hwaddr addr,
1574 uint64_t data,
1575 unsigned size,
1576 MemTxAttrs attrs);
1577
1578 /**
1579 * address_space_init: initializes an address space
1580 *
1581 * @as: an uninitialized #AddressSpace
1582 * @root: a #MemoryRegion that routes addresses for the address space
1583 * @name: an address space name. The name is only used for debugging
1584 * output.
1585 */
1586 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1587
1588 /**
1589 * address_space_init_shareable: return an address space for a memory region,
1590 * creating it if it does not already exist
1591 *
1592 * @root: a #MemoryRegion that routes addresses for the address space
1593 * @name: an address space name. The name is only used for debugging
1594 * output.
1595 *
1596 * This function will return a pointer to an existing AddressSpace
1597 * which was initialized with the specified MemoryRegion, or it will
1598 * create and initialize one if it does not already exist. The ASes
1599 * are reference-counted, so the memory will be freed automatically
1600 * when the AddressSpace is destroyed via address_space_destroy.
1601 */
1602 AddressSpace *address_space_init_shareable(MemoryRegion *root,
1603 const char *name);
1604
1605 /**
1606 * address_space_destroy: destroy an address space
1607 *
1608 * Releases all resources associated with an address space. After an address space
1609 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1610 * as well.
1611 *
1612 * @as: address space to be destroyed
1613 */
1614 void address_space_destroy(AddressSpace *as);
1615
1616 /**
1617 * address_space_rw: read from or write to an address space.
1618 *
1619 * Return a MemTxResult indicating whether the operation succeeded
1620 * or failed (eg unassigned memory, device rejected the transaction,
1621 * IOMMU fault).
1622 *
1623 * @as: #AddressSpace to be accessed
1624 * @addr: address within that address space
1625 * @attrs: memory transaction attributes
1626 * @buf: buffer with the data transferred
1627 * @is_write: indicates the transfer direction
1628 */
1629 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1630 MemTxAttrs attrs, uint8_t *buf,
1631 int len, bool is_write);
1632
1633 /**
1634 * address_space_write: write to address space.
1635 *
1636 * Return a MemTxResult indicating whether the operation succeeded
1637 * or failed (eg unassigned memory, device rejected the transaction,
1638 * IOMMU fault).
1639 *
1640 * @as: #AddressSpace to be accessed
1641 * @addr: address within that address space
1642 * @attrs: memory transaction attributes
1643 * @buf: buffer with the data transferred
1644 */
1645 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1646 MemTxAttrs attrs,
1647 const uint8_t *buf, int len);
1648
1649 /* address_space_ld*: load from an address space
1650 * address_space_st*: store to an address space
1651 *
1652 * These functions perform a load or store of the byte, word,
1653 * longword or quad to the specified address within the AddressSpace.
1654 * The _le suffixed functions treat the data as little endian;
1655 * _be indicates big endian; no suffix indicates "same endianness
1656 * as guest CPU".
1657 *
1658 * The "guest CPU endianness" accessors are deprecated for use outside
1659 * target-* code; devices should be CPU-agnostic and use either the LE
1660 * or the BE accessors.
1661 *
1662 * @as #AddressSpace to be accessed
1663 * @addr: address within that address space
1664 * @val: data value, for stores
1665 * @attrs: memory transaction attributes
1666 * @result: location to write the success/failure of the transaction;
1667 * if NULL, this information is discarded
1668 */
1669 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1670 MemTxAttrs attrs, MemTxResult *result);
1671 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1672 MemTxAttrs attrs, MemTxResult *result);
1673 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1674 MemTxAttrs attrs, MemTxResult *result);
1675 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1676 MemTxAttrs attrs, MemTxResult *result);
1677 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1678 MemTxAttrs attrs, MemTxResult *result);
1679 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1680 MemTxAttrs attrs, MemTxResult *result);
1681 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1682 MemTxAttrs attrs, MemTxResult *result);
1683 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1684 MemTxAttrs attrs, MemTxResult *result);
1685 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1686 MemTxAttrs attrs, MemTxResult *result);
1687 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1688 MemTxAttrs attrs, MemTxResult *result);
1689 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1690 MemTxAttrs attrs, MemTxResult *result);
1691 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1692 MemTxAttrs attrs, MemTxResult *result);
1693 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1694 MemTxAttrs attrs, MemTxResult *result);
1695 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1696 MemTxAttrs attrs, MemTxResult *result);
1697
1698 uint32_t ldub_phys(AddressSpace *as, hwaddr addr);
1699 uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr);
1700 uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr);
1701 uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr);
1702 uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr);
1703 uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr);
1704 uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr);
1705 void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1706 void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1707 void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1708 void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1709 void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1710 void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1711 void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1712
1713 struct MemoryRegionCache {
1714 hwaddr xlat;
1715 hwaddr len;
1716 AddressSpace *as;
1717 };
1718
1719 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1720
1721 /* address_space_cache_init: prepare for repeated access to a physical
1722 * memory region
1723 *
1724 * @cache: #MemoryRegionCache to be filled
1725 * @as: #AddressSpace to be accessed
1726 * @addr: address within that address space
1727 * @len: length of buffer
1728 * @is_write: indicates the transfer direction
1729 *
1730 * Will only work with RAM, and may map a subset of the requested range by
1731 * returning a value that is less than @len. On failure, return a negative
1732 * errno value.
1733 *
1734 * Because it only works with RAM, this function can be used for
1735 * read-modify-write operations. In this case, is_write should be %true.
1736 *
1737 * Note that addresses passed to the address_space_*_cached functions
1738 * are relative to @addr.
1739 */
1740 int64_t address_space_cache_init(MemoryRegionCache *cache,
1741 AddressSpace *as,
1742 hwaddr addr,
1743 hwaddr len,
1744 bool is_write);
1745
1746 /**
1747 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1748 *
1749 * @cache: The #MemoryRegionCache to operate on.
1750 * @addr: The first physical address that was written, relative to the
1751 * address that was passed to @address_space_cache_init.
1752 * @access_len: The number of bytes that were written starting at @addr.
1753 */
1754 void address_space_cache_invalidate(MemoryRegionCache *cache,
1755 hwaddr addr,
1756 hwaddr access_len);
1757
1758 /**
1759 * address_space_cache_destroy: free a #MemoryRegionCache
1760 *
1761 * @cache: The #MemoryRegionCache whose memory should be released.
1762 */
1763 void address_space_cache_destroy(MemoryRegionCache *cache);
1764
1765 /* address_space_ld*_cached: load from a cached #MemoryRegion
1766 * address_space_st*_cached: store into a cached #MemoryRegion
1767 *
1768 * These functions perform a load or store of the byte, word,
1769 * longword or quad to the specified address. The address is
1770 * a physical address in the AddressSpace, but it must lie within
1771 * a #MemoryRegion that was mapped with address_space_cache_init.
1772 *
1773 * The _le suffixed functions treat the data as little endian;
1774 * _be indicates big endian; no suffix indicates "same endianness
1775 * as guest CPU".
1776 *
1777 * The "guest CPU endianness" accessors are deprecated for use outside
1778 * target-* code; devices should be CPU-agnostic and use either the LE
1779 * or the BE accessors.
1780 *
1781 * @cache: previously initialized #MemoryRegionCache to be accessed
1782 * @addr: address within the address space
1783 * @val: data value, for stores
1784 * @attrs: memory transaction attributes
1785 * @result: location to write the success/failure of the transaction;
1786 * if NULL, this information is discarded
1787 */
1788 uint32_t address_space_ldub_cached(MemoryRegionCache *cache, hwaddr addr,
1789 MemTxAttrs attrs, MemTxResult *result);
1790 uint32_t address_space_lduw_le_cached(MemoryRegionCache *cache, hwaddr addr,
1791 MemTxAttrs attrs, MemTxResult *result);
1792 uint32_t address_space_lduw_be_cached(MemoryRegionCache *cache, hwaddr addr,
1793 MemTxAttrs attrs, MemTxResult *result);
1794 uint32_t address_space_ldl_le_cached(MemoryRegionCache *cache, hwaddr addr,
1795 MemTxAttrs attrs, MemTxResult *result);
1796 uint32_t address_space_ldl_be_cached(MemoryRegionCache *cache, hwaddr addr,
1797 MemTxAttrs attrs, MemTxResult *result);
1798 uint64_t address_space_ldq_le_cached(MemoryRegionCache *cache, hwaddr addr,
1799 MemTxAttrs attrs, MemTxResult *result);
1800 uint64_t address_space_ldq_be_cached(MemoryRegionCache *cache, hwaddr addr,
1801 MemTxAttrs attrs, MemTxResult *result);
1802 void address_space_stb_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1803 MemTxAttrs attrs, MemTxResult *result);
1804 void address_space_stw_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1805 MemTxAttrs attrs, MemTxResult *result);
1806 void address_space_stw_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1807 MemTxAttrs attrs, MemTxResult *result);
1808 void address_space_stl_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1809 MemTxAttrs attrs, MemTxResult *result);
1810 void address_space_stl_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1811 MemTxAttrs attrs, MemTxResult *result);
1812 void address_space_stq_le_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val,
1813 MemTxAttrs attrs, MemTxResult *result);
1814 void address_space_stq_be_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val,
1815 MemTxAttrs attrs, MemTxResult *result);
1816
1817 uint32_t ldub_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1818 uint32_t lduw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1819 uint32_t lduw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1820 uint32_t ldl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1821 uint32_t ldl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1822 uint64_t ldq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1823 uint64_t ldq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1824 void stb_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1825 void stw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1826 void stw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1827 void stl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1828 void stl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1829 void stq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val);
1830 void stq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val);
1831 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1832 * entry. Should be called from an RCU critical section.
1833 */
1834 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
1835 bool is_write);
1836
1837 /* address_space_translate: translate an address range into an address space
1838 * into a MemoryRegion and an address range into that section. Should be
1839 * called from an RCU critical section, to avoid that the last reference
1840 * to the returned region disappears after address_space_translate returns.
1841 *
1842 * @as: #AddressSpace to be accessed
1843 * @addr: address within that address space
1844 * @xlat: pointer to address within the returned memory region section's
1845 * #MemoryRegion.
1846 * @len: pointer to length
1847 * @is_write: indicates the transfer direction
1848 */
1849 MemoryRegion *flatview_translate(FlatView *fv,
1850 hwaddr addr, hwaddr *xlat,
1851 hwaddr *len, bool is_write);
1852
1853 static inline MemoryRegion *address_space_translate(AddressSpace *as,
1854 hwaddr addr, hwaddr *xlat,
1855 hwaddr *len, bool is_write)
1856 {
1857 return flatview_translate(address_space_to_flatview(as),
1858 addr, xlat, len, is_write);
1859 }
1860
1861 /* address_space_access_valid: check for validity of accessing an address
1862 * space range
1863 *
1864 * Check whether memory is assigned to the given address space range, and
1865 * access is permitted by any IOMMU regions that are active for the address
1866 * space.
1867 *
1868 * For now, addr and len should be aligned to a page size. This limitation
1869 * will be lifted in the future.
1870 *
1871 * @as: #AddressSpace to be accessed
1872 * @addr: address within that address space
1873 * @len: length of the area to be checked
1874 * @is_write: indicates the transfer direction
1875 */
1876 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1877
1878 /* address_space_map: map a physical memory region into a host virtual address
1879 *
1880 * May map a subset of the requested range, given by and returned in @plen.
1881 * May return %NULL if resources needed to perform the mapping are exhausted.
1882 * Use only for reads OR writes - not for read-modify-write operations.
1883 * Use cpu_register_map_client() to know when retrying the map operation is
1884 * likely to succeed.
1885 *
1886 * @as: #AddressSpace to be accessed
1887 * @addr: address within that address space
1888 * @plen: pointer to length of buffer; updated on return
1889 * @is_write: indicates the transfer direction
1890 */
1891 void *address_space_map(AddressSpace *as, hwaddr addr,
1892 hwaddr *plen, bool is_write);
1893
1894 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1895 *
1896 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1897 * the amount of memory that was actually read or written by the caller.
1898 *
1899 * @as: #AddressSpace used
1900 * @addr: address within that address space
1901 * @len: buffer length as returned by address_space_map()
1902 * @access_len: amount of data actually transferred
1903 * @is_write: indicates the transfer direction
1904 */
1905 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1906 int is_write, hwaddr access_len);
1907
1908
1909 /* Internal functions, part of the implementation of address_space_read. */
1910 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
1911 MemTxAttrs attrs, uint8_t *buf,
1912 int len, hwaddr addr1, hwaddr l,
1913 MemoryRegion *mr);
1914
1915 MemTxResult flatview_read_full(FlatView *fv, hwaddr addr,
1916 MemTxAttrs attrs, uint8_t *buf, int len);
1917 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1918
1919 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1920 {
1921 if (is_write) {
1922 return memory_region_is_ram(mr) &&
1923 !mr->readonly && !memory_region_is_ram_device(mr);
1924 } else {
1925 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
1926 memory_region_is_romd(mr);
1927 }
1928 }
1929
1930 /**
1931 * address_space_read: read from an address space.
1932 *
1933 * Return a MemTxResult indicating whether the operation succeeded
1934 * or failed (eg unassigned memory, device rejected the transaction,
1935 * IOMMU fault).
1936 *
1937 * @as: #AddressSpace to be accessed
1938 * @addr: address within that address space
1939 * @attrs: memory transaction attributes
1940 * @buf: buffer with the data transferred
1941 */
1942 static inline __attribute__((__always_inline__))
1943 MemTxResult flatview_read(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
1944 uint8_t *buf, int len)
1945 {
1946 MemTxResult result = MEMTX_OK;
1947 hwaddr l, addr1;
1948 void *ptr;
1949 MemoryRegion *mr;
1950
1951 if (__builtin_constant_p(len)) {
1952 if (len) {
1953 rcu_read_lock();
1954 l = len;
1955 mr = flatview_translate(fv, addr, &addr1, &l, false);
1956 if (len == l && memory_access_is_direct(mr, false)) {
1957 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
1958 memcpy(buf, ptr, len);
1959 } else {
1960 result = flatview_read_continue(fv, addr, attrs, buf, len,
1961 addr1, l, mr);
1962 }
1963 rcu_read_unlock();
1964 }
1965 } else {
1966 result = flatview_read_full(fv, addr, attrs, buf, len);
1967 }
1968 return result;
1969 }
1970
1971 static inline MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
1972 MemTxAttrs attrs, uint8_t *buf,
1973 int len)
1974 {
1975 return flatview_read(address_space_to_flatview(as), addr, attrs, buf, len);
1976 }
1977
1978 /**
1979 * address_space_read_cached: read from a cached RAM region
1980 *
1981 * @cache: Cached region to be addressed
1982 * @addr: address relative to the base of the RAM region
1983 * @buf: buffer with the data transferred
1984 * @len: length of the data transferred
1985 */
1986 static inline void
1987 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
1988 void *buf, int len)
1989 {
1990 assert(addr < cache->len && len <= cache->len - addr);
1991 address_space_read(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len);
1992 }
1993
1994 /**
1995 * address_space_write_cached: write to a cached RAM region
1996 *
1997 * @cache: Cached region to be addressed
1998 * @addr: address relative to the base of the RAM region
1999 * @buf: buffer with the data transferred
2000 * @len: length of the data transferred
2001 */
2002 static inline void
2003 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2004 void *buf, int len)
2005 {
2006 assert(addr < cache->len && len <= cache->len - addr);
2007 address_space_write(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len);
2008 }
2009
2010 #endif
2011
2012 #endif