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