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